blob: 78984e08ff585f7a0dc3a581cd6cc628852dcc44 [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;
Mike Lockwoodce952c82011-11-14 10:47:42 -080094 private static final int MSG_MUTE_CHANGED = 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040096 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -080097 private static final int STREAM_MASTER = -100;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -040098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 protected Context mContext;
100 private AudioManager mAudioManager;
101 protected AudioService mAudioService;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700102 private boolean mRingIsSilent;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700103 private boolean mShowCombinedVolumes;
Amith Yamasani71def772011-10-12 12:25:24 -0700104 private boolean mVoiceCapable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800106 /** Dialog containing all the sliders */
107 private final Dialog mDialog;
108 /** Dialog's content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private final View mView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800110
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700111 /** The visible portion of the volume overlay */
112 private final ViewGroup mPanel;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800113 /** Contains the sliders and their touchable icons */
114 private final ViewGroup mSliderGroup;
115 /** The button that expands the dialog to show all sliders */
116 private final View mMoreButton;
117 /** Dummy divider icon that needs to vanish with the more button */
118 private final View mDivider;
119
120 /** Currently active stream that shows up at the top of the list of sliders */
121 private int mActiveStreamType = -1;
122 /** All the slider controls mapped by stream type */
123 private HashMap<Integer,StreamControl> mStreamControls;
124
Amith Yamasani71def772011-10-12 12:25:24 -0700125 private enum StreamResources {
126 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
127 R.string.volume_icon_description_bluetooth,
128 R.drawable.ic_audio_bt,
129 R.drawable.ic_audio_bt,
130 false),
131 RingerStream(AudioManager.STREAM_RING,
132 R.string.volume_icon_description_ringer,
133 R.drawable.ic_audio_ring_notif,
134 R.drawable.ic_audio_ring_notif_mute,
135 false),
136 VoiceStream(AudioManager.STREAM_VOICE_CALL,
137 R.string.volume_icon_description_incall,
138 R.drawable.ic_audio_phone,
139 R.drawable.ic_audio_phone,
140 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700141 AlarmStream(AudioManager.STREAM_ALARM,
142 R.string.volume_alarm,
143 R.drawable.ic_audio_alarm,
144 R.drawable.ic_audio_alarm_mute,
145 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700146 MediaStream(AudioManager.STREAM_MUSIC,
147 R.string.volume_icon_description_media,
148 R.drawable.ic_audio_vol,
149 R.drawable.ic_audio_vol_mute,
150 true),
151 NotificationStream(AudioManager.STREAM_NOTIFICATION,
152 R.string.volume_icon_description_notification,
153 R.drawable.ic_audio_notification,
154 R.drawable.ic_audio_notification_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400155 true),
156 // for now, use media resources for master volume
157 MasterStream(STREAM_MASTER,
158 R.string.volume_icon_description_media,
159 R.drawable.ic_audio_vol,
160 R.drawable.ic_audio_vol_mute,
161 false);
Amith Yamasani71def772011-10-12 12:25:24 -0700162
163 int streamType;
164 int descRes;
165 int iconRes;
166 int iconMuteRes;
167 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
168 boolean show;
169
170 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
171 this.streamType = streamType;
172 this.descRes = descRes;
173 this.iconRes = iconRes;
174 this.iconMuteRes = iconMuteRes;
175 this.show = show;
176 }
177 };
178
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800179 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700180 private static final StreamResources[] STREAMS = {
181 StreamResources.BluetoothSCOStream,
182 StreamResources.RingerStream,
183 StreamResources.VoiceStream,
184 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700185 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400186 StreamResources.AlarmStream,
187 StreamResources.MasterStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800188 };
189
190 /** Object that contains data for each slider */
191 private class StreamControl {
192 int streamType;
193 ViewGroup group;
194 ImageView icon;
195 SeekBar seekbarView;
196 int iconRes;
197 int iconMuteRes;
198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 // Synchronize when accessing this
201 private ToneGenerator mToneGenerators[];
202 private Vibrator mVibrator;
203
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800204 public VolumePanel(final Context context, AudioService volumeService) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 mContext = context;
206 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
207 mAudioService = volumeService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400209 // For now, only show master volume if master volume is supported
210 boolean useMasterVolume = context.getResources().getBoolean(
211 com.android.internal.R.bool.config_useMasterVolume);
212 if (useMasterVolume) {
213 for (int i = 0; i < STREAMS.length; i++) {
214 StreamResources streamRes = STREAMS[i];
215 streamRes.show = (streamRes.streamType == STREAM_MASTER);
216 }
217 }
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 LayoutInflater inflater = (LayoutInflater) context
220 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800221 View view = mView = inflater.inflate(R.layout.volume_adjust, null);
222 mView.setOnTouchListener(new View.OnTouchListener() {
223 public boolean onTouch(View v, MotionEvent event) {
224 resetTimeout();
Amith Yamasani284e6302011-09-16 18:24:47 -0700225 return false;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800226 }
227 });
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700228 mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800229 mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
230 mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800231 mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
232
Amith Yamasani284e6302011-09-16 18:24:47 -0700233 mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
234 public boolean onTouchEvent(MotionEvent event) {
235 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
236 forceTimeout();
237 return true;
238 }
239 return false;
240 }
241 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800242 mDialog.setTitle("Volume control"); // No need to localize
243 mDialog.setContentView(mView);
244 mDialog.setOnDismissListener(new OnDismissListener() {
245 public void onDismiss(DialogInterface dialog) {
246 mActiveStreamType = -1;
Eric Laurent402f7f22011-02-04 12:30:32 -0800247 mAudioManager.forceVolumeControlStream(mActiveStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800248 }
249 });
250 // Change some window properties
251 Window window = mDialog.getWindow();
252 window.setGravity(Gravity.TOP);
Amith Yamasani284e6302011-09-16 18:24:47 -0700253 LayoutParams lp = window.getAttributes();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800254 lp.token = null;
Amith Yamasani284e6302011-09-16 18:24:47 -0700255 // Offset from the top
256 lp.y = mContext.getResources().getDimensionPixelOffset(
257 com.android.internal.R.dimen.volume_panel_top);
258 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
259 lp.width = LayoutParams.WRAP_CONTENT;
260 lp.height = LayoutParams.WRAP_CONTENT;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800261 window.setAttributes(lp);
Amith Yamasani284e6302011-09-16 18:24:47 -0700262 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
263 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Jeff Brownc2346132012-04-13 01:55:38 -0700266 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800267
Amith Yamasani71def772011-10-12 12:25:24 -0700268 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400269 mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
Amith Yamasani42722bf2011-07-22 10:34:27 -0700270 // If we don't want to show multiple volumes, hide the settings button and divider
271 if (!mShowCombinedVolumes) {
272 mMoreButton.setVisibility(View.GONE);
273 mDivider.setVisibility(View.GONE);
274 } else {
275 mMoreButton.setOnClickListener(this);
276 }
277
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800278 listenToRingerMode();
279 }
280
281 private void listenToRingerMode() {
282 final IntentFilter filter = new IntentFilter();
283 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
284 mContext.registerReceiver(new BroadcastReceiver() {
285
286 public void onReceive(Context context, Intent intent) {
287 final String action = intent.getAction();
288
289 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
290 removeMessages(MSG_RINGER_MODE_CHANGED);
291 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
292 }
293 }
294 }, filter);
295 }
296
297 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400298 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700299 return mAudioManager.isMasterMute();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400300 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700301 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400302 }
303 }
304
305 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400306 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700307 return mAudioManager.getMasterMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400308 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700309 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400310 }
311 }
312
313 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400314 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700315 return mAudioManager.getMasterVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400316 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700317 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400318 }
319 }
320
321 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400322 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700323 mAudioManager.setMasterVolume(index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400324 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700325 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400326 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800327 }
328
329 private void createSliders() {
330 LayoutInflater inflater = (LayoutInflater) mContext
331 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700332 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700333 Resources res = mContext.getResources();
Amith Yamasani71def772011-10-12 12:25:24 -0700334 for (int i = 0; i < STREAMS.length; i++) {
335 StreamResources streamRes = STREAMS[i];
336 int streamType = streamRes.streamType;
337 if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
338 streamRes = StreamResources.RingerStream;
339 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800340 StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700341 sc.streamType = streamType;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800342 sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
343 sc.group.setTag(sc);
344 sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800345 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700346 sc.icon.setContentDescription(res.getString(streamRes.descRes));
347 sc.iconRes = streamRes.iconRes;
348 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800349 sc.icon.setImageResource(sc.iconRes);
350 sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700351 int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
352 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400353 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800354 sc.seekbarView.setOnSeekBarChangeListener(this);
355 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700356 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800357 }
358 }
359
360 private void reorderSliders(int activeStreamType) {
361 mSliderGroup.removeAllViews();
362
363 StreamControl active = mStreamControls.get(activeStreamType);
364 if (active == null) {
365 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
366 mActiveStreamType = -1;
367 } else {
368 mSliderGroup.addView(active.group);
369 mActiveStreamType = activeStreamType;
370 active.group.setVisibility(View.VISIBLE);
371 updateSlider(active);
372 }
373
Amith Yamasani42722bf2011-07-22 10:34:27 -0700374 addOtherVolumes();
375 }
376
377 private void addOtherVolumes() {
378 if (!mShowCombinedVolumes) return;
379
Amith Yamasani71def772011-10-12 12:25:24 -0700380 for (int i = 0; i < STREAMS.length; i++) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800381 // Skip the phone specific ones and the active one
Amith Yamasani71def772011-10-12 12:25:24 -0700382 final int streamType = STREAMS[i].streamType;
383 if (!STREAMS[i].show || streamType == mActiveStreamType) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800384 continue;
385 }
386 StreamControl sc = mStreamControls.get(streamType);
387 mSliderGroup.addView(sc.group);
388 updateSlider(sc);
389 }
390 }
391
392 /** Update the mute and progress state of a slider */
393 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700394 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800395 final boolean muted = isMuted(sc.streamType);
396 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8c787522012-05-14 14:09:43 -0700397 if (sc.streamType == AudioManager.STREAM_RING &&
398 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
Amith Yamasanic696a532011-10-28 17:02:37 -0700399 sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
400 }
Eric Laurent8c787522012-05-14 14:09:43 -0700401 if (sc.streamType != mAudioManager.getMasterStreamType() && muted) {
402 sc.seekbarView.setEnabled(false);
403 } else {
404 sc.seekbarView.setEnabled(true);
405 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800406 }
407
408 private boolean isExpanded() {
409 return mMoreButton.getVisibility() != View.VISIBLE;
410 }
411
412 private void expand() {
413 final int count = mSliderGroup.getChildCount();
414 for (int i = 0; i < count; i++) {
415 mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
416 }
417 mMoreButton.setVisibility(View.INVISIBLE);
418 mDivider.setVisibility(View.INVISIBLE);
419 }
420
421 private void collapse() {
422 mMoreButton.setVisibility(View.VISIBLE);
423 mDivider.setVisibility(View.VISIBLE);
424 final int count = mSliderGroup.getChildCount();
425 for (int i = 1; i < count; i++) {
426 mSliderGroup.getChildAt(i).setVisibility(View.GONE);
427 }
428 }
429
430 private void updateStates() {
431 final int count = mSliderGroup.getChildCount();
432 for (int i = 0; i < count; i++) {
433 StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
434 updateSlider(sc);
435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
437
438 public void postVolumeChanged(int streamType, int flags) {
439 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800440 if (mStreamControls == null) {
441 createSliders();
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 removeMessages(MSG_FREE_RESOURCES);
444 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
445 }
446
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400447 public void postMasterVolumeChanged(int flags) {
448 postVolumeChanged(STREAM_MASTER, flags);
449 }
450
Mike Lockwoodce952c82011-11-14 10:47:42 -0800451 public void postMuteChanged(int streamType, int flags) {
452 if (hasMessages(MSG_VOLUME_CHANGED)) return;
453 if (mStreamControls == null) {
454 createSliders();
455 }
456 removeMessages(MSG_FREE_RESOURCES);
457 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
458 }
459
460 public void postMasterMuteChanged(int flags) {
461 postMuteChanged(STREAM_MASTER, flags);
462 }
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * Override this if you have other work to do when the volume changes (for
466 * example, vibrating, playing a sound, etc.). Make sure to call through to
467 * the superclass implementation.
468 */
469 protected void onVolumeChanged(int streamType, int flags) {
470
471 if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
472
473 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasani8df96092011-12-08 17:03:54 -0800474 if (mActiveStreamType != streamType) {
Amith Yamasanie3361b82011-02-10 18:20:50 -0800475 reorderSliders(streamType);
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 onShowVolumeChanged(streamType, flags);
478 }
479
Marco Nelissen69f593c2009-07-28 09:55:04 -0700480 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 removeMessages(MSG_PLAY_SOUND);
482 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
483 }
484
485 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
486 removeMessages(MSG_PLAY_SOUND);
487 removeMessages(MSG_VIBRATE);
488 onStopSounds();
489 }
490
491 removeMessages(MSG_FREE_RESOURCES);
492 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800493
494 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
Mike Lockwoodce952c82011-11-14 10:47:42 -0800497 protected void onMuteChanged(int streamType, int flags) {
498
499 if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
500
501 StreamControl sc = mStreamControls.get(streamType);
502 if (sc != null) {
503 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
504 }
505
506 onVolumeChanged(streamType, flags);
507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700510 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800511
Marco Nelissen69f593c2009-07-28 09:55:04 -0700512 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
514 if (LOGD) {
515 Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
516 + ", flags: " + flags + "), index: " + index);
517 }
518
519 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800520
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400521 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
523 switch (streamType) {
524
525 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800526// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700527 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
528 mContext, RingtoneManager.TYPE_RINGTONE);
529 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700530 mRingIsSilent = true;
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 break;
533 }
534
535 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800536 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800537 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
538 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
539 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
540 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800541 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800543 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545 break;
546 }
547
548 case AudioManager.STREAM_VOICE_CALL: {
549 /*
550 * For in-call voice call volume, there is no inaudible volume.
551 * Rescale the UI control so the progress bar doesn't go all
552 * the way to zero and don't show the mute icon.
553 */
554 index++;
555 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 break;
557 }
558
559 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 break;
561 }
562
563 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700564 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
565 mContext, RingtoneManager.TYPE_NOTIFICATION);
566 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700567 mRingIsSilent = true;
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 break;
570 }
571
572 case AudioManager.STREAM_BLUETOOTH_SCO: {
573 /*
574 * For in-call voice call volume, there is no inaudible volume.
575 * Rescale the UI control so the progress bar doesn't go all
576 * the way to zero and don't show the mute icon.
577 */
578 index++;
579 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 break;
581 }
582 }
583
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800584 StreamControl sc = mStreamControls.get(streamType);
585 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700586 if (sc.seekbarView.getMax() != max) {
587 sc.seekbarView.setMax(max);
588 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800589 sc.seekbarView.setProgress(index);
Eric Laurent8c787522012-05-14 14:09:43 -0700590 if (streamType != mAudioManager.getMasterStreamType() && isMuted(streamType)) {
591 sc.seekbarView.setEnabled(false);
592 } else {
593 sc.seekbarView.setEnabled(true);
594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800597 if (!mDialog.isShowing()) {
Eric Laurent402f7f22011-02-04 12:30:32 -0800598 mAudioManager.forceVolumeControlStream(streamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800599 mDialog.setContentView(mView);
600 // Showing dialog - use collapsed state
Amith Yamasani42722bf2011-07-22 10:34:27 -0700601 if (mShowCombinedVolumes) {
602 collapse();
603 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800604 mDialog.show();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 // Do a little vibrate if applicable (only when going into vibrate mode)
608 if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
609 mAudioService.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700610 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
614
615 protected void onPlaySound(int streamType, int flags) {
616
617 if (hasMessages(MSG_STOP_SOUNDS)) {
618 removeMessages(MSG_STOP_SOUNDS);
619 // Force stop right now
620 onStopSounds();
621 }
622
623 synchronized (this) {
624 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800625 if (toneGen != null) {
626 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
627 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631
632 protected void onStopSounds() {
633
634 synchronized (this) {
635 int numStreamTypes = AudioSystem.getNumStreamTypes();
636 for (int i = numStreamTypes - 1; i >= 0; i--) {
637 ToneGenerator toneGen = mToneGenerators[i];
638 if (toneGen != null) {
639 toneGen.stopTone();
640 }
641 }
642 }
643 }
644
645 protected void onVibrate() {
646
647 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700648 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 return;
650 }
651
652 mVibrator.vibrate(VIBRATE_DURATION);
653 }
654
655 /**
656 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
657 */
658 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400659 if (streamType == STREAM_MASTER) return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 synchronized (this) {
661 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800662 try {
663 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
664 } catch (RuntimeException e) {
665 if (LOGD) {
666 Log.d(TAG, "ToneGenerator constructor failed with "
667 + "RuntimeException: " + e);
668 }
669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800671 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 }
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
676 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800677 * Switch between icons because Bluetooth music is same as music volume, but with
678 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800680 private void setMusicIcon(int resId, int resMuteId) {
681 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
682 if (sc != null) {
683 sc.iconRes = resId;
684 sc.iconMuteRes = resMuteId;
685 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688
689 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 synchronized (this) {
691 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
692 if (mToneGenerators[i] != null) {
693 mToneGenerators[i].release();
694 }
695 mToneGenerators[i] = null;
696 }
697 }
698 }
699
700 @Override
701 public void handleMessage(Message msg) {
702 switch (msg.what) {
703
704 case MSG_VOLUME_CHANGED: {
705 onVolumeChanged(msg.arg1, msg.arg2);
706 break;
707 }
708
Mike Lockwoodce952c82011-11-14 10:47:42 -0800709 case MSG_MUTE_CHANGED: {
710 onMuteChanged(msg.arg1, msg.arg2);
711 break;
712 }
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 case MSG_FREE_RESOURCES: {
715 onFreeResources();
716 break;
717 }
718
719 case MSG_STOP_SOUNDS: {
720 onStopSounds();
721 break;
722 }
723
724 case MSG_PLAY_SOUND: {
725 onPlaySound(msg.arg1, msg.arg2);
726 break;
727 }
728
729 case MSG_VIBRATE: {
730 onVibrate();
731 break;
732 }
733
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800734 case MSG_TIMEOUT: {
735 if (mDialog.isShowing()) {
736 mDialog.dismiss();
737 mActiveStreamType = -1;
738 }
739 break;
740 }
741 case MSG_RINGER_MODE_CHANGED: {
742 if (mDialog.isShowing()) {
743 updateStates();
744 }
745 break;
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 }
749
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800750 private void resetTimeout() {
751 removeMessages(MSG_TIMEOUT);
752 sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
753 }
754
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700755 private void forceTimeout() {
756 removeMessages(MSG_TIMEOUT);
757 sendMessage(obtainMessage(MSG_TIMEOUT));
758 }
759
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800760 public void onProgressChanged(SeekBar seekBar, int progress,
761 boolean fromUser) {
762 final Object tag = seekBar.getTag();
763 if (fromUser && tag instanceof StreamControl) {
764 StreamControl sc = (StreamControl) tag;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400765 if (getStreamVolume(sc.streamType) != progress) {
766 setStreamVolume(sc.streamType, progress, 0);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800767 }
768 }
769 resetTimeout();
770 }
771
772 public void onStartTrackingTouch(SeekBar seekBar) {
773 }
774
775 public void onStopTrackingTouch(SeekBar seekBar) {
776 }
777
778 public void onClick(View v) {
779 if (v == mMoreButton) {
780 expand();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800781 }
782 resetTimeout();
783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784}