blob: cf9bcdd8616cac00b6352f4926633f851810228c [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
21import android.app.Dialog;
22import android.content.DialogInterface.OnDismissListener;
23import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080025import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080027import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.Resources;
29import android.media.AudioManager;
30import android.media.AudioService;
31import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070032import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.media.ToneGenerator;
Marco Nelissen69f593c2009-07-28 09:55:04 -070034import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Handler;
36import android.os.Message;
37import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.util.Log;
Amith Yamasani284e6302011-09-16 18:24:47 -070039import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080041import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080042import android.widget.SeekBar.OnSeekBarChangeListener;
43
44import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46/**
47 * Handle the volume up and down keys.
48 *
49 * This code really should be moved elsewhere.
50 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070051 * Seriously, it really really should be moved elsewhere. This is used by
52 * android.media.AudioService, which actually runs in the system process, to
53 * show the volume dialog when the user changes the volume. What a mess.
54 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * @hide
56 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080057public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058{
59 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070060 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 /**
63 * The delay before playing a sound. This small period exists so the user
64 * can press another key (non-volume keys, too) to have it NOT be audible.
65 * <p>
66 * PhoneWindow will implement this part.
67 */
68 public static final int PLAY_SOUND_DELAY = 300;
69
70 /**
71 * The delay before vibrating. This small period exists so if the user is
72 * moving to silent mode, it will not emit a short vibrate (it normally
73 * would since vibrate is between normal mode and silent mode using hardware
74 * keys).
75 */
76 public static final int VIBRATE_DELAY = 300;
77
78 private static final int VIBRATE_DURATION = 300;
79 private static final int BEEP_DURATION = 150;
80 private static final int MAX_VOLUME = 100;
81 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080082 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 private static final int MSG_VOLUME_CHANGED = 0;
85 private static final int MSG_FREE_RESOURCES = 1;
86 private static final int MSG_PLAY_SOUND = 2;
87 private static final int MSG_STOP_SOUNDS = 3;
88 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080089 private static final int MSG_TIMEOUT = 5;
90 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -080091 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -070092 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
93 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
94 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040096 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -080097 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -070098 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 protected Context mContext;
101 private AudioManager mAudioManager;
102 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700103 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700104 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700105 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800107 /** Dialog containing all the sliders */
108 private final Dialog mDialog;
109 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800111
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700112 /** The visible portion of the volume overlay */
113 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800114 /** Contains the sliders and their touchable icons */
115 private final ViewGroup mSliderGroup;
116 /** The button that expands the dialog to show all sliders */
117 private final View mMoreButton;
118 /** Dummy divider icon that needs to vanish with the more button */
119 private final View mDivider;
120
121 /** Currently active stream that shows up at the top of the list of sliders */
122 private int mActiveStreamType = -1;
123 /** All the slider controls mapped by stream type */
124 private HashMap<Integer,StreamControl> mStreamControls;
125
Amith Yamasani71def772011-10-12 12:25:24 -0700126 private enum StreamResources {
127 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
128 R.string.volume_icon_description_bluetooth,
129 R.drawable.ic_audio_bt,
130 R.drawable.ic_audio_bt,
131 false),
132 RingerStream(AudioManager.STREAM_RING,
133 R.string.volume_icon_description_ringer,
134 R.drawable.ic_audio_ring_notif,
135 R.drawable.ic_audio_ring_notif_mute,
136 false),
137 VoiceStream(AudioManager.STREAM_VOICE_CALL,
138 R.string.volume_icon_description_incall,
139 R.drawable.ic_audio_phone,
140 R.drawable.ic_audio_phone,
141 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700142 AlarmStream(AudioManager.STREAM_ALARM,
143 R.string.volume_alarm,
144 R.drawable.ic_audio_alarm,
145 R.drawable.ic_audio_alarm_mute,
146 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700147 MediaStream(AudioManager.STREAM_MUSIC,
148 R.string.volume_icon_description_media,
149 R.drawable.ic_audio_vol,
150 R.drawable.ic_audio_vol_mute,
151 true),
152 NotificationStream(AudioManager.STREAM_NOTIFICATION,
153 R.string.volume_icon_description_notification,
154 R.drawable.ic_audio_notification,
155 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400156 true),
157 // for now, use media resources for master volume
158 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700159 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400160 R.drawable.ic_audio_vol,
161 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700162 false),
163 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
164 R.string.volume_icon_description_media, //FIXME should have its own description
165 R.drawable.ic_media_route_on_holo_dark,
166 R.drawable.ic_media_route_disabled_holo_dark,
167 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700168
169 int streamType;
170 int descRes;
171 int iconRes;
172 int iconMuteRes;
173 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
174 boolean show;
175
176 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
177 this.streamType = streamType;
178 this.descRes = descRes;
179 this.iconRes = iconRes;
180 this.iconMuteRes = iconMuteRes;
181 this.show = show;
182 }
183 };
184
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800185 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700186 private static final StreamResources[] STREAMS = {
187 StreamResources.BluetoothSCOStream,
188 StreamResources.RingerStream,
189 StreamResources.VoiceStream,
190 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700191 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400192 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700193 StreamResources.MasterStream,
194 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800195 };
196
197 /** Object that contains data for each slider */
198 private class StreamControl {
199 int streamType;
200 ViewGroup group;
201 ImageView icon;
202 SeekBar seekbarView;
203 int iconRes;
204 int iconMuteRes;
205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
207 // Synchronize when accessing this
208 private ToneGenerator mToneGenerators[];
209 private Vibrator mVibrator;
210
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800211 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mContext = context;
213 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
214 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400216 // For now, only show master volume if master volume is supported
217 boolean useMasterVolume = context.getResources().getBoolean(
218 com.android.internal.R.bool.config_useMasterVolume);
219 if (useMasterVolume) {
220 for (int i = 0; i < STREAMS.length; i++) {
221 StreamResources streamRes = STREAMS[i];
222 streamRes.show = (streamRes.streamType == STREAM_MASTER);
223 }
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 LayoutInflater inflater = (LayoutInflater) context
227 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800228 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
229 mView.setOnTouchListener(new View.OnTouchListener() {
230 public boolean onTouch(View v, MotionEvent event) {
231 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700232 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800233 }
234 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700235 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800236 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
237 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800238 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
239
Amith Yamasani284e6302011-09-16 18:24:47 -0700240 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
241 public boolean onTouchEvent(MotionEvent event) {
242 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
243 forceTimeout();
244 return true;
245 }
246 return false;
247 }
248 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800249 mDialog.setTitle("Volume control"); // No need to localize
250 mDialog.setContentView(mView);
251 mDialog.setOnDismissListener(new OnDismissListener() {
252 public void onDismiss(DialogInterface dialog) {
253 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800254 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800255 }
256 });
257 // Change some window properties
258 Window window = mDialog.getWindow();
259 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700260 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800261 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700262 // Offset from the top
263 lp.y = mContext.getResources().getDimensionPixelOffset(
264 com.android.internal.R.dimen.volume_panel_top);
265 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
266 lp.width = LayoutParams.WRAP_CONTENT;
267 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800268 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700269 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
270 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Jeff Brownc2346132012-04-13 01:55:38 -0700273 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800274
Amith Yamasani71def772011-10-12 12:25:24 -0700275 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400276 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700277 // If we don't want to show multiple volumes, hide the settings button and divider
278 if (!mShowCombinedVolumes) {
279 mMoreButton.setVisibility(View.GONE);
280 mDivider.setVisibility(View.GONE);
281 } else {
282 mMoreButton.setOnClickListener(this);
283 }
284
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800285 listenToRingerMode();
286 }
287
288 private void listenToRingerMode() {
289 final IntentFilter filter = new IntentFilter();
290 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
291 mContext.registerReceiver(new BroadcastReceiver() {
292
293 public void onReceive(Context context, Intent intent) {
294 final String action = intent.getAction();
295
296 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
297 removeMessages(MSG_RINGER_MODE_CHANGED);
298 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
299 }
300 }
301 }, filter);
302 }
303
304 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400305 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700306 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700307 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
308 return (mAudioService.getRemoteStreamVolume() <= 0);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400309 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700310 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400311 }
312 }
313
314 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400315 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700316 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700317 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
318 return mAudioService.getRemoteStreamMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400319 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700320 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400321 }
322 }
323
324 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400325 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700326 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700327 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
328 return mAudioService.getRemoteStreamVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400329 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700330 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400331 }
332 }
333
334 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400335 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700336 mAudioManager.setMasterVolume(index, flags);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700337 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
338 mAudioService.setRemoteStreamVolume(index);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400339 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700340 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400341 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800342 }
343
344 private void createSliders() {
345 LayoutInflater inflater = (LayoutInflater) mContext
346 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700347 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700348 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700349 for (int i = 0; i < STREAMS.length; i++) {
350 StreamResources streamRes = STREAMS[i];
351 int streamType = streamRes.streamType;
352 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
353 streamRes = StreamResources.RingerStream;
354 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800355 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700356 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800357 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
358 sc.group.setTag(sc);
359 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800360 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700361 sc.icon.setContentDescription(res.getString(streamRes.descRes));
362 sc.iconRes = streamRes.iconRes;
363 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800364 sc.icon.setImageResource(sc.iconRes);
365 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700366 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
367 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400368 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800369 sc.seekbarView.setOnSeekBarChangeListener(this);
370 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700371 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800372 }
373 }
374
375 private void reorderSliders(int activeStreamType) {
376 mSliderGroup.removeAllViews();
377
378 StreamControl active = mStreamControls.get(activeStreamType);
379 if (active == null) {
380 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
381 mActiveStreamType = -1;
382 } else {
383 mSliderGroup.addView(active.group);
384 mActiveStreamType = activeStreamType;
385 active.group.setVisibility(View.VISIBLE);
386 updateSlider(active);
387 }
388
Amith Yamasani42722bf2011-07-22 10:34:27 -0700389 addOtherVolumes();
390 }
391
392 private void addOtherVolumes() {
393 if (!mShowCombinedVolumes) return;
394
Amith Yamasani71def772011-10-12 12:25:24 -0700395 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800396 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700397 final int streamType = STREAMS[i].streamType;
398 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800399 continue;
400 }
401 StreamControl sc = mStreamControls.get(streamType);
402 mSliderGroup.addView(sc.group);
403 updateSlider(sc);
404 }
405 }
406
407 /** Update the mute and progress state of a slider */
408 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700409 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800410 final boolean muted = isMuted(sc.streamType);
411 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8c787522012-05-14 14:09:43 -0700412 if (sc.streamType == AudioManager.STREAM_RING &&
413 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
Amith Yamasanic696a532011-10-28 17:02:37 -0700414 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
415 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700416 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
417 // never disable touch interactions for remote playback, the muting is not tied to
418 // the state of the phone.
419 sc.seekbarView.setEnabled(true);
420 } else if (sc.streamType != mAudioManager.getMasterStreamType() && muted) {
Eric Laurent8c787522012-05-14 14:09:43 -0700421 sc.seekbarView.setEnabled(false);
422 } else {
423 sc.seekbarView.setEnabled(true);
424 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800425 }
426
427 private boolean isExpanded() {
428 return mMoreButton.getVisibility() != View.VISIBLE;
429 }
430
431 private void expand() {
432 final int count = mSliderGroup.getChildCount();
433 for (int i = 0; i < count; i++) {
434 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
435 }
436 mMoreButton.setVisibility(View.INVISIBLE);
437 mDivider.setVisibility(View.INVISIBLE);
438 }
439
440 private void collapse() {
441 mMoreButton.setVisibility(View.VISIBLE);
442 mDivider.setVisibility(View.VISIBLE);
443 final int count = mSliderGroup.getChildCount();
444 for (int i = 1; i < count; i++) {
445 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
446 }
447 }
448
449 private void updateStates() {
450 final int count = mSliderGroup.getChildCount();
451 for (int i = 0; i < count; i++) {
452 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
453 updateSlider(sc);
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
457 public void postVolumeChanged(int streamType, int flags) {
458 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700459 synchronized (this) {
460 if (mStreamControls == null) {
461 createSliders();
462 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 removeMessages(MSG_FREE_RESOURCES);
465 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
466 }
467
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700468 public void postRemoteVolumeChanged(int streamType, int flags) {
469 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
470 synchronized (this) {
471 if (mStreamControls == null) {
472 createSliders();
473 }
474 }
475 removeMessages(MSG_FREE_RESOURCES);
476 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget();
477 }
478
479 public void postRemoteSliderVisibility(boolean visible) {
480 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
481 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
482 }
483
484 /**
485 * Called by AudioService when it has received new remote playback information that
486 * would affect the VolumePanel display (mainly volumes). The difference with
487 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
488 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
489 * displayed.
490 * This special code path is due to the fact that remote volume updates arrive to AudioService
491 * asynchronously. So after AudioService has sent the volume update (which should be treated
492 * as a request to update the volume), the application will likely set a new volume. If the UI
493 * is still up, we need to refresh the display to show this new value.
494 */
495 public void postHasNewRemotePlaybackInfo() {
496 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
497 // don't create or prevent resources to be freed, if they disappear, this update came too
498 // late and shouldn't warrant the panel to be displayed longer
499 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
500 }
501
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400502 public void postMasterVolumeChanged(int flags) {
503 postVolumeChanged(STREAM_MASTER, flags);
504 }
505
Mike Lockwoodce952c82011-11-14 10:47:42 -0800506 public void postMuteChanged(int streamType, int flags) {
507 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700508 synchronized (this) {
509 if (mStreamControls == null) {
510 createSliders();
511 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800512 }
513 removeMessages(MSG_FREE_RESOURCES);
514 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
515 }
516
517 public void postMasterMuteChanged(int flags) {
518 postMuteChanged(STREAM_MASTER, flags);
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 /**
522 * Override this if you have other work to do when the volume changes (for
523 * example, vibrating, playing a sound, etc.). Make sure to call through to
524 * the superclass implementation.
525 */
526 protected void onVolumeChanged(int streamType, int flags) {
527
528 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
529
530 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700531 synchronized (this) {
532 if (mActiveStreamType != streamType) {
533 reorderSliders(streamType);
534 }
535 onShowVolumeChanged(streamType, flags);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538
Marco Nelissen69f593c2009-07-28 09:55:04 -0700539 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 removeMessages(MSG_PLAY_SOUND);
541 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
542 }
543
544 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
545 removeMessages(MSG_PLAY_SOUND);
546 removeMessages(MSG_VIBRATE);
547 onStopSounds();
548 }
549
550 removeMessages(MSG_FREE_RESOURCES);
551 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800552
553 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555
Mike Lockwoodce952c82011-11-14 10:47:42 -0800556 protected void onMuteChanged(int streamType, int flags) {
557
558 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
559
560 StreamControl sc = mStreamControls.get(streamType);
561 if (sc != null) {
562 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
563 }
564
565 onVolumeChanged(streamType, flags);
566 }
567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700569 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800570
Marco Nelissen69f593c2009-07-28 09:55:04 -0700571 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572
573 if (LOGD) {
574 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
575 + ", flags: " + flags + "), index: " + index);
576 }
577
578 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800579
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400580 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581
582 switch (streamType) {
583
584 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800585// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700586 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
587 mContext, RingtoneManager.TYPE_RINGTONE);
588 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700589 mRingIsSilent = true;
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 break;
592 }
593
594 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800595 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800596 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
597 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
598 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
599 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800600 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800602 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604 break;
605 }
606
607 case AudioManager.STREAM_VOICE_CALL: {
608 /*
609 * For in-call voice call volume, there is no inaudible volume.
610 * Rescale the UI control so the progress bar doesn't go all
611 * the way to zero and don't show the mute icon.
612 */
613 index++;
614 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 break;
616 }
617
618 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 break;
620 }
621
622 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700623 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
624 mContext, RingtoneManager.TYPE_NOTIFICATION);
625 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700626 mRingIsSilent = true;
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 break;
629 }
630
631 case AudioManager.STREAM_BLUETOOTH_SCO: {
632 /*
633 * For in-call voice call volume, there is no inaudible volume.
634 * Rescale the UI control so the progress bar doesn't go all
635 * the way to zero and don't show the mute icon.
636 */
637 index++;
638 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 break;
640 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700641
642 case AudioService.STREAM_REMOTE_MUSIC: {
643 if (LOGD) { Log.d(TAG, "showing remote volume "+index+" over "+ max); }
644 break;
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800648 StreamControl sc = mStreamControls.get(streamType);
649 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700650 if (sc.seekbarView.getMax() != max) {
651 sc.seekbarView.setMax(max);
652 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800653 sc.seekbarView.setProgress(index);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700654 if (streamType != mAudioManager.getMasterStreamType()
655 && streamType != AudioService.STREAM_REMOTE_MUSIC && isMuted(streamType)) {
Eric Laurent8c787522012-05-14 14:09:43 -0700656 sc.seekbarView.setEnabled(false);
657 } else {
658 sc.seekbarView.setEnabled(true);
659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 }
661
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800662 if (!mDialog.isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700663 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
664 // when the stream is for remote playback, use -1 to reset the stream type evaluation
665 mAudioManager.forceVolumeControlStream(stream);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800666 mDialog.setContentView(mView);
667 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700668 if (mShowCombinedVolumes) {
669 collapse();
670 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800671 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700675 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
676 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 mAudioService.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700678 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
683 protected void onPlaySound(int streamType, int flags) {
684
685 if (hasMessages(MSG_STOP_SOUNDS)) {
686 removeMessages(MSG_STOP_SOUNDS);
687 // Force stop right now
688 onStopSounds();
689 }
690
691 synchronized (this) {
692 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800693 if (toneGen != null) {
694 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
695 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 protected void onStopSounds() {
701
702 synchronized (this) {
703 int numStreamTypes = AudioSystem.getNumStreamTypes();
704 for (int i = numStreamTypes - 1; i >= 0; i--) {
705 ToneGenerator toneGen = mToneGenerators[i];
706 if (toneGen != null) {
707 toneGen.stopTone();
708 }
709 }
710 }
711 }
712
713 protected void onVibrate() {
714
715 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700716 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 return;
718 }
719
720 mVibrator.vibrate(VIBRATE_DURATION);
721 }
722
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700723 protected void onRemoteVolumeChanged(int streamType, int flags) {
724 // streamType is the real stream type being affected, but for the UI sliders, we
725 // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real
726 // stream type.
727 if (LOGD) Log.d(TAG, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")");
728
729 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || mDialog.isShowing()) {
730 synchronized (this) {
731 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
732 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
733 }
734 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags);
735 }
736 } else {
737 if (LOGD) Log.d(TAG, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
738 }
739
740 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
741 removeMessages(MSG_PLAY_SOUND);
742 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
743 }
744
745 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
746 removeMessages(MSG_PLAY_SOUND);
747 removeMessages(MSG_VIBRATE);
748 onStopSounds();
749 }
750
751 removeMessages(MSG_FREE_RESOURCES);
752 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
753
754 resetTimeout();
755 }
756
757 protected void onRemoteVolumeUpdateIfShown() {
758 if (LOGD) Log.d(TAG, "onRemoteVolumeUpdateIfShown()");
759 if (mDialog.isShowing()
760 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
761 && (mStreamControls != null)) {
762 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0);
763 }
764 }
765
766
767 /**
768 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
769 * Hide or show a slider
770 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
771 * or AudioService.STREAM_REMOTE_MUSIC
772 * @param visible
773 */
774 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
775 if (LOGD) Log.d(TAG, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
776 boolean isVisible = (visible == 1);
777 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
778 StreamResources streamRes = STREAMS[i];
779 if (streamRes.streamType == streamType) {
780 streamRes.show = isVisible;
781 if (!isVisible && (mActiveStreamType == streamType)) {
782 mActiveStreamType = -1;
783 }
784 break;
785 }
786 }
787 }
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 /**
790 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
791 */
792 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400793 if (streamType == STREAM_MASTER) return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 synchronized (this) {
795 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800796 try {
797 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
798 } catch (RuntimeException e) {
799 if (LOGD) {
800 Log.d(TAG, "ToneGenerator constructor failed with "
801 + "RuntimeException: " + e);
802 }
803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800805 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807 }
808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
810 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800811 * Switch between icons because Bluetooth music is same as music volume, but with
812 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800814 private void setMusicIcon(int resId, int resMuteId) {
815 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
816 if (sc != null) {
817 sc.iconRes = resId;
818 sc.iconMuteRes = resMuteId;
819 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
823 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 synchronized (this) {
825 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
826 if (mToneGenerators[i] != null) {
827 mToneGenerators[i].release();
828 }
829 mToneGenerators[i] = null;
830 }
831 }
832 }
833
834 @Override
835 public void handleMessage(Message msg) {
836 switch (msg.what) {
837
838 case MSG_VOLUME_CHANGED: {
839 onVolumeChanged(msg.arg1, msg.arg2);
840 break;
841 }
842
Mike Lockwoodce952c82011-11-14 10:47:42 -0800843 case MSG_MUTE_CHANGED: {
844 onMuteChanged(msg.arg1, msg.arg2);
845 break;
846 }
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 case MSG_FREE_RESOURCES: {
849 onFreeResources();
850 break;
851 }
852
853 case MSG_STOP_SOUNDS: {
854 onStopSounds();
855 break;
856 }
857
858 case MSG_PLAY_SOUND: {
859 onPlaySound(msg.arg1, msg.arg2);
860 break;
861 }
862
863 case MSG_VIBRATE: {
864 onVibrate();
865 break;
866 }
867
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800868 case MSG_TIMEOUT: {
869 if (mDialog.isShowing()) {
870 mDialog.dismiss();
871 mActiveStreamType = -1;
872 }
873 break;
874 }
875 case MSG_RINGER_MODE_CHANGED: {
876 if (mDialog.isShowing()) {
877 updateStates();
878 }
879 break;
880 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700881
882 case MSG_REMOTE_VOLUME_CHANGED: {
883 onRemoteVolumeChanged(msg.arg1, msg.arg2);
884 break;
885 }
886
887 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
888 onRemoteVolumeUpdateIfShown();
889 break;
890
891 case MSG_SLIDER_VISIBILITY_CHANGED:
892 onSliderVisibilityChanged(msg.arg1, msg.arg2);
893 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895 }
896
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800897 private void resetTimeout() {
898 removeMessages(MSG_TIMEOUT);
899 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
900 }
901
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700902 private void forceTimeout() {
903 removeMessages(MSG_TIMEOUT);
904 sendMessage(obtainMessage(MSG_TIMEOUT));
905 }
906
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800907 public void onProgressChanged(SeekBar seekBar, int progress,
908 boolean fromUser) {
909 final Object tag = seekBar.getTag();
910 if (fromUser && tag instanceof StreamControl) {
911 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400912 if (getStreamVolume(sc.streamType) != progress) {
913 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800914 }
915 }
916 resetTimeout();
917 }
918
919 public void onStartTrackingTouch(SeekBar seekBar) {
920 }
921
922 public void onStopTrackingTouch(SeekBar seekBar) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700923 final Object tag = seekBar.getTag();
924 if (tag instanceof StreamControl) {
925 StreamControl sc = (StreamControl) tag;
926 // because remote volume updates are asynchronous, AudioService might have received
927 // a new remote volume value since the finger adjusted the slider. So when the
928 // progress of the slider isn't being tracked anymore, adjust the slider to the last
929 // "published" remote volume value, so the UI reflects the actual volume.
930 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
931 seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC));
932 }
933 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800934 }
935
936 public void onClick(View v) {
937 if (v == mMoreButton) {
938 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800939 }
940 resetTimeout();
941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942}