blob: 5233da26572b8c1b56d091006de743ebd408313d [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080023import android.content.DialogInterface;
John Spurlock86005342014-05-23 11:58:00 -040024import android.content.DialogInterface.OnDismissListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Intent;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080026import android.content.IntentFilter;
John Spurlock7e6809a2014-08-06 16:03:14 -040027import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.Resources;
John Spurlockad494bc2014-07-19 15:56:19 -040029import android.content.res.TypedArray;
John Spurlock2078caf2014-05-29 22:20:14 -040030import android.graphics.PixelFormat;
31import android.graphics.drawable.ColorDrawable;
John Spurlock7b414672014-07-18 13:02:39 -040032import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.media.AudioManager;
34import android.media.AudioService;
35import android.media.AudioSystem;
Marco Nelissen69f593c2009-07-28 09:55:04 -070036import android.media.RingtoneManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.media.ToneGenerator;
RoboErik19c95182014-06-23 15:38:48 -070038import android.media.VolumeProvider;
39import android.media.session.MediaController;
40import android.media.session.MediaController.VolumeInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070041import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Handler;
43import android.os.Message;
44import android.os.Vibrator;
John Spurlockae641c92014-06-30 18:11:40 -040045import android.provider.Settings.Global;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.util.Log;
John Spurlockad494bc2014-07-19 15:56:19 -040047import android.util.SparseArray;
John Spurlock2d28d6e2014-08-01 13:10:14 -040048import android.view.KeyEvent;
John Spurlock3346a802014-05-20 16:25:37 -040049import android.view.LayoutInflater;
50import android.view.MotionEvent;
51import android.view.View;
John Spurlock7f1df5e2014-05-31 19:11:40 -040052import android.view.View.OnClickListener;
John Spurlock3346a802014-05-20 16:25:37 -040053import android.view.ViewGroup;
54import android.view.Window;
55import android.view.WindowManager;
Amith Yamasani284e6302011-09-16 18:24:47 -070056import android.view.WindowManager.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.widget.ImageView;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080058import android.widget.SeekBar;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080059import android.widget.SeekBar.OnSeekBarChangeListener;
60
John Spurlock86005342014-05-23 11:58:00 -040061import com.android.internal.R;
John Spurlock35134602014-07-24 18:10:48 -040062import com.android.systemui.statusbar.phone.SystemUIDialog;
John Spurlock86005342014-05-23 11:58:00 -040063import com.android.systemui.statusbar.policy.ZenModeController;
64
John Spurlockad494bc2014-07-19 15:56:19 -040065import java.io.FileDescriptor;
66import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68/**
John Spurlock3346a802014-05-20 16:25:37 -040069 * Handles the user interface for the volume keys.
Dianne Hackborne8ecde12011-08-03 18:55:19 -070070 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * @hide
72 */
John Spurlock3346a802014-05-20 16:25:37 -040073public class VolumePanel extends Handler {
John Spurlockae641c92014-06-30 18:11:40 -040074 private static final String TAG = "VolumePanel";
75 private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
John Spurlock3346a802014-05-20 16:25:37 -040077 private static final int PLAY_SOUND_DELAY = AudioService.PLAY_SOUND_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79 /**
80 * The delay before vibrating. This small period exists so if the user is
81 * moving to silent mode, it will not emit a short vibrate (it normally
82 * would since vibrate is between normal mode and silent mode using hardware
83 * keys).
84 */
85 public static final int VIBRATE_DELAY = 300;
86
87 private static final int VIBRATE_DURATION = 300;
88 private static final int BEEP_DURATION = 150;
89 private static final int MAX_VOLUME = 100;
90 private static final int FREE_DELAY = 10000;
Amith Yamasani2bbdd772011-02-02 18:54:13 -080091 private static final int TIMEOUT_DELAY = 3000;
John Spurlock8845da72014-07-07 21:29:48 -040092 private static final int TIMEOUT_DELAY_SHORT = 1500;
John Spurlockea9938c2014-07-11 18:51:32 -040093 private static final int TIMEOUT_DELAY_COLLAPSED = 4500;
John Spurlock35134602014-07-24 18:10:48 -040094 private static final int TIMEOUT_DELAY_SAFETY_WARNING = 5000;
John Spurlock3bd4fee2014-05-29 20:51:09 -040095 private static final int TIMEOUT_DELAY_EXPANDED = 10000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 private static final int MSG_VOLUME_CHANGED = 0;
98 private static final int MSG_FREE_RESOURCES = 1;
99 private static final int MSG_PLAY_SOUND = 2;
100 private static final int MSG_STOP_SOUNDS = 3;
101 private static final int MSG_VIBRATE = 4;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800102 private static final int MSG_TIMEOUT = 5;
103 private static final int MSG_RINGER_MODE_CHANGED = 6;
Mike Lockwoodce952c82011-11-14 10:47:42 -0800104 private static final int MSG_MUTE_CHANGED = 7;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700105 private static final int MSG_REMOTE_VOLUME_CHANGED = 8;
106 private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9;
107 private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700108 private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11;
John Spurlock86005342014-05-23 11:58:00 -0400109 private static final int MSG_LAYOUT_DIRECTION = 12;
John Spurlock45601d62014-08-07 17:40:50 -0400110 private static final int MSG_ZEN_MODE_AVAILABLE_CHANGED = 13;
John Spurlockae641c92014-06-30 18:11:40 -0400111 private static final int MSG_USER_ACTIVITY = 14;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400113 // Pseudo stream type for master volume
Mike Lockwood47676902011-11-08 10:31:21 -0800114 private static final int STREAM_MASTER = -100;
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700115 // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400116
John Spurlock7b414672014-07-18 13:02:39 -0400117 private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
118 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
119 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
120 .build();
121
John Spurlock86005342014-05-23 11:58:00 -0400122 private final String mTag;
John Spurlock3346a802014-05-20 16:25:37 -0400123 protected final Context mContext;
124 private final AudioManager mAudioManager;
John Spurlock86005342014-05-23 11:58:00 -0400125 private final ZenModeController mZenController;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700126 private boolean mRingIsSilent;
Amith Yamasani71def772011-10-12 12:25:24 -0700127 private boolean mVoiceCapable;
John Spurlock45601d62014-08-07 17:40:50 -0400128 private boolean mZenModeAvailable;
John Spurlock8845da72014-07-07 21:29:48 -0400129 private boolean mZenPanelExpanded;
John Spurlock3bd4fee2014-05-29 20:51:09 -0400130 private int mTimeoutDelay = TIMEOUT_DELAY;
John Spurlockad494bc2014-07-19 15:56:19 -0400131 private float mDisabledAlpha;
132 private int mLastRingerMode = AudioManager.RINGER_MODE_NORMAL;
133 private int mLastRingerProgress = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Christopher Tatec4b78d22012-05-22 13:57:58 -0700135 // True if we want to play tones on the system stream when the master stream is specified.
136 private final boolean mPlayMasterStreamTones;
137
John Spurlock86005342014-05-23 11:58:00 -0400138
139 /** Volume panel content view */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 private final View mView;
John Spurlockeb2727b2014-07-19 23:11:36 -0400141 /** Dialog hosting the panel */
John Spurlock86005342014-05-23 11:58:00 -0400142 private final Dialog mDialog;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800143
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -0700144 /** The visible portion of the volume overlay */
145 private final ViewGroup mPanel;
John Spurlock86005342014-05-23 11:58:00 -0400146 /** Contains the slider and its touchable icons */
147 private final ViewGroup mSliderPanel;
John Spurlockeb2727b2014-07-19 23:11:36 -0400148 /** The zen mode configuration panel view */
John Spurlock86005342014-05-23 11:58:00 -0400149 private ZenModePanel mZenPanel;
John Spurlock86005342014-05-23 11:58:00 -0400150
John Spurlockae641c92014-06-30 18:11:40 -0400151 private Callback mCallback;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800152
153 /** Currently active stream that shows up at the top of the list of sliders */
154 private int mActiveStreamType = -1;
155 /** All the slider controls mapped by stream type */
John Spurlockad494bc2014-07-19 15:56:19 -0400156 private SparseArray<StreamControl> mStreamControls;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800157
Amith Yamasani71def772011-10-12 12:25:24 -0700158 private enum StreamResources {
159 BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
160 R.string.volume_icon_description_bluetooth,
161 R.drawable.ic_audio_bt,
162 R.drawable.ic_audio_bt,
163 false),
164 RingerStream(AudioManager.STREAM_RING,
165 R.string.volume_icon_description_ringer,
John Spurlock86005342014-05-23 11:58:00 -0400166 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400167 com.android.systemui.R.drawable.ic_ringer_vibrate,
Amith Yamasani71def772011-10-12 12:25:24 -0700168 false),
169 VoiceStream(AudioManager.STREAM_VOICE_CALL,
170 R.string.volume_icon_description_incall,
171 R.drawable.ic_audio_phone,
172 R.drawable.ic_audio_phone,
173 false),
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700174 AlarmStream(AudioManager.STREAM_ALARM,
175 R.string.volume_alarm,
176 R.drawable.ic_audio_alarm,
177 R.drawable.ic_audio_alarm_mute,
178 false),
Amith Yamasani71def772011-10-12 12:25:24 -0700179 MediaStream(AudioManager.STREAM_MUSIC,
180 R.string.volume_icon_description_media,
181 R.drawable.ic_audio_vol,
182 R.drawable.ic_audio_vol_mute,
183 true),
184 NotificationStream(AudioManager.STREAM_NOTIFICATION,
185 R.string.volume_icon_description_notification,
John Spurlock86005342014-05-23 11:58:00 -0400186 com.android.systemui.R.drawable.ic_ringer_audible,
John Spurlockad494bc2014-07-19 15:56:19 -0400187 com.android.systemui.R.drawable.ic_ringer_vibrate,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400188 true),
189 // for now, use media resources for master volume
190 MasterStream(STREAM_MASTER,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700191 R.string.volume_icon_description_media, //FIXME should have its own description
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400192 R.drawable.ic_audio_vol,
193 R.drawable.ic_audio_vol_mute,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700194 false),
195 RemoteStream(AudioService.STREAM_REMOTE_MUSIC,
196 R.string.volume_icon_description_media, //FIXME should have its own description
197 R.drawable.ic_media_route_on_holo_dark,
198 R.drawable.ic_media_route_disabled_holo_dark,
199 false);// will be dynamically updated
Amith Yamasani71def772011-10-12 12:25:24 -0700200
201 int streamType;
202 int descRes;
203 int iconRes;
204 int iconMuteRes;
205 // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
206 boolean show;
207
208 StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
209 this.streamType = streamType;
210 this.descRes = descRes;
211 this.iconRes = iconRes;
212 this.iconMuteRes = iconMuteRes;
213 this.show = show;
214 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700215 }
Amith Yamasani71def772011-10-12 12:25:24 -0700216
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800217 // List of stream types and their order
Amith Yamasani71def772011-10-12 12:25:24 -0700218 private static final StreamResources[] STREAMS = {
219 StreamResources.BluetoothSCOStream,
220 StreamResources.RingerStream,
221 StreamResources.VoiceStream,
222 StreamResources.MediaStream,
Amith Yamasani92e1b2d2011-10-14 17:24:47 -0700223 StreamResources.NotificationStream,
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400224 StreamResources.AlarmStream,
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700225 StreamResources.MasterStream,
226 StreamResources.RemoteStream
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800227 };
228
229 /** Object that contains data for each slider */
230 private class StreamControl {
231 int streamType;
RoboErik19c95182014-06-23 15:38:48 -0700232 MediaController controller;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800233 ViewGroup group;
234 ImageView icon;
235 SeekBar seekbarView;
236 int iconRes;
237 int iconMuteRes;
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 // Synchronize when accessing this
241 private ToneGenerator mToneGenerators[];
242 private Vibrator mVibrator;
243
John Spurlock35134602014-07-24 18:10:48 -0400244 private static AlertDialog sSafetyWarning;
245 private static Object sSafetyWarningLock = new Object();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700246
John Spurlock35134602014-07-24 18:10:48 -0400247 private static class SafetyWarning extends SystemUIDialog
248 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Eric Laurentfde16d52012-12-03 14:42:39 -0800249 private final Context mContext;
Eric Laurentfde16d52012-12-03 14:42:39 -0800250 private final VolumePanel mVolumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400251 private final AudioManager mAudioManager;
Eric Laurentc34dcc12012-09-10 13:51:52 -0700252
John Spurlock2d28d6e2014-08-01 13:10:14 -0400253 private boolean mNewVolumeUp;
254
John Spurlock35134602014-07-24 18:10:48 -0400255 SafetyWarning(Context context, VolumePanel volumePanel, AudioManager audioManager) {
256 super(context);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700257 mContext = context;
Eric Laurentfde16d52012-12-03 14:42:39 -0800258 mVolumePanel = volumePanel;
John Spurlock35134602014-07-24 18:10:48 -0400259 mAudioManager = audioManager;
260
261 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
262 setButton(DialogInterface.BUTTON_POSITIVE,
263 mContext.getString(com.android.internal.R.string.yes), this);
264 setButton(DialogInterface.BUTTON_NEGATIVE,
265 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
266 setOnDismissListener(this);
267
Eric Laurentc34dcc12012-09-10 13:51:52 -0700268 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
John Spurlock35134602014-07-24 18:10:48 -0400269 context.registerReceiver(mReceiver, filter);
Eric Laurentc34dcc12012-09-10 13:51:52 -0700270 }
271
272 @Override
John Spurlock2d28d6e2014-08-01 13:10:14 -0400273 public boolean onKeyDown(int keyCode, KeyEvent event) {
274 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) {
275 mNewVolumeUp = true;
276 }
277 return super.onKeyDown(keyCode, event);
278 }
279
280 @Override
281 public boolean onKeyUp(int keyCode, KeyEvent event) {
282 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp) {
283 if (LOGD) Log.d(TAG, "Confirmed warning via VOLUME_UP");
284 mAudioManager.disableSafeMediaVolume();
285 dismiss();
286 }
287 return super.onKeyUp(keyCode, event);
288 }
289
290 @Override
John Spurlock35134602014-07-24 18:10:48 -0400291 public void onClick(DialogInterface dialog, int which) {
292 mAudioManager.disableSafeMediaVolume();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700293 }
294
Alan Viverette494fb7b2014-04-10 18:12:56 -0700295 @Override
Eric Laurentc34dcc12012-09-10 13:51:52 -0700296 public void onDismiss(DialogInterface unused) {
John Spurlock35134602014-07-24 18:10:48 -0400297 mContext.unregisterReceiver(mReceiver);
Eric Laurentfde16d52012-12-03 14:42:39 -0800298 cleanUp();
299 }
300
301 private void cleanUp() {
John Spurlock35134602014-07-24 18:10:48 -0400302 synchronized (sSafetyWarningLock) {
303 sSafetyWarning = null;
Eric Laurent0516a9e2012-09-19 11:53:03 -0700304 }
John Spurlock1dad2722014-07-11 11:07:53 -0400305 mVolumePanel.forceTimeout(0);
Eric Laurentfde16d52012-12-03 14:42:39 -0800306 mVolumePanel.updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700307 }
John Spurlock35134602014-07-24 18:10:48 -0400308
309 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
313 if (LOGD) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
314 cancel();
315 cleanUp();
316 }
317 }
318 };
Eric Laurentc34dcc12012-09-10 13:51:52 -0700319 }
320
John Spurlockeb2727b2014-07-19 23:11:36 -0400321 public VolumePanel(Context context, ZenModeController zenController) {
322 mTag = String.format("%s.%08x", TAG, hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mContext = context;
John Spurlock86005342014-05-23 11:58:00 -0400324 mZenController = zenController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400327 // For now, only show master volume if master volume is supported
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700328 final Resources res = context.getResources();
329 final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400330 if (useMasterVolume) {
331 for (int i = 0; i < STREAMS.length; i++) {
332 StreamResources streamRes = STREAMS[i];
333 streamRes.show = (streamRes.streamType == STREAM_MASTER);
334 }
335 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400336 if (LOGD) Log.d(mTag, "new VolumePanel");
337
John Spurlockad494bc2014-07-19 15:56:19 -0400338 mDisabledAlpha = 0.5f;
339 if (mContext.getTheme() != null) {
340 final TypedArray arr = mContext.getTheme().obtainStyledAttributes(
341 new int[] { android.R.attr.disabledAlpha });
342 mDisabledAlpha = arr.getFloat(0, mDisabledAlpha);
343 arr.recycle();
344 }
345
John Spurlockeb2727b2014-07-19 23:11:36 -0400346 mDialog = new Dialog(context) {
347 @Override
348 public boolean onTouchEvent(MotionEvent event) {
349 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE &&
John Spurlock35134602014-07-24 18:10:48 -0400350 sSafetyWarning == null) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400351 forceTimeout(0);
352 return true;
Amith Yamasani284e6302011-09-16 18:24:47 -0700353 }
John Spurlockeb2727b2014-07-19 23:11:36 -0400354 return false;
355 }
356 };
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700357
John Spurlockeb2727b2014-07-19 23:11:36 -0400358 final Window window = mDialog.getWindow();
John Spurlockeb2727b2014-07-19 23:11:36 -0400359 window.requestFeature(Window.FEATURE_NO_TITLE);
John Spurlockeb2727b2014-07-19 23:11:36 -0400360 mDialog.setCanceledOnTouchOutside(true);
361 mDialog.setContentView(com.android.systemui.R.layout.volume_dialog);
362 mDialog.setOnDismissListener(new OnDismissListener() {
363 @Override
364 public void onDismiss(DialogInterface dialog) {
365 mActiveStreamType = -1;
366 mAudioManager.forceVolumeControlStream(mActiveStreamType);
367 setZenPanelVisible(false);
368 }
369 });
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700370
John Spurlockeb2727b2014-07-19 23:11:36 -0400371 mDialog.create();
Alan Viverette494fb7b2014-04-10 18:12:56 -0700372
John Spurlock7e6809a2014-08-06 16:03:14 -0400373 final LayoutParams lp = window.getAttributes();
374 lp.token = null;
375 lp.y = res.getDimensionPixelOffset(com.android.systemui.R.dimen.volume_panel_top);
376 lp.type = LayoutParams.TYPE_STATUS_BAR_PANEL;
377 lp.format = PixelFormat.TRANSLUCENT;
378 lp.windowAnimations = com.android.systemui.R.style.VolumePanelAnimation;
379 lp.setTitle(TAG);
380 window.setAttributes(lp);
381
382 updateWidth();
383
384 window.setBackgroundDrawable(new ColorDrawable(0x00000000));
385 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
386 window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE
387 | LayoutParams.FLAG_NOT_TOUCH_MODAL
388 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
389 | LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockeb2727b2014-07-19 23:11:36 -0400390 mView = window.findViewById(R.id.content);
391 mView.setOnTouchListener(new View.OnTouchListener() {
392 @Override
393 public boolean onTouch(View v, MotionEvent event) {
394 resetTimeout();
395 return false;
396 }
397 });
Alan Viverette494fb7b2014-04-10 18:12:56 -0700398
John Spurlock86005342014-05-23 11:58:00 -0400399 mPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.visible_panel);
400 mSliderPanel = (ViewGroup) mView.findViewById(com.android.systemui.R.id.slider_panel);
John Spurlockeb2727b2014-07-19 23:11:36 -0400401 mZenPanel = (ZenModePanel) mView.findViewById(com.android.systemui.R.id.zen_mode_panel);
402 initZenModePanel();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700405 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Amith Yamasani71def772011-10-12 12:25:24 -0700406 mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700407
John Spurlock45601d62014-08-07 17:40:50 -0400408 if (mZenController != null && !useMasterVolume) {
409 mZenModeAvailable = mZenController.isZenAvailable();
410 mZenController.addCallback(mZenCallback);
411 }
Amith Yamasani42722bf2011-07-22 10:34:27 -0700412
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700413 final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume);
414 final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds);
Christopher Tatec4b78d22012-05-22 13:57:58 -0700415 mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
416
John Spurlock8c79d2e2014-07-24 15:15:25 -0400417 registerReceiver();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800418 }
419
John Spurlock7e6809a2014-08-06 16:03:14 -0400420 public void onConfigurationChanged(Configuration newConfig) {
421 updateWidth();
422 }
423
424 private void updateWidth() {
425 final Resources res = mContext.getResources();
426 final LayoutParams lp = mDialog.getWindow().getAttributes();
427 lp.width = res.getDimensionPixelSize(com.android.systemui.R.dimen.notification_panel_width);
428 lp.gravity =
429 res.getInteger(com.android.systemui.R.integer.notification_panel_layout_gravity);
430 mDialog.getWindow().setAttributes(lp);
431 }
432
John Spurlockad494bc2014-07-19 15:56:19 -0400433 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
434 pw.println("VolumePanel state:");
435 pw.print(" mTag="); pw.println(mTag);
436 pw.print(" mRingIsSilent="); pw.println(mRingIsSilent);
437 pw.print(" mVoiceCapable="); pw.println(mVoiceCapable);
John Spurlock45601d62014-08-07 17:40:50 -0400438 pw.print(" mZenModeAvailable="); pw.println(mZenModeAvailable);
John Spurlockad494bc2014-07-19 15:56:19 -0400439 pw.print(" mZenPanelExpanded="); pw.println(mZenPanelExpanded);
440 pw.print(" mTimeoutDelay="); pw.println(mTimeoutDelay);
441 pw.print(" mDisabledAlpha="); pw.println(mDisabledAlpha);
442 pw.print(" mLastRingerMode="); pw.println(mLastRingerMode);
443 pw.print(" mLastRingerProgress="); pw.println(mLastRingerProgress);
444 pw.print(" mPlayMasterStreamTones="); pw.println(mPlayMasterStreamTones);
445 pw.print(" isShowing()="); pw.println(isShowing());
446 pw.print(" mCallback="); pw.println(mCallback);
447 pw.print(" sConfirmSafeVolumeDialog=");
John Spurlock35134602014-07-24 18:10:48 -0400448 pw.println(sSafetyWarning != null ? "<not null>" : null);
John Spurlockad494bc2014-07-19 15:56:19 -0400449 pw.print(" mActiveStreamType="); pw.println(mActiveStreamType);
450 pw.print(" mStreamControls=");
451 if (mStreamControls == null) {
452 pw.println("null");
453 } else {
454 final int N = mStreamControls.size();
455 pw.print("<size "); pw.print(N); pw.println('>');
456 for (int i = 0; i < N; i++) {
457 final StreamControl sc = mStreamControls.valueAt(i);
458 pw.print(" stream "); pw.print(sc.streamType); pw.print(":");
459 if (sc.seekbarView != null) {
460 pw.print(" progress="); pw.print(sc.seekbarView.getProgress());
461 pw.print(" of "); pw.print(sc.seekbarView.getMax());
462 if (!sc.seekbarView.isEnabled()) pw.print(" (disabled)");
463 }
464 if (sc.icon != null && sc.icon.isClickable()) pw.print(" (clickable)");
465 pw.println();
466 }
467 }
468 }
469
John Spurlockeb2727b2014-07-19 23:11:36 -0400470 private void initZenModePanel() {
471 mZenPanel.init(mZenController);
472 mZenPanel.setCallback(new ZenModePanel.Callback() {
473 @Override
474 public void onMoreSettings() {
475 if (mCallback != null) {
476 mCallback.onZenSettings();
477 }
478 }
479
480 @Override
481 public void onInteraction() {
482 resetTimeout();
483 }
484
485 @Override
486 public void onExpanded(boolean expanded) {
487 if (mZenPanelExpanded == expanded) return;
488 mZenPanelExpanded = expanded;
489 updateTimeoutDelay();
490 resetTimeout();
491 }
492 });
John Spurlock7f8f22a2014-07-02 18:54:17 -0400493 }
494
John Spurlock86005342014-05-23 11:58:00 -0400495 private void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800496 mPanel.setLayoutDirection(layoutDirection);
497 updateStates();
498 }
499
John Spurlock8c79d2e2014-07-24 15:15:25 -0400500 private void registerReceiver() {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800501 final IntentFilter filter = new IntentFilter();
502 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlock8c79d2e2014-07-24 15:15:25 -0400503 filter.addAction(Intent.ACTION_SCREEN_OFF);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800504 mContext.registerReceiver(new BroadcastReceiver() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700505 @Override
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800506 public void onReceive(Context context, Intent intent) {
507 final String action = intent.getAction();
508
509 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
510 removeMessages(MSG_RINGER_MODE_CHANGED);
511 sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
512 }
John Spurlock8c79d2e2014-07-24 15:15:25 -0400513
514 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
515 postDismiss(0);
516 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800517 }
518 }, filter);
519 }
520
521 private boolean isMuted(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400522 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700523 return mAudioManager.isMasterMute();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700524 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700525 // TODO do we need to support a distinct mute property for remote?
526 return false;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400527 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700528 return mAudioManager.isStreamMute(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400529 }
530 }
531
532 private int getStreamMaxVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400533 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700534 return mAudioManager.getMasterMaxVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700535 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700536 if (mStreamControls != null) {
537 StreamControl sc = mStreamControls.get(streamType);
538 if (sc != null && sc.controller != null) {
539 VolumeInfo vi = sc.controller.getVolumeInfo();
540 return vi.getMaxVolume();
541 }
542 }
543 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400544 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700545 return mAudioManager.getStreamMaxVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400546 }
547 }
548
549 private int getStreamVolume(int streamType) {
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400550 if (streamType == STREAM_MASTER) {
Eric Laurent8c787522012-05-14 14:09:43 -0700551 return mAudioManager.getMasterVolume();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700552 } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
RoboErik19c95182014-06-23 15:38:48 -0700553 if (mStreamControls != null) {
554 StreamControl sc = mStreamControls.get(streamType);
555 if (sc != null && sc.controller != null) {
556 VolumeInfo vi = sc.controller.getVolumeInfo();
557 return vi.getCurrentVolume();
558 }
559 }
560 return -1;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400561 } else {
Eric Laurent8c787522012-05-14 14:09:43 -0700562 return mAudioManager.getStreamVolume(streamType);
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400563 }
564 }
565
RoboErik19c95182014-06-23 15:38:48 -0700566 private void setStreamVolume(StreamControl sc, int index, int flags) {
567 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
568 if (sc.controller != null) {
569 sc.controller.setVolumeTo(index, flags);
570 } else {
Jean-Michel Trivi65820412014-06-30 12:10:44 -0700571 Log.w(mTag, "Adjusting remote volume without a controller!");
RoboErik19c95182014-06-23 15:38:48 -0700572 }
573 } else if (getStreamVolume(sc.streamType) != index) {
574 if (sc.streamType == STREAM_MASTER) {
575 mAudioManager.setMasterVolume(index, flags);
576 } else {
577 mAudioManager.setStreamVolume(sc.streamType, index, flags);
578 }
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400579 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800580 }
581
582 private void createSliders() {
Alan Viverette494fb7b2014-04-10 18:12:56 -0700583 final Resources res = mContext.getResources();
584 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
585 Context.LAYOUT_INFLATER_SERVICE);
586
John Spurlockad494bc2014-07-19 15:56:19 -0400587 mStreamControls = new SparseArray<StreamControl>(STREAMS.length);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700588
Amith Yamasani71def772011-10-12 12:25:24 -0700589 for (int i = 0; i < STREAMS.length; i++) {
590 StreamResources streamRes = STREAMS[i];
Alan Viverette494fb7b2014-04-10 18:12:56 -0700591
592 final int streamType = streamRes.streamType;
Alan Viverette494fb7b2014-04-10 18:12:56 -0700593
594 final StreamControl sc = new StreamControl();
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700595 sc.streamType = streamType;
John Spurlock86005342014-05-23 11:58:00 -0400596 sc.group = (ViewGroup) inflater.inflate(
597 com.android.systemui.R.layout.volume_panel_item, null);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800598 sc.group.setTag(sc);
John Spurlock86005342014-05-23 11:58:00 -0400599 sc.icon = (ImageView) sc.group.findViewById(com.android.systemui.R.id.stream_icon);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800600 sc.icon.setTag(sc);
Amith Yamasani71def772011-10-12 12:25:24 -0700601 sc.icon.setContentDescription(res.getString(streamRes.descRes));
602 sc.iconRes = streamRes.iconRes;
603 sc.iconMuteRes = streamRes.iconMuteRes;
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800604 sc.icon.setImageResource(sc.iconRes);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400605 sc.icon.setClickable(isNotificationOrRing(streamType));
606 if (sc.icon.isClickable()) {
John Spurlockab4ea762014-07-19 18:50:16 -0400607 sc.icon.setSoundEffectsEnabled(false);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400608 sc.icon.setOnClickListener(new OnClickListener() {
609 @Override
610 public void onClick(View v) {
611 resetTimeout();
612 toggle(sc);
613 }
614 });
John Spurlock7f1df5e2014-05-31 19:11:40 -0400615 }
John Spurlock86005342014-05-23 11:58:00 -0400616 sc.seekbarView = (SeekBar) sc.group.findViewById(com.android.systemui.R.id.seekbar);
Alan Viverette494fb7b2014-04-10 18:12:56 -0700617 final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700618 streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400619 sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
Alan Viverettee8ebaf32014-04-11 15:44:15 -0700620 sc.seekbarView.setOnSeekBarChangeListener(mSeekListener);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800621 sc.seekbarView.setTag(sc);
Amith Yamasanid47a3aee2011-08-23 11:11:35 -0700622 mStreamControls.put(streamType, sc);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800623 }
624 }
625
John Spurlock7f1df5e2014-05-31 19:11:40 -0400626 private void toggle(StreamControl sc) {
627 if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
628 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
629 postVolumeChanged(sc.streamType, AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
630 } else {
631 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
632 postVolumeChanged(sc.streamType, AudioManager.FLAG_PLAY_SOUND);
633 }
634 }
635
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800636 private void reorderSliders(int activeStreamType) {
John Spurlock86005342014-05-23 11:58:00 -0400637 mSliderPanel.removeAllViews();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800638
Alan Viverette494fb7b2014-04-10 18:12:56 -0700639 final StreamControl active = mStreamControls.get(activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800640 if (active == null) {
John Spurlockae641c92014-06-30 18:11:40 -0400641 Log.e(TAG, "Missing stream type! - " + activeStreamType);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800642 mActiveStreamType = -1;
643 } else {
John Spurlock86005342014-05-23 11:58:00 -0400644 mSliderPanel.addView(active.group);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800645 mActiveStreamType = activeStreamType;
646 active.group.setVisibility(View.VISIBLE);
647 updateSlider(active);
John Spurlock8845da72014-07-07 21:29:48 -0400648 updateTimeoutDelay();
John Spurlock45601d62014-08-07 17:40:50 -0400649 updateZenPanelVisible();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800650 }
651 }
652
John Spurlockad494bc2014-07-19 15:56:19 -0400653 private void updateSliderProgress(StreamControl sc, int progress) {
654 final boolean isRinger = isNotificationOrRing(sc.streamType);
655 if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
656 progress = mLastRingerProgress;
657 }
658 if (progress < 0) {
659 progress = getStreamVolume(sc.streamType);
660 }
661 sc.seekbarView.setProgress(progress);
662 if (isRinger) {
663 mLastRingerProgress = progress;
664 }
665 }
666
667 private void updateSliderIcon(StreamControl sc, boolean muted) {
668 if (isNotificationOrRing(sc.streamType)) {
669 int ringerMode = mAudioManager.getRingerMode();
670 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
671 ringerMode = mLastRingerMode;
672 } else {
673 mLastRingerMode = ringerMode;
674 }
675 muted = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
676 }
677 sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
678 }
679
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800680 /** Update the mute and progress state of a slider */
681 private void updateSlider(StreamControl sc) {
John Spurlockad494bc2014-07-19 15:56:19 -0400682 updateSliderProgress(sc, -1);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800683 final boolean muted = isMuted(sc.streamType);
Fabrice Di Meglio8c028842013-01-09 18:20:38 -0800684 // Force reloading the image resource
685 sc.icon.setImageDrawable(null);
John Spurlockad494bc2014-07-19 15:56:19 -0400686 updateSliderIcon(sc, muted);
John Spurlock7f1df5e2014-05-31 19:11:40 -0400687 updateSliderEnabled(sc, muted, false);
688 }
689
John Spurlock5f640e42014-05-31 20:15:59 -0400690 private void updateSliderEnabled(final StreamControl sc, boolean muted, boolean fixedVolume) {
691 final boolean wasEnabled = sc.seekbarView.isEnabled();
John Spurlockae641c92014-06-30 18:11:40 -0400692 final boolean isRinger = isNotificationOrRing(sc.streamType);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700693 if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) {
694 // never disable touch interactions for remote playback, the muting is not tied to
695 // the state of the phone.
RoboErik19c95182014-06-23 15:38:48 -0700696 sc.seekbarView.setEnabled(!fixedVolume);
John Spurlockae641c92014-06-30 18:11:40 -0400697 } else if (isRinger && mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
John Spurlock7f1df5e2014-05-31 19:11:40 -0400698 sc.seekbarView.setEnabled(false);
John Spurlockae641c92014-06-30 18:11:40 -0400699 sc.icon.setEnabled(false);
John Spurlockad494bc2014-07-19 15:56:19 -0400700 sc.icon.setAlpha(mDisabledAlpha);
John Spurlockae641c92014-06-30 18:11:40 -0400701 sc.icon.setClickable(false);
John Spurlock4f0f1202014-08-05 13:28:33 -0400702 } else if (fixedVolume ||
703 (sc.streamType != mAudioManager.getMasterStreamType() && muted) ||
704 (sSafetyWarning != null)) {
705 sc.seekbarView.setEnabled(false);
Eric Laurent8c787522012-05-14 14:09:43 -0700706 } else {
707 sc.seekbarView.setEnabled(true);
John Spurlockae641c92014-06-30 18:11:40 -0400708 sc.icon.setEnabled(true);
John Spurlockad494bc2014-07-19 15:56:19 -0400709 sc.icon.setAlpha(1f);
Eric Laurent8c787522012-05-14 14:09:43 -0700710 }
John Spurlockae641c92014-06-30 18:11:40 -0400711 // show the silent hint when the disabled slider is touched in silent mode
712 if (isRinger && wasEnabled != sc.seekbarView.isEnabled()) {
John Spurlock5f640e42014-05-31 20:15:59 -0400713 if (sc.seekbarView.isEnabled()) {
John Spurlockae641c92014-06-30 18:11:40 -0400714 sc.group.setOnTouchListener(null);
715 sc.icon.setClickable(true);
John Spurlock5f640e42014-05-31 20:15:59 -0400716 } else {
John Spurlockae641c92014-06-30 18:11:40 -0400717 final View.OnTouchListener showHintOnTouch = new View.OnTouchListener() {
John Spurlock5f640e42014-05-31 20:15:59 -0400718 @Override
719 public boolean onTouch(View v, MotionEvent event) {
720 resetTimeout();
John Spurlockae641c92014-06-30 18:11:40 -0400721 showSilentHint();
John Spurlock5f640e42014-05-31 20:15:59 -0400722 return false;
723 }
John Spurlockae641c92014-06-30 18:11:40 -0400724 };
725 sc.group.setOnTouchListener(showHintOnTouch);
John Spurlock5f640e42014-05-31 20:15:59 -0400726 }
727 }
728 }
729
John Spurlockae641c92014-06-30 18:11:40 -0400730 private void showSilentHint() {
731 if (mZenPanel != null) {
732 mZenPanel.showSilentHint();
733 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800734 }
735
John Spurlock7f1df5e2014-05-31 19:11:40 -0400736 private static boolean isNotificationOrRing(int streamType) {
737 return streamType == AudioManager.STREAM_RING
738 || streamType == AudioManager.STREAM_NOTIFICATION;
739 }
740
John Spurlockae641c92014-06-30 18:11:40 -0400741 public void setCallback(Callback callback) {
742 mCallback = callback;
John Spurlock86005342014-05-23 11:58:00 -0400743 }
744
John Spurlock8845da72014-07-07 21:29:48 -0400745 private void updateTimeoutDelay() {
John Spurlock35134602014-07-24 18:10:48 -0400746 mTimeoutDelay = sSafetyWarning != null ? TIMEOUT_DELAY_SAFETY_WARNING
747 : mActiveStreamType == AudioManager.STREAM_MUSIC ? TIMEOUT_DELAY_SHORT
John Spurlockea9938c2014-07-11 18:51:32 -0400748 : mZenPanelExpanded ? TIMEOUT_DELAY_EXPANDED
749 : isZenPanelVisible() ? TIMEOUT_DELAY_COLLAPSED
750 : TIMEOUT_DELAY;
751 }
752
753 private boolean isZenPanelVisible() {
754 return mZenPanel != null && mZenPanel.getVisibility() == View.VISIBLE;
John Spurlock8845da72014-07-07 21:29:48 -0400755 }
756
John Spurlockae641c92014-06-30 18:11:40 -0400757 private void setZenPanelVisible(boolean visible) {
758 if (LOGD) Log.d(mTag, "setZenPanelVisible " + visible + " mZenPanel=" + mZenPanel);
John Spurlockea9938c2014-07-11 18:51:32 -0400759 final boolean changing = visible != isZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -0400760 if (visible) {
John Spurlockeb2727b2014-07-19 23:11:36 -0400761 mZenPanel.setHidden(false);
John Spurlockae641c92014-06-30 18:11:40 -0400762 resetTimeout();
763 } else {
John Spurlockeb2727b2014-07-19 23:11:36 -0400764 mZenPanel.setHidden(true);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800765 }
John Spurlockea9938c2014-07-11 18:51:32 -0400766 if (changing) {
767 updateTimeoutDelay();
768 resetTimeout();
769 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800770 }
771
Eric Laurentfde16d52012-12-03 14:42:39 -0800772 public void updateStates() {
John Spurlock86005342014-05-23 11:58:00 -0400773 final int count = mSliderPanel.getChildCount();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800774 for (int i = 0; i < count; i++) {
John Spurlock86005342014-05-23 11:58:00 -0400775 StreamControl sc = (StreamControl) mSliderPanel.getChildAt(i).getTag();
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800776 updateSlider(sc);
777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
779
John Spurlock45601d62014-08-07 17:40:50 -0400780 private void updateZenPanelVisible() {
781 setZenPanelVisible(mZenModeAvailable && isNotificationOrRing(mActiveStreamType));
John Spurlock86005342014-05-23 11:58:00 -0400782 }
783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 public void postVolumeChanged(int streamType, int flags) {
785 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700786 synchronized (this) {
787 if (mStreamControls == null) {
788 createSliders();
789 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 removeMessages(MSG_FREE_RESOURCES);
792 obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
793 }
794
RoboErik19c95182014-06-23 15:38:48 -0700795 public void postRemoteVolumeChanged(MediaController controller, int flags) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700796 if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return;
797 synchronized (this) {
798 if (mStreamControls == null) {
799 createSliders();
800 }
801 }
802 removeMessages(MSG_FREE_RESOURCES);
RoboErik19c95182014-06-23 15:38:48 -0700803 obtainMessage(MSG_REMOTE_VOLUME_CHANGED, flags, 0, controller).sendToTarget();
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700804 }
805
806 public void postRemoteSliderVisibility(boolean visible) {
807 obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED,
808 AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget();
809 }
810
811 /**
812 * Called by AudioService when it has received new remote playback information that
813 * would affect the VolumePanel display (mainly volumes). The difference with
814 * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message
815 * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being
816 * displayed.
817 * This special code path is due to the fact that remote volume updates arrive to AudioService
818 * asynchronously. So after AudioService has sent the volume update (which should be treated
819 * as a request to update the volume), the application will likely set a new volume. If the UI
820 * is still up, we need to refresh the display to show this new value.
821 */
822 public void postHasNewRemotePlaybackInfo() {
823 if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return;
824 // don't create or prevent resources to be freed, if they disappear, this update came too
825 // late and shouldn't warrant the panel to be displayed longer
826 obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget();
827 }
828
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400829 public void postMasterVolumeChanged(int flags) {
830 postVolumeChanged(STREAM_MASTER, flags);
831 }
832
Mike Lockwoodce952c82011-11-14 10:47:42 -0800833 public void postMuteChanged(int streamType, int flags) {
834 if (hasMessages(MSG_VOLUME_CHANGED)) return;
Amith Yamasania6549862012-05-30 17:29:28 -0700835 synchronized (this) {
836 if (mStreamControls == null) {
837 createSliders();
838 }
Mike Lockwoodce952c82011-11-14 10:47:42 -0800839 }
840 removeMessages(MSG_FREE_RESOURCES);
841 obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget();
842 }
843
844 public void postMasterMuteChanged(int flags) {
845 postMuteChanged(STREAM_MASTER, flags);
846 }
847
Eric Laurentfde16d52012-12-03 14:42:39 -0800848 public void postDisplaySafeVolumeWarning(int flags) {
Eric Laurent0516a9e2012-09-19 11:53:03 -0700849 if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return;
Eric Laurentfde16d52012-12-03 14:42:39 -0800850 obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget();
Eric Laurentc34dcc12012-09-10 13:51:52 -0700851 }
852
John Spurlock1dad2722014-07-11 11:07:53 -0400853 public void postDismiss(long delay) {
854 forceTimeout(delay);
John Spurlock86005342014-05-23 11:58:00 -0400855 }
856
857 public void postLayoutDirection(int layoutDirection) {
858 removeMessages(MSG_LAYOUT_DIRECTION);
John Spurlock84da84c2014-05-31 22:21:52 -0400859 obtainMessage(MSG_LAYOUT_DIRECTION, layoutDirection, 0).sendToTarget();
John Spurlock3346a802014-05-20 16:25:37 -0400860 }
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 /**
863 * Override this if you have other work to do when the volume changes (for
864 * example, vibrating, playing a sound, etc.). Make sure to call through to
865 * the superclass implementation.
866 */
867 protected void onVolumeChanged(int streamType, int flags) {
868
John Spurlock86005342014-05-23 11:58:00 -0400869 if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870
871 if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
Amith Yamasania6549862012-05-30 17:29:28 -0700872 synchronized (this) {
873 if (mActiveStreamType != streamType) {
874 reorderSliders(streamType);
875 }
RoboErik19c95182014-06-23 15:38:48 -0700876 onShowVolumeChanged(streamType, flags, null);
Amith Yamasanie3361b82011-02-10 18:20:50 -0800877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
Marco Nelissen69f593c2009-07-28 09:55:04 -0700880 if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 removeMessages(MSG_PLAY_SOUND);
882 sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
883 }
884
885 if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
886 removeMessages(MSG_PLAY_SOUND);
887 removeMessages(MSG_VIBRATE);
888 onStopSounds();
889 }
890
891 removeMessages(MSG_FREE_RESOURCES);
892 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800893 resetTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
Mike Lockwoodce952c82011-11-14 10:47:42 -0800896 protected void onMuteChanged(int streamType, int flags) {
897
John Spurlock86005342014-05-23 11:58:00 -0400898 if (LOGD) Log.d(mTag, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")");
Mike Lockwoodce952c82011-11-14 10:47:42 -0800899
900 StreamControl sc = mStreamControls.get(streamType);
901 if (sc != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400902 updateSliderIcon(sc, isMuted(sc.streamType));
Mike Lockwoodce952c82011-11-14 10:47:42 -0800903 }
904
905 onVolumeChanged(streamType, flags);
906 }
907
RoboErik19c95182014-06-23 15:38:48 -0700908 protected void onShowVolumeChanged(int streamType, int flags, MediaController controller) {
Eric Laurent8c787522012-05-14 14:09:43 -0700909 int index = getStreamVolume(streamType);
Eric Laurentd72d51c2011-02-03 18:47:47 -0800910
Marco Nelissen69f593c2009-07-28 09:55:04 -0700911 mRingIsSilent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912
913 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -0400914 Log.d(mTag, "onShowVolumeChanged(streamType: " + streamType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 + ", flags: " + flags + "), index: " + index);
916 }
917
918 // get max volume for progress bar
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800919
Mike Lockwood8dc1dab2011-10-27 09:52:41 -0400920 int max = getStreamMaxVolume(streamType);
RoboErik19c95182014-06-23 15:38:48 -0700921 StreamControl sc = mStreamControls.get(streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 switch (streamType) {
924
925 case AudioManager.STREAM_RING: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800926// setRingerIcon();
Marco Nelissen69f593c2009-07-28 09:55:04 -0700927 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
928 mContext, RingtoneManager.TYPE_RINGTONE);
929 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700930 mRingIsSilent = true;
931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 break;
933 }
934
935 case AudioManager.STREAM_MUSIC: {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800936 // Special case for when Bluetooth is active for music
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800937 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
938 (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
939 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
940 AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800941 setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 } else {
Amith Yamasani2bbdd772011-02-02 18:54:13 -0800943 setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
945 break;
946 }
947
948 case AudioManager.STREAM_VOICE_CALL: {
949 /*
950 * For in-call voice call volume, there is no inaudible volume.
951 * Rescale the UI control so the progress bar doesn't go all
952 * the way to zero and don't show the mute icon.
953 */
954 index++;
955 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 break;
957 }
958
959 case AudioManager.STREAM_ALARM: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 break;
961 }
962
963 case AudioManager.STREAM_NOTIFICATION: {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700964 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
965 mContext, RingtoneManager.TYPE_NOTIFICATION);
966 if (ringuri == null) {
Marco Nelissen69f593c2009-07-28 09:55:04 -0700967 mRingIsSilent = true;
968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 break;
970 }
971
972 case AudioManager.STREAM_BLUETOOTH_SCO: {
973 /*
974 * For in-call voice call volume, there is no inaudible volume.
975 * Rescale the UI control so the progress bar doesn't go all
976 * the way to zero and don't show the mute icon.
977 */
978 index++;
979 max++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 break;
981 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -0700982
983 case AudioService.STREAM_REMOTE_MUSIC: {
RoboErik19c95182014-06-23 15:38:48 -0700984 if (controller == null && sc != null) {
985 // If we weren't passed one try using the last one set.
986 controller = sc.controller;
987 }
988 if (controller == null) {
989 // We still don't have one, ignore the command.
990 Log.w(mTag, "sent remote volume change without a controller!");
991 } else {
992 VolumeInfo vi = controller.getVolumeInfo();
993 index = vi.getCurrentVolume();
994 max = vi.getMaxVolume();
995 if ((vi.getVolumeControl() & VolumeProvider.VOLUME_CONTROL_FIXED) != 0) {
996 // if the remote volume is fixed add the flag for the UI
997 flags |= AudioManager.FLAG_FIXED_VOLUME;
998 }
999 }
John Spurlock86005342014-05-23 11:58:00 -04001000 if (LOGD) { Log.d(mTag, "showing remote volume "+index+" over "+ max); }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001001 break;
1002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001005 if (sc != null) {
RoboErik19c95182014-06-23 15:38:48 -07001006 if (streamType == AudioService.STREAM_REMOTE_MUSIC && controller != sc.controller) {
1007 if (sc.controller != null) {
1008 sc.controller.removeCallback(mMediaControllerCb);
1009 }
1010 sc.controller = controller;
1011 if (controller != null) {
1012 sc.controller.addCallback(mMediaControllerCb);
1013 }
1014 }
Amith Yamasanid47a3aee2011-08-23 11:11:35 -07001015 if (sc.seekbarView.getMax() != max) {
1016 sc.seekbarView.setMax(max);
1017 }
John Spurlockad494bc2014-07-19 15:56:19 -04001018 updateSliderProgress(sc, index);
John Spurlock7f1df5e2014-05-31 19:11:40 -04001019 updateSliderEnabled(sc, isMuted(streamType),
1020 (flags & AudioManager.FLAG_FIXED_VOLUME) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
John Spurlock86005342014-05-23 11:58:00 -04001023 if (!isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001024 int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
1025 // when the stream is for remote playback, use -1 to reset the stream type evaluation
1026 mAudioManager.forceVolumeControlStream(stream);
John Spurlockeb2727b2014-07-19 23:11:36 -04001027 mDialog.show();
1028 if (mCallback != null) {
1029 mCallback.onVisible(true);
John Spurlock86005342014-05-23 11:58:00 -04001030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 // Do a little vibrate if applicable (only when going into vibrate mode)
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001034 if ((streamType != AudioService.STREAM_REMOTE_MUSIC) &&
1035 ((flags & AudioManager.FLAG_VIBRATE) != 0) &&
John Spurlock3346a802014-05-20 16:25:37 -04001036 mAudioManager.isStreamAffectedByRingerMode(streamType) &&
Eric Laurent8c787522012-05-14 14:09:43 -07001037 mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
1039 }
John Spurlocka11b4af2014-06-01 11:52:23 -04001040
1041 // Pulse the slider icon if an adjustment was suppressed due to silent mode.
John Spurlockae641c92014-06-30 18:11:40 -04001042 if ((flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) {
1043 showSilentHint();
John Spurlocka11b4af2014-06-01 11:52:23 -04001044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046
John Spurlock86005342014-05-23 11:58:00 -04001047 private boolean isShowing() {
John Spurlockeb2727b2014-07-19 23:11:36 -04001048 return mDialog.isShowing();
John Spurlock86005342014-05-23 11:58:00 -04001049 }
1050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 protected void onPlaySound(int streamType, int flags) {
1052
1053 if (hasMessages(MSG_STOP_SOUNDS)) {
1054 removeMessages(MSG_STOP_SOUNDS);
1055 // Force stop right now
1056 onStopSounds();
1057 }
1058
1059 synchronized (this) {
1060 ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
Eric Laurent733a42b2011-01-19 10:41:57 -08001061 if (toneGen != null) {
1062 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
1063 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
1064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067
1068 protected void onStopSounds() {
1069
1070 synchronized (this) {
1071 int numStreamTypes = AudioSystem.getNumStreamTypes();
1072 for (int i = numStreamTypes - 1; i >= 0; i--) {
1073 ToneGenerator toneGen = mToneGenerators[i];
1074 if (toneGen != null) {
1075 toneGen.stopTone();
1076 }
1077 }
1078 }
1079 }
1080
1081 protected void onVibrate() {
1082
1083 // Make sure we ended up in vibrate ringer mode
Eric Laurent8c787522012-05-14 14:09:43 -07001084 if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 return;
1086 }
1087
John Spurlock7b414672014-07-18 13:02:39 -04001088 mVibrator.vibrate(VIBRATE_DURATION, VIBRATION_ATTRIBUTES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
RoboErik19c95182014-06-23 15:38:48 -07001091 protected void onRemoteVolumeChanged(MediaController controller, int flags) {
1092 if (LOGD) Log.d(mTag, "onRemoteVolumeChanged(controller:" + controller + ", flags: " + flags
1093 + ")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001094
John Spurlock86005342014-05-23 11:58:00 -04001095 if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || isShowing()) {
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001096 synchronized (this) {
1097 if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) {
1098 reorderSliders(AudioService.STREAM_REMOTE_MUSIC);
1099 }
RoboErik19c95182014-06-23 15:38:48 -07001100 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags, controller);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001101 }
1102 } else {
John Spurlock86005342014-05-23 11:58:00 -04001103 if (LOGD) Log.d(mTag, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001104 }
1105
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001106 removeMessages(MSG_FREE_RESOURCES);
1107 sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001108 resetTimeout();
1109 }
1110
1111 protected void onRemoteVolumeUpdateIfShown() {
John Spurlock86005342014-05-23 11:58:00 -04001112 if (LOGD) Log.d(mTag, "onRemoteVolumeUpdateIfShown()");
1113 if (isShowing()
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001114 && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC)
1115 && (mStreamControls != null)) {
RoboErik19c95182014-06-23 15:38:48 -07001116 onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0, null);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001117 }
1118 }
1119
RoboErik19c95182014-06-23 15:38:48 -07001120 /**
1121 * Clear the current remote stream controller.
1122 */
1123 private void clearRemoteStreamController() {
1124 if (mStreamControls != null) {
1125 StreamControl sc = mStreamControls.get(AudioService.STREAM_REMOTE_MUSIC);
1126 if (sc != null) {
1127 if (sc.controller != null) {
1128 sc.controller.removeCallback(mMediaControllerCb);
1129 sc.controller = null;
1130 }
1131 }
1132 }
1133 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001134
1135 /**
1136 * Handler for MSG_SLIDER_VISIBILITY_CHANGED
1137 * Hide or show a slider
1138 * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER,
1139 * or AudioService.STREAM_REMOTE_MUSIC
1140 * @param visible
1141 */
1142 synchronized protected void onSliderVisibilityChanged(int streamType, int visible) {
John Spurlock86005342014-05-23 11:58:00 -04001143 if (LOGD) Log.d(mTag, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")");
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001144 boolean isVisible = (visible == 1);
1145 for (int i = STREAMS.length - 1 ; i >= 0 ; i--) {
1146 StreamResources streamRes = STREAMS[i];
1147 if (streamRes.streamType == streamType) {
1148 streamRes.show = isVisible;
1149 if (!isVisible && (mActiveStreamType == streamType)) {
1150 mActiveStreamType = -1;
1151 }
1152 break;
1153 }
1154 }
1155 }
1156
Eric Laurentfde16d52012-12-03 14:42:39 -08001157 protected void onDisplaySafeVolumeWarning(int flags) {
John Spurlock35134602014-07-24 18:10:48 -04001158 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
1159 || isShowing()) {
1160 synchronized (sSafetyWarningLock) {
1161 if (sSafetyWarning != null) {
Eric Laurentfde16d52012-12-03 14:42:39 -08001162 return;
1163 }
John Spurlock35134602014-07-24 18:10:48 -04001164 sSafetyWarning = new SafetyWarning(mContext, this, mAudioManager);
1165 sSafetyWarning.show();
Eric Laurentfde16d52012-12-03 14:42:39 -08001166 }
1167 updateStates();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001168 }
Eric Laurentfde16d52012-12-03 14:42:39 -08001169 resetTimeout();
Eric Laurentc34dcc12012-09-10 13:51:52 -07001170 }
1171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
1174 */
1175 private ToneGenerator getOrCreateToneGenerator(int streamType) {
Christopher Tatec4b78d22012-05-22 13:57:58 -07001176 if (streamType == STREAM_MASTER) {
1177 // For devices that use the master volume setting only but still want to
1178 // play a volume-changed tone, direct the master volume pseudostream to
1179 // the system stream's tone generator.
1180 if (mPlayMasterStreamTones) {
1181 streamType = AudioManager.STREAM_SYSTEM;
1182 } else {
1183 return null;
1184 }
1185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 synchronized (this) {
1187 if (mToneGenerators[streamType] == null) {
Eric Laurent733a42b2011-01-19 10:41:57 -08001188 try {
1189 mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
1190 } catch (RuntimeException e) {
1191 if (LOGD) {
John Spurlock86005342014-05-23 11:58:00 -04001192 Log.d(mTag, "ToneGenerator constructor failed with "
Eric Laurent733a42b2011-01-19 10:41:57 -08001193 + "RuntimeException: " + e);
1194 }
1195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
Eric Laurent733a42b2011-01-19 10:41:57 -08001197 return mToneGenerators[streamType];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 }
1200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201
1202 /**
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001203 * Switch between icons because Bluetooth music is same as music volume, but with
1204 * different icons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 */
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001206 private void setMusicIcon(int resId, int resMuteId) {
1207 StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
1208 if (sc != null) {
1209 sc.iconRes = resId;
1210 sc.iconMuteRes = resMuteId;
John Spurlockad494bc2014-07-19 15:56:19 -04001211 updateSliderIcon(sc, isMuted(sc.streamType));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214
1215 protected void onFreeResources() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 synchronized (this) {
1217 for (int i = mToneGenerators.length - 1; i >= 0; i--) {
1218 if (mToneGenerators[i] != null) {
1219 mToneGenerators[i].release();
1220 }
1221 mToneGenerators[i] = null;
1222 }
1223 }
1224 }
1225
1226 @Override
1227 public void handleMessage(Message msg) {
1228 switch (msg.what) {
1229
1230 case MSG_VOLUME_CHANGED: {
1231 onVolumeChanged(msg.arg1, msg.arg2);
1232 break;
1233 }
1234
Mike Lockwoodce952c82011-11-14 10:47:42 -08001235 case MSG_MUTE_CHANGED: {
1236 onMuteChanged(msg.arg1, msg.arg2);
1237 break;
1238 }
1239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 case MSG_FREE_RESOURCES: {
1241 onFreeResources();
1242 break;
1243 }
1244
1245 case MSG_STOP_SOUNDS: {
1246 onStopSounds();
1247 break;
1248 }
1249
1250 case MSG_PLAY_SOUND: {
1251 onPlaySound(msg.arg1, msg.arg2);
1252 break;
1253 }
1254
1255 case MSG_VIBRATE: {
1256 onVibrate();
1257 break;
1258 }
1259
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001260 case MSG_TIMEOUT: {
John Spurlock86005342014-05-23 11:58:00 -04001261 if (isShowing()) {
John Spurlockeb2727b2014-07-19 23:11:36 -04001262 mDialog.dismiss();
1263 clearRemoteStreamController();
1264 mActiveStreamType = -1;
1265 if (mCallback != null) {
1266 mCallback.onVisible(false);
John Spurlock86005342014-05-23 11:58:00 -04001267 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001268 }
John Spurlock35134602014-07-24 18:10:48 -04001269 synchronized (sSafetyWarningLock) {
1270 if (sSafetyWarning != null) {
1271 if (LOGD) Log.d(mTag, "SafetyWarning timeout");
1272 sSafetyWarning.dismiss();
Eric Laurentfde16d52012-12-03 14:42:39 -08001273 }
1274 }
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001275 break;
1276 }
1277 case MSG_RINGER_MODE_CHANGED: {
John Spurlock86005342014-05-23 11:58:00 -04001278 if (isShowing()) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001279 updateStates();
1280 }
1281 break;
1282 }
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001283
1284 case MSG_REMOTE_VOLUME_CHANGED: {
RoboErik19c95182014-06-23 15:38:48 -07001285 onRemoteVolumeChanged((MediaController) msg.obj, msg.arg1);
Jean-Michel Trivi3114ce32012-06-11 15:03:52 -07001286 break;
1287 }
1288
1289 case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN:
1290 onRemoteVolumeUpdateIfShown();
1291 break;
1292
1293 case MSG_SLIDER_VISIBILITY_CHANGED:
1294 onSliderVisibilityChanged(msg.arg1, msg.arg2);
1295 break;
Eric Laurentc34dcc12012-09-10 13:51:52 -07001296
1297 case MSG_DISPLAY_SAFE_VOLUME_WARNING:
Eric Laurentfde16d52012-12-03 14:42:39 -08001298 onDisplaySafeVolumeWarning(msg.arg1);
Eric Laurentc34dcc12012-09-10 13:51:52 -07001299 break;
John Spurlock86005342014-05-23 11:58:00 -04001300
1301 case MSG_LAYOUT_DIRECTION:
1302 setLayoutDirection(msg.arg1);
1303 break;
1304
John Spurlock45601d62014-08-07 17:40:50 -04001305 case MSG_ZEN_MODE_AVAILABLE_CHANGED:
1306 mZenModeAvailable = msg.arg1 != 0;
1307 updateZenPanelVisible();
John Spurlockae641c92014-06-30 18:11:40 -04001308 break;
1309
1310 case MSG_USER_ACTIVITY:
1311 if (mCallback != null) {
1312 mCallback.onInteraction();
1313 }
John Spurlock86005342014-05-23 11:58:00 -04001314 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316 }
1317
John Spurlockae641c92014-06-30 18:11:40 -04001318 private void resetTimeout() {
John Spurlockea9938c2014-07-11 18:51:32 -04001319 if (LOGD) Log.d(mTag, "resetTimeout at " + System.currentTimeMillis()
1320 + " delay=" + mTimeoutDelay);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001321 removeMessages(MSG_TIMEOUT);
John Spurlock3bd4fee2014-05-29 20:51:09 -04001322 sendEmptyMessageDelayed(MSG_TIMEOUT, mTimeoutDelay);
John Spurlockae641c92014-06-30 18:11:40 -04001323 removeMessages(MSG_USER_ACTIVITY);
1324 sendEmptyMessage(MSG_USER_ACTIVITY);
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001325 }
1326
John Spurlock1dad2722014-07-11 11:07:53 -04001327 private void forceTimeout(long delay) {
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001328 removeMessages(MSG_TIMEOUT);
John Spurlock1dad2722014-07-11 11:07:53 -04001329 sendEmptyMessageDelayed(MSG_TIMEOUT, delay);
John Spurlock86005342014-05-23 11:58:00 -04001330 }
1331
1332 public ZenModeController getZenController() {
1333 return mZenController;
Amith Yamasanibaf6dbf2011-08-18 17:40:29 -07001334 }
1335
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001336 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
1337 @Override
1338 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1339 final Object tag = seekBar.getTag();
1340 if (fromUser && tag instanceof StreamControl) {
1341 StreamControl sc = (StreamControl) tag;
John Spurlockac9ab5b2014-07-20 13:09:44 -04001342 setStreamVolume(sc, progress,
1343 AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE);
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001344 }
1345 resetTimeout();
1346 }
1347
1348 @Override
1349 public void onStartTrackingTouch(SeekBar seekBar) {
1350 }
1351
1352 @Override
1353 public void onStopTrackingTouch(SeekBar seekBar) {
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001354 }
Alan Viverettee8ebaf32014-04-11 15:44:15 -07001355 };
Amith Yamasani2bbdd772011-02-02 18:54:13 -08001356
John Spurlock86005342014-05-23 11:58:00 -04001357 private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
John Spurlock45601d62014-08-07 17:40:50 -04001358 public void onZenAvailableChanged(boolean available) {
1359 obtainMessage(MSG_ZEN_MODE_AVAILABLE_CHANGED, available ? 1 : 0, 0).sendToTarget();
John Spurlock86005342014-05-23 11:58:00 -04001360 }
1361 };
RoboErik19c95182014-06-23 15:38:48 -07001362
1363 private final MediaController.Callback mMediaControllerCb = new MediaController.Callback() {
1364 public void onVolumeInfoChanged(VolumeInfo info) {
1365 onRemoteVolumeUpdateIfShown();
1366 }
1367 };
John Spurlockae641c92014-06-30 18:11:40 -04001368
1369 public interface Callback {
1370 void onZenSettings();
1371 void onInteraction();
John Spurlock33f4e042014-07-11 13:10:58 -04001372 void onVisible(boolean visible);
John Spurlockae641c92014-06-30 18:11:40 -04001373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374}