blob: e1aa9a49183936237f090890f0d86b72a6c4bf7c [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++) {
262 StreamControl sc = new StreamControl();
263 sc.streamType = STREAM_TYPES[i];
264 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
265 sc.group.setTag(sc);
266 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700267 if ((silentableStreams & (1 << sc.streamType)) != 0) {
268 sc.icon.setOnClickListener(this);
269 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800270 sc.icon.setTag(sc);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700271 sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i]));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800272 sc.iconRes = STREAM_ICONS_NORMAL[i];
273 sc.iconMuteRes = STREAM_ICONS_MUTED[i];
274 sc.icon.setImageResource(sc.iconRes);
275 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
276 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(STREAM_TYPES[i]));
277 sc.seekbarView.setOnSeekBarChangeListener(this);
278 sc.seekbarView.setTag(sc);
279 mStreamControls.put(STREAM_TYPES[i], sc);
280 }
281 }
282
283 private void reorderSliders(int activeStreamType) {
284 mSliderGroup.removeAllViews();
285
286 StreamControl active = mStreamControls.get(activeStreamType);
287 if (active == null) {
288 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
289 mActiveStreamType = -1;
290 } else {
291 mSliderGroup.addView(active.group);
292 mActiveStreamType = activeStreamType;
293 active.group.setVisibility(View.VISIBLE);
294 updateSlider(active);
295 }
296
Amith Yamasani42722bf2011-07-22 10:34:27 -0700297 addOtherVolumes();
298 }
299
300 private void addOtherVolumes() {
301 if (!mShowCombinedVolumes) return;
302
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800303 for (int i = 0; i < STREAM_TYPES.length; i++) {
304 // Skip the phone specific ones and the active one
305 final int streamType = STREAM_TYPES[i];
306 if (streamType == AudioManager.STREAM_RING
307 || streamType == AudioManager.STREAM_VOICE_CALL
Eric Laurente2ad9012011-02-17 17:31:51 -0800308 || streamType == AudioManager.STREAM_BLUETOOTH_SCO
Amith Yamasani42722bf2011-07-22 10:34:27 -0700309 || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800310 continue;
311 }
312 StreamControl sc = mStreamControls.get(streamType);
313 mSliderGroup.addView(sc.group);
314 updateSlider(sc);
315 }
316 }
317
318 /** Update the mute and progress state of a slider */
319 private void updateSlider(StreamControl sc) {
320 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
321 final boolean muted = isMuted(sc.streamType);
322 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
323 sc.seekbarView.setEnabled(!muted);
324 }
325
326 private boolean isExpanded() {
327 return mMoreButton.getVisibility() != View.VISIBLE;
328 }
329
330 private void expand() {
331 final int count = mSliderGroup.getChildCount();
332 for (int i = 0; i < count; i++) {
333 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
334 }
335 mMoreButton.setVisibility(View.INVISIBLE);
336 mDivider.setVisibility(View.INVISIBLE);
337 }
338
339 private void collapse() {
340 mMoreButton.setVisibility(View.VISIBLE);
341 mDivider.setVisibility(View.VISIBLE);
342 final int count = mSliderGroup.getChildCount();
343 for (int i = 1; i < count; i++) {
344 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
345 }
346 }
347
348 private void updateStates() {
349 final int count = mSliderGroup.getChildCount();
350 for (int i = 0; i < count; i++) {
351 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
352 updateSlider(sc);
353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
355
356 public void postVolumeChanged(int streamType, int flags) {
357 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800358 if (mStreamControls == null) {
359 createSliders();
360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 removeMessages(MSG_FREE_RESOURCES);
362 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
363 }
364
365 /**
366 * Override this if you have other work to do when the volume changes (for
367 * example, vibrating, playing a sound, etc.). Make sure to call through to
368 * the superclass implementation.
369 */
370 protected void onVolumeChanged(int streamType, int flags) {
371
372 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
373
374 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800375 if (mActiveStreamType == -1) {
376 reorderSliders(streamType);
377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 onShowVolumeChanged(streamType, flags);
379 }
380
Marco Nelissen69f593c2009-07-28 09:55:04 -0700381 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 removeMessages(MSG_PLAY_SOUND);
383 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
384 }
385
386 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
387 removeMessages(MSG_PLAY_SOUND);
388 removeMessages(MSG_VIBRATE);
389 onStopSounds();
390 }
391
392 removeMessages(MSG_FREE_RESOURCES);
393 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800394
395 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397
398 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800399 int index = mAudioService.isStreamMute(streamType) ?
400 mAudioService.getLastAudibleStreamVolume(streamType)
401 : mAudioService.getStreamVolume(streamType);
402
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800403// int message = UNKNOWN_VOLUME_TEXT;
404// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700405 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
407 if (LOGD) {
408 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
409 + ", flags: " + flags + "), index: " + index);
410 }
411
412 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 int max = mAudioService.getStreamMaxVolume(streamType);
415
416 switch (streamType) {
417
418 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800419// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700420 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
421 mContext, RingtoneManager.TYPE_RINGTONE);
422 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700423 mRingIsSilent = true;
424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 break;
426 }
427
428 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800429 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800430 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
431 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
432 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
433 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800434 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800436 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
438 break;
439 }
440
441 case AudioManager.STREAM_VOICE_CALL: {
442 /*
443 * For in-call voice call volume, there is no inaudible volume.
444 * Rescale the UI control so the progress bar doesn't go all
445 * the way to zero and don't show the mute icon.
446 */
447 index++;
448 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 break;
450 }
451
452 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 break;
454 }
455
456 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700457 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
458 mContext, RingtoneManager.TYPE_NOTIFICATION);
459 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700460 mRingIsSilent = true;
461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 break;
463 }
464
465 case AudioManager.STREAM_BLUETOOTH_SCO: {
466 /*
467 * For in-call voice call volume, there is no inaudible volume.
468 * Rescale the UI control so the progress bar doesn't go all
469 * the way to zero and don't show the mute icon.
470 */
471 index++;
472 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 break;
474 }
475 }
476
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800477 StreamControl sc = mStreamControls.get(streamType);
478 if (sc != null) {
479 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800482 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800483 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800484 mDialog.setContentView(mView);
485 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700486 if (mShowCombinedVolumes) {
487 collapse();
488 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800489 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 // Do a little vibrate if applicable (only when going into vibrate mode)
493 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
494 mAudioService.isStreamAffectedByRingerMode(streamType) &&
495 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
496 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
497 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 protected void onPlaySound(int streamType, int flags) {
502
503 if (hasMessages(MSG_STOP_SOUNDS)) {
504 removeMessages(MSG_STOP_SOUNDS);
505 // Force stop right now
506 onStopSounds();
507 }
508
509 synchronized (this) {
510 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800511 if (toneGen != null) {
512 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
513 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517
518 protected void onStopSounds() {
519
520 synchronized (this) {
521 int numStreamTypes = AudioSystem.getNumStreamTypes();
522 for (int i = numStreamTypes - 1; i >= 0; i--) {
523 ToneGenerator toneGen = mToneGenerators[i];
524 if (toneGen != null) {
525 toneGen.stopTone();
526 }
527 }
528 }
529 }
530
531 protected void onVibrate() {
532
533 // Make sure we ended up in vibrate ringer mode
534 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
535 return;
536 }
537
538 mVibrator.vibrate(VIBRATE_DURATION);
539 }
540
541 /**
542 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
543 */
544 private ToneGenerator getOrCreateToneGenerator(int streamType) {
545 synchronized (this) {
546 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800547 try {
548 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
549 } catch (RuntimeException e) {
550 if (LOGD) {
551 Log.d(TAG, "ToneGenerator constructor failed with "
552 + "RuntimeException: " + e);
553 }
554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800556 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558 }
559
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800560// /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800561// * Makes the ringer icon visible with an icon that is chosen
562// * based on the current ringer mode.
563// */
564// private void setRingerIcon() {
565// mSmallStreamIcon.setVisibility(View.GONE);
566// mLargeStreamIcon.setVisibility(View.VISIBLE);
567//
568// int ringerMode = mAudioService.getRingerMode();
569// int icon;
570//
571// if (LOGD) Log.d(TAG, "setRingerIcon(), ringerMode: " + ringerMode);
572//
573// if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
574// icon = com.android.internal.R.drawable.ic_volume_off;
575// } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
576// icon = com.android.internal.R.drawable.ic_vibrate;
577// } else {
578// icon = com.android.internal.R.drawable.ic_volume;
579// }
580// mLargeStreamIcon.setImageResource(icon);
581// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
583 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800584 * Switch between icons because Bluetooth music is same as music volume, but with
585 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800587 private void setMusicIcon(int resId, int resMuteId) {
588 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
589 if (sc != null) {
590 sc.iconRes = resId;
591 sc.iconMuteRes = resMuteId;
592 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595
596 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 synchronized (this) {
598 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
599 if (mToneGenerators[i] != null) {
600 mToneGenerators[i].release();
601 }
602 mToneGenerators[i] = null;
603 }
604 }
605 }
606
607 @Override
608 public void handleMessage(Message msg) {
609 switch (msg.what) {
610
611 case MSG_VOLUME_CHANGED: {
612 onVolumeChanged(msg.arg1, msg.arg2);
613 break;
614 }
615
616 case MSG_FREE_RESOURCES: {
617 onFreeResources();
618 break;
619 }
620
621 case MSG_STOP_SOUNDS: {
622 onStopSounds();
623 break;
624 }
625
626 case MSG_PLAY_SOUND: {
627 onPlaySound(msg.arg1, msg.arg2);
628 break;
629 }
630
631 case MSG_VIBRATE: {
632 onVibrate();
633 break;
634 }
635
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800636 case MSG_TIMEOUT: {
637 if (mDialog.isShowing()) {
638 mDialog.dismiss();
639 mActiveStreamType = -1;
640 }
641 break;
642 }
643 case MSG_RINGER_MODE_CHANGED: {
644 if (mDialog.isShowing()) {
645 updateStates();
646 }
647 break;
648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 }
651
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800652 private void resetTimeout() {
653 removeMessages(MSG_TIMEOUT);
654 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
655 }
656
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700657 private void forceTimeout() {
658 removeMessages(MSG_TIMEOUT);
659 sendMessage(obtainMessage(MSG_TIMEOUT));
660 }
661
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800662 public void onProgressChanged(SeekBar seekBar, int progress,
663 boolean fromUser) {
664 final Object tag = seekBar.getTag();
665 if (fromUser && tag instanceof StreamControl) {
666 StreamControl sc = (StreamControl) tag;
667 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
668 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
669 }
670 }
671 resetTimeout();
672 }
673
674 public void onStartTrackingTouch(SeekBar seekBar) {
675 }
676
677 public void onStopTrackingTouch(SeekBar seekBar) {
678 }
679
680 public void onClick(View v) {
681 if (v == mMoreButton) {
682 expand();
683 } else if (v.getTag() instanceof StreamControl) {
684 StreamControl sc = (StreamControl) v.getTag();
685 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
686 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
687 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700688 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800689 }
690 resetTimeout();
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692}