blob: f03c5eb281d34e8147aad01e9025c46398fd6551 [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;
29import android.content.pm.PackageManager.NameNotFoundException;
30import android.content.pm.ServiceInfo;
John Spurlock7e6809a2014-08-06 16:03:14 -040031import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.res.Resources;
John Spurlockad494bc2014-07-19 15:56:19 -040033import android.content.res.TypedArray;
John Spurlock2078caf2014-05-29 22:20:14 -040034import android.graphics.PixelFormat;
35import android.graphics.drawable.ColorDrawable;
John Spurlock7b414672014-07-18 13:02:39 -040036import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.media.AudioManager;
38import android.media.AudioService;
39import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070040import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070042import android.media.VolumeProvider;
43import android.media.session.MediaController;
RoboErikd2b8c942014-08-19 11:23:40 -070044import android.media.session.MediaController.PlaybackInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070045import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Handler;
47import android.os.Message;
48import android.os.Vibrator;
John Spurlockae641c92014-06-30 18:11:40 -040049import android.provider.Settings.Global;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040051import android.util.SparseArray;
John Spurlock2d28d6e2014-08-01 13:10:14 -040052import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040053import android.view.LayoutInflater;
54import android.view.MotionEvent;
55import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040056import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040057import android.view.ViewGroup;
58import android.view.Window;
59import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070060import android.view.WindowManager.LayoutParams;
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;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800164
Amith Yamasani71def772011-10-12 12:25:24 -0700165 private enum StreamResources {
166 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
167 R.string.volume_icon_description_bluetooth,
168 R.drawable.ic_audio_bt,
169 R.drawable.ic_audio_bt,
170 false),
171 RingerStream(AudioManager.STREAM_RING,
172 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400173 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400174 com.android.systemui.R.drawable.ic_ringer_vibrate,
Amith Yamasani71def772011-10-12 12:25:24 -0700175 false),
176 VoiceStream(AudioManager.STREAM_VOICE_CALL,
177 R.string.volume_icon_description_incall,
178 R.drawable.ic_audio_phone,
179 R.drawable.ic_audio_phone,
180 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700181 AlarmStream(AudioManager.STREAM_ALARM,
182 R.string.volume_alarm,
183 R.drawable.ic_audio_alarm,
184 R.drawable.ic_audio_alarm_mute,
185 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700186 MediaStream(AudioManager.STREAM_MUSIC,
187 R.string.volume_icon_description_media,
188 R.drawable.ic_audio_vol,
189 R.drawable.ic_audio_vol_mute,
190 true),
191 NotificationStream(AudioManager.STREAM_NOTIFICATION,
192 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400193 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400194 com.android.systemui.R.drawable.ic_ringer_vibrate,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400195 true),
196 // for now, use media resources for master volume
197 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700198 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400199 R.drawable.ic_audio_vol,
200 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700201 false),
RoboErik2811dd32014-08-12 09:48:13 -0700202 RemoteStream(STREAM_REMOTE_MUSIC,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700203 R.string.volume_icon_description_media, //FIXME should have its own description
204 R.drawable.ic_media_route_on_holo_dark,
205 R.drawable.ic_media_route_disabled_holo_dark,
206 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700207
208 int streamType;
209 int descRes;
210 int iconRes;
211 int iconMuteRes;
212 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
213 boolean show;
214
215 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
216 this.streamType = streamType;
217 this.descRes = descRes;
218 this.iconRes = iconRes;
219 this.iconMuteRes = iconMuteRes;
220 this.show = show;
221 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700222 }
Amith Yamasani71def772011-10-12 12:25:24 -0700223
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800224 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700225 private static final StreamResources[] STREAMS = {
226 StreamResources.BluetoothSCOStream,
227 StreamResources.RingerStream,
228 StreamResources.VoiceStream,
229 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700230 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400231 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700232 StreamResources.MasterStream,
233 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800234 };
235
236 /** Object that contains data for each slider */
237 private class StreamControl {
238 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700239 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800240 ViewGroup group;
241 ImageView icon;
242 SeekBar seekbarView;
John Spurlockb4782522014-08-22 14:54:46 -0400243 TextView suppressorView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800244 int iconRes;
245 int iconMuteRes;
John Spurlockb4782522014-08-22 14:54:46 -0400246 int iconSuppressedRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 // Synchronize when accessing this
250 private ToneGenerator mToneGenerators[];
251 private Vibrator mVibrator;
252
John Spurlock35134602014-07-24 18:10:48 -0400253 private static AlertDialog sSafetyWarning;
254 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700255
John Spurlock35134602014-07-24 18:10:48 -0400256 private static class SafetyWarning extends SystemUIDialog
257 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800258 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800259 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400260 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700261
John Spurlock2d28d6e2014-08-01 13:10:14 -0400262 private boolean mNewVolumeUp;
263
John Spurlock35134602014-07-24 18:10:48 -0400264 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
265 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700266 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800267 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400268 mAudioManager = audioManager;
269
270 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
271 setButton(DialogInterface.BUTTON_POSITIVE,
272 mContext.getString(com.android.internal.R.string.yes), this);
273 setButton(DialogInterface.BUTTON_NEGATIVE,
274 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
275 setOnDismissListener(this);
276
Eric Laurentc34dcc12012-09-10 13:51:52 -0700277 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400278 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700279 }
280
281 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400282 public boolean onKeyDown(int keyCode, KeyEvent event) {
283 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
284 mNewVolumeUp = true;
285 }
286 return super.onKeyDown(keyCode, event);
287 }
288
289 @Override
290 public boolean onKeyUp(int keyCode, KeyEvent event) {
291 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
292 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
293 mAudioManager.disableSafeMediaVolume();
294 dismiss();
295 }
296 return super.onKeyUp(keyCode, event);
297 }
298
299 @Override
John Spurlock35134602014-07-24 18:10:48 -0400300 public void onClick(DialogInterface dialog, int which) {
301 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700302 }
303
Alan Viverette494fb7b2014-04-10 18:12:56 -0700304 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700305 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400306 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800307 cleanUp();
308 }
309
310 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400311 synchronized (sSafetyWarningLock) {
312 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700313 }
John Spurlock1dad2722014-07-11 11:07:53 -0400314 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800315 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700316 }
John Spurlock35134602014-07-24 18:10:48 -0400317
318 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
319 @Override
320 public void onReceive(Context context, Intent intent) {
321 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
322 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
323 cancel();
324 cleanUp();
325 }
326 }
327 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700328 }
329
John Spurlockeb2727b2014-07-19 23:11:36 -0400330 public VolumePanel(Context context, ZenModeController zenController) {
331 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400333 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400336 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700337 final Resources res = context.getResources();
338 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400339 if (useMasterVolume) {
340 for (int i = 0; i < STREAMS.length; i++) {
341 StreamResources streamRes = STREAMS[i];
342 streamRes.show = (streamRes.streamType == STREAM_MASTER);
343 }
344 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400345 if (LOGD) Log.d(mTag, "new VolumePanel");
346
John Spurlockad494bc2014-07-19 15:56:19 -0400347 mDisabledAlpha = 0.5f;
348 if (mContext.getTheme() != null) {
349 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
350 new int[] { android.R.attr.disabledAlpha });
351 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
352 arr.recycle();
353 }
354
John Spurlockeb2727b2014-07-19 23:11:36 -0400355 mDialog = new Dialog(context) {
356 @Override
357 public boolean onTouchEvent(MotionEvent event) {
358 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400359 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400360 forceTimeout(0);
361 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700362 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400363 return false;
364 }
365 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700366
John Spurlockeb2727b2014-07-19 23:11:36 -0400367 final Window window = mDialog.getWindow();
John Spurlockeb2727b2014-07-19 23:11:36 -0400368 window.requestFeature(Window.FEATURE_NO_TITLE);
John Spurlockeb2727b2014-07-19 23:11:36 -0400369 mDialog.setCanceledOnTouchOutside(true);
370 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
371 mDialog.setOnDismissListener(new OnDismissListener() {
372 @Override
373 public void onDismiss(DialogInterface dialog) {
374 mActiveStreamType = -1;
375 mAudioManager.forceVolumeControlStream(mActiveStreamType);
376 setZenPanelVisible(false);
377 }
378 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700379
John Spurlockeb2727b2014-07-19 23:11:36 -0400380 mDialog.create();
Alan Viverette494fb7b2014-04-10 18:12:56 -0700381
John Spurlock7e6809a2014-08-06 16:03:14 -0400382 final LayoutParams lp = window.getAttributes();
383 lp.token = null;
384 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
385 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
386 lp.format = PixelFormat.TRANSLUCENT;
387 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
388 lp.setTitle(TAG);
389 window.setAttributes(lp);
390
391 updateWidth();
392
393 window.setBackgroundDrawable(new ColorDrawable(0x00000000));
394 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
395 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
396 | LayoutParams.FLAG_NOT_TOUCH_MODAL
397 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
398 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockeb2727b2014-07-19 23:11:36 -0400399 mView = window.findViewById(R.id.content);
400 mView.setOnTouchListener(new View.OnTouchListener() {
401 @Override
402 public boolean onTouch(View v, MotionEvent event) {
403 resetTimeout();
404 return false;
405 }
406 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700407
John Spurlock86005342014-05-23 11:58:00 -0400408 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
409 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400410 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
411 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700414 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700415 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700416
John Spurlock45601d62014-08-07 17:40:50 -0400417 if (mZenController != null && !useMasterVolume) {
418 mZenModeAvailable = mZenController.isZenAvailable();
419 mZenController.addCallback(mZenCallback);
420 }
Amith Yamasani42722bf2011-07-22 10:34:27 -0700421
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700422 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
423 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700424 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
425
John Spurlock8c79d2e2014-07-24 15:15:25 -0400426 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800427 }
428
John Spurlock7e6809a2014-08-06 16:03:14 -0400429 public void onConfigurationChanged(Configuration newConfig) {
430 updateWidth();
431 }
432
433 private void updateWidth() {
434 final Resources res = mContext.getResources();
435 final LayoutParams lp = mDialog.getWindow().getAttributes();
436 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.notification_panel_width);
437 lp.gravity =
438 res.getInteger(com.android.systemui.R.integer.notification_panel_layout_gravity);
439 mDialog.getWindow().setAttributes(lp);
440 }
441
John Spurlockad494bc2014-07-19 15:56:19 -0400442 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
443 pw.println("VolumePanel state:");
444 pw.print(" mTag="); pw.println(mTag);
445 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
446 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
John Spurlock45601d62014-08-07 17:40:50 -0400447 pw.print(" mZenModeAvailable="); pw.println(mZenModeAvailable);
John Spurlockad494bc2014-07-19 15:56:19 -0400448 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
449 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
450 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
451 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
452 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
453 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
454 pw.print(" isShowing()="); pw.println(isShowing());
455 pw.print(" mCallback="); pw.println(mCallback);
456 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400457 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400458 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
459 pw.print(" mStreamControls=");
460 if (mStreamControls == null) {
461 pw.println("null");
462 } else {
463 final int N = mStreamControls.size();
464 pw.print("<size "); pw.print(N); pw.println('>');
465 for (int i = 0; i < N; i++) {
466 final StreamControl sc = mStreamControls.valueAt(i);
467 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
468 if (sc.seekbarView != null) {
469 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
470 pw.print(" of "); pw.print(sc.seekbarView.getMax());
471 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
472 }
473 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
474 pw.println();
475 }
476 }
477 }
478
John Spurlockeb2727b2014-07-19 23:11:36 -0400479 private void initZenModePanel() {
480 mZenPanel.init(mZenController);
481 mZenPanel.setCallback(new ZenModePanel.Callback() {
482 @Override
483 public void onMoreSettings() {
484 if (mCallback != null) {
485 mCallback.onZenSettings();
486 }
487 }
488
489 @Override
490 public void onInteraction() {
491 resetTimeout();
492 }
493
494 @Override
495 public void onExpanded(boolean expanded) {
496 if (mZenPanelExpanded == expanded) return;
497 mZenPanelExpanded = expanded;
498 updateTimeoutDelay();
499 resetTimeout();
500 }
501 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400502 }
503
John Spurlock86005342014-05-23 11:58:00 -0400504 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800505 mPanel.setLayoutDirection(layoutDirection);
506 updateStates();
507 }
508
John Spurlock8c79d2e2014-07-24 15:15:25 -0400509 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800510 final IntentFilter filter = new IntentFilter();
511 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400512 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800513 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700514 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800515 public void onReceive(Context context, Intent intent) {
516 final String action = intent.getAction();
517
518 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
519 removeMessages(MSG_RINGER_MODE_CHANGED);
520 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
521 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400522
523 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
524 postDismiss(0);
525 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800526 }
527 }, filter);
528 }
529
530 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400531 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700532 return mAudioManager.isMasterMute();
RoboErik2811dd32014-08-12 09:48:13 -0700533 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700534 // TODO do we need to support a distinct mute property for remote?
535 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400536 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700537 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400538 }
539 }
540
541 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400542 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700543 return mAudioManager.getMasterMaxVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700544 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700545 if (mStreamControls != null) {
546 StreamControl sc = mStreamControls.get(streamType);
547 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700548 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700549 return ai.getMaxVolume();
RoboErik19c95182014-06-23 15:38:48 -0700550 }
551 }
552 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400553 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700554 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400555 }
556 }
557
558 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400559 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700560 return mAudioManager.getMasterVolume();
RoboErik2811dd32014-08-12 09:48:13 -0700561 } else if (streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700562 if (mStreamControls != null) {
563 StreamControl sc = mStreamControls.get(streamType);
564 if (sc != null && sc.controller != null) {
RoboErikd2b8c942014-08-19 11:23:40 -0700565 PlaybackInfo ai = sc.controller.getPlaybackInfo();
RoboErika66c40b2014-08-15 15:21:41 -0700566 return ai.getCurrentVolume();
RoboErik19c95182014-06-23 15:38:48 -0700567 }
568 }
569 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400570 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700571 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400572 }
573 }
574
RoboErik19c95182014-06-23 15:38:48 -0700575 private void setStreamVolume(StreamControl sc, int index, int flags) {
RoboErik2811dd32014-08-12 09:48:13 -0700576 if (sc.streamType == STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700577 if (sc.controller != null) {
578 sc.controller.setVolumeTo(index, flags);
579 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700580 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700581 }
582 } else if (getStreamVolume(sc.streamType) != index) {
583 if (sc.streamType == STREAM_MASTER) {
584 mAudioManager.setMasterVolume(index, flags);
585 } else {
586 mAudioManager.setStreamVolume(sc.streamType, index, flags);
587 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400588 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800589 }
590
591 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700592 final Resources res = mContext.getResources();
593 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
594 Context.LAYOUT_INFLATER_SERVICE);
595
John Spurlockad494bc2014-07-19 15:56:19 -0400596 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700597
Amith Yamasani71def772011-10-12 12:25:24 -0700598 for (int i = 0; i < STREAMS.length; i++) {
599 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700600
601 final int streamType = streamRes.streamType;
Alan Viverette494fb7b2014-04-10 18:12:56 -0700602
603 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700604 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400605 sc.group = (ViewGroup) inflater.inflate(
606 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800607 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400608 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800609 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700610 sc.icon.setContentDescription(res.getString(streamRes.descRes));
611 sc.iconRes = streamRes.iconRes;
612 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800613 sc.icon.setImageResource(sc.iconRes);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400614 sc.icon.setClickable(isNotificationOrRing(streamType));
615 if (sc.icon.isClickable()) {
John Spurlockab4ea762014-07-19 18:50:16 -0400616 sc.icon.setSoundEffectsEnabled(false);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400617 sc.icon.setOnClickListener(new OnClickListener() {
618 @Override
619 public void onClick(View v) {
620 resetTimeout();
621 toggle(sc);
622 }
623 });
John Spurlockb4782522014-08-22 14:54:46 -0400624 sc.iconSuppressedRes = com.android.systemui.R.drawable.ic_ringer_mute;
John Spurlock7f1df5e2014-05-31 19:11:40 -0400625 }
John Spurlock86005342014-05-23 11:58:00 -0400626 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
John Spurlockb4782522014-08-22 14:54:46 -0400627 sc.suppressorView =
628 (TextView) sc.group.findViewById(com.android.systemui.R.id.suppressor);
629 sc.suppressorView.setVisibility(View.GONE);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700630 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700631 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400632 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700633 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800634 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700635 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800636 }
637 }
638
John Spurlock7f1df5e2014-05-31 19:11:40 -0400639 private void toggle(StreamControl sc) {
640 if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
641 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
642 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
643 } else {
644 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
645 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
646 }
647 }
648
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800649 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400650 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800651
Alan Viverette494fb7b2014-04-10 18:12:56 -0700652 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800653 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400654 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800655 mActiveStreamType = -1;
656 } else {
John Spurlock86005342014-05-23 11:58:00 -0400657 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800658 mActiveStreamType = activeStreamType;
659 active.group.setVisibility(View.VISIBLE);
660 updateSlider(active);
John Spurlock8845da72014-07-07 21:29:48 -0400661 updateTimeoutDelay();
John Spurlock45601d62014-08-07 17:40:50 -0400662 updateZenPanelVisible();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800663 }
664 }
665
John Spurlockad494bc2014-07-19 15:56:19 -0400666 private void updateSliderProgress(StreamControl sc, int progress) {
667 final boolean isRinger = isNotificationOrRing(sc.streamType);
668 if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
669 progress = mLastRingerProgress;
670 }
671 if (progress < 0) {
672 progress = getStreamVolume(sc.streamType);
673 }
674 sc.seekbarView.setProgress(progress);
675 if (isRinger) {
676 mLastRingerProgress = progress;
677 }
678 }
679
680 private void updateSliderIcon(StreamControl sc, boolean muted) {
681 if (isNotificationOrRing(sc.streamType)) {
682 int ringerMode = mAudioManager.getRingerMode();
683 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
684 ringerMode = mLastRingerMode;
685 } else {
686 mLastRingerMode = ringerMode;
687 }
688 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
689 }
690 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
691 }
692
John Spurlockb4782522014-08-22 14:54:46 -0400693 private void updateSliderSupressor(StreamControl sc) {
694 final ComponentName suppressor = isNotificationOrRing(sc.streamType)
695 ? mZenController.getEffectsSuppressor() : null;
696 if (suppressor == null) {
697 sc.seekbarView.setVisibility(View.VISIBLE);
698 sc.suppressorView.setVisibility(View.GONE);
699 } else {
700 sc.seekbarView.setVisibility(View.GONE);
701 sc.suppressorView.setVisibility(View.VISIBLE);
702 sc.suppressorView.setText(mContext.getString(com.android.systemui.R.string.muted_by,
703 getSuppressorCaption(suppressor)));
704 sc.icon.setImageResource(sc.iconSuppressedRes);
705 }
706 }
707
708 private String getSuppressorCaption(ComponentName suppressor) {
709 final PackageManager pm = mContext.getPackageManager();
710 try {
711 final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
712 if (info != null) {
713 final CharSequence seq = info.loadLabel(pm);
714 if (seq != null) {
715 final String str = seq.toString().trim();
716 if (str.length() > 0) {
717 return str;
718 }
719 }
720 }
721 } catch (Throwable e) {
722 Log.w(TAG, "Error loading suppressor caption", e);
723 }
724 return suppressor.getPackageName();
725 }
726
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800727 /** Update the mute and progress state of a slider */
728 private void updateSlider(StreamControl sc) {
John Spurlockad494bc2014-07-19 15:56:19 -0400729 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800730 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800731 // Force reloading the image resource
732 sc.icon.setImageDrawable(null);
John Spurlockad494bc2014-07-19 15:56:19 -0400733 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400734 updateSliderEnabled(sc, muted, false);
John Spurlockb4782522014-08-22 14:54:46 -0400735 updateSliderSupressor(sc);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400736 }
737
John Spurlock5f640e42014-05-31 20:15:59 -0400738 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
739 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400740 final boolean isRinger = isNotificationOrRing(sc.streamType);
RoboErik2811dd32014-08-12 09:48:13 -0700741 if (sc.streamType == STREAM_REMOTE_MUSIC) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700742 // never disable touch interactions for remote playback, the muting is not tied to
743 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700744 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlockae641c92014-06-30 18:11:40 -0400745 } else if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400746 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400747 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400748 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400749 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400750 } else if (fixedVolume ||
751 (sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
752 (sSafetyWarning != null)) {
753 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700754 } else {
755 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400756 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400757 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700758 }
John Spurlockae641c92014-06-30 18:11:40 -0400759 // show the silent hint when the disabled slider is touched in silent mode
760 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400761 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400762 sc.group.setOnTouchListener(null);
763 sc.icon.setClickable(true);
John Spurlock5f640e42014-05-31 20:15:59 -0400764 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400765 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400766 @Override
767 public boolean onTouch(View v, MotionEvent event) {
768 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400769 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400770 return false;
771 }
John Spurlockae641c92014-06-30 18:11:40 -0400772 };
773 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400774 }
775 }
776 }
777
John Spurlockae641c92014-06-30 18:11:40 -0400778 private void showSilentHint() {
779 if (mZenPanel != null) {
780 mZenPanel.showSilentHint();
781 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800782 }
783
John Spurlock7f1df5e2014-05-31 19:11:40 -0400784 private static boolean isNotificationOrRing(int streamType) {
785 return streamType == AudioManager.STREAM_RING
786 || streamType == AudioManager.STREAM_NOTIFICATION;
787 }
788
John Spurlockae641c92014-06-30 18:11:40 -0400789 public void setCallback(Callback callback) {
790 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400791 }
792
John Spurlock8845da72014-07-07 21:29:48 -0400793 private void updateTimeoutDelay() {
John Spurlock35134602014-07-24 18:10:48 -0400794 mTimeoutDelay = sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
795 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400796 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
797 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
798 : TIMEOUT_DELAY;
799 }
800
801 private boolean isZenPanelVisible() {
802 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400803 }
804
John Spurlockae641c92014-06-30 18:11:40 -0400805 private void setZenPanelVisible(boolean visible) {
806 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400807 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400808 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400809 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400810 resetTimeout();
811 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400812 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800813 }
John Spurlockea9938c2014-07-11 18:51:32 -0400814 if (changing) {
815 updateTimeoutDelay();
816 resetTimeout();
817 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800818 }
819
Eric Laurentfde16d52012-12-03 14:42:39 -0800820 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400821 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800822 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400823 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800824 updateSlider(sc);
825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827
John Spurlock45601d62014-08-07 17:40:50 -0400828 private void updateZenPanelVisible() {
829 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400830 }
831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 public void postVolumeChanged(int streamType, int flags) {
833 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700834 synchronized (this) {
835 if (mStreamControls == null) {
836 createSliders();
837 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 removeMessages(MSG_FREE_RESOURCES);
840 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
841 }
842
RoboErik19c95182014-06-23 15:38:48 -0700843 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700844 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
845 synchronized (this) {
846 if (mStreamControls == null) {
847 createSliders();
848 }
849 }
850 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700851 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700852 }
853
854 public void postRemoteSliderVisibility(boolean visible) {
855 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
RoboErik2811dd32014-08-12 09:48:13 -0700856 STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700857 }
858
859 /**
860 * Called by AudioService when it has received new remote playback information that
861 * would affect the VolumePanel display (mainly volumes). The difference with
862 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
863 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
864 * displayed.
865 * This special code path is due to the fact that remote volume updates arrive to AudioService
866 * asynchronously. So after AudioService has sent the volume update (which should be treated
867 * as a request to update the volume), the application will likely set a new volume. If the UI
868 * is still up, we need to refresh the display to show this new value.
869 */
870 public void postHasNewRemotePlaybackInfo() {
871 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
872 // don't create or prevent resources to be freed, if they disappear, this update came too
873 // late and shouldn't warrant the panel to be displayed longer
874 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
875 }
876
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400877 public void postMasterVolumeChanged(int flags) {
878 postVolumeChanged(STREAM_MASTER, flags);
879 }
880
Mike Lockwoodce952c82011-11-14 10:47:42 -0800881 public void postMuteChanged(int streamType, int flags) {
882 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700883 synchronized (this) {
884 if (mStreamControls == null) {
885 createSliders();
886 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800887 }
888 removeMessages(MSG_FREE_RESOURCES);
889 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
890 }
891
892 public void postMasterMuteChanged(int flags) {
893 postMuteChanged(STREAM_MASTER, flags);
894 }
895
Eric Laurentfde16d52012-12-03 14:42:39 -0800896 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700897 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800898 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700899 }
900
John Spurlock1dad2722014-07-11 11:07:53 -0400901 public void postDismiss(long delay) {
902 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400903 }
904
905 public void postLayoutDirection(int layoutDirection) {
906 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400907 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400908 }
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 /**
911 * Override this if you have other work to do when the volume changes (for
912 * example, vibrating, playing a sound, etc.). Make sure to call through to
913 * the superclass implementation.
914 */
915 protected void onVolumeChanged(int streamType, int flags) {
916
John Spurlock86005342014-05-23 11:58:00 -0400917 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
919 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700920 synchronized (this) {
921 if (mActiveStreamType != streamType) {
922 reorderSliders(streamType);
923 }
RoboErik19c95182014-06-23 15:38:48 -0700924 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927
Marco Nelissen69f593c2009-07-28 09:55:04 -0700928 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 removeMessages(MSG_PLAY_SOUND);
930 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
931 }
932
933 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
934 removeMessages(MSG_PLAY_SOUND);
935 removeMessages(MSG_VIBRATE);
936 onStopSounds();
937 }
938
939 removeMessages(MSG_FREE_RESOURCES);
940 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800941 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943
Mike Lockwoodce952c82011-11-14 10:47:42 -0800944 protected void onMuteChanged(int streamType, int flags) {
945
John Spurlock86005342014-05-23 11:58:00 -0400946 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800947
948 StreamControl sc = mStreamControls.get(streamType);
949 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400950 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -0800951 }
952
953 onVolumeChanged(streamType, flags);
954 }
955
RoboErik19c95182014-06-23 15:38:48 -0700956 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -0700957 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800958
Marco Nelissen69f593c2009-07-28 09:55:04 -0700959 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960
961 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400962 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 + ", flags: " + flags + "), index: " + index);
964 }
965
966 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800967
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400968 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -0700969 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
971 switch (streamType) {
972
973 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800974// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700975 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
976 mContext, RingtoneManager.TYPE_RINGTONE);
977 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700978 mRingIsSilent = true;
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 break;
981 }
982
983 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800984 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800985 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
986 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
987 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
988 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800989 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800991 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993 break;
994 }
995
996 case AudioManager.STREAM_VOICE_CALL: {
997 /*
998 * For in-call voice call volume, there is no inaudible volume.
999 * Rescale the UI control so the progress bar doesn't go all
1000 * the way to zero and don't show the mute icon.
1001 */
1002 index++;
1003 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 break;
1005 }
1006
1007 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 break;
1009 }
1010
1011 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001012 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1013 mContext, RingtoneManager.TYPE_NOTIFICATION);
1014 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001015 mRingIsSilent = true;
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 break;
1018 }
1019
1020 case AudioManager.STREAM_BLUETOOTH_SCO: {
1021 /*
1022 * For in-call voice call volume, there is no inaudible volume.
1023 * Rescale the UI control so the progress bar doesn't go all
1024 * the way to zero and don't show the mute icon.
1025 */
1026 index++;
1027 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 break;
1029 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001030
RoboErik2811dd32014-08-12 09:48:13 -07001031 case STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -07001032 if (controller == null && sc != null) {
1033 // If we weren't passed one try using the last one set.
1034 controller = sc.controller;
1035 }
1036 if (controller == null) {
1037 // We still don't have one, ignore the command.
1038 Log.w(mTag, "sent remote volume change without a controller!");
1039 } else {
RoboErikd2b8c942014-08-19 11:23:40 -07001040 PlaybackInfo vi = controller.getPlaybackInfo();
RoboErik19c95182014-06-23 15:38:48 -07001041 index = vi.getCurrentVolume();
1042 max = vi.getMaxVolume();
1043 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
1044 // if the remote volume is fixed add the flag for the UI
1045 flags |= AudioManager.FLAG_FIXED_VOLUME;
1046 }
1047 }
John Spurlock86005342014-05-23 11:58:00 -04001048 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001049 break;
1050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001053 if (sc != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001054 if (streamType == STREAM_REMOTE_MUSIC && controller != sc.controller) {
RoboErik19c95182014-06-23 15:38:48 -07001055 if (sc.controller != null) {
1056 sc.controller.removeCallback(mMediaControllerCb);
1057 }
1058 sc.controller = controller;
1059 if (controller != null) {
1060 sc.controller.addCallback(mMediaControllerCb);
1061 }
1062 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001063 if (sc.seekbarView.getMax() != max) {
1064 sc.seekbarView.setMax(max);
1065 }
John Spurlockad494bc2014-07-19 15:56:19 -04001066 updateSliderProgress(sc, index);
John Spurlock7f1df5e2014-05-31 19:11:40 -04001067 updateSliderEnabled(sc, isMuted(streamType),
1068 (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070
John Spurlock86005342014-05-23 11:58:00 -04001071 if (!isShowing()) {
RoboErik2811dd32014-08-12 09:48:13 -07001072 int stream = (streamType == STREAM_REMOTE_MUSIC) ? -1 : streamType;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001073 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1074 mAudioManager.forceVolumeControlStream(stream);
John Spurlockeb2727b2014-07-19 23:11:36 -04001075 mDialog.show();
1076 if (mCallback != null) {
1077 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 // Do a little vibrate if applicable (only when going into vibrate mode)
RoboErik2811dd32014-08-12 09:48:13 -07001082 if ((streamType != STREAM_REMOTE_MUSIC) &&
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001083 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock3346a802014-05-20 16:25:37 -04001084 mAudioManager.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -07001085 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1087 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001088
1089 // Pulse the slider icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001090 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1091 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
John Spurlock86005342014-05-23 11:58:00 -04001095 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001096 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001097 }
1098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 protected void onPlaySound(int streamType, int flags) {
1100
1101 if (hasMessages(MSG_STOP_SOUNDS)) {
1102 removeMessages(MSG_STOP_SOUNDS);
1103 // Force stop right now
1104 onStopSounds();
1105 }
1106
1107 synchronized (this) {
1108 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001109 if (toneGen != null) {
1110 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1111 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115
1116 protected void onStopSounds() {
1117
1118 synchronized (this) {
1119 int numStreamTypes = AudioSystem.getNumStreamTypes();
1120 for (int i = numStreamTypes - 1; i >= 0; i--) {
1121 ToneGenerator toneGen = mToneGenerators[i];
1122 if (toneGen != null) {
1123 toneGen.stopTone();
1124 }
1125 }
1126 }
1127 }
1128
1129 protected void onVibrate() {
1130
1131 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -07001132 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 return;
1134 }
1135
John Spurlock7b414672014-07-18 13:02:39 -04001136 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138
RoboErik19c95182014-06-23 15:38:48 -07001139 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
1140 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: " + flags
1141 + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001142
John Spurlock86005342014-05-23 11:58:00 -04001143 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001144 synchronized (this) {
RoboErik2811dd32014-08-12 09:48:13 -07001145 if (mActiveStreamType != STREAM_REMOTE_MUSIC) {
1146 reorderSliders(STREAM_REMOTE_MUSIC);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001147 }
RoboErik2811dd32014-08-12 09:48:13 -07001148 onShowVolumeChanged(STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001149 }
1150 } else {
John Spurlock86005342014-05-23 11:58:00 -04001151 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001152 }
1153
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001154 removeMessages(MSG_FREE_RESOURCES);
1155 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001156 resetTimeout();
1157 }
1158
1159 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001160 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1161 if (isShowing()
RoboErik2811dd32014-08-12 09:48:13 -07001162 && (mActiveStreamType == STREAM_REMOTE_MUSIC)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001163 && (mStreamControls != null)) {
RoboErik2811dd32014-08-12 09:48:13 -07001164 onShowVolumeChanged(STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001165 }
1166 }
1167
RoboErik19c95182014-06-23 15:38:48 -07001168 /**
1169 * Clear the current remote stream controller.
1170 */
1171 private void clearRemoteStreamController() {
1172 if (mStreamControls != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001173 StreamControl sc = mStreamControls.get(STREAM_REMOTE_MUSIC);
RoboErik19c95182014-06-23 15:38:48 -07001174 if (sc != null) {
1175 if (sc.controller != null) {
1176 sc.controller.removeCallback(mMediaControllerCb);
1177 sc.controller = null;
1178 }
1179 }
1180 }
1181 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001182
1183 /**
RoboErik2811dd32014-08-12 09:48:13 -07001184 * Handler for MSG_SLIDER_VISIBILITY_CHANGED Hide or show a slider
1185 *
1186 * @param streamType can be a valid stream type value, or
1187 * VolumePanel.STREAM_MASTER, or VolumePanel.STREAM_REMOTE_MUSIC
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001188 * @param visible
1189 */
1190 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001191 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001192 boolean isVisible = (visible == 1);
1193 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1194 StreamResources streamRes = STREAMS[i];
1195 if (streamRes.streamType == streamType) {
1196 streamRes.show = isVisible;
1197 if (!isVisible && (mActiveStreamType == streamType)) {
1198 mActiveStreamType = -1;
1199 }
1200 break;
1201 }
1202 }
1203 }
1204
Eric Laurentfde16d52012-12-03 14:42:39 -08001205 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001206 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1207 || isShowing()) {
1208 synchronized (sSafetyWarningLock) {
1209 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001210 return;
1211 }
John Spurlock35134602014-07-24 18:10:48 -04001212 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1213 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001214 }
1215 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001216 }
Eric Laurentfde16d52012-12-03 14:42:39 -08001217 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001218 }
1219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1222 */
1223 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001224 if (streamType == STREAM_MASTER) {
1225 // For devices that use the master volume setting only but still want to
1226 // play a volume-changed tone, direct the master volume pseudostream to
1227 // the system stream's tone generator.
1228 if (mPlayMasterStreamTones) {
1229 streamType = AudioManager.STREAM_SYSTEM;
1230 } else {
1231 return null;
1232 }
1233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 synchronized (this) {
1235 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001236 try {
1237 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1238 } catch (RuntimeException e) {
1239 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001240 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001241 + "RuntimeException: " + e);
1242 }
1243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001245 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247 }
1248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
1250 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001251 * Switch between icons because Bluetooth music is same as music volume, but with
1252 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001254 private void setMusicIcon(int resId, int resMuteId) {
1255 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1256 if (sc != null) {
1257 sc.iconRes = resId;
1258 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001259 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262
1263 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 synchronized (this) {
1265 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1266 if (mToneGenerators[i] != null) {
1267 mToneGenerators[i].release();
1268 }
1269 mToneGenerators[i] = null;
1270 }
1271 }
1272 }
1273
1274 @Override
1275 public void handleMessage(Message msg) {
1276 switch (msg.what) {
1277
1278 case MSG_VOLUME_CHANGED: {
1279 onVolumeChanged(msg.arg1, msg.arg2);
1280 break;
1281 }
1282
Mike Lockwoodce952c82011-11-14 10:47:42 -08001283 case MSG_MUTE_CHANGED: {
1284 onMuteChanged(msg.arg1, msg.arg2);
1285 break;
1286 }
1287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 case MSG_FREE_RESOURCES: {
1289 onFreeResources();
1290 break;
1291 }
1292
1293 case MSG_STOP_SOUNDS: {
1294 onStopSounds();
1295 break;
1296 }
1297
1298 case MSG_PLAY_SOUND: {
1299 onPlaySound(msg.arg1, msg.arg2);
1300 break;
1301 }
1302
1303 case MSG_VIBRATE: {
1304 onVibrate();
1305 break;
1306 }
1307
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001308 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001309 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001310 mDialog.dismiss();
1311 clearRemoteStreamController();
1312 mActiveStreamType = -1;
1313 if (mCallback != null) {
1314 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001315 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001316 }
John Spurlock35134602014-07-24 18:10:48 -04001317 synchronized (sSafetyWarningLock) {
1318 if (sSafetyWarning != null) {
1319 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1320 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001321 }
1322 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001323 break;
1324 }
John Spurlockb4782522014-08-22 14:54:46 -04001325
1326 case MSG_RINGER_MODE_CHANGED:
1327 case MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001328 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001329 updateStates();
1330 }
1331 break;
1332 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001333
1334 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001335 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001336 break;
1337 }
1338
1339 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1340 onRemoteVolumeUpdateIfShown();
1341 break;
1342
1343 case MSG_SLIDER_VISIBILITY_CHANGED:
1344 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1345 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001346
1347 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001348 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001349 break;
John Spurlock86005342014-05-23 11:58:00 -04001350
1351 case MSG_LAYOUT_DIRECTION:
1352 setLayoutDirection(msg.arg1);
1353 break;
1354
John Spurlock45601d62014-08-07 17:40:50 -04001355 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1356 mZenModeAvailable = msg.arg1 != 0;
1357 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001358 break;
1359
1360 case MSG_USER_ACTIVITY:
1361 if (mCallback != null) {
1362 mCallback.onInteraction();
1363 }
John Spurlock86005342014-05-23 11:58:00 -04001364 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
1367
John Spurlockae641c92014-06-30 18:11:40 -04001368 private void resetTimeout() {
John Spurlockea9938c2014-07-11 18:51:32 -04001369 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
1370 + " delay=" + mTimeoutDelay);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001371 removeMessages(MSG_TIMEOUT);
John Spurlock3bd4fee2014-05-29 20:51:09 -04001372 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
John Spurlockae641c92014-06-30 18:11:40 -04001373 removeMessages(MSG_USER_ACTIVITY);
1374 sendEmptyMessage(MSG_USER_ACTIVITY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001375 }
1376
John Spurlock1dad2722014-07-11 11:07:53 -04001377 private void forceTimeout(long delay) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001378 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001379 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001380 }
1381
1382 public ZenModeController getZenController() {
1383 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001384 }
1385
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001386 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1387 @Override
1388 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1389 final Object tag = seekBar.getTag();
1390 if (fromUser && tag instanceof StreamControl) {
1391 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001392 setStreamVolume(sc, progress,
1393 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001394 }
1395 resetTimeout();
1396 }
1397
1398 @Override
1399 public void onStartTrackingTouch(SeekBar seekBar) {
1400 }
1401
1402 @Override
1403 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001404 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001405 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001406
John Spurlock86005342014-05-23 11:58:00 -04001407 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockb4782522014-08-22 14:54:46 -04001408 @Override
John Spurlock45601d62014-08-07 17:40:50 -04001409 public void onZenAvailableChanged(boolean available) {
1410 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001411 }
John Spurlockb4782522014-08-22 14:54:46 -04001412 @Override
1413 public void onEffectsSupressorChanged() {
1414 obtainMessage(MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED,
1415 mZenController.getEffectsSuppressor()).sendToTarget();
1416 }
John Spurlock86005342014-05-23 11:58:00 -04001417 };
RoboErik19c95182014-06-23 15:38:48 -07001418
1419 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
RoboErikd2b8c942014-08-19 11:23:40 -07001420 public void onAudioInfoChanged(PlaybackInfo info) {
RoboErik19c95182014-06-23 15:38:48 -07001421 onRemoteVolumeUpdateIfShown();
1422 }
1423 };
John Spurlockae641c92014-06-30 18:11:40 -04001424
1425 public interface Callback {
1426 void onZenSettings();
1427 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001428 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430}