blob: 9152cc344949231c2cb5ee260f502c842f59030a [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;
Amith Yamasani284e6302011-09-16 18:24:47 -070037import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Vibrator;
Amith Yamasani9b8e8482011-08-10 15:55:36 -070039import android.provider.Settings;
40import android.provider.Settings.System;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.Log;
Amith Yamasani284e6302011-09-16 18:24:47 -070042import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080044import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080045import android.widget.SeekBar.OnSeekBarChangeListener;
46
47import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49/**
50 * Handle the volume up and down keys.
51 *
52 * This code really should be moved elsewhere.
53 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070054 * Seriously, it really really should be moved elsewhere. This is used by
55 * android.media.AudioService, which actually runs in the system process, to
56 * show the volume dialog when the user changes the volume. What a mess.
57 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 * @hide
59 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080060public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
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;
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;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 protected Context mContext;
100 private AudioManager mAudioManager;
101 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700102 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700103 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700104 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800106 /** Dialog containing all the sliders */
107 private final Dialog mDialog;
108 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800110
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700111 /** The visible portion of the volume overlay */
112 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800113 /** Contains the sliders and their touchable icons */
114 private final ViewGroup mSliderGroup;
115 /** The button that expands the dialog to show all sliders */
116 private final View mMoreButton;
117 /** Dummy divider icon that needs to vanish with the more button */
118 private final View mDivider;
119
120 /** Currently active stream that shows up at the top of the list of sliders */
121 private int mActiveStreamType = -1;
122 /** All the slider controls mapped by stream type */
123 private HashMap<Integer,StreamControl> mStreamControls;
124
Amith Yamasani71def772011-10-12 12:25:24 -0700125 private enum StreamResources {
126 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
127 R.string.volume_icon_description_bluetooth,
128 R.drawable.ic_audio_bt,
129 R.drawable.ic_audio_bt,
130 false),
131 RingerStream(AudioManager.STREAM_RING,
132 R.string.volume_icon_description_ringer,
133 R.drawable.ic_audio_ring_notif,
134 R.drawable.ic_audio_ring_notif_mute,
135 false),
136 VoiceStream(AudioManager.STREAM_VOICE_CALL,
137 R.string.volume_icon_description_incall,
138 R.drawable.ic_audio_phone,
139 R.drawable.ic_audio_phone,
140 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700141 AlarmStream(AudioManager.STREAM_ALARM,
142 R.string.volume_alarm,
143 R.drawable.ic_audio_alarm,
144 R.drawable.ic_audio_alarm_mute,
145 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700146 MediaStream(AudioManager.STREAM_MUSIC,
147 R.string.volume_icon_description_media,
148 R.drawable.ic_audio_vol,
149 R.drawable.ic_audio_vol_mute,
150 true),
151 NotificationStream(AudioManager.STREAM_NOTIFICATION,
152 R.string.volume_icon_description_notification,
153 R.drawable.ic_audio_notification,
154 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400155 true),
156 // for now, use media resources for master volume
157 MasterStream(STREAM_MASTER,
158 R.string.volume_icon_description_media,
159 R.drawable.ic_audio_vol,
160 R.drawable.ic_audio_vol_mute,
161 false);
Amith Yamasani71def772011-10-12 12:25:24 -0700162
163 int streamType;
164 int descRes;
165 int iconRes;
166 int iconMuteRes;
167 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
168 boolean show;
169
170 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
171 this.streamType = streamType;
172 this.descRes = descRes;
173 this.iconRes = iconRes;
174 this.iconMuteRes = iconMuteRes;
175 this.show = show;
176 }
177 };
178
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800179 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700180 private static final StreamResources[] STREAMS = {
181 StreamResources.BluetoothSCOStream,
182 StreamResources.RingerStream,
183 StreamResources.VoiceStream,
184 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700185 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400186 StreamResources.AlarmStream,
187 StreamResources.MasterStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800188 };
189
190 /** Object that contains data for each slider */
191 private class StreamControl {
192 int streamType;
193 ViewGroup group;
194 ImageView icon;
195 SeekBar seekbarView;
196 int iconRes;
197 int iconMuteRes;
198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 // Synchronize when accessing this
201 private ToneGenerator mToneGenerators[];
202 private Vibrator mVibrator;
203
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800204 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 mContext = context;
206 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
207 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400209 // For now, only show master volume if master volume is supported
210 boolean useMasterVolume = context.getResources().getBoolean(
211 com.android.internal.R.bool.config_useMasterVolume);
212 if (useMasterVolume) {
213 for (int i = 0; i < STREAMS.length; i++) {
214 StreamResources streamRes = STREAMS[i];
215 streamRes.show = (streamRes.streamType == STREAM_MASTER);
216 }
217 }
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 LayoutInflater inflater = (LayoutInflater) context
220 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800221 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
222 mView.setOnTouchListener(new View.OnTouchListener() {
223 public boolean onTouch(View v, MotionEvent event) {
224 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700225 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800226 }
227 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700228 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800229 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
230 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800231 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
232
Amith Yamasani284e6302011-09-16 18:24:47 -0700233 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
234 public boolean onTouchEvent(MotionEvent event) {
235 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
236 forceTimeout();
237 return true;
238 }
239 return false;
240 }
241 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800242 mDialog.setTitle("Volume control"); // No need to localize
243 mDialog.setContentView(mView);
244 mDialog.setOnDismissListener(new OnDismissListener() {
245 public void onDismiss(DialogInterface dialog) {
246 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800247 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800248 }
249 });
250 // Change some window properties
251 Window window = mDialog.getWindow();
252 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700253 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800254 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700255 // Offset from the top
256 lp.y = mContext.getResources().getDimensionPixelOffset(
257 com.android.internal.R.dimen.volume_panel_top);
258 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
259 lp.width = LayoutParams.WRAP_CONTENT;
260 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800261 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700262 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
263 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
266 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800267
Amith Yamasani71def772011-10-12 12:25:24 -0700268 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400269 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700270 // If we don't want to show multiple volumes, hide the settings button and divider
271 if (!mShowCombinedVolumes) {
272 mMoreButton.setVisibility(View.GONE);
273 mDivider.setVisibility(View.GONE);
274 } else {
275 mMoreButton.setOnClickListener(this);
276 }
277
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800278 listenToRingerMode();
279 }
280
281 private void listenToRingerMode() {
282 final IntentFilter filter = new IntentFilter();
283 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
284 mContext.registerReceiver(new BroadcastReceiver() {
285
286 public void onReceive(Context context, Intent intent) {
287 final String action = intent.getAction();
288
289 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
290 removeMessages(MSG_RINGER_MODE_CHANGED);
291 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
292 }
293 }
294 }, filter);
295 }
296
297 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400298 if (streamType == STREAM_MASTER) {
Mike Lockwoodce952c82011-11-14 10:47:42 -0800299 return mAudioService.isMasterMute();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400300 } else {
301 return mAudioService.isStreamMute(streamType);
302 }
303 }
304
305 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400306 if (streamType == STREAM_MASTER) {
Mike Lockwood47676902011-11-08 10:31:21 -0800307 return mAudioService.getMasterMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400308 } else {
309 return mAudioService.getStreamMaxVolume(streamType);
310 }
311 }
312
313 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400314 if (streamType == STREAM_MASTER) {
Mike Lockwood47676902011-11-08 10:31:21 -0800315 return mAudioService.getMasterVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400316 } else {
317 return mAudioService.getStreamVolume(streamType);
318 }
319 }
320
321 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400322 if (streamType == STREAM_MASTER) {
Mike Lockwood47676902011-11-08 10:31:21 -0800323 mAudioService.setMasterVolume(index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400324 } else {
325 mAudioService.setStreamVolume(streamType, index, flags);
326 }
327 }
328
329 private int getLastAudibleStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400330 if (streamType == STREAM_MASTER) {
Mike Lockwoodce952c82011-11-14 10:47:42 -0800331 return mAudioService.getLastAudibleMasterVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400332 } else {
333 return mAudioService.getLastAudibleStreamVolume(streamType);
334 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800335 }
336
337 private void createSliders() {
338 LayoutInflater inflater = (LayoutInflater) mContext
339 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700340 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700341 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700342 for (int i = 0; i < STREAMS.length; i++) {
343 StreamResources streamRes = STREAMS[i];
344 int streamType = streamRes.streamType;
345 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
346 streamRes = StreamResources.RingerStream;
347 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800348 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700349 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800350 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
351 sc.group.setTag(sc);
352 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800353 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700354 sc.icon.setContentDescription(res.getString(streamRes.descRes));
355 sc.iconRes = streamRes.iconRes;
356 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800357 sc.icon.setImageResource(sc.iconRes);
358 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700359 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
360 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400361 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800362 sc.seekbarView.setOnSeekBarChangeListener(this);
363 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700364 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800365 }
366 }
367
368 private void reorderSliders(int activeStreamType) {
369 mSliderGroup.removeAllViews();
370
371 StreamControl active = mStreamControls.get(activeStreamType);
372 if (active == null) {
373 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
374 mActiveStreamType = -1;
375 } else {
376 mSliderGroup.addView(active.group);
377 mActiveStreamType = activeStreamType;
378 active.group.setVisibility(View.VISIBLE);
379 updateSlider(active);
380 }
381
Amith Yamasani42722bf2011-07-22 10:34:27 -0700382 addOtherVolumes();
383 }
384
385 private void addOtherVolumes() {
386 if (!mShowCombinedVolumes) return;
387
Amith Yamasani71def772011-10-12 12:25:24 -0700388 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800389 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700390 final int streamType = STREAMS[i].streamType;
391 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800392 continue;
393 }
394 StreamControl sc = mStreamControls.get(streamType);
395 mSliderGroup.addView(sc.group);
396 updateSlider(sc);
397 }
398 }
399
400 /** Update the mute and progress state of a slider */
401 private void updateSlider(StreamControl sc) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400402 sc.seekbarView.setProgress(getLastAudibleStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800403 final boolean muted = isMuted(sc.streamType);
404 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Amith Yamasanic696a532011-10-28 17:02:37 -0700405 if (sc.streamType == AudioManager.STREAM_RING && muted
406 && mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
407 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
408 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800409 }
410
411 private boolean isExpanded() {
412 return mMoreButton.getVisibility() != View.VISIBLE;
413 }
414
415 private void expand() {
416 final int count = mSliderGroup.getChildCount();
417 for (int i = 0; i < count; i++) {
418 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
419 }
420 mMoreButton.setVisibility(View.INVISIBLE);
421 mDivider.setVisibility(View.INVISIBLE);
422 }
423
424 private void collapse() {
425 mMoreButton.setVisibility(View.VISIBLE);
426 mDivider.setVisibility(View.VISIBLE);
427 final int count = mSliderGroup.getChildCount();
428 for (int i = 1; i < count; i++) {
429 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
430 }
431 }
432
433 private void updateStates() {
434 final int count = mSliderGroup.getChildCount();
435 for (int i = 0; i < count; i++) {
436 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
437 updateSlider(sc);
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
441 public void postVolumeChanged(int streamType, int flags) {
442 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800443 if (mStreamControls == null) {
444 createSliders();
445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 removeMessages(MSG_FREE_RESOURCES);
447 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
448 }
449
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400450 public void postMasterVolumeChanged(int flags) {
451 postVolumeChanged(STREAM_MASTER, flags);
452 }
453
Mike Lockwoodce952c82011-11-14 10:47:42 -0800454 public void postMuteChanged(int streamType, int flags) {
455 if (hasMessages(MSG_VOLUME_CHANGED)) return;
456 if (mStreamControls == null) {
457 createSliders();
458 }
459 removeMessages(MSG_FREE_RESOURCES);
460 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
461 }
462
463 public void postMasterMuteChanged(int flags) {
464 postMuteChanged(STREAM_MASTER, flags);
465 }
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
468 * Override this if you have other work to do when the volume changes (for
469 * example, vibrating, playing a sound, etc.). Make sure to call through to
470 * the superclass implementation.
471 */
472 protected void onVolumeChanged(int streamType, int flags) {
473
474 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
475
476 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasani8df96092011-12-08 17:03:54 -0800477 if (mActiveStreamType != streamType) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800478 reorderSliders(streamType);
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 onShowVolumeChanged(streamType, flags);
481 }
482
Marco Nelissen69f593c2009-07-28 09:55:04 -0700483 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 removeMessages(MSG_PLAY_SOUND);
485 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
486 }
487
488 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
489 removeMessages(MSG_PLAY_SOUND);
490 removeMessages(MSG_VIBRATE);
491 onStopSounds();
492 }
493
494 removeMessages(MSG_FREE_RESOURCES);
495 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800496
497 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499
Mike Lockwoodce952c82011-11-14 10:47:42 -0800500 protected void onMuteChanged(int streamType, int flags) {
501
502 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
503
504 StreamControl sc = mStreamControls.get(streamType);
505 if (sc != null) {
506 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
507 }
508
509 onVolumeChanged(streamType, flags);
510 }
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 protected void onShowVolumeChanged(int streamType, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400513 int index = isMuted(streamType) ?
514 getLastAudibleStreamVolume(streamType)
515 : getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800516
Marco Nelissen69f593c2009-07-28 09:55:04 -0700517 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 if (LOGD) {
520 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
521 + ", flags: " + flags + "), index: " + index);
522 }
523
524 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800525
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400526 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
528 switch (streamType) {
529
530 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800531// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700532 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
533 mContext, RingtoneManager.TYPE_RINGTONE);
534 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700535 mRingIsSilent = true;
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 break;
538 }
539
540 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800541 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800542 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
543 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
544 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
545 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800546 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800548 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550 break;
551 }
552
553 case AudioManager.STREAM_VOICE_CALL: {
554 /*
555 * For in-call voice call volume, there is no inaudible volume.
556 * Rescale the UI control so the progress bar doesn't go all
557 * the way to zero and don't show the mute icon.
558 */
559 index++;
560 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 break;
562 }
563
564 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 break;
566 }
567
568 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700569 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
570 mContext, RingtoneManager.TYPE_NOTIFICATION);
571 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700572 mRingIsSilent = true;
573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 break;
575 }
576
577 case AudioManager.STREAM_BLUETOOTH_SCO: {
578 /*
579 * For in-call voice call volume, there is no inaudible volume.
580 * Rescale the UI control so the progress bar doesn't go all
581 * the way to zero and don't show the mute icon.
582 */
583 index++;
584 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 break;
586 }
587 }
588
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800589 StreamControl sc = mStreamControls.get(streamType);
590 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700591 if (sc.seekbarView.getMax() != max) {
592 sc.seekbarView.setMax(max);
593 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800594 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800597 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800598 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800599 mDialog.setContentView(mView);
600 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700601 if (mShowCombinedVolumes) {
602 collapse();
603 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800604 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 // Do a little vibrate if applicable (only when going into vibrate mode)
608 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
609 mAudioService.isStreamAffectedByRingerMode(streamType) &&
610 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
611 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
612 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615
616 protected void onPlaySound(int streamType, int flags) {
617
618 if (hasMessages(MSG_STOP_SOUNDS)) {
619 removeMessages(MSG_STOP_SOUNDS);
620 // Force stop right now
621 onStopSounds();
622 }
623
624 synchronized (this) {
625 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800626 if (toneGen != null) {
627 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
628 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
633 protected void onStopSounds() {
634
635 synchronized (this) {
636 int numStreamTypes = AudioSystem.getNumStreamTypes();
637 for (int i = numStreamTypes - 1; i >= 0; i--) {
638 ToneGenerator toneGen = mToneGenerators[i];
639 if (toneGen != null) {
640 toneGen.stopTone();
641 }
642 }
643 }
644 }
645
646 protected void onVibrate() {
647
648 // Make sure we ended up in vibrate ringer mode
649 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
650 return;
651 }
652
653 mVibrator.vibrate(VIBRATE_DURATION);
654 }
655
656 /**
657 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
658 */
659 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400660 if (streamType == STREAM_MASTER) return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 synchronized (this) {
662 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800663 try {
664 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
665 } catch (RuntimeException e) {
666 if (LOGD) {
667 Log.d(TAG, "ToneGenerator constructor failed with "
668 + "RuntimeException: " + e);
669 }
670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800672 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674 }
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
677 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800678 * Switch between icons because Bluetooth music is same as music volume, but with
679 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800681 private void setMusicIcon(int resId, int resMuteId) {
682 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
683 if (sc != null) {
684 sc.iconRes = resId;
685 sc.iconMuteRes = resMuteId;
686 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689
690 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 synchronized (this) {
692 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
693 if (mToneGenerators[i] != null) {
694 mToneGenerators[i].release();
695 }
696 mToneGenerators[i] = null;
697 }
698 }
699 }
700
701 @Override
702 public void handleMessage(Message msg) {
703 switch (msg.what) {
704
705 case MSG_VOLUME_CHANGED: {
706 onVolumeChanged(msg.arg1, msg.arg2);
707 break;
708 }
709
Mike Lockwoodce952c82011-11-14 10:47:42 -0800710 case MSG_MUTE_CHANGED: {
711 onMuteChanged(msg.arg1, msg.arg2);
712 break;
713 }
714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 case MSG_FREE_RESOURCES: {
716 onFreeResources();
717 break;
718 }
719
720 case MSG_STOP_SOUNDS: {
721 onStopSounds();
722 break;
723 }
724
725 case MSG_PLAY_SOUND: {
726 onPlaySound(msg.arg1, msg.arg2);
727 break;
728 }
729
730 case MSG_VIBRATE: {
731 onVibrate();
732 break;
733 }
734
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800735 case MSG_TIMEOUT: {
736 if (mDialog.isShowing()) {
737 mDialog.dismiss();
738 mActiveStreamType = -1;
739 }
740 break;
741 }
742 case MSG_RINGER_MODE_CHANGED: {
743 if (mDialog.isShowing()) {
744 updateStates();
745 }
746 break;
747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749 }
750
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800751 private void resetTimeout() {
752 removeMessages(MSG_TIMEOUT);
753 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
754 }
755
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700756 private void forceTimeout() {
757 removeMessages(MSG_TIMEOUT);
758 sendMessage(obtainMessage(MSG_TIMEOUT));
759 }
760
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800761 public void onProgressChanged(SeekBar seekBar, int progress,
762 boolean fromUser) {
763 final Object tag = seekBar.getTag();
764 if (fromUser && tag instanceof StreamControl) {
765 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400766 if (getStreamVolume(sc.streamType) != progress) {
767 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800768 }
769 }
770 resetTimeout();
771 }
772
773 public void onStartTrackingTouch(SeekBar seekBar) {
774 }
775
776 public void onStopTrackingTouch(SeekBar seekBar) {
777 }
778
779 public void onClick(View v) {
780 if (v == mMoreButton) {
781 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800782 }
783 resetTimeout();
784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785}