blob: 0ad92b3608746be05f729222f74f40c9c1e444fc [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070041import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070043import android.media.VolumeProvider;
44import android.media.session.MediaController;
RoboErikd2b8c942014-08-19 11:23:40 -070045import android.media.session.MediaController.PlaybackInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070046import android.net.Uri;
John Spurlockbb4a7022014-11-08 12:40:19 -050047import android.os.AsyncTask;
48import android.os.Bundle;
John Spurlocka0457c22014-09-26 13:22:08 -040049import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Handler;
51import android.os.Message;
52import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040054import android.util.SparseArray;
John Spurlock2d28d6e2014-08-01 13:10:14 -040055import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040056import android.view.LayoutInflater;
57import android.view.MotionEvent;
58import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040059import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040060import android.view.ViewGroup;
61import android.view.Window;
62import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070063import android.view.WindowManager.LayoutParams;
Selim Cinek62ea3402014-09-08 12:11:51 +020064import android.view.accessibility.AccessibilityEvent;
Selim Cinek9f6ceb12014-08-29 16:28:39 +020065import android.view.accessibility.AccessibilityManager;
John Spurlockbb4a7022014-11-08 12:40:19 -050066import android.view.animation.AnimationUtils;
67import android.view.animation.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080069import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080070import android.widget.SeekBar.OnSeekBarChangeListener;
John Spurlockb4782522014-08-22 14:54:46 -040071import android.widget.TextView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080072
John Spurlock86005342014-05-23 11:58:00 -040073import com.android.internal.R;
John Spurlockbb4a7022014-11-08 12:40:19 -050074import com.android.systemui.DemoMode;
John Spurlock35134602014-07-24 18:10:48 -040075import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040076import com.android.systemui.statusbar.policy.ZenModeController;
77
John Spurlockad494bc2014-07-19 15:56:19 -040078import java.io.FileDescriptor;
79import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81/**
John Spurlock3346a802014-05-20 16:25:37 -040082 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * @hide
85 */
John Spurlockbb4a7022014-11-08 12:40:19 -050086public class VolumePanel extends Handler implements DemoMode {
John Spurlockae641c92014-06-30 18:11:40 -040087 private static final String TAG = "VolumePanel";
88 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
John Spurlock61560172015-02-06 19:46:04 -050090 private static final int PLAY_SOUND_DELAY = AudioSystem.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 /**
93 * The delay before vibrating. This small period exists so if the user is
94 * moving to silent mode, it will not emit a short vibrate (it normally
95 * would since vibrate is between normal mode and silent mode using hardware
96 * keys).
97 */
98 public static final int VIBRATE_DELAY = 300;
99
100 private static final int VIBRATE_DURATION = 300;
101 private static final int BEEP_DURATION = 150;
102 private static final int MAX_VOLUME = 100;
103 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800104 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -0400105 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -0400106 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -0400107 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400108 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 private static final int MSG_VOLUME_CHANGED = 0;
111 private static final int MSG_FREE_RESOURCES = 1;
112 private static final int MSG_PLAY_SOUND = 2;
113 private static final int MSG_STOP_SOUNDS = 3;
114 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800115 private static final int MSG_TIMEOUT = 5;
116 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -0800117 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700118 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
119 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
120 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700121 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -0400122 private static final int MSG_LAYOUT_DIRECTION = 12;
John Spurlock45601d62014-08-07 17:40:50 -0400123 private static final int MSG_ZEN_MODE_AVAILABLE_CHANGED = 13;
John Spurlockae641c92014-06-30 18:11:40 -0400124 private static final int MSG_USER_ACTIVITY = 14;
John Spurlockb4782522014-08-22 14:54:46 -0400125 private static final int MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED = 15;
John Spurlock530052a2014-11-30 16:26:19 -0500126 private static final int MSG_INTERNAL_RINGER_MODE_CHANGED = 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400128 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800129 private static final int STREAM_MASTER = -100;
RoboErik2811dd32014-08-12 09:48:13 -0700130 // Pseudo stream type for remote volume
131 private static final int STREAM_REMOTE_MUSIC = -200;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400132
John Spurlock7b414672014-07-18 13:02:39 -0400133 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
134 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
135 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
136 .build();
137
John Spurlock4bebb1d2014-10-02 19:17:19 -0400138 private static final int IC_AUDIO_VOL = com.android.systemui.R.drawable.ic_audio_vol;
139 private static final int IC_AUDIO_VOL_MUTE = com.android.systemui.R.drawable.ic_audio_vol_mute;
John Spurlockbb4a7022014-11-08 12:40:19 -0500140 private static final int IC_AUDIO_BT = com.android.systemui.R.drawable.ic_audio_bt;
141 private static final int IC_AUDIO_BT_MUTE = com.android.systemui.R.drawable.ic_audio_bt_mute;
John Spurlock4bebb1d2014-10-02 19:17:19 -0400142
John Spurlock86005342014-05-23 11:58:00 -0400143 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400144 protected final Context mContext;
145 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400146 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700147 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700148 private boolean mVoiceCapable;
John Spurlock45601d62014-08-07 17:40:50 -0400149 private boolean mZenModeAvailable;
John Spurlock8845da72014-07-07 21:29:48 -0400150 private boolean mZenPanelExpanded;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400151 private int mTimeoutDelay = TIMEOUT_DELAY;
John Spurlockad494bc2014-07-19 15:56:19 -0400152 private float mDisabledAlpha;
153 private int mLastRingerMode = AudioManager.RINGER_MODE_NORMAL;
154 private int mLastRingerProgress = 0;
John Spurlockbb4a7022014-11-08 12:40:19 -0500155 private int mDemoIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Christopher Tatec4b78d22012-05-22 13:57:58 -0700157 // True if we want to play tones on the system stream when the master stream is specified.
158 private final boolean mPlayMasterStreamTones;
159
John Spurlock86005342014-05-23 11:58:00 -0400160
161 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private final View mView;
John Spurlockeb2727b2014-07-19 23:11:36 -0400163 /** Dialog hosting the panel */
John Spurlock86005342014-05-23 11:58:00 -0400164 private final Dialog mDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800165
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700166 /** The visible portion of the volume overlay */
167 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400168 /** Contains the slider and its touchable icons */
169 private final ViewGroup mSliderPanel;
John Spurlockeb2727b2014-07-19 23:11:36 -0400170 /** The zen mode configuration panel view */
John Spurlock86005342014-05-23 11:58:00 -0400171 private ZenModePanel mZenPanel;
John Spurlock661f2cf2014-11-17 10:29:10 -0500172 /** The component currently suppressing notification stream effects */
173 private ComponentName mNotificationEffectsSuppressor;
John Spurlock86005342014-05-23 11:58:00 -0400174
John Spurlockae641c92014-06-30 18:11:40 -0400175 private Callback mCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800176
177 /** Currently active stream that shows up at the top of the list of sliders */
178 private int mActiveStreamType = -1;
179 /** All the slider controls mapped by stream type */
John Spurlockad494bc2014-07-19 15:56:19 -0400180 private SparseArray<StreamControl> mStreamControls;
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200181 private final AccessibilityManager mAccessibilityManager;
John Spurlockbb4a7022014-11-08 12:40:19 -0500182 private final SecondaryIconTransition mSecondaryIconTransition;
John Spurlock661f2cf2014-11-17 10:29:10 -0500183 private final IconPulser mIconPulser;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800184
Amith Yamasani71def772011-10-12 12:25:24 -0700185 private enum StreamResources {
186 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
187 R.string.volume_icon_description_bluetooth,
John Spurlockbb4a7022014-11-08 12:40:19 -0500188 IC_AUDIO_BT,
189 IC_AUDIO_BT_MUTE,
Amith Yamasani71def772011-10-12 12:25:24 -0700190 false),
191 RingerStream(AudioManager.STREAM_RING,
192 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400193 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlock661f2cf2014-11-17 10:29:10 -0500194 com.android.systemui.R.drawable.ic_ringer_mute,
Amith Yamasani71def772011-10-12 12:25:24 -0700195 false),
196 VoiceStream(AudioManager.STREAM_VOICE_CALL,
197 R.string.volume_icon_description_incall,
John Spurlockbb4a7022014-11-08 12:40:19 -0500198 com.android.systemui.R.drawable.ic_audio_phone,
199 com.android.systemui.R.drawable.ic_audio_phone,
Amith Yamasani71def772011-10-12 12:25:24 -0700200 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700201 AlarmStream(AudioManager.STREAM_ALARM,
202 R.string.volume_alarm,
John Spurlock4bebb1d2014-10-02 19:17:19 -0400203 com.android.systemui.R.drawable.ic_audio_alarm,
204 com.android.systemui.R.drawable.ic_audio_alarm_mute,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700205 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700206 MediaStream(AudioManager.STREAM_MUSIC,
207 R.string.volume_icon_description_media,
John Spurlock4bebb1d2014-10-02 19:17:19 -0400208 IC_AUDIO_VOL,
209 IC_AUDIO_VOL_MUTE,
Amith Yamasani71def772011-10-12 12:25:24 -0700210 true),
211 NotificationStream(AudioManager.STREAM_NOTIFICATION,
212 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400213 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlock661f2cf2014-11-17 10:29:10 -0500214 com.android.systemui.R.drawable.ic_ringer_mute,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400215 true),
216 // for now, use media resources for master volume
217 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700218 R.string.volume_icon_description_media, //FIXME should have its own description
John Spurlock4bebb1d2014-10-02 19:17:19 -0400219 IC_AUDIO_VOL,
220 IC_AUDIO_VOL_MUTE,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700221 false),
RoboErik2811dd32014-08-12 09:48:13 -0700222 RemoteStream(STREAM_REMOTE_MUSIC,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700223 R.string.volume_icon_description_media, //FIXME should have its own description
John Spurlockcfe9fb62014-11-26 11:37:00 -0500224 com.android.systemui.R.drawable.ic_audio_remote,
225 com.android.systemui.R.drawable.ic_audio_remote,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700226 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700227
228 int streamType;
229 int descRes;
230 int iconRes;
231 int iconMuteRes;
232 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
233 boolean show;
234
235 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
236 this.streamType = streamType;
237 this.descRes = descRes;
238 this.iconRes = iconRes;
239 this.iconMuteRes = iconMuteRes;
240 this.show = show;
241 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700242 }
Amith Yamasani71def772011-10-12 12:25:24 -0700243
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800244 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700245 private static final StreamResources[] STREAMS = {
246 StreamResources.BluetoothSCOStream,
247 StreamResources.RingerStream,
248 StreamResources.VoiceStream,
249 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700250 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400251 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700252 StreamResources.MasterStream,
253 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800254 };
255
256 /** Object that contains data for each slider */
257 private class StreamControl {
258 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700259 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800260 ViewGroup group;
261 ImageView icon;
262 SeekBar seekbarView;
John Spurlockb4782522014-08-22 14:54:46 -0400263 TextView suppressorView;
John Spurlockbb4a7022014-11-08 12:40:19 -0500264 View divider;
265 ImageView secondaryIcon;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800266 int iconRes;
267 int iconMuteRes;
John Spurlockb4782522014-08-22 14:54:46 -0400268 int iconSuppressedRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
271 // Synchronize when accessing this
272 private ToneGenerator mToneGenerators[];
273 private Vibrator mVibrator;
John Spurlock661f2cf2014-11-17 10:29:10 -0500274 private boolean mHasVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
John Spurlock35134602014-07-24 18:10:48 -0400276 private static AlertDialog sSafetyWarning;
277 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700278
John Spurlock35134602014-07-24 18:10:48 -0400279 private static class SafetyWarning extends SystemUIDialog
280 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800281 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800282 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400283 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700284
John Spurlock2d28d6e2014-08-01 13:10:14 -0400285 private boolean mNewVolumeUp;
286
John Spurlock35134602014-07-24 18:10:48 -0400287 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
288 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700289 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800290 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400291 mAudioManager = audioManager;
292
293 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
294 setButton(DialogInterface.BUTTON_POSITIVE,
295 mContext.getString(com.android.internal.R.string.yes), this);
296 setButton(DialogInterface.BUTTON_NEGATIVE,
297 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
298 setOnDismissListener(this);
299
Eric Laurentc34dcc12012-09-10 13:51:52 -0700300 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400301 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700302 }
303
304 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400305 public boolean onKeyDown(int keyCode, KeyEvent event) {
306 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
307 mNewVolumeUp = true;
308 }
309 return super.onKeyDown(keyCode, event);
310 }
311
312 @Override
313 public boolean onKeyUp(int keyCode, KeyEvent event) {
314 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
315 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
316 mAudioManager.disableSafeMediaVolume();
317 dismiss();
318 }
319 return super.onKeyUp(keyCode, event);
320 }
321
322 @Override
John Spurlock35134602014-07-24 18:10:48 -0400323 public void onClick(DialogInterface dialog, int which) {
324 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700325 }
326
Alan Viverette494fb7b2014-04-10 18:12:56 -0700327 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700328 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400329 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800330 cleanUp();
331 }
332
333 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400334 synchronized (sSafetyWarningLock) {
335 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700336 }
John Spurlock1dad2722014-07-11 11:07:53 -0400337 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800338 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700339 }
John Spurlock35134602014-07-24 18:10:48 -0400340
341 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
342 @Override
343 public void onReceive(Context context, Intent intent) {
344 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
345 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
346 cancel();
347 cleanUp();
348 }
349 }
350 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700351 }
352
John Spurlockeb2727b2014-07-19 23:11:36 -0400353 public VolumePanel(Context context, ZenModeController zenController) {
354 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400356 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200358 mAccessibilityManager = (AccessibilityManager) context.getSystemService(
359 Context.ACCESSIBILITY_SERVICE);
John Spurlockbb4a7022014-11-08 12:40:19 -0500360 mSecondaryIconTransition = new SecondaryIconTransition();
John Spurlock661f2cf2014-11-17 10:29:10 -0500361 mIconPulser = new IconPulser(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400363 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700364 final Resources res = context.getResources();
365 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400366 if (useMasterVolume) {
367 for (int i = 0; i < STREAMS.length; i++) {
368 StreamResources streamRes = STREAMS[i];
369 streamRes.show = (streamRes.streamType == STREAM_MASTER);
370 }
371 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400372 if (LOGD) Log.d(mTag, "new VolumePanel");
373
John Spurlockad494bc2014-07-19 15:56:19 -0400374 mDisabledAlpha = 0.5f;
375 if (mContext.getTheme() != null) {
376 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
377 new int[] { android.R.attr.disabledAlpha });
378 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
379 arr.recycle();
380 }
381
John Spurlockeb2727b2014-07-19 23:11:36 -0400382 mDialog = new Dialog(context) {
383 @Override
384 public boolean onTouchEvent(MotionEvent event) {
385 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400386 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400387 forceTimeout(0);
388 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700389 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400390 return false;
391 }
392 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700393
John Spurlockeb2727b2014-07-19 23:11:36 -0400394 final Window window = mDialog.getWindow();
John Spurlockeb2727b2014-07-19 23:11:36 -0400395 window.requestFeature(Window.FEATURE_NO_TITLE);
John Spurlockeb2727b2014-07-19 23:11:36 -0400396 mDialog.setCanceledOnTouchOutside(true);
397 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
398 mDialog.setOnDismissListener(new OnDismissListener() {
399 @Override
400 public void onDismiss(DialogInterface dialog) {
401 mActiveStreamType = -1;
402 mAudioManager.forceVolumeControlStream(mActiveStreamType);
403 setZenPanelVisible(false);
John Spurlockbb4a7022014-11-08 12:40:19 -0500404 mDemoIcon = 0;
405 mSecondaryIconTransition.cancel();
John Spurlockeb2727b2014-07-19 23:11:36 -0400406 }
407 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700408
John Spurlockeb2727b2014-07-19 23:11:36 -0400409 mDialog.create();
Alan Viverette494fb7b2014-04-10 18:12:56 -0700410
John Spurlock7e6809a2014-08-06 16:03:14 -0400411 final LayoutParams lp = window.getAttributes();
412 lp.token = null;
413 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
414 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
415 lp.format = PixelFormat.TRANSLUCENT;
416 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
417 lp.setTitle(TAG);
418 window.setAttributes(lp);
419
420 updateWidth();
421
422 window.setBackgroundDrawable(new ColorDrawable(0x00000000));
423 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
424 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
425 | LayoutParams.FLAG_NOT_TOUCH_MODAL
426 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
427 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockeb2727b2014-07-19 23:11:36 -0400428 mView = window.findViewById(R.id.content);
John Spurlocka0457c22014-09-26 13:22:08 -0400429 Interaction.register(mView, new Interaction.Callback() {
John Spurlockeb2727b2014-07-19 23:11:36 -0400430 @Override
John Spurlocka0457c22014-09-26 13:22:08 -0400431 public void onInteraction() {
John Spurlockeb2727b2014-07-19 23:11:36 -0400432 resetTimeout();
John Spurlockeb2727b2014-07-19 23:11:36 -0400433 }
434 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700435
John Spurlock86005342014-05-23 11:58:00 -0400436 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
437 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400438 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
439 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700442 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
John Spurlock661f2cf2014-11-17 10:29:10 -0500443 mHasVibrator = mVibrator != null && mVibrator.hasVibrator();
Amith Yamasani71def772011-10-12 12:25:24 -0700444 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700445
John Spurlock45601d62014-08-07 17:40:50 -0400446 if (mZenController != null && !useMasterVolume) {
447 mZenModeAvailable = mZenController.isZenAvailable();
John Spurlock661f2cf2014-11-17 10:29:10 -0500448 mNotificationEffectsSuppressor = mZenController.getEffectsSuppressor();
John Spurlock45601d62014-08-07 17:40:50 -0400449 mZenController.addCallback(mZenCallback);
450 }
Amith Yamasani42722bf2011-07-22 10:34:27 -0700451
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700452 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
453 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700454 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
455
John Spurlock8c79d2e2014-07-24 15:15:25 -0400456 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800457 }
458
John Spurlock7e6809a2014-08-06 16:03:14 -0400459 public void onConfigurationChanged(Configuration newConfig) {
460 updateWidth();
Jason Monke2f47712014-09-09 09:35:55 -0400461 if (mZenPanel != null) {
462 mZenPanel.updateLocale();
463 }
John Spurlock7e6809a2014-08-06 16:03:14 -0400464 }
465
466 private void updateWidth() {
467 final Resources res = mContext.getResources();
468 final LayoutParams lp = mDialog.getWindow().getAttributes();
469 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.notification_panel_width);
470 lp.gravity =
471 res.getInteger(com.android.systemui.R.integer.notification_panel_layout_gravity);
472 mDialog.getWindow().setAttributes(lp);
473 }
474
John Spurlockad494bc2014-07-19 15:56:19 -0400475 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
476 pw.println("VolumePanel state:");
477 pw.print(" mTag="); pw.println(mTag);
478 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
479 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
John Spurlock661f2cf2014-11-17 10:29:10 -0500480 pw.print(" mHasVibrator="); pw.println(mHasVibrator);
John Spurlock45601d62014-08-07 17:40:50 -0400481 pw.print(" mZenModeAvailable="); pw.println(mZenModeAvailable);
John Spurlockad494bc2014-07-19 15:56:19 -0400482 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
John Spurlock661f2cf2014-11-17 10:29:10 -0500483 pw.print(" mNotificationEffectsSuppressor="); pw.println(mNotificationEffectsSuppressor);
John Spurlockad494bc2014-07-19 15:56:19 -0400484 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
485 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
486 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
487 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
488 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
489 pw.print(" isShowing()="); pw.println(isShowing());
490 pw.print(" mCallback="); pw.println(mCallback);
491 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400492 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400493 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
494 pw.print(" mStreamControls=");
495 if (mStreamControls == null) {
496 pw.println("null");
497 } else {
498 final int N = mStreamControls.size();
499 pw.print("<size "); pw.print(N); pw.println('>');
500 for (int i = 0; i < N; i++) {
501 final StreamControl sc = mStreamControls.valueAt(i);
502 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
503 if (sc.seekbarView != null) {
504 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
505 pw.print(" of "); pw.print(sc.seekbarView.getMax());
506 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
507 }
508 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
509 pw.println();
510 }
511 }
John Spurlock530052a2014-11-30 16:26:19 -0500512 if (mZenPanel != null) {
513 mZenPanel.dump(fd, pw, args);
514 }
John Spurlockad494bc2014-07-19 15:56:19 -0400515 }
516
John Spurlockeb2727b2014-07-19 23:11:36 -0400517 private void initZenModePanel() {
518 mZenPanel.init(mZenController);
519 mZenPanel.setCallback(new ZenModePanel.Callback() {
520 @Override
521 public void onMoreSettings() {
522 if (mCallback != null) {
523 mCallback.onZenSettings();
524 }
525 }
526
527 @Override
528 public void onInteraction() {
529 resetTimeout();
530 }
531
532 @Override
533 public void onExpanded(boolean expanded) {
534 if (mZenPanelExpanded == expanded) return;
535 mZenPanelExpanded = expanded;
536 updateTimeoutDelay();
537 resetTimeout();
538 }
539 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400540 }
541
John Spurlock86005342014-05-23 11:58:00 -0400542 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800543 mPanel.setLayoutDirection(layoutDirection);
544 updateStates();
545 }
546
John Spurlock8c79d2e2014-07-24 15:15:25 -0400547 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800548 final IntentFilter filter = new IntentFilter();
549 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlockbcc10872014-11-28 15:29:21 -0500550 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400551 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800552 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700553 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800554 public void onReceive(Context context, Intent intent) {
555 final String action = intent.getAction();
556
557 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
558 removeMessages(MSG_RINGER_MODE_CHANGED);
John Spurlockbcc10872014-11-28 15:29:21 -0500559 sendEmptyMessage(MSG_RINGER_MODE_CHANGED);
560 }
561
562 if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) {
563 removeMessages(MSG_INTERNAL_RINGER_MODE_CHANGED);
564 sendEmptyMessage(MSG_INTERNAL_RINGER_MODE_CHANGED);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800565 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400566
567 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
568 postDismiss(0);
569 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800570 }
571 }, filter);
572 }
573
574 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400575 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700576 return mAudioManager.isMasterMute();
RoboErik2811dd32014-08-12 09:48:13 -0700577 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700578 // TODO do we need to support a distinct mute property for remote?
579 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400580 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700581 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400582 }
583 }
584
585 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400586 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700587 return mAudioManager.getMasterMaxVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700588 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700589 if (mStreamControls != null) {
590 StreamControl sc = mStreamControls.get(streamType);
591 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700592 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700593 return ai.getMaxVolume();
RoboErik19c95182014-06-23 15:38:48 -0700594 }
595 }
596 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400597 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700598 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400599 }
600 }
601
602 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400603 if (streamType == STREAM_MASTER) {
RoboErik5452e252015-02-06 15:33:53 -0800604 return mAudioManager.getLastAudibleMasterVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700605 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700606 if (mStreamControls != null) {
607 StreamControl sc = mStreamControls.get(streamType);
608 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700609 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700610 return ai.getCurrentVolume();
RoboErik19c95182014-06-23 15:38:48 -0700611 }
612 }
613 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400614 } else {
RoboErik5452e252015-02-06 15:33:53 -0800615 return mAudioManager.getLastAudibleStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400616 }
617 }
618
RoboErik19c95182014-06-23 15:38:48 -0700619 private void setStreamVolume(StreamControl sc, int index, int flags) {
RoboErik2811dd32014-08-12 09:48:13 -0700620 if (sc.streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700621 if (sc.controller != null) {
622 sc.controller.setVolumeTo(index, flags);
623 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700624 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700625 }
626 } else if (getStreamVolume(sc.streamType) != index) {
627 if (sc.streamType == STREAM_MASTER) {
628 mAudioManager.setMasterVolume(index, flags);
629 } else {
630 mAudioManager.setStreamVolume(sc.streamType, index, flags);
631 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400632 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800633 }
634
635 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700636 final Resources res = mContext.getResources();
637 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
638 Context.LAYOUT_INFLATER_SERVICE);
639
John Spurlockad494bc2014-07-19 15:56:19 -0400640 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700641
John Spurlockbb4a7022014-11-08 12:40:19 -0500642 final StreamResources notificationStream = StreamResources.NotificationStream;
Amith Yamasani71def772011-10-12 12:25:24 -0700643 for (int i = 0; i < STREAMS.length; i++) {
644 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700645
646 final int streamType = streamRes.streamType;
John Spurlockbb4a7022014-11-08 12:40:19 -0500647 final boolean isNotification = isNotificationOrRing(streamType);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700648
649 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700650 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400651 sc.group = (ViewGroup) inflater.inflate(
652 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800653 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400654 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800655 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700656 sc.icon.setContentDescription(res.getString(streamRes.descRes));
657 sc.iconRes = streamRes.iconRes;
658 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800659 sc.icon.setImageResource(sc.iconRes);
John Spurlock661f2cf2014-11-17 10:29:10 -0500660 sc.icon.setClickable(isNotification && mHasVibrator);
John Spurlockbb4a7022014-11-08 12:40:19 -0500661 if (isNotification) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500662 if (mHasVibrator) {
663 sc.icon.setSoundEffectsEnabled(false);
664 sc.iconMuteRes = com.android.systemui.R.drawable.ic_ringer_vibrate;
665 sc.icon.setOnClickListener(new OnClickListener() {
666 @Override
667 public void onClick(View v) {
668 resetTimeout();
669 toggleRinger(sc);
670 }
671 });
672 }
John Spurlockb4782522014-08-22 14:54:46 -0400673 sc.iconSuppressedRes = com.android.systemui.R.drawable.ic_ringer_mute;
John Spurlock7f1df5e2014-05-31 19:11:40 -0400674 }
John Spurlock86005342014-05-23 11:58:00 -0400675 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
John Spurlockb4782522014-08-22 14:54:46 -0400676 sc.suppressorView =
677 (TextView) sc.group.findViewById(com.android.systemui.R.id.suppressor);
678 sc.suppressorView.setVisibility(View.GONE);
John Spurlockbb4a7022014-11-08 12:40:19 -0500679 final boolean showSecondary = !isNotification && notificationStream.show;
680 sc.divider = sc.group.findViewById(com.android.systemui.R.id.divider);
681 sc.secondaryIcon = (ImageView) sc.group
682 .findViewById(com.android.systemui.R.id.secondary_icon);
683 sc.secondaryIcon.setImageResource(com.android.systemui.R.drawable.ic_ringer_audible);
684 sc.secondaryIcon.setContentDescription(res.getString(notificationStream.descRes));
685 sc.secondaryIcon.setClickable(showSecondary);
686 sc.divider.setVisibility(showSecondary ? View.VISIBLE : View.GONE);
687 sc.secondaryIcon.setVisibility(showSecondary ? View.VISIBLE : View.GONE);
688 if (showSecondary) {
689 sc.secondaryIcon.setOnClickListener(new OnClickListener() {
690 @Override
691 public void onClick(View v) {
692 mSecondaryIconTransition.start(sc);
693 }
694 });
695 }
Alan Viverette494fb7b2014-04-10 18:12:56 -0700696 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700697 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400698 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700699 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800700 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700701 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800702 }
703 }
704
John Spurlock661f2cf2014-11-17 10:29:10 -0500705 private void toggleRinger(StreamControl sc) {
706 if (!mHasVibrator) return;
707 if (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_NORMAL) {
708 mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_VIBRATE);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400709 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
710 } else {
John Spurlock661f2cf2014-11-17 10:29:10 -0500711 mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400712 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
713 }
714 }
715
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800716 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400717 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800718
Alan Viverette494fb7b2014-04-10 18:12:56 -0700719 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800720 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400721 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800722 mActiveStreamType = -1;
723 } else {
John Spurlock86005342014-05-23 11:58:00 -0400724 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800725 mActiveStreamType = activeStreamType;
726 active.group.setVisibility(View.VISIBLE);
John Spurlock530052a2014-11-30 16:26:19 -0500727 updateSlider(active, true /*forceReloadIcon*/);
John Spurlock8845da72014-07-07 21:29:48 -0400728 updateTimeoutDelay();
John Spurlock45601d62014-08-07 17:40:50 -0400729 updateZenPanelVisible();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800730 }
731 }
732
John Spurlockad494bc2014-07-19 15:56:19 -0400733 private void updateSliderProgress(StreamControl sc, int progress) {
734 final boolean isRinger = isNotificationOrRing(sc.streamType);
John Spurlock661f2cf2014-11-17 10:29:10 -0500735 if (isRinger && mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_SILENT) {
John Spurlockad494bc2014-07-19 15:56:19 -0400736 progress = mLastRingerProgress;
737 }
738 if (progress < 0) {
739 progress = getStreamVolume(sc.streamType);
740 }
741 sc.seekbarView.setProgress(progress);
742 if (isRinger) {
743 mLastRingerProgress = progress;
744 }
745 }
746
747 private void updateSliderIcon(StreamControl sc, boolean muted) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500748 ComponentName suppressor = null;
John Spurlockad494bc2014-07-19 15:56:19 -0400749 if (isNotificationOrRing(sc.streamType)) {
John Spurlock661f2cf2014-11-17 10:29:10 -0500750 suppressor = mNotificationEffectsSuppressor;
751 int ringerMode = mAudioManager.getRingerModeInternal();
John Spurlockad494bc2014-07-19 15:56:19 -0400752 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
753 ringerMode = mLastRingerMode;
754 } else {
755 mLastRingerMode = ringerMode;
756 }
John Spurlock661f2cf2014-11-17 10:29:10 -0500757 if (mHasVibrator) {
758 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
759 } else {
760 muted = false;
761 }
John Spurlockad494bc2014-07-19 15:56:19 -0400762 }
John Spurlock661f2cf2014-11-17 10:29:10 -0500763 sc.icon.setImageResource(mDemoIcon != 0 ? mDemoIcon
764 : suppressor != null ? sc.iconSuppressedRes
765 : muted ? sc.iconMuteRes
766 : sc.iconRes);
John Spurlockad494bc2014-07-19 15:56:19 -0400767 }
768
John Spurlock661f2cf2014-11-17 10:29:10 -0500769 private void updateSliderSuppressor(StreamControl sc) {
John Spurlockb4782522014-08-22 14:54:46 -0400770 final ComponentName suppressor = isNotificationOrRing(sc.streamType)
John Spurlock661f2cf2014-11-17 10:29:10 -0500771 ? mNotificationEffectsSuppressor : null;
John Spurlockb4782522014-08-22 14:54:46 -0400772 if (suppressor == null) {
773 sc.seekbarView.setVisibility(View.VISIBLE);
774 sc.suppressorView.setVisibility(View.GONE);
775 } else {
776 sc.seekbarView.setVisibility(View.GONE);
777 sc.suppressorView.setVisibility(View.VISIBLE);
John Spurlock96e7f0e2014-11-14 14:35:01 -0500778 sc.suppressorView.setText(mContext.getString(R.string.muted_by,
John Spurlockb4782522014-08-22 14:54:46 -0400779 getSuppressorCaption(suppressor)));
John Spurlockb4782522014-08-22 14:54:46 -0400780 }
781 }
782
783 private String getSuppressorCaption(ComponentName suppressor) {
784 final PackageManager pm = mContext.getPackageManager();
785 try {
786 final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
787 if (info != null) {
788 final CharSequence seq = info.loadLabel(pm);
789 if (seq != null) {
790 final String str = seq.toString().trim();
791 if (str.length() > 0) {
792 return str;
793 }
794 }
795 }
796 } catch (Throwable e) {
797 Log.w(TAG, "Error loading suppressor caption", e);
798 }
799 return suppressor.getPackageName();
800 }
801
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800802 /** Update the mute and progress state of a slider */
John Spurlock530052a2014-11-30 16:26:19 -0500803 private void updateSlider(StreamControl sc, boolean forceReloadIcon) {
John Spurlockad494bc2014-07-19 15:56:19 -0400804 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800805 final boolean muted = isMuted(sc.streamType);
John Spurlock530052a2014-11-30 16:26:19 -0500806 if (forceReloadIcon) {
807 sc.icon.setImageDrawable(null);
808 }
John Spurlockad494bc2014-07-19 15:56:19 -0400809 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400810 updateSliderEnabled(sc, muted, false);
John Spurlock661f2cf2014-11-17 10:29:10 -0500811 updateSliderSuppressor(sc);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400812 }
813
John Spurlock5f640e42014-05-31 20:15:59 -0400814 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
815 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400816 final boolean isRinger = isNotificationOrRing(sc.streamType);
RoboErik2811dd32014-08-12 09:48:13 -0700817 if (sc.streamType == STREAM_REMOTE_MUSIC) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700818 // never disable touch interactions for remote playback, the muting is not tied to
819 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700820 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlock661f2cf2014-11-17 10:29:10 -0500821 } else if (isRinger && mNotificationEffectsSuppressor != null) {
822 sc.icon.setEnabled(true);
823 sc.icon.setAlpha(1f);
824 sc.icon.setClickable(false);
825 } else if (isRinger
826 && mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400827 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400828 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400829 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400830 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400831 } else if (fixedVolume ||
John Spurlock5eb21f32014-11-19 11:54:53 -0500832 (sc.streamType != mAudioManager.getMasterStreamType() && !isRinger && muted) ||
John Spurlock4f0f1202014-08-05 13:28:33 -0400833 (sSafetyWarning != null)) {
834 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700835 } else {
836 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400837 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400838 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700839 }
John Spurlockae641c92014-06-30 18:11:40 -0400840 // show the silent hint when the disabled slider is touched in silent mode
841 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400842 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400843 sc.group.setOnTouchListener(null);
John Spurlock661f2cf2014-11-17 10:29:10 -0500844 sc.icon.setClickable(mHasVibrator);
John Spurlock5f640e42014-05-31 20:15:59 -0400845 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400846 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400847 @Override
848 public boolean onTouch(View v, MotionEvent event) {
849 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400850 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400851 return false;
852 }
John Spurlockae641c92014-06-30 18:11:40 -0400853 };
854 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400855 }
856 }
857 }
858
John Spurlockae641c92014-06-30 18:11:40 -0400859 private void showSilentHint() {
860 if (mZenPanel != null) {
861 mZenPanel.showSilentHint();
862 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800863 }
864
John Spurlock661f2cf2014-11-17 10:29:10 -0500865 private void showVibrateHint() {
866 final StreamControl active = mStreamControls.get(mActiveStreamType);
867 if (active != null) {
868 mIconPulser.start(active.icon);
869 if (!hasMessages(MSG_VIBRATE)) {
870 sendEmptyMessageDelayed(MSG_VIBRATE, VIBRATE_DELAY);
871 }
872 }
873 }
874
John Spurlock7f1df5e2014-05-31 19:11:40 -0400875 private static boolean isNotificationOrRing(int streamType) {
876 return streamType == AudioManager.STREAM_RING
877 || streamType == AudioManager.STREAM_NOTIFICATION;
878 }
879
John Spurlockae641c92014-06-30 18:11:40 -0400880 public void setCallback(Callback callback) {
881 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400882 }
883
John Spurlock8845da72014-07-07 21:29:48 -0400884 private void updateTimeoutDelay() {
John Spurlockbb4a7022014-11-08 12:40:19 -0500885 mTimeoutDelay = mDemoIcon != 0 ? TIMEOUT_DELAY_EXPANDED
886 : sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
John Spurlock35134602014-07-24 18:10:48 -0400887 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400888 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
889 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
890 : TIMEOUT_DELAY;
891 }
892
893 private boolean isZenPanelVisible() {
894 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400895 }
896
John Spurlockae641c92014-06-30 18:11:40 -0400897 private void setZenPanelVisible(boolean visible) {
898 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400899 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400900 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400901 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400902 resetTimeout();
903 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400904 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800905 }
John Spurlockea9938c2014-07-11 18:51:32 -0400906 if (changing) {
907 updateTimeoutDelay();
908 resetTimeout();
909 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800910 }
911
John Spurlock530052a2014-11-30 16:26:19 -0500912 private void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400913 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800914 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400915 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
John Spurlock530052a2014-11-30 16:26:19 -0500916 updateSlider(sc, true /*forceReloadIcon*/);
917 }
918 }
919
920 private void updateActiveSlider() {
921 final StreamControl active = mStreamControls.get(mActiveStreamType);
922 if (active != null) {
923 updateSlider(active, false /*forceReloadIcon*/);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926
John Spurlock45601d62014-08-07 17:40:50 -0400927 private void updateZenPanelVisible() {
928 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400929 }
930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 public void postVolumeChanged(int streamType, int flags) {
932 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700933 synchronized (this) {
934 if (mStreamControls == null) {
935 createSliders();
936 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 removeMessages(MSG_FREE_RESOURCES);
939 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
940 }
941
RoboErik19c95182014-06-23 15:38:48 -0700942 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700943 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
944 synchronized (this) {
945 if (mStreamControls == null) {
946 createSliders();
947 }
948 }
949 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700950 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700951 }
952
953 public void postRemoteSliderVisibility(boolean visible) {
954 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
RoboErik2811dd32014-08-12 09:48:13 -0700955 STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700956 }
957
958 /**
959 * Called by AudioService when it has received new remote playback information that
960 * would affect the VolumePanel display (mainly volumes). The difference with
961 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
962 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
963 * displayed.
964 * This special code path is due to the fact that remote volume updates arrive to AudioService
965 * asynchronously. So after AudioService has sent the volume update (which should be treated
966 * as a request to update the volume), the application will likely set a new volume. If the UI
967 * is still up, we need to refresh the display to show this new value.
968 */
969 public void postHasNewRemotePlaybackInfo() {
970 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
971 // don't create or prevent resources to be freed, if they disappear, this update came too
972 // late and shouldn't warrant the panel to be displayed longer
973 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
974 }
975
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400976 public void postMasterVolumeChanged(int flags) {
977 postVolumeChanged(STREAM_MASTER, flags);
978 }
979
Mike Lockwoodce952c82011-11-14 10:47:42 -0800980 public void postMuteChanged(int streamType, int flags) {
981 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700982 synchronized (this) {
983 if (mStreamControls == null) {
984 createSliders();
985 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800986 }
987 removeMessages(MSG_FREE_RESOURCES);
988 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
989 }
990
991 public void postMasterMuteChanged(int flags) {
992 postMuteChanged(STREAM_MASTER, flags);
993 }
994
Eric Laurentfde16d52012-12-03 14:42:39 -0800995 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700996 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800997 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700998 }
999
John Spurlock1dad2722014-07-11 11:07:53 -04001000 public void postDismiss(long delay) {
1001 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -04001002 }
1003
1004 public void postLayoutDirection(int layoutDirection) {
1005 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -04001006 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -04001007 }
1008
John Spurlock661f2cf2014-11-17 10:29:10 -05001009 private static String flagsToString(int flags) {
1010 return flags == 0 ? "0" : (flags + "=" + AudioManager.flagsToString(flags));
1011 }
1012
1013 private static String streamToString(int stream) {
John Spurlock61560172015-02-06 19:46:04 -05001014 return AudioSystem.streamToString(stream);
John Spurlock661f2cf2014-11-17 10:29:10 -05001015 }
1016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 /**
1018 * Override this if you have other work to do when the volume changes (for
1019 * example, vibrating, playing a sound, etc.). Make sure to call through to
1020 * the superclass implementation.
1021 */
1022 protected void onVolumeChanged(int streamType, int flags) {
1023
John Spurlock661f2cf2014-11-17 10:29:10 -05001024 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamToString(streamType)
1025 + ", flags: " + flagsToString(flags) + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
1027 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -07001028 synchronized (this) {
1029 if (mActiveStreamType != streamType) {
1030 reorderSliders(streamType);
1031 }
RoboErik19c95182014-06-23 15:38:48 -07001032 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -08001033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035
Marco Nelissen69f593c2009-07-28 09:55:04 -07001036 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 removeMessages(MSG_PLAY_SOUND);
1038 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
1039 }
1040
1041 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
1042 removeMessages(MSG_PLAY_SOUND);
1043 removeMessages(MSG_VIBRATE);
1044 onStopSounds();
1045 }
1046
1047 removeMessages(MSG_FREE_RESOURCES);
1048 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001049 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
1051
Mike Lockwoodce952c82011-11-14 10:47:42 -08001052 protected void onMuteChanged(int streamType, int flags) {
1053
John Spurlock661f2cf2014-11-17 10:29:10 -05001054 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamToString(streamType)
1055 + ", flags: " + flagsToString(flags) + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -08001056
1057 StreamControl sc = mStreamControls.get(streamType);
1058 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -04001059 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -08001060 }
1061
1062 onVolumeChanged(streamType, flags);
1063 }
1064
RoboErik19c95182014-06-23 15:38:48 -07001065 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -07001066 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -08001067
Marco Nelissen69f593c2009-07-28 09:55:04 -07001068 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
1070 if (LOGD) {
John Spurlock661f2cf2014-11-17 10:29:10 -05001071 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamToString(streamType)
1072 + ", flags: " + flagsToString(flags) + "), index: " + index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074
1075 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001076
Mike Lockwood8dc1dab2011-10-27 09:52:41 -04001077 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -07001078 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079
1080 switch (streamType) {
1081
1082 case AudioManager.STREAM_RING: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001083 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1084 mContext, RingtoneManager.TYPE_RINGTONE);
1085 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001086 mRingIsSilent = true;
1087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 break;
1089 }
1090
1091 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001092 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -08001093 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
1094 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
1095 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1096 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
John Spurlockbb4a7022014-11-08 12:40:19 -05001097 setMusicIcon(IC_AUDIO_BT, IC_AUDIO_BT_MUTE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } else {
John Spurlock4bebb1d2014-10-02 19:17:19 -04001099 setMusicIcon(IC_AUDIO_VOL, IC_AUDIO_VOL_MUTE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101 break;
1102 }
1103
1104 case AudioManager.STREAM_VOICE_CALL: {
1105 /*
1106 * For in-call voice call volume, there is no inaudible volume.
1107 * Rescale the UI control so the progress bar doesn't go all
1108 * the way to zero and don't show the mute icon.
1109 */
1110 index++;
1111 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 break;
1113 }
1114
1115 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 break;
1117 }
1118
1119 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001120 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1121 mContext, RingtoneManager.TYPE_NOTIFICATION);
1122 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001123 mRingIsSilent = true;
1124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 break;
1126 }
1127
1128 case AudioManager.STREAM_BLUETOOTH_SCO: {
1129 /*
1130 * For in-call voice call volume, there is no inaudible volume.
1131 * Rescale the UI control so the progress bar doesn't go all
1132 * the way to zero and don't show the mute icon.
1133 */
1134 index++;
1135 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 break;
1137 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001138
RoboErik2811dd32014-08-12 09:48:13 -07001139 case STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -07001140 if (controller == null && sc != null) {
1141 // If we weren't passed one try using the last one set.
1142 controller = sc.controller;
1143 }
1144 if (controller == null) {
1145 // We still don't have one, ignore the command.
1146 Log.w(mTag, "sent remote volume change without a controller!");
1147 } else {
RoboErikd2b8c942014-08-19 11:23:40 -07001148 PlaybackInfo vi = controller.getPlaybackInfo();
RoboErik19c95182014-06-23 15:38:48 -07001149 index = vi.getCurrentVolume();
1150 max = vi.getMaxVolume();
1151 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
1152 // if the remote volume is fixed add the flag for the UI
1153 flags |= AudioManager.FLAG_FIXED_VOLUME;
1154 }
1155 }
John Spurlock86005342014-05-23 11:58:00 -04001156 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001157 break;
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001161 if (sc != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001162 if (streamType == STREAM_REMOTE_MUSIC && controller != sc.controller) {
RoboErik19c95182014-06-23 15:38:48 -07001163 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001164 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001165 }
1166 sc.controller = controller;
1167 if (controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001168 sc.controller.registerCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001169 }
1170 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001171 if (sc.seekbarView.getMax() != max) {
1172 sc.seekbarView.setMax(max);
1173 }
John Spurlockad494bc2014-07-19 15:56:19 -04001174 updateSliderProgress(sc, index);
John Spurlock661f2cf2014-11-17 10:29:10 -05001175 final boolean muted = isMuted(streamType);
1176 updateSliderEnabled(sc, muted, (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
1177 if (isNotificationOrRing(streamType)) {
1178 // check for secondary-icon transition completion
1179 if (mSecondaryIconTransition.isRunning()) {
1180 mSecondaryIconTransition.cancel(); // safe to reset
1181 sc.seekbarView.setAlpha(0); sc.seekbarView.animate().alpha(1);
1182 mZenPanel.setAlpha(0); mZenPanel.animate().alpha(1);
1183 }
1184 updateSliderIcon(sc, muted);
John Spurlockbb4a7022014-11-08 12:40:19 -05001185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
John Spurlock86005342014-05-23 11:58:00 -04001188 if (!isShowing()) {
RoboErik2811dd32014-08-12 09:48:13 -07001189 int stream = (streamType == STREAM_REMOTE_MUSIC) ? -1 : streamType;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001190 // when the stream is for remote playback, use -1 to reset the stream type evaluation
John Spurlocke506c852014-11-26 10:11:19 -05001191 if (stream != STREAM_MASTER) {
1192 mAudioManager.forceVolumeControlStream(stream);
1193 }
John Spurlockeb2727b2014-07-19 23:11:36 -04001194 mDialog.show();
1195 if (mCallback != null) {
1196 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001197 }
Selim Cinek62ea3402014-09-08 12:11:51 +02001198 announceDialogShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 // Do a little vibrate if applicable (only when going into vibrate mode)
RoboErik2811dd32014-08-12 09:48:13 -07001202 if ((streamType != STREAM_REMOTE_MUSIC) &&
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001203 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock661f2cf2014-11-17 10:29:10 -05001204 isNotificationOrRing(streamType) &&
1205 mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1207 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001208
John Spurlock661f2cf2014-11-17 10:29:10 -05001209 // Pulse the zen icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001210 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1211 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001212 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001213
1214 // Pulse the slider icon & vibrate if an adjustment down was suppressed due to vibrate mode.
1215 if ((flags & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) {
1216 showVibrateHint();
1217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219
Selim Cinek62ea3402014-09-08 12:11:51 +02001220 private void announceDialogShown() {
1221 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
1222 }
1223
John Spurlock86005342014-05-23 11:58:00 -04001224 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001225 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001226 }
1227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 protected void onPlaySound(int streamType, int flags) {
1229
1230 if (hasMessages(MSG_STOP_SOUNDS)) {
1231 removeMessages(MSG_STOP_SOUNDS);
1232 // Force stop right now
1233 onStopSounds();
1234 }
1235
1236 synchronized (this) {
1237 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001238 if (toneGen != null) {
1239 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1240 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244
1245 protected void onStopSounds() {
1246
1247 synchronized (this) {
1248 int numStreamTypes = AudioSystem.getNumStreamTypes();
1249 for (int i = numStreamTypes - 1; i >= 0; i--) {
1250 ToneGenerator toneGen = mToneGenerators[i];
1251 if (toneGen != null) {
1252 toneGen.stopTone();
1253 }
1254 }
1255 }
1256 }
1257
1258 protected void onVibrate() {
1259
1260 // Make sure we ended up in vibrate ringer mode
John Spurlock661f2cf2014-11-17 10:29:10 -05001261 if (mAudioManager.getRingerModeInternal() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 return;
1263 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001264 if (mVibrator != null) {
1265 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
1266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268
RoboErik19c95182014-06-23 15:38:48 -07001269 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
John Spurlock661f2cf2014-11-17 10:29:10 -05001270 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: "
1271 + flagsToString(flags) + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001272
John Spurlock86005342014-05-23 11:58:00 -04001273 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001274 synchronized (this) {
RoboErik2811dd32014-08-12 09:48:13 -07001275 if (mActiveStreamType != STREAM_REMOTE_MUSIC) {
1276 reorderSliders(STREAM_REMOTE_MUSIC);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001277 }
RoboErik2811dd32014-08-12 09:48:13 -07001278 onShowVolumeChanged(STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001279 }
1280 } else {
John Spurlock86005342014-05-23 11:58:00 -04001281 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001282 }
1283
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001284 removeMessages(MSG_FREE_RESOURCES);
1285 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001286 resetTimeout();
1287 }
1288
1289 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001290 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1291 if (isShowing()
RoboErik2811dd32014-08-12 09:48:13 -07001292 && (mActiveStreamType == STREAM_REMOTE_MUSIC)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001293 && (mStreamControls != null)) {
RoboErik2811dd32014-08-12 09:48:13 -07001294 onShowVolumeChanged(STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001295 }
1296 }
1297
RoboErik19c95182014-06-23 15:38:48 -07001298 /**
1299 * Clear the current remote stream controller.
1300 */
1301 private void clearRemoteStreamController() {
1302 if (mStreamControls != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001303 StreamControl sc = mStreamControls.get(STREAM_REMOTE_MUSIC);
RoboErik19c95182014-06-23 15:38:48 -07001304 if (sc != null) {
1305 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001306 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001307 sc.controller = null;
1308 }
1309 }
1310 }
1311 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001312
1313 /**
RoboErik2811dd32014-08-12 09:48:13 -07001314 * Handler for MSG_SLIDER_VISIBILITY_CHANGED Hide or show a slider
1315 *
1316 * @param streamType can be a valid stream type value, or
1317 * VolumePanel.STREAM_MASTER, or VolumePanel.STREAM_REMOTE_MUSIC
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001318 * @param visible
1319 */
1320 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001321 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001322 boolean isVisible = (visible == 1);
1323 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1324 StreamResources streamRes = STREAMS[i];
1325 if (streamRes.streamType == streamType) {
1326 streamRes.show = isVisible;
1327 if (!isVisible && (mActiveStreamType == streamType)) {
1328 mActiveStreamType = -1;
1329 }
1330 break;
1331 }
1332 }
1333 }
1334
Eric Laurentfde16d52012-12-03 14:42:39 -08001335 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001336 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1337 || isShowing()) {
1338 synchronized (sSafetyWarningLock) {
1339 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001340 return;
1341 }
John Spurlock35134602014-07-24 18:10:48 -04001342 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1343 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001344 }
1345 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001346 }
Selim Cinek1cf89062014-08-29 19:21:44 +02001347 if (mAccessibilityManager.isTouchExplorationEnabled()) {
1348 removeMessages(MSG_TIMEOUT);
1349 } else {
1350 updateTimeoutDelay();
1351 resetTimeout();
1352 }
Eric Laurentc34dcc12012-09-10 13:51:52 -07001353 }
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 /**
1356 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1357 */
1358 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001359 if (streamType == STREAM_MASTER) {
1360 // For devices that use the master volume setting only but still want to
1361 // play a volume-changed tone, direct the master volume pseudostream to
1362 // the system stream's tone generator.
1363 if (mPlayMasterStreamTones) {
1364 streamType = AudioManager.STREAM_SYSTEM;
1365 } else {
1366 return null;
1367 }
1368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 synchronized (this) {
1370 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001371 try {
1372 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1373 } catch (RuntimeException e) {
1374 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001375 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001376 + "RuntimeException: " + e);
1377 }
1378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001380 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382 }
1383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384
1385 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001386 * Switch between icons because Bluetooth music is same as music volume, but with
1387 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001389 private void setMusicIcon(int resId, int resMuteId) {
1390 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1391 if (sc != null) {
1392 sc.iconRes = resId;
1393 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001394 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
1397
1398 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 synchronized (this) {
1400 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1401 if (mToneGenerators[i] != null) {
1402 mToneGenerators[i].release();
1403 }
1404 mToneGenerators[i] = null;
1405 }
1406 }
1407 }
1408
1409 @Override
1410 public void handleMessage(Message msg) {
1411 switch (msg.what) {
1412
1413 case MSG_VOLUME_CHANGED: {
1414 onVolumeChanged(msg.arg1, msg.arg2);
1415 break;
1416 }
1417
Mike Lockwoodce952c82011-11-14 10:47:42 -08001418 case MSG_MUTE_CHANGED: {
1419 onMuteChanged(msg.arg1, msg.arg2);
1420 break;
1421 }
1422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 case MSG_FREE_RESOURCES: {
1424 onFreeResources();
1425 break;
1426 }
1427
1428 case MSG_STOP_SOUNDS: {
1429 onStopSounds();
1430 break;
1431 }
1432
1433 case MSG_PLAY_SOUND: {
1434 onPlaySound(msg.arg1, msg.arg2);
1435 break;
1436 }
1437
1438 case MSG_VIBRATE: {
1439 onVibrate();
1440 break;
1441 }
1442
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001443 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001444 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001445 mDialog.dismiss();
1446 clearRemoteStreamController();
1447 mActiveStreamType = -1;
1448 if (mCallback != null) {
1449 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001450 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001451 }
John Spurlock35134602014-07-24 18:10:48 -04001452 synchronized (sSafetyWarningLock) {
1453 if (sSafetyWarning != null) {
1454 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1455 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001456 }
1457 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001458 break;
1459 }
John Spurlockb4782522014-08-22 14:54:46 -04001460
1461 case MSG_RINGER_MODE_CHANGED:
John Spurlock661f2cf2014-11-17 10:29:10 -05001462 case MSG_INTERNAL_RINGER_MODE_CHANGED:
John Spurlockb4782522014-08-22 14:54:46 -04001463 case MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001464 if (isShowing()) {
John Spurlock530052a2014-11-30 16:26:19 -05001465 updateActiveSlider();
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001466 }
1467 break;
1468 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001469
1470 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001471 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001472 break;
1473 }
1474
1475 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1476 onRemoteVolumeUpdateIfShown();
1477 break;
1478
1479 case MSG_SLIDER_VISIBILITY_CHANGED:
1480 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1481 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001482
1483 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001484 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001485 break;
John Spurlock86005342014-05-23 11:58:00 -04001486
1487 case MSG_LAYOUT_DIRECTION:
1488 setLayoutDirection(msg.arg1);
1489 break;
1490
John Spurlock45601d62014-08-07 17:40:50 -04001491 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1492 mZenModeAvailable = msg.arg1 != 0;
1493 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001494 break;
1495
1496 case MSG_USER_ACTIVITY:
1497 if (mCallback != null) {
1498 mCallback.onInteraction();
1499 }
John Spurlock86005342014-05-23 11:58:00 -04001500 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502 }
1503
John Spurlockae641c92014-06-30 18:11:40 -04001504 private void resetTimeout() {
John Spurlocka0457c22014-09-26 13:22:08 -04001505 final boolean touchExploration = mAccessibilityManager.isTouchExplorationEnabled();
John Spurlockea9938c2014-07-11 18:51:32 -04001506 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
John Spurlocka0457c22014-09-26 13:22:08 -04001507 + " delay=" + mTimeoutDelay + " touchExploration=" + touchExploration);
1508 if (sSafetyWarning == null || !touchExploration) {
Selim Cinek1cf89062014-08-29 19:21:44 +02001509 removeMessages(MSG_TIMEOUT);
1510 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
1511 removeMessages(MSG_USER_ACTIVITY);
1512 sendEmptyMessage(MSG_USER_ACTIVITY);
1513 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001514 }
1515
John Spurlock1dad2722014-07-11 11:07:53 -04001516 private void forceTimeout(long delay) {
John Spurlocka0457c22014-09-26 13:22:08 -04001517 if (LOGD) Log.d(mTag, "forceTimeout delay=" + delay + " callers=" + Debug.getCallers(3));
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001518 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001519 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001520 }
1521
1522 public ZenModeController getZenController() {
1523 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001524 }
1525
John Spurlockbb4a7022014-11-08 12:40:19 -05001526 @Override
1527 public void dispatchDemoCommand(String command, Bundle args) {
1528 if (!COMMAND_VOLUME.equals(command)) return;
1529 String icon = args.getString("icon");
1530 final String iconMute = args.getString("iconmute");
1531 final boolean mute = iconMute != null;
1532 icon = mute ? iconMute : icon;
1533 icon = icon.endsWith("Stream") ? icon : (icon + "Stream");
1534 final StreamResources sr = StreamResources.valueOf(icon);
1535 mDemoIcon = mute ? sr.iconMuteRes : sr.iconRes;
1536 final int forcedStreamType = StreamResources.MediaStream.streamType;
1537 mAudioManager.forceVolumeControlStream(forcedStreamType);
1538 mAudioManager.adjustStreamVolume(forcedStreamType, AudioManager.ADJUST_SAME,
1539 AudioManager.FLAG_SHOW_UI);
1540 }
1541
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001542 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1543 @Override
1544 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1545 final Object tag = seekBar.getTag();
1546 if (fromUser && tag instanceof StreamControl) {
1547 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001548 setStreamVolume(sc, progress,
1549 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001550 }
1551 resetTimeout();
1552 }
1553
1554 @Override
1555 public void onStartTrackingTouch(SeekBar seekBar) {
1556 }
1557
1558 @Override
1559 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001560 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001561 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001562
John Spurlock86005342014-05-23 11:58:00 -04001563 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockb4782522014-08-22 14:54:46 -04001564 @Override
John Spurlock45601d62014-08-07 17:40:50 -04001565 public void onZenAvailableChanged(boolean available) {
1566 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001567 }
John Spurlock661f2cf2014-11-17 10:29:10 -05001568
John Spurlockb4782522014-08-22 14:54:46 -04001569 @Override
1570 public void onEffectsSupressorChanged() {
John Spurlock661f2cf2014-11-17 10:29:10 -05001571 mNotificationEffectsSuppressor = mZenController.getEffectsSuppressor();
1572 sendEmptyMessage(MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED);
1573 }
John Spurlock86005342014-05-23 11:58:00 -04001574 };
RoboErik19c95182014-06-23 15:38:48 -07001575
1576 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
RoboErikd2b8c942014-08-19 11:23:40 -07001577 public void onAudioInfoChanged(PlaybackInfo info) {
RoboErik19c95182014-06-23 15:38:48 -07001578 onRemoteVolumeUpdateIfShown();
1579 }
1580 };
John Spurlockae641c92014-06-30 18:11:40 -04001581
John Spurlockbb4a7022014-11-08 12:40:19 -05001582 private final class SecondaryIconTransition extends AnimatorListenerAdapter
1583 implements Runnable {
1584 private static final int ANIMATION_TIME = 400;
1585 private static final int WAIT_FOR_SWITCH_TIME = 1000;
1586
1587 private final int mAnimationTime = (int)(ANIMATION_TIME * ValueAnimator.getDurationScale());
1588 private final int mFadeOutTime = mAnimationTime / 2;
1589 private final int mDelayTime = mAnimationTime / 3;
1590
1591 private final Interpolator mIconInterpolator =
1592 AnimationUtils.loadInterpolator(mContext, android.R.interpolator.fast_out_slow_in);
1593
1594 private StreamControl mTarget;
1595
1596 public void start(StreamControl sc) {
1597 if (sc == null) throw new IllegalArgumentException();
John Spurlock530052a2014-11-30 16:26:19 -05001598 if (LOGD) Log.d(mTag, "Secondary icon animation start");
John Spurlockbb4a7022014-11-08 12:40:19 -05001599 if (mTarget != null) {
1600 cancel();
1601 }
1602 mTarget = sc;
1603 mTimeoutDelay = mAnimationTime + WAIT_FOR_SWITCH_TIME;
1604 resetTimeout();
1605 mTarget.secondaryIcon.setClickable(false);
1606 final int N = mTarget.group.getChildCount();
1607 for (int i = 0; i < N; i++) {
1608 final View child = mTarget.group.getChildAt(i);
1609 if (child != mTarget.secondaryIcon) {
1610 child.animate().alpha(0).setDuration(mFadeOutTime).start();
1611 }
1612 }
1613 mTarget.secondaryIcon.animate()
1614 .translationXBy(mTarget.icon.getX() - mTarget.secondaryIcon.getX())
1615 .setInterpolator(mIconInterpolator)
1616 .setStartDelay(mDelayTime)
1617 .setDuration(mAnimationTime - mDelayTime)
1618 .setListener(this)
1619 .start();
1620 }
1621
1622 public boolean isRunning() {
1623 return mTarget != null;
1624 }
1625
1626 public void cancel() {
1627 if (mTarget == null) return;
1628 mTarget.secondaryIcon.setClickable(true);
1629 final int N = mTarget.group.getChildCount();
1630 for (int i = 0; i < N; i++) {
1631 final View child = mTarget.group.getChildAt(i);
1632 if (child != mTarget.secondaryIcon) {
1633 child.animate().cancel();
1634 child.setAlpha(1);
1635 }
1636 }
1637 mTarget.secondaryIcon.animate().cancel();
1638 mTarget.secondaryIcon.setTranslationX(0);
1639 mTarget = null;
1640 }
1641
1642 @Override
1643 public void onAnimationEnd(Animator animation) {
1644 if (mTarget == null) return;
1645 AsyncTask.execute(this);
1646 }
1647
1648 @Override
1649 public void run() {
1650 if (mTarget == null) return;
John Spurlock530052a2014-11-30 16:26:19 -05001651 if (LOGD) Log.d(mTag, "Secondary icon animation complete, show notification slider");
John Spurlockbb4a7022014-11-08 12:40:19 -05001652 mAudioManager.forceVolumeControlStream(StreamResources.NotificationStream.streamType);
1653 mAudioManager.adjustStreamVolume(StreamResources.NotificationStream.streamType,
1654 AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
1655 }
1656 }
1657
John Spurlockae641c92014-06-30 18:11:40 -04001658 public interface Callback {
1659 void onZenSettings();
1660 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001661 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663}