blob: 83df8a5b3522f4e0c5409df537ad0b14f9d4933e [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 protected Context mContext;
96 private AudioManager mAudioManager;
97 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -070098 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -070099 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700100 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800102 /** Dialog containing all the sliders */
103 private final Dialog mDialog;
104 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800106
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700107 /** The visible portion of the volume overlay */
108 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800109 /** Contains the sliders and their touchable icons */
110 private final ViewGroup mSliderGroup;
111 /** The button that expands the dialog to show all sliders */
112 private final View mMoreButton;
113 /** Dummy divider icon that needs to vanish with the more button */
114 private final View mDivider;
115
116 /** Currently active stream that shows up at the top of the list of sliders */
117 private int mActiveStreamType = -1;
118 /** All the slider controls mapped by stream type */
119 private HashMap<Integer,StreamControl> mStreamControls;
120
Amith Yamasani71def772011-10-12 12:25:24 -0700121 private enum StreamResources {
122 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
123 R.string.volume_icon_description_bluetooth,
124 R.drawable.ic_audio_bt,
125 R.drawable.ic_audio_bt,
126 false),
127 RingerStream(AudioManager.STREAM_RING,
128 R.string.volume_icon_description_ringer,
129 R.drawable.ic_audio_ring_notif,
130 R.drawable.ic_audio_ring_notif_mute,
131 false),
132 VoiceStream(AudioManager.STREAM_VOICE_CALL,
133 R.string.volume_icon_description_incall,
134 R.drawable.ic_audio_phone,
135 R.drawable.ic_audio_phone,
136 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700137 AlarmStream(AudioManager.STREAM_ALARM,
138 R.string.volume_alarm,
139 R.drawable.ic_audio_alarm,
140 R.drawable.ic_audio_alarm_mute,
141 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700142 MediaStream(AudioManager.STREAM_MUSIC,
143 R.string.volume_icon_description_media,
144 R.drawable.ic_audio_vol,
145 R.drawable.ic_audio_vol_mute,
146 true),
147 NotificationStream(AudioManager.STREAM_NOTIFICATION,
148 R.string.volume_icon_description_notification,
149 R.drawable.ic_audio_notification,
150 R.drawable.ic_audio_notification_mute,
151 true);
152
153 int streamType;
154 int descRes;
155 int iconRes;
156 int iconMuteRes;
157 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
158 boolean show;
159
160 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
161 this.streamType = streamType;
162 this.descRes = descRes;
163 this.iconRes = iconRes;
164 this.iconMuteRes = iconMuteRes;
165 this.show = show;
166 }
167 };
168
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800169 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700170 private static final StreamResources[] STREAMS = {
171 StreamResources.BluetoothSCOStream,
172 StreamResources.RingerStream,
173 StreamResources.VoiceStream,
174 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700175 StreamResources.NotificationStream,
176 StreamResources.AlarmStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800177 };
178
179 /** Object that contains data for each slider */
180 private class StreamControl {
181 int streamType;
182 ViewGroup group;
183 ImageView icon;
184 SeekBar seekbarView;
185 int iconRes;
186 int iconMuteRes;
187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189 // Synchronize when accessing this
190 private ToneGenerator mToneGenerators[];
191 private Vibrator mVibrator;
192
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800193 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 mContext = context;
195 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
196 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 LayoutInflater inflater = (LayoutInflater) context
199 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800200 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
201 mView.setOnTouchListener(new View.OnTouchListener() {
202 public boolean onTouch(View v, MotionEvent event) {
203 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700204 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800205 }
206 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700207 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800208 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
209 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800210 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
211
Amith Yamasani284e6302011-09-16 18:24:47 -0700212 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
213 public boolean onTouchEvent(MotionEvent event) {
214 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
215 forceTimeout();
216 return true;
217 }
218 return false;
219 }
220 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800221 mDialog.setTitle("Volume control"); // No need to localize
222 mDialog.setContentView(mView);
223 mDialog.setOnDismissListener(new OnDismissListener() {
224 public void onDismiss(DialogInterface dialog) {
225 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800226 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800227 }
228 });
229 // Change some window properties
230 Window window = mDialog.getWindow();
231 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700232 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800233 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700234 // Offset from the top
235 lp.y = mContext.getResources().getDimensionPixelOffset(
236 com.android.internal.R.dimen.volume_panel_top);
237 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
238 lp.width = LayoutParams.WRAP_CONTENT;
239 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800240 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700241 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
242 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
245 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800246
Amith Yamasani71def772011-10-12 12:25:24 -0700247 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
248 mShowCombinedVolumes = !mVoiceCapable;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700249 // If we don't want to show multiple volumes, hide the settings button and divider
250 if (!mShowCombinedVolumes) {
251 mMoreButton.setVisibility(View.GONE);
252 mDivider.setVisibility(View.GONE);
253 } else {
254 mMoreButton.setOnClickListener(this);
255 }
256
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800257 listenToRingerMode();
258 }
259
260 private void listenToRingerMode() {
261 final IntentFilter filter = new IntentFilter();
262 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
263 mContext.registerReceiver(new BroadcastReceiver() {
264
265 public void onReceive(Context context, Intent intent) {
266 final String action = intent.getAction();
267
268 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
269 removeMessages(MSG_RINGER_MODE_CHANGED);
270 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
271 }
272 }
273 }, filter);
274 }
275
276 private boolean isMuted(int streamType) {
277 return mAudioManager.isStreamMute(streamType);
278 }
279
280 private void createSliders() {
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700281 final int silentableStreams = System.getInt(mContext.getContentResolver(),
282 System.MODE_RINGER_STREAMS_AFFECTED,
283 ((1 << AudioSystem.STREAM_NOTIFICATION) | (1 << AudioSystem.STREAM_RING)));
284
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800285 LayoutInflater inflater = (LayoutInflater) mContext
286 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700287 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700288 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700289 for (int i = 0; i < STREAMS.length; i++) {
290 StreamResources streamRes = STREAMS[i];
291 int streamType = streamRes.streamType;
292 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
293 streamRes = StreamResources.RingerStream;
294 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800295 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700296 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800297 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
298 sc.group.setTag(sc);
299 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700300 if ((silentableStreams & (1 << sc.streamType)) != 0) {
301 sc.icon.setOnClickListener(this);
302 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800303 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700304 sc.icon.setContentDescription(res.getString(streamRes.descRes));
305 sc.iconRes = streamRes.iconRes;
306 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800307 sc.icon.setImageResource(sc.iconRes);
308 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700309 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
310 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
311 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800312 sc.seekbarView.setOnSeekBarChangeListener(this);
313 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700314 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800315 }
316 }
317
318 private void reorderSliders(int activeStreamType) {
319 mSliderGroup.removeAllViews();
320
321 StreamControl active = mStreamControls.get(activeStreamType);
322 if (active == null) {
323 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
324 mActiveStreamType = -1;
325 } else {
326 mSliderGroup.addView(active.group);
327 mActiveStreamType = activeStreamType;
328 active.group.setVisibility(View.VISIBLE);
329 updateSlider(active);
330 }
331
Amith Yamasani42722bf2011-07-22 10:34:27 -0700332 addOtherVolumes();
333 }
334
335 private void addOtherVolumes() {
336 if (!mShowCombinedVolumes) return;
337
Amith Yamasani71def772011-10-12 12:25:24 -0700338 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800339 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700340 final int streamType = STREAMS[i].streamType;
341 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800342 continue;
343 }
344 StreamControl sc = mStreamControls.get(streamType);
345 mSliderGroup.addView(sc.group);
346 updateSlider(sc);
347 }
348 }
349
350 /** Update the mute and progress state of a slider */
351 private void updateSlider(StreamControl sc) {
352 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
353 final boolean muted = isMuted(sc.streamType);
354 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
355 sc.seekbarView.setEnabled(!muted);
356 }
357
358 private boolean isExpanded() {
359 return mMoreButton.getVisibility() != View.VISIBLE;
360 }
361
362 private void expand() {
363 final int count = mSliderGroup.getChildCount();
364 for (int i = 0; i < count; i++) {
365 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
366 }
367 mMoreButton.setVisibility(View.INVISIBLE);
368 mDivider.setVisibility(View.INVISIBLE);
369 }
370
371 private void collapse() {
372 mMoreButton.setVisibility(View.VISIBLE);
373 mDivider.setVisibility(View.VISIBLE);
374 final int count = mSliderGroup.getChildCount();
375 for (int i = 1; i < count; i++) {
376 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
377 }
378 }
379
380 private void updateStates() {
381 final int count = mSliderGroup.getChildCount();
382 for (int i = 0; i < count; i++) {
383 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
384 updateSlider(sc);
385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 }
387
388 public void postVolumeChanged(int streamType, int flags) {
389 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800390 if (mStreamControls == null) {
391 createSliders();
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 removeMessages(MSG_FREE_RESOURCES);
394 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
395 }
396
397 /**
398 * Override this if you have other work to do when the volume changes (for
399 * example, vibrating, playing a sound, etc.). Make sure to call through to
400 * the superclass implementation.
401 */
402 protected void onVolumeChanged(int streamType, int flags) {
403
404 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
405
406 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800407 if (mActiveStreamType == -1) {
408 reorderSliders(streamType);
409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 onShowVolumeChanged(streamType, flags);
411 }
412
Marco Nelissen69f593c2009-07-28 09:55:04 -0700413 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 removeMessages(MSG_PLAY_SOUND);
415 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
416 }
417
418 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
419 removeMessages(MSG_PLAY_SOUND);
420 removeMessages(MSG_VIBRATE);
421 onStopSounds();
422 }
423
424 removeMessages(MSG_FREE_RESOURCES);
425 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800426
427 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800431 int index = mAudioService.isStreamMute(streamType) ?
432 mAudioService.getLastAudibleStreamVolume(streamType)
433 : mAudioService.getStreamVolume(streamType);
434
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800435// int message = UNKNOWN_VOLUME_TEXT;
436// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700437 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 if (LOGD) {
440 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
441 + ", flags: " + flags + "), index: " + index);
442 }
443
444 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 int max = mAudioService.getStreamMaxVolume(streamType);
447
448 switch (streamType) {
449
450 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800451// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700452 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
453 mContext, RingtoneManager.TYPE_RINGTONE);
454 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700455 mRingIsSilent = true;
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 break;
458 }
459
460 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800461 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800462 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
463 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
464 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
465 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800466 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800468 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470 break;
471 }
472
473 case AudioManager.STREAM_VOICE_CALL: {
474 /*
475 * For in-call voice call volume, there is no inaudible volume.
476 * Rescale the UI control so the progress bar doesn't go all
477 * the way to zero and don't show the mute icon.
478 */
479 index++;
480 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 break;
482 }
483
484 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 break;
486 }
487
488 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700489 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
490 mContext, RingtoneManager.TYPE_NOTIFICATION);
491 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700492 mRingIsSilent = true;
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 break;
495 }
496
497 case AudioManager.STREAM_BLUETOOTH_SCO: {
498 /*
499 * For in-call voice call volume, there is no inaudible volume.
500 * Rescale the UI control so the progress bar doesn't go all
501 * the way to zero and don't show the mute icon.
502 */
503 index++;
504 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 break;
506 }
507 }
508
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800509 StreamControl sc = mStreamControls.get(streamType);
510 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700511 if (sc.seekbarView.getMax() != max) {
512 sc.seekbarView.setMax(max);
513 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800514 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800517 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800518 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800519 mDialog.setContentView(mView);
520 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700521 if (mShowCombinedVolumes) {
522 collapse();
523 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800524 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 // Do a little vibrate if applicable (only when going into vibrate mode)
528 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
529 mAudioService.isStreamAffectedByRingerMode(streamType) &&
530 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
531 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
532 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
536 protected void onPlaySound(int streamType, int flags) {
537
538 if (hasMessages(MSG_STOP_SOUNDS)) {
539 removeMessages(MSG_STOP_SOUNDS);
540 // Force stop right now
541 onStopSounds();
542 }
543
544 synchronized (this) {
545 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800546 if (toneGen != null) {
547 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
548 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
553 protected void onStopSounds() {
554
555 synchronized (this) {
556 int numStreamTypes = AudioSystem.getNumStreamTypes();
557 for (int i = numStreamTypes - 1; i >= 0; i--) {
558 ToneGenerator toneGen = mToneGenerators[i];
559 if (toneGen != null) {
560 toneGen.stopTone();
561 }
562 }
563 }
564 }
565
566 protected void onVibrate() {
567
568 // Make sure we ended up in vibrate ringer mode
569 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
570 return;
571 }
572
573 mVibrator.vibrate(VIBRATE_DURATION);
574 }
575
576 /**
577 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
578 */
579 private ToneGenerator getOrCreateToneGenerator(int streamType) {
580 synchronized (this) {
581 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800582 try {
583 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
584 } catch (RuntimeException e) {
585 if (LOGD) {
586 Log.d(TAG, "ToneGenerator constructor failed with "
587 + "RuntimeException: " + e);
588 }
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800591 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593 }
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
596 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800597 * Switch between icons because Bluetooth music is same as music volume, but with
598 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800600 private void setMusicIcon(int resId, int resMuteId) {
601 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
602 if (sc != null) {
603 sc.iconRes = resId;
604 sc.iconMuteRes = resMuteId;
605 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608
609 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 synchronized (this) {
611 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
612 if (mToneGenerators[i] != null) {
613 mToneGenerators[i].release();
614 }
615 mToneGenerators[i] = null;
616 }
617 }
618 }
619
620 @Override
621 public void handleMessage(Message msg) {
622 switch (msg.what) {
623
624 case MSG_VOLUME_CHANGED: {
625 onVolumeChanged(msg.arg1, msg.arg2);
626 break;
627 }
628
629 case MSG_FREE_RESOURCES: {
630 onFreeResources();
631 break;
632 }
633
634 case MSG_STOP_SOUNDS: {
635 onStopSounds();
636 break;
637 }
638
639 case MSG_PLAY_SOUND: {
640 onPlaySound(msg.arg1, msg.arg2);
641 break;
642 }
643
644 case MSG_VIBRATE: {
645 onVibrate();
646 break;
647 }
648
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800649 case MSG_TIMEOUT: {
650 if (mDialog.isShowing()) {
651 mDialog.dismiss();
652 mActiveStreamType = -1;
653 }
654 break;
655 }
656 case MSG_RINGER_MODE_CHANGED: {
657 if (mDialog.isShowing()) {
658 updateStates();
659 }
660 break;
661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663 }
664
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800665 private void resetTimeout() {
666 removeMessages(MSG_TIMEOUT);
667 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
668 }
669
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700670 private void forceTimeout() {
671 removeMessages(MSG_TIMEOUT);
672 sendMessage(obtainMessage(MSG_TIMEOUT));
673 }
674
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800675 public void onProgressChanged(SeekBar seekBar, int progress,
676 boolean fromUser) {
677 final Object tag = seekBar.getTag();
678 if (fromUser && tag instanceof StreamControl) {
679 StreamControl sc = (StreamControl) tag;
680 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
681 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
682 }
683 }
684 resetTimeout();
685 }
686
687 public void onStartTrackingTouch(SeekBar seekBar) {
688 }
689
690 public void onStopTrackingTouch(SeekBar seekBar) {
691 }
692
693 public void onClick(View v) {
694 if (v == mMoreButton) {
695 expand();
696 } else if (v.getTag() instanceof StreamControl) {
697 StreamControl sc = (StreamControl) v.getTag();
698 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
699 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
700 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700701 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800702 }
703 resetTimeout();
704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705}