blob: 122865e1e4cf9f401f062f5ba8ecc13323c41996 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800101 /** Dialog containing all the sliders */
102 private final Dialog mDialog;
103 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800105
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700106 /** The visible portion of the volume overlay */
107 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800108 /** Contains the sliders and their touchable icons */
109 private final ViewGroup mSliderGroup;
110 /** The button that expands the dialog to show all sliders */
111 private final View mMoreButton;
112 /** Dummy divider icon that needs to vanish with the more button */
113 private final View mDivider;
114
115 /** Currently active stream that shows up at the top of the list of sliders */
116 private int mActiveStreamType = -1;
117 /** All the slider controls mapped by stream type */
118 private HashMap<Integer,StreamControl> mStreamControls;
119
120 // List of stream types and their order
Amith Yamasani42722bf2011-07-22 10:34:27 -0700121 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800122 private static final int [] STREAM_TYPES = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800123 AudioManager.STREAM_BLUETOOTH_SCO,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800124 AudioManager.STREAM_RING,
125 AudioManager.STREAM_VOICE_CALL,
126 AudioManager.STREAM_MUSIC,
127 AudioManager.STREAM_NOTIFICATION
128 };
129
Amith Yamasani42722bf2011-07-22 10:34:27 -0700130 private static final int [] CONTENT_DESCRIPTIONS = {
131 R.string.volume_icon_description_bluetooth,
132 R.string.volume_icon_description_ringer,
133 R.string.volume_icon_description_incall,
134 R.string.volume_icon_description_media,
135 R.string.volume_icon_description_notification
136 };
137
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800138 // These icons need to correspond to the ones above.
139 private static final int [] STREAM_ICONS_NORMAL = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800140 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700141 R.drawable.ic_audio_ring_notif,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800142 R.drawable.ic_audio_phone,
143 R.drawable.ic_audio_vol,
144 R.drawable.ic_audio_notification,
145 };
146
147 // These icons need to correspond to the ones above.
148 private static final int [] STREAM_ICONS_MUTED = {
Eric Laurente2ad9012011-02-17 17:31:51 -0800149 R.drawable.ic_audio_bt,
Amith Yamasani42722bf2011-07-22 10:34:27 -0700150 R.drawable.ic_audio_ring_notif_mute,
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800151 R.drawable.ic_audio_phone,
152 R.drawable.ic_audio_vol_mute,
153 R.drawable.ic_audio_notification_mute,
154 };
155
156 /** Object that contains data for each slider */
157 private class StreamControl {
158 int streamType;
159 ViewGroup group;
160 ImageView icon;
161 SeekBar seekbarView;
162 int iconRes;
163 int iconMuteRes;
164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 // Synchronize when accessing this
167 private ToneGenerator mToneGenerators[];
168 private Vibrator mVibrator;
169
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800170 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 mContext = context;
172 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
173 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
175 LayoutInflater inflater = (LayoutInflater) context
176 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800177 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
178 mView.setOnTouchListener(new View.OnTouchListener() {
179 public boolean onTouch(View v, MotionEvent event) {
180 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700181 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800182 }
183 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700184 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800185 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
186 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800187 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
188
Amith Yamasani284e6302011-09-16 18:24:47 -0700189 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
190 public boolean onTouchEvent(MotionEvent event) {
191 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
192 forceTimeout();
193 return true;
194 }
195 return false;
196 }
197 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800198 mDialog.setTitle("Volume control"); // No need to localize
199 mDialog.setContentView(mView);
200 mDialog.setOnDismissListener(new OnDismissListener() {
201 public void onDismiss(DialogInterface dialog) {
202 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800203 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800204 }
205 });
206 // Change some window properties
207 Window window = mDialog.getWindow();
208 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700209 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800210 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700211 // Offset from the top
212 lp.y = mContext.getResources().getDimensionPixelOffset(
213 com.android.internal.R.dimen.volume_panel_top);
214 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
215 lp.width = LayoutParams.WRAP_CONTENT;
216 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800217 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700218 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
219 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
222 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800223
Amith Yamasani42722bf2011-07-22 10:34:27 -0700224 mShowCombinedVolumes = !context.getResources().getBoolean(R.bool.config_voice_capable);
225 // If we don't want to show multiple volumes, hide the settings button and divider
226 if (!mShowCombinedVolumes) {
227 mMoreButton.setVisibility(View.GONE);
228 mDivider.setVisibility(View.GONE);
229 } else {
230 mMoreButton.setOnClickListener(this);
231 }
232
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800233 listenToRingerMode();
234 }
235
236 private void listenToRingerMode() {
237 final IntentFilter filter = new IntentFilter();
238 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
239 mContext.registerReceiver(new BroadcastReceiver() {
240
241 public void onReceive(Context context, Intent intent) {
242 final String action = intent.getAction();
243
244 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
245 removeMessages(MSG_RINGER_MODE_CHANGED);
246 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
247 }
248 }
249 }, filter);
250 }
251
252 private boolean isMuted(int streamType) {
253 return mAudioManager.isStreamMute(streamType);
254 }
255
256 private void createSliders() {
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700257 final int silentableStreams = System.getInt(mContext.getContentResolver(),
258 System.MODE_RINGER_STREAMS_AFFECTED,
259 ((1 << AudioSystem.STREAM_NOTIFICATION) | (1 << AudioSystem.STREAM_RING)));
260
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800261 LayoutInflater inflater = (LayoutInflater) mContext
262 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
263 mStreamControls = new HashMap<Integer,StreamControl>(STREAM_TYPES.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700264 Resources res = mContext.getResources();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800265 for (int i = 0; i < STREAM_TYPES.length; i++) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700266 final int streamType = STREAM_TYPES[i];
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800267 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700268 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800269 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
270 sc.group.setTag(sc);
271 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani9b8e8482011-08-10 15:55:36 -0700272 if ((silentableStreams & (1 << sc.streamType)) != 0) {
273 sc.icon.setOnClickListener(this);
274 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800275 sc.icon.setTag(sc);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700276 sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i]));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800277 sc.iconRes = STREAM_ICONS_NORMAL[i];
278 sc.iconMuteRes = STREAM_ICONS_MUTED[i];
279 sc.icon.setImageResource(sc.iconRes);
280 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700281 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
282 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
283 sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800284 sc.seekbarView.setOnSeekBarChangeListener(this);
285 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700286 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800287 }
288 }
289
290 private void reorderSliders(int activeStreamType) {
291 mSliderGroup.removeAllViews();
292
293 StreamControl active = mStreamControls.get(activeStreamType);
294 if (active == null) {
295 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
296 mActiveStreamType = -1;
297 } else {
298 mSliderGroup.addView(active.group);
299 mActiveStreamType = activeStreamType;
300 active.group.setVisibility(View.VISIBLE);
301 updateSlider(active);
302 }
303
Amith Yamasani42722bf2011-07-22 10:34:27 -0700304 addOtherVolumes();
305 }
306
307 private void addOtherVolumes() {
308 if (!mShowCombinedVolumes) return;
309
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800310 for (int i = 0; i < STREAM_TYPES.length; i++) {
311 // Skip the phone specific ones and the active one
312 final int streamType = STREAM_TYPES[i];
313 if (streamType == AudioManager.STREAM_RING
314 || streamType == AudioManager.STREAM_VOICE_CALL
Eric Laurente2ad9012011-02-17 17:31:51 -0800315 || streamType == AudioManager.STREAM_BLUETOOTH_SCO
Amith Yamasani42722bf2011-07-22 10:34:27 -0700316 || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800317 continue;
318 }
319 StreamControl sc = mStreamControls.get(streamType);
320 mSliderGroup.addView(sc.group);
321 updateSlider(sc);
322 }
323 }
324
325 /** Update the mute and progress state of a slider */
326 private void updateSlider(StreamControl sc) {
327 sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
328 final boolean muted = isMuted(sc.streamType);
329 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
330 sc.seekbarView.setEnabled(!muted);
331 }
332
333 private boolean isExpanded() {
334 return mMoreButton.getVisibility() != View.VISIBLE;
335 }
336
337 private void expand() {
338 final int count = mSliderGroup.getChildCount();
339 for (int i = 0; i < count; i++) {
340 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
341 }
342 mMoreButton.setVisibility(View.INVISIBLE);
343 mDivider.setVisibility(View.INVISIBLE);
344 }
345
346 private void collapse() {
347 mMoreButton.setVisibility(View.VISIBLE);
348 mDivider.setVisibility(View.VISIBLE);
349 final int count = mSliderGroup.getChildCount();
350 for (int i = 1; i < count; i++) {
351 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
352 }
353 }
354
355 private void updateStates() {
356 final int count = mSliderGroup.getChildCount();
357 for (int i = 0; i < count; i++) {
358 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
359 updateSlider(sc);
360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362
363 public void postVolumeChanged(int streamType, int flags) {
364 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800365 if (mStreamControls == null) {
366 createSliders();
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 removeMessages(MSG_FREE_RESOURCES);
369 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
370 }
371
372 /**
373 * Override this if you have other work to do when the volume changes (for
374 * example, vibrating, playing a sound, etc.). Make sure to call through to
375 * the superclass implementation.
376 */
377 protected void onVolumeChanged(int streamType, int flags) {
378
379 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
380
381 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800382 if (mActiveStreamType == -1) {
383 reorderSliders(streamType);
384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 onShowVolumeChanged(streamType, flags);
386 }
387
Marco Nelissen69f593c2009-07-28 09:55:04 -0700388 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 removeMessages(MSG_PLAY_SOUND);
390 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
391 }
392
393 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
394 removeMessages(MSG_PLAY_SOUND);
395 removeMessages(MSG_VIBRATE);
396 onStopSounds();
397 }
398
399 removeMessages(MSG_FREE_RESOURCES);
400 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800401
402 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404
405 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurentd72d51c2011-02-03 18:47:47 -0800406 int index = mAudioService.isStreamMute(streamType) ?
407 mAudioService.getLastAudibleStreamVolume(streamType)
408 : mAudioService.getStreamVolume(streamType);
409
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800410// int message = UNKNOWN_VOLUME_TEXT;
411// int additionalMessage = 0;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700412 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413
414 if (LOGD) {
415 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
416 + ", flags: " + flags + "), index: " + index);
417 }
418
419 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 int max = mAudioService.getStreamMaxVolume(streamType);
422
423 switch (streamType) {
424
425 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800426// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700427 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
428 mContext, RingtoneManager.TYPE_RINGTONE);
429 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700430 mRingIsSilent = true;
431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 break;
433 }
434
435 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800436 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800437 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
438 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
439 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
440 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800441 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800443 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 break;
446 }
447
448 case AudioManager.STREAM_VOICE_CALL: {
449 /*
450 * For in-call voice call volume, there is no inaudible volume.
451 * Rescale the UI control so the progress bar doesn't go all
452 * the way to zero and don't show the mute icon.
453 */
454 index++;
455 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 break;
457 }
458
459 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 break;
461 }
462
463 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700464 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
465 mContext, RingtoneManager.TYPE_NOTIFICATION);
466 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700467 mRingIsSilent = true;
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 break;
470 }
471
472 case AudioManager.STREAM_BLUETOOTH_SCO: {
473 /*
474 * For in-call voice call volume, there is no inaudible volume.
475 * Rescale the UI control so the progress bar doesn't go all
476 * the way to zero and don't show the mute icon.
477 */
478 index++;
479 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 break;
481 }
482 }
483
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800484 StreamControl sc = mStreamControls.get(streamType);
485 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700486 if (sc.seekbarView.getMax() != max) {
487 sc.seekbarView.setMax(max);
488 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800489 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800492 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800493 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800494 mDialog.setContentView(mView);
495 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700496 if (mShowCombinedVolumes) {
497 collapse();
498 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800499 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 // Do a little vibrate if applicable (only when going into vibrate mode)
503 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
504 mAudioService.isStreamAffectedByRingerMode(streamType) &&
505 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
506 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
507 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510
511 protected void onPlaySound(int streamType, int flags) {
512
513 if (hasMessages(MSG_STOP_SOUNDS)) {
514 removeMessages(MSG_STOP_SOUNDS);
515 // Force stop right now
516 onStopSounds();
517 }
518
519 synchronized (this) {
520 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800521 if (toneGen != null) {
522 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
523 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527
528 protected void onStopSounds() {
529
530 synchronized (this) {
531 int numStreamTypes = AudioSystem.getNumStreamTypes();
532 for (int i = numStreamTypes - 1; i >= 0; i--) {
533 ToneGenerator toneGen = mToneGenerators[i];
534 if (toneGen != null) {
535 toneGen.stopTone();
536 }
537 }
538 }
539 }
540
541 protected void onVibrate() {
542
543 // Make sure we ended up in vibrate ringer mode
544 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
545 return;
546 }
547
548 mVibrator.vibrate(VIBRATE_DURATION);
549 }
550
551 /**
552 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
553 */
554 private ToneGenerator getOrCreateToneGenerator(int streamType) {
555 synchronized (this) {
556 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800557 try {
558 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
559 } catch (RuntimeException e) {
560 if (LOGD) {
561 Log.d(TAG, "ToneGenerator constructor failed with "
562 + "RuntimeException: " + e);
563 }
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800566 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800572 * Switch between icons because Bluetooth music is same as music volume, but with
573 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800575 private void setMusicIcon(int resId, int resMuteId) {
576 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
577 if (sc != null) {
578 sc.iconRes = resId;
579 sc.iconMuteRes = resMuteId;
580 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583
584 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 synchronized (this) {
586 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
587 if (mToneGenerators[i] != null) {
588 mToneGenerators[i].release();
589 }
590 mToneGenerators[i] = null;
591 }
592 }
593 }
594
595 @Override
596 public void handleMessage(Message msg) {
597 switch (msg.what) {
598
599 case MSG_VOLUME_CHANGED: {
600 onVolumeChanged(msg.arg1, msg.arg2);
601 break;
602 }
603
604 case MSG_FREE_RESOURCES: {
605 onFreeResources();
606 break;
607 }
608
609 case MSG_STOP_SOUNDS: {
610 onStopSounds();
611 break;
612 }
613
614 case MSG_PLAY_SOUND: {
615 onPlaySound(msg.arg1, msg.arg2);
616 break;
617 }
618
619 case MSG_VIBRATE: {
620 onVibrate();
621 break;
622 }
623
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800624 case MSG_TIMEOUT: {
625 if (mDialog.isShowing()) {
626 mDialog.dismiss();
627 mActiveStreamType = -1;
628 }
629 break;
630 }
631 case MSG_RINGER_MODE_CHANGED: {
632 if (mDialog.isShowing()) {
633 updateStates();
634 }
635 break;
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638 }
639
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800640 private void resetTimeout() {
641 removeMessages(MSG_TIMEOUT);
642 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
643 }
644
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700645 private void forceTimeout() {
646 removeMessages(MSG_TIMEOUT);
647 sendMessage(obtainMessage(MSG_TIMEOUT));
648 }
649
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800650 public void onProgressChanged(SeekBar seekBar, int progress,
651 boolean fromUser) {
652 final Object tag = seekBar.getTag();
653 if (fromUser && tag instanceof StreamControl) {
654 StreamControl sc = (StreamControl) tag;
655 if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
656 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
657 }
658 }
659 resetTimeout();
660 }
661
662 public void onStartTrackingTouch(SeekBar seekBar) {
663 }
664
665 public void onStopTrackingTouch(SeekBar seekBar) {
666 }
667
668 public void onClick(View v) {
669 if (v == mMoreButton) {
670 expand();
671 } else if (v.getTag() instanceof StreamControl) {
672 StreamControl sc = (StreamControl) v.getTag();
673 mAudioManager.setRingerMode(mAudioManager.isSilentMode()
674 ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT);
675 // Expand the dialog if it hasn't been expanded yet.
Amith Yamasani42722bf2011-07-22 10:34:27 -0700676 if (mShowCombinedVolumes && !isExpanded()) expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800677 }
678 resetTimeout();
679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680}