blob: 52f9c0b480cad4adcf222414abcc50fcd7bf1e6f [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;
Jean-Michel Trivifa9a6982013-06-27 16:22:58 -070035import android.media.VolumeController;
Marco Nelissen69f593c2009-07-28 09:55:04 -070036import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Handler;
38import android.os.Message;
39import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.Log;
Amith Yamasani284e6302011-09-16 18:24:47 -070041import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080043import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080044import android.widget.SeekBar.OnSeekBarChangeListener;
45
46import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48/**
49 * Handle the volume up and down keys.
50 *
51 * This code really should be moved elsewhere.
52 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070053 * Seriously, it really really should be moved elsewhere. This is used by
54 * android.media.AudioService, which actually runs in the system process, to
55 * show the volume dialog when the user changes the volume. What a mess.
56 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 * @hide
58 */
Jean-Michel Trivifa9a6982013-06-27 16:22:58 -070059public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener,
60 VolumeController
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061{
62 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070063 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
65 /**
66 * The delay before playing a sound. This small period exists so the user
67 * can press another key (non-volume keys, too) to have it NOT be audible.
68 * <p>
69 * PhoneWindow will implement this part.
70 */
71 public static final int PLAY_SOUND_DELAY = 300;
72
73 /**
74 * The delay before vibrating. This small period exists so if the user is
75 * moving to silent mode, it will not emit a short vibrate (it normally
76 * would since vibrate is between normal mode and silent mode using hardware
77 * keys).
78 */
79 public static final int VIBRATE_DELAY = 300;
80
81 private static final int VIBRATE_DURATION = 300;
82 private static final int BEEP_DURATION = 150;
83 private static final int MAX_VOLUME = 100;
84 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080085 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 private static final int MSG_VOLUME_CHANGED = 0;
88 private static final int MSG_FREE_RESOURCES = 1;
89 private static final int MSG_PLAY_SOUND = 2;
90 private static final int MSG_STOP_SOUNDS = 3;
91 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080092 private static final int MSG_TIMEOUT = 5;
93 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -080094 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -070095 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
96 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
97 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -070098 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400100 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800101 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700102 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 protected Context mContext;
105 private AudioManager mAudioManager;
106 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700107 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700108 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700109 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Christopher Tatec4b78d22012-05-22 13:57:58 -0700111 // True if we want to play tones on the system stream when the master stream is specified.
112 private final boolean mPlayMasterStreamTones;
113
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800114 /** Dialog containing all the sliders */
115 private final Dialog mDialog;
116 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800118
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700119 /** The visible portion of the volume overlay */
120 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800121 /** Contains the sliders and their touchable icons */
122 private final ViewGroup mSliderGroup;
123 /** The button that expands the dialog to show all sliders */
124 private final View mMoreButton;
125 /** Dummy divider icon that needs to vanish with the more button */
126 private final View mDivider;
127
128 /** Currently active stream that shows up at the top of the list of sliders */
129 private int mActiveStreamType = -1;
130 /** All the slider controls mapped by stream type */
131 private HashMap<Integer,StreamControl> mStreamControls;
132
Amith Yamasani71def772011-10-12 12:25:24 -0700133 private enum StreamResources {
134 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
135 R.string.volume_icon_description_bluetooth,
136 R.drawable.ic_audio_bt,
137 R.drawable.ic_audio_bt,
138 false),
139 RingerStream(AudioManager.STREAM_RING,
140 R.string.volume_icon_description_ringer,
141 R.drawable.ic_audio_ring_notif,
142 R.drawable.ic_audio_ring_notif_mute,
143 false),
144 VoiceStream(AudioManager.STREAM_VOICE_CALL,
145 R.string.volume_icon_description_incall,
146 R.drawable.ic_audio_phone,
147 R.drawable.ic_audio_phone,
148 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700149 AlarmStream(AudioManager.STREAM_ALARM,
150 R.string.volume_alarm,
151 R.drawable.ic_audio_alarm,
152 R.drawable.ic_audio_alarm_mute,
153 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700154 MediaStream(AudioManager.STREAM_MUSIC,
155 R.string.volume_icon_description_media,
156 R.drawable.ic_audio_vol,
157 R.drawable.ic_audio_vol_mute,
158 true),
159 NotificationStream(AudioManager.STREAM_NOTIFICATION,
160 R.string.volume_icon_description_notification,
161 R.drawable.ic_audio_notification,
162 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400163 true),
164 // for now, use media resources for master volume
165 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700166 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400167 R.drawable.ic_audio_vol,
168 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700169 false),
170 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
171 R.string.volume_icon_description_media, //FIXME should have its own description
172 R.drawable.ic_media_route_on_holo_dark,
173 R.drawable.ic_media_route_disabled_holo_dark,
174 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700175
176 int streamType;
177 int descRes;
178 int iconRes;
179 int iconMuteRes;
180 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
181 boolean show;
182
183 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
184 this.streamType = streamType;
185 this.descRes = descRes;
186 this.iconRes = iconRes;
187 this.iconMuteRes = iconMuteRes;
188 this.show = show;
189 }
190 };
191
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800192 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700193 private static final StreamResources[] STREAMS = {
194 StreamResources.BluetoothSCOStream,
195 StreamResources.RingerStream,
196 StreamResources.VoiceStream,
197 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700198 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400199 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700200 StreamResources.MasterStream,
201 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800202 };
203
204 /** Object that contains data for each slider */
205 private class StreamControl {
206 int streamType;
207 ViewGroup group;
208 ImageView icon;
209 SeekBar seekbarView;
210 int iconRes;
211 int iconMuteRes;
212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
214 // Synchronize when accessing this
215 private ToneGenerator mToneGenerators[];
216 private Vibrator mVibrator;
217
Eric Laurentc34dcc12012-09-10 13:51:52 -0700218 private static AlertDialog sConfirmSafeVolumeDialog;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700219 private static Object sConfirmSafeVolumeLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700220
221 private static class WarningDialogReceiver extends BroadcastReceiver
222 implements DialogInterface.OnDismissListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800223 private final Context mContext;
224 private final Dialog mDialog;
225 private final VolumePanel mVolumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700226
Eric Laurentfde16d52012-12-03 14:42:39 -0800227 WarningDialogReceiver(Context context, Dialog dialog, VolumePanel volumePanel) {
Eric Laurentc34dcc12012-09-10 13:51:52 -0700228 mContext = context;
229 mDialog = dialog;
Eric Laurentfde16d52012-12-03 14:42:39 -0800230 mVolumePanel = volumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700231 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
232 context.registerReceiver(this, filter);
233 }
234
235 @Override
236 public void onReceive(Context context, Intent intent) {
237 mDialog.cancel();
Eric Laurentfde16d52012-12-03 14:42:39 -0800238 cleanUp();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700239 }
240
241 public void onDismiss(DialogInterface unused) {
242 mContext.unregisterReceiver(this);
Eric Laurentfde16d52012-12-03 14:42:39 -0800243 cleanUp();
244 }
245
246 private void cleanUp() {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700247 synchronized (sConfirmSafeVolumeLock) {
248 sConfirmSafeVolumeDialog = null;
249 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800250 mVolumePanel.forceTimeout();
251 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700252 }
253 }
254
255
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800256 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mContext = context;
258 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
259 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400261 // For now, only show master volume if master volume is supported
262 boolean useMasterVolume = context.getResources().getBoolean(
263 com.android.internal.R.bool.config_useMasterVolume);
264 if (useMasterVolume) {
265 for (int i = 0; i < STREAMS.length; i++) {
266 StreamResources streamRes = STREAMS[i];
267 streamRes.show = (streamRes.streamType == STREAM_MASTER);
268 }
269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 LayoutInflater inflater = (LayoutInflater) context
272 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800273 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
274 mView.setOnTouchListener(new View.OnTouchListener() {
275 public boolean onTouch(View v, MotionEvent event) {
276 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700277 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800278 }
279 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700280 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800281 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
282 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800283 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
284
Amith Yamasani284e6302011-09-16 18:24:47 -0700285 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
286 public boolean onTouchEvent(MotionEvent event) {
Eric Laurentfde16d52012-12-03 14:42:39 -0800287 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
288 sConfirmSafeVolumeDialog == null) {
Amith Yamasani284e6302011-09-16 18:24:47 -0700289 forceTimeout();
290 return true;
291 }
292 return false;
293 }
294 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800295 mDialog.setTitle("Volume control"); // No need to localize
296 mDialog.setContentView(mView);
297 mDialog.setOnDismissListener(new OnDismissListener() {
298 public void onDismiss(DialogInterface dialog) {
299 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800300 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800301 }
302 });
303 // Change some window properties
304 Window window = mDialog.getWindow();
305 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700306 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800307 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700308 // Offset from the top
309 lp.y = mContext.getResources().getDimensionPixelOffset(
310 com.android.internal.R.dimen.volume_panel_top);
311 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
312 lp.width = LayoutParams.WRAP_CONTENT;
313 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800314 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700315 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
316 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Jeff Brownc2346132012-04-13 01:55:38 -0700319 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800320
Amith Yamasani71def772011-10-12 12:25:24 -0700321 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400322 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700323 // If we don't want to show multiple volumes, hide the settings button and divider
324 if (!mShowCombinedVolumes) {
325 mMoreButton.setVisibility(View.GONE);
326 mDivider.setVisibility(View.GONE);
327 } else {
328 mMoreButton.setOnClickListener(this);
329 }
330
Christopher Tatec4b78d22012-05-22 13:57:58 -0700331 boolean masterVolumeOnly = context.getResources().getBoolean(
332 com.android.internal.R.bool.config_useMasterVolume);
333 boolean masterVolumeKeySounds = mContext.getResources().getBoolean(
334 com.android.internal.R.bool.config_useVolumeKeySounds);
335
336 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
337
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800338 listenToRingerMode();
339 }
340
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800341 public void setLayoutDirection(int layoutDirection) {
342 mPanel.setLayoutDirection(layoutDirection);
343 updateStates();
344 }
345
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800346 private void listenToRingerMode() {
347 final IntentFilter filter = new IntentFilter();
348 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
349 mContext.registerReceiver(new BroadcastReceiver() {
350
351 public void onReceive(Context context, Intent intent) {
352 final String action = intent.getAction();
353
354 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
355 removeMessages(MSG_RINGER_MODE_CHANGED);
356 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
357 }
358 }
359 }, filter);
360 }
361
362 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400363 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700364 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700365 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
366 return (mAudioService.getRemoteStreamVolume() <= 0);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400367 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700368 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400369 }
370 }
371
372 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400373 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700374 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700375 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
376 return mAudioService.getRemoteStreamMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400377 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700378 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400379 }
380 }
381
382 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400383 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700384 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700385 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
386 return mAudioService.getRemoteStreamVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400387 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700388 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400389 }
390 }
391
392 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400393 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700394 mAudioManager.setMasterVolume(index, flags);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700395 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
396 mAudioService.setRemoteStreamVolume(index);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400397 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700398 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400399 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800400 }
401
402 private void createSliders() {
403 LayoutInflater inflater = (LayoutInflater) mContext
404 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700405 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700406 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700407 for (int i = 0; i < STREAMS.length; i++) {
408 StreamResources streamRes = STREAMS[i];
409 int streamType = streamRes.streamType;
410 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
411 streamRes = StreamResources.RingerStream;
412 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800413 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700414 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800415 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
416 sc.group.setTag(sc);
417 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800418 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700419 sc.icon.setContentDescription(res.getString(streamRes.descRes));
420 sc.iconRes = streamRes.iconRes;
421 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800422 sc.icon.setImageResource(sc.iconRes);
423 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700424 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
425 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400426 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800427 sc.seekbarView.setOnSeekBarChangeListener(this);
428 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700429 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800430 }
431 }
432
433 private void reorderSliders(int activeStreamType) {
434 mSliderGroup.removeAllViews();
435
436 StreamControl active = mStreamControls.get(activeStreamType);
437 if (active == null) {
438 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
439 mActiveStreamType = -1;
440 } else {
441 mSliderGroup.addView(active.group);
442 mActiveStreamType = activeStreamType;
443 active.group.setVisibility(View.VISIBLE);
444 updateSlider(active);
445 }
446
Amith Yamasani42722bf2011-07-22 10:34:27 -0700447 addOtherVolumes();
448 }
449
450 private void addOtherVolumes() {
451 if (!mShowCombinedVolumes) return;
452
Amith Yamasani71def772011-10-12 12:25:24 -0700453 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800454 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700455 final int streamType = STREAMS[i].streamType;
456 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800457 continue;
458 }
459 StreamControl sc = mStreamControls.get(streamType);
460 mSliderGroup.addView(sc.group);
461 updateSlider(sc);
462 }
463 }
464
465 /** Update the mute and progress state of a slider */
466 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700467 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800468 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800469 // Force reloading the image resource
470 sc.icon.setImageDrawable(null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800471 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8a51aca2013-03-25 18:36:35 -0700472 if (((sc.streamType == AudioManager.STREAM_RING) ||
473 (sc.streamType == AudioManager.STREAM_NOTIFICATION)) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700474 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
Amith Yamasanic696a532011-10-28 17:02:37 -0700475 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
476 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700477 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
478 // never disable touch interactions for remote playback, the muting is not tied to
479 // the state of the phone.
480 sc.seekbarView.setEnabled(true);
Eric Laurentfde16d52012-12-03 14:42:39 -0800481 } else if ((sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
482 (sConfirmSafeVolumeDialog != null)) {
Eric Laurent8c787522012-05-14 14:09:43 -0700483 sc.seekbarView.setEnabled(false);
484 } else {
485 sc.seekbarView.setEnabled(true);
486 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800487 }
488
489 private boolean isExpanded() {
490 return mMoreButton.getVisibility() != View.VISIBLE;
491 }
492
493 private void expand() {
494 final int count = mSliderGroup.getChildCount();
495 for (int i = 0; i < count; i++) {
496 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
497 }
498 mMoreButton.setVisibility(View.INVISIBLE);
499 mDivider.setVisibility(View.INVISIBLE);
500 }
501
502 private void collapse() {
503 mMoreButton.setVisibility(View.VISIBLE);
504 mDivider.setVisibility(View.VISIBLE);
505 final int count = mSliderGroup.getChildCount();
506 for (int i = 1; i < count; i++) {
507 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
508 }
509 }
510
Eric Laurentfde16d52012-12-03 14:42:39 -0800511 public void updateStates() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800512 final int count = mSliderGroup.getChildCount();
513 for (int i = 0; i < count; i++) {
514 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
515 updateSlider(sc);
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
519 public void postVolumeChanged(int streamType, int flags) {
520 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700521 synchronized (this) {
522 if (mStreamControls == null) {
523 createSliders();
524 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 removeMessages(MSG_FREE_RESOURCES);
527 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
528 }
529
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700530 public void postRemoteVolumeChanged(int streamType, int flags) {
531 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
532 synchronized (this) {
533 if (mStreamControls == null) {
534 createSliders();
535 }
536 }
537 removeMessages(MSG_FREE_RESOURCES);
538 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget();
539 }
540
541 public void postRemoteSliderVisibility(boolean visible) {
542 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
543 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
544 }
545
546 /**
547 * Called by AudioService when it has received new remote playback information that
548 * would affect the VolumePanel display (mainly volumes). The difference with
549 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
550 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
551 * displayed.
552 * This special code path is due to the fact that remote volume updates arrive to AudioService
553 * asynchronously. So after AudioService has sent the volume update (which should be treated
554 * as a request to update the volume), the application will likely set a new volume. If the UI
555 * is still up, we need to refresh the display to show this new value.
556 */
557 public void postHasNewRemotePlaybackInfo() {
558 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
559 // don't create or prevent resources to be freed, if they disappear, this update came too
560 // late and shouldn't warrant the panel to be displayed longer
561 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
562 }
563
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400564 public void postMasterVolumeChanged(int flags) {
565 postVolumeChanged(STREAM_MASTER, flags);
566 }
567
Mike Lockwoodce952c82011-11-14 10:47:42 -0800568 public void postMuteChanged(int streamType, int flags) {
569 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700570 synchronized (this) {
571 if (mStreamControls == null) {
572 createSliders();
573 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800574 }
575 removeMessages(MSG_FREE_RESOURCES);
576 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
577 }
578
579 public void postMasterMuteChanged(int flags) {
580 postMuteChanged(STREAM_MASTER, flags);
581 }
582
Eric Laurentfde16d52012-12-03 14:42:39 -0800583 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700584 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800585 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700586 }
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 /**
589 * Override this if you have other work to do when the volume changes (for
590 * example, vibrating, playing a sound, etc.). Make sure to call through to
591 * the superclass implementation.
592 */
593 protected void onVolumeChanged(int streamType, int flags) {
594
595 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
596
597 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700598 synchronized (this) {
599 if (mActiveStreamType != streamType) {
600 reorderSliders(streamType);
601 }
602 onShowVolumeChanged(streamType, flags);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
Marco Nelissen69f593c2009-07-28 09:55:04 -0700606 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 removeMessages(MSG_PLAY_SOUND);
608 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
609 }
610
611 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
612 removeMessages(MSG_PLAY_SOUND);
613 removeMessages(MSG_VIBRATE);
614 onStopSounds();
615 }
616
617 removeMessages(MSG_FREE_RESOURCES);
618 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800619 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
621
Mike Lockwoodce952c82011-11-14 10:47:42 -0800622 protected void onMuteChanged(int streamType, int flags) {
623
624 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
625
626 StreamControl sc = mStreamControls.get(streamType);
627 if (sc != null) {
628 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
629 }
630
631 onVolumeChanged(streamType, flags);
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700635 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800636
Marco Nelissen69f593c2009-07-28 09:55:04 -0700637 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 if (LOGD) {
640 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
641 + ", flags: " + flags + "), index: " + index);
642 }
643
644 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800645
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400646 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
648 switch (streamType) {
649
650 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800651// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700652 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
653 mContext, RingtoneManager.TYPE_RINGTONE);
654 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700655 mRingIsSilent = true;
656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 break;
658 }
659
660 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800661 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800662 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
663 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
664 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
665 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800666 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800668 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670 break;
671 }
672
673 case AudioManager.STREAM_VOICE_CALL: {
674 /*
675 * For in-call voice call volume, there is no inaudible volume.
676 * Rescale the UI control so the progress bar doesn't go all
677 * the way to zero and don't show the mute icon.
678 */
679 index++;
680 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 break;
682 }
683
684 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 break;
686 }
687
688 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700689 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
690 mContext, RingtoneManager.TYPE_NOTIFICATION);
691 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700692 mRingIsSilent = true;
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 break;
695 }
696
697 case AudioManager.STREAM_BLUETOOTH_SCO: {
698 /*
699 * For in-call voice call volume, there is no inaudible volume.
700 * Rescale the UI control so the progress bar doesn't go all
701 * the way to zero and don't show the mute icon.
702 */
703 index++;
704 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 break;
706 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700707
708 case AudioService.STREAM_REMOTE_MUSIC: {
709 if (LOGD) { Log.d(TAG, "showing remote volume "+index+" over "+ max); }
710 break;
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800714 StreamControl sc = mStreamControls.get(streamType);
715 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700716 if (sc.seekbarView.getMax() != max) {
717 sc.seekbarView.setMax(max);
718 }
Eric Laurent4bbcc652012-09-24 14:26:30 -0700719
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800720 sc.seekbarView.setProgress(index);
Eric Laurent4bbcc652012-09-24 14:26:30 -0700721 if (((flags & AudioManager.FLAG_FIXED_VOLUME) != 0) ||
722 (streamType != mAudioManager.getMasterStreamType() &&
723 streamType != AudioService.STREAM_REMOTE_MUSIC &&
Eric Laurentfde16d52012-12-03 14:42:39 -0800724 isMuted(streamType)) ||
725 sConfirmSafeVolumeDialog != null) {
Eric Laurent8c787522012-05-14 14:09:43 -0700726 sc.seekbarView.setEnabled(false);
727 } else {
728 sc.seekbarView.setEnabled(true);
729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800732 if (!mDialog.isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700733 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
734 // when the stream is for remote playback, use -1 to reset the stream type evaluation
735 mAudioManager.forceVolumeControlStream(stream);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800736 mDialog.setContentView(mView);
737 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700738 if (mShowCombinedVolumes) {
739 collapse();
740 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800741 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700745 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
746 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 mAudioService.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700748 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
753 protected void onPlaySound(int streamType, int flags) {
754
755 if (hasMessages(MSG_STOP_SOUNDS)) {
756 removeMessages(MSG_STOP_SOUNDS);
757 // Force stop right now
758 onStopSounds();
759 }
760
761 synchronized (this) {
762 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800763 if (toneGen != null) {
764 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
765 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 protected void onStopSounds() {
771
772 synchronized (this) {
773 int numStreamTypes = AudioSystem.getNumStreamTypes();
774 for (int i = numStreamTypes - 1; i >= 0; i--) {
775 ToneGenerator toneGen = mToneGenerators[i];
776 if (toneGen != null) {
777 toneGen.stopTone();
778 }
779 }
780 }
781 }
782
783 protected void onVibrate() {
784
785 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700786 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 return;
788 }
789
790 mVibrator.vibrate(VIBRATE_DURATION);
791 }
792
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700793 protected void onRemoteVolumeChanged(int streamType, int flags) {
794 // streamType is the real stream type being affected, but for the UI sliders, we
795 // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real
796 // stream type.
797 if (LOGD) Log.d(TAG, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")");
798
799 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || mDialog.isShowing()) {
800 synchronized (this) {
801 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
802 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
803 }
804 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags);
805 }
806 } else {
807 if (LOGD) Log.d(TAG, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
808 }
809
810 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
811 removeMessages(MSG_PLAY_SOUND);
812 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
813 }
814
815 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
816 removeMessages(MSG_PLAY_SOUND);
817 removeMessages(MSG_VIBRATE);
818 onStopSounds();
819 }
820
821 removeMessages(MSG_FREE_RESOURCES);
822 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700823 resetTimeout();
824 }
825
826 protected void onRemoteVolumeUpdateIfShown() {
827 if (LOGD) Log.d(TAG, "onRemoteVolumeUpdateIfShown()");
828 if (mDialog.isShowing()
829 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
830 && (mStreamControls != null)) {
831 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0);
832 }
833 }
834
835
836 /**
837 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
838 * Hide or show a slider
839 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
840 * or AudioService.STREAM_REMOTE_MUSIC
841 * @param visible
842 */
843 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
844 if (LOGD) Log.d(TAG, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
845 boolean isVisible = (visible == 1);
846 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
847 StreamResources streamRes = STREAMS[i];
848 if (streamRes.streamType == streamType) {
849 streamRes.show = isVisible;
850 if (!isVisible && (mActiveStreamType == streamType)) {
851 mActiveStreamType = -1;
852 }
853 break;
854 }
855 }
856 }
857
Eric Laurentfde16d52012-12-03 14:42:39 -0800858 protected void onDisplaySafeVolumeWarning(int flags) {
859 if ((flags & AudioManager.FLAG_SHOW_UI) != 0 || mDialog.isShowing()) {
860 synchronized (sConfirmSafeVolumeLock) {
861 if (sConfirmSafeVolumeDialog != null) {
862 return;
863 }
864 sConfirmSafeVolumeDialog = new AlertDialog.Builder(mContext)
865 .setMessage(com.android.internal.R.string.safe_media_volume_warning)
866 .setPositiveButton(com.android.internal.R.string.yes,
867 new DialogInterface.OnClickListener() {
868 public void onClick(DialogInterface dialog, int which) {
869 mAudioService.disableSafeMediaVolume();
870 }
871 })
872 .setNegativeButton(com.android.internal.R.string.no, null)
873 .setIconAttribute(android.R.attr.alertDialogIcon)
874 .create();
875 final WarningDialogReceiver warning = new WarningDialogReceiver(mContext,
876 sConfirmSafeVolumeDialog, this);
Eric Laurent0516a9e2012-09-19 11:53:03 -0700877
Eric Laurentfde16d52012-12-03 14:42:39 -0800878 sConfirmSafeVolumeDialog.setOnDismissListener(warning);
879 sConfirmSafeVolumeDialog.getWindow().setType(
880 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
881 sConfirmSafeVolumeDialog.show();
882 }
883 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700884 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800885 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700886 }
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 /**
889 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
890 */
891 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -0700892 if (streamType == STREAM_MASTER) {
893 // For devices that use the master volume setting only but still want to
894 // play a volume-changed tone, direct the master volume pseudostream to
895 // the system stream's tone generator.
896 if (mPlayMasterStreamTones) {
897 streamType = AudioManager.STREAM_SYSTEM;
898 } else {
899 return null;
900 }
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 synchronized (this) {
903 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800904 try {
905 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
906 } catch (RuntimeException e) {
907 if (LOGD) {
908 Log.d(TAG, "ToneGenerator constructor failed with "
909 + "RuntimeException: " + e);
910 }
911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800913 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 }
915 }
916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
918 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800919 * Switch between icons because Bluetooth music is same as music volume, but with
920 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800922 private void setMusicIcon(int resId, int resMuteId) {
923 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
924 if (sc != null) {
925 sc.iconRes = resId;
926 sc.iconMuteRes = resMuteId;
927 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 }
930
931 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 synchronized (this) {
933 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
934 if (mToneGenerators[i] != null) {
935 mToneGenerators[i].release();
936 }
937 mToneGenerators[i] = null;
938 }
939 }
940 }
941
942 @Override
943 public void handleMessage(Message msg) {
944 switch (msg.what) {
945
946 case MSG_VOLUME_CHANGED: {
947 onVolumeChanged(msg.arg1, msg.arg2);
948 break;
949 }
950
Mike Lockwoodce952c82011-11-14 10:47:42 -0800951 case MSG_MUTE_CHANGED: {
952 onMuteChanged(msg.arg1, msg.arg2);
953 break;
954 }
955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 case MSG_FREE_RESOURCES: {
957 onFreeResources();
958 break;
959 }
960
961 case MSG_STOP_SOUNDS: {
962 onStopSounds();
963 break;
964 }
965
966 case MSG_PLAY_SOUND: {
967 onPlaySound(msg.arg1, msg.arg2);
968 break;
969 }
970
971 case MSG_VIBRATE: {
972 onVibrate();
973 break;
974 }
975
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800976 case MSG_TIMEOUT: {
977 if (mDialog.isShowing()) {
978 mDialog.dismiss();
979 mActiveStreamType = -1;
980 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800981 synchronized (sConfirmSafeVolumeLock) {
982 if (sConfirmSafeVolumeDialog != null) {
983 sConfirmSafeVolumeDialog.dismiss();
984 }
985 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800986 break;
987 }
988 case MSG_RINGER_MODE_CHANGED: {
989 if (mDialog.isShowing()) {
990 updateStates();
991 }
992 break;
993 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700994
995 case MSG_REMOTE_VOLUME_CHANGED: {
996 onRemoteVolumeChanged(msg.arg1, msg.arg2);
997 break;
998 }
999
1000 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1001 onRemoteVolumeUpdateIfShown();
1002 break;
1003
1004 case MSG_SLIDER_VISIBILITY_CHANGED:
1005 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1006 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001007
1008 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001009 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012 }
1013
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001014 private void resetTimeout() {
1015 removeMessages(MSG_TIMEOUT);
1016 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
1017 }
1018
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001019 private void forceTimeout() {
1020 removeMessages(MSG_TIMEOUT);
1021 sendMessage(obtainMessage(MSG_TIMEOUT));
1022 }
1023
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001024 public void onProgressChanged(SeekBar seekBar, int progress,
1025 boolean fromUser) {
1026 final Object tag = seekBar.getTag();
1027 if (fromUser && tag instanceof StreamControl) {
1028 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -04001029 if (getStreamVolume(sc.streamType) != progress) {
1030 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001031 }
1032 }
1033 resetTimeout();
1034 }
1035
1036 public void onStartTrackingTouch(SeekBar seekBar) {
1037 }
1038
1039 public void onStopTrackingTouch(SeekBar seekBar) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001040 final Object tag = seekBar.getTag();
1041 if (tag instanceof StreamControl) {
1042 StreamControl sc = (StreamControl) tag;
1043 // because remote volume updates are asynchronous, AudioService might have received
1044 // a new remote volume value since the finger adjusted the slider. So when the
1045 // progress of the slider isn't being tracked anymore, adjust the slider to the last
1046 // "published" remote volume value, so the UI reflects the actual volume.
1047 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
1048 seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC));
1049 }
1050 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001051 }
1052
1053 public void onClick(View v) {
1054 if (v == mMoreButton) {
1055 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001056 }
1057 resetTimeout();
1058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059}