blob: 662137abf22d4653e6abfbecb894627d30fe2bea [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;
39import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080040import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080041import android.widget.SeekBar.OnSeekBarChangeListener;
42
43import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * Handle the volume up and down keys.
47 *
48 * This code really should be moved elsewhere.
49 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070050 * Seriously, it really really should be moved elsewhere. This is used by
51 * android.media.AudioService, which actually runs in the system process, to
52 * show the volume dialog when the user changes the volume. What a mess.
53 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * @hide
55 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080056public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057{
58 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070059 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 /**
62 * The delay before playing a sound. This small period exists so the user
63 * can press another key (non-volume keys, too) to have it NOT be audible.
64 * <p>
65 * PhoneWindow will implement this part.
66 */
67 public static final int PLAY_SOUND_DELAY = 300;
68
69 /**
70 * The delay before vibrating. This small period exists so if the user is
71 * moving to silent mode, it will not emit a short vibrate (it normally
72 * would since vibrate is between normal mode and silent mode using hardware
73 * keys).
74 */
75 public static final int VIBRATE_DELAY = 300;
76
77 private static final int VIBRATE_DURATION = 300;
78 private static final int BEEP_DURATION = 150;
79 private static final int MAX_VOLUME = 100;
80 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080081 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int MSG_VOLUME_CHANGED = 0;
84 private static final int MSG_FREE_RESOURCES = 1;
85 private static final int MSG_PLAY_SOUND = 2;
86 private static final int MSG_STOP_SOUNDS = 3;
87 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080088 private static final int MSG_TIMEOUT = 5;
89 private static final int MSG_RINGER_MODE_CHANGED = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 protected Context mContext;
92 private AudioManager mAudioManager;
93 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -070094 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -070095 private boolean mShowCombinedVolumes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Amith Yamasani2bbdd772011-02-02 18:54:13 -080097 /** Dialog containing all the sliders */
98 private final Dialog mDialog;
99 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800101
102 /** Contains the sliders and their touchable icons */
103 private final ViewGroup mSliderGroup;
104 /** The button that expands the dialog to show all sliders */
105 private final View mMoreButton;
106 /** Dummy divider icon that needs to vanish with the more button */
107 private final View mDivider;
108
109 /** Currently active stream that shows up at the top of the list of sliders */
110 private int mActiveStreamType = -1;
111 /** All the slider controls mapped by stream type */
112 private HashMap<Integer,StreamControl> mStreamControls;
113
114 // List of stream types and their order
Amith Yamasani42722bf2011-07-22 10:34:27 -0700115 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800116 private static final int [] STREAM_TYPES = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800117 AudioManager.STREAM_BLUETOOTH_SCO,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800118 AudioManager.STREAM_RING,
119 AudioManager.STREAM_VOICE_CALL,
120 AudioManager.STREAM_MUSIC,
121 AudioManager.STREAM_NOTIFICATION
122 };
123
Amith Yamasani42722bf2011-07-22 10:34:27 -0700124 private static final int [] CONTENT_DESCRIPTIONS = {
125 R.string.volume_icon_description_bluetooth,
126 R.string.volume_icon_description_ringer,
127 R.string.volume_icon_description_incall,
128 R.string.volume_icon_description_media,
129 R.string.volume_icon_description_notification
130 };
131
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800132 // These icons need to correspond to the ones above.
133 private static final int [] STREAM_ICONS_NORMAL = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800134 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700135 R.drawable.ic_audio_ring_notif,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800136 R.drawable.ic_audio_phone,
137 R.drawable.ic_audio_vol,
138 R.drawable.ic_audio_notification,
139 };
140
141 // These icons need to correspond to the ones above.
142 private static final int [] STREAM_ICONS_MUTED = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800143 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700144 R.drawable.ic_audio_ring_notif_mute,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800145 R.drawable.ic_audio_phone,
146 R.drawable.ic_audio_vol_mute,
147 R.drawable.ic_audio_notification_mute,
148 };
149
150 /** Object that contains data for each slider */
151 private class StreamControl {
152 int streamType;
153 ViewGroup group;
154 ImageView icon;
155 SeekBar seekbarView;
156 int iconRes;
157 int iconMuteRes;
158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
160 // Synchronize when accessing this
161 private ToneGenerator mToneGenerators[];
162 private Vibrator mVibrator;
163
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800164 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 mContext = context;
166 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
167 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 LayoutInflater inflater = (LayoutInflater) context
170 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800171 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
172 mView.setOnTouchListener(new View.OnTouchListener() {
173 public boolean onTouch(View v, MotionEvent event) {
174 resetTimeout();
175 return true;
176 }
177 });
178 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
179 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800180 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
181
182 mDialog = new Dialog(context, R.style.Theme_Panel_Volume);
183 mDialog.setTitle("Volume control"); // No need to localize
184 mDialog.setContentView(mView);
185 mDialog.setOnDismissListener(new OnDismissListener() {
186 public void onDismiss(DialogInterface dialog) {
187 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800188 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800189 }
190 });
191 // Change some window properties
192 Window window = mDialog.getWindow();
193 window.setGravity(Gravity.TOP);
194 WindowManager.LayoutParams lp = window.getAttributes();
195 lp.token = null;
Dianne Hackborne8ecde12011-08-03 18:55:19 -0700196 lp.type = WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800197 window.setAttributes(lp);
198 window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
201 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800202
Amith Yamasani42722bf2011-07-22 10:34:27 -0700203 mShowCombinedVolumes = !context.getResources().getBoolean(R.bool.config_voice_capable);
204 // If we don't want to show multiple volumes, hide the settings button and divider
205 if (!mShowCombinedVolumes) {
206 mMoreButton.setVisibility(View.GONE);
207 mDivider.setVisibility(View.GONE);
208 } else {
209 mMoreButton.setOnClickListener(this);
210 }
211
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800212 listenToRingerMode();
213 }
214
215 private void listenToRingerMode() {
216 final IntentFilter filter = new IntentFilter();
217 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
218 mContext.registerReceiver(new BroadcastReceiver() {
219
220 public void onReceive(Context context, Intent intent) {
221 final String action = intent.getAction();
222
223 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
224 removeMessages(MSG_RINGER_MODE_CHANGED);
225 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
226 }
227 }
228 }, filter);
229 }
230
231 private boolean isMuted(int streamType) {
232 return mAudioManager.isStreamMute(streamType);
233 }
234
235 private void createSliders() {
236 LayoutInflater inflater = (LayoutInflater) mContext
237 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
238 mStreamControls = new HashMap<Integer,StreamControl>(STREAM_TYPES.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700239 Resources res = mContext.getResources();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800240 for (int i = 0; i < STREAM_TYPES.length; i++) {
241 StreamControl sc = new StreamControl();
242 sc.streamType = STREAM_TYPES[i];
243 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
244 sc.group.setTag(sc);
245 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
246 sc.icon.setOnClickListener(this);
247 sc.icon.setTag(sc);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700248 sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i]));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800249 sc.iconRes = STREAM_ICONS_NORMAL[i];
250 sc.iconMuteRes = STREAM_ICONS_MUTED[i];
251 sc.icon.setImageResource(sc.iconRes);
252 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
253 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(STREAM_TYPES[i]));
254 sc.seekbarView.setOnSeekBarChangeListener(this);
255 sc.seekbarView.setTag(sc);
256 mStreamControls.put(STREAM_TYPES[i], sc);
257 }
258 }
259
260 private void reorderSliders(int activeStreamType) {
261 mSliderGroup.removeAllViews();
262
263 StreamControl active = mStreamControls.get(activeStreamType);
264 if (active == null) {
265 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
266 mActiveStreamType = -1;
267 } else {
268 mSliderGroup.addView(active.group);
269 mActiveStreamType = activeStreamType;
270 active.group.setVisibility(View.VISIBLE);
271 updateSlider(active);
272 }
273
Amith Yamasani42722bf2011-07-22 10:34:27 -0700274 addOtherVolumes();
275 }
276
277 private void addOtherVolumes() {
278 if (!mShowCombinedVolumes) return;
279
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800280 for (int i = 0; i < STREAM_TYPES.length; i++) {
281 // Skip the phone specific ones and the active one
282 final int streamType = STREAM_TYPES[i];
283 if (streamType == AudioManager.STREAM_RING
284 || streamType == AudioManager.STREAM_VOICE_CALL
Eric Laurente2ad9012011-02-17 17:31:51 -0800285 || streamType == AudioManager.STREAM_BLUETOOTH_SCO
Amith Yamasani42722bf2011-07-22 10:34:27 -0700286 || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800287 continue;
288 }
289 StreamControl sc = mStreamControls.get(streamType);
290 mSliderGroup.addView(sc.group);
291 updateSlider(sc);
292 }
293 }
294
295 /** Update the mute and progress state of a slider */
296 private void updateSlider(StreamControl sc) {
297 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
298 final boolean muted = isMuted(sc.streamType);
299 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
300 sc.seekbarView.setEnabled(!muted);
301 }
302
303 private boolean isExpanded() {
304 return mMoreButton.getVisibility() != View.VISIBLE;
305 }
306
307 private void expand() {
308 final int count = mSliderGroup.getChildCount();
309 for (int i = 0; i < count; i++) {
310 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
311 }
312 mMoreButton.setVisibility(View.INVISIBLE);
313 mDivider.setVisibility(View.INVISIBLE);
314 }
315
316 private void collapse() {
317 mMoreButton.setVisibility(View.VISIBLE);
318 mDivider.setVisibility(View.VISIBLE);
319 final int count = mSliderGroup.getChildCount();
320 for (int i = 1; i < count; i++) {
321 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
322 }
323 }
324
325 private void updateStates() {
326 final int count = mSliderGroup.getChildCount();
327 for (int i = 0; i < count; i++) {
328 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
329 updateSlider(sc);
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332
333 public void postVolumeChanged(int streamType, int flags) {
334 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800335 if (mStreamControls == null) {
336 createSliders();
337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 removeMessages(MSG_FREE_RESOURCES);
339 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
340 }
341
342 /**
343 * Override this if you have other work to do when the volume changes (for
344 * example, vibrating, playing a sound, etc.). Make sure to call through to
345 * the superclass implementation.
346 */
347 protected void onVolumeChanged(int streamType, int flags) {
348
349 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
350
351 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800352 if (mActiveStreamType == -1) {
353 reorderSliders(streamType);
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 onShowVolumeChanged(streamType, flags);
356 }
357
Marco Nelissen69f593c2009-07-28 09:55:04 -0700358 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 removeMessages(MSG_PLAY_SOUND);
360 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
361 }
362
363 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
364 removeMessages(MSG_PLAY_SOUND);
365 removeMessages(MSG_VIBRATE);
366 onStopSounds();
367 }
368
369 removeMessages(MSG_FREE_RESOURCES);
370 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800371
372 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374
375 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800376 int index = mAudioService.isStreamMute(streamType) ?
377 mAudioService.getLastAudibleStreamVolume(streamType)
378 : mAudioService.getStreamVolume(streamType);
379
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800380// int message = UNKNOWN_VOLUME_TEXT;
381// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700382 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383
384 if (LOGD) {
385 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
386 + ", flags: " + flags + "), index: " + index);
387 }
388
389 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 int max = mAudioService.getStreamMaxVolume(streamType);
392
393 switch (streamType) {
394
395 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800396// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700397 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
398 mContext, RingtoneManager.TYPE_RINGTONE);
399 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700400 mRingIsSilent = true;
401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 break;
403 }
404
405 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800406 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800407 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
408 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
409 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
410 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800411 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800413 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 break;
416 }
417
418 case AudioManager.STREAM_VOICE_CALL: {
419 /*
420 * For in-call voice call volume, there is no inaudible volume.
421 * Rescale the UI control so the progress bar doesn't go all
422 * the way to zero and don't show the mute icon.
423 */
424 index++;
425 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 break;
427 }
428
429 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 break;
431 }
432
433 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700434 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
435 mContext, RingtoneManager.TYPE_NOTIFICATION);
436 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700437 mRingIsSilent = true;
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 break;
440 }
441
442 case AudioManager.STREAM_BLUETOOTH_SCO: {
443 /*
444 * For in-call voice call volume, there is no inaudible volume.
445 * Rescale the UI control so the progress bar doesn't go all
446 * the way to zero and don't show the mute icon.
447 */
448 index++;
449 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 break;
451 }
452 }
453
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800454 StreamControl sc = mStreamControls.get(streamType);
455 if (sc != null) {
456 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800459 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800460 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800461 mDialog.setContentView(mView);
462 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700463 if (mShowCombinedVolumes) {
464 collapse();
465 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800466 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 // Do a little vibrate if applicable (only when going into vibrate mode)
470 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
471 mAudioService.isStreamAffectedByRingerMode(streamType) &&
472 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
473 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
474 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477
478 protected void onPlaySound(int streamType, int flags) {
479
480 if (hasMessages(MSG_STOP_SOUNDS)) {
481 removeMessages(MSG_STOP_SOUNDS);
482 // Force stop right now
483 onStopSounds();
484 }
485
486 synchronized (this) {
487 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800488 if (toneGen != null) {
489 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
490 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
495 protected void onStopSounds() {
496
497 synchronized (this) {
498 int numStreamTypes = AudioSystem.getNumStreamTypes();
499 for (int i = numStreamTypes - 1; i >= 0; i--) {
500 ToneGenerator toneGen = mToneGenerators[i];
501 if (toneGen != null) {
502 toneGen.stopTone();
503 }
504 }
505 }
506 }
507
508 protected void onVibrate() {
509
510 // Make sure we ended up in vibrate ringer mode
511 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
512 return;
513 }
514
515 mVibrator.vibrate(VIBRATE_DURATION);
516 }
517
518 /**
519 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
520 */
521 private ToneGenerator getOrCreateToneGenerator(int streamType) {
522 synchronized (this) {
523 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800524 try {
525 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
526 } catch (RuntimeException e) {
527 if (LOGD) {
528 Log.d(TAG, "ToneGenerator constructor failed with "
529 + "RuntimeException: " + e);
530 }
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800533 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535 }
536
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800537// /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800538// * Makes the ringer icon visible with an icon that is chosen
539// * based on the current ringer mode.
540// */
541// private void setRingerIcon() {
542// mSmallStreamIcon.setVisibility(View.GONE);
543// mLargeStreamIcon.setVisibility(View.VISIBLE);
544//
545// int ringerMode = mAudioService.getRingerMode();
546// int icon;
547//
548// if (LOGD) Log.d(TAG, "setRingerIcon(), ringerMode: " + ringerMode);
549//
550// if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
551// icon = com.android.internal.R.drawable.ic_volume_off;
552// } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
553// icon = com.android.internal.R.drawable.ic_vibrate;
554// } else {
555// icon = com.android.internal.R.drawable.ic_volume;
556// }
557// mLargeStreamIcon.setImageResource(icon);
558// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800561 * Switch between icons because Bluetooth music is same as music volume, but with
562 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800564 private void setMusicIcon(int resId, int resMuteId) {
565 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
566 if (sc != null) {
567 sc.iconRes = resId;
568 sc.iconMuteRes = resMuteId;
569 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 synchronized (this) {
575 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
576 if (mToneGenerators[i] != null) {
577 mToneGenerators[i].release();
578 }
579 mToneGenerators[i] = null;
580 }
581 }
582 }
583
584 @Override
585 public void handleMessage(Message msg) {
586 switch (msg.what) {
587
588 case MSG_VOLUME_CHANGED: {
589 onVolumeChanged(msg.arg1, msg.arg2);
590 break;
591 }
592
593 case MSG_FREE_RESOURCES: {
594 onFreeResources();
595 break;
596 }
597
598 case MSG_STOP_SOUNDS: {
599 onStopSounds();
600 break;
601 }
602
603 case MSG_PLAY_SOUND: {
604 onPlaySound(msg.arg1, msg.arg2);
605 break;
606 }
607
608 case MSG_VIBRATE: {
609 onVibrate();
610 break;
611 }
612
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800613 case MSG_TIMEOUT: {
614 if (mDialog.isShowing()) {
615 mDialog.dismiss();
616 mActiveStreamType = -1;
617 }
618 break;
619 }
620 case MSG_RINGER_MODE_CHANGED: {
621 if (mDialog.isShowing()) {
622 updateStates();
623 }
624 break;
625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627 }
628
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800629 private void resetTimeout() {
630 removeMessages(MSG_TIMEOUT);
631 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
632 }
633
634 public void onProgressChanged(SeekBar seekBar, int progress,
635 boolean fromUser) {
636 final Object tag = seekBar.getTag();
637 if (fromUser && tag instanceof StreamControl) {
638 StreamControl sc = (StreamControl) tag;
639 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
640 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
641 }
642 }
643 resetTimeout();
644 }
645
646 public void onStartTrackingTouch(SeekBar seekBar) {
647 }
648
649 public void onStopTrackingTouch(SeekBar seekBar) {
650 }
651
652 public void onClick(View v) {
653 if (v == mMoreButton) {
654 expand();
655 } else if (v.getTag() instanceof StreamControl) {
656 StreamControl sc = (StreamControl) v.getTag();
657 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
658 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
659 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700660 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800661 }
662 resetTimeout();
663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664}