blob: 8315bd76846167fc3bad3ce096796f74c215f65e [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 {
221 private Context mContext;
222 private Dialog mDialog;
223
224 WarningDialogReceiver(Context context, Dialog dialog) {
225 mContext = context;
226 mDialog = dialog;
227 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
228 context.registerReceiver(this, filter);
229 }
230
231 @Override
232 public void onReceive(Context context, Intent intent) {
233 mDialog.cancel();
Eric Laurent0516a9e2012-09-19 11:53:03 -0700234 synchronized (sConfirmSafeVolumeLock) {
235 sConfirmSafeVolumeDialog = null;
236 }
Eric Laurentc34dcc12012-09-10 13:51:52 -0700237 }
238
239 public void onDismiss(DialogInterface unused) {
240 mContext.unregisterReceiver(this);
Eric Laurent0516a9e2012-09-19 11:53:03 -0700241 synchronized (sConfirmSafeVolumeLock) {
242 sConfirmSafeVolumeDialog = null;
243 }
Eric Laurentc34dcc12012-09-10 13:51:52 -0700244 }
245 }
246
247
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800248 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 mContext = context;
250 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
251 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400253 // For now, only show master volume if master volume is supported
254 boolean useMasterVolume = context.getResources().getBoolean(
255 com.android.internal.R.bool.config_useMasterVolume);
256 if (useMasterVolume) {
257 for (int i = 0; i < STREAMS.length; i++) {
258 StreamResources streamRes = STREAMS[i];
259 streamRes.show = (streamRes.streamType == STREAM_MASTER);
260 }
261 }
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 LayoutInflater inflater = (LayoutInflater) context
264 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800265 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
266 mView.setOnTouchListener(new View.OnTouchListener() {
267 public boolean onTouch(View v, MotionEvent event) {
268 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700269 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800270 }
271 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700272 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800273 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
274 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800275 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
276
Amith Yamasani284e6302011-09-16 18:24:47 -0700277 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
278 public boolean onTouchEvent(MotionEvent event) {
279 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
280 forceTimeout();
281 return true;
282 }
283 return false;
284 }
285 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800286 mDialog.setTitle("Volume control"); // No need to localize
287 mDialog.setContentView(mView);
288 mDialog.setOnDismissListener(new OnDismissListener() {
289 public void onDismiss(DialogInterface dialog) {
290 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800291 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800292 }
293 });
294 // Change some window properties
295 Window window = mDialog.getWindow();
296 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700297 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800298 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700299 // Offset from the top
300 lp.y = mContext.getResources().getDimensionPixelOffset(
301 com.android.internal.R.dimen.volume_panel_top);
302 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
303 lp.width = LayoutParams.WRAP_CONTENT;
304 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800305 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700306 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
307 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Jeff Brownc2346132012-04-13 01:55:38 -0700310 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800311
Amith Yamasani71def772011-10-12 12:25:24 -0700312 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400313 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700314 // If we don't want to show multiple volumes, hide the settings button and divider
315 if (!mShowCombinedVolumes) {
316 mMoreButton.setVisibility(View.GONE);
317 mDivider.setVisibility(View.GONE);
318 } else {
319 mMoreButton.setOnClickListener(this);
320 }
321
Christopher Tatec4b78d22012-05-22 13:57:58 -0700322 boolean masterVolumeOnly = context.getResources().getBoolean(
323 com.android.internal.R.bool.config_useMasterVolume);
324 boolean masterVolumeKeySounds = mContext.getResources().getBoolean(
325 com.android.internal.R.bool.config_useVolumeKeySounds);
326
327 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
328
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800329 listenToRingerMode();
330 }
331
332 private void listenToRingerMode() {
333 final IntentFilter filter = new IntentFilter();
334 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
335 mContext.registerReceiver(new BroadcastReceiver() {
336
337 public void onReceive(Context context, Intent intent) {
338 final String action = intent.getAction();
339
340 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
341 removeMessages(MSG_RINGER_MODE_CHANGED);
342 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
343 }
344 }
345 }, filter);
346 }
347
348 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400349 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700350 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700351 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
352 return (mAudioService.getRemoteStreamVolume() <= 0);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400353 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700354 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400355 }
356 }
357
358 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400359 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700360 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700361 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
362 return mAudioService.getRemoteStreamMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400363 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700364 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400365 }
366 }
367
368 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400369 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700370 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700371 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
372 return mAudioService.getRemoteStreamVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400373 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700374 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400375 }
376 }
377
378 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400379 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700380 mAudioManager.setMasterVolume(index, flags);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700381 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
382 mAudioService.setRemoteStreamVolume(index);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400383 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700384 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400385 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800386 }
387
388 private void createSliders() {
389 LayoutInflater inflater = (LayoutInflater) mContext
390 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700391 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700392 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700393 for (int i = 0; i < STREAMS.length; i++) {
394 StreamResources streamRes = STREAMS[i];
395 int streamType = streamRes.streamType;
396 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
397 streamRes = StreamResources.RingerStream;
398 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800399 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700400 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800401 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
402 sc.group.setTag(sc);
403 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800404 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700405 sc.icon.setContentDescription(res.getString(streamRes.descRes));
406 sc.iconRes = streamRes.iconRes;
407 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800408 sc.icon.setImageResource(sc.iconRes);
409 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700410 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
411 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400412 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800413 sc.seekbarView.setOnSeekBarChangeListener(this);
414 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700415 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800416 }
417 }
418
419 private void reorderSliders(int activeStreamType) {
420 mSliderGroup.removeAllViews();
421
422 StreamControl active = mStreamControls.get(activeStreamType);
423 if (active == null) {
424 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
425 mActiveStreamType = -1;
426 } else {
427 mSliderGroup.addView(active.group);
428 mActiveStreamType = activeStreamType;
429 active.group.setVisibility(View.VISIBLE);
430 updateSlider(active);
431 }
432
Amith Yamasani42722bf2011-07-22 10:34:27 -0700433 addOtherVolumes();
434 }
435
436 private void addOtherVolumes() {
437 if (!mShowCombinedVolumes) return;
438
Amith Yamasani71def772011-10-12 12:25:24 -0700439 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800440 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700441 final int streamType = STREAMS[i].streamType;
442 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800443 continue;
444 }
445 StreamControl sc = mStreamControls.get(streamType);
446 mSliderGroup.addView(sc.group);
447 updateSlider(sc);
448 }
449 }
450
451 /** Update the mute and progress state of a slider */
452 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700453 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800454 final boolean muted = isMuted(sc.streamType);
455 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8c787522012-05-14 14:09:43 -0700456 if (sc.streamType == AudioManager.STREAM_RING &&
457 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
Amith Yamasanic696a532011-10-28 17:02:37 -0700458 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
459 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700460 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
461 // never disable touch interactions for remote playback, the muting is not tied to
462 // the state of the phone.
463 sc.seekbarView.setEnabled(true);
464 } else if (sc.streamType != mAudioManager.getMasterStreamType() && muted) {
Eric Laurent8c787522012-05-14 14:09:43 -0700465 sc.seekbarView.setEnabled(false);
466 } else {
467 sc.seekbarView.setEnabled(true);
468 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800469 }
470
471 private boolean isExpanded() {
472 return mMoreButton.getVisibility() != View.VISIBLE;
473 }
474
475 private void expand() {
476 final int count = mSliderGroup.getChildCount();
477 for (int i = 0; i < count; i++) {
478 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
479 }
480 mMoreButton.setVisibility(View.INVISIBLE);
481 mDivider.setVisibility(View.INVISIBLE);
482 }
483
484 private void collapse() {
485 mMoreButton.setVisibility(View.VISIBLE);
486 mDivider.setVisibility(View.VISIBLE);
487 final int count = mSliderGroup.getChildCount();
488 for (int i = 1; i < count; i++) {
489 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
490 }
491 }
492
493 private void updateStates() {
494 final int count = mSliderGroup.getChildCount();
495 for (int i = 0; i < count; i++) {
496 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
497 updateSlider(sc);
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 public void postVolumeChanged(int streamType, int flags) {
502 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700503 synchronized (this) {
504 if (mStreamControls == null) {
505 createSliders();
506 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 removeMessages(MSG_FREE_RESOURCES);
509 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
510 }
511
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700512 public void postRemoteVolumeChanged(int streamType, int flags) {
513 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
514 synchronized (this) {
515 if (mStreamControls == null) {
516 createSliders();
517 }
518 }
519 removeMessages(MSG_FREE_RESOURCES);
520 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget();
521 }
522
523 public void postRemoteSliderVisibility(boolean visible) {
524 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
525 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
526 }
527
528 /**
529 * Called by AudioService when it has received new remote playback information that
530 * would affect the VolumePanel display (mainly volumes). The difference with
531 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
532 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
533 * displayed.
534 * This special code path is due to the fact that remote volume updates arrive to AudioService
535 * asynchronously. So after AudioService has sent the volume update (which should be treated
536 * as a request to update the volume), the application will likely set a new volume. If the UI
537 * is still up, we need to refresh the display to show this new value.
538 */
539 public void postHasNewRemotePlaybackInfo() {
540 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
541 // don't create or prevent resources to be freed, if they disappear, this update came too
542 // late and shouldn't warrant the panel to be displayed longer
543 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
544 }
545
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400546 public void postMasterVolumeChanged(int flags) {
547 postVolumeChanged(STREAM_MASTER, flags);
548 }
549
Mike Lockwoodce952c82011-11-14 10:47:42 -0800550 public void postMuteChanged(int streamType, int flags) {
551 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700552 synchronized (this) {
553 if (mStreamControls == null) {
554 createSliders();
555 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800556 }
557 removeMessages(MSG_FREE_RESOURCES);
558 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
559 }
560
561 public void postMasterMuteChanged(int flags) {
562 postMuteChanged(STREAM_MASTER, flags);
563 }
564
Eric Laurentc34dcc12012-09-10 13:51:52 -0700565 public void postDisplaySafeVolumeWarning() {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700566 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700567 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, 0, 0).sendToTarget();
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 /**
571 * Override this if you have other work to do when the volume changes (for
572 * example, vibrating, playing a sound, etc.). Make sure to call through to
573 * the superclass implementation.
574 */
575 protected void onVolumeChanged(int streamType, int flags) {
576
577 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
578
579 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700580 synchronized (this) {
581 if (mActiveStreamType != streamType) {
582 reorderSliders(streamType);
583 }
584 onShowVolumeChanged(streamType, flags);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587
Marco Nelissen69f593c2009-07-28 09:55:04 -0700588 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 removeMessages(MSG_PLAY_SOUND);
590 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
591 }
592
593 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
594 removeMessages(MSG_PLAY_SOUND);
595 removeMessages(MSG_VIBRATE);
596 onStopSounds();
597 }
598
599 removeMessages(MSG_FREE_RESOURCES);
600 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800601
602 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
Mike Lockwoodce952c82011-11-14 10:47:42 -0800605 protected void onMuteChanged(int streamType, int flags) {
606
607 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
608
609 StreamControl sc = mStreamControls.get(streamType);
610 if (sc != null) {
611 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
612 }
613
614 onVolumeChanged(streamType, flags);
615 }
616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700618 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800619
Marco Nelissen69f593c2009-07-28 09:55:04 -0700620 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
622 if (LOGD) {
623 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
624 + ", flags: " + flags + "), index: " + index);
625 }
626
627 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800628
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400629 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
631 switch (streamType) {
632
633 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800634// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700635 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
636 mContext, RingtoneManager.TYPE_RINGTONE);
637 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700638 mRingIsSilent = true;
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 break;
641 }
642
643 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800644 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800645 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
646 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
647 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
648 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800649 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800651 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653 break;
654 }
655
656 case AudioManager.STREAM_VOICE_CALL: {
657 /*
658 * For in-call voice call volume, there is no inaudible volume.
659 * Rescale the UI control so the progress bar doesn't go all
660 * the way to zero and don't show the mute icon.
661 */
662 index++;
663 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 break;
665 }
666
667 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 break;
669 }
670
671 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700672 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
673 mContext, RingtoneManager.TYPE_NOTIFICATION);
674 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700675 mRingIsSilent = true;
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 break;
678 }
679
680 case AudioManager.STREAM_BLUETOOTH_SCO: {
681 /*
682 * For in-call voice call volume, there is no inaudible volume.
683 * Rescale the UI control so the progress bar doesn't go all
684 * the way to zero and don't show the mute icon.
685 */
686 index++;
687 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 break;
689 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700690
691 case AudioService.STREAM_REMOTE_MUSIC: {
692 if (LOGD) { Log.d(TAG, "showing remote volume "+index+" over "+ max); }
693 break;
694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800697 StreamControl sc = mStreamControls.get(streamType);
698 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700699 if (sc.seekbarView.getMax() != max) {
700 sc.seekbarView.setMax(max);
701 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800702 sc.seekbarView.setProgress(index);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700703 if (streamType != mAudioManager.getMasterStreamType()
704 && streamType != AudioService.STREAM_REMOTE_MUSIC && isMuted(streamType)) {
Eric Laurent8c787522012-05-14 14:09:43 -0700705 sc.seekbarView.setEnabled(false);
706 } else {
707 sc.seekbarView.setEnabled(true);
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800711 if (!mDialog.isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700712 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
713 // when the stream is for remote playback, use -1 to reset the stream type evaluation
714 mAudioManager.forceVolumeControlStream(stream);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800715 mDialog.setContentView(mView);
716 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700717 if (mShowCombinedVolumes) {
718 collapse();
719 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800720 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700724 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
725 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 mAudioService.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700727 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 protected void onPlaySound(int streamType, int flags) {
733
734 if (hasMessages(MSG_STOP_SOUNDS)) {
735 removeMessages(MSG_STOP_SOUNDS);
736 // Force stop right now
737 onStopSounds();
738 }
739
740 synchronized (this) {
741 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800742 if (toneGen != null) {
743 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
744 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
749 protected void onStopSounds() {
750
751 synchronized (this) {
752 int numStreamTypes = AudioSystem.getNumStreamTypes();
753 for (int i = numStreamTypes - 1; i >= 0; i--) {
754 ToneGenerator toneGen = mToneGenerators[i];
755 if (toneGen != null) {
756 toneGen.stopTone();
757 }
758 }
759 }
760 }
761
762 protected void onVibrate() {
763
764 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700765 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 return;
767 }
768
769 mVibrator.vibrate(VIBRATE_DURATION);
770 }
771
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700772 protected void onRemoteVolumeChanged(int streamType, int flags) {
773 // streamType is the real stream type being affected, but for the UI sliders, we
774 // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real
775 // stream type.
776 if (LOGD) Log.d(TAG, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")");
777
778 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || mDialog.isShowing()) {
779 synchronized (this) {
780 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
781 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
782 }
783 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags);
784 }
785 } else {
786 if (LOGD) Log.d(TAG, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
787 }
788
789 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
790 removeMessages(MSG_PLAY_SOUND);
791 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
792 }
793
794 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
795 removeMessages(MSG_PLAY_SOUND);
796 removeMessages(MSG_VIBRATE);
797 onStopSounds();
798 }
799
800 removeMessages(MSG_FREE_RESOURCES);
801 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
802
803 resetTimeout();
804 }
805
806 protected void onRemoteVolumeUpdateIfShown() {
807 if (LOGD) Log.d(TAG, "onRemoteVolumeUpdateIfShown()");
808 if (mDialog.isShowing()
809 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
810 && (mStreamControls != null)) {
811 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0);
812 }
813 }
814
815
816 /**
817 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
818 * Hide or show a slider
819 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
820 * or AudioService.STREAM_REMOTE_MUSIC
821 * @param visible
822 */
823 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
824 if (LOGD) Log.d(TAG, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
825 boolean isVisible = (visible == 1);
826 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
827 StreamResources streamRes = STREAMS[i];
828 if (streamRes.streamType == streamType) {
829 streamRes.show = isVisible;
830 if (!isVisible && (mActiveStreamType == streamType)) {
831 mActiveStreamType = -1;
832 }
833 break;
834 }
835 }
836 }
837
Eric Laurentc34dcc12012-09-10 13:51:52 -0700838 protected void onDisplaySafeVolumeWarning() {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700839 synchronized (sConfirmSafeVolumeLock) {
840 if (sConfirmSafeVolumeDialog != null) {
841 return;
842 }
843 sConfirmSafeVolumeDialog = new AlertDialog.Builder(mContext)
844 .setMessage(com.android.internal.R.string.safe_media_volume_warning)
845 .setPositiveButton(com.android.internal.R.string.yes,
846 new DialogInterface.OnClickListener() {
847 public void onClick(DialogInterface dialog, int which) {
848 mAudioService.disableSafeMediaVolume();
849 }
850 })
851 .setNegativeButton(com.android.internal.R.string.no, null)
852 .setIconAttribute(android.R.attr.alertDialogIcon)
853 .create();
854 final WarningDialogReceiver warning = new WarningDialogReceiver(mContext,
855 sConfirmSafeVolumeDialog);
856
857 sConfirmSafeVolumeDialog.setOnDismissListener(warning);
858 sConfirmSafeVolumeDialog.getWindow().setType(
859 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
860 sConfirmSafeVolumeDialog.show();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700861 }
Eric Laurentc34dcc12012-09-10 13:51:52 -0700862 }
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 /**
865 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
866 */
867 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -0700868 if (streamType == STREAM_MASTER) {
869 // For devices that use the master volume setting only but still want to
870 // play a volume-changed tone, direct the master volume pseudostream to
871 // the system stream's tone generator.
872 if (mPlayMasterStreamTones) {
873 streamType = AudioManager.STREAM_SYSTEM;
874 } else {
875 return null;
876 }
877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 synchronized (this) {
879 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800880 try {
881 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
882 } catch (RuntimeException e) {
883 if (LOGD) {
884 Log.d(TAG, "ToneGenerator constructor failed with "
885 + "RuntimeException: " + e);
886 }
887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800889 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
891 }
892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893
894 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800895 * Switch between icons because Bluetooth music is same as music volume, but with
896 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800898 private void setMusicIcon(int resId, int resMuteId) {
899 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
900 if (sc != null) {
901 sc.iconRes = resId;
902 sc.iconMuteRes = resMuteId;
903 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906
907 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 synchronized (this) {
909 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
910 if (mToneGenerators[i] != null) {
911 mToneGenerators[i].release();
912 }
913 mToneGenerators[i] = null;
914 }
915 }
916 }
917
918 @Override
919 public void handleMessage(Message msg) {
920 switch (msg.what) {
921
922 case MSG_VOLUME_CHANGED: {
923 onVolumeChanged(msg.arg1, msg.arg2);
924 break;
925 }
926
Mike Lockwoodce952c82011-11-14 10:47:42 -0800927 case MSG_MUTE_CHANGED: {
928 onMuteChanged(msg.arg1, msg.arg2);
929 break;
930 }
931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 case MSG_FREE_RESOURCES: {
933 onFreeResources();
934 break;
935 }
936
937 case MSG_STOP_SOUNDS: {
938 onStopSounds();
939 break;
940 }
941
942 case MSG_PLAY_SOUND: {
943 onPlaySound(msg.arg1, msg.arg2);
944 break;
945 }
946
947 case MSG_VIBRATE: {
948 onVibrate();
949 break;
950 }
951
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800952 case MSG_TIMEOUT: {
953 if (mDialog.isShowing()) {
954 mDialog.dismiss();
955 mActiveStreamType = -1;
956 }
957 break;
958 }
959 case MSG_RINGER_MODE_CHANGED: {
960 if (mDialog.isShowing()) {
961 updateStates();
962 }
963 break;
964 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700965
966 case MSG_REMOTE_VOLUME_CHANGED: {
967 onRemoteVolumeChanged(msg.arg1, msg.arg2);
968 break;
969 }
970
971 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
972 onRemoteVolumeUpdateIfShown();
973 break;
974
975 case MSG_SLIDER_VISIBILITY_CHANGED:
976 onSliderVisibilityChanged(msg.arg1, msg.arg2);
977 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700978
979 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
980 onDisplaySafeVolumeWarning();
981 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983 }
984
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800985 private void resetTimeout() {
986 removeMessages(MSG_TIMEOUT);
987 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
988 }
989
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700990 private void forceTimeout() {
991 removeMessages(MSG_TIMEOUT);
992 sendMessage(obtainMessage(MSG_TIMEOUT));
993 }
994
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800995 public void onProgressChanged(SeekBar seekBar, int progress,
996 boolean fromUser) {
997 final Object tag = seekBar.getTag();
998 if (fromUser && tag instanceof StreamControl) {
999 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -04001000 if (getStreamVolume(sc.streamType) != progress) {
1001 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001002 }
1003 }
1004 resetTimeout();
1005 }
1006
1007 public void onStartTrackingTouch(SeekBar seekBar) {
1008 }
1009
1010 public void onStopTrackingTouch(SeekBar seekBar) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001011 final Object tag = seekBar.getTag();
1012 if (tag instanceof StreamControl) {
1013 StreamControl sc = (StreamControl) tag;
1014 // because remote volume updates are asynchronous, AudioService might have received
1015 // a new remote volume value since the finger adjusted the slider. So when the
1016 // progress of the slider isn't being tracked anymore, adjust the slider to the last
1017 // "published" remote volume value, so the UI reflects the actual volume.
1018 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
1019 seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC));
1020 }
1021 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001022 }
1023
1024 public void onClick(View v) {
1025 if (v == mMoreButton) {
1026 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001027 }
1028 resetTimeout();
1029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030}