blob: 6949ffbbd61aad731b30ce79d10012c6d02d0a60 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Spurlock3346a802014-05-20 16:25:37 -040017package com.android.systemui.volume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Eric Laurentc34dcc12012-09-10 13:51:52 -070019import android.app.AlertDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080020import android.app.Dialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080021import android.content.BroadcastReceiver;
John Spurlockb4782522014-08-22 14:54:46 -040022import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080024import android.content.DialogInterface;
John Spurlock86005342014-05-23 11:58:00 -040025import android.content.DialogInterface.OnDismissListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080027import android.content.IntentFilter;
John Spurlockb4782522014-08-22 14:54:46 -040028import android.content.pm.PackageManager;
John Spurlockb4782522014-08-22 14:54:46 -040029import android.content.pm.ServiceInfo;
John Spurlock7e6809a2014-08-06 16:03:14 -040030import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.res.Resources;
John Spurlockad494bc2014-07-19 15:56:19 -040032import android.content.res.TypedArray;
John Spurlock2078caf2014-05-29 22:20:14 -040033import android.graphics.PixelFormat;
34import android.graphics.drawable.ColorDrawable;
John Spurlock7b414672014-07-18 13:02:39 -040035import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.media.AudioManager;
37import android.media.AudioService;
38import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070039import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070041import android.media.VolumeProvider;
42import android.media.session.MediaController;
RoboErikd2b8c942014-08-19 11:23:40 -070043import android.media.session.MediaController.PlaybackInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070044import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
46import android.os.Message;
47import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040049import android.util.SparseArray;
John Spurlock2d28d6e2014-08-01 13:10:14 -040050import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040051import android.view.LayoutInflater;
52import android.view.MotionEvent;
53import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040054import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040055import android.view.ViewGroup;
56import android.view.Window;
57import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070058import android.view.WindowManager.LayoutParams;
Selim Cinek62ea3402014-09-08 12:11:51 +020059import android.view.accessibility.AccessibilityEvent;
Selim Cinek9f6ceb12014-08-29 16:28:39 +020060import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080062import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080063import android.widget.SeekBar.OnSeekBarChangeListener;
John Spurlockb4782522014-08-22 14:54:46 -040064import android.widget.TextView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080065
John Spurlock86005342014-05-23 11:58:00 -040066import com.android.internal.R;
John Spurlock35134602014-07-24 18:10:48 -040067import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040068import com.android.systemui.statusbar.policy.ZenModeController;
69
John Spurlockad494bc2014-07-19 15:56:19 -040070import java.io.FileDescriptor;
71import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/**
John Spurlock3346a802014-05-20 16:25:37 -040074 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * @hide
77 */
John Spurlock3346a802014-05-20 16:25:37 -040078public class VolumePanel extends Handler {
John Spurlockae641c92014-06-30 18:11:40 -040079 private static final String TAG = "VolumePanel";
80 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
John Spurlock3346a802014-05-20 16:25:37 -040082 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 /**
85 * The delay before vibrating. This small period exists so if the user is
86 * moving to silent mode, it will not emit a short vibrate (it normally
87 * would since vibrate is between normal mode and silent mode using hardware
88 * keys).
89 */
90 public static final int VIBRATE_DELAY = 300;
91
92 private static final int VIBRATE_DURATION = 300;
93 private static final int BEEP_DURATION = 150;
94 private static final int MAX_VOLUME = 100;
95 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080096 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -040097 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -040098 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -040099 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400100 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 private static final int MSG_VOLUME_CHANGED = 0;
103 private static final int MSG_FREE_RESOURCES = 1;
104 private static final int MSG_PLAY_SOUND = 2;
105 private static final int MSG_STOP_SOUNDS = 3;
106 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800107 private static final int MSG_TIMEOUT = 5;
108 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -0800109 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700110 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
111 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
112 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700113 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -0400114 private static final int MSG_LAYOUT_DIRECTION = 12;
John Spurlock45601d62014-08-07 17:40:50 -0400115 private static final int MSG_ZEN_MODE_AVAILABLE_CHANGED = 13;
John Spurlockae641c92014-06-30 18:11:40 -0400116 private static final int MSG_USER_ACTIVITY = 14;
John Spurlockb4782522014-08-22 14:54:46 -0400117 private static final int MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400119 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800120 private static final int STREAM_MASTER = -100;
RoboErik2811dd32014-08-12 09:48:13 -0700121 // Pseudo stream type for remote volume
122 private static final int STREAM_REMOTE_MUSIC = -200;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400123
John Spurlock7b414672014-07-18 13:02:39 -0400124 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
125 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
126 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
127 .build();
128
John Spurlock86005342014-05-23 11:58:00 -0400129 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400130 protected final Context mContext;
131 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400132 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700133 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700134 private boolean mVoiceCapable;
John Spurlock45601d62014-08-07 17:40:50 -0400135 private boolean mZenModeAvailable;
John Spurlock8845da72014-07-07 21:29:48 -0400136 private boolean mZenPanelExpanded;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400137 private int mTimeoutDelay = TIMEOUT_DELAY;
John Spurlockad494bc2014-07-19 15:56:19 -0400138 private float mDisabledAlpha;
139 private int mLastRingerMode = AudioManager.RINGER_MODE_NORMAL;
140 private int mLastRingerProgress = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
Christopher Tatec4b78d22012-05-22 13:57:58 -0700142 // True if we want to play tones on the system stream when the master stream is specified.
143 private final boolean mPlayMasterStreamTones;
144
John Spurlock86005342014-05-23 11:58:00 -0400145
146 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private final View mView;
John Spurlockeb2727b2014-07-19 23:11:36 -0400148 /** Dialog hosting the panel */
John Spurlock86005342014-05-23 11:58:00 -0400149 private final Dialog mDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800150
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700151 /** The visible portion of the volume overlay */
152 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400153 /** Contains the slider and its touchable icons */
154 private final ViewGroup mSliderPanel;
John Spurlockeb2727b2014-07-19 23:11:36 -0400155 /** The zen mode configuration panel view */
John Spurlock86005342014-05-23 11:58:00 -0400156 private ZenModePanel mZenPanel;
John Spurlock86005342014-05-23 11:58:00 -0400157
John Spurlockae641c92014-06-30 18:11:40 -0400158 private Callback mCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800159
160 /** Currently active stream that shows up at the top of the list of sliders */
161 private int mActiveStreamType = -1;
162 /** All the slider controls mapped by stream type */
John Spurlockad494bc2014-07-19 15:56:19 -0400163 private SparseArray<StreamControl> mStreamControls;
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200164 private final AccessibilityManager mAccessibilityManager;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800165
Amith Yamasani71def772011-10-12 12:25:24 -0700166 private enum StreamResources {
167 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
168 R.string.volume_icon_description_bluetooth,
169 R.drawable.ic_audio_bt,
170 R.drawable.ic_audio_bt,
171 false),
172 RingerStream(AudioManager.STREAM_RING,
173 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400174 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400175 com.android.systemui.R.drawable.ic_ringer_vibrate,
Amith Yamasani71def772011-10-12 12:25:24 -0700176 false),
177 VoiceStream(AudioManager.STREAM_VOICE_CALL,
178 R.string.volume_icon_description_incall,
179 R.drawable.ic_audio_phone,
180 R.drawable.ic_audio_phone,
181 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700182 AlarmStream(AudioManager.STREAM_ALARM,
183 R.string.volume_alarm,
184 R.drawable.ic_audio_alarm,
185 R.drawable.ic_audio_alarm_mute,
186 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700187 MediaStream(AudioManager.STREAM_MUSIC,
188 R.string.volume_icon_description_media,
189 R.drawable.ic_audio_vol,
190 R.drawable.ic_audio_vol_mute,
191 true),
192 NotificationStream(AudioManager.STREAM_NOTIFICATION,
193 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400194 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400195 com.android.systemui.R.drawable.ic_ringer_vibrate,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400196 true),
197 // for now, use media resources for master volume
198 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700199 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400200 R.drawable.ic_audio_vol,
201 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700202 false),
RoboErik2811dd32014-08-12 09:48:13 -0700203 RemoteStream(STREAM_REMOTE_MUSIC,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700204 R.string.volume_icon_description_media, //FIXME should have its own description
205 R.drawable.ic_media_route_on_holo_dark,
206 R.drawable.ic_media_route_disabled_holo_dark,
207 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700208
209 int streamType;
210 int descRes;
211 int iconRes;
212 int iconMuteRes;
213 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
214 boolean show;
215
216 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
217 this.streamType = streamType;
218 this.descRes = descRes;
219 this.iconRes = iconRes;
220 this.iconMuteRes = iconMuteRes;
221 this.show = show;
222 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700223 }
Amith Yamasani71def772011-10-12 12:25:24 -0700224
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800225 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700226 private static final StreamResources[] STREAMS = {
227 StreamResources.BluetoothSCOStream,
228 StreamResources.RingerStream,
229 StreamResources.VoiceStream,
230 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700231 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400232 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700233 StreamResources.MasterStream,
234 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800235 };
236
237 /** Object that contains data for each slider */
238 private class StreamControl {
239 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700240 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800241 ViewGroup group;
242 ImageView icon;
243 SeekBar seekbarView;
John Spurlockb4782522014-08-22 14:54:46 -0400244 TextView suppressorView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800245 int iconRes;
246 int iconMuteRes;
John Spurlockb4782522014-08-22 14:54:46 -0400247 int iconSuppressedRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 // Synchronize when accessing this
251 private ToneGenerator mToneGenerators[];
252 private Vibrator mVibrator;
253
John Spurlock35134602014-07-24 18:10:48 -0400254 private static AlertDialog sSafetyWarning;
255 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700256
John Spurlock35134602014-07-24 18:10:48 -0400257 private static class SafetyWarning extends SystemUIDialog
258 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800259 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800260 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400261 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700262
John Spurlock2d28d6e2014-08-01 13:10:14 -0400263 private boolean mNewVolumeUp;
264
John Spurlock35134602014-07-24 18:10:48 -0400265 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
266 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700267 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800268 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400269 mAudioManager = audioManager;
270
271 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
272 setButton(DialogInterface.BUTTON_POSITIVE,
273 mContext.getString(com.android.internal.R.string.yes), this);
274 setButton(DialogInterface.BUTTON_NEGATIVE,
275 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
276 setOnDismissListener(this);
277
Eric Laurentc34dcc12012-09-10 13:51:52 -0700278 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400279 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700280 }
281
282 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400283 public boolean onKeyDown(int keyCode, KeyEvent event) {
284 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
285 mNewVolumeUp = true;
286 }
287 return super.onKeyDown(keyCode, event);
288 }
289
290 @Override
291 public boolean onKeyUp(int keyCode, KeyEvent event) {
292 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
293 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
294 mAudioManager.disableSafeMediaVolume();
295 dismiss();
296 }
297 return super.onKeyUp(keyCode, event);
298 }
299
300 @Override
John Spurlock35134602014-07-24 18:10:48 -0400301 public void onClick(DialogInterface dialog, int which) {
302 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700303 }
304
Alan Viverette494fb7b2014-04-10 18:12:56 -0700305 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700306 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400307 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800308 cleanUp();
309 }
310
311 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400312 synchronized (sSafetyWarningLock) {
313 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700314 }
John Spurlock1dad2722014-07-11 11:07:53 -0400315 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800316 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700317 }
John Spurlock35134602014-07-24 18:10:48 -0400318
319 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
320 @Override
321 public void onReceive(Context context, Intent intent) {
322 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
323 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
324 cancel();
325 cleanUp();
326 }
327 }
328 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700329 }
330
John Spurlockeb2727b2014-07-19 23:11:36 -0400331 public VolumePanel(Context context, ZenModeController zenController) {
332 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400334 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Selim Cinek9f6ceb12014-08-29 16:28:39 +0200336 mAccessibilityManager = (AccessibilityManager) context.getSystemService(
337 Context.ACCESSIBILITY_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400339 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700340 final Resources res = context.getResources();
341 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400342 if (useMasterVolume) {
343 for (int i = 0; i < STREAMS.length; i++) {
344 StreamResources streamRes = STREAMS[i];
345 streamRes.show = (streamRes.streamType == STREAM_MASTER);
346 }
347 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400348 if (LOGD) Log.d(mTag, "new VolumePanel");
349
John Spurlockad494bc2014-07-19 15:56:19 -0400350 mDisabledAlpha = 0.5f;
351 if (mContext.getTheme() != null) {
352 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
353 new int[] { android.R.attr.disabledAlpha });
354 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
355 arr.recycle();
356 }
357
John Spurlockeb2727b2014-07-19 23:11:36 -0400358 mDialog = new Dialog(context) {
359 @Override
360 public boolean onTouchEvent(MotionEvent event) {
361 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400362 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400363 forceTimeout(0);
364 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700365 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400366 return false;
367 }
368 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700369
John Spurlockeb2727b2014-07-19 23:11:36 -0400370 final Window window = mDialog.getWindow();
John Spurlockeb2727b2014-07-19 23:11:36 -0400371 window.requestFeature(Window.FEATURE_NO_TITLE);
John Spurlockeb2727b2014-07-19 23:11:36 -0400372 mDialog.setCanceledOnTouchOutside(true);
373 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
374 mDialog.setOnDismissListener(new OnDismissListener() {
375 @Override
376 public void onDismiss(DialogInterface dialog) {
377 mActiveStreamType = -1;
378 mAudioManager.forceVolumeControlStream(mActiveStreamType);
379 setZenPanelVisible(false);
380 }
381 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700382
John Spurlockeb2727b2014-07-19 23:11:36 -0400383 mDialog.create();
Alan Viverette494fb7b2014-04-10 18:12:56 -0700384
John Spurlock7e6809a2014-08-06 16:03:14 -0400385 final LayoutParams lp = window.getAttributes();
386 lp.token = null;
387 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
388 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
389 lp.format = PixelFormat.TRANSLUCENT;
390 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
391 lp.setTitle(TAG);
392 window.setAttributes(lp);
393
394 updateWidth();
395
396 window.setBackgroundDrawable(new ColorDrawable(0x00000000));
397 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
398 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
399 | LayoutParams.FLAG_NOT_TOUCH_MODAL
400 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
401 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockeb2727b2014-07-19 23:11:36 -0400402 mView = window.findViewById(R.id.content);
403 mView.setOnTouchListener(new View.OnTouchListener() {
404 @Override
405 public boolean onTouch(View v, MotionEvent event) {
406 resetTimeout();
407 return false;
408 }
409 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700410
John Spurlock86005342014-05-23 11:58:00 -0400411 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
412 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400413 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
414 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700417 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700418 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700419
John Spurlock45601d62014-08-07 17:40:50 -0400420 if (mZenController != null && !useMasterVolume) {
421 mZenModeAvailable = mZenController.isZenAvailable();
422 mZenController.addCallback(mZenCallback);
423 }
Amith Yamasani42722bf2011-07-22 10:34:27 -0700424
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700425 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
426 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700427 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
428
John Spurlock8c79d2e2014-07-24 15:15:25 -0400429 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800430 }
431
John Spurlock7e6809a2014-08-06 16:03:14 -0400432 public void onConfigurationChanged(Configuration newConfig) {
433 updateWidth();
434 }
435
436 private void updateWidth() {
437 final Resources res = mContext.getResources();
438 final LayoutParams lp = mDialog.getWindow().getAttributes();
439 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.notification_panel_width);
440 lp.gravity =
441 res.getInteger(com.android.systemui.R.integer.notification_panel_layout_gravity);
442 mDialog.getWindow().setAttributes(lp);
443 }
444
John Spurlockad494bc2014-07-19 15:56:19 -0400445 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
446 pw.println("VolumePanel state:");
447 pw.print(" mTag="); pw.println(mTag);
448 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
449 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
John Spurlock45601d62014-08-07 17:40:50 -0400450 pw.print(" mZenModeAvailable="); pw.println(mZenModeAvailable);
John Spurlockad494bc2014-07-19 15:56:19 -0400451 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
452 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
453 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
454 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
455 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
456 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
457 pw.print(" isShowing()="); pw.println(isShowing());
458 pw.print(" mCallback="); pw.println(mCallback);
459 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400460 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400461 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
462 pw.print(" mStreamControls=");
463 if (mStreamControls == null) {
464 pw.println("null");
465 } else {
466 final int N = mStreamControls.size();
467 pw.print("<size "); pw.print(N); pw.println('>');
468 for (int i = 0; i < N; i++) {
469 final StreamControl sc = mStreamControls.valueAt(i);
470 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
471 if (sc.seekbarView != null) {
472 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
473 pw.print(" of "); pw.print(sc.seekbarView.getMax());
474 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
475 }
476 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
477 pw.println();
478 }
479 }
480 }
481
John Spurlockeb2727b2014-07-19 23:11:36 -0400482 private void initZenModePanel() {
483 mZenPanel.init(mZenController);
484 mZenPanel.setCallback(new ZenModePanel.Callback() {
485 @Override
486 public void onMoreSettings() {
487 if (mCallback != null) {
488 mCallback.onZenSettings();
489 }
490 }
491
492 @Override
493 public void onInteraction() {
494 resetTimeout();
495 }
496
497 @Override
498 public void onExpanded(boolean expanded) {
499 if (mZenPanelExpanded == expanded) return;
500 mZenPanelExpanded = expanded;
501 updateTimeoutDelay();
502 resetTimeout();
503 }
504 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400505 }
506
John Spurlock86005342014-05-23 11:58:00 -0400507 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800508 mPanel.setLayoutDirection(layoutDirection);
509 updateStates();
510 }
511
John Spurlock8c79d2e2014-07-24 15:15:25 -0400512 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800513 final IntentFilter filter = new IntentFilter();
514 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400515 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800516 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700517 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800518 public void onReceive(Context context, Intent intent) {
519 final String action = intent.getAction();
520
521 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
522 removeMessages(MSG_RINGER_MODE_CHANGED);
523 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
524 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400525
526 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
527 postDismiss(0);
528 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800529 }
530 }, filter);
531 }
532
533 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400534 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700535 return mAudioManager.isMasterMute();
RoboErik2811dd32014-08-12 09:48:13 -0700536 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700537 // TODO do we need to support a distinct mute property for remote?
538 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400539 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700540 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400541 }
542 }
543
544 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400545 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700546 return mAudioManager.getMasterMaxVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700547 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700548 if (mStreamControls != null) {
549 StreamControl sc = mStreamControls.get(streamType);
550 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700551 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700552 return ai.getMaxVolume();
RoboErik19c95182014-06-23 15:38:48 -0700553 }
554 }
555 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400556 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700557 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400558 }
559 }
560
561 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400562 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700563 return mAudioManager.getMasterVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700564 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700565 if (mStreamControls != null) {
566 StreamControl sc = mStreamControls.get(streamType);
567 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700568 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700569 return ai.getCurrentVolume();
RoboErik19c95182014-06-23 15:38:48 -0700570 }
571 }
572 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400573 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700574 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400575 }
576 }
577
RoboErik19c95182014-06-23 15:38:48 -0700578 private void setStreamVolume(StreamControl sc, int index, int flags) {
RoboErik2811dd32014-08-12 09:48:13 -0700579 if (sc.streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700580 if (sc.controller != null) {
581 sc.controller.setVolumeTo(index, flags);
582 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700583 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700584 }
585 } else if (getStreamVolume(sc.streamType) != index) {
586 if (sc.streamType == STREAM_MASTER) {
587 mAudioManager.setMasterVolume(index, flags);
588 } else {
589 mAudioManager.setStreamVolume(sc.streamType, index, flags);
590 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400591 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800592 }
593
594 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700595 final Resources res = mContext.getResources();
596 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
597 Context.LAYOUT_INFLATER_SERVICE);
598
John Spurlockad494bc2014-07-19 15:56:19 -0400599 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700600
Amith Yamasani71def772011-10-12 12:25:24 -0700601 for (int i = 0; i < STREAMS.length; i++) {
602 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700603
604 final int streamType = streamRes.streamType;
Alan Viverette494fb7b2014-04-10 18:12:56 -0700605
606 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700607 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400608 sc.group = (ViewGroup) inflater.inflate(
609 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800610 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400611 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800612 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700613 sc.icon.setContentDescription(res.getString(streamRes.descRes));
614 sc.iconRes = streamRes.iconRes;
615 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800616 sc.icon.setImageResource(sc.iconRes);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400617 sc.icon.setClickable(isNotificationOrRing(streamType));
618 if (sc.icon.isClickable()) {
John Spurlockab4ea762014-07-19 18:50:16 -0400619 sc.icon.setSoundEffectsEnabled(false);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400620 sc.icon.setOnClickListener(new OnClickListener() {
621 @Override
622 public void onClick(View v) {
623 resetTimeout();
624 toggle(sc);
625 }
626 });
John Spurlockb4782522014-08-22 14:54:46 -0400627 sc.iconSuppressedRes = com.android.systemui.R.drawable.ic_ringer_mute;
John Spurlock7f1df5e2014-05-31 19:11:40 -0400628 }
John Spurlock86005342014-05-23 11:58:00 -0400629 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
John Spurlockb4782522014-08-22 14:54:46 -0400630 sc.suppressorView =
631 (TextView) sc.group.findViewById(com.android.systemui.R.id.suppressor);
632 sc.suppressorView.setVisibility(View.GONE);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700633 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700634 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400635 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700636 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800637 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700638 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800639 }
640 }
641
John Spurlock7f1df5e2014-05-31 19:11:40 -0400642 private void toggle(StreamControl sc) {
643 if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
644 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
645 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
646 } else {
647 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
648 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
649 }
650 }
651
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800652 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400653 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800654
Alan Viverette494fb7b2014-04-10 18:12:56 -0700655 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800656 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400657 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800658 mActiveStreamType = -1;
659 } else {
John Spurlock86005342014-05-23 11:58:00 -0400660 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800661 mActiveStreamType = activeStreamType;
662 active.group.setVisibility(View.VISIBLE);
663 updateSlider(active);
John Spurlock8845da72014-07-07 21:29:48 -0400664 updateTimeoutDelay();
John Spurlock45601d62014-08-07 17:40:50 -0400665 updateZenPanelVisible();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800666 }
667 }
668
John Spurlockad494bc2014-07-19 15:56:19 -0400669 private void updateSliderProgress(StreamControl sc, int progress) {
670 final boolean isRinger = isNotificationOrRing(sc.streamType);
671 if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
672 progress = mLastRingerProgress;
673 }
674 if (progress < 0) {
675 progress = getStreamVolume(sc.streamType);
676 }
677 sc.seekbarView.setProgress(progress);
678 if (isRinger) {
679 mLastRingerProgress = progress;
680 }
681 }
682
683 private void updateSliderIcon(StreamControl sc, boolean muted) {
684 if (isNotificationOrRing(sc.streamType)) {
685 int ringerMode = mAudioManager.getRingerMode();
686 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
687 ringerMode = mLastRingerMode;
688 } else {
689 mLastRingerMode = ringerMode;
690 }
691 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
692 }
693 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
694 }
695
John Spurlockb4782522014-08-22 14:54:46 -0400696 private void updateSliderSupressor(StreamControl sc) {
697 final ComponentName suppressor = isNotificationOrRing(sc.streamType)
698 ? mZenController.getEffectsSuppressor() : null;
699 if (suppressor == null) {
700 sc.seekbarView.setVisibility(View.VISIBLE);
701 sc.suppressorView.setVisibility(View.GONE);
702 } else {
703 sc.seekbarView.setVisibility(View.GONE);
704 sc.suppressorView.setVisibility(View.VISIBLE);
705 sc.suppressorView.setText(mContext.getString(com.android.systemui.R.string.muted_by,
706 getSuppressorCaption(suppressor)));
707 sc.icon.setImageResource(sc.iconSuppressedRes);
708 }
709 }
710
711 private String getSuppressorCaption(ComponentName suppressor) {
712 final PackageManager pm = mContext.getPackageManager();
713 try {
714 final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
715 if (info != null) {
716 final CharSequence seq = info.loadLabel(pm);
717 if (seq != null) {
718 final String str = seq.toString().trim();
719 if (str.length() > 0) {
720 return str;
721 }
722 }
723 }
724 } catch (Throwable e) {
725 Log.w(TAG, "Error loading suppressor caption", e);
726 }
727 return suppressor.getPackageName();
728 }
729
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800730 /** Update the mute and progress state of a slider */
731 private void updateSlider(StreamControl sc) {
John Spurlockad494bc2014-07-19 15:56:19 -0400732 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800733 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800734 // Force reloading the image resource
735 sc.icon.setImageDrawable(null);
John Spurlockad494bc2014-07-19 15:56:19 -0400736 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400737 updateSliderEnabled(sc, muted, false);
John Spurlockb4782522014-08-22 14:54:46 -0400738 updateSliderSupressor(sc);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400739 }
740
John Spurlock5f640e42014-05-31 20:15:59 -0400741 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
742 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400743 final boolean isRinger = isNotificationOrRing(sc.streamType);
RoboErik2811dd32014-08-12 09:48:13 -0700744 if (sc.streamType == STREAM_REMOTE_MUSIC) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700745 // never disable touch interactions for remote playback, the muting is not tied to
746 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700747 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlockae641c92014-06-30 18:11:40 -0400748 } else if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400749 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400750 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400751 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400752 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400753 } else if (fixedVolume ||
754 (sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
755 (sSafetyWarning != null)) {
756 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700757 } else {
758 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400759 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400760 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700761 }
John Spurlockae641c92014-06-30 18:11:40 -0400762 // show the silent hint when the disabled slider is touched in silent mode
763 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400764 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400765 sc.group.setOnTouchListener(null);
766 sc.icon.setClickable(true);
John Spurlock5f640e42014-05-31 20:15:59 -0400767 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400768 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400769 @Override
770 public boolean onTouch(View v, MotionEvent event) {
771 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400772 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400773 return false;
774 }
John Spurlockae641c92014-06-30 18:11:40 -0400775 };
776 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400777 }
778 }
779 }
780
John Spurlockae641c92014-06-30 18:11:40 -0400781 private void showSilentHint() {
782 if (mZenPanel != null) {
783 mZenPanel.showSilentHint();
784 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800785 }
786
John Spurlock7f1df5e2014-05-31 19:11:40 -0400787 private static boolean isNotificationOrRing(int streamType) {
788 return streamType == AudioManager.STREAM_RING
789 || streamType == AudioManager.STREAM_NOTIFICATION;
790 }
791
John Spurlockae641c92014-06-30 18:11:40 -0400792 public void setCallback(Callback callback) {
793 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400794 }
795
John Spurlock8845da72014-07-07 21:29:48 -0400796 private void updateTimeoutDelay() {
Selim Cinek1cf89062014-08-29 19:21:44 +0200797 mTimeoutDelay = sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
John Spurlock35134602014-07-24 18:10:48 -0400798 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400799 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
800 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
801 : TIMEOUT_DELAY;
802 }
803
804 private boolean isZenPanelVisible() {
805 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400806 }
807
John Spurlockae641c92014-06-30 18:11:40 -0400808 private void setZenPanelVisible(boolean visible) {
809 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400810 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400811 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400812 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400813 resetTimeout();
814 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400815 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800816 }
John Spurlockea9938c2014-07-11 18:51:32 -0400817 if (changing) {
818 updateTimeoutDelay();
819 resetTimeout();
820 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800821 }
822
Eric Laurentfde16d52012-12-03 14:42:39 -0800823 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400824 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800825 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400826 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800827 updateSlider(sc);
828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830
John Spurlock45601d62014-08-07 17:40:50 -0400831 private void updateZenPanelVisible() {
832 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400833 }
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 public void postVolumeChanged(int streamType, int flags) {
836 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700837 synchronized (this) {
838 if (mStreamControls == null) {
839 createSliders();
840 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 removeMessages(MSG_FREE_RESOURCES);
843 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
844 }
845
RoboErik19c95182014-06-23 15:38:48 -0700846 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700847 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
848 synchronized (this) {
849 if (mStreamControls == null) {
850 createSliders();
851 }
852 }
853 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700854 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700855 }
856
857 public void postRemoteSliderVisibility(boolean visible) {
858 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
RoboErik2811dd32014-08-12 09:48:13 -0700859 STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700860 }
861
862 /**
863 * Called by AudioService when it has received new remote playback information that
864 * would affect the VolumePanel display (mainly volumes). The difference with
865 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
866 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
867 * displayed.
868 * This special code path is due to the fact that remote volume updates arrive to AudioService
869 * asynchronously. So after AudioService has sent the volume update (which should be treated
870 * as a request to update the volume), the application will likely set a new volume. If the UI
871 * is still up, we need to refresh the display to show this new value.
872 */
873 public void postHasNewRemotePlaybackInfo() {
874 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
875 // don't create or prevent resources to be freed, if they disappear, this update came too
876 // late and shouldn't warrant the panel to be displayed longer
877 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
878 }
879
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400880 public void postMasterVolumeChanged(int flags) {
881 postVolumeChanged(STREAM_MASTER, flags);
882 }
883
Mike Lockwoodce952c82011-11-14 10:47:42 -0800884 public void postMuteChanged(int streamType, int flags) {
885 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700886 synchronized (this) {
887 if (mStreamControls == null) {
888 createSliders();
889 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800890 }
891 removeMessages(MSG_FREE_RESOURCES);
892 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
893 }
894
895 public void postMasterMuteChanged(int flags) {
896 postMuteChanged(STREAM_MASTER, flags);
897 }
898
Eric Laurentfde16d52012-12-03 14:42:39 -0800899 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700900 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800901 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700902 }
903
John Spurlock1dad2722014-07-11 11:07:53 -0400904 public void postDismiss(long delay) {
905 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400906 }
907
908 public void postLayoutDirection(int layoutDirection) {
909 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400910 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400911 }
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 /**
914 * Override this if you have other work to do when the volume changes (for
915 * example, vibrating, playing a sound, etc.). Make sure to call through to
916 * the superclass implementation.
917 */
918 protected void onVolumeChanged(int streamType, int flags) {
919
John Spurlock86005342014-05-23 11:58:00 -0400920 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921
922 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700923 synchronized (this) {
924 if (mActiveStreamType != streamType) {
925 reorderSliders(streamType);
926 }
RoboErik19c95182014-06-23 15:38:48 -0700927 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 }
930
Marco Nelissen69f593c2009-07-28 09:55:04 -0700931 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 removeMessages(MSG_PLAY_SOUND);
933 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
934 }
935
936 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
937 removeMessages(MSG_PLAY_SOUND);
938 removeMessages(MSG_VIBRATE);
939 onStopSounds();
940 }
941
942 removeMessages(MSG_FREE_RESOURCES);
943 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800944 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946
Mike Lockwoodce952c82011-11-14 10:47:42 -0800947 protected void onMuteChanged(int streamType, int flags) {
948
John Spurlock86005342014-05-23 11:58:00 -0400949 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800950
951 StreamControl sc = mStreamControls.get(streamType);
952 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400953 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -0800954 }
955
956 onVolumeChanged(streamType, flags);
957 }
958
RoboErik19c95182014-06-23 15:38:48 -0700959 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -0700960 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800961
Marco Nelissen69f593c2009-07-28 09:55:04 -0700962 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963
964 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400965 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 + ", flags: " + flags + "), index: " + index);
967 }
968
969 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800970
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400971 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -0700972 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
974 switch (streamType) {
975
976 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800977// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700978 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
979 mContext, RingtoneManager.TYPE_RINGTONE);
980 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700981 mRingIsSilent = true;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 break;
984 }
985
986 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800987 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800988 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
989 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
990 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
991 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800992 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800994 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996 break;
997 }
998
999 case AudioManager.STREAM_VOICE_CALL: {
1000 /*
1001 * For in-call voice call volume, there is no inaudible volume.
1002 * Rescale the UI control so the progress bar doesn't go all
1003 * the way to zero and don't show the mute icon.
1004 */
1005 index++;
1006 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 break;
1008 }
1009
1010 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 break;
1012 }
1013
1014 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001015 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1016 mContext, RingtoneManager.TYPE_NOTIFICATION);
1017 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001018 mRingIsSilent = true;
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 break;
1021 }
1022
1023 case AudioManager.STREAM_BLUETOOTH_SCO: {
1024 /*
1025 * For in-call voice call volume, there is no inaudible volume.
1026 * Rescale the UI control so the progress bar doesn't go all
1027 * the way to zero and don't show the mute icon.
1028 */
1029 index++;
1030 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 break;
1032 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001033
RoboErik2811dd32014-08-12 09:48:13 -07001034 case STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -07001035 if (controller == null && sc != null) {
1036 // If we weren't passed one try using the last one set.
1037 controller = sc.controller;
1038 }
1039 if (controller == null) {
1040 // We still don't have one, ignore the command.
1041 Log.w(mTag, "sent remote volume change without a controller!");
1042 } else {
RoboErikd2b8c942014-08-19 11:23:40 -07001043 PlaybackInfo vi = controller.getPlaybackInfo();
RoboErik19c95182014-06-23 15:38:48 -07001044 index = vi.getCurrentVolume();
1045 max = vi.getMaxVolume();
1046 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
1047 // if the remote volume is fixed add the flag for the UI
1048 flags |= AudioManager.FLAG_FIXED_VOLUME;
1049 }
1050 }
John Spurlock86005342014-05-23 11:58:00 -04001051 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001052 break;
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001056 if (sc != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001057 if (streamType == STREAM_REMOTE_MUSIC && controller != sc.controller) {
RoboErik19c95182014-06-23 15:38:48 -07001058 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001059 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001060 }
1061 sc.controller = controller;
1062 if (controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001063 sc.controller.registerCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001064 }
1065 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001066 if (sc.seekbarView.getMax() != max) {
1067 sc.seekbarView.setMax(max);
1068 }
John Spurlockad494bc2014-07-19 15:56:19 -04001069 updateSliderProgress(sc, index);
John Spurlock7f1df5e2014-05-31 19:11:40 -04001070 updateSliderEnabled(sc, isMuted(streamType),
1071 (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073
John Spurlock86005342014-05-23 11:58:00 -04001074 if (!isShowing()) {
RoboErik2811dd32014-08-12 09:48:13 -07001075 int stream = (streamType == STREAM_REMOTE_MUSIC) ? -1 : streamType;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001076 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1077 mAudioManager.forceVolumeControlStream(stream);
John Spurlockeb2727b2014-07-19 23:11:36 -04001078 mDialog.show();
1079 if (mCallback != null) {
1080 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001081 }
Selim Cinek62ea3402014-09-08 12:11:51 +02001082 announceDialogShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 // Do a little vibrate if applicable (only when going into vibrate mode)
RoboErik2811dd32014-08-12 09:48:13 -07001086 if ((streamType != STREAM_REMOTE_MUSIC) &&
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001087 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock3346a802014-05-20 16:25:37 -04001088 mAudioManager.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -07001089 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1091 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001092
1093 // Pulse the slider icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001094 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1095 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098
Selim Cinek62ea3402014-09-08 12:11:51 +02001099 private void announceDialogShown() {
1100 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
1101 }
1102
John Spurlock86005342014-05-23 11:58:00 -04001103 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001104 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001105 }
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 protected void onPlaySound(int streamType, int flags) {
1108
1109 if (hasMessages(MSG_STOP_SOUNDS)) {
1110 removeMessages(MSG_STOP_SOUNDS);
1111 // Force stop right now
1112 onStopSounds();
1113 }
1114
1115 synchronized (this) {
1116 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001117 if (toneGen != null) {
1118 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1119 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
1124 protected void onStopSounds() {
1125
1126 synchronized (this) {
1127 int numStreamTypes = AudioSystem.getNumStreamTypes();
1128 for (int i = numStreamTypes - 1; i >= 0; i--) {
1129 ToneGenerator toneGen = mToneGenerators[i];
1130 if (toneGen != null) {
1131 toneGen.stopTone();
1132 }
1133 }
1134 }
1135 }
1136
1137 protected void onVibrate() {
1138
1139 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -07001140 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 return;
1142 }
1143
John Spurlock7b414672014-07-18 13:02:39 -04001144 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146
RoboErik19c95182014-06-23 15:38:48 -07001147 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
1148 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: " + flags
1149 + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001150
John Spurlock86005342014-05-23 11:58:00 -04001151 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001152 synchronized (this) {
RoboErik2811dd32014-08-12 09:48:13 -07001153 if (mActiveStreamType != STREAM_REMOTE_MUSIC) {
1154 reorderSliders(STREAM_REMOTE_MUSIC);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001155 }
RoboErik2811dd32014-08-12 09:48:13 -07001156 onShowVolumeChanged(STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001157 }
1158 } else {
John Spurlock86005342014-05-23 11:58:00 -04001159 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001160 }
1161
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001162 removeMessages(MSG_FREE_RESOURCES);
1163 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001164 resetTimeout();
1165 }
1166
1167 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001168 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1169 if (isShowing()
RoboErik2811dd32014-08-12 09:48:13 -07001170 && (mActiveStreamType == STREAM_REMOTE_MUSIC)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001171 && (mStreamControls != null)) {
RoboErik2811dd32014-08-12 09:48:13 -07001172 onShowVolumeChanged(STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001173 }
1174 }
1175
RoboErik19c95182014-06-23 15:38:48 -07001176 /**
1177 * Clear the current remote stream controller.
1178 */
1179 private void clearRemoteStreamController() {
1180 if (mStreamControls != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001181 StreamControl sc = mStreamControls.get(STREAM_REMOTE_MUSIC);
RoboErik19c95182014-06-23 15:38:48 -07001182 if (sc != null) {
1183 if (sc.controller != null) {
RoboErik14f717a2014-09-04 16:08:00 -07001184 sc.controller.unregisterCallback(mMediaControllerCb);
RoboErik19c95182014-06-23 15:38:48 -07001185 sc.controller = null;
1186 }
1187 }
1188 }
1189 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001190
1191 /**
RoboErik2811dd32014-08-12 09:48:13 -07001192 * Handler for MSG_SLIDER_VISIBILITY_CHANGED Hide or show a slider
1193 *
1194 * @param streamType can be a valid stream type value, or
1195 * VolumePanel.STREAM_MASTER, or VolumePanel.STREAM_REMOTE_MUSIC
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001196 * @param visible
1197 */
1198 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001199 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001200 boolean isVisible = (visible == 1);
1201 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1202 StreamResources streamRes = STREAMS[i];
1203 if (streamRes.streamType == streamType) {
1204 streamRes.show = isVisible;
1205 if (!isVisible && (mActiveStreamType == streamType)) {
1206 mActiveStreamType = -1;
1207 }
1208 break;
1209 }
1210 }
1211 }
1212
Eric Laurentfde16d52012-12-03 14:42:39 -08001213 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001214 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1215 || isShowing()) {
1216 synchronized (sSafetyWarningLock) {
1217 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001218 return;
1219 }
John Spurlock35134602014-07-24 18:10:48 -04001220 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1221 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001222 }
1223 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001224 }
Selim Cinek1cf89062014-08-29 19:21:44 +02001225 if (mAccessibilityManager.isTouchExplorationEnabled()) {
1226 removeMessages(MSG_TIMEOUT);
1227 } else {
1228 updateTimeoutDelay();
1229 resetTimeout();
1230 }
Eric Laurentc34dcc12012-09-10 13:51:52 -07001231 }
1232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 /**
1234 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1235 */
1236 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001237 if (streamType == STREAM_MASTER) {
1238 // For devices that use the master volume setting only but still want to
1239 // play a volume-changed tone, direct the master volume pseudostream to
1240 // the system stream's tone generator.
1241 if (mPlayMasterStreamTones) {
1242 streamType = AudioManager.STREAM_SYSTEM;
1243 } else {
1244 return null;
1245 }
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 synchronized (this) {
1248 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001249 try {
1250 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1251 } catch (RuntimeException e) {
1252 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001253 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001254 + "RuntimeException: " + e);
1255 }
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001258 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
1260 }
1261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262
1263 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001264 * Switch between icons because Bluetooth music is same as music volume, but with
1265 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001267 private void setMusicIcon(int resId, int resMuteId) {
1268 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1269 if (sc != null) {
1270 sc.iconRes = resId;
1271 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001272 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275
1276 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 synchronized (this) {
1278 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1279 if (mToneGenerators[i] != null) {
1280 mToneGenerators[i].release();
1281 }
1282 mToneGenerators[i] = null;
1283 }
1284 }
1285 }
1286
1287 @Override
1288 public void handleMessage(Message msg) {
1289 switch (msg.what) {
1290
1291 case MSG_VOLUME_CHANGED: {
1292 onVolumeChanged(msg.arg1, msg.arg2);
1293 break;
1294 }
1295
Mike Lockwoodce952c82011-11-14 10:47:42 -08001296 case MSG_MUTE_CHANGED: {
1297 onMuteChanged(msg.arg1, msg.arg2);
1298 break;
1299 }
1300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 case MSG_FREE_RESOURCES: {
1302 onFreeResources();
1303 break;
1304 }
1305
1306 case MSG_STOP_SOUNDS: {
1307 onStopSounds();
1308 break;
1309 }
1310
1311 case MSG_PLAY_SOUND: {
1312 onPlaySound(msg.arg1, msg.arg2);
1313 break;
1314 }
1315
1316 case MSG_VIBRATE: {
1317 onVibrate();
1318 break;
1319 }
1320
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001321 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001322 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001323 mDialog.dismiss();
1324 clearRemoteStreamController();
1325 mActiveStreamType = -1;
1326 if (mCallback != null) {
1327 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001328 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001329 }
John Spurlock35134602014-07-24 18:10:48 -04001330 synchronized (sSafetyWarningLock) {
1331 if (sSafetyWarning != null) {
1332 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1333 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001334 }
1335 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001336 break;
1337 }
John Spurlockb4782522014-08-22 14:54:46 -04001338
1339 case MSG_RINGER_MODE_CHANGED:
1340 case MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001341 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001342 updateStates();
1343 }
1344 break;
1345 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001346
1347 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001348 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001349 break;
1350 }
1351
1352 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1353 onRemoteVolumeUpdateIfShown();
1354 break;
1355
1356 case MSG_SLIDER_VISIBILITY_CHANGED:
1357 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1358 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001359
1360 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001361 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001362 break;
John Spurlock86005342014-05-23 11:58:00 -04001363
1364 case MSG_LAYOUT_DIRECTION:
1365 setLayoutDirection(msg.arg1);
1366 break;
1367
John Spurlock45601d62014-08-07 17:40:50 -04001368 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1369 mZenModeAvailable = msg.arg1 != 0;
1370 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001371 break;
1372
1373 case MSG_USER_ACTIVITY:
1374 if (mCallback != null) {
1375 mCallback.onInteraction();
1376 }
John Spurlock86005342014-05-23 11:58:00 -04001377 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
1379 }
1380
John Spurlockae641c92014-06-30 18:11:40 -04001381 private void resetTimeout() {
John Spurlockea9938c2014-07-11 18:51:32 -04001382 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
1383 + " delay=" + mTimeoutDelay);
Selim Cinek1cf89062014-08-29 19:21:44 +02001384 if (sSafetyWarning == null || !mAccessibilityManager.isTouchExplorationEnabled()) {
1385 removeMessages(MSG_TIMEOUT);
1386 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
1387 removeMessages(MSG_USER_ACTIVITY);
1388 sendEmptyMessage(MSG_USER_ACTIVITY);
1389 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001390 }
1391
John Spurlock1dad2722014-07-11 11:07:53 -04001392 private void forceTimeout(long delay) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001393 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001394 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001395 }
1396
1397 public ZenModeController getZenController() {
1398 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001399 }
1400
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001401 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1402 @Override
1403 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1404 final Object tag = seekBar.getTag();
1405 if (fromUser && tag instanceof StreamControl) {
1406 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001407 setStreamVolume(sc, progress,
1408 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001409 }
1410 resetTimeout();
1411 }
1412
1413 @Override
1414 public void onStartTrackingTouch(SeekBar seekBar) {
1415 }
1416
1417 @Override
1418 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001419 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001420 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001421
John Spurlock86005342014-05-23 11:58:00 -04001422 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockb4782522014-08-22 14:54:46 -04001423 @Override
John Spurlock45601d62014-08-07 17:40:50 -04001424 public void onZenAvailableChanged(boolean available) {
1425 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001426 }
John Spurlockb4782522014-08-22 14:54:46 -04001427 @Override
1428 public void onEffectsSupressorChanged() {
1429 obtainMessage(MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED,
1430 mZenController.getEffectsSuppressor()).sendToTarget();
1431 }
John Spurlock86005342014-05-23 11:58:00 -04001432 };
RoboErik19c95182014-06-23 15:38:48 -07001433
1434 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
RoboErikd2b8c942014-08-19 11:23:40 -07001435 public void onAudioInfoChanged(PlaybackInfo info) {
RoboErik19c95182014-06-23 15:38:48 -07001436 onRemoteVolumeUpdateIfShown();
1437 }
1438 };
John Spurlockae641c92014-06-30 18:11:40 -04001439
1440 public interface Callback {
1441 void onZenSettings();
1442 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001443 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445}