blob: 6251c45aeb3c4f09c16abd901cec8f8abb731922 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Amith Yamasani2bbdd772011-02-02 18:54:13 -080019import com.android.internal.R;
20
Eric Laurentc34dcc12012-09-10 13:51:52 -070021import android.app.AlertDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080022import android.app.Dialog;
23import android.content.DialogInterface.OnDismissListener;
24import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080026import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080028import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.Resources;
30import android.media.AudioManager;
31import android.media.AudioService;
32import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070033import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.media.ToneGenerator;
Marco Nelissen69f593c2009-07-28 09:55:04 -070035import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Handler;
37import android.os.Message;
38import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.util.Log;
Amith Yamasani284e6302011-09-16 18:24:47 -070040import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080042import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080043import android.widget.SeekBar.OnSeekBarChangeListener;
44
45import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47/**
48 * Handle the volume up and down keys.
49 *
50 * This code really should be moved elsewhere.
51 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070052 * Seriously, it really really should be moved elsewhere. This is used by
53 * android.media.AudioService, which actually runs in the system process, to
54 * show the volume dialog when the user changes the volume. What a mess.
55 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * @hide
57 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080058public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059{
60 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070061 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 /**
64 * The delay before playing a sound. This small period exists so the user
65 * can press another key (non-volume keys, too) to have it NOT be audible.
66 * <p>
67 * PhoneWindow will implement this part.
68 */
69 public static final int PLAY_SOUND_DELAY = 300;
70
71 /**
72 * The delay before vibrating. This small period exists so if the user is
73 * moving to silent mode, it will not emit a short vibrate (it normally
74 * would since vibrate is between normal mode and silent mode using hardware
75 * keys).
76 */
77 public static final int VIBRATE_DELAY = 300;
78
79 private static final int VIBRATE_DURATION = 300;
80 private static final int BEEP_DURATION = 150;
81 private static final int MAX_VOLUME = 100;
82 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080083 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 private static final int MSG_VOLUME_CHANGED = 0;
86 private static final int MSG_FREE_RESOURCES = 1;
87 private static final int MSG_PLAY_SOUND = 2;
88 private static final int MSG_STOP_SOUNDS = 3;
89 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080090 private static final int MSG_TIMEOUT = 5;
91 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -080092 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -070093 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
94 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
95 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -070096 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040098 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -080099 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700100 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 protected Context mContext;
103 private AudioManager mAudioManager;
104 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700105 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700106 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700107 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Christopher Tatec4b78d22012-05-22 13:57:58 -0700109 // True if we want to play tones on the system stream when the master stream is specified.
110 private final boolean mPlayMasterStreamTones;
111
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800112 /** Dialog containing all the sliders */
113 private final Dialog mDialog;
114 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800116
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700117 /** The visible portion of the volume overlay */
118 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800119 /** Contains the sliders and their touchable icons */
120 private final ViewGroup mSliderGroup;
121 /** The button that expands the dialog to show all sliders */
122 private final View mMoreButton;
123 /** Dummy divider icon that needs to vanish with the more button */
124 private final View mDivider;
125
126 /** Currently active stream that shows up at the top of the list of sliders */
127 private int mActiveStreamType = -1;
128 /** All the slider controls mapped by stream type */
129 private HashMap<Integer,StreamControl> mStreamControls;
130
Amith Yamasani71def772011-10-12 12:25:24 -0700131 private enum StreamResources {
132 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
133 R.string.volume_icon_description_bluetooth,
134 R.drawable.ic_audio_bt,
135 R.drawable.ic_audio_bt,
136 false),
137 RingerStream(AudioManager.STREAM_RING,
138 R.string.volume_icon_description_ringer,
139 R.drawable.ic_audio_ring_notif,
140 R.drawable.ic_audio_ring_notif_mute,
141 false),
142 VoiceStream(AudioManager.STREAM_VOICE_CALL,
143 R.string.volume_icon_description_incall,
144 R.drawable.ic_audio_phone,
145 R.drawable.ic_audio_phone,
146 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700147 AlarmStream(AudioManager.STREAM_ALARM,
148 R.string.volume_alarm,
149 R.drawable.ic_audio_alarm,
150 R.drawable.ic_audio_alarm_mute,
151 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700152 MediaStream(AudioManager.STREAM_MUSIC,
153 R.string.volume_icon_description_media,
154 R.drawable.ic_audio_vol,
155 R.drawable.ic_audio_vol_mute,
156 true),
157 NotificationStream(AudioManager.STREAM_NOTIFICATION,
158 R.string.volume_icon_description_notification,
159 R.drawable.ic_audio_notification,
160 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400161 true),
162 // for now, use media resources for master volume
163 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700164 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400165 R.drawable.ic_audio_vol,
166 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700167 false),
168 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
169 R.string.volume_icon_description_media, //FIXME should have its own description
170 R.drawable.ic_media_route_on_holo_dark,
171 R.drawable.ic_media_route_disabled_holo_dark,
172 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700173
174 int streamType;
175 int descRes;
176 int iconRes;
177 int iconMuteRes;
178 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
179 boolean show;
180
181 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
182 this.streamType = streamType;
183 this.descRes = descRes;
184 this.iconRes = iconRes;
185 this.iconMuteRes = iconMuteRes;
186 this.show = show;
187 }
188 };
189
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800190 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700191 private static final StreamResources[] STREAMS = {
192 StreamResources.BluetoothSCOStream,
193 StreamResources.RingerStream,
194 StreamResources.VoiceStream,
195 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700196 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400197 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700198 StreamResources.MasterStream,
199 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800200 };
201
202 /** Object that contains data for each slider */
203 private class StreamControl {
204 int streamType;
205 ViewGroup group;
206 ImageView icon;
207 SeekBar seekbarView;
208 int iconRes;
209 int iconMuteRes;
210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
212 // Synchronize when accessing this
213 private ToneGenerator mToneGenerators[];
214 private Vibrator mVibrator;
215
Eric Laurentc34dcc12012-09-10 13:51:52 -0700216 private static AlertDialog sConfirmSafeVolumeDialog;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700217 private static Object sConfirmSafeVolumeLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700218
219 private static class WarningDialogReceiver extends BroadcastReceiver
220 implements DialogInterface.OnDismissListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800221 private final Context mContext;
222 private final Dialog mDialog;
223 private final VolumePanel mVolumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700224
Eric Laurentfde16d52012-12-03 14:42:39 -0800225 WarningDialogReceiver(Context context, Dialog dialog, VolumePanel volumePanel) {
Eric Laurentc34dcc12012-09-10 13:51:52 -0700226 mContext = context;
227 mDialog = dialog;
Eric Laurentfde16d52012-12-03 14:42:39 -0800228 mVolumePanel = volumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700229 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
230 context.registerReceiver(this, filter);
231 }
232
233 @Override
234 public void onReceive(Context context, Intent intent) {
235 mDialog.cancel();
Eric Laurentfde16d52012-12-03 14:42:39 -0800236 cleanUp();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700237 }
238
239 public void onDismiss(DialogInterface unused) {
240 mContext.unregisterReceiver(this);
Eric Laurentfde16d52012-12-03 14:42:39 -0800241 cleanUp();
242 }
243
244 private void cleanUp() {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700245 synchronized (sConfirmSafeVolumeLock) {
246 sConfirmSafeVolumeDialog = null;
247 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800248 mVolumePanel.forceTimeout();
249 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700250 }
251 }
252
253
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800254 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 mContext = context;
256 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
257 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400259 // For now, only show master volume if master volume is supported
260 boolean useMasterVolume = context.getResources().getBoolean(
261 com.android.internal.R.bool.config_useMasterVolume);
262 if (useMasterVolume) {
263 for (int i = 0; i < STREAMS.length; i++) {
264 StreamResources streamRes = STREAMS[i];
265 streamRes.show = (streamRes.streamType == STREAM_MASTER);
266 }
267 }
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 LayoutInflater inflater = (LayoutInflater) context
270 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800271 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
272 mView.setOnTouchListener(new View.OnTouchListener() {
273 public boolean onTouch(View v, MotionEvent event) {
274 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700275 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800276 }
277 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700278 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800279 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
280 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800281 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
282
Amith Yamasani284e6302011-09-16 18:24:47 -0700283 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
284 public boolean onTouchEvent(MotionEvent event) {
Eric Laurentfde16d52012-12-03 14:42:39 -0800285 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
286 sConfirmSafeVolumeDialog == null) {
Amith Yamasani284e6302011-09-16 18:24:47 -0700287 forceTimeout();
288 return true;
289 }
290 return false;
291 }
292 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800293 mDialog.setTitle("Volume control"); // No need to localize
294 mDialog.setContentView(mView);
295 mDialog.setOnDismissListener(new OnDismissListener() {
296 public void onDismiss(DialogInterface dialog) {
297 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800298 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800299 }
300 });
301 // Change some window properties
302 Window window = mDialog.getWindow();
303 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700304 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800305 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700306 // Offset from the top
307 lp.y = mContext.getResources().getDimensionPixelOffset(
308 com.android.internal.R.dimen.volume_panel_top);
309 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
310 lp.width = LayoutParams.WRAP_CONTENT;
311 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800312 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700313 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
314 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Jeff Brownc2346132012-04-13 01:55:38 -0700317 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800318
Amith Yamasani71def772011-10-12 12:25:24 -0700319 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400320 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700321 // If we don't want to show multiple volumes, hide the settings button and divider
322 if (!mShowCombinedVolumes) {
323 mMoreButton.setVisibility(View.GONE);
324 mDivider.setVisibility(View.GONE);
325 } else {
326 mMoreButton.setOnClickListener(this);
327 }
328
Christopher Tatec4b78d22012-05-22 13:57:58 -0700329 boolean masterVolumeOnly = context.getResources().getBoolean(
330 com.android.internal.R.bool.config_useMasterVolume);
331 boolean masterVolumeKeySounds = mContext.getResources().getBoolean(
332 com.android.internal.R.bool.config_useVolumeKeySounds);
333
334 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
335
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800336 listenToRingerMode();
337 }
338
339 private void listenToRingerMode() {
340 final IntentFilter filter = new IntentFilter();
341 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
342 mContext.registerReceiver(new BroadcastReceiver() {
343
344 public void onReceive(Context context, Intent intent) {
345 final String action = intent.getAction();
346
347 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
348 removeMessages(MSG_RINGER_MODE_CHANGED);
349 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
350 }
351 }
352 }, filter);
353 }
354
355 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400356 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700357 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700358 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
359 return (mAudioService.getRemoteStreamVolume() <= 0);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400360 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700361 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400362 }
363 }
364
365 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400366 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700367 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700368 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
369 return mAudioService.getRemoteStreamMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400370 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700371 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400372 }
373 }
374
375 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400376 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700377 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700378 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
379 return mAudioService.getRemoteStreamVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400380 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700381 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400382 }
383 }
384
385 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400386 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700387 mAudioManager.setMasterVolume(index, flags);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700388 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
389 mAudioService.setRemoteStreamVolume(index);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400390 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700391 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400392 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800393 }
394
395 private void createSliders() {
396 LayoutInflater inflater = (LayoutInflater) mContext
397 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700398 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700399 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700400 for (int i = 0; i < STREAMS.length; i++) {
401 StreamResources streamRes = STREAMS[i];
402 int streamType = streamRes.streamType;
403 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
404 streamRes = StreamResources.RingerStream;
405 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800406 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700407 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800408 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
409 sc.group.setTag(sc);
410 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800411 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700412 sc.icon.setContentDescription(res.getString(streamRes.descRes));
413 sc.iconRes = streamRes.iconRes;
414 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800415 sc.icon.setImageResource(sc.iconRes);
416 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700417 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
418 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400419 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800420 sc.seekbarView.setOnSeekBarChangeListener(this);
421 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700422 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800423 }
424 }
425
426 private void reorderSliders(int activeStreamType) {
427 mSliderGroup.removeAllViews();
428
429 StreamControl active = mStreamControls.get(activeStreamType);
430 if (active == null) {
431 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
432 mActiveStreamType = -1;
433 } else {
434 mSliderGroup.addView(active.group);
435 mActiveStreamType = activeStreamType;
436 active.group.setVisibility(View.VISIBLE);
437 updateSlider(active);
438 }
439
Amith Yamasani42722bf2011-07-22 10:34:27 -0700440 addOtherVolumes();
441 }
442
443 private void addOtherVolumes() {
444 if (!mShowCombinedVolumes) return;
445
Amith Yamasani71def772011-10-12 12:25:24 -0700446 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800447 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700448 final int streamType = STREAMS[i].streamType;
449 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800450 continue;
451 }
452 StreamControl sc = mStreamControls.get(streamType);
453 mSliderGroup.addView(sc.group);
454 updateSlider(sc);
455 }
456 }
457
458 /** Update the mute and progress state of a slider */
459 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700460 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800461 final boolean muted = isMuted(sc.streamType);
462 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8c787522012-05-14 14:09:43 -0700463 if (sc.streamType == AudioManager.STREAM_RING &&
464 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
Amith Yamasanic696a532011-10-28 17:02:37 -0700465 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
466 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700467 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
468 // never disable touch interactions for remote playback, the muting is not tied to
469 // the state of the phone.
470 sc.seekbarView.setEnabled(true);
Eric Laurentfde16d52012-12-03 14:42:39 -0800471 } else if ((sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
472 (sConfirmSafeVolumeDialog != null)) {
Eric Laurent8c787522012-05-14 14:09:43 -0700473 sc.seekbarView.setEnabled(false);
474 } else {
475 sc.seekbarView.setEnabled(true);
476 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800477 }
478
479 private boolean isExpanded() {
480 return mMoreButton.getVisibility() != View.VISIBLE;
481 }
482
483 private void expand() {
484 final int count = mSliderGroup.getChildCount();
485 for (int i = 0; i < count; i++) {
486 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
487 }
488 mMoreButton.setVisibility(View.INVISIBLE);
489 mDivider.setVisibility(View.INVISIBLE);
490 }
491
492 private void collapse() {
493 mMoreButton.setVisibility(View.VISIBLE);
494 mDivider.setVisibility(View.VISIBLE);
495 final int count = mSliderGroup.getChildCount();
496 for (int i = 1; i < count; i++) {
497 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
498 }
499 }
500
Eric Laurentfde16d52012-12-03 14:42:39 -0800501 public void updateStates() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800502 final int count = mSliderGroup.getChildCount();
503 for (int i = 0; i < count; i++) {
504 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
505 updateSlider(sc);
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508
509 public void postVolumeChanged(int streamType, int flags) {
510 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700511 synchronized (this) {
512 if (mStreamControls == null) {
513 createSliders();
514 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 removeMessages(MSG_FREE_RESOURCES);
517 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
518 }
519
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700520 public void postRemoteVolumeChanged(int streamType, int flags) {
521 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
522 synchronized (this) {
523 if (mStreamControls == null) {
524 createSliders();
525 }
526 }
527 removeMessages(MSG_FREE_RESOURCES);
528 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget();
529 }
530
531 public void postRemoteSliderVisibility(boolean visible) {
532 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
533 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
534 }
535
536 /**
537 * Called by AudioService when it has received new remote playback information that
538 * would affect the VolumePanel display (mainly volumes). The difference with
539 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
540 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
541 * displayed.
542 * This special code path is due to the fact that remote volume updates arrive to AudioService
543 * asynchronously. So after AudioService has sent the volume update (which should be treated
544 * as a request to update the volume), the application will likely set a new volume. If the UI
545 * is still up, we need to refresh the display to show this new value.
546 */
547 public void postHasNewRemotePlaybackInfo() {
548 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
549 // don't create or prevent resources to be freed, if they disappear, this update came too
550 // late and shouldn't warrant the panel to be displayed longer
551 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
552 }
553
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400554 public void postMasterVolumeChanged(int flags) {
555 postVolumeChanged(STREAM_MASTER, flags);
556 }
557
Mike Lockwoodce952c82011-11-14 10:47:42 -0800558 public void postMuteChanged(int streamType, int flags) {
559 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700560 synchronized (this) {
561 if (mStreamControls == null) {
562 createSliders();
563 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800564 }
565 removeMessages(MSG_FREE_RESOURCES);
566 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
567 }
568
569 public void postMasterMuteChanged(int flags) {
570 postMuteChanged(STREAM_MASTER, flags);
571 }
572
Eric Laurentfde16d52012-12-03 14:42:39 -0800573 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700574 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800575 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700576 }
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 /**
579 * Override this if you have other work to do when the volume changes (for
580 * example, vibrating, playing a sound, etc.). Make sure to call through to
581 * the superclass implementation.
582 */
583 protected void onVolumeChanged(int streamType, int flags) {
584
585 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
586
587 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700588 synchronized (this) {
589 if (mActiveStreamType != streamType) {
590 reorderSliders(streamType);
591 }
592 onShowVolumeChanged(streamType, flags);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595
Marco Nelissen69f593c2009-07-28 09:55:04 -0700596 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 removeMessages(MSG_PLAY_SOUND);
598 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
599 }
600
601 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
602 removeMessages(MSG_PLAY_SOUND);
603 removeMessages(MSG_VIBRATE);
604 onStopSounds();
605 }
606
607 removeMessages(MSG_FREE_RESOURCES);
608 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800609 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611
Mike Lockwoodce952c82011-11-14 10:47:42 -0800612 protected void onMuteChanged(int streamType, int flags) {
613
614 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
615
616 StreamControl sc = mStreamControls.get(streamType);
617 if (sc != null) {
618 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
619 }
620
621 onVolumeChanged(streamType, flags);
622 }
623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700625 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800626
Marco Nelissen69f593c2009-07-28 09:55:04 -0700627 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 if (LOGD) {
630 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
631 + ", flags: " + flags + "), index: " + index);
632 }
633
634 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800635
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400636 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 switch (streamType) {
639
640 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800641// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700642 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
643 mContext, RingtoneManager.TYPE_RINGTONE);
644 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700645 mRingIsSilent = true;
646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 break;
648 }
649
650 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800651 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800652 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
653 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
654 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
655 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800656 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800658 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660 break;
661 }
662
663 case AudioManager.STREAM_VOICE_CALL: {
664 /*
665 * For in-call voice call volume, there is no inaudible volume.
666 * Rescale the UI control so the progress bar doesn't go all
667 * the way to zero and don't show the mute icon.
668 */
669 index++;
670 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 break;
672 }
673
674 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 break;
676 }
677
678 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700679 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
680 mContext, RingtoneManager.TYPE_NOTIFICATION);
681 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700682 mRingIsSilent = true;
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 break;
685 }
686
687 case AudioManager.STREAM_BLUETOOTH_SCO: {
688 /*
689 * For in-call voice call volume, there is no inaudible volume.
690 * Rescale the UI control so the progress bar doesn't go all
691 * the way to zero and don't show the mute icon.
692 */
693 index++;
694 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 break;
696 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700697
698 case AudioService.STREAM_REMOTE_MUSIC: {
699 if (LOGD) { Log.d(TAG, "showing remote volume "+index+" over "+ max); }
700 break;
701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800704 StreamControl sc = mStreamControls.get(streamType);
705 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700706 if (sc.seekbarView.getMax() != max) {
707 sc.seekbarView.setMax(max);
708 }
Eric Laurent4bbcc652012-09-24 14:26:30 -0700709
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800710 sc.seekbarView.setProgress(index);
Eric Laurent4bbcc652012-09-24 14:26:30 -0700711 if (((flags & AudioManager.FLAG_FIXED_VOLUME) != 0) ||
712 (streamType != mAudioManager.getMasterStreamType() &&
713 streamType != AudioService.STREAM_REMOTE_MUSIC &&
Eric Laurentfde16d52012-12-03 14:42:39 -0800714 isMuted(streamType)) ||
715 sConfirmSafeVolumeDialog != null) {
Eric Laurent8c787522012-05-14 14:09:43 -0700716 sc.seekbarView.setEnabled(false);
717 } else {
718 sc.seekbarView.setEnabled(true);
719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800722 if (!mDialog.isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700723 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
724 // when the stream is for remote playback, use -1 to reset the stream type evaluation
725 mAudioManager.forceVolumeControlStream(stream);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800726 mDialog.setContentView(mView);
727 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700728 if (mShowCombinedVolumes) {
729 collapse();
730 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800731 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700735 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
736 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mAudioService.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700738 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 protected void onPlaySound(int streamType, int flags) {
744
745 if (hasMessages(MSG_STOP_SOUNDS)) {
746 removeMessages(MSG_STOP_SOUNDS);
747 // Force stop right now
748 onStopSounds();
749 }
750
751 synchronized (this) {
752 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800753 if (toneGen != null) {
754 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
755 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 protected void onStopSounds() {
761
762 synchronized (this) {
763 int numStreamTypes = AudioSystem.getNumStreamTypes();
764 for (int i = numStreamTypes - 1; i >= 0; i--) {
765 ToneGenerator toneGen = mToneGenerators[i];
766 if (toneGen != null) {
767 toneGen.stopTone();
768 }
769 }
770 }
771 }
772
773 protected void onVibrate() {
774
775 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700776 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 return;
778 }
779
780 mVibrator.vibrate(VIBRATE_DURATION);
781 }
782
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700783 protected void onRemoteVolumeChanged(int streamType, int flags) {
784 // streamType is the real stream type being affected, but for the UI sliders, we
785 // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real
786 // stream type.
787 if (LOGD) Log.d(TAG, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")");
788
789 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || mDialog.isShowing()) {
790 synchronized (this) {
791 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
792 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
793 }
794 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags);
795 }
796 } else {
797 if (LOGD) Log.d(TAG, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
798 }
799
800 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
801 removeMessages(MSG_PLAY_SOUND);
802 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
803 }
804
805 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
806 removeMessages(MSG_PLAY_SOUND);
807 removeMessages(MSG_VIBRATE);
808 onStopSounds();
809 }
810
811 removeMessages(MSG_FREE_RESOURCES);
812 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700813 resetTimeout();
814 }
815
816 protected void onRemoteVolumeUpdateIfShown() {
817 if (LOGD) Log.d(TAG, "onRemoteVolumeUpdateIfShown()");
818 if (mDialog.isShowing()
819 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
820 && (mStreamControls != null)) {
821 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0);
822 }
823 }
824
825
826 /**
827 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
828 * Hide or show a slider
829 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
830 * or AudioService.STREAM_REMOTE_MUSIC
831 * @param visible
832 */
833 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
834 if (LOGD) Log.d(TAG, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
835 boolean isVisible = (visible == 1);
836 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
837 StreamResources streamRes = STREAMS[i];
838 if (streamRes.streamType == streamType) {
839 streamRes.show = isVisible;
840 if (!isVisible && (mActiveStreamType == streamType)) {
841 mActiveStreamType = -1;
842 }
843 break;
844 }
845 }
846 }
847
Eric Laurentfde16d52012-12-03 14:42:39 -0800848 protected void onDisplaySafeVolumeWarning(int flags) {
849 if ((flags & AudioManager.FLAG_SHOW_UI) != 0 || mDialog.isShowing()) {
850 synchronized (sConfirmSafeVolumeLock) {
851 if (sConfirmSafeVolumeDialog != null) {
852 return;
853 }
854 sConfirmSafeVolumeDialog = new AlertDialog.Builder(mContext)
855 .setMessage(com.android.internal.R.string.safe_media_volume_warning)
856 .setPositiveButton(com.android.internal.R.string.yes,
857 new DialogInterface.OnClickListener() {
858 public void onClick(DialogInterface dialog, int which) {
859 mAudioService.disableSafeMediaVolume();
860 }
861 })
862 .setNegativeButton(com.android.internal.R.string.no, null)
863 .setIconAttribute(android.R.attr.alertDialogIcon)
864 .create();
865 final WarningDialogReceiver warning = new WarningDialogReceiver(mContext,
866 sConfirmSafeVolumeDialog, this);
Eric Laurent0516a9e2012-09-19 11:53:03 -0700867
Eric Laurentfde16d52012-12-03 14:42:39 -0800868 sConfirmSafeVolumeDialog.setOnDismissListener(warning);
869 sConfirmSafeVolumeDialog.getWindow().setType(
870 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
871 sConfirmSafeVolumeDialog.show();
872 }
873 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700874 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800875 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700876 }
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 /**
879 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
880 */
881 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -0700882 if (streamType == STREAM_MASTER) {
883 // For devices that use the master volume setting only but still want to
884 // play a volume-changed tone, direct the master volume pseudostream to
885 // the system stream's tone generator.
886 if (mPlayMasterStreamTones) {
887 streamType = AudioManager.STREAM_SYSTEM;
888 } else {
889 return null;
890 }
891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 synchronized (this) {
893 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800894 try {
895 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
896 } catch (RuntimeException e) {
897 if (LOGD) {
898 Log.d(TAG, "ToneGenerator constructor failed with "
899 + "RuntimeException: " + e);
900 }
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800903 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
908 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800909 * Switch between icons because Bluetooth music is same as music volume, but with
910 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800912 private void setMusicIcon(int resId, int resMuteId) {
913 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
914 if (sc != null) {
915 sc.iconRes = resId;
916 sc.iconMuteRes = resMuteId;
917 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
921 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 synchronized (this) {
923 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
924 if (mToneGenerators[i] != null) {
925 mToneGenerators[i].release();
926 }
927 mToneGenerators[i] = null;
928 }
929 }
930 }
931
932 @Override
933 public void handleMessage(Message msg) {
934 switch (msg.what) {
935
936 case MSG_VOLUME_CHANGED: {
937 onVolumeChanged(msg.arg1, msg.arg2);
938 break;
939 }
940
Mike Lockwoodce952c82011-11-14 10:47:42 -0800941 case MSG_MUTE_CHANGED: {
942 onMuteChanged(msg.arg1, msg.arg2);
943 break;
944 }
945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 case MSG_FREE_RESOURCES: {
947 onFreeResources();
948 break;
949 }
950
951 case MSG_STOP_SOUNDS: {
952 onStopSounds();
953 break;
954 }
955
956 case MSG_PLAY_SOUND: {
957 onPlaySound(msg.arg1, msg.arg2);
958 break;
959 }
960
961 case MSG_VIBRATE: {
962 onVibrate();
963 break;
964 }
965
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800966 case MSG_TIMEOUT: {
967 if (mDialog.isShowing()) {
968 mDialog.dismiss();
969 mActiveStreamType = -1;
970 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800971 synchronized (sConfirmSafeVolumeLock) {
972 if (sConfirmSafeVolumeDialog != null) {
973 sConfirmSafeVolumeDialog.dismiss();
974 }
975 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800976 break;
977 }
978 case MSG_RINGER_MODE_CHANGED: {
979 if (mDialog.isShowing()) {
980 updateStates();
981 }
982 break;
983 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700984
985 case MSG_REMOTE_VOLUME_CHANGED: {
986 onRemoteVolumeChanged(msg.arg1, msg.arg2);
987 break;
988 }
989
990 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
991 onRemoteVolumeUpdateIfShown();
992 break;
993
994 case MSG_SLIDER_VISIBILITY_CHANGED:
995 onSliderVisibilityChanged(msg.arg1, msg.arg2);
996 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700997
998 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -0800999 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001000 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002 }
1003
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001004 private void resetTimeout() {
1005 removeMessages(MSG_TIMEOUT);
1006 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
1007 }
1008
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001009 private void forceTimeout() {
1010 removeMessages(MSG_TIMEOUT);
1011 sendMessage(obtainMessage(MSG_TIMEOUT));
1012 }
1013
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001014 public void onProgressChanged(SeekBar seekBar, int progress,
1015 boolean fromUser) {
1016 final Object tag = seekBar.getTag();
1017 if (fromUser && tag instanceof StreamControl) {
1018 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -04001019 if (getStreamVolume(sc.streamType) != progress) {
1020 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001021 }
1022 }
1023 resetTimeout();
1024 }
1025
1026 public void onStartTrackingTouch(SeekBar seekBar) {
1027 }
1028
1029 public void onStopTrackingTouch(SeekBar seekBar) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001030 final Object tag = seekBar.getTag();
1031 if (tag instanceof StreamControl) {
1032 StreamControl sc = (StreamControl) tag;
1033 // because remote volume updates are asynchronous, AudioService might have received
1034 // a new remote volume value since the finger adjusted the slider. So when the
1035 // progress of the slider isn't being tracked anymore, adjust the slider to the last
1036 // "published" remote volume value, so the UI reflects the actual volume.
1037 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
1038 seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC));
1039 }
1040 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001041 }
1042
1043 public void onClick(View v) {
1044 if (v == mMoreButton) {
1045 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001046 }
1047 resetTimeout();
1048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049}