blob: d202036b2e8d9abdc2473a4bff6368e9ae63bda9 [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 Spurlockad494bc2014-07-19 15:56:19 -040028import android.content.res.TypedArray;
John Spurlock2078caf2014-05-29 22:20:14 -040029import android.graphics.PixelFormat;
30import android.graphics.drawable.ColorDrawable;
John Spurlock7b414672014-07-18 13:02:39 -040031import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.media.AudioManager;
33import android.media.AudioService;
34import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070035import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070037import android.media.VolumeProvider;
38import android.media.session.MediaController;
39import android.media.session.MediaController.VolumeInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070040import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Handler;
42import android.os.Message;
43import android.os.Vibrator;
John Spurlockae641c92014-06-30 18:11:40 -040044import android.provider.Settings.Global;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040046import android.util.SparseArray;
John Spurlock3346a802014-05-20 16:25:37 -040047import android.view.Gravity;
John Spurlock2d28d6e2014-08-01 13:10:14 -040048import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040049import android.view.LayoutInflater;
50import android.view.MotionEvent;
51import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040052import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040053import android.view.ViewGroup;
54import android.view.Window;
55import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070056import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080058import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080059import android.widget.SeekBar.OnSeekBarChangeListener;
60
John Spurlock86005342014-05-23 11:58:00 -040061import com.android.internal.R;
John Spurlock35134602014-07-24 18:10:48 -040062import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040063import com.android.systemui.statusbar.policy.ZenModeController;
64
John Spurlockad494bc2014-07-19 15:56:19 -040065import java.io.FileDescriptor;
66import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68/**
John Spurlock3346a802014-05-20 16:25:37 -040069 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070070 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * @hide
72 */
John Spurlock3346a802014-05-20 16:25:37 -040073public class VolumePanel extends Handler {
John Spurlockae641c92014-06-30 18:11:40 -040074 private static final String TAG = "VolumePanel";
75 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
John Spurlock3346a802014-05-20 16:25:37 -040077 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79 /**
80 * The delay before vibrating. This small period exists so if the user is
81 * moving to silent mode, it will not emit a short vibrate (it normally
82 * would since vibrate is between normal mode and silent mode using hardware
83 * keys).
84 */
85 public static final int VIBRATE_DELAY = 300;
86
87 private static final int VIBRATE_DURATION = 300;
88 private static final int BEEP_DURATION = 150;
89 private static final int MAX_VOLUME = 100;
90 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080091 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -040092 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -040093 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -040094 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
John Spurlock3bd4fee2014-05-29 20:51:09 -040095 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 private static final int MSG_VOLUME_CHANGED = 0;
98 private static final int MSG_FREE_RESOURCES = 1;
99 private static final int MSG_PLAY_SOUND = 2;
100 private static final int MSG_STOP_SOUNDS = 3;
101 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800102 private static final int MSG_TIMEOUT = 5;
103 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -0800104 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700105 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
106 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
107 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700108 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -0400109 private static final int MSG_LAYOUT_DIRECTION = 12;
110 private static final int MSG_ZEN_MODE_CHANGED = 13;
John Spurlockae641c92014-06-30 18:11:40 -0400111 private static final int MSG_USER_ACTIVITY = 14;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400113 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800114 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700115 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400116
John Spurlock7b414672014-07-18 13:02:39 -0400117 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
118 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
119 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
120 .build();
121
John Spurlock86005342014-05-23 11:58:00 -0400122 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400123 protected final Context mContext;
124 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400125 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700126 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700127 private boolean mVoiceCapable;
John Spurlock86005342014-05-23 11:58:00 -0400128 private boolean mZenModeCapable;
John Spurlock8845da72014-07-07 21:29:48 -0400129 private boolean mZenPanelExpanded;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400130 private int mTimeoutDelay = TIMEOUT_DELAY;
John Spurlockad494bc2014-07-19 15:56:19 -0400131 private float mDisabledAlpha;
132 private int mLastRingerMode = AudioManager.RINGER_MODE_NORMAL;
133 private int mLastRingerProgress = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Christopher Tatec4b78d22012-05-22 13:57:58 -0700135 // True if we want to play tones on the system stream when the master stream is specified.
136 private final boolean mPlayMasterStreamTones;
137
John Spurlock86005342014-05-23 11:58:00 -0400138
139 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 private final View mView;
John Spurlockeb2727b2014-07-19 23:11:36 -0400141 /** Dialog hosting the panel */
John Spurlock86005342014-05-23 11:58:00 -0400142 private final Dialog mDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800143
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700144 /** The visible portion of the volume overlay */
145 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400146 /** Contains the slider and its touchable icons */
147 private final ViewGroup mSliderPanel;
John Spurlockeb2727b2014-07-19 23:11:36 -0400148 /** The zen mode configuration panel view */
John Spurlock86005342014-05-23 11:58:00 -0400149 private ZenModePanel mZenPanel;
John Spurlock86005342014-05-23 11:58:00 -0400150
John Spurlockae641c92014-06-30 18:11:40 -0400151 private Callback mCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800152
153 /** Currently active stream that shows up at the top of the list of sliders */
154 private int mActiveStreamType = -1;
155 /** All the slider controls mapped by stream type */
John Spurlockad494bc2014-07-19 15:56:19 -0400156 private SparseArray<StreamControl> mStreamControls;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800157
Amith Yamasani71def772011-10-12 12:25:24 -0700158 private enum StreamResources {
159 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
160 R.string.volume_icon_description_bluetooth,
161 R.drawable.ic_audio_bt,
162 R.drawable.ic_audio_bt,
163 false),
164 RingerStream(AudioManager.STREAM_RING,
165 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400166 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400167 com.android.systemui.R.drawable.ic_ringer_vibrate,
Amith Yamasani71def772011-10-12 12:25:24 -0700168 false),
169 VoiceStream(AudioManager.STREAM_VOICE_CALL,
170 R.string.volume_icon_description_incall,
171 R.drawable.ic_audio_phone,
172 R.drawable.ic_audio_phone,
173 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700174 AlarmStream(AudioManager.STREAM_ALARM,
175 R.string.volume_alarm,
176 R.drawable.ic_audio_alarm,
177 R.drawable.ic_audio_alarm_mute,
178 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700179 MediaStream(AudioManager.STREAM_MUSIC,
180 R.string.volume_icon_description_media,
181 R.drawable.ic_audio_vol,
182 R.drawable.ic_audio_vol_mute,
183 true),
184 NotificationStream(AudioManager.STREAM_NOTIFICATION,
185 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400186 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400187 com.android.systemui.R.drawable.ic_ringer_vibrate,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400188 true),
189 // for now, use media resources for master volume
190 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700191 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400192 R.drawable.ic_audio_vol,
193 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700194 false),
195 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
196 R.string.volume_icon_description_media, //FIXME should have its own description
197 R.drawable.ic_media_route_on_holo_dark,
198 R.drawable.ic_media_route_disabled_holo_dark,
199 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700200
201 int streamType;
202 int descRes;
203 int iconRes;
204 int iconMuteRes;
205 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
206 boolean show;
207
208 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
209 this.streamType = streamType;
210 this.descRes = descRes;
211 this.iconRes = iconRes;
212 this.iconMuteRes = iconMuteRes;
213 this.show = show;
214 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700215 }
Amith Yamasani71def772011-10-12 12:25:24 -0700216
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800217 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700218 private static final StreamResources[] STREAMS = {
219 StreamResources.BluetoothSCOStream,
220 StreamResources.RingerStream,
221 StreamResources.VoiceStream,
222 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700223 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400224 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700225 StreamResources.MasterStream,
226 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800227 };
228
229 /** Object that contains data for each slider */
230 private class StreamControl {
231 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700232 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800233 ViewGroup group;
234 ImageView icon;
235 SeekBar seekbarView;
236 int iconRes;
237 int iconMuteRes;
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 // Synchronize when accessing this
241 private ToneGenerator mToneGenerators[];
242 private Vibrator mVibrator;
243
John Spurlock35134602014-07-24 18:10:48 -0400244 private static AlertDialog sSafetyWarning;
245 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700246
John Spurlock35134602014-07-24 18:10:48 -0400247 private static class SafetyWarning extends SystemUIDialog
248 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800249 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800250 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400251 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700252
John Spurlock2d28d6e2014-08-01 13:10:14 -0400253 private boolean mNewVolumeUp;
254
John Spurlock35134602014-07-24 18:10:48 -0400255 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
256 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700257 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800258 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400259 mAudioManager = audioManager;
260
261 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
262 setButton(DialogInterface.BUTTON_POSITIVE,
263 mContext.getString(com.android.internal.R.string.yes), this);
264 setButton(DialogInterface.BUTTON_NEGATIVE,
265 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
266 setOnDismissListener(this);
267
Eric Laurentc34dcc12012-09-10 13:51:52 -0700268 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400269 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700270 }
271
272 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400273 public boolean onKeyDown(int keyCode, KeyEvent event) {
274 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
275 mNewVolumeUp = true;
276 }
277 return super.onKeyDown(keyCode, event);
278 }
279
280 @Override
281 public boolean onKeyUp(int keyCode, KeyEvent event) {
282 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
283 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
284 mAudioManager.disableSafeMediaVolume();
285 dismiss();
286 }
287 return super.onKeyUp(keyCode, event);
288 }
289
290 @Override
John Spurlock35134602014-07-24 18:10:48 -0400291 public void onClick(DialogInterface dialog, int which) {
292 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700293 }
294
Alan Viverette494fb7b2014-04-10 18:12:56 -0700295 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700296 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400297 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800298 cleanUp();
299 }
300
301 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400302 synchronized (sSafetyWarningLock) {
303 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700304 }
John Spurlock1dad2722014-07-11 11:07:53 -0400305 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800306 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700307 }
John Spurlock35134602014-07-24 18:10:48 -0400308
309 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
313 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
314 cancel();
315 cleanUp();
316 }
317 }
318 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700319 }
320
John Spurlockeb2727b2014-07-19 23:11:36 -0400321 public VolumePanel(Context context, ZenModeController zenController) {
322 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400324 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400327 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700328 final Resources res = context.getResources();
329 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400330 if (useMasterVolume) {
331 for (int i = 0; i < STREAMS.length; i++) {
332 StreamResources streamRes = STREAMS[i];
333 streamRes.show = (streamRes.streamType == STREAM_MASTER);
334 }
335 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400336 if (LOGD) Log.d(mTag, "new VolumePanel");
337
John Spurlockad494bc2014-07-19 15:56:19 -0400338 mDisabledAlpha = 0.5f;
339 if (mContext.getTheme() != null) {
340 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
341 new int[] { android.R.attr.disabledAlpha });
342 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
343 arr.recycle();
344 }
345
John Spurlockeb2727b2014-07-19 23:11:36 -0400346 mDialog = new Dialog(context) {
347 @Override
348 public boolean onTouchEvent(MotionEvent event) {
349 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400350 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400351 forceTimeout(0);
352 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700353 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400354 return false;
355 }
356 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700357
John Spurlockeb2727b2014-07-19 23:11:36 -0400358 // Change some window properties
359 final Window window = mDialog.getWindow();
360 final LayoutParams lp = window.getAttributes();
361 lp.token = null;
362 // Offset from the top
363 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
364 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
365 lp.format = PixelFormat.TRANSLUCENT;
366 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
367 lp.gravity = Gravity.TOP;
368 window.setAttributes(lp);
369 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
370 window.requestFeature(Window.FEATURE_NO_TITLE);
371 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
372 | LayoutParams.FLAG_NOT_TOUCH_MODAL
373 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
374 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
375 mDialog.setCanceledOnTouchOutside(true);
376 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
377 mDialog.setOnDismissListener(new OnDismissListener() {
378 @Override
379 public void onDismiss(DialogInterface dialog) {
380 mActiveStreamType = -1;
381 mAudioManager.forceVolumeControlStream(mActiveStreamType);
382 setZenPanelVisible(false);
383 }
384 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700385
John Spurlockeb2727b2014-07-19 23:11:36 -0400386 mDialog.create();
387 // temporary workaround, until we support window-level shadows
388 mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
Alan Viverette494fb7b2014-04-10 18:12:56 -0700389
John Spurlockeb2727b2014-07-19 23:11:36 -0400390 mView = window.findViewById(R.id.content);
391 mView.setOnTouchListener(new View.OnTouchListener() {
392 @Override
393 public boolean onTouch(View v, MotionEvent event) {
394 resetTimeout();
395 return false;
396 }
397 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700398
John Spurlock86005342014-05-23 11:58:00 -0400399 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
400 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400401 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
402 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700405 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700406 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700407
John Spurlock86005342014-05-23 11:58:00 -0400408 mZenModeCapable = !useMasterVolume && mZenController != null;
John Spurlockae641c92014-06-30 18:11:40 -0400409 updateZenMode(mZenController != null ? mZenController.getZen() : Global.ZEN_MODE_OFF);
John Spurlock86005342014-05-23 11:58:00 -0400410 mZenController.addCallback(mZenCallback);
Amith Yamasani42722bf2011-07-22 10:34:27 -0700411
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700412 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
413 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700414 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
415
John Spurlock8c79d2e2014-07-24 15:15:25 -0400416 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800417 }
418
John Spurlockad494bc2014-07-19 15:56:19 -0400419 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
420 pw.println("VolumePanel state:");
421 pw.print(" mTag="); pw.println(mTag);
422 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
423 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
424 pw.print(" mZenModeCapable="); pw.println(mZenModeCapable);
425 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
426 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
427 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
428 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
429 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
430 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
431 pw.print(" isShowing()="); pw.println(isShowing());
432 pw.print(" mCallback="); pw.println(mCallback);
433 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400434 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400435 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
436 pw.print(" mStreamControls=");
437 if (mStreamControls == null) {
438 pw.println("null");
439 } else {
440 final int N = mStreamControls.size();
441 pw.print("<size "); pw.print(N); pw.println('>');
442 for (int i = 0; i < N; i++) {
443 final StreamControl sc = mStreamControls.valueAt(i);
444 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
445 if (sc.seekbarView != null) {
446 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
447 pw.print(" of "); pw.print(sc.seekbarView.getMax());
448 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
449 }
450 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
451 pw.println();
452 }
453 }
454 }
455
John Spurlockeb2727b2014-07-19 23:11:36 -0400456 private void initZenModePanel() {
457 mZenPanel.init(mZenController);
458 mZenPanel.setCallback(new ZenModePanel.Callback() {
459 @Override
460 public void onMoreSettings() {
461 if (mCallback != null) {
462 mCallback.onZenSettings();
463 }
464 }
465
466 @Override
467 public void onInteraction() {
468 resetTimeout();
469 }
470
471 @Override
472 public void onExpanded(boolean expanded) {
473 if (mZenPanelExpanded == expanded) return;
474 mZenPanelExpanded = expanded;
475 updateTimeoutDelay();
476 resetTimeout();
477 }
478 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400479 }
480
John Spurlock86005342014-05-23 11:58:00 -0400481 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800482 mPanel.setLayoutDirection(layoutDirection);
483 updateStates();
484 }
485
John Spurlock8c79d2e2014-07-24 15:15:25 -0400486 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800487 final IntentFilter filter = new IntentFilter();
488 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400489 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800490 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700491 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800492 public void onReceive(Context context, Intent intent) {
493 final String action = intent.getAction();
494
495 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
496 removeMessages(MSG_RINGER_MODE_CHANGED);
497 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
498 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400499
500 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
501 postDismiss(0);
502 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800503 }
504 }, filter);
505 }
506
507 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400508 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700509 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700510 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700511 // TODO do we need to support a distinct mute property for remote?
512 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400513 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700514 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400515 }
516 }
517
518 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400519 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700520 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700521 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700522 if (mStreamControls != null) {
523 StreamControl sc = mStreamControls.get(streamType);
524 if (sc != null && sc.controller != null) {
525 VolumeInfo vi = sc.controller.getVolumeInfo();
526 return vi.getMaxVolume();
527 }
528 }
529 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400530 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700531 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400532 }
533 }
534
535 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400536 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700537 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700538 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700539 if (mStreamControls != null) {
540 StreamControl sc = mStreamControls.get(streamType);
541 if (sc != null && sc.controller != null) {
542 VolumeInfo vi = sc.controller.getVolumeInfo();
543 return vi.getCurrentVolume();
544 }
545 }
546 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400547 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700548 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400549 }
550 }
551
RoboErik19c95182014-06-23 15:38:48 -0700552 private void setStreamVolume(StreamControl sc, int index, int flags) {
553 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
554 if (sc.controller != null) {
555 sc.controller.setVolumeTo(index, flags);
556 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700557 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700558 }
559 } else if (getStreamVolume(sc.streamType) != index) {
560 if (sc.streamType == STREAM_MASTER) {
561 mAudioManager.setMasterVolume(index, flags);
562 } else {
563 mAudioManager.setStreamVolume(sc.streamType, index, flags);
564 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400565 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800566 }
567
568 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700569 final Resources res = mContext.getResources();
570 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
571 Context.LAYOUT_INFLATER_SERVICE);
572
John Spurlockad494bc2014-07-19 15:56:19 -0400573 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700574
Amith Yamasani71def772011-10-12 12:25:24 -0700575 for (int i = 0; i < STREAMS.length; i++) {
576 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700577
578 final int streamType = streamRes.streamType;
Alan Viverette494fb7b2014-04-10 18:12:56 -0700579
580 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700581 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400582 sc.group = (ViewGroup) inflater.inflate(
583 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800584 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400585 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800586 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700587 sc.icon.setContentDescription(res.getString(streamRes.descRes));
588 sc.iconRes = streamRes.iconRes;
589 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800590 sc.icon.setImageResource(sc.iconRes);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400591 sc.icon.setClickable(isNotificationOrRing(streamType));
592 if (sc.icon.isClickable()) {
John Spurlockab4ea762014-07-19 18:50:16 -0400593 sc.icon.setSoundEffectsEnabled(false);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400594 sc.icon.setOnClickListener(new OnClickListener() {
595 @Override
596 public void onClick(View v) {
597 resetTimeout();
598 toggle(sc);
599 }
600 });
John Spurlock7f1df5e2014-05-31 19:11:40 -0400601 }
John Spurlock86005342014-05-23 11:58:00 -0400602 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700603 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700604 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400605 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700606 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800607 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700608 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800609 }
610 }
611
John Spurlock7f1df5e2014-05-31 19:11:40 -0400612 private void toggle(StreamControl sc) {
613 if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
614 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
615 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
616 } else {
617 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
618 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
619 }
620 }
621
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800622 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400623 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800624
Alan Viverette494fb7b2014-04-10 18:12:56 -0700625 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800626 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400627 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800628 mActiveStreamType = -1;
629 } else {
John Spurlock86005342014-05-23 11:58:00 -0400630 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800631 mActiveStreamType = activeStreamType;
632 active.group.setVisibility(View.VISIBLE);
633 updateSlider(active);
John Spurlock8845da72014-07-07 21:29:48 -0400634 updateTimeoutDelay();
John Spurlockae641c92014-06-30 18:11:40 -0400635 setZenPanelVisible(isNotificationOrRing(mActiveStreamType));
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800636 }
637 }
638
John Spurlockad494bc2014-07-19 15:56:19 -0400639 private void updateSliderProgress(StreamControl sc, int progress) {
640 final boolean isRinger = isNotificationOrRing(sc.streamType);
641 if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
642 progress = mLastRingerProgress;
643 }
644 if (progress < 0) {
645 progress = getStreamVolume(sc.streamType);
646 }
647 sc.seekbarView.setProgress(progress);
648 if (isRinger) {
649 mLastRingerProgress = progress;
650 }
651 }
652
653 private void updateSliderIcon(StreamControl sc, boolean muted) {
654 if (isNotificationOrRing(sc.streamType)) {
655 int ringerMode = mAudioManager.getRingerMode();
656 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
657 ringerMode = mLastRingerMode;
658 } else {
659 mLastRingerMode = ringerMode;
660 }
661 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
662 }
663 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
664 }
665
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800666 /** Update the mute and progress state of a slider */
667 private void updateSlider(StreamControl sc) {
John Spurlockad494bc2014-07-19 15:56:19 -0400668 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800669 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800670 // Force reloading the image resource
671 sc.icon.setImageDrawable(null);
John Spurlockad494bc2014-07-19 15:56:19 -0400672 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400673 updateSliderEnabled(sc, muted, false);
674 }
675
John Spurlock5f640e42014-05-31 20:15:59 -0400676 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
677 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400678 final boolean isRinger = isNotificationOrRing(sc.streamType);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700679 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
680 // never disable touch interactions for remote playback, the muting is not tied to
681 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700682 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlockae641c92014-06-30 18:11:40 -0400683 } else if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400684 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400685 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400686 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400687 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400688 } else if (fixedVolume ||
689 (sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
690 (sSafetyWarning != null)) {
691 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700692 } else {
693 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400694 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400695 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700696 }
John Spurlockae641c92014-06-30 18:11:40 -0400697 // show the silent hint when the disabled slider is touched in silent mode
698 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400699 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400700 sc.group.setOnTouchListener(null);
701 sc.icon.setClickable(true);
John Spurlock5f640e42014-05-31 20:15:59 -0400702 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400703 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400704 @Override
705 public boolean onTouch(View v, MotionEvent event) {
706 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400707 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400708 return false;
709 }
John Spurlockae641c92014-06-30 18:11:40 -0400710 };
711 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400712 }
713 }
714 }
715
John Spurlockae641c92014-06-30 18:11:40 -0400716 private void showSilentHint() {
717 if (mZenPanel != null) {
718 mZenPanel.showSilentHint();
719 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800720 }
721
John Spurlock7f1df5e2014-05-31 19:11:40 -0400722 private static boolean isNotificationOrRing(int streamType) {
723 return streamType == AudioManager.STREAM_RING
724 || streamType == AudioManager.STREAM_NOTIFICATION;
725 }
726
John Spurlockae641c92014-06-30 18:11:40 -0400727 public void setCallback(Callback callback) {
728 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400729 }
730
John Spurlock8845da72014-07-07 21:29:48 -0400731 private void updateTimeoutDelay() {
John Spurlock35134602014-07-24 18:10:48 -0400732 mTimeoutDelay = sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
733 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400734 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
735 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
736 : TIMEOUT_DELAY;
737 }
738
739 private boolean isZenPanelVisible() {
740 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400741 }
742
John Spurlockae641c92014-06-30 18:11:40 -0400743 private void setZenPanelVisible(boolean visible) {
744 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400745 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400746 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400747 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400748 resetTimeout();
749 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400750 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800751 }
John Spurlockea9938c2014-07-11 18:51:32 -0400752 if (changing) {
753 updateTimeoutDelay();
754 resetTimeout();
755 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800756 }
757
Eric Laurentfde16d52012-12-03 14:42:39 -0800758 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400759 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800760 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400761 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800762 updateSlider(sc);
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765
John Spurlockae641c92014-06-30 18:11:40 -0400766 private void updateZenMode(int zen) {
767 final boolean show = mZenModeCapable && isNotificationOrRing(mActiveStreamType);
768 setZenPanelVisible(show);
John Spurlock86005342014-05-23 11:58:00 -0400769 }
770
John Spurlockae641c92014-06-30 18:11:40 -0400771 public void postZenModeChanged(int zen) {
John Spurlock86005342014-05-23 11:58:00 -0400772 removeMessages(MSG_ZEN_MODE_CHANGED);
John Spurlockae641c92014-06-30 18:11:40 -0400773 obtainMessage(MSG_ZEN_MODE_CHANGED, zen).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -0400774 }
775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 public void postVolumeChanged(int streamType, int flags) {
777 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700778 synchronized (this) {
779 if (mStreamControls == null) {
780 createSliders();
781 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 removeMessages(MSG_FREE_RESOURCES);
784 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
785 }
786
RoboErik19c95182014-06-23 15:38:48 -0700787 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700788 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
789 synchronized (this) {
790 if (mStreamControls == null) {
791 createSliders();
792 }
793 }
794 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700795 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700796 }
797
798 public void postRemoteSliderVisibility(boolean visible) {
799 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
800 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
801 }
802
803 /**
804 * Called by AudioService when it has received new remote playback information that
805 * would affect the VolumePanel display (mainly volumes). The difference with
806 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
807 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
808 * displayed.
809 * This special code path is due to the fact that remote volume updates arrive to AudioService
810 * asynchronously. So after AudioService has sent the volume update (which should be treated
811 * as a request to update the volume), the application will likely set a new volume. If the UI
812 * is still up, we need to refresh the display to show this new value.
813 */
814 public void postHasNewRemotePlaybackInfo() {
815 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
816 // don't create or prevent resources to be freed, if they disappear, this update came too
817 // late and shouldn't warrant the panel to be displayed longer
818 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
819 }
820
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400821 public void postMasterVolumeChanged(int flags) {
822 postVolumeChanged(STREAM_MASTER, flags);
823 }
824
Mike Lockwoodce952c82011-11-14 10:47:42 -0800825 public void postMuteChanged(int streamType, int flags) {
826 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700827 synchronized (this) {
828 if (mStreamControls == null) {
829 createSliders();
830 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800831 }
832 removeMessages(MSG_FREE_RESOURCES);
833 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
834 }
835
836 public void postMasterMuteChanged(int flags) {
837 postMuteChanged(STREAM_MASTER, flags);
838 }
839
Eric Laurentfde16d52012-12-03 14:42:39 -0800840 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700841 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800842 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700843 }
844
John Spurlock1dad2722014-07-11 11:07:53 -0400845 public void postDismiss(long delay) {
846 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400847 }
848
849 public void postLayoutDirection(int layoutDirection) {
850 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400851 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400852 }
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /**
855 * Override this if you have other work to do when the volume changes (for
856 * example, vibrating, playing a sound, etc.). Make sure to call through to
857 * the superclass implementation.
858 */
859 protected void onVolumeChanged(int streamType, int flags) {
860
John Spurlock86005342014-05-23 11:58:00 -0400861 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862
863 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700864 synchronized (this) {
865 if (mActiveStreamType != streamType) {
866 reorderSliders(streamType);
867 }
RoboErik19c95182014-06-23 15:38:48 -0700868 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
Marco Nelissen69f593c2009-07-28 09:55:04 -0700872 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 removeMessages(MSG_PLAY_SOUND);
874 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
875 }
876
877 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
878 removeMessages(MSG_PLAY_SOUND);
879 removeMessages(MSG_VIBRATE);
880 onStopSounds();
881 }
882
883 removeMessages(MSG_FREE_RESOURCES);
884 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800885 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887
Mike Lockwoodce952c82011-11-14 10:47:42 -0800888 protected void onMuteChanged(int streamType, int flags) {
889
John Spurlock86005342014-05-23 11:58:00 -0400890 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800891
892 StreamControl sc = mStreamControls.get(streamType);
893 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400894 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -0800895 }
896
897 onVolumeChanged(streamType, flags);
898 }
899
RoboErik19c95182014-06-23 15:38:48 -0700900 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -0700901 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800902
Marco Nelissen69f593c2009-07-28 09:55:04 -0700903 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904
905 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400906 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 + ", flags: " + flags + "), index: " + index);
908 }
909
910 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800911
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400912 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -0700913 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
915 switch (streamType) {
916
917 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800918// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700919 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
920 mContext, RingtoneManager.TYPE_RINGTONE);
921 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700922 mRingIsSilent = true;
923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 break;
925 }
926
927 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800928 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800929 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
930 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
931 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
932 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800933 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800935 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937 break;
938 }
939
940 case AudioManager.STREAM_VOICE_CALL: {
941 /*
942 * For in-call voice call volume, there is no inaudible volume.
943 * Rescale the UI control so the progress bar doesn't go all
944 * the way to zero and don't show the mute icon.
945 */
946 index++;
947 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 break;
949 }
950
951 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 break;
953 }
954
955 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700956 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
957 mContext, RingtoneManager.TYPE_NOTIFICATION);
958 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700959 mRingIsSilent = true;
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 break;
962 }
963
964 case AudioManager.STREAM_BLUETOOTH_SCO: {
965 /*
966 * For in-call voice call volume, there is no inaudible volume.
967 * Rescale the UI control so the progress bar doesn't go all
968 * the way to zero and don't show the mute icon.
969 */
970 index++;
971 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 break;
973 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700974
975 case AudioService.STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -0700976 if (controller == null && sc != null) {
977 // If we weren't passed one try using the last one set.
978 controller = sc.controller;
979 }
980 if (controller == null) {
981 // We still don't have one, ignore the command.
982 Log.w(mTag, "sent remote volume change without a controller!");
983 } else {
984 VolumeInfo vi = controller.getVolumeInfo();
985 index = vi.getCurrentVolume();
986 max = vi.getMaxVolume();
987 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
988 // if the remote volume is fixed add the flag for the UI
989 flags |= AudioManager.FLAG_FIXED_VOLUME;
990 }
991 }
John Spurlock86005342014-05-23 11:58:00 -0400992 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700993 break;
994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800997 if (sc != null) {
RoboErik19c95182014-06-23 15:38:48 -0700998 if (streamType == AudioService.STREAM_REMOTE_MUSIC && controller != sc.controller) {
999 if (sc.controller != null) {
1000 sc.controller.removeCallback(mMediaControllerCb);
1001 }
1002 sc.controller = controller;
1003 if (controller != null) {
1004 sc.controller.addCallback(mMediaControllerCb);
1005 }
1006 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001007 if (sc.seekbarView.getMax() != max) {
1008 sc.seekbarView.setMax(max);
1009 }
John Spurlockad494bc2014-07-19 15:56:19 -04001010 updateSliderProgress(sc, index);
John Spurlock7f1df5e2014-05-31 19:11:40 -04001011 updateSliderEnabled(sc, isMuted(streamType),
1012 (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014
John Spurlock86005342014-05-23 11:58:00 -04001015 if (!isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001016 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
1017 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1018 mAudioManager.forceVolumeControlStream(stream);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001019
John Spurlockeb2727b2014-07-19 23:11:36 -04001020 mDialog.show();
1021 if (mCallback != null) {
1022 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001027 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
1028 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock3346a802014-05-20 16:25:37 -04001029 mAudioManager.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -07001030 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1032 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001033
1034 // Pulse the slider icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001035 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1036 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039
John Spurlock86005342014-05-23 11:58:00 -04001040 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001041 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001042 }
1043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 protected void onPlaySound(int streamType, int flags) {
1045
1046 if (hasMessages(MSG_STOP_SOUNDS)) {
1047 removeMessages(MSG_STOP_SOUNDS);
1048 // Force stop right now
1049 onStopSounds();
1050 }
1051
1052 synchronized (this) {
1053 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001054 if (toneGen != null) {
1055 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1056 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060
1061 protected void onStopSounds() {
1062
1063 synchronized (this) {
1064 int numStreamTypes = AudioSystem.getNumStreamTypes();
1065 for (int i = numStreamTypes - 1; i >= 0; i--) {
1066 ToneGenerator toneGen = mToneGenerators[i];
1067 if (toneGen != null) {
1068 toneGen.stopTone();
1069 }
1070 }
1071 }
1072 }
1073
1074 protected void onVibrate() {
1075
1076 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -07001077 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 return;
1079 }
1080
John Spurlock7b414672014-07-18 13:02:39 -04001081 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083
RoboErik19c95182014-06-23 15:38:48 -07001084 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
1085 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: " + flags
1086 + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001087
John Spurlock86005342014-05-23 11:58:00 -04001088 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001089 synchronized (this) {
1090 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
1091 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
1092 }
RoboErik19c95182014-06-23 15:38:48 -07001093 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001094 }
1095 } else {
John Spurlock86005342014-05-23 11:58:00 -04001096 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001097 }
1098
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001099 removeMessages(MSG_FREE_RESOURCES);
1100 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001101 resetTimeout();
1102 }
1103
1104 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001105 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1106 if (isShowing()
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001107 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
1108 && (mStreamControls != null)) {
RoboErik19c95182014-06-23 15:38:48 -07001109 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001110 }
1111 }
1112
RoboErik19c95182014-06-23 15:38:48 -07001113 /**
1114 * Clear the current remote stream controller.
1115 */
1116 private void clearRemoteStreamController() {
1117 if (mStreamControls != null) {
1118 StreamControl sc = mStreamControls.get(AudioService.STREAM_REMOTE_MUSIC);
1119 if (sc != null) {
1120 if (sc.controller != null) {
1121 sc.controller.removeCallback(mMediaControllerCb);
1122 sc.controller = null;
1123 }
1124 }
1125 }
1126 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001127
1128 /**
1129 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
1130 * Hide or show a slider
1131 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
1132 * or AudioService.STREAM_REMOTE_MUSIC
1133 * @param visible
1134 */
1135 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001136 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001137 boolean isVisible = (visible == 1);
1138 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1139 StreamResources streamRes = STREAMS[i];
1140 if (streamRes.streamType == streamType) {
1141 streamRes.show = isVisible;
1142 if (!isVisible && (mActiveStreamType == streamType)) {
1143 mActiveStreamType = -1;
1144 }
1145 break;
1146 }
1147 }
1148 }
1149
Eric Laurentfde16d52012-12-03 14:42:39 -08001150 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001151 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1152 || isShowing()) {
1153 synchronized (sSafetyWarningLock) {
1154 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001155 return;
1156 }
John Spurlock35134602014-07-24 18:10:48 -04001157 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1158 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001159 }
1160 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001161 }
Eric Laurentfde16d52012-12-03 14:42:39 -08001162 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001163 }
1164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1167 */
1168 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001169 if (streamType == STREAM_MASTER) {
1170 // For devices that use the master volume setting only but still want to
1171 // play a volume-changed tone, direct the master volume pseudostream to
1172 // the system stream's tone generator.
1173 if (mPlayMasterStreamTones) {
1174 streamType = AudioManager.STREAM_SYSTEM;
1175 } else {
1176 return null;
1177 }
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 synchronized (this) {
1180 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001181 try {
1182 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1183 } catch (RuntimeException e) {
1184 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001185 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001186 + "RuntimeException: " + e);
1187 }
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001190 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194
1195 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001196 * Switch between icons because Bluetooth music is same as music volume, but with
1197 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001199 private void setMusicIcon(int resId, int resMuteId) {
1200 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1201 if (sc != null) {
1202 sc.iconRes = resId;
1203 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001204 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
1208 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 synchronized (this) {
1210 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1211 if (mToneGenerators[i] != null) {
1212 mToneGenerators[i].release();
1213 }
1214 mToneGenerators[i] = null;
1215 }
1216 }
1217 }
1218
1219 @Override
1220 public void handleMessage(Message msg) {
1221 switch (msg.what) {
1222
1223 case MSG_VOLUME_CHANGED: {
1224 onVolumeChanged(msg.arg1, msg.arg2);
1225 break;
1226 }
1227
Mike Lockwoodce952c82011-11-14 10:47:42 -08001228 case MSG_MUTE_CHANGED: {
1229 onMuteChanged(msg.arg1, msg.arg2);
1230 break;
1231 }
1232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 case MSG_FREE_RESOURCES: {
1234 onFreeResources();
1235 break;
1236 }
1237
1238 case MSG_STOP_SOUNDS: {
1239 onStopSounds();
1240 break;
1241 }
1242
1243 case MSG_PLAY_SOUND: {
1244 onPlaySound(msg.arg1, msg.arg2);
1245 break;
1246 }
1247
1248 case MSG_VIBRATE: {
1249 onVibrate();
1250 break;
1251 }
1252
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001253 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001254 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001255 mDialog.dismiss();
1256 clearRemoteStreamController();
1257 mActiveStreamType = -1;
1258 if (mCallback != null) {
1259 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001260 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001261 }
John Spurlock35134602014-07-24 18:10:48 -04001262 synchronized (sSafetyWarningLock) {
1263 if (sSafetyWarning != null) {
1264 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1265 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001266 }
1267 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001268 break;
1269 }
1270 case MSG_RINGER_MODE_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001271 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001272 updateStates();
1273 }
1274 break;
1275 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001276
1277 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001278 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001279 break;
1280 }
1281
1282 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1283 onRemoteVolumeUpdateIfShown();
1284 break;
1285
1286 case MSG_SLIDER_VISIBILITY_CHANGED:
1287 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1288 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001289
1290 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001291 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001292 break;
John Spurlock86005342014-05-23 11:58:00 -04001293
1294 case MSG_LAYOUT_DIRECTION:
1295 setLayoutDirection(msg.arg1);
1296 break;
1297
1298 case MSG_ZEN_MODE_CHANGED:
John Spurlockae641c92014-06-30 18:11:40 -04001299 updateZenMode(msg.arg1);
1300 break;
1301
1302 case MSG_USER_ACTIVITY:
1303 if (mCallback != null) {
1304 mCallback.onInteraction();
1305 }
John Spurlock86005342014-05-23 11:58:00 -04001306 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308 }
1309
John Spurlockae641c92014-06-30 18:11:40 -04001310 private void resetTimeout() {
John Spurlockea9938c2014-07-11 18:51:32 -04001311 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
1312 + " delay=" + mTimeoutDelay);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001313 removeMessages(MSG_TIMEOUT);
John Spurlock3bd4fee2014-05-29 20:51:09 -04001314 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
John Spurlockae641c92014-06-30 18:11:40 -04001315 removeMessages(MSG_USER_ACTIVITY);
1316 sendEmptyMessage(MSG_USER_ACTIVITY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001317 }
1318
John Spurlock1dad2722014-07-11 11:07:53 -04001319 private void forceTimeout(long delay) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001320 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001321 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001322 }
1323
1324 public ZenModeController getZenController() {
1325 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001326 }
1327
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001328 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1329 @Override
1330 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1331 final Object tag = seekBar.getTag();
1332 if (fromUser && tag instanceof StreamControl) {
1333 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001334 setStreamVolume(sc, progress,
1335 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001336 }
1337 resetTimeout();
1338 }
1339
1340 @Override
1341 public void onStartTrackingTouch(SeekBar seekBar) {
1342 }
1343
1344 @Override
1345 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001346 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001347 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001348
John Spurlock86005342014-05-23 11:58:00 -04001349 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockae641c92014-06-30 18:11:40 -04001350 public void onZenChanged(int zen) {
John Spurlock7f1df5e2014-05-31 19:11:40 -04001351 postZenModeChanged(zen);
John Spurlock86005342014-05-23 11:58:00 -04001352 }
1353 };
RoboErik19c95182014-06-23 15:38:48 -07001354
1355 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
1356 public void onVolumeInfoChanged(VolumeInfo info) {
1357 onRemoteVolumeUpdateIfShown();
1358 }
1359 };
John Spurlockae641c92014-06-30 18:11:40 -04001360
1361 public interface Callback {
1362 void onZenSettings();
1363 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001364 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366}