blob: cbfc64108cabeea2ef75c6d44750bcf79df71851 [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
John Spurlock3346a802014-05-20 16:25:37 -040017package com.android.systemui.volume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Eric Laurentc34dcc12012-09-10 13:51:52 -070019import android.app.AlertDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080020import android.app.Dialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080021import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080023import android.content.DialogInterface;
John Spurlock86005342014-05-23 11:58:00 -040024import android.content.DialogInterface.OnDismissListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080026import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.res.Resources;
John Spurlock2078caf2014-05-29 22:20:14 -040028import android.graphics.PixelFormat;
29import android.graphics.drawable.ColorDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.media.AudioManager;
31import android.media.AudioService;
32import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070033import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.media.ToneGenerator;
Marco Nelissen69f593c2009-07-28 09:55:04 -070035import android.net.Uri;
John Spurlock3bd4fee2014-05-29 20:51:09 -040036import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Handler;
38import android.os.Message;
39import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.Log;
John Spurlock3346a802014-05-20 16:25:37 -040041import android.view.Gravity;
42import android.view.LayoutInflater;
43import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewGroup;
John Spurlock86005342014-05-23 11:58:00 -040046import android.view.ViewStub;
John Spurlock3346a802014-05-20 16:25:37 -040047import android.view.Window;
48import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070049import android.view.WindowManager.LayoutParams;
John Spurlock2078caf2014-05-29 22:20:14 -040050import android.widget.FrameLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080052import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080053import android.widget.SeekBar.OnSeekBarChangeListener;
54
John Spurlock86005342014-05-23 11:58:00 -040055import com.android.internal.R;
56import com.android.systemui.statusbar.policy.ZenModeController;
57
Amith Yamasani2bbdd772011-02-02 18:54:13 -080058import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60/**
John Spurlock3346a802014-05-20 16:25:37 -040061 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070062 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 * @hide
64 */
John Spurlock3346a802014-05-20 16:25:37 -040065public class VolumePanel extends Handler {
Marco Nelissen69f593c2009-07-28 09:55:04 -070066 private static boolean LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
John Spurlock3346a802014-05-20 16:25:37 -040068 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70 /**
71 * The delay before vibrating. This small period exists so if the user is
72 * moving to silent mode, it will not emit a short vibrate (it normally
73 * would since vibrate is between normal mode and silent mode using hardware
74 * keys).
75 */
76 public static final int VIBRATE_DELAY = 300;
77
78 private static final int VIBRATE_DURATION = 300;
79 private static final int BEEP_DURATION = 150;
80 private static final int MAX_VOLUME = 100;
81 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080082 private static final int TIMEOUT_DELAY = 3000;
John Spurlock3bd4fee2014-05-29 20:51:09 -040083 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 private static final int MSG_VOLUME_CHANGED = 0;
86 private static final int MSG_FREE_RESOURCES = 1;
87 private static final int MSG_PLAY_SOUND = 2;
88 private static final int MSG_STOP_SOUNDS = 3;
89 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080090 private static final int MSG_TIMEOUT = 5;
91 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -080092 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -070093 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
94 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
95 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -070096 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -040097 private static final int MSG_LAYOUT_DIRECTION = 12;
98 private static final int MSG_ZEN_MODE_CHANGED = 13;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400100 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800101 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700102 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400103
John Spurlock86005342014-05-23 11:58:00 -0400104 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400105 protected final Context mContext;
106 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400107 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700108 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700109 private boolean mVoiceCapable;
John Spurlock86005342014-05-23 11:58:00 -0400110 private boolean mZenModeCapable;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400111 private int mTimeoutDelay = TIMEOUT_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Christopher Tatec4b78d22012-05-22 13:57:58 -0700113 // True if we want to play tones on the system stream when the master stream is specified.
114 private final boolean mPlayMasterStreamTones;
115
John Spurlock86005342014-05-23 11:58:00 -0400116
117 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private final View mView;
John Spurlock86005342014-05-23 11:58:00 -0400119 /** Dialog hosting the panel, if not embedded */
120 private final Dialog mDialog;
121 /** Parent view hosting the panel, if embedded */
122 private final ViewGroup mParent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800123
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700124 /** The visible portion of the volume overlay */
125 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400126 /** Contains the slider and its touchable icons */
127 private final ViewGroup mSliderPanel;
128 /** The button that expands the dialog to show the zen panel */
129 private final ImageView mExpandButton;
130 /** Dummy divider icon that needs to vanish with the expand button */
131 private final View mExpandDivider;
132 /** The zen mode configuration panel view stub */
133 private final ViewStub mZenPanelStub;
134 /** The zen mode configuration panel view, once inflated */
135 private ZenModePanel mZenPanel;
136 /** Dummy divider icon that needs to vanish with the zen panel */
137 private final View mZenPanelDivider;
138
139 private ZenModePanel.Callback mZenPanelCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800140
141 /** Currently active stream that shows up at the top of the list of sliders */
142 private int mActiveStreamType = -1;
143 /** All the slider controls mapped by stream type */
144 private HashMap<Integer,StreamControl> mStreamControls;
145
Amith Yamasani71def772011-10-12 12:25:24 -0700146 private enum StreamResources {
147 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
148 R.string.volume_icon_description_bluetooth,
149 R.drawable.ic_audio_bt,
150 R.drawable.ic_audio_bt,
151 false),
152 RingerStream(AudioManager.STREAM_RING,
153 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400154 com.android.systemui.R.drawable.ic_ringer_audible,
155 com.android.systemui.R.drawable.ic_ringer_silent,
Amith Yamasani71def772011-10-12 12:25:24 -0700156 false),
157 VoiceStream(AudioManager.STREAM_VOICE_CALL,
158 R.string.volume_icon_description_incall,
159 R.drawable.ic_audio_phone,
160 R.drawable.ic_audio_phone,
161 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700162 AlarmStream(AudioManager.STREAM_ALARM,
163 R.string.volume_alarm,
164 R.drawable.ic_audio_alarm,
165 R.drawable.ic_audio_alarm_mute,
166 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700167 MediaStream(AudioManager.STREAM_MUSIC,
168 R.string.volume_icon_description_media,
169 R.drawable.ic_audio_vol,
170 R.drawable.ic_audio_vol_mute,
171 true),
172 NotificationStream(AudioManager.STREAM_NOTIFICATION,
173 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400174 com.android.systemui.R.drawable.ic_ringer_audible,
175 com.android.systemui.R.drawable.ic_ringer_silent,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400176 true),
177 // for now, use media resources for master volume
178 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700179 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400180 R.drawable.ic_audio_vol,
181 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700182 false),
183 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
184 R.string.volume_icon_description_media, //FIXME should have its own description
185 R.drawable.ic_media_route_on_holo_dark,
186 R.drawable.ic_media_route_disabled_holo_dark,
187 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700188
189 int streamType;
190 int descRes;
191 int iconRes;
192 int iconMuteRes;
193 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
194 boolean show;
195
196 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
197 this.streamType = streamType;
198 this.descRes = descRes;
199 this.iconRes = iconRes;
200 this.iconMuteRes = iconMuteRes;
201 this.show = show;
202 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700203 }
Amith Yamasani71def772011-10-12 12:25:24 -0700204
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800205 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700206 private static final StreamResources[] STREAMS = {
207 StreamResources.BluetoothSCOStream,
208 StreamResources.RingerStream,
209 StreamResources.VoiceStream,
210 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700211 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400212 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700213 StreamResources.MasterStream,
214 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800215 };
216
217 /** Object that contains data for each slider */
218 private class StreamControl {
219 int streamType;
220 ViewGroup group;
221 ImageView icon;
222 SeekBar seekbarView;
223 int iconRes;
224 int iconMuteRes;
225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 // Synchronize when accessing this
228 private ToneGenerator mToneGenerators[];
229 private Vibrator mVibrator;
230
Eric Laurentc34dcc12012-09-10 13:51:52 -0700231 private static AlertDialog sConfirmSafeVolumeDialog;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700232 private static Object sConfirmSafeVolumeLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700233
234 private static class WarningDialogReceiver extends BroadcastReceiver
235 implements DialogInterface.OnDismissListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800236 private final Context mContext;
237 private final Dialog mDialog;
238 private final VolumePanel mVolumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700239
Eric Laurentfde16d52012-12-03 14:42:39 -0800240 WarningDialogReceiver(Context context, Dialog dialog, VolumePanel volumePanel) {
Eric Laurentc34dcc12012-09-10 13:51:52 -0700241 mContext = context;
242 mDialog = dialog;
Eric Laurentfde16d52012-12-03 14:42:39 -0800243 mVolumePanel = volumePanel;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700244 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
245 context.registerReceiver(this, filter);
246 }
247
248 @Override
249 public void onReceive(Context context, Intent intent) {
250 mDialog.cancel();
Eric Laurentfde16d52012-12-03 14:42:39 -0800251 cleanUp();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700252 }
253
Alan Viverette494fb7b2014-04-10 18:12:56 -0700254 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700255 public void onDismiss(DialogInterface unused) {
256 mContext.unregisterReceiver(this);
Eric Laurentfde16d52012-12-03 14:42:39 -0800257 cleanUp();
258 }
259
260 private void cleanUp() {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700261 synchronized (sConfirmSafeVolumeLock) {
262 sConfirmSafeVolumeDialog = null;
263 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800264 mVolumePanel.forceTimeout();
265 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700266 }
267 }
268
269
John Spurlock86005342014-05-23 11:58:00 -0400270 public VolumePanel(Context context, ViewGroup parent, ZenModeController zenController) {
271 mTag = String.format("VolumePanel%s.%08x", parent == null ? "Dialog" : "", hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400273 mParent = parent;
274 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400277 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700278 final Resources res = context.getResources();
279 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400280 if (useMasterVolume) {
281 for (int i = 0; i < STREAMS.length; i++) {
282 StreamResources streamRes = STREAMS[i];
283 streamRes.show = (streamRes.streamType == STREAM_MASTER);
284 }
285 }
John Spurlock86005342014-05-23 11:58:00 -0400286 if (LOGD) Log.d(mTag, String.format("new VolumePanel hasParent=%s", parent != null));
287 final int layoutId = com.android.systemui.R.layout.volume_panel;
288 if (parent == null) {
289 // dialog mode
290 mDialog = new Dialog(context) {
291 @Override
292 public boolean onTouchEvent(MotionEvent event) {
293 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
294 sConfirmSafeVolumeDialog == null) {
295 forceTimeout();
296 return true;
297 }
298 return false;
Amith Yamasani284e6302011-09-16 18:24:47 -0700299 }
John Spurlock86005342014-05-23 11:58:00 -0400300 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700301
John Spurlock86005342014-05-23 11:58:00 -0400302 // Change some window properties
303 final Window window = mDialog.getWindow();
304 final LayoutParams lp = window.getAttributes();
305 lp.token = null;
306 // Offset from the top
307 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
308 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.volume_panel_width);
309 lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
John Spurlock2078caf2014-05-29 22:20:14 -0400310 lp.format = PixelFormat.TRANSLUCENT;
John Spurlock86005342014-05-23 11:58:00 -0400311 lp.windowAnimations = R.style.Animation_VolumePanel;
John Spurlock86005342014-05-23 11:58:00 -0400312 window.setAttributes(lp);
313 window.setGravity(Gravity.TOP);
314 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
315 window.requestFeature(Window.FEATURE_NO_TITLE);
316 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
317 | LayoutParams.FLAG_NOT_TOUCH_MODAL
John Spurlock2078caf2014-05-29 22:20:14 -0400318 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
319 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlock86005342014-05-23 11:58:00 -0400320 mDialog.setCanceledOnTouchOutside(true);
John Spurlock2078caf2014-05-29 22:20:14 -0400321 // temporary workaround for no window shadows
322 final FrameLayout layout = new FrameLayout(mContext);
323 final int z = res.getDimensionPixelSize(com.android.systemui.R.dimen.volume_panel_z);
324 layout.setPadding(z, z, z, z);
325 final View v = LayoutInflater.from(mContext).inflate(layoutId, layout, false);
326 v.setBackgroundResource(com.android.systemui.R.drawable.qs_panel_background);
327 v.setElevation(z);
328 layout.addView(v);
329 mDialog.setContentView(layout);
John Spurlock86005342014-05-23 11:58:00 -0400330 mDialog.setOnDismissListener(new OnDismissListener() {
331 @Override
332 public void onDismiss(DialogInterface dialog) {
333 mActiveStreamType = -1;
334 mAudioManager.forceVolumeControlStream(mActiveStreamType);
335 }
336 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700337
John Spurlock86005342014-05-23 11:58:00 -0400338 mDialog.create();
John Spurlock2078caf2014-05-29 22:20:14 -0400339 mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
Alan Viverette494fb7b2014-04-10 18:12:56 -0700340
John Spurlock86005342014-05-23 11:58:00 -0400341 mView = window.findViewById(R.id.content);
342 mView.setOnTouchListener(new View.OnTouchListener() {
343 @Override
344 public boolean onTouch(View v, MotionEvent event) {
345 resetTimeout();
346 return false;
347 }
348 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700349
John Spurlock86005342014-05-23 11:58:00 -0400350 } else {
351 // embedded mode
352 mDialog = null;
353 mView = LayoutInflater.from(mContext).inflate(layoutId, parent, true);
354 }
355 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
356 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
357 mExpandButton = (ImageView) mView.findViewById(com.android.systemui.R.id.expand_button);
358 mExpandDivider = mView.findViewById(com.android.systemui.R.id.expand_button_divider);
359 mZenPanelStub = (ViewStub)mView.findViewById(com.android.systemui.R.id.zen_panel_stub);
360 mZenPanelDivider = mView.findViewById(com.android.systemui.R.id.zen_panel_divider);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700363 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700364 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700365
John Spurlock86005342014-05-23 11:58:00 -0400366 mZenModeCapable = !useMasterVolume && mZenController != null;
367 mZenPanelDivider.setVisibility(View.GONE);
368 mExpandButton.setOnClickListener(mClickListener);
369 updateZenMode(mZenController == null ? false : mZenController.isZen());
370 mZenController.addCallback(mZenCallback);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700371
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700372 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
373 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700374 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
375
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800376 listenToRingerMode();
377 }
378
John Spurlock86005342014-05-23 11:58:00 -0400379 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800380 mPanel.setLayoutDirection(layoutDirection);
381 updateStates();
382 }
383
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800384 private void listenToRingerMode() {
385 final IntentFilter filter = new IntentFilter();
386 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
387 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700388 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800389 public void onReceive(Context context, Intent intent) {
390 final String action = intent.getAction();
391
392 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
393 removeMessages(MSG_RINGER_MODE_CHANGED);
394 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
395 }
396 }
397 }, filter);
398 }
399
400 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400401 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700402 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700403 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
John Spurlock3346a802014-05-20 16:25:37 -0400404 return (mAudioManager.getRemoteStreamVolume() <= 0);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400405 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700406 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400407 }
408 }
409
410 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400411 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700412 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700413 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
John Spurlock3346a802014-05-20 16:25:37 -0400414 return mAudioManager.getRemoteStreamMaxVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400415 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700416 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400417 }
418 }
419
420 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400421 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700422 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700423 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
John Spurlock3346a802014-05-20 16:25:37 -0400424 return mAudioManager.getRemoteStreamVolume();
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400425 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700426 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400427 }
428 }
429
430 private void setStreamVolume(int streamType, int index, int flags) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400431 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700432 mAudioManager.setMasterVolume(index, flags);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700433 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
John Spurlock3346a802014-05-20 16:25:37 -0400434 mAudioManager.setRemoteStreamVolume(index);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400435 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700436 mAudioManager.setStreamVolume(streamType, index, flags);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400437 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800438 }
439
440 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700441 final Resources res = mContext.getResources();
442 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
443 Context.LAYOUT_INFLATER_SERVICE);
444
Amith Yamasani71def772011-10-12 12:25:24 -0700445 mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700446
Amith Yamasani71def772011-10-12 12:25:24 -0700447 for (int i = 0; i < STREAMS.length; i++) {
448 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700449
450 final int streamType = streamRes.streamType;
Alan Viverette494fb7b2014-04-10 18:12:56 -0700451
452 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700453 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400454 sc.group = (ViewGroup) inflater.inflate(
455 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800456 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400457 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800458 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700459 sc.icon.setContentDescription(res.getString(streamRes.descRes));
460 sc.iconRes = streamRes.iconRes;
461 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800462 sc.icon.setImageResource(sc.iconRes);
John Spurlock86005342014-05-23 11:58:00 -0400463 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700464 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700465 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400466 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700467 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800468 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700469 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800470 }
471 }
472
473 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400474 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800475
Alan Viverette494fb7b2014-04-10 18:12:56 -0700476 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800477 if (active == null) {
478 Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
479 mActiveStreamType = -1;
480 } else {
John Spurlock86005342014-05-23 11:58:00 -0400481 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800482 mActiveStreamType = activeStreamType;
483 active.group.setVisibility(View.VISIBLE);
484 updateSlider(active);
John Spurlock86005342014-05-23 11:58:00 -0400485 updateZenMode(mZenController == null ? false : mZenController.isZen());
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800486 }
487 }
488
489 /** Update the mute and progress state of a slider */
490 private void updateSlider(StreamControl sc) {
Eric Laurent8c787522012-05-14 14:09:43 -0700491 sc.seekbarView.setProgress(getStreamVolume(sc.streamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800492 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800493 // Force reloading the image resource
494 sc.icon.setImageDrawable(null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800495 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
Eric Laurent8a51aca2013-03-25 18:36:35 -0700496 if (((sc.streamType == AudioManager.STREAM_RING) ||
497 (sc.streamType == AudioManager.STREAM_NOTIFICATION)) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700498 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
John Spurlock86005342014-05-23 11:58:00 -0400499 sc.icon.setImageResource(com.android.systemui.R.drawable.ic_ringer_vibrate);
Amith Yamasanic696a532011-10-28 17:02:37 -0700500 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700501 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
502 // never disable touch interactions for remote playback, the muting is not tied to
503 // the state of the phone.
504 sc.seekbarView.setEnabled(true);
Eric Laurentfde16d52012-12-03 14:42:39 -0800505 } else if ((sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
506 (sConfirmSafeVolumeDialog != null)) {
Eric Laurent8c787522012-05-14 14:09:43 -0700507 sc.seekbarView.setEnabled(false);
508 } else {
509 sc.seekbarView.setEnabled(true);
510 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800511 }
512
John Spurlock86005342014-05-23 11:58:00 -0400513 public void setZenModePanelCallback(ZenModePanel.Callback callback) {
514 mZenPanelCallback = callback;
515 }
516
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800517 private void expand() {
John Spurlock86005342014-05-23 11:58:00 -0400518 if (LOGD) Log.d(mTag, "expand mZenPanel=" + mZenPanel);
519 if (mZenPanel == null) {
520 mZenPanel = (ZenModePanel) mZenPanelStub.inflate();
521 mZenPanel.init(mZenController);
522 mZenPanel.setCallback(new ZenModePanel.Callback() {
523 @Override
524 public void onMoreSettings() {
525 if (mZenPanelCallback != null) {
526 mZenPanelCallback.onMoreSettings();
527 }
528 }
529
530 @Override
531 public void onInteraction() {
John Spurlock3bd4fee2014-05-29 20:51:09 -0400532 resetTimeout();
John Spurlock86005342014-05-23 11:58:00 -0400533 if (mZenPanelCallback != null) {
534 mZenPanelCallback.onInteraction();
535 }
536 }
537 });
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800538 }
John Spurlock86005342014-05-23 11:58:00 -0400539 mZenPanel.setVisibility(View.VISIBLE);
540 mZenPanelDivider.setVisibility(View.VISIBLE);
John Spurlock3bd4fee2014-05-29 20:51:09 -0400541 mTimeoutDelay = TIMEOUT_DELAY_EXPANDED;
542 resetTimeout();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800543 }
544
545 private void collapse() {
John Spurlock86005342014-05-23 11:58:00 -0400546 if (LOGD) Log.d(mTag, "collapse mZenPanel=" + mZenPanel);
547 if (mZenPanel != null) {
548 mZenPanel.setVisibility(View.GONE);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800549 }
John Spurlock86005342014-05-23 11:58:00 -0400550 mZenPanelDivider.setVisibility(View.GONE);
John Spurlock3bd4fee2014-05-29 20:51:09 -0400551 mTimeoutDelay = TIMEOUT_DELAY;
552 resetTimeout();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800553 }
554
Eric Laurentfde16d52012-12-03 14:42:39 -0800555 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400556 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800557 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400558 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800559 updateSlider(sc);
560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
562
John Spurlock86005342014-05-23 11:58:00 -0400563 private void updateZenMode(boolean zen) {
564 if (mZenModeCapable) {
565 final boolean show = mActiveStreamType == AudioManager.STREAM_NOTIFICATION
566 || mActiveStreamType == AudioManager.STREAM_RING;
567 mExpandButton.setVisibility(show ? View.VISIBLE : View.GONE);
568 mExpandDivider.setVisibility(show ? View.VISIBLE : View.GONE);
569 mExpandButton.setImageResource(zen ? com.android.systemui.R.drawable.ic_vol_zen_on
570 : com.android.systemui.R.drawable.ic_vol_zen_off);
571 } else {
572 mExpandButton.setVisibility(View.GONE);
573 mExpandDivider.setVisibility(View.GONE);
574 }
575 }
576
577 public void postZenModeChanged(boolean zen) {
578 removeMessages(MSG_ZEN_MODE_CHANGED);
579 obtainMessage(MSG_ZEN_MODE_CHANGED, zen ? 1 : 0).sendToTarget();
580 }
581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 public void postVolumeChanged(int streamType, int flags) {
583 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700584 synchronized (this) {
585 if (mStreamControls == null) {
586 createSliders();
587 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 removeMessages(MSG_FREE_RESOURCES);
590 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
591 }
592
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700593 public void postRemoteVolumeChanged(int streamType, int flags) {
594 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
595 synchronized (this) {
596 if (mStreamControls == null) {
597 createSliders();
598 }
599 }
600 removeMessages(MSG_FREE_RESOURCES);
601 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget();
602 }
603
604 public void postRemoteSliderVisibility(boolean visible) {
605 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
606 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
607 }
608
609 /**
610 * Called by AudioService when it has received new remote playback information that
611 * would affect the VolumePanel display (mainly volumes). The difference with
612 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
613 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
614 * displayed.
615 * This special code path is due to the fact that remote volume updates arrive to AudioService
616 * asynchronously. So after AudioService has sent the volume update (which should be treated
617 * as a request to update the volume), the application will likely set a new volume. If the UI
618 * is still up, we need to refresh the display to show this new value.
619 */
620 public void postHasNewRemotePlaybackInfo() {
621 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
622 // don't create or prevent resources to be freed, if they disappear, this update came too
623 // late and shouldn't warrant the panel to be displayed longer
624 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
625 }
626
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400627 public void postMasterVolumeChanged(int flags) {
628 postVolumeChanged(STREAM_MASTER, flags);
629 }
630
Mike Lockwoodce952c82011-11-14 10:47:42 -0800631 public void postMuteChanged(int streamType, int flags) {
632 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700633 synchronized (this) {
634 if (mStreamControls == null) {
635 createSliders();
636 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800637 }
638 removeMessages(MSG_FREE_RESOURCES);
639 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
640 }
641
642 public void postMasterMuteChanged(int flags) {
643 postMuteChanged(STREAM_MASTER, flags);
644 }
645
Eric Laurentfde16d52012-12-03 14:42:39 -0800646 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700647 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800648 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700649 }
650
John Spurlock3346a802014-05-20 16:25:37 -0400651 public void postDismiss() {
John Spurlock86005342014-05-23 11:58:00 -0400652 forceTimeout();
653 }
654
655 public void postLayoutDirection(int layoutDirection) {
656 removeMessages(MSG_LAYOUT_DIRECTION);
657 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 /**
661 * Override this if you have other work to do when the volume changes (for
662 * example, vibrating, playing a sound, etc.). Make sure to call through to
663 * the superclass implementation.
664 */
665 protected void onVolumeChanged(int streamType, int flags) {
666
John Spurlock86005342014-05-23 11:58:00 -0400667 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668
669 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700670 synchronized (this) {
671 if (mActiveStreamType != streamType) {
672 reorderSliders(streamType);
673 }
674 onShowVolumeChanged(streamType, flags);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677
Marco Nelissen69f593c2009-07-28 09:55:04 -0700678 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 removeMessages(MSG_PLAY_SOUND);
680 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
681 }
682
683 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
684 removeMessages(MSG_PLAY_SOUND);
685 removeMessages(MSG_VIBRATE);
686 onStopSounds();
687 }
688
689 removeMessages(MSG_FREE_RESOURCES);
690 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800691 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693
Mike Lockwoodce952c82011-11-14 10:47:42 -0800694 protected void onMuteChanged(int streamType, int flags) {
695
John Spurlock86005342014-05-23 11:58:00 -0400696 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800697
698 StreamControl sc = mStreamControls.get(streamType);
699 if (sc != null) {
700 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
701 }
702
703 onVolumeChanged(streamType, flags);
704 }
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 protected void onShowVolumeChanged(int streamType, int flags) {
Eric Laurent8c787522012-05-14 14:09:43 -0700707 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800708
Marco Nelissen69f593c2009-07-28 09:55:04 -0700709 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400712 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 + ", flags: " + flags + "), index: " + index);
714 }
715
716 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800717
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400718 int max = getStreamMaxVolume(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
720 switch (streamType) {
721
722 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800723// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700724 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
725 mContext, RingtoneManager.TYPE_RINGTONE);
726 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700727 mRingIsSilent = true;
728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 break;
730 }
731
732 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800733 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800734 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
735 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
736 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
737 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800738 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800740 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 break;
743 }
744
745 case AudioManager.STREAM_VOICE_CALL: {
746 /*
747 * For in-call voice call volume, there is no inaudible volume.
748 * Rescale the UI control so the progress bar doesn't go all
749 * the way to zero and don't show the mute icon.
750 */
751 index++;
752 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 break;
754 }
755
756 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 break;
758 }
759
760 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700761 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
762 mContext, RingtoneManager.TYPE_NOTIFICATION);
763 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700764 mRingIsSilent = true;
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 break;
767 }
768
769 case AudioManager.STREAM_BLUETOOTH_SCO: {
770 /*
771 * For in-call voice call volume, there is no inaudible volume.
772 * Rescale the UI control so the progress bar doesn't go all
773 * the way to zero and don't show the mute icon.
774 */
775 index++;
776 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 break;
778 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700779
780 case AudioService.STREAM_REMOTE_MUSIC: {
John Spurlock86005342014-05-23 11:58:00 -0400781 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700782 break;
783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800786 StreamControl sc = mStreamControls.get(streamType);
787 if (sc != null) {
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700788 if (sc.seekbarView.getMax() != max) {
789 sc.seekbarView.setMax(max);
790 }
Eric Laurent4bbcc652012-09-24 14:26:30 -0700791
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800792 sc.seekbarView.setProgress(index);
Eric Laurent4bbcc652012-09-24 14:26:30 -0700793 if (((flags & AudioManager.FLAG_FIXED_VOLUME) != 0) ||
794 (streamType != mAudioManager.getMasterStreamType() &&
795 streamType != AudioService.STREAM_REMOTE_MUSIC &&
Eric Laurentfde16d52012-12-03 14:42:39 -0800796 isMuted(streamType)) ||
797 sConfirmSafeVolumeDialog != null) {
Eric Laurent8c787522012-05-14 14:09:43 -0700798 sc.seekbarView.setEnabled(false);
799 } else {
800 sc.seekbarView.setEnabled(true);
801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803
John Spurlock86005342014-05-23 11:58:00 -0400804 if (!isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700805 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
806 // when the stream is for remote playback, use -1 to reset the stream type evaluation
807 mAudioManager.forceVolumeControlStream(stream);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700808
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800809 // Showing dialog - use collapsed state
John Spurlock86005342014-05-23 11:58:00 -0400810 if (mZenModeCapable) {
Amith Yamasani42722bf2011-07-22 10:34:27 -0700811 collapse();
812 }
John Spurlock86005342014-05-23 11:58:00 -0400813 if (mDialog != null) {
814 mDialog.show();
815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700819 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
820 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock3346a802014-05-20 16:25:37 -0400821 mAudioManager.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -0700822 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
John Spurlock86005342014-05-23 11:58:00 -0400827 private boolean isShowing() {
828 return mDialog != null ? mDialog.isShowing() : mParent.isAttachedToWindow();
829 }
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 protected void onPlaySound(int streamType, int flags) {
832
833 if (hasMessages(MSG_STOP_SOUNDS)) {
834 removeMessages(MSG_STOP_SOUNDS);
835 // Force stop right now
836 onStopSounds();
837 }
838
839 synchronized (this) {
840 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -0800841 if (toneGen != null) {
842 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
843 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
847
848 protected void onStopSounds() {
849
850 synchronized (this) {
851 int numStreamTypes = AudioSystem.getNumStreamTypes();
852 for (int i = numStreamTypes - 1; i >= 0; i--) {
853 ToneGenerator toneGen = mToneGenerators[i];
854 if (toneGen != null) {
855 toneGen.stopTone();
856 }
857 }
858 }
859 }
860
861 protected void onVibrate() {
862
863 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -0700864 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 return;
866 }
867
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400868 mVibrator.vibrate(VIBRATE_DURATION, AudioManager.STREAM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700871 protected void onRemoteVolumeChanged(int streamType, int flags) {
872 // streamType is the real stream type being affected, but for the UI sliders, we
873 // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real
874 // stream type.
John Spurlock86005342014-05-23 11:58:00 -0400875 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700876
John Spurlock86005342014-05-23 11:58:00 -0400877 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700878 synchronized (this) {
879 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
880 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
881 }
882 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags);
883 }
884 } else {
John Spurlock86005342014-05-23 11:58:00 -0400885 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700886 }
887
888 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
889 removeMessages(MSG_PLAY_SOUND);
890 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
891 }
892
893 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
894 removeMessages(MSG_PLAY_SOUND);
895 removeMessages(MSG_VIBRATE);
896 onStopSounds();
897 }
898
899 removeMessages(MSG_FREE_RESOURCES);
900 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700901 resetTimeout();
902 }
903
904 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -0400905 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
906 if (isShowing()
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700907 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
908 && (mStreamControls != null)) {
909 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0);
910 }
911 }
912
913
914 /**
915 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
916 * Hide or show a slider
917 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
918 * or AudioService.STREAM_REMOTE_MUSIC
919 * @param visible
920 */
921 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -0400922 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700923 boolean isVisible = (visible == 1);
924 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
925 StreamResources streamRes = STREAMS[i];
926 if (streamRes.streamType == streamType) {
927 streamRes.show = isVisible;
928 if (!isVisible && (mActiveStreamType == streamType)) {
929 mActiveStreamType = -1;
930 }
931 break;
932 }
933 }
934 }
935
Eric Laurentfde16d52012-12-03 14:42:39 -0800936 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock86005342014-05-23 11:58:00 -0400937 if ((flags & AudioManager.FLAG_SHOW_UI) != 0 || isShowing()) {
Eric Laurentfde16d52012-12-03 14:42:39 -0800938 synchronized (sConfirmSafeVolumeLock) {
939 if (sConfirmSafeVolumeDialog != null) {
940 return;
941 }
942 sConfirmSafeVolumeDialog = new AlertDialog.Builder(mContext)
943 .setMessage(com.android.internal.R.string.safe_media_volume_warning)
944 .setPositiveButton(com.android.internal.R.string.yes,
945 new DialogInterface.OnClickListener() {
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700946 @Override
Eric Laurentfde16d52012-12-03 14:42:39 -0800947 public void onClick(DialogInterface dialog, int which) {
John Spurlock3346a802014-05-20 16:25:37 -0400948 mAudioManager.disableSafeMediaVolume();
Eric Laurentfde16d52012-12-03 14:42:39 -0800949 }
950 })
951 .setNegativeButton(com.android.internal.R.string.no, null)
952 .setIconAttribute(android.R.attr.alertDialogIcon)
953 .create();
954 final WarningDialogReceiver warning = new WarningDialogReceiver(mContext,
955 sConfirmSafeVolumeDialog, this);
Eric Laurent0516a9e2012-09-19 11:53:03 -0700956
Eric Laurentfde16d52012-12-03 14:42:39 -0800957 sConfirmSafeVolumeDialog.setOnDismissListener(warning);
958 sConfirmSafeVolumeDialog.getWindow().setType(
959 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
960 sConfirmSafeVolumeDialog.show();
961 }
962 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700963 }
Eric Laurentfde16d52012-12-03 14:42:39 -0800964 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700965 }
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 /**
968 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
969 */
970 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -0700971 if (streamType == STREAM_MASTER) {
972 // For devices that use the master volume setting only but still want to
973 // play a volume-changed tone, direct the master volume pseudostream to
974 // the system stream's tone generator.
975 if (mPlayMasterStreamTones) {
976 streamType = AudioManager.STREAM_SYSTEM;
977 } else {
978 return null;
979 }
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 synchronized (this) {
982 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -0800983 try {
984 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
985 } catch (RuntimeException e) {
986 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400987 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -0800988 + "RuntimeException: " + e);
989 }
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
Eric Laurent733a42b2011-01-19 10:41:57 -0800992 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 }
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
997 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800998 * Switch between icons because Bluetooth music is same as music volume, but with
999 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001001 private void setMusicIcon(int resId, int resMuteId) {
1002 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1003 if (sc != null) {
1004 sc.iconRes = resId;
1005 sc.iconMuteRes = resMuteId;
1006 sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
1010 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 synchronized (this) {
1012 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1013 if (mToneGenerators[i] != null) {
1014 mToneGenerators[i].release();
1015 }
1016 mToneGenerators[i] = null;
1017 }
1018 }
1019 }
1020
1021 @Override
1022 public void handleMessage(Message msg) {
1023 switch (msg.what) {
1024
1025 case MSG_VOLUME_CHANGED: {
1026 onVolumeChanged(msg.arg1, msg.arg2);
1027 break;
1028 }
1029
Mike Lockwoodce952c82011-11-14 10:47:42 -08001030 case MSG_MUTE_CHANGED: {
1031 onMuteChanged(msg.arg1, msg.arg2);
1032 break;
1033 }
1034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 case MSG_FREE_RESOURCES: {
1036 onFreeResources();
1037 break;
1038 }
1039
1040 case MSG_STOP_SOUNDS: {
1041 onStopSounds();
1042 break;
1043 }
1044
1045 case MSG_PLAY_SOUND: {
1046 onPlaySound(msg.arg1, msg.arg2);
1047 break;
1048 }
1049
1050 case MSG_VIBRATE: {
1051 onVibrate();
1052 break;
1053 }
1054
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001055 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001056 if (isShowing()) {
1057 if (mDialog != null) {
1058 mDialog.dismiss();
John Spurlockf71205c2014-05-29 10:17:51 -04001059 mActiveStreamType = -1;
John Spurlock86005342014-05-23 11:58:00 -04001060 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001061 }
Eric Laurentfde16d52012-12-03 14:42:39 -08001062 synchronized (sConfirmSafeVolumeLock) {
1063 if (sConfirmSafeVolumeDialog != null) {
1064 sConfirmSafeVolumeDialog.dismiss();
1065 }
1066 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001067 break;
1068 }
1069 case MSG_RINGER_MODE_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001070 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001071 updateStates();
1072 }
1073 break;
1074 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001075
1076 case MSG_REMOTE_VOLUME_CHANGED: {
1077 onRemoteVolumeChanged(msg.arg1, msg.arg2);
1078 break;
1079 }
1080
1081 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1082 onRemoteVolumeUpdateIfShown();
1083 break;
1084
1085 case MSG_SLIDER_VISIBILITY_CHANGED:
1086 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1087 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001088
1089 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001090 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001091 break;
John Spurlock86005342014-05-23 11:58:00 -04001092
1093 case MSG_LAYOUT_DIRECTION:
1094 setLayoutDirection(msg.arg1);
1095 break;
1096
1097 case MSG_ZEN_MODE_CHANGED:
1098 updateZenMode(msg.arg1 != 0);
1099 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101 }
1102
John Spurlock86005342014-05-23 11:58:00 -04001103 public void resetTimeout() {
1104 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis());
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001105 removeMessages(MSG_TIMEOUT);
John Spurlock3bd4fee2014-05-29 20:51:09 -04001106 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001107 }
1108
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001109 private void forceTimeout() {
1110 removeMessages(MSG_TIMEOUT);
John Spurlock86005342014-05-23 11:58:00 -04001111 sendEmptyMessage(MSG_TIMEOUT);
1112 }
1113
1114 public ZenModeController getZenController() {
1115 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001116 }
1117
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001118 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1119 @Override
1120 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1121 final Object tag = seekBar.getTag();
1122 if (fromUser && tag instanceof StreamControl) {
1123 StreamControl sc = (StreamControl) tag;
1124 if (getStreamVolume(sc.streamType) != progress) {
1125 setStreamVolume(sc.streamType, progress, 0);
1126 }
1127 }
1128 resetTimeout();
1129 }
1130
1131 @Override
1132 public void onStartTrackingTouch(SeekBar seekBar) {
1133 }
1134
1135 @Override
1136 public void onStopTrackingTouch(SeekBar seekBar) {
1137 final Object tag = seekBar.getTag();
1138 if (tag instanceof StreamControl) {
1139 StreamControl sc = (StreamControl) tag;
1140 // Because remote volume updates are asynchronous, AudioService
1141 // might have received a new remote volume value since the
1142 // finger adjusted the slider. So when the progress of the
1143 // slider isn't being tracked anymore, adjust the slider to the
1144 // last "published" remote volume value, so the UI reflects the
1145 // actual volume.
1146 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
1147 seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC));
1148 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001149 }
1150 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001151 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001152
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001153 private final View.OnClickListener mClickListener = new View.OnClickListener() {
1154 @Override
1155 public void onClick(View v) {
John Spurlock86005342014-05-23 11:58:00 -04001156 if (v == mExpandButton && mZenController != null) {
1157 final boolean newZen = !mZenController.isZen();
John Spurlock3bd4fee2014-05-29 20:51:09 -04001158 AsyncTask.execute(new Runnable() {
1159 @Override
1160 public void run() {
1161 mZenController.setZen(newZen);
1162 }
1163 });
John Spurlock86005342014-05-23 11:58:00 -04001164 if (newZen) {
1165 expand();
1166 } else {
1167 collapse();
1168 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001169 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001170 resetTimeout();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001171 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001172 };
John Spurlock86005342014-05-23 11:58:00 -04001173
1174 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
1175 public void onZenChanged(boolean zen) {
1176 updateZenMode(zen);
1177 }
1178 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179}