blob: fb87e23a4d9e08d5372c2217c6a82011df562def [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;
Amith Yamasani9b8e8482011-08-10 15:55:36 -070038import android.provider.Settings;
39import android.provider.Settings.System;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.Log;
41import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080042import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080043import android.widget.SeekBar.OnSeekBarChangeListener;
44
45import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47/**
48 * Handle the volume up and down keys.
49 *
50 * This code really should be moved elsewhere.
51 *
Dianne Hackborne8ecde12011-08-03 18:55:19 -070052 * Seriously, it really really should be moved elsewhere. This is used by
53 * android.media.AudioService, which actually runs in the system process, to
54 * show the volume dialog when the user changes the volume. What a mess.
55 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * @hide
57 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080058public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059{
60 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070061 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 /**
64 * The delay before playing a sound. This small period exists so the user
65 * can press another key (non-volume keys, too) to have it NOT be audible.
66 * <p>
67 * PhoneWindow will implement this part.
68 */
69 public static final int PLAY_SOUND_DELAY = 300;
70
71 /**
72 * The delay before vibrating. This small period exists so if the user is
73 * moving to silent mode, it will not emit a short vibrate (it normally
74 * would since vibrate is between normal mode and silent mode using hardware
75 * keys).
76 */
77 public static final int VIBRATE_DELAY = 300;
78
79 private static final int VIBRATE_DURATION = 300;
80 private static final int BEEP_DURATION = 150;
81 private static final int MAX_VOLUME = 100;
82 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080083 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 private static final int MSG_VOLUME_CHANGED = 0;
86 private static final int MSG_FREE_RESOURCES = 1;
87 private static final int MSG_PLAY_SOUND = 2;
88 private static final int MSG_STOP_SOUNDS = 3;
89 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080090 private static final int MSG_TIMEOUT = 5;
91 private static final int MSG_RINGER_MODE_CHANGED = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 protected Context mContext;
94 private AudioManager mAudioManager;
95 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -070096 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -070097 private boolean mShowCombinedVolumes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Amith Yamasani2bbdd772011-02-02 18:54:13 -080099 /** Dialog containing all the sliders */
100 private final Dialog mDialog;
101 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800103
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700104 /** The visible portion of the volume overlay */
105 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800106 /** Contains the sliders and their touchable icons */
107 private final ViewGroup mSliderGroup;
108 /** The button that expands the dialog to show all sliders */
109 private final View mMoreButton;
110 /** Dummy divider icon that needs to vanish with the more button */
111 private final View mDivider;
112
113 /** Currently active stream that shows up at the top of the list of sliders */
114 private int mActiveStreamType = -1;
115 /** All the slider controls mapped by stream type */
116 private HashMap<Integer,StreamControl> mStreamControls;
117
118 // List of stream types and their order
Amith Yamasani42722bf2011-07-22 10:34:27 -0700119 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800120 private static final int [] STREAM_TYPES = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800121 AudioManager.STREAM_BLUETOOTH_SCO,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800122 AudioManager.STREAM_RING,
123 AudioManager.STREAM_VOICE_CALL,
124 AudioManager.STREAM_MUSIC,
125 AudioManager.STREAM_NOTIFICATION
126 };
127
Amith Yamasani42722bf2011-07-22 10:34:27 -0700128 private static final int [] CONTENT_DESCRIPTIONS = {
129 R.string.volume_icon_description_bluetooth,
130 R.string.volume_icon_description_ringer,
131 R.string.volume_icon_description_incall,
132 R.string.volume_icon_description_media,
133 R.string.volume_icon_description_notification
134 };
135
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800136 // These icons need to correspond to the ones above.
137 private static final int [] STREAM_ICONS_NORMAL = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800138 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700139 R.drawable.ic_audio_ring_notif,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800140 R.drawable.ic_audio_phone,
141 R.drawable.ic_audio_vol,
142 R.drawable.ic_audio_notification,
143 };
144
145 // These icons need to correspond to the ones above.
146 private static final int [] STREAM_ICONS_MUTED = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800147 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700148 R.drawable.ic_audio_ring_notif_mute,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800149 R.drawable.ic_audio_phone,
150 R.drawable.ic_audio_vol_mute,
151 R.drawable.ic_audio_notification_mute,
152 };
153
154 /** Object that contains data for each slider */
155 private class StreamControl {
156 int streamType;
157 ViewGroup group;
158 ImageView icon;
159 SeekBar seekbarView;
160 int iconRes;
161 int iconMuteRes;
162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 // Synchronize when accessing this
165 private ToneGenerator mToneGenerators[];
166 private Vibrator mVibrator;
167
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800168 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 mContext = context;
170 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
171 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
173 LayoutInflater inflater = (LayoutInflater) context
174 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800175 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
176 mView.setOnTouchListener(new View.OnTouchListener() {
177 public boolean onTouch(View v, MotionEvent event) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700178 // Dismiss the dialog if the user touches outside the visible area. This is not
179 // handled by the usual dialog dismissing code because there is a region above
180 // the panel (marginTop) that is still within the dialog.
181 if (event.getAction() == MotionEvent.ACTION_DOWN) {
182 int x = (int) event.getX();
183 int y = (int) event.getY();
184 if (x < mPanel.getLeft() || x > mPanel.getRight() || y < mPanel.getTop()
185 || y > mPanel.getBottom()) {
186 forceTimeout();
187 return true;
188 }
189 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800190 resetTimeout();
191 return true;
192 }
193 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700194 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800195 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
196 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800197 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
198
199 mDialog = new Dialog(context, R.style.Theme_Panel_Volume);
200 mDialog.setTitle("Volume control"); // No need to localize
201 mDialog.setContentView(mView);
202 mDialog.setOnDismissListener(new OnDismissListener() {
203 public void onDismiss(DialogInterface dialog) {
204 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800205 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800206 }
207 });
208 // Change some window properties
209 Window window = mDialog.getWindow();
210 window.setGravity(Gravity.TOP);
211 WindowManager.LayoutParams lp = window.getAttributes();
212 lp.token = null;
Dianne Hackborne8ecde12011-08-03 18:55:19 -0700213 lp.type = WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800214 window.setAttributes(lp);
215 window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
218 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800219
Amith Yamasani42722bf2011-07-22 10:34:27 -0700220 mShowCombinedVolumes = !context.getResources().getBoolean(R.bool.config_voice_capable);
221 // If we don't want to show multiple volumes, hide the settings button and divider
222 if (!mShowCombinedVolumes) {
223 mMoreButton.setVisibility(View.GONE);
224 mDivider.setVisibility(View.GONE);
225 } else {
226 mMoreButton.setOnClickListener(this);
227 }
228
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800229 listenToRingerMode();
230 }
231
232 private void listenToRingerMode() {
233 final IntentFilter filter = new IntentFilter();
234 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
235 mContext.registerReceiver(new BroadcastReceiver() {
236
237 public void onReceive(Context context, Intent intent) {
238 final String action = intent.getAction();
239
240 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
241 removeMessages(MSG_RINGER_MODE_CHANGED);
242 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
243 }
244 }
245 }, filter);
246 }
247
248 private boolean isMuted(int streamType) {
249 return mAudioManager.isStreamMute(streamType);
250 }
251
252 private void createSliders() {
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700253 final int silentableStreams = System.getInt(mContext.getContentResolver(),
254 System.MODE_RINGER_STREAMS_AFFECTED,
255 ((1 << AudioSystem.STREAM_NOTIFICATION) | (1 << AudioSystem.STREAM_RING)));
256
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800257 LayoutInflater inflater = (LayoutInflater) mContext
258 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
259 mStreamControls = new HashMap<Integer,StreamControl>(STREAM_TYPES.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700260 Resources res = mContext.getResources();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800261 for (int i = 0; i < STREAM_TYPES.length; i++) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700262 final int streamType = STREAM_TYPES[i];
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800263 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700264 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800265 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
266 sc.group.setTag(sc);
267 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700268 if ((silentableStreams & (1 << sc.streamType)) != 0) {
269 sc.icon.setOnClickListener(this);
270 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800271 sc.icon.setTag(sc);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700272 sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i]));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800273 sc.iconRes = STREAM_ICONS_NORMAL[i];
274 sc.iconMuteRes = STREAM_ICONS_MUTED[i];
275 sc.icon.setImageResource(sc.iconRes);
276 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700277 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
278 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
279 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800280 sc.seekbarView.setOnSeekBarChangeListener(this);
281 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700282 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800283 }
284 }
285
286 private void reorderSliders(int activeStreamType) {
287 mSliderGroup.removeAllViews();
288
289 StreamControl active = mStreamControls.get(activeStreamType);
290 if (active == null) {
291 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
292 mActiveStreamType = -1;
293 } else {
294 mSliderGroup.addView(active.group);
295 mActiveStreamType = activeStreamType;
296 active.group.setVisibility(View.VISIBLE);
297 updateSlider(active);
298 }
299
Amith Yamasani42722bf2011-07-22 10:34:27 -0700300 addOtherVolumes();
301 }
302
303 private void addOtherVolumes() {
304 if (!mShowCombinedVolumes) return;
305
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800306 for (int i = 0; i < STREAM_TYPES.length; i++) {
307 // Skip the phone specific ones and the active one
308 final int streamType = STREAM_TYPES[i];
309 if (streamType == AudioManager.STREAM_RING
310 || streamType == AudioManager.STREAM_VOICE_CALL
Eric Laurente2ad9012011-02-17 17:31:51 -0800311 || streamType == AudioManager.STREAM_BLUETOOTH_SCO
Amith Yamasani42722bf2011-07-22 10:34:27 -0700312 || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800313 continue;
314 }
315 StreamControl sc = mStreamControls.get(streamType);
316 mSliderGroup.addView(sc.group);
317 updateSlider(sc);
318 }
319 }
320
321 /** Update the mute and progress state of a slider */
322 private void updateSlider(StreamControl sc) {
323 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
324 final boolean muted = isMuted(sc.streamType);
325 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
326 sc.seekbarView.setEnabled(!muted);
327 }
328
329 private boolean isExpanded() {
330 return mMoreButton.getVisibility() != View.VISIBLE;
331 }
332
333 private void expand() {
334 final int count = mSliderGroup.getChildCount();
335 for (int i = 0; i < count; i++) {
336 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
337 }
338 mMoreButton.setVisibility(View.INVISIBLE);
339 mDivider.setVisibility(View.INVISIBLE);
340 }
341
342 private void collapse() {
343 mMoreButton.setVisibility(View.VISIBLE);
344 mDivider.setVisibility(View.VISIBLE);
345 final int count = mSliderGroup.getChildCount();
346 for (int i = 1; i < count; i++) {
347 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
348 }
349 }
350
351 private void updateStates() {
352 final int count = mSliderGroup.getChildCount();
353 for (int i = 0; i < count; i++) {
354 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
355 updateSlider(sc);
356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 public void postVolumeChanged(int streamType, int flags) {
360 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800361 if (mStreamControls == null) {
362 createSliders();
363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 removeMessages(MSG_FREE_RESOURCES);
365 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
366 }
367
368 /**
369 * Override this if you have other work to do when the volume changes (for
370 * example, vibrating, playing a sound, etc.). Make sure to call through to
371 * the superclass implementation.
372 */
373 protected void onVolumeChanged(int streamType, int flags) {
374
375 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
376
377 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800378 if (mActiveStreamType == -1) {
379 reorderSliders(streamType);
380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 onShowVolumeChanged(streamType, flags);
382 }
383
Marco Nelissen69f593c2009-07-28 09:55:04 -0700384 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 removeMessages(MSG_PLAY_SOUND);
386 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
387 }
388
389 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
390 removeMessages(MSG_PLAY_SOUND);
391 removeMessages(MSG_VIBRATE);
392 onStopSounds();
393 }
394
395 removeMessages(MSG_FREE_RESOURCES);
396 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800397
398 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400
401 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800402 int index = mAudioService.isStreamMute(streamType) ?
403 mAudioService.getLastAudibleStreamVolume(streamType)
404 : mAudioService.getStreamVolume(streamType);
405
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800406// int message = UNKNOWN_VOLUME_TEXT;
407// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700408 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409
410 if (LOGD) {
411 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
412 + ", flags: " + flags + "), index: " + index);
413 }
414
415 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 int max = mAudioService.getStreamMaxVolume(streamType);
418
419 switch (streamType) {
420
421 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800422// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700423 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
424 mContext, RingtoneManager.TYPE_RINGTONE);
425 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700426 mRingIsSilent = true;
427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 break;
429 }
430
431 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800432 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800433 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
434 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
435 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
436 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800437 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800439 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441 break;
442 }
443
444 case AudioManager.STREAM_VOICE_CALL: {
445 /*
446 * For in-call voice call volume, there is no inaudible volume.
447 * Rescale the UI control so the progress bar doesn't go all
448 * the way to zero and don't show the mute icon.
449 */
450 index++;
451 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 break;
453 }
454
455 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 break;
457 }
458
459 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700460 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
461 mContext, RingtoneManager.TYPE_NOTIFICATION);
462 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700463 mRingIsSilent = true;
464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 break;
466 }
467
468 case AudioManager.STREAM_BLUETOOTH_SCO: {
469 /*
470 * For in-call voice call volume, there is no inaudible volume.
471 * Rescale the UI control so the progress bar doesn't go all
472 * the way to zero and don't show the mute icon.
473 */
474 index++;
475 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 break;
477 }
478 }
479
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800480 StreamControl sc = mStreamControls.get(streamType);
481 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700482 if (sc.seekbarView.getMax() != max) {
483 sc.seekbarView.setMax(max);
484 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800485 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800488 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800489 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800490 mDialog.setContentView(mView);
491 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700492 if (mShowCombinedVolumes) {
493 collapse();
494 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800495 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 // Do a little vibrate if applicable (only when going into vibrate mode)
499 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
500 mAudioService.isStreamAffectedByRingerMode(streamType) &&
501 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
502 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
503 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506
507 protected void onPlaySound(int streamType, int flags) {
508
509 if (hasMessages(MSG_STOP_SOUNDS)) {
510 removeMessages(MSG_STOP_SOUNDS);
511 // Force stop right now
512 onStopSounds();
513 }
514
515 synchronized (this) {
516 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800517 if (toneGen != null) {
518 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
519 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523
524 protected void onStopSounds() {
525
526 synchronized (this) {
527 int numStreamTypes = AudioSystem.getNumStreamTypes();
528 for (int i = numStreamTypes - 1; i >= 0; i--) {
529 ToneGenerator toneGen = mToneGenerators[i];
530 if (toneGen != null) {
531 toneGen.stopTone();
532 }
533 }
534 }
535 }
536
537 protected void onVibrate() {
538
539 // Make sure we ended up in vibrate ringer mode
540 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
541 return;
542 }
543
544 mVibrator.vibrate(VIBRATE_DURATION);
545 }
546
547 /**
548 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
549 */
550 private ToneGenerator getOrCreateToneGenerator(int streamType) {
551 synchronized (this) {
552 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800553 try {
554 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
555 } catch (RuntimeException e) {
556 if (LOGD) {
557 Log.d(TAG, "ToneGenerator constructor failed with "
558 + "RuntimeException: " + e);
559 }
560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800562 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 }
565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566
567 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800568 * Switch between icons because Bluetooth music is same as music volume, but with
569 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800571 private void setMusicIcon(int resId, int resMuteId) {
572 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
573 if (sc != null) {
574 sc.iconRes = resId;
575 sc.iconMuteRes = resMuteId;
576 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
580 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 synchronized (this) {
582 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
583 if (mToneGenerators[i] != null) {
584 mToneGenerators[i].release();
585 }
586 mToneGenerators[i] = null;
587 }
588 }
589 }
590
591 @Override
592 public void handleMessage(Message msg) {
593 switch (msg.what) {
594
595 case MSG_VOLUME_CHANGED: {
596 onVolumeChanged(msg.arg1, msg.arg2);
597 break;
598 }
599
600 case MSG_FREE_RESOURCES: {
601 onFreeResources();
602 break;
603 }
604
605 case MSG_STOP_SOUNDS: {
606 onStopSounds();
607 break;
608 }
609
610 case MSG_PLAY_SOUND: {
611 onPlaySound(msg.arg1, msg.arg2);
612 break;
613 }
614
615 case MSG_VIBRATE: {
616 onVibrate();
617 break;
618 }
619
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800620 case MSG_TIMEOUT: {
621 if (mDialog.isShowing()) {
622 mDialog.dismiss();
623 mActiveStreamType = -1;
624 }
625 break;
626 }
627 case MSG_RINGER_MODE_CHANGED: {
628 if (mDialog.isShowing()) {
629 updateStates();
630 }
631 break;
632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634 }
635
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800636 private void resetTimeout() {
637 removeMessages(MSG_TIMEOUT);
638 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
639 }
640
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700641 private void forceTimeout() {
642 removeMessages(MSG_TIMEOUT);
643 sendMessage(obtainMessage(MSG_TIMEOUT));
644 }
645
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800646 public void onProgressChanged(SeekBar seekBar, int progress,
647 boolean fromUser) {
648 final Object tag = seekBar.getTag();
649 if (fromUser && tag instanceof StreamControl) {
650 StreamControl sc = (StreamControl) tag;
651 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
652 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
653 }
654 }
655 resetTimeout();
656 }
657
658 public void onStartTrackingTouch(SeekBar seekBar) {
659 }
660
661 public void onStopTrackingTouch(SeekBar seekBar) {
662 }
663
664 public void onClick(View v) {
665 if (v == mMoreButton) {
666 expand();
667 } else if (v.getTag() instanceof StreamControl) {
668 StreamControl sc = (StreamControl) v.getTag();
669 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
670 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
671 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700672 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800673 }
674 resetTimeout();
675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676}