blob: 08ccd941fa07c4df56f79194dc90249723caeed0 [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;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080029import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.media.AudioManager;
31import android.media.AudioService;
32import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070033import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.media.ToneGenerator;
Marco Nelissen69f593c2009-07-28 09:55:04 -070035import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Handler;
37import android.os.Message;
38import android.os.Vibrator;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080039import android.telephony.TelephonyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.Log;
41import android.widget.ImageView;
42import android.widget.ProgressBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080043import android.widget.SeekBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.widget.TextView;
45import android.widget.Toast;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080046import android.widget.SeekBar.OnSeekBarChangeListener;
47
48import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50/**
51 * Handle the volume up and down keys.
52 *
53 * This code really should be moved elsewhere.
54 *
55 * @hide
56 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -080057public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058{
59 private static final String TAG = "VolumePanel";
Marco Nelissen69f593c2009-07-28 09:55:04 -070060 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 /**
63 * The delay before playing a sound. This small period exists so the user
64 * can press another key (non-volume keys, too) to have it NOT be audible.
65 * <p>
66 * PhoneWindow will implement this part.
67 */
68 public static final int PLAY_SOUND_DELAY = 300;
69
70 /**
71 * The delay before vibrating. This small period exists so if the user is
72 * moving to silent mode, it will not emit a short vibrate (it normally
73 * would since vibrate is between normal mode and silent mode using hardware
74 * keys).
75 */
76 public static final int VIBRATE_DELAY = 300;
77
78 private static final int VIBRATE_DURATION = 300;
79 private static final int BEEP_DURATION = 150;
80 private static final int MAX_VOLUME = 100;
81 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080082 private static final int TIMEOUT_DELAY = 3000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 private static final int MSG_VOLUME_CHANGED = 0;
85 private static final int MSG_FREE_RESOURCES = 1;
86 private static final int MSG_PLAY_SOUND = 2;
87 private static final int MSG_STOP_SOUNDS = 3;
88 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080089 private static final int MSG_TIMEOUT = 5;
90 private static final int MSG_RINGER_MODE_CHANGED = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 protected Context mContext;
93 private AudioManager mAudioManager;
94 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -070095 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -070096 private boolean mShowCombinedVolumes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Amith Yamasani2bbdd772011-02-02 18:54:13 -080098 /** Dialog containing all the sliders */
99 private final Dialog mDialog;
100 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800102
103 /** Contains the sliders and their touchable icons */
104 private final ViewGroup mSliderGroup;
105 /** The button that expands the dialog to show all sliders */
106 private final View mMoreButton;
107 /** Dummy divider icon that needs to vanish with the more button */
108 private final View mDivider;
109
110 /** Currently active stream that shows up at the top of the list of sliders */
111 private int mActiveStreamType = -1;
112 /** All the slider controls mapped by stream type */
113 private HashMap<Integer,StreamControl> mStreamControls;
114
115 // List of stream types and their order
Amith Yamasani42722bf2011-07-22 10:34:27 -0700116 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800117 private static final int [] STREAM_TYPES = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800118 AudioManager.STREAM_BLUETOOTH_SCO,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800119 AudioManager.STREAM_RING,
120 AudioManager.STREAM_VOICE_CALL,
121 AudioManager.STREAM_MUSIC,
122 AudioManager.STREAM_NOTIFICATION
123 };
124
Amith Yamasani42722bf2011-07-22 10:34:27 -0700125 private static final int [] CONTENT_DESCRIPTIONS = {
126 R.string.volume_icon_description_bluetooth,
127 R.string.volume_icon_description_ringer,
128 R.string.volume_icon_description_incall,
129 R.string.volume_icon_description_media,
130 R.string.volume_icon_description_notification
131 };
132
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800133 // These icons need to correspond to the ones above.
134 private static final int [] STREAM_ICONS_NORMAL = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800135 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700136 R.drawable.ic_audio_ring_notif,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800137 R.drawable.ic_audio_phone,
138 R.drawable.ic_audio_vol,
139 R.drawable.ic_audio_notification,
140 };
141
142 // These icons need to correspond to the ones above.
143 private static final int [] STREAM_ICONS_MUTED = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800144 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700145 R.drawable.ic_audio_ring_notif_mute,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800146 R.drawable.ic_audio_phone,
147 R.drawable.ic_audio_vol_mute,
148 R.drawable.ic_audio_notification_mute,
149 };
150
151 /** Object that contains data for each slider */
152 private class StreamControl {
153 int streamType;
154 ViewGroup group;
155 ImageView icon;
156 SeekBar seekbarView;
157 int iconRes;
158 int iconMuteRes;
159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 // Synchronize when accessing this
162 private ToneGenerator mToneGenerators[];
163 private Vibrator mVibrator;
164
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800165 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 mContext = context;
167 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
168 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170 LayoutInflater inflater = (LayoutInflater) context
171 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800172 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
173 mView.setOnTouchListener(new View.OnTouchListener() {
174 public boolean onTouch(View v, MotionEvent event) {
175 resetTimeout();
176 return true;
177 }
178 });
179 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
180 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800181 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
182
183 mDialog = new Dialog(context, R.style.Theme_Panel_Volume);
184 mDialog.setTitle("Volume control"); // No need to localize
185 mDialog.setContentView(mView);
186 mDialog.setOnDismissListener(new OnDismissListener() {
187 public void onDismiss(DialogInterface dialog) {
188 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800189 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800190 }
191 });
192 // Change some window properties
193 Window window = mDialog.getWindow();
194 window.setGravity(Gravity.TOP);
195 WindowManager.LayoutParams lp = window.getAttributes();
196 lp.token = null;
197 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
198 window.setAttributes(lp);
199 window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
202 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800203
Amith Yamasani42722bf2011-07-22 10:34:27 -0700204 mShowCombinedVolumes = !context.getResources().getBoolean(R.bool.config_voice_capable);
205 // If we don't want to show multiple volumes, hide the settings button and divider
206 if (!mShowCombinedVolumes) {
207 mMoreButton.setVisibility(View.GONE);
208 mDivider.setVisibility(View.GONE);
209 } else {
210 mMoreButton.setOnClickListener(this);
211 }
212
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800213 listenToRingerMode();
214 }
215
216 private void listenToRingerMode() {
217 final IntentFilter filter = new IntentFilter();
218 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
219 mContext.registerReceiver(new BroadcastReceiver() {
220
221 public void onReceive(Context context, Intent intent) {
222 final String action = intent.getAction();
223
224 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
225 removeMessages(MSG_RINGER_MODE_CHANGED);
226 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
227 }
228 }
229 }, filter);
230 }
231
232 private boolean isMuted(int streamType) {
233 return mAudioManager.isStreamMute(streamType);
234 }
235
236 private void createSliders() {
237 LayoutInflater inflater = (LayoutInflater) mContext
238 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
239 mStreamControls = new HashMap<Integer,StreamControl>(STREAM_TYPES.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700240 Resources res = mContext.getResources();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800241 for (int i = 0; i < STREAM_TYPES.length; i++) {
242 StreamControl sc = new StreamControl();
243 sc.streamType = STREAM_TYPES[i];
244 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
245 sc.group.setTag(sc);
246 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
247 sc.icon.setOnClickListener(this);
248 sc.icon.setTag(sc);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700249 sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i]));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800250 sc.iconRes = STREAM_ICONS_NORMAL[i];
251 sc.iconMuteRes = STREAM_ICONS_MUTED[i];
252 sc.icon.setImageResource(sc.iconRes);
253 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
254 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(STREAM_TYPES[i]));
255 sc.seekbarView.setOnSeekBarChangeListener(this);
256 sc.seekbarView.setTag(sc);
257 mStreamControls.put(STREAM_TYPES[i], sc);
258 }
259 }
260
261 private void reorderSliders(int activeStreamType) {
262 mSliderGroup.removeAllViews();
263
264 StreamControl active = mStreamControls.get(activeStreamType);
265 if (active == null) {
266 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
267 mActiveStreamType = -1;
268 } else {
269 mSliderGroup.addView(active.group);
270 mActiveStreamType = activeStreamType;
271 active.group.setVisibility(View.VISIBLE);
272 updateSlider(active);
273 }
274
Amith Yamasani42722bf2011-07-22 10:34:27 -0700275 addOtherVolumes();
276 }
277
278 private void addOtherVolumes() {
279 if (!mShowCombinedVolumes) return;
280
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800281 for (int i = 0; i < STREAM_TYPES.length; i++) {
282 // Skip the phone specific ones and the active one
283 final int streamType = STREAM_TYPES[i];
284 if (streamType == AudioManager.STREAM_RING
285 || streamType == AudioManager.STREAM_VOICE_CALL
Eric Laurente2ad9012011-02-17 17:31:51 -0800286 || streamType == AudioManager.STREAM_BLUETOOTH_SCO
Amith Yamasani42722bf2011-07-22 10:34:27 -0700287 || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800288 continue;
289 }
290 StreamControl sc = mStreamControls.get(streamType);
291 mSliderGroup.addView(sc.group);
292 updateSlider(sc);
293 }
294 }
295
296 /** Update the mute and progress state of a slider */
297 private void updateSlider(StreamControl sc) {
298 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
299 final boolean muted = isMuted(sc.streamType);
300 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
301 sc.seekbarView.setEnabled(!muted);
302 }
303
304 private boolean isExpanded() {
305 return mMoreButton.getVisibility() != View.VISIBLE;
306 }
307
308 private void expand() {
309 final int count = mSliderGroup.getChildCount();
310 for (int i = 0; i < count; i++) {
311 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
312 }
313 mMoreButton.setVisibility(View.INVISIBLE);
314 mDivider.setVisibility(View.INVISIBLE);
315 }
316
317 private void collapse() {
318 mMoreButton.setVisibility(View.VISIBLE);
319 mDivider.setVisibility(View.VISIBLE);
320 final int count = mSliderGroup.getChildCount();
321 for (int i = 1; i < count; i++) {
322 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
323 }
324 }
325
326 private void updateStates() {
327 final int count = mSliderGroup.getChildCount();
328 for (int i = 0; i < count; i++) {
329 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
330 updateSlider(sc);
331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
333
334 public void postVolumeChanged(int streamType, int flags) {
335 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800336 if (mStreamControls == null) {
337 createSliders();
338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 removeMessages(MSG_FREE_RESOURCES);
340 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
341 }
342
343 /**
344 * Override this if you have other work to do when the volume changes (for
345 * example, vibrating, playing a sound, etc.). Make sure to call through to
346 * the superclass implementation.
347 */
348 protected void onVolumeChanged(int streamType, int flags) {
349
350 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
351
352 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800353 if (mActiveStreamType == -1) {
354 reorderSliders(streamType);
355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 onShowVolumeChanged(streamType, flags);
357 }
358
Marco Nelissen69f593c2009-07-28 09:55:04 -0700359 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 removeMessages(MSG_PLAY_SOUND);
361 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
362 }
363
364 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
365 removeMessages(MSG_PLAY_SOUND);
366 removeMessages(MSG_VIBRATE);
367 onStopSounds();
368 }
369
370 removeMessages(MSG_FREE_RESOURCES);
371 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800372
373 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375
376 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800377 int index = mAudioService.isStreamMute(streamType) ?
378 mAudioService.getLastAudibleStreamVolume(streamType)
379 : mAudioService.getStreamVolume(streamType);
380
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800381// int message = UNKNOWN_VOLUME_TEXT;
382// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700383 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384
385 if (LOGD) {
386 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
387 + ", flags: " + flags + "), index: " + index);
388 }
389
390 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 int max = mAudioService.getStreamMaxVolume(streamType);
393
394 switch (streamType) {
395
396 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800397// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700398 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
399 mContext, RingtoneManager.TYPE_RINGTONE);
400 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700401 mRingIsSilent = true;
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 break;
404 }
405
406 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800407 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800408 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
409 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
410 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
411 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800412 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800414 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 break;
417 }
418
419 case AudioManager.STREAM_VOICE_CALL: {
420 /*
421 * For in-call voice call volume, there is no inaudible volume.
422 * Rescale the UI control so the progress bar doesn't go all
423 * the way to zero and don't show the mute icon.
424 */
425 index++;
426 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 break;
428 }
429
430 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 break;
432 }
433
434 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700435 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
436 mContext, RingtoneManager.TYPE_NOTIFICATION);
437 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700438 mRingIsSilent = true;
439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 break;
441 }
442
443 case AudioManager.STREAM_BLUETOOTH_SCO: {
444 /*
445 * For in-call voice call volume, there is no inaudible volume.
446 * Rescale the UI control so the progress bar doesn't go all
447 * the way to zero and don't show the mute icon.
448 */
449 index++;
450 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 break;
452 }
453 }
454
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800455 StreamControl sc = mStreamControls.get(streamType);
456 if (sc != null) {
457 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800460 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800461 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800462 mDialog.setContentView(mView);
463 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700464 if (mShowCombinedVolumes) {
465 collapse();
466 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800467 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 // Do a little vibrate if applicable (only when going into vibrate mode)
471 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
472 mAudioService.isStreamAffectedByRingerMode(streamType) &&
473 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
474 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
475 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478
479 protected void onPlaySound(int streamType, int flags) {
480
481 if (hasMessages(MSG_STOP_SOUNDS)) {
482 removeMessages(MSG_STOP_SOUNDS);
483 // Force stop right now
484 onStopSounds();
485 }
486
487 synchronized (this) {
488 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800489 if (toneGen != null) {
490 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
491 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
496 protected void onStopSounds() {
497
498 synchronized (this) {
499 int numStreamTypes = AudioSystem.getNumStreamTypes();
500 for (int i = numStreamTypes - 1; i >= 0; i--) {
501 ToneGenerator toneGen = mToneGenerators[i];
502 if (toneGen != null) {
503 toneGen.stopTone();
504 }
505 }
506 }
507 }
508
509 protected void onVibrate() {
510
511 // Make sure we ended up in vibrate ringer mode
512 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
513 return;
514 }
515
516 mVibrator.vibrate(VIBRATE_DURATION);
517 }
518
519 /**
520 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
521 */
522 private ToneGenerator getOrCreateToneGenerator(int streamType) {
523 synchronized (this) {
524 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800525 try {
526 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
527 } catch (RuntimeException e) {
528 if (LOGD) {
529 Log.d(TAG, "ToneGenerator constructor failed with "
530 + "RuntimeException: " + e);
531 }
532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800534 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 }
537
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800538// /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800539// * Makes the ringer icon visible with an icon that is chosen
540// * based on the current ringer mode.
541// */
542// private void setRingerIcon() {
543// mSmallStreamIcon.setVisibility(View.GONE);
544// mLargeStreamIcon.setVisibility(View.VISIBLE);
545//
546// int ringerMode = mAudioService.getRingerMode();
547// int icon;
548//
549// if (LOGD) Log.d(TAG, "setRingerIcon(), ringerMode: " + ringerMode);
550//
551// if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
552// icon = com.android.internal.R.drawable.ic_volume_off;
553// } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
554// icon = com.android.internal.R.drawable.ic_vibrate;
555// } else {
556// icon = com.android.internal.R.drawable.ic_volume;
557// }
558// mLargeStreamIcon.setImageResource(icon);
559// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
561 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800562 * Switch between icons because Bluetooth music is same as music volume, but with
563 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800565 private void setMusicIcon(int resId, int resMuteId) {
566 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
567 if (sc != null) {
568 sc.iconRes = resId;
569 sc.iconMuteRes = resMuteId;
570 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
574 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 synchronized (this) {
576 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
577 if (mToneGenerators[i] != null) {
578 mToneGenerators[i].release();
579 }
580 mToneGenerators[i] = null;
581 }
582 }
583 }
584
585 @Override
586 public void handleMessage(Message msg) {
587 switch (msg.what) {
588
589 case MSG_VOLUME_CHANGED: {
590 onVolumeChanged(msg.arg1, msg.arg2);
591 break;
592 }
593
594 case MSG_FREE_RESOURCES: {
595 onFreeResources();
596 break;
597 }
598
599 case MSG_STOP_SOUNDS: {
600 onStopSounds();
601 break;
602 }
603
604 case MSG_PLAY_SOUND: {
605 onPlaySound(msg.arg1, msg.arg2);
606 break;
607 }
608
609 case MSG_VIBRATE: {
610 onVibrate();
611 break;
612 }
613
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800614 case MSG_TIMEOUT: {
615 if (mDialog.isShowing()) {
616 mDialog.dismiss();
617 mActiveStreamType = -1;
618 }
619 break;
620 }
621 case MSG_RINGER_MODE_CHANGED: {
622 if (mDialog.isShowing()) {
623 updateStates();
624 }
625 break;
626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628 }
629
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800630 private void resetTimeout() {
631 removeMessages(MSG_TIMEOUT);
632 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
633 }
634
635 public void onProgressChanged(SeekBar seekBar, int progress,
636 boolean fromUser) {
637 final Object tag = seekBar.getTag();
638 if (fromUser && tag instanceof StreamControl) {
639 StreamControl sc = (StreamControl) tag;
640 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
641 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
642 }
643 }
644 resetTimeout();
645 }
646
647 public void onStartTrackingTouch(SeekBar seekBar) {
648 }
649
650 public void onStopTrackingTouch(SeekBar seekBar) {
651 }
652
653 public void onClick(View v) {
654 if (v == mMoreButton) {
655 expand();
656 } else if (v.getTag() instanceof StreamControl) {
657 StreamControl sc = (StreamControl) v.getTag();
658 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
659 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
660 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700661 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800662 }
663 resetTimeout();
664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665}