blob: 0d60d3f03f0aa726ef3498281efa56aa6e90c4e6 [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
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040095 // Pseudo stream type for master volume
96 private static final int STREAM_MASTER = -1;
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 protected Context mContext;
99 private AudioManager mAudioManager;
100 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700101 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700102 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700103 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800105 /** Dialog containing all the sliders */
106 private final Dialog mDialog;
107 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800109
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700110 /** The visible portion of the volume overlay */
111 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800112 /** Contains the sliders and their touchable icons */
113 private final ViewGroup mSliderGroup;
114 /** The button that expands the dialog to show all sliders */
115 private final View mMoreButton;
116 /** Dummy divider icon that needs to vanish with the more button */
117 private final View mDivider;
118
119 /** Currently active stream that shows up at the top of the list of sliders */
120 private int mActiveStreamType = -1;
121 /** All the slider controls mapped by stream type */
122 private HashMap<Integer,StreamControl> mStreamControls;
123
Amith Yamasani71def772011-10-12 12:25:24 -0700124 private enum StreamResources {
125 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
126 R.string.volume_icon_description_bluetooth,
127 R.drawable.ic_audio_bt,
128 R.drawable.ic_audio_bt,
129 false),
130 RingerStream(AudioManager.STREAM_RING,
131 R.string.volume_icon_description_ringer,
132 R.drawable.ic_audio_ring_notif,
133 R.drawable.ic_audio_ring_notif_mute,
134 false),
135 VoiceStream(AudioManager.STREAM_VOICE_CALL,
136 R.string.volume_icon_description_incall,
137 R.drawable.ic_audio_phone,
138 R.drawable.ic_audio_phone,
139 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700140 AlarmStream(AudioManager.STREAM_ALARM,
141 R.string.volume_alarm,
142 R.drawable.ic_audio_alarm,
143 R.drawable.ic_audio_alarm_mute,
144 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700145 MediaStream(AudioManager.STREAM_MUSIC,
146 R.string.volume_icon_description_media,
147 R.drawable.ic_audio_vol,
148 R.drawable.ic_audio_vol_mute,
149 true),
150 NotificationStream(AudioManager.STREAM_NOTIFICATION,
151 R.string.volume_icon_description_notification,
152 R.drawable.ic_audio_notification,
153 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400154 true),
155 // for now, use media resources for master volume
156 MasterStream(STREAM_MASTER,
157 R.string.volume_icon_description_media,
158 R.drawable.ic_audio_vol,
159 R.drawable.ic_audio_vol_mute,
160 false);
Amith Yamasani71def772011-10-12 12:25:24 -0700161
162 int streamType;
163 int descRes;
164 int iconRes;
165 int iconMuteRes;
166 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
167 boolean show;
168
169 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
170 this.streamType = streamType;
171 this.descRes = descRes;
172 this.iconRes = iconRes;
173 this.iconMuteRes = iconMuteRes;
174 this.show = show;
175 }
176 };
177
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800178 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700179 private static final StreamResources[] STREAMS = {
180 StreamResources.BluetoothSCOStream,
181 StreamResources.RingerStream,
182 StreamResources.VoiceStream,
183 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700184 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400185 StreamResources.AlarmStream,
186 StreamResources.MasterStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800187 };
188
189 /** Object that contains data for each slider */
190 private class StreamControl {
191 int streamType;
192 ViewGroup group;
193 ImageView icon;
194 SeekBar seekbarView;
195 int iconRes;
196 int iconMuteRes;
197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 // Synchronize when accessing this
200 private ToneGenerator mToneGenerators[];
201 private Vibrator mVibrator;
202
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800203 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 mContext = context;
205 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
206 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400208 // For now, only show master volume if master volume is supported
209 boolean useMasterVolume = context.getResources().getBoolean(
210 com.android.internal.R.bool.config_useMasterVolume);
211 if (useMasterVolume) {
212 for (int i = 0; i < STREAMS.length; i++) {
213 StreamResources streamRes = STREAMS[i];
214 streamRes.show = (streamRes.streamType == STREAM_MASTER);
215 }
216 }
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 LayoutInflater inflater = (LayoutInflater) context
219 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800220 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
221 mView.setOnTouchListener(new View.OnTouchListener() {
222 public boolean onTouch(View v, MotionEvent event) {
223 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700224 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800225 }
226 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700227 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800228 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
229 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800230 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
231
Amith Yamasani284e6302011-09-16 18:24:47 -0700232 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
233 public boolean onTouchEvent(MotionEvent event) {
234 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
235 forceTimeout();
236 return true;
237 }
238 return false;
239 }
240 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800241 mDialog.setTitle("Volume control"); // No need to localize
242 mDialog.setContentView(mView);
243 mDialog.setOnDismissListener(new OnDismissListener() {
244 public void onDismiss(DialogInterface dialog) {
245 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800246 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800247 }
248 });
249 // Change some window properties
250 Window window = mDialog.getWindow();
251 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700252 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800253 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700254 // Offset from the top
255 lp.y = mContext.getResources().getDimensionPixelOffset(
256 com.android.internal.R.dimen.volume_panel_top);
257 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
258 lp.width = LayoutParams.WRAP_CONTENT;
259 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800260 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700261 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
262 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
265 mVibrator = new Vibrator();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800266
Amith Yamasani71def772011-10-12 12:25:24 -0700267 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400268 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700269 // If we don't want to show multiple volumes, hide the settings button and divider
270 if (!mShowCombinedVolumes) {
271 mMoreButton.setVisibility(View.GONE);
272 mDivider.setVisibility(View.GONE);
273 } else {
274 mMoreButton.setOnClickListener(this);
275 }
276
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800277 listenToRingerMode();
278 }
279
280 private void listenToRingerMode() {
281 final IntentFilter filter = new IntentFilter();
282 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
283 mContext.registerReceiver(new BroadcastReceiver() {
284
285 public void onReceive(Context context, Intent intent) {
286 final String action = intent.getAction();
287
288 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
289 removeMessages(MSG_RINGER_MODE_CHANGED);
290 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
291 }
292 }
293 }, filter);
294 }
295
296 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400297 if (streamType == STREAM_MASTER) {
298 // master volume mute not yet supported
299 return false;
300 } else {
301 return mAudioService.isStreamMute(streamType);
302 }
303 }
304
305 private int getStreamMaxVolume(int streamType) {
306 // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
307 if (streamType == STREAM_MASTER) {
308 return 100;
309 } else {
310 return mAudioService.getStreamMaxVolume(streamType);
311 }
312 }
313
314 private int getStreamVolume(int streamType) {
315 // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
316 if (streamType == STREAM_MASTER) {
317 return Math.round(mAudioService.getMasterVolume() * 100);
318 } else {
319 return mAudioService.getStreamVolume(streamType);
320 }
321 }
322
323 private void setStreamVolume(int streamType, int index, int flags) {
324 // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
325 if (streamType == STREAM_MASTER) {
326 mAudioService.setMasterVolume((float)index / 100.0f);
327 } else {
328 mAudioService.setStreamVolume(streamType, index, flags);
329 }
330 }
331
332 private int getLastAudibleStreamVolume(int streamType) {
333 // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
334 if (streamType == STREAM_MASTER) {
335 // master volume mute not yet supported
336 return getStreamVolume(STREAM_MASTER);
337 } else {
338 return mAudioService.getLastAudibleStreamVolume(streamType);
339 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800340 }
341
342 private void createSliders() {
343 LayoutInflater inflater = (LayoutInflater) mContext
344 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700345 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700346 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700347 for (int i = 0; i < STREAMS.length; i++) {
348 StreamResources streamRes = STREAMS[i];
349 int streamType = streamRes.streamType;
350 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
351 streamRes = StreamResources.RingerStream;
352 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800353 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700354 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800355 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
356 sc.group.setTag(sc);
357 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800358 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700359 sc.icon.setContentDescription(res.getString(streamRes.descRes));
360 sc.iconRes = streamRes.iconRes;
361 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800362 sc.icon.setImageResource(sc.iconRes);
363 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700364 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
365 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400366 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800367 sc.seekbarView.setOnSeekBarChangeListener(this);
368 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700369 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800370 }
371 }
372
373 private void reorderSliders(int activeStreamType) {
374 mSliderGroup.removeAllViews();
375
376 StreamControl active = mStreamControls.get(activeStreamType);
377 if (active == null) {
378 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
379 mActiveStreamType = -1;
380 } else {
381 mSliderGroup.addView(active.group);
382 mActiveStreamType = activeStreamType;
383 active.group.setVisibility(View.VISIBLE);
384 updateSlider(active);
385 }
386
Amith Yamasani42722bf2011-07-22 10:34:27 -0700387 addOtherVolumes();
388 }
389
390 private void addOtherVolumes() {
391 if (!mShowCombinedVolumes) return;
392
Amith Yamasani71def772011-10-12 12:25:24 -0700393 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800394 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700395 final int streamType = STREAMS[i].streamType;
396 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800397 continue;
398 }
399 StreamControl sc = mStreamControls.get(streamType);
400 mSliderGroup.addView(sc.group);
401 updateSlider(sc);
402 }
403 }
404
405 /** Update the mute and progress state of a slider */
406 private void updateSlider(StreamControl sc) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400407 sc.seekbarView.setProgress(getLastAudibleStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800408 final boolean muted = isMuted(sc.streamType);
409 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Amith Yamasanic696a532011-10-28 17:02:37 -0700410 if (sc.streamType == AudioManager.STREAM_RING && muted
411 && mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
412 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
413 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800414 }
415
416 private boolean isExpanded() {
417 return mMoreButton.getVisibility() != View.VISIBLE;
418 }
419
420 private void expand() {
421 final int count = mSliderGroup.getChildCount();
422 for (int i = 0; i < count; i++) {
423 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
424 }
425 mMoreButton.setVisibility(View.INVISIBLE);
426 mDivider.setVisibility(View.INVISIBLE);
427 }
428
429 private void collapse() {
430 mMoreButton.setVisibility(View.VISIBLE);
431 mDivider.setVisibility(View.VISIBLE);
432 final int count = mSliderGroup.getChildCount();
433 for (int i = 1; i < count; i++) {
434 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
435 }
436 }
437
438 private void updateStates() {
439 final int count = mSliderGroup.getChildCount();
440 for (int i = 0; i < count; i++) {
441 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
442 updateSlider(sc);
443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
446 public void postVolumeChanged(int streamType, int flags) {
447 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800448 if (mStreamControls == null) {
449 createSliders();
450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 removeMessages(MSG_FREE_RESOURCES);
452 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
453 }
454
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400455 public void postMasterVolumeChanged(int flags) {
456 postVolumeChanged(STREAM_MASTER, flags);
457 }
458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 /**
460 * Override this if you have other work to do when the volume changes (for
461 * example, vibrating, playing a sound, etc.). Make sure to call through to
462 * the superclass implementation.
463 */
464 protected void onVolumeChanged(int streamType, int flags) {
465
466 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
467
468 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasani8df96092011-12-08 17:03:54 -0800469 if (mActiveStreamType != streamType) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800470 reorderSliders(streamType);
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 onShowVolumeChanged(streamType, flags);
473 }
474
Marco Nelissen69f593c2009-07-28 09:55:04 -0700475 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 removeMessages(MSG_PLAY_SOUND);
477 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
478 }
479
480 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
481 removeMessages(MSG_PLAY_SOUND);
482 removeMessages(MSG_VIBRATE);
483 onStopSounds();
484 }
485
486 removeMessages(MSG_FREE_RESOURCES);
487 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800488
489 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491
492 protected void onShowVolumeChanged(int streamType, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400493 int index = isMuted(streamType) ?
494 getLastAudibleStreamVolume(streamType)
495 : getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800496
Marco Nelissen69f593c2009-07-28 09:55:04 -0700497 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 if (LOGD) {
500 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
501 + ", flags: " + flags + "), index: " + index);
502 }
503
504 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800505
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400506 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
508 switch (streamType) {
509
510 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800511// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700512 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
513 mContext, RingtoneManager.TYPE_RINGTONE);
514 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700515 mRingIsSilent = true;
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 break;
518 }
519
520 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800521 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800522 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
523 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
524 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
525 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800526 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800528 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530 break;
531 }
532
533 case AudioManager.STREAM_VOICE_CALL: {
534 /*
535 * For in-call voice call volume, there is no inaudible volume.
536 * Rescale the UI control so the progress bar doesn't go all
537 * the way to zero and don't show the mute icon.
538 */
539 index++;
540 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 break;
542 }
543
544 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 break;
546 }
547
548 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700549 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
550 mContext, RingtoneManager.TYPE_NOTIFICATION);
551 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700552 mRingIsSilent = true;
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 break;
555 }
556
557 case AudioManager.STREAM_BLUETOOTH_SCO: {
558 /*
559 * For in-call voice call volume, there is no inaudible volume.
560 * Rescale the UI control so the progress bar doesn't go all
561 * the way to zero and don't show the mute icon.
562 */
563 index++;
564 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 break;
566 }
567 }
568
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800569 StreamControl sc = mStreamControls.get(streamType);
570 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700571 if (sc.seekbarView.getMax() != max) {
572 sc.seekbarView.setMax(max);
573 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800574 sc.seekbarView.setProgress(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800577 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800578 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800579 mDialog.setContentView(mView);
580 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700581 if (mShowCombinedVolumes) {
582 collapse();
583 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800584 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 // Do a little vibrate if applicable (only when going into vibrate mode)
588 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
589 mAudioService.isStreamAffectedByRingerMode(streamType) &&
590 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
591 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
592 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595
596 protected void onPlaySound(int streamType, int flags) {
597
598 if (hasMessages(MSG_STOP_SOUNDS)) {
599 removeMessages(MSG_STOP_SOUNDS);
600 // Force stop right now
601 onStopSounds();
602 }
603
604 synchronized (this) {
605 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800606 if (toneGen != null) {
607 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
608 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612
613 protected void onStopSounds() {
614
615 synchronized (this) {
616 int numStreamTypes = AudioSystem.getNumStreamTypes();
617 for (int i = numStreamTypes - 1; i >= 0; i--) {
618 ToneGenerator toneGen = mToneGenerators[i];
619 if (toneGen != null) {
620 toneGen.stopTone();
621 }
622 }
623 }
624 }
625
626 protected void onVibrate() {
627
628 // Make sure we ended up in vibrate ringer mode
629 if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
630 return;
631 }
632
633 mVibrator.vibrate(VIBRATE_DURATION);
634 }
635
636 /**
637 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
638 */
639 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400640 if (streamType == STREAM_MASTER) return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 synchronized (this) {
642 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800643 try {
644 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
645 } catch (RuntimeException e) {
646 if (LOGD) {
647 Log.d(TAG, "ToneGenerator constructor failed with "
648 + "RuntimeException: " + e);
649 }
650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800652 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654 }
655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
657 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800658 * Switch between icons because Bluetooth music is same as music volume, but with
659 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800661 private void setMusicIcon(int resId, int resMuteId) {
662 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
663 if (sc != null) {
664 sc.iconRes = resId;
665 sc.iconMuteRes = resMuteId;
666 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669
670 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 synchronized (this) {
672 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
673 if (mToneGenerators[i] != null) {
674 mToneGenerators[i].release();
675 }
676 mToneGenerators[i] = null;
677 }
678 }
679 }
680
681 @Override
682 public void handleMessage(Message msg) {
683 switch (msg.what) {
684
685 case MSG_VOLUME_CHANGED: {
686 onVolumeChanged(msg.arg1, msg.arg2);
687 break;
688 }
689
690 case MSG_FREE_RESOURCES: {
691 onFreeResources();
692 break;
693 }
694
695 case MSG_STOP_SOUNDS: {
696 onStopSounds();
697 break;
698 }
699
700 case MSG_PLAY_SOUND: {
701 onPlaySound(msg.arg1, msg.arg2);
702 break;
703 }
704
705 case MSG_VIBRATE: {
706 onVibrate();
707 break;
708 }
709
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800710 case MSG_TIMEOUT: {
711 if (mDialog.isShowing()) {
712 mDialog.dismiss();
713 mActiveStreamType = -1;
714 }
715 break;
716 }
717 case MSG_RINGER_MODE_CHANGED: {
718 if (mDialog.isShowing()) {
719 updateStates();
720 }
721 break;
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724 }
725
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800726 private void resetTimeout() {
727 removeMessages(MSG_TIMEOUT);
728 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
729 }
730
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700731 private void forceTimeout() {
732 removeMessages(MSG_TIMEOUT);
733 sendMessage(obtainMessage(MSG_TIMEOUT));
734 }
735
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800736 public void onProgressChanged(SeekBar seekBar, int progress,
737 boolean fromUser) {
738 final Object tag = seekBar.getTag();
739 if (fromUser && tag instanceof StreamControl) {
740 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400741 if (getStreamVolume(sc.streamType) != progress) {
742 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800743 }
744 }
745 resetTimeout();
746 }
747
748 public void onStartTrackingTouch(SeekBar seekBar) {
749 }
750
751 public void onStopTrackingTouch(SeekBar seekBar) {
752 }
753
754 public void onClick(View v) {
755 if (v == mMoreButton) {
756 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800757 }
758 resetTimeout();
759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760}