blob: 12c887ef27cee80dc64150bccad70830e83aff0b [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 Cinek9f6ceb12014-08-29 16:28:39 +020059import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080061import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080062import android.widget.SeekBar.OnSeekBarChangeListener;
John Spurlockb4782522014-08-22 14:54:46 -040063import android.widget.TextView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080064
John Spurlock86005342014-05-23 11:58:00 -040065import com.android.internal.R;
John Spurlock35134602014-07-24 18:10:48 -040066import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040067import com.android.systemui.statusbar.policy.ZenModeController;
68
John Spurlockad494bc2014-07-19 15:56:19 -040069import java.io.FileDescriptor;
70import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72/**
John Spurlock3346a802014-05-20 16:25:37 -040073 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * @hide
76 */
John Spurlock3346a802014-05-20 16:25:37 -040077public class VolumePanel extends Handler {
John Spurlockae641c92014-06-30 18:11:40 -040078 private static final String TAG = "VolumePanel";
79 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
John Spurlock3346a802014-05-20 16:25:37 -040081 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 /**
84 * The delay before vibrating. This small period exists so if the user is
85 * moving to silent mode, it will not emit a short vibrate (it normally
86 * would since vibrate is between normal mode and silent mode using hardware
87 * keys).
88 */
89 public static final int VIBRATE_DELAY = 300;
90
91 private static final int VIBRATE_DURATION = 300;
92 private static final int BEEP_DURATION = 150;
93 private static final int MAX_VOLUME = 100;
94 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080095 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -040096 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -040097 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -040098 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
Selim Cinek9f6ceb12014-08-29 16:28:39 +020099 private static final int TIMEOUT_DELAY_SAFETY_WARNING_TALKBACK = 25000;
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 Cinek9f6ceb12014-08-29 16:28:39 +0200797 mTimeoutDelay = sSafetyWarning != null ? mAccessibilityManager.isEnabled() ?
798 TIMEOUT_DELAY_SAFETY_WARNING_TALKBACK : TIMEOUT_DELAY_SAFETY_WARNING
John Spurlock35134602014-07-24 18:10:48 -0400799 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400800 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
801 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
802 : TIMEOUT_DELAY;
803 }
804
805 private boolean isZenPanelVisible() {
806 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400807 }
808
John Spurlockae641c92014-06-30 18:11:40 -0400809 private void setZenPanelVisible(boolean visible) {
810 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400811 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400812 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400813 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400814 resetTimeout();
815 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400816 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800817 }
John Spurlockea9938c2014-07-11 18:51:32 -0400818 if (changing) {
819 updateTimeoutDelay();
820 resetTimeout();
821 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800822 }
823
Eric Laurentfde16d52012-12-03 14:42:39 -0800824 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400825 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800826 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400827 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800828 updateSlider(sc);
829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831
John Spurlock45601d62014-08-07 17:40:50 -0400832 private void updateZenPanelVisible() {
833 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400834 }
835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 public void postVolumeChanged(int streamType, int flags) {
837 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700838 synchronized (this) {
839 if (mStreamControls == null) {
840 createSliders();
841 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 removeMessages(MSG_FREE_RESOURCES);
844 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
845 }
846
RoboErik19c95182014-06-23 15:38:48 -0700847 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700848 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
849 synchronized (this) {
850 if (mStreamControls == null) {
851 createSliders();
852 }
853 }
854 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700855 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700856 }
857
858 public void postRemoteSliderVisibility(boolean visible) {
859 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
RoboErik2811dd32014-08-12 09:48:13 -0700860 STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700861 }
862
863 /**
864 * Called by AudioService when it has received new remote playback information that
865 * would affect the VolumePanel display (mainly volumes). The difference with
866 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
867 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
868 * displayed.
869 * This special code path is due to the fact that remote volume updates arrive to AudioService
870 * asynchronously. So after AudioService has sent the volume update (which should be treated
871 * as a request to update the volume), the application will likely set a new volume. If the UI
872 * is still up, we need to refresh the display to show this new value.
873 */
874 public void postHasNewRemotePlaybackInfo() {
875 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
876 // don't create or prevent resources to be freed, if they disappear, this update came too
877 // late and shouldn't warrant the panel to be displayed longer
878 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
879 }
880
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400881 public void postMasterVolumeChanged(int flags) {
882 postVolumeChanged(STREAM_MASTER, flags);
883 }
884
Mike Lockwoodce952c82011-11-14 10:47:42 -0800885 public void postMuteChanged(int streamType, int flags) {
886 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700887 synchronized (this) {
888 if (mStreamControls == null) {
889 createSliders();
890 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800891 }
892 removeMessages(MSG_FREE_RESOURCES);
893 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
894 }
895
896 public void postMasterMuteChanged(int flags) {
897 postMuteChanged(STREAM_MASTER, flags);
898 }
899
Eric Laurentfde16d52012-12-03 14:42:39 -0800900 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700901 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800902 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700903 }
904
John Spurlock1dad2722014-07-11 11:07:53 -0400905 public void postDismiss(long delay) {
906 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400907 }
908
909 public void postLayoutDirection(int layoutDirection) {
910 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400911 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400912 }
913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 /**
915 * Override this if you have other work to do when the volume changes (for
916 * example, vibrating, playing a sound, etc.). Make sure to call through to
917 * the superclass implementation.
918 */
919 protected void onVolumeChanged(int streamType, int flags) {
920
John Spurlock86005342014-05-23 11:58:00 -0400921 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700924 synchronized (this) {
925 if (mActiveStreamType != streamType) {
926 reorderSliders(streamType);
927 }
RoboErik19c95182014-06-23 15:38:48 -0700928 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931
Marco Nelissen69f593c2009-07-28 09:55:04 -0700932 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 removeMessages(MSG_PLAY_SOUND);
934 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
935 }
936
937 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
938 removeMessages(MSG_PLAY_SOUND);
939 removeMessages(MSG_VIBRATE);
940 onStopSounds();
941 }
942
943 removeMessages(MSG_FREE_RESOURCES);
944 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800945 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
947
Mike Lockwoodce952c82011-11-14 10:47:42 -0800948 protected void onMuteChanged(int streamType, int flags) {
949
John Spurlock86005342014-05-23 11:58:00 -0400950 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800951
952 StreamControl sc = mStreamControls.get(streamType);
953 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400954 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -0800955 }
956
957 onVolumeChanged(streamType, flags);
958 }
959
RoboErik19c95182014-06-23 15:38:48 -0700960 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -0700961 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800962
Marco Nelissen69f593c2009-07-28 09:55:04 -0700963 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
965 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400966 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 + ", flags: " + flags + "), index: " + index);
968 }
969
970 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800971
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400972 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -0700973 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974
975 switch (streamType) {
976
977 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800978// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700979 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
980 mContext, RingtoneManager.TYPE_RINGTONE);
981 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700982 mRingIsSilent = true;
983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 break;
985 }
986
987 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800988 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800989 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
990 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
991 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
992 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800993 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800995 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 break;
998 }
999
1000 case AudioManager.STREAM_VOICE_CALL: {
1001 /*
1002 * For in-call voice call volume, there is no inaudible volume.
1003 * Rescale the UI control so the progress bar doesn't go all
1004 * the way to zero and don't show the mute icon.
1005 */
1006 index++;
1007 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 break;
1009 }
1010
1011 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 break;
1013 }
1014
1015 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001016 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
1017 mContext, RingtoneManager.TYPE_NOTIFICATION);
1018 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001019 mRingIsSilent = true;
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 break;
1022 }
1023
1024 case AudioManager.STREAM_BLUETOOTH_SCO: {
1025 /*
1026 * For in-call voice call volume, there is no inaudible volume.
1027 * Rescale the UI control so the progress bar doesn't go all
1028 * the way to zero and don't show the mute icon.
1029 */
1030 index++;
1031 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 break;
1033 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001034
RoboErik2811dd32014-08-12 09:48:13 -07001035 case STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -07001036 if (controller == null && sc != null) {
1037 // If we weren't passed one try using the last one set.
1038 controller = sc.controller;
1039 }
1040 if (controller == null) {
1041 // We still don't have one, ignore the command.
1042 Log.w(mTag, "sent remote volume change without a controller!");
1043 } else {
RoboErikd2b8c942014-08-19 11:23:40 -07001044 PlaybackInfo vi = controller.getPlaybackInfo();
RoboErik19c95182014-06-23 15:38:48 -07001045 index = vi.getCurrentVolume();
1046 max = vi.getMaxVolume();
1047 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
1048 // if the remote volume is fixed add the flag for the UI
1049 flags |= AudioManager.FLAG_FIXED_VOLUME;
1050 }
1051 }
John Spurlock86005342014-05-23 11:58:00 -04001052 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001053 break;
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001057 if (sc != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001058 if (streamType == STREAM_REMOTE_MUSIC && controller != sc.controller) {
RoboErik19c95182014-06-23 15:38:48 -07001059 if (sc.controller != null) {
1060 sc.controller.removeCallback(mMediaControllerCb);
1061 }
1062 sc.controller = controller;
1063 if (controller != null) {
1064 sc.controller.addCallback(mMediaControllerCb);
1065 }
1066 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001067 if (sc.seekbarView.getMax() != max) {
1068 sc.seekbarView.setMax(max);
1069 }
John Spurlockad494bc2014-07-19 15:56:19 -04001070 updateSliderProgress(sc, index);
John Spurlock7f1df5e2014-05-31 19:11:40 -04001071 updateSliderEnabled(sc, isMuted(streamType),
1072 (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074
John Spurlock86005342014-05-23 11:58:00 -04001075 if (!isShowing()) {
RoboErik2811dd32014-08-12 09:48:13 -07001076 int stream = (streamType == STREAM_REMOTE_MUSIC) ? -1 : streamType;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001077 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1078 mAudioManager.forceVolumeControlStream(stream);
John Spurlockeb2727b2014-07-19 23:11:36 -04001079 mDialog.show();
1080 if (mCallback != null) {
1081 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001082 }
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
John Spurlock86005342014-05-23 11:58:00 -04001099 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001100 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 protected void onPlaySound(int streamType, int flags) {
1104
1105 if (hasMessages(MSG_STOP_SOUNDS)) {
1106 removeMessages(MSG_STOP_SOUNDS);
1107 // Force stop right now
1108 onStopSounds();
1109 }
1110
1111 synchronized (this) {
1112 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001113 if (toneGen != null) {
1114 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1115 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119
1120 protected void onStopSounds() {
1121
1122 synchronized (this) {
1123 int numStreamTypes = AudioSystem.getNumStreamTypes();
1124 for (int i = numStreamTypes - 1; i >= 0; i--) {
1125 ToneGenerator toneGen = mToneGenerators[i];
1126 if (toneGen != null) {
1127 toneGen.stopTone();
1128 }
1129 }
1130 }
1131 }
1132
1133 protected void onVibrate() {
1134
1135 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -07001136 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 return;
1138 }
1139
John Spurlock7b414672014-07-18 13:02:39 -04001140 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142
RoboErik19c95182014-06-23 15:38:48 -07001143 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
1144 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: " + flags
1145 + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001146
John Spurlock86005342014-05-23 11:58:00 -04001147 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001148 synchronized (this) {
RoboErik2811dd32014-08-12 09:48:13 -07001149 if (mActiveStreamType != STREAM_REMOTE_MUSIC) {
1150 reorderSliders(STREAM_REMOTE_MUSIC);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001151 }
RoboErik2811dd32014-08-12 09:48:13 -07001152 onShowVolumeChanged(STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001153 }
1154 } else {
John Spurlock86005342014-05-23 11:58:00 -04001155 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001156 }
1157
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001158 removeMessages(MSG_FREE_RESOURCES);
1159 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001160 resetTimeout();
1161 }
1162
1163 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001164 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1165 if (isShowing()
RoboErik2811dd32014-08-12 09:48:13 -07001166 && (mActiveStreamType == STREAM_REMOTE_MUSIC)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001167 && (mStreamControls != null)) {
RoboErik2811dd32014-08-12 09:48:13 -07001168 onShowVolumeChanged(STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001169 }
1170 }
1171
RoboErik19c95182014-06-23 15:38:48 -07001172 /**
1173 * Clear the current remote stream controller.
1174 */
1175 private void clearRemoteStreamController() {
1176 if (mStreamControls != null) {
RoboErik2811dd32014-08-12 09:48:13 -07001177 StreamControl sc = mStreamControls.get(STREAM_REMOTE_MUSIC);
RoboErik19c95182014-06-23 15:38:48 -07001178 if (sc != null) {
1179 if (sc.controller != null) {
1180 sc.controller.removeCallback(mMediaControllerCb);
1181 sc.controller = null;
1182 }
1183 }
1184 }
1185 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001186
1187 /**
RoboErik2811dd32014-08-12 09:48:13 -07001188 * Handler for MSG_SLIDER_VISIBILITY_CHANGED Hide or show a slider
1189 *
1190 * @param streamType can be a valid stream type value, or
1191 * VolumePanel.STREAM_MASTER, or VolumePanel.STREAM_REMOTE_MUSIC
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001192 * @param visible
1193 */
1194 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001195 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001196 boolean isVisible = (visible == 1);
1197 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1198 StreamResources streamRes = STREAMS[i];
1199 if (streamRes.streamType == streamType) {
1200 streamRes.show = isVisible;
1201 if (!isVisible && (mActiveStreamType == streamType)) {
1202 mActiveStreamType = -1;
1203 }
1204 break;
1205 }
1206 }
1207 }
1208
Eric Laurentfde16d52012-12-03 14:42:39 -08001209 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001210 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1211 || isShowing()) {
1212 synchronized (sSafetyWarningLock) {
1213 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001214 return;
1215 }
John Spurlock35134602014-07-24 18:10:48 -04001216 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1217 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001218 }
1219 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001220 }
Selim Cinek9f6ceb12014-08-29 16:28:39 +02001221 updateTimeoutDelay();
Eric Laurentfde16d52012-12-03 14:42:39 -08001222 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001223 }
1224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 /**
1226 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1227 */
1228 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001229 if (streamType == STREAM_MASTER) {
1230 // For devices that use the master volume setting only but still want to
1231 // play a volume-changed tone, direct the master volume pseudostream to
1232 // the system stream's tone generator.
1233 if (mPlayMasterStreamTones) {
1234 streamType = AudioManager.STREAM_SYSTEM;
1235 } else {
1236 return null;
1237 }
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (this) {
1240 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001241 try {
1242 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1243 } catch (RuntimeException e) {
1244 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001245 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001246 + "RuntimeException: " + e);
1247 }
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001250 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 }
1253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254
1255 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001256 * Switch between icons because Bluetooth music is same as music volume, but with
1257 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001259 private void setMusicIcon(int resId, int resMuteId) {
1260 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1261 if (sc != null) {
1262 sc.iconRes = resId;
1263 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001264 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267
1268 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 synchronized (this) {
1270 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1271 if (mToneGenerators[i] != null) {
1272 mToneGenerators[i].release();
1273 }
1274 mToneGenerators[i] = null;
1275 }
1276 }
1277 }
1278
1279 @Override
1280 public void handleMessage(Message msg) {
1281 switch (msg.what) {
1282
1283 case MSG_VOLUME_CHANGED: {
1284 onVolumeChanged(msg.arg1, msg.arg2);
1285 break;
1286 }
1287
Mike Lockwoodce952c82011-11-14 10:47:42 -08001288 case MSG_MUTE_CHANGED: {
1289 onMuteChanged(msg.arg1, msg.arg2);
1290 break;
1291 }
1292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 case MSG_FREE_RESOURCES: {
1294 onFreeResources();
1295 break;
1296 }
1297
1298 case MSG_STOP_SOUNDS: {
1299 onStopSounds();
1300 break;
1301 }
1302
1303 case MSG_PLAY_SOUND: {
1304 onPlaySound(msg.arg1, msg.arg2);
1305 break;
1306 }
1307
1308 case MSG_VIBRATE: {
1309 onVibrate();
1310 break;
1311 }
1312
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001313 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001314 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001315 mDialog.dismiss();
1316 clearRemoteStreamController();
1317 mActiveStreamType = -1;
1318 if (mCallback != null) {
1319 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001320 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001321 }
John Spurlock35134602014-07-24 18:10:48 -04001322 synchronized (sSafetyWarningLock) {
1323 if (sSafetyWarning != null) {
1324 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1325 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001326 }
1327 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001328 break;
1329 }
John Spurlockb4782522014-08-22 14:54:46 -04001330
1331 case MSG_RINGER_MODE_CHANGED:
1332 case MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001333 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001334 updateStates();
1335 }
1336 break;
1337 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001338
1339 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001340 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001341 break;
1342 }
1343
1344 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1345 onRemoteVolumeUpdateIfShown();
1346 break;
1347
1348 case MSG_SLIDER_VISIBILITY_CHANGED:
1349 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1350 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001351
1352 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001353 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001354 break;
John Spurlock86005342014-05-23 11:58:00 -04001355
1356 case MSG_LAYOUT_DIRECTION:
1357 setLayoutDirection(msg.arg1);
1358 break;
1359
John Spurlock45601d62014-08-07 17:40:50 -04001360 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1361 mZenModeAvailable = msg.arg1 != 0;
1362 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001363 break;
1364
1365 case MSG_USER_ACTIVITY:
1366 if (mCallback != null) {
1367 mCallback.onInteraction();
1368 }
John Spurlock86005342014-05-23 11:58:00 -04001369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372
John Spurlockae641c92014-06-30 18:11:40 -04001373 private void resetTimeout() {
John Spurlockea9938c2014-07-11 18:51:32 -04001374 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
1375 + " delay=" + mTimeoutDelay);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001376 removeMessages(MSG_TIMEOUT);
John Spurlock3bd4fee2014-05-29 20:51:09 -04001377 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
John Spurlockae641c92014-06-30 18:11:40 -04001378 removeMessages(MSG_USER_ACTIVITY);
1379 sendEmptyMessage(MSG_USER_ACTIVITY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001380 }
1381
John Spurlock1dad2722014-07-11 11:07:53 -04001382 private void forceTimeout(long delay) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001383 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001384 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001385 }
1386
1387 public ZenModeController getZenController() {
1388 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001389 }
1390
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001391 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1392 @Override
1393 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1394 final Object tag = seekBar.getTag();
1395 if (fromUser && tag instanceof StreamControl) {
1396 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001397 setStreamVolume(sc, progress,
1398 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001399 }
1400 resetTimeout();
1401 }
1402
1403 @Override
1404 public void onStartTrackingTouch(SeekBar seekBar) {
1405 }
1406
1407 @Override
1408 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001409 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001410 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001411
John Spurlock86005342014-05-23 11:58:00 -04001412 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlockb4782522014-08-22 14:54:46 -04001413 @Override
John Spurlock45601d62014-08-07 17:40:50 -04001414 public void onZenAvailableChanged(boolean available) {
1415 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001416 }
John Spurlockb4782522014-08-22 14:54:46 -04001417 @Override
1418 public void onEffectsSupressorChanged() {
1419 obtainMessage(MSG_NOTIFICATION_EFFECTS_SUPPRESSOR_CHANGED,
1420 mZenController.getEffectsSuppressor()).sendToTarget();
1421 }
John Spurlock86005342014-05-23 11:58:00 -04001422 };
RoboErik19c95182014-06-23 15:38:48 -07001423
1424 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
RoboErikd2b8c942014-08-19 11:23:40 -07001425 public void onAudioInfoChanged(PlaybackInfo info) {
RoboErik19c95182014-06-23 15:38:48 -07001426 onRemoteVolumeUpdateIfShown();
1427 }
1428 };
John Spurlockae641c92014-06-30 18:11:40 -04001429
1430 public interface Callback {
1431 void onZenSettings();
1432 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001433 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435}