blob: 1fe46987f932053bc4432b0bd3b8046bbacdce3e [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
John Spurlockbb4a7022014-11-08 12:40:19 -050019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
Eric Laurentc34dcc12012-09-10 13:51:52 -070022import android.app.AlertDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080023import android.app.Dialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080024import android.content.BroadcastReceiver;
John Spurlockb4782522014-08-22 14:54:46 -040025import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080027import android.content.DialogInterface;
John Spurlock86005342014-05-23 11:58:00 -040028import android.content.DialogInterface.OnDismissListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080030import android.content.IntentFilter;
John Spurlockb4782522014-08-22 14:54:46 -040031import android.content.pm.PackageManager;
John Spurlockb4782522014-08-22 14:54:46 -040032import android.content.pm.ServiceInfo;
John Spurlock7e6809a2014-08-06 16:03:14 -040033import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.res.Resources;
John Spurlockad494bc2014-07-19 15:56:19 -040035import android.content.res.TypedArray;
John Spurlock2078caf2014-05-29 22:20:14 -040036import android.graphics.PixelFormat;
37import android.graphics.drawable.ColorDrawable;
John Spurlock7b414672014-07-18 13:02:39 -040038import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.media.AudioManager;
40import android.media.AudioService;
41import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070042import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070044import android.media.VolumeProvider;
45import android.media.session.MediaController;
RoboErikd2b8c942014-08-19 11:23:40 -070046import android.media.session.MediaController.PlaybackInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070047import android.net.Uri;
John Spurlockbb4a7022014-11-08 12:40:19 -050048import android.os.AsyncTask;
49import android.os.Bundle;
John Spurlocka0457c22014-09-26 13:22:08 -040050import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Handler;
52import android.os.Message;
53import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040055import android.util.SparseArray;
John Spurlock2d28d6e2014-08-01 13:10:14 -040056import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040057import android.view.LayoutInflater;
58import android.view.MotionEvent;
59import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040060import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040061import android.view.ViewGroup;
62import android.view.Window;
63import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070064import android.view.WindowManager.LayoutParams;
Selim Cinek62ea3402014-09-08 12:11:51 +020065import android.view.accessibility.AccessibilityEvent;
Selim Cinek9f6ceb12014-08-29 16:28:39 +020066import android.view.accessibility.AccessibilityManager;
John Spurlockbb4a7022014-11-08 12:40:19 -050067import android.view.animation.AnimationUtils;
68import android.view.animation.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080070import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080071import android.widget.SeekBar.OnSeekBarChangeListener;
John Spurlockb4782522014-08-22 14:54:46 -040072import android.widget.TextView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080073
John Spurlock86005342014-05-23 11:58:00 -040074import com.android.internal.R;
John Spurlockbb4a7022014-11-08 12:40:19 -050075import com.android.systemui.DemoMode;
John Spurlock35134602014-07-24 18:10:48 -040076import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040077import com.android.systemui.statusbar.policy.ZenModeController;
78
John Spurlockad494bc2014-07-19 15:56:19 -040079import java.io.FileDescriptor;
80import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82/**
John Spurlock3346a802014-05-20 16:25:37 -040083 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * @hide
86 */
John Spurlockbb4a7022014-11-08 12:40:19 -050087public class VolumePanel extends Handler implements DemoMode {
John Spurlockae641c92014-06-30 18:11:40 -040088 private static final String TAG = "VolumePanel";
89 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
John Spurlock3346a802014-05-20 16:25:37 -040091 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 /**
94 * The delay before vibrating. This small period exists so if the user is
95 * moving to silent mode, it will not emit a short vibrate (it normally
96 * would since vibrate is between normal mode and silent mode using hardware
97 * keys).
98 */
99 public static final int VIBRATE_DELAY = 300;
100
101 private static final int VIBRATE_DURATION = 300;
102 private static final int BEEP_DURATION = 150;
103 private static final int MAX_VOLUME = 100;
104 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800105 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -0400106 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -0400107 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -0400108 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400109 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 private static final int MSG_VOLUME_CHANGED = 0;
112 private static final int MSG_FREE_RESOURCES = 1;
113 private static final int MSG_PLAY_SOUND = 2;
114 private static final int MSG_STOP_SOUNDS = 3;
115 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800116 private static final int MSG_TIMEOUT = 5;
117 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -0800118 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700119 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
120 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
121 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700122 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -0400123 private static final int MSG_LAYOUT_DIRECTION = 12;
John Spurlock45601d62014-08-07 17:40:50 -0400124 private static final int MSG_ZEN_MODE_AVAILABLE_CHANGED = 13;
John Spurlockae641c92014-06-30 18:11:40 -0400125 private static final int MSG_USER_ACTIVITY = 14;
John Spurlockb4782522014-08-22 14:54:46 -0400126 private static final int MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED = 15;
John Spurlock661f2cf2014-11-17 10:29:10 -0500127 private static final int MSG_ZEN_MODE_CHANGED = 16;
128 private static final int MSG_INTERNAL_RINGER_MODE_CHANGED = 17;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400130 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800131 private static final int STREAM_MASTER = -100;
RoboErik2811dd32014-08-12 09:48:13 -0700132 // Pseudo stream type for remote volume
133 private static final int STREAM_REMOTE_MUSIC = -200;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400134
John Spurlock7b414672014-07-18 13:02:39 -0400135 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
136 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
137 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
138 .build();
139
John Spurlock4bebb1d2014-10-02 19:17:19 -0400140 private static final int IC_AUDIO_VOL = com.android.systemui.R.drawable.ic_audio_vol;
141 private static final int IC_AUDIO_VOL_MUTE = com.android.systemui.R.drawable.ic_audio_vol_mute;
John Spurlockbb4a7022014-11-08 12:40:19 -0500142 private static final int IC_AUDIO_BT = com.android.systemui.R.drawable.ic_audio_bt;
143 private static final int IC_AUDIO_BT_MUTE = com.android.systemui.R.drawable.ic_audio_bt_mute;
John Spurlock4bebb1d2014-10-02 19:17:19 -0400144
John Spurlock86005342014-05-23 11:58:00 -0400145 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400146 protected final Context mContext;
147 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400148 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700149 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700150 private boolean mVoiceCapable;
John Spurlock45601d62014-08-07 17:40:50 -0400151 private boolean mZenModeAvailable;
John Spurlock8845da72014-07-07 21:29:48 -0400152 private boolean mZenPanelExpanded;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400153 private int mTimeoutDelay = TIMEOUT_DELAY;
John Spurlockad494bc2014-07-19 15:56:19 -0400154 private float mDisabledAlpha;
155 private int mLastRingerMode = AudioManager.RINGER_MODE_NORMAL;
156 private int mLastRingerProgress = 0;
John Spurlockbb4a7022014-11-08 12:40:19 -0500157 private int mDemoIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
Christopher Tatec4b78d22012-05-22 13:57:58 -0700159 // True if we want to play tones on the system stream when the master stream is specified.
160 private final boolean mPlayMasterStreamTones;
161
John Spurlock86005342014-05-23 11:58:00 -0400162
163 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private final View mView;
John Spurlockeb2727b2014-07-19 23:11:36 -0400165 /** Dialog hosting the panel */
John Spurlock86005342014-05-23 11:58:00 -0400166 private final Dialog mDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800167
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700168 /** The visible portion of the volume overlay */
169 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400170 /** Contains the slider and its touchable icons */
171 private final ViewGroup mSliderPanel;
John Spurlockeb2727b2014-07-19 23:11:36 -0400172 /** The zen mode configuration panel view */
John Spurlock86005342014-05-23 11:58:00 -0400173 private ZenModePanel mZenPanel;
John Spurlock661f2cf2014-11-17 10:29:10 -0500174 /** The component currently suppressing notification stream effects */
175 private ComponentName mNotificationEffectsSuppressor;
John Spurlock86005342014-05-23 11:58:00 -0400176
John Spurlockae641c92014-06-30 18:11:40 -0400177 private Callback mCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800178
179 /** Currently active stream that shows up at the top of the list of sliders */
180 private int mActiveStreamType = -1;
181 /** All the slider controls mapped by stream type */
John Spurlockad494bc2014-07-19 15:56:19 -0400182 private SparseArray<StreamControl> mStreamControls;
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200183 private final AccessibilityManager mAccessibilityManager;
John Spurlockbb4a7022014-11-08 12:40:19 -0500184 private final SecondaryIconTransition mSecondaryIconTransition;
John Spurlock661f2cf2014-11-17 10:29:10 -0500185 private final IconPulser mIconPulser;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800186
Amith Yamasani71def772011-10-12 12:25:24 -0700187 private enum StreamResources {
188 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
189 R.string.volume_icon_description_bluetooth,
John Spurlockbb4a7022014-11-08 12:40:19 -0500190 IC_AUDIO_BT,
191 IC_AUDIO_BT_MUTE,
Amith Yamasani71def772011-10-12 12:25:24 -0700192 false),
193 RingerStream(AudioManager.STREAM_RING,
194 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400195 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlock661f2cf2014-11-17 10:29:10 -0500196 com.android.systemui.R.drawable.ic_ringer_mute,
Amith Yamasani71def772011-10-12 12:25:24 -0700197 false),
198 VoiceStream(AudioManager.STREAM_VOICE_CALL,
199 R.string.volume_icon_description_incall,
John Spurlockbb4a7022014-11-08 12:40:19 -0500200 com.android.systemui.R.drawable.ic_audio_phone,
201 com.android.systemui.R.drawable.ic_audio_phone,
Amith Yamasani71def772011-10-12 12:25:24 -0700202 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700203 AlarmStream(AudioManager.STREAM_ALARM,
204 R.string.volume_alarm,
John Spurlock4bebb1d2014-10-02 19:17:19 -0400205 com.android.systemui.R.drawable.ic_audio_alarm,
206 com.android.systemui.R.drawable.ic_audio_alarm_mute,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700207 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700208 MediaStream(AudioManager.STREAM_MUSIC,
209 R.string.volume_icon_description_media,
John Spurlock4bebb1d2014-10-02 19:17:19 -0400210 IC_AUDIO_VOL,
211 IC_AUDIO_VOL_MUTE,
Amith Yamasani71def772011-10-12 12:25:24 -0700212 true),
213 NotificationStream(AudioManager.STREAM_NOTIFICATION,
214 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400215 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlock661f2cf2014-11-17 10:29:10 -0500216 com.android.systemui.R.drawable.ic_ringer_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400217 true),
218 // for now, use media resources for master volume
219 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700220 R.string.volume_icon_description_media, //FIXME should have its own description
John Spurlock4bebb1d2014-10-02 19:17:19 -0400221 IC_AUDIO_VOL,
222 IC_AUDIO_VOL_MUTE,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700223 false),
RoboErik2811dd32014-08-12 09:48:13 -0700224 RemoteStream(STREAM_REMOTE_MUSIC,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700225 R.string.volume_icon_description_media, //FIXME should have its own description
226 R.drawable.ic_media_route_on_holo_dark,
227 R.drawable.ic_media_route_disabled_holo_dark,
228 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700229
230 int streamType;
231 int descRes;
232 int iconRes;
233 int iconMuteRes;
234 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
235 boolean show;
236
237 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
238 this.streamType = streamType;
239 this.descRes = descRes;
240 this.iconRes = iconRes;
241 this.iconMuteRes = iconMuteRes;
242 this.show = show;
243 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700244 }
Amith Yamasani71def772011-10-12 12:25:24 -0700245
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800246 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700247 private static final StreamResources[] STREAMS = {
248 StreamResources.BluetoothSCOStream,
249 StreamResources.RingerStream,
250 StreamResources.VoiceStream,
251 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700252 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400253 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700254 StreamResources.MasterStream,
255 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800256 };
257
258 /** Object that contains data for each slider */
259 private class StreamControl {
260 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700261 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800262 ViewGroup group;
263 ImageView icon;
264 SeekBar seekbarView;
John Spurlockb4782522014-08-22 14:54:46 -0400265 TextView suppressorView;
John Spurlockbb4a7022014-11-08 12:40:19 -0500266 View divider;
267 ImageView secondaryIcon;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800268 int iconRes;
269 int iconMuteRes;
John Spurlockb4782522014-08-22 14:54:46 -0400270 int iconSuppressedRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
273 // Synchronize when accessing this
274 private ToneGenerator mToneGenerators[];
275 private Vibrator mVibrator;
John Spurlock661f2cf2014-11-17 10:29:10 -0500276 private boolean mHasVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
John Spurlock35134602014-07-24 18:10:48 -0400278 private static AlertDialog sSafetyWarning;
279 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700280
John Spurlock35134602014-07-24 18:10:48 -0400281 private static class SafetyWarning extends SystemUIDialog
282 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800283 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800284 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400285 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700286
John Spurlock2d28d6e2014-08-01 13:10:14 -0400287 private boolean mNewVolumeUp;
288
John Spurlock35134602014-07-24 18:10:48 -0400289 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
290 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700291 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800292 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400293 mAudioManager = audioManager;
294
295 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
296 setButton(DialogInterface.BUTTON_POSITIVE,
297 mContext.getString(com.android.internal.R.string.yes), this);
298 setButton(DialogInterface.BUTTON_NEGATIVE,
299 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
300 setOnDismissListener(this);
301
Eric Laurentc34dcc12012-09-10 13:51:52 -0700302 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400303 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700304 }
305
306 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400307 public boolean onKeyDown(int keyCode, KeyEvent event) {
308 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
309 mNewVolumeUp = true;
310 }
311 return super.onKeyDown(keyCode, event);
312 }
313
314 @Override
315 public boolean onKeyUp(int keyCode, KeyEvent event) {
316 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
317 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
318 mAudioManager.disableSafeMediaVolume();
319 dismiss();
320 }
321 return super.onKeyUp(keyCode, event);
322 }
323
324 @Override
John Spurlock35134602014-07-24 18:10:48 -0400325 public void onClick(DialogInterface dialog, int which) {
326 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700327 }
328
Alan Viverette494fb7b2014-04-10 18:12:56 -0700329 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700330 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400331 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800332 cleanUp();
333 }
334
335 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400336 synchronized (sSafetyWarningLock) {
337 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700338 }
John Spurlock1dad2722014-07-11 11:07:53 -0400339 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800340 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700341 }
John Spurlock35134602014-07-24 18:10:48 -0400342
343 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
344 @Override
345 public void onReceive(Context context, Intent intent) {
346 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
347 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
348 cancel();
349 cleanUp();
350 }
351 }
352 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700353 }
354
John Spurlockeb2727b2014-07-19 23:11:36 -0400355 public VolumePanel(Context context, ZenModeController zenController) {
356 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400358 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200360 mAccessibilityManager = (AccessibilityManager) context.getSystemService(
361 Context.ACCESSIBILITY_SERVICE);
John Spurlockbb4a7022014-11-08 12:40:19 -0500362 mSecondaryIconTransition = new SecondaryIconTransition();
John Spurlock661f2cf2014-11-17 10:29:10 -0500363 mIconPulser = new IconPulser(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400365 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700366 final Resources res = context.getResources();
367 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400368 if (useMasterVolume) {
369 for (int i = 0; i < STREAMS.length; i++) {
370 StreamResources streamRes = STREAMS[i];
371 streamRes.show = (streamRes.streamType == STREAM_MASTER);
372 }
373 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400374 if (LOGD) Log.d(mTag, "new VolumePanel");
375
John Spurlockad494bc2014-07-19 15:56:19 -0400376 mDisabledAlpha = 0.5f;
377 if (mContext.getTheme() != null) {
378 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
379 new int[] { android.R.attr.disabledAlpha });
380 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
381 arr.recycle();
382 }
383
John Spurlockeb2727b2014-07-19 23:11:36 -0400384 mDialog = new Dialog(context) {
385 @Override
386 public boolean onTouchEvent(MotionEvent event) {
387 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400388 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400389 forceTimeout(0);
390 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700391 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400392 return false;
393 }
394 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700395
John Spurlockeb2727b2014-07-19 23:11:36 -0400396 final Window window = mDialog.getWindow();
John Spurlockeb2727b2014-07-19 23:11:36 -0400397 window.requestFeature(Window.FEATURE_NO_TITLE);
John Spurlockeb2727b2014-07-19 23:11:36 -0400398 mDialog.setCanceledOnTouchOutside(true);
399 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
400 mDialog.setOnDismissListener(new OnDismissListener() {
401 @Override
402 public void onDismiss(DialogInterface dialog) {
403 mActiveStreamType = -1;
404 mAudioManager.forceVolumeControlStream(mActiveStreamType);
405 setZenPanelVisible(false);
John Spurlockbb4a7022014-11-08 12:40:19 -0500406 mDemoIcon = 0;
407 mSecondaryIconTransition.cancel();
John Spurlockeb2727b2014-07-19 23:11:36 -0400408 }
409 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700410
John Spurlockeb2727b2014-07-19 23:11:36 -0400411 mDialog.create();
Alan Viverette494fb7b2014-04-10 18:12:56 -0700412
John Spurlock7e6809a2014-08-06 16:03:14 -0400413 final LayoutParams lp = window.getAttributes();
414 lp.token = null;
415 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
416 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
417 lp.format = PixelFormat.TRANSLUCENT;
418 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
419 lp.setTitle(TAG);
420 window.setAttributes(lp);
421
422 updateWidth();
423
424 window.setBackgroundDrawable(new ColorDrawable(0x00000000));
425 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
426 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
427 | LayoutParams.FLAG_NOT_TOUCH_MODAL
428 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
429 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockeb2727b2014-07-19 23:11:36 -0400430 mView = window.findViewById(R.id.content);
John Spurlocka0457c22014-09-26 13:22:08 -0400431 Interaction.register(mView, new Interaction.Callback() {
John Spurlockeb2727b2014-07-19 23:11:36 -0400432 @Override
John Spurlocka0457c22014-09-26 13:22:08 -0400433 public void onInteraction() {
John Spurlockeb2727b2014-07-19 23:11:36 -0400434 resetTimeout();
John Spurlockeb2727b2014-07-19 23:11:36 -0400435 }
436 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700437
John Spurlock86005342014-05-23 11:58:00 -0400438 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
439 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400440 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
441 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700444 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
John Spurlock661f2cf2014-11-17 10:29:10 -0500445 mHasVibrator = mVibrator != null && mVibrator.hasVibrator();
Amith Yamasani71def772011-10-12 12:25:24 -0700446 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700447
John Spurlock45601d62014-08-07 17:40:50 -0400448 if (mZenController != null && !useMasterVolume) {
449 mZenModeAvailable = mZenController.isZenAvailable();
John Spurlock661f2cf2014-11-17 10:29:10 -0500450 mNotificationEffectsSuppressor = mZenController.getEffectsSuppressor();
John Spurlock45601d62014-08-07 17:40:50 -0400451 mZenController.addCallback(mZenCallback);
452 }
Amith Yamasani42722bf2011-07-22 10:34:27 -0700453
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700454 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
455 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700456 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
457
John Spurlock8c79d2e2014-07-24 15:15:25 -0400458 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800459 }
460
John Spurlock7e6809a2014-08-06 16:03:14 -0400461 public void onConfigurationChanged(Configuration newConfig) {
462 updateWidth();
Jason Monke2f47712014-09-09 09:35:55 -0400463 if (mZenPanel != null) {
464 mZenPanel.updateLocale();
465 }
John Spurlock7e6809a2014-08-06 16:03:14 -0400466 }
467
468 private void updateWidth() {
469 final Resources res = mContext.getResources();
470 final LayoutParams lp = mDialog.getWindow().getAttributes();
471 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.notification_panel_width);
472 lp.gravity =
473 res.getInteger(com.android.systemui.R.integer.notification_panel_layout_gravity);
474 mDialog.getWindow().setAttributes(lp);
475 }
476
John Spurlockad494bc2014-07-19 15:56:19 -0400477 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
478 pw.println("VolumePanel state:");
479 pw.print(" mTag="); pw.println(mTag);
480 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
481 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
John Spurlock661f2cf2014-11-17 10:29:10 -0500482 pw.print(" mHasVibrator="); pw.println(mHasVibrator);
John Spurlock45601d62014-08-07 17:40:50 -0400483 pw.print(" mZenModeAvailable="); pw.println(mZenModeAvailable);
John Spurlockad494bc2014-07-19 15:56:19 -0400484 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
John Spurlock661f2cf2014-11-17 10:29:10 -0500485 pw.print(" mNotificationEffectsSuppressor="); pw.println(mNotificationEffectsSuppressor);
John Spurlockad494bc2014-07-19 15:56:19 -0400486 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
487 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
488 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
489 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
490 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
491 pw.print(" isShowing()="); pw.println(isShowing());
492 pw.print(" mCallback="); pw.println(mCallback);
493 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400494 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400495 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
496 pw.print(" mStreamControls=");
497 if (mStreamControls == null) {
498 pw.println("null");
499 } else {
500 final int N = mStreamControls.size();
501 pw.print("<size "); pw.print(N); pw.println('>');
502 for (int i = 0; i < N; i++) {
503 final StreamControl sc = mStreamControls.valueAt(i);
504 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
505 if (sc.seekbarView != null) {
506 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
507 pw.print(" of "); pw.print(sc.seekbarView.getMax());
508 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
509 }
510 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
511 pw.println();
512 }
513 }
514 }
515
John Spurlockeb2727b2014-07-19 23:11:36 -0400516 private void initZenModePanel() {
517 mZenPanel.init(mZenController);
518 mZenPanel.setCallback(new ZenModePanel.Callback() {
519 @Override
520 public void onMoreSettings() {
521 if (mCallback != null) {
522 mCallback.onZenSettings();
523 }
524 }
525
526 @Override
527 public void onInteraction() {
528 resetTimeout();
529 }
530
531 @Override
532 public void onExpanded(boolean expanded) {
533 if (mZenPanelExpanded == expanded) return;
534 mZenPanelExpanded = expanded;
535 updateTimeoutDelay();
536 resetTimeout();
537 }
538 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400539 }
540
John Spurlock86005342014-05-23 11:58:00 -0400541 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800542 mPanel.setLayoutDirection(layoutDirection);
543 updateStates();
544 }
545
John Spurlock8c79d2e2014-07-24 15:15:25 -0400546 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800547 final IntentFilter filter = new IntentFilter();
548 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400549 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800550 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700551 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800552 public void onReceive(Context context, Intent intent) {
553 final String action = intent.getAction();
554
555 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
556 removeMessages(MSG_RINGER_MODE_CHANGED);
557 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
558 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400559
560 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
561 postDismiss(0);
562 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800563 }
564 }, filter);
565 }
566
567 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400568 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700569 return mAudioManager.isMasterMute();
RoboErik2811dd32014-08-12 09:48:13 -0700570 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700571 // TODO do we need to support a distinct mute property for remote?
572 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400573 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700574 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400575 }
576 }
577
578 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400579 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700580 return mAudioManager.getMasterMaxVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700581 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700582 if (mStreamControls != null) {
583 StreamControl sc = mStreamControls.get(streamType);
584 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700585 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700586 return ai.getMaxVolume();
RoboErik19c95182014-06-23 15:38:48 -0700587 }
588 }
589 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400590 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700591 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400592 }
593 }
594
595 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400596 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700597 return mAudioManager.getMasterVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700598 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700599 if (mStreamControls != null) {
600 StreamControl sc = mStreamControls.get(streamType);
601 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700602 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700603 return ai.getCurrentVolume();
RoboErik19c95182014-06-23 15:38:48 -0700604 }
605 }
606 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400607 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700608 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400609 }
610 }
611
RoboErik19c95182014-06-23 15:38:48 -0700612 private void setStreamVolume(StreamControl sc, int index, int flags) {
RoboErik2811dd32014-08-12 09:48:13 -0700613 if (sc.streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700614 if (sc.controller != null) {
615 sc.controller.setVolumeTo(index, flags);
616 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700617 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700618 }
619 } else if (getStreamVolume(sc.streamType) != index) {
620 if (sc.streamType == STREAM_MASTER) {
621 mAudioManager.setMasterVolume(index, flags);
622 } else {
623 mAudioManager.setStreamVolume(sc.streamType, index, flags);
624 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400625 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800626 }
627
628 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700629 final Resources res = mContext.getResources();
630 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
631 Context.LAYOUT_INFLATER_SERVICE);
632
John Spurlockad494bc2014-07-19 15:56:19 -0400633 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700634
John Spurlockbb4a7022014-11-08 12:40:19 -0500635 final StreamResources notificationStream = StreamResources.NotificationStream;
Amith Yamasani71def772011-10-12 12:25:24 -0700636 for (int i = 0; i < STREAMS.length; i++) {
637 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700638
639 final int streamType = streamRes.streamType;
John Spurlockbb4a7022014-11-08 12:40:19 -0500640 final boolean isNotification = isNotificationOrRing(streamType);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700641
642 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700643 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400644 sc.group = (ViewGroup) inflater.inflate(
645 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800646 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400647 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800648 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700649 sc.icon.setContentDescription(res.getString(streamRes.descRes));
650 sc.iconRes = streamRes.iconRes;
651 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800652 sc.icon.setImageResource(sc.iconRes);
John Spurlock661f2cf2014-11-17 10:29:10 -0500653 sc.icon.setClickable(isNotification && mHasVibrator);
John Spurlockbb4a7022014-11-08 12:40:19 -0500654 if (isNotification) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500655 if (mHasVibrator) {
656 sc.icon.setSoundEffectsEnabled(false);
657 sc.iconMuteRes = com.android.systemui.R.drawable.ic_ringer_vibrate;
658 sc.icon.setOnClickListener(new OnClickListener() {
659 @Override
660 public void onClick(View v) {
661 resetTimeout();
662 toggleRinger(sc);
663 }
664 });
665 }
John Spurlockb4782522014-08-22 14:54:46 -0400666 sc.iconSuppressedRes = com.android.systemui.R.drawable.ic_ringer_mute;
John Spurlock7f1df5e2014-05-31 19:11:40 -0400667 }
John Spurlock86005342014-05-23 11:58:00 -0400668 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
John Spurlockb4782522014-08-22 14:54:46 -0400669 sc.suppressorView =
670 (TextView) sc.group.findViewById(com.android.systemui.R.id.suppressor);
671 sc.suppressorView.setVisibility(View.GONE);
John Spurlockbb4a7022014-11-08 12:40:19 -0500672 final boolean showSecondary = !isNotification && notificationStream.show;
673 sc.divider = sc.group.findViewById(com.android.systemui.R.id.divider);
674 sc.secondaryIcon = (ImageView) sc.group
675 .findViewById(com.android.systemui.R.id.secondary_icon);
676 sc.secondaryIcon.setImageResource(com.android.systemui.R.drawable.ic_ringer_audible);
677 sc.secondaryIcon.setContentDescription(res.getString(notificationStream.descRes));
678 sc.secondaryIcon.setClickable(showSecondary);
679 sc.divider.setVisibility(showSecondary ? View.VISIBLE : View.GONE);
680 sc.secondaryIcon.setVisibility(showSecondary ? View.VISIBLE : View.GONE);
681 if (showSecondary) {
682 sc.secondaryIcon.setOnClickListener(new OnClickListener() {
683 @Override
684 public void onClick(View v) {
685 mSecondaryIconTransition.start(sc);
686 }
687 });
688 }
Alan Viverette494fb7b2014-04-10 18:12:56 -0700689 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700690 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400691 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700692 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800693 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700694 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800695 }
696 }
697
John Spurlock661f2cf2014-11-17 10:29:10 -0500698 private void toggleRinger(StreamControl sc) {
699 if (!mHasVibrator) return;
700 if (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_NORMAL) {
701 mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_VIBRATE);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400702 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
703 } else {
John Spurlock661f2cf2014-11-17 10:29:10 -0500704 mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400705 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
706 }
707 }
708
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800709 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400710 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800711
Alan Viverette494fb7b2014-04-10 18:12:56 -0700712 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800713 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400714 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800715 mActiveStreamType = -1;
716 } else {
John Spurlock86005342014-05-23 11:58:00 -0400717 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800718 mActiveStreamType = activeStreamType;
719 active.group.setVisibility(View.VISIBLE);
720 updateSlider(active);
John Spurlock8845da72014-07-07 21:29:48 -0400721 updateTimeoutDelay();
John Spurlock45601d62014-08-07 17:40:50 -0400722 updateZenPanelVisible();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800723 }
724 }
725
John Spurlockad494bc2014-07-19 15:56:19 -0400726 private void updateSliderProgress(StreamControl sc, int progress) {
727 final boolean isRinger = isNotificationOrRing(sc.streamType);
John Spurlock661f2cf2014-11-17 10:29:10 -0500728 if (isRinger && mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_SILENT) {
John Spurlockad494bc2014-07-19 15:56:19 -0400729 progress = mLastRingerProgress;
730 }
731 if (progress < 0) {
732 progress = getStreamVolume(sc.streamType);
733 }
734 sc.seekbarView.setProgress(progress);
735 if (isRinger) {
736 mLastRingerProgress = progress;
737 }
738 }
739
740 private void updateSliderIcon(StreamControl sc, boolean muted) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500741 ComponentName suppressor = null;
John Spurlockad494bc2014-07-19 15:56:19 -0400742 if (isNotificationOrRing(sc.streamType)) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500743 suppressor = mNotificationEffectsSuppressor;
744 int ringerMode = mAudioManager.getRingerModeInternal();
John Spurlockad494bc2014-07-19 15:56:19 -0400745 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
746 ringerMode = mLastRingerMode;
747 } else {
748 mLastRingerMode = ringerMode;
749 }
John Spurlock661f2cf2014-11-17 10:29:10 -0500750 if (mHasVibrator) {
751 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
752 } else {
753 muted = false;
754 }
John Spurlockad494bc2014-07-19 15:56:19 -0400755 }
John Spurlock661f2cf2014-11-17 10:29:10 -0500756 sc.icon.setImageResource(mDemoIcon != 0 ? mDemoIcon
757 : suppressor != null ? sc.iconSuppressedRes
758 : muted ? sc.iconMuteRes
759 : sc.iconRes);
John Spurlockad494bc2014-07-19 15:56:19 -0400760 }
761
John Spurlock661f2cf2014-11-17 10:29:10 -0500762 private void updateSliderSuppressor(StreamControl sc) {
John Spurlockb4782522014-08-22 14:54:46 -0400763 final ComponentName suppressor = isNotificationOrRing(sc.streamType)
John Spurlock661f2cf2014-11-17 10:29:10 -0500764 ? mNotificationEffectsSuppressor : null;
John Spurlockb4782522014-08-22 14:54:46 -0400765 if (suppressor == null) {
766 sc.seekbarView.setVisibility(View.VISIBLE);
767 sc.suppressorView.setVisibility(View.GONE);
768 } else {
769 sc.seekbarView.setVisibility(View.GONE);
770 sc.suppressorView.setVisibility(View.VISIBLE);
John Spurlock96e7f0e2014-11-14 14:35:01 -0500771 sc.suppressorView.setText(mContext.getString(R.string.muted_by,
John Spurlockb4782522014-08-22 14:54:46 -0400772 getSuppressorCaption(suppressor)));
John Spurlockb4782522014-08-22 14:54:46 -0400773 }
774 }
775
776 private String getSuppressorCaption(ComponentName suppressor) {
777 final PackageManager pm = mContext.getPackageManager();
778 try {
779 final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
780 if (info != null) {
781 final CharSequence seq = info.loadLabel(pm);
782 if (seq != null) {
783 final String str = seq.toString().trim();
784 if (str.length() > 0) {
785 return str;
786 }
787 }
788 }
789 } catch (Throwable e) {
790 Log.w(TAG, "Error loading suppressor caption", e);
791 }
792 return suppressor.getPackageName();
793 }
794
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800795 /** Update the mute and progress state of a slider */
796 private void updateSlider(StreamControl sc) {
John Spurlockad494bc2014-07-19 15:56:19 -0400797 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800798 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800799 // Force reloading the image resource
800 sc.icon.setImageDrawable(null);
John Spurlockad494bc2014-07-19 15:56:19 -0400801 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400802 updateSliderEnabled(sc, muted, false);
John Spurlock661f2cf2014-11-17 10:29:10 -0500803 updateSliderSuppressor(sc);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400804 }
805
John Spurlock5f640e42014-05-31 20:15:59 -0400806 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
807 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400808 final boolean isRinger = isNotificationOrRing(sc.streamType);
RoboErik2811dd32014-08-12 09:48:13 -0700809 if (sc.streamType == STREAM_REMOTE_MUSIC) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700810 // never disable touch interactions for remote playback, the muting is not tied to
811 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700812 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlock661f2cf2014-11-17 10:29:10 -0500813 } else if (isRinger && mNotificationEffectsSuppressor != null) {
814 sc.icon.setEnabled(true);
815 sc.icon.setAlpha(1f);
816 sc.icon.setClickable(false);
817 } else if (isRinger
818 && mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400819 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400820 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400821 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400822 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400823 } else if (fixedVolume ||
John Spurlock5eb21f32014-11-19 11:54:53 -0500824 (sc.streamType != mAudioManager.getMasterStreamType() && !isRinger && muted) ||
John Spurlock4f0f1202014-08-05 13:28:33 -0400825 (sSafetyWarning != null)) {
826 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700827 } else {
828 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400829 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400830 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700831 }
John Spurlockae641c92014-06-30 18:11:40 -0400832 // show the silent hint when the disabled slider is touched in silent mode
833 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400834 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400835 sc.group.setOnTouchListener(null);
John Spurlock661f2cf2014-11-17 10:29:10 -0500836 sc.icon.setClickable(mHasVibrator);
John Spurlock5f640e42014-05-31 20:15:59 -0400837 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400838 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400839 @Override
840 public boolean onTouch(View v, MotionEvent event) {
841 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400842 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400843 return false;
844 }
John Spurlockae641c92014-06-30 18:11:40 -0400845 };
846 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400847 }
848 }
849 }
850
John Spurlockae641c92014-06-30 18:11:40 -0400851 private void showSilentHint() {
852 if (mZenPanel != null) {
853 mZenPanel.showSilentHint();
854 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800855 }
856
John Spurlock661f2cf2014-11-17 10:29:10 -0500857 private void showVibrateHint() {
858 final StreamControl active = mStreamControls.get(mActiveStreamType);
859 if (active != null) {
860 mIconPulser.start(active.icon);
861 if (!hasMessages(MSG_VIBRATE)) {
862 sendEmptyMessageDelayed(MSG_VIBRATE, VIBRATE_DELAY);
863 }
864 }
865 }
866
John Spurlock7f1df5e2014-05-31 19:11:40 -0400867 private static boolean isNotificationOrRing(int streamType) {
868 return streamType == AudioManager.STREAM_RING
869 || streamType == AudioManager.STREAM_NOTIFICATION;
870 }
871
John Spurlockae641c92014-06-30 18:11:40 -0400872 public void setCallback(Callback callback) {
873 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400874 }
875
John Spurlock8845da72014-07-07 21:29:48 -0400876 private void updateTimeoutDelay() {
John Spurlockbb4a7022014-11-08 12:40:19 -0500877 mTimeoutDelay = mDemoIcon != 0 ? TIMEOUT_DELAY_EXPANDED
878 : sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
John Spurlock35134602014-07-24 18:10:48 -0400879 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400880 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
881 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
882 : TIMEOUT_DELAY;
883 }
884
885 private boolean isZenPanelVisible() {
886 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400887 }
888
John Spurlockae641c92014-06-30 18:11:40 -0400889 private void setZenPanelVisible(boolean visible) {
890 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400891 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400892 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400893 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400894 resetTimeout();
895 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400896 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800897 }
John Spurlockea9938c2014-07-11 18:51:32 -0400898 if (changing) {
899 updateTimeoutDelay();
900 resetTimeout();
901 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800902 }
903
Eric Laurentfde16d52012-12-03 14:42:39 -0800904 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400905 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800906 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400907 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800908 updateSlider(sc);
909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
John Spurlock45601d62014-08-07 17:40:50 -0400912 private void updateZenPanelVisible() {
913 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400914 }
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public void postVolumeChanged(int streamType, int flags) {
917 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700918 synchronized (this) {
919 if (mStreamControls == null) {
920 createSliders();
921 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 removeMessages(MSG_FREE_RESOURCES);
924 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
925 }
926
RoboErik19c95182014-06-23 15:38:48 -0700927 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700928 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
929 synchronized (this) {
930 if (mStreamControls == null) {
931 createSliders();
932 }
933 }
934 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700935 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700936 }
937
938 public void postRemoteSliderVisibility(boolean visible) {
939 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
RoboErik2811dd32014-08-12 09:48:13 -0700940 STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700941 }
942
943 /**
944 * Called by AudioService when it has received new remote playback information that
945 * would affect the VolumePanel display (mainly volumes). The difference with
946 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
947 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
948 * displayed.
949 * This special code path is due to the fact that remote volume updates arrive to AudioService
950 * asynchronously. So after AudioService has sent the volume update (which should be treated
951 * as a request to update the volume), the application will likely set a new volume. If the UI
952 * is still up, we need to refresh the display to show this new value.
953 */
954 public void postHasNewRemotePlaybackInfo() {
955 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
956 // don't create or prevent resources to be freed, if they disappear, this update came too
957 // late and shouldn't warrant the panel to be displayed longer
958 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
959 }
960
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400961 public void postMasterVolumeChanged(int flags) {
962 postVolumeChanged(STREAM_MASTER, flags);
963 }
964
Mike Lockwoodce952c82011-11-14 10:47:42 -0800965 public void postMuteChanged(int streamType, int flags) {
966 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700967 synchronized (this) {
968 if (mStreamControls == null) {
969 createSliders();
970 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800971 }
972 removeMessages(MSG_FREE_RESOURCES);
973 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
974 }
975
976 public void postMasterMuteChanged(int flags) {
977 postMuteChanged(STREAM_MASTER, flags);
978 }
979
Eric Laurentfde16d52012-12-03 14:42:39 -0800980 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700981 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800982 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700983 }
984
John Spurlock1dad2722014-07-11 11:07:53 -0400985 public void postDismiss(long delay) {
986 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400987 }
988
989 public void postLayoutDirection(int layoutDirection) {
990 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400991 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400992 }
993
John Spurlock661f2cf2014-11-17 10:29:10 -0500994 public void postInternalRingerModeChanged(int mode) {
995 removeMessages(MSG_INTERNAL_RINGER_MODE_CHANGED);
996 obtainMessage(MSG_INTERNAL_RINGER_MODE_CHANGED, mode, 0).sendToTarget();
997 }
998
999 private static String flagsToString(int flags) {
1000 return flags == 0 ? "0" : (flags + "=" + AudioManager.flagsToString(flags));
1001 }
1002
1003 private static String streamToString(int stream) {
1004 return AudioService.streamToString(stream);
1005 }
1006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 /**
1008 * Override this if you have other work to do when the volume changes (for
1009 * example, vibrating, playing a sound, etc.). Make sure to call through to
1010 * the superclass implementation.
1011 */
1012 protected void onVolumeChanged(int streamType, int flags) {
1013
John Spurlock661f2cf2014-11-17 10:29:10 -05001014 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamToString(streamType)
1015 + ", flags: " + flagsToString(flags) + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016
1017 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -07001018 synchronized (this) {
1019 if (mActiveStreamType != streamType) {
1020 reorderSliders(streamType);
1021 }
RoboErik19c95182014-06-23 15:38:48 -07001022 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -08001023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025
Marco Nelissen69f593c2009-07-28 09:55:04 -07001026 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 removeMessages(MSG_PLAY_SOUND);
1028 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
1029 }
1030
1031 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
1032 removeMessages(MSG_PLAY_SOUND);
1033 removeMessages(MSG_VIBRATE);
1034 onStopSounds();
1035 }
1036
1037 removeMessages(MSG_FREE_RESOURCES);
1038 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001039 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
Mike Lockwoodce952c82011-11-14 10:47:42 -08001042 protected void onMuteChanged(int streamType, int flags) {
1043
John Spurlock661f2cf2014-11-17 10:29:10 -05001044 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamToString(streamType)
1045 + ", flags: " + flagsToString(flags) + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -08001046
1047 StreamControl sc = mStreamControls.get(streamType);
1048 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -04001049 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -08001050 }
1051
1052 onVolumeChanged(streamType, flags);
1053 }
1054
RoboErik19c95182014-06-23 15:38:48 -07001055 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -07001056 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -08001057
Marco Nelissen69f593c2009-07-28 09:55:04 -07001058 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059
1060 if (LOGD) {
John Spurlock661f2cf2014-11-17 10:29:10 -05001061 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamToString(streamType)
1062 + ", flags: " + flagsToString(flags) + "), index: " + index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064
1065 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001066
Mike Lockwood8dc1dab2011-10-27 09:52:41 -04001067 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -07001068 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
1070 switch (streamType) {
1071
1072 case AudioManager.STREAM_RING: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001073 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1074 mContext, RingtoneManager.TYPE_RINGTONE);
1075 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001076 mRingIsSilent = true;
1077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 break;
1079 }
1080
1081 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001082 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -08001083 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
1084 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
1085 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1086 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
John Spurlockbb4a7022014-11-08 12:40:19 -05001087 setMusicIcon(IC_AUDIO_BT, IC_AUDIO_BT_MUTE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 } else {
John Spurlock4bebb1d2014-10-02 19:17:19 -04001089 setMusicIcon(IC_AUDIO_VOL, IC_AUDIO_VOL_MUTE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091 break;
1092 }
1093
1094 case AudioManager.STREAM_VOICE_CALL: {
1095 /*
1096 * For in-call voice call volume, there is no inaudible volume.
1097 * Rescale the UI control so the progress bar doesn't go all
1098 * the way to zero and don't show the mute icon.
1099 */
1100 index++;
1101 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 break;
1103 }
1104
1105 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 break;
1107 }
1108
1109 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001110 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1111 mContext, RingtoneManager.TYPE_NOTIFICATION);
1112 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001113 mRingIsSilent = true;
1114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 break;
1116 }
1117
1118 case AudioManager.STREAM_BLUETOOTH_SCO: {
1119 /*
1120 * For in-call voice call volume, there is no inaudible volume.
1121 * Rescale the UI control so the progress bar doesn't go all
1122 * the way to zero and don't show the mute icon.
1123 */
1124 index++;
1125 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 break;
1127 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001128
RoboErik2811dd32014-08-12 09:48:13 -07001129 case STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -07001130 if (controller == null && sc != null) {
1131 // If we weren't passed one try using the last one set.
1132 controller = sc.controller;
1133 }
1134 if (controller == null) {
1135 // We still don't have one, ignore the command.
1136 Log.w(mTag, "sent remote volume change without a controller!");
1137 } else {
RoboErikd2b8c942014-08-19 11:23:40 -07001138 PlaybackInfo vi = controller.getPlaybackInfo();
RoboErik19c95182014-06-23 15:38:48 -07001139 index = vi.getCurrentVolume();
1140 max = vi.getMaxVolume();
1141 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
1142 // if the remote volume is fixed add the flag for the UI
1143 flags |= AudioManager.FLAG_FIXED_VOLUME;
1144 }
1145 }
John Spurlock86005342014-05-23 11:58:00 -04001146 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001147 break;
1148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001151 if (sc != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001152 if (streamType == STREAM_REMOTE_MUSIC && controller != sc.controller) {
RoboErik19c95182014-06-23 15:38:48 -07001153 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001154 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001155 }
1156 sc.controller = controller;
1157 if (controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001158 sc.controller.registerCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001159 }
1160 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001161 if (sc.seekbarView.getMax() != max) {
1162 sc.seekbarView.setMax(max);
1163 }
John Spurlockad494bc2014-07-19 15:56:19 -04001164 updateSliderProgress(sc, index);
John Spurlock661f2cf2014-11-17 10:29:10 -05001165 final boolean muted = isMuted(streamType);
1166 updateSliderEnabled(sc, muted, (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
1167 if (isNotificationOrRing(streamType)) {
1168 // check for secondary-icon transition completion
1169 if (mSecondaryIconTransition.isRunning()) {
1170 mSecondaryIconTransition.cancel(); // safe to reset
1171 sc.seekbarView.setAlpha(0); sc.seekbarView.animate().alpha(1);
1172 mZenPanel.setAlpha(0); mZenPanel.animate().alpha(1);
1173 }
1174 updateSliderIcon(sc, muted);
John Spurlockbb4a7022014-11-08 12:40:19 -05001175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177
John Spurlock86005342014-05-23 11:58:00 -04001178 if (!isShowing()) {
RoboErik2811dd32014-08-12 09:48:13 -07001179 int stream = (streamType == STREAM_REMOTE_MUSIC) ? -1 : streamType;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001180 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1181 mAudioManager.forceVolumeControlStream(stream);
John Spurlockeb2727b2014-07-19 23:11:36 -04001182 mDialog.show();
1183 if (mCallback != null) {
1184 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001185 }
Selim Cinek62ea3402014-09-08 12:11:51 +02001186 announceDialogShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 // Do a little vibrate if applicable (only when going into vibrate mode)
RoboErik2811dd32014-08-12 09:48:13 -07001190 if ((streamType != STREAM_REMOTE_MUSIC) &&
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001191 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock661f2cf2014-11-17 10:29:10 -05001192 isNotificationOrRing(streamType) &&
1193 mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1195 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001196
John Spurlock661f2cf2014-11-17 10:29:10 -05001197 // Pulse the zen icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001198 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1199 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001200 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001201
1202 // Pulse the slider icon & vibrate if an adjustment down was suppressed due to vibrate mode.
1203 if ((flags & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) {
1204 showVibrateHint();
1205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
Selim Cinek62ea3402014-09-08 12:11:51 +02001208 private void announceDialogShown() {
1209 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
1210 }
1211
John Spurlock86005342014-05-23 11:58:00 -04001212 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001213 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001214 }
1215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 protected void onPlaySound(int streamType, int flags) {
1217
1218 if (hasMessages(MSG_STOP_SOUNDS)) {
1219 removeMessages(MSG_STOP_SOUNDS);
1220 // Force stop right now
1221 onStopSounds();
1222 }
1223
1224 synchronized (this) {
1225 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001226 if (toneGen != null) {
1227 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1228 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232
1233 protected void onStopSounds() {
1234
1235 synchronized (this) {
1236 int numStreamTypes = AudioSystem.getNumStreamTypes();
1237 for (int i = numStreamTypes - 1; i >= 0; i--) {
1238 ToneGenerator toneGen = mToneGenerators[i];
1239 if (toneGen != null) {
1240 toneGen.stopTone();
1241 }
1242 }
1243 }
1244 }
1245
1246 protected void onVibrate() {
1247
1248 // Make sure we ended up in vibrate ringer mode
John Spurlock661f2cf2014-11-17 10:29:10 -05001249 if (mAudioManager.getRingerModeInternal() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 return;
1251 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001252 if (mVibrator != null) {
1253 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
1254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256
RoboErik19c95182014-06-23 15:38:48 -07001257 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
John Spurlock661f2cf2014-11-17 10:29:10 -05001258 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: "
1259 + flagsToString(flags) + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001260
John Spurlock86005342014-05-23 11:58:00 -04001261 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001262 synchronized (this) {
RoboErik2811dd32014-08-12 09:48:13 -07001263 if (mActiveStreamType != STREAM_REMOTE_MUSIC) {
1264 reorderSliders(STREAM_REMOTE_MUSIC);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001265 }
RoboErik2811dd32014-08-12 09:48:13 -07001266 onShowVolumeChanged(STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001267 }
1268 } else {
John Spurlock86005342014-05-23 11:58:00 -04001269 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001270 }
1271
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001272 removeMessages(MSG_FREE_RESOURCES);
1273 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001274 resetTimeout();
1275 }
1276
1277 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001278 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1279 if (isShowing()
RoboErik2811dd32014-08-12 09:48:13 -07001280 && (mActiveStreamType == STREAM_REMOTE_MUSIC)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001281 && (mStreamControls != null)) {
RoboErik2811dd32014-08-12 09:48:13 -07001282 onShowVolumeChanged(STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001283 }
1284 }
1285
RoboErik19c95182014-06-23 15:38:48 -07001286 /**
1287 * Clear the current remote stream controller.
1288 */
1289 private void clearRemoteStreamController() {
1290 if (mStreamControls != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001291 StreamControl sc = mStreamControls.get(STREAM_REMOTE_MUSIC);
RoboErik19c95182014-06-23 15:38:48 -07001292 if (sc != null) {
1293 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001294 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001295 sc.controller = null;
1296 }
1297 }
1298 }
1299 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001300
1301 /**
RoboErik2811dd32014-08-12 09:48:13 -07001302 * Handler for MSG_SLIDER_VISIBILITY_CHANGED Hide or show a slider
1303 *
1304 * @param streamType can be a valid stream type value, or
1305 * VolumePanel.STREAM_MASTER, or VolumePanel.STREAM_REMOTE_MUSIC
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001306 * @param visible
1307 */
1308 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001309 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001310 boolean isVisible = (visible == 1);
1311 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1312 StreamResources streamRes = STREAMS[i];
1313 if (streamRes.streamType == streamType) {
1314 streamRes.show = isVisible;
1315 if (!isVisible && (mActiveStreamType == streamType)) {
1316 mActiveStreamType = -1;
1317 }
1318 break;
1319 }
1320 }
1321 }
1322
Eric Laurentfde16d52012-12-03 14:42:39 -08001323 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001324 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1325 || isShowing()) {
1326 synchronized (sSafetyWarningLock) {
1327 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001328 return;
1329 }
John Spurlock35134602014-07-24 18:10:48 -04001330 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1331 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001332 }
1333 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001334 }
Selim Cinek1cf89062014-08-29 19:21:44 +02001335 if (mAccessibilityManager.isTouchExplorationEnabled()) {
1336 removeMessages(MSG_TIMEOUT);
1337 } else {
1338 updateTimeoutDelay();
1339 resetTimeout();
1340 }
Eric Laurentc34dcc12012-09-10 13:51:52 -07001341 }
1342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 /**
1344 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1345 */
1346 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001347 if (streamType == STREAM_MASTER) {
1348 // For devices that use the master volume setting only but still want to
1349 // play a volume-changed tone, direct the master volume pseudostream to
1350 // the system stream's tone generator.
1351 if (mPlayMasterStreamTones) {
1352 streamType = AudioManager.STREAM_SYSTEM;
1353 } else {
1354 return null;
1355 }
1356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 synchronized (this) {
1358 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001359 try {
1360 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1361 } catch (RuntimeException e) {
1362 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001363 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001364 + "RuntimeException: " + e);
1365 }
1366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001368 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370 }
1371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372
1373 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001374 * Switch between icons because Bluetooth music is same as music volume, but with
1375 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001377 private void setMusicIcon(int resId, int resMuteId) {
1378 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1379 if (sc != null) {
1380 sc.iconRes = resId;
1381 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001382 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385
1386 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 synchronized (this) {
1388 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1389 if (mToneGenerators[i] != null) {
1390 mToneGenerators[i].release();
1391 }
1392 mToneGenerators[i] = null;
1393 }
1394 }
1395 }
1396
1397 @Override
1398 public void handleMessage(Message msg) {
1399 switch (msg.what) {
1400
1401 case MSG_VOLUME_CHANGED: {
1402 onVolumeChanged(msg.arg1, msg.arg2);
1403 break;
1404 }
1405
Mike Lockwoodce952c82011-11-14 10:47:42 -08001406 case MSG_MUTE_CHANGED: {
1407 onMuteChanged(msg.arg1, msg.arg2);
1408 break;
1409 }
1410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 case MSG_FREE_RESOURCES: {
1412 onFreeResources();
1413 break;
1414 }
1415
1416 case MSG_STOP_SOUNDS: {
1417 onStopSounds();
1418 break;
1419 }
1420
1421 case MSG_PLAY_SOUND: {
1422 onPlaySound(msg.arg1, msg.arg2);
1423 break;
1424 }
1425
1426 case MSG_VIBRATE: {
1427 onVibrate();
1428 break;
1429 }
1430
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001431 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001432 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001433 mDialog.dismiss();
1434 clearRemoteStreamController();
1435 mActiveStreamType = -1;
1436 if (mCallback != null) {
1437 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001438 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001439 }
John Spurlock35134602014-07-24 18:10:48 -04001440 synchronized (sSafetyWarningLock) {
1441 if (sSafetyWarning != null) {
1442 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1443 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001444 }
1445 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001446 break;
1447 }
John Spurlockb4782522014-08-22 14:54:46 -04001448
John Spurlock661f2cf2014-11-17 10:29:10 -05001449 case MSG_ZEN_MODE_CHANGED:
John Spurlockb4782522014-08-22 14:54:46 -04001450 case MSG_RINGER_MODE_CHANGED:
John Spurlock661f2cf2014-11-17 10:29:10 -05001451 case MSG_INTERNAL_RINGER_MODE_CHANGED:
John Spurlockb4782522014-08-22 14:54:46 -04001452 case MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001453 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001454 updateStates();
1455 }
1456 break;
1457 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001458
1459 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001460 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001461 break;
1462 }
1463
1464 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1465 onRemoteVolumeUpdateIfShown();
1466 break;
1467
1468 case MSG_SLIDER_VISIBILITY_CHANGED:
1469 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1470 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001471
1472 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001473 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001474 break;
John Spurlock86005342014-05-23 11:58:00 -04001475
1476 case MSG_LAYOUT_DIRECTION:
1477 setLayoutDirection(msg.arg1);
1478 break;
1479
John Spurlock45601d62014-08-07 17:40:50 -04001480 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1481 mZenModeAvailable = msg.arg1 != 0;
1482 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001483 break;
1484
1485 case MSG_USER_ACTIVITY:
1486 if (mCallback != null) {
1487 mCallback.onInteraction();
1488 }
John Spurlock86005342014-05-23 11:58:00 -04001489 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
1492
John Spurlockae641c92014-06-30 18:11:40 -04001493 private void resetTimeout() {
John Spurlocka0457c22014-09-26 13:22:08 -04001494 final boolean touchExploration = mAccessibilityManager.isTouchExplorationEnabled();
John Spurlockea9938c2014-07-11 18:51:32 -04001495 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
John Spurlocka0457c22014-09-26 13:22:08 -04001496 + " delay=" + mTimeoutDelay + " touchExploration=" + touchExploration);
1497 if (sSafetyWarning == null || !touchExploration) {
Selim Cinek1cf89062014-08-29 19:21:44 +02001498 removeMessages(MSG_TIMEOUT);
1499 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
1500 removeMessages(MSG_USER_ACTIVITY);
1501 sendEmptyMessage(MSG_USER_ACTIVITY);
1502 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001503 }
1504
John Spurlock1dad2722014-07-11 11:07:53 -04001505 private void forceTimeout(long delay) {
John Spurlocka0457c22014-09-26 13:22:08 -04001506 if (LOGD) Log.d(mTag, "forceTimeout delay=" + delay + " callers=" + Debug.getCallers(3));
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001507 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001508 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001509 }
1510
1511 public ZenModeController getZenController() {
1512 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001513 }
1514
John Spurlockbb4a7022014-11-08 12:40:19 -05001515 @Override
1516 public void dispatchDemoCommand(String command, Bundle args) {
1517 if (!COMMAND_VOLUME.equals(command)) return;
1518 String icon = args.getString("icon");
1519 final String iconMute = args.getString("iconmute");
1520 final boolean mute = iconMute != null;
1521 icon = mute ? iconMute : icon;
1522 icon = icon.endsWith("Stream") ? icon : (icon + "Stream");
1523 final StreamResources sr = StreamResources.valueOf(icon);
1524 mDemoIcon = mute ? sr.iconMuteRes : sr.iconRes;
1525 final int forcedStreamType = StreamResources.MediaStream.streamType;
1526 mAudioManager.forceVolumeControlStream(forcedStreamType);
1527 mAudioManager.adjustStreamVolume(forcedStreamType, AudioManager.ADJUST_SAME,
1528 AudioManager.FLAG_SHOW_UI);
1529 }
1530
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001531 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1532 @Override
1533 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1534 final Object tag = seekBar.getTag();
1535 if (fromUser && tag instanceof StreamControl) {
1536 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001537 setStreamVolume(sc, progress,
1538 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001539 }
1540 resetTimeout();
1541 }
1542
1543 @Override
1544 public void onStartTrackingTouch(SeekBar seekBar) {
1545 }
1546
1547 @Override
1548 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001549 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001550 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001551
John Spurlock86005342014-05-23 11:58:00 -04001552 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockb4782522014-08-22 14:54:46 -04001553 @Override
John Spurlock45601d62014-08-07 17:40:50 -04001554 public void onZenAvailableChanged(boolean available) {
1555 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001556 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001557
John Spurlockb4782522014-08-22 14:54:46 -04001558 @Override
1559 public void onEffectsSupressorChanged() {
John Spurlock661f2cf2014-11-17 10:29:10 -05001560 mNotificationEffectsSuppressor = mZenController.getEffectsSuppressor();
1561 sendEmptyMessage(MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED);
1562 }
1563
1564 public void onZenChanged(int zen) {
1565 sendEmptyMessage(MSG_ZEN_MODE_CHANGED);
John Spurlockb4782522014-08-22 14:54:46 -04001566 }
John Spurlock86005342014-05-23 11:58:00 -04001567 };
RoboErik19c95182014-06-23 15:38:48 -07001568
1569 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
RoboErikd2b8c942014-08-19 11:23:40 -07001570 public void onAudioInfoChanged(PlaybackInfo info) {
RoboErik19c95182014-06-23 15:38:48 -07001571 onRemoteVolumeUpdateIfShown();
1572 }
1573 };
John Spurlockae641c92014-06-30 18:11:40 -04001574
John Spurlockbb4a7022014-11-08 12:40:19 -05001575 private final class SecondaryIconTransition extends AnimatorListenerAdapter
1576 implements Runnable {
1577 private static final int ANIMATION_TIME = 400;
1578 private static final int WAIT_FOR_SWITCH_TIME = 1000;
1579
1580 private final int mAnimationTime = (int)(ANIMATION_TIME * ValueAnimator.getDurationScale());
1581 private final int mFadeOutTime = mAnimationTime / 2;
1582 private final int mDelayTime = mAnimationTime / 3;
1583
1584 private final Interpolator mIconInterpolator =
1585 AnimationUtils.loadInterpolator(mContext, android.R.interpolator.fast_out_slow_in);
1586
1587 private StreamControl mTarget;
1588
1589 public void start(StreamControl sc) {
1590 if (sc == null) throw new IllegalArgumentException();
1591 if (mTarget != null) {
1592 cancel();
1593 }
1594 mTarget = sc;
1595 mTimeoutDelay = mAnimationTime + WAIT_FOR_SWITCH_TIME;
1596 resetTimeout();
1597 mTarget.secondaryIcon.setClickable(false);
1598 final int N = mTarget.group.getChildCount();
1599 for (int i = 0; i < N; i++) {
1600 final View child = mTarget.group.getChildAt(i);
1601 if (child != mTarget.secondaryIcon) {
1602 child.animate().alpha(0).setDuration(mFadeOutTime).start();
1603 }
1604 }
1605 mTarget.secondaryIcon.animate()
1606 .translationXBy(mTarget.icon.getX() - mTarget.secondaryIcon.getX())
1607 .setInterpolator(mIconInterpolator)
1608 .setStartDelay(mDelayTime)
1609 .setDuration(mAnimationTime - mDelayTime)
1610 .setListener(this)
1611 .start();
1612 }
1613
1614 public boolean isRunning() {
1615 return mTarget != null;
1616 }
1617
1618 public void cancel() {
1619 if (mTarget == null) return;
1620 mTarget.secondaryIcon.setClickable(true);
1621 final int N = mTarget.group.getChildCount();
1622 for (int i = 0; i < N; i++) {
1623 final View child = mTarget.group.getChildAt(i);
1624 if (child != mTarget.secondaryIcon) {
1625 child.animate().cancel();
1626 child.setAlpha(1);
1627 }
1628 }
1629 mTarget.secondaryIcon.animate().cancel();
1630 mTarget.secondaryIcon.setTranslationX(0);
1631 mTarget = null;
1632 }
1633
1634 @Override
1635 public void onAnimationEnd(Animator animation) {
1636 if (mTarget == null) return;
1637 AsyncTask.execute(this);
1638 }
1639
1640 @Override
1641 public void run() {
1642 if (mTarget == null) return;
1643 mAudioManager.forceVolumeControlStream(StreamResources.NotificationStream.streamType);
1644 mAudioManager.adjustStreamVolume(StreamResources.NotificationStream.streamType,
1645 AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
1646 }
1647 }
1648
John Spurlockae641c92014-06-30 18:11:40 -04001649 public interface Callback {
1650 void onZenSettings();
1651 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001652 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654}