blob: 0d41e2029086cc12d87243b0c8ce4e0935190241 [file] [log] [blame]
John Spurlockf88d8082015-03-25 18:09:51 -04001/*
2 * Copyright (C) 2015 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
17package com.android.systemui.volume;
18
Jason Monk782cd672017-03-22 12:50:57 -040019import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
20import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_GENERIC;
21
Julia Reynolds42411922017-11-08 11:19:09 -050022import static com.android.systemui.volume.Events.DISMISS_REASON_TOUCH_OUTSIDE;
23
John Spurlock5adeabc2015-05-05 14:50:43 -040024import android.accessibilityservice.AccessibilityServiceInfo;
Julia Reynolds4b006a82016-04-20 16:09:06 -040025import android.animation.ObjectAnimator;
Julia Reynolds2feab162016-04-21 14:58:52 -040026import android.annotation.NonNull;
John Spurlockf88d8082015-03-25 18:09:51 -040027import android.annotation.SuppressLint;
28import android.app.Dialog;
29import android.app.KeyguardManager;
30import android.content.Context;
Julia Reynolds42411922017-11-08 11:19:09 -050031import android.content.DialogInterface;
John Spurlock77bb25af2015-05-11 12:12:07 -040032import android.content.res.ColorStateList;
Julia Reynoldsc791e052016-02-19 16:02:01 -050033import android.content.res.Configuration;
John Spurlockf88d8082015-03-25 18:09:51 -040034import android.content.res.Resources;
Jason Monk98ad83c2017-06-13 14:03:45 -040035import android.graphics.Color;
John Spurlockf88d8082015-03-25 18:09:51 -040036import android.graphics.Rect;
Jason Monk98ad83c2017-06-13 14:03:45 -040037import android.graphics.drawable.ColorDrawable;
John Spurlockf88d8082015-03-25 18:09:51 -040038import android.media.AudioManager;
39import android.media.AudioSystem;
John Spurlock5adeabc2015-05-05 14:50:43 -040040import android.os.Debug;
John Spurlockf88d8082015-03-25 18:09:51 -040041import android.os.Handler;
42import android.os.Looper;
43import android.os.Message;
44import android.os.SystemClock;
45import android.provider.Settings.Global;
John Spurlockf88d8082015-03-25 18:09:51 -040046import android.util.Log;
Julia Reynolds03c548f2016-12-14 15:02:38 -050047import android.util.Slog;
John Spurlockf88d8082015-03-25 18:09:51 -040048import android.util.SparseBooleanArray;
Lucas Dupin448786c2017-07-24 17:44:25 -070049import android.view.ContextThemeWrapper;
John Spurlockf88d8082015-03-25 18:09:51 -040050import android.view.MotionEvent;
51import android.view.View;
John Spurlock5adeabc2015-05-05 14:50:43 -040052import android.view.View.AccessibilityDelegate;
53import android.view.View.OnAttachStateChangeListener;
John Spurlockf88d8082015-03-25 18:09:51 -040054import android.view.View.OnClickListener;
John Spurlockf88d8082015-03-25 18:09:51 -040055import android.view.View.OnTouchListener;
56import android.view.ViewGroup;
John Spurlockf88d8082015-03-25 18:09:51 -040057import android.view.Window;
58import android.view.WindowManager;
John Spurlock5adeabc2015-05-05 14:50:43 -040059import android.view.accessibility.AccessibilityEvent;
60import android.view.accessibility.AccessibilityManager;
61import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
Julia Reynolds4b006a82016-04-20 16:09:06 -040062import android.view.animation.DecelerateInterpolator;
John Spurlockf88d8082015-03-25 18:09:51 -040063import android.widget.ImageButton;
John Spurlockf88d8082015-03-25 18:09:51 -040064import android.widget.SeekBar;
65import android.widget.SeekBar.OnSeekBarChangeListener;
66import android.widget.TextView;
Lucas Dupin7224c1e2017-07-06 14:35:30 -070067
Andrew Sapperstein5c373442016-06-12 13:17:16 -070068import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050069import com.android.systemui.Dependency;
Julia Reynolds42411922017-11-08 11:19:09 -050070import com.android.systemui.HardwareUiLayout;
71import com.android.systemui.Interpolators;
John Spurlockf88d8082015-03-25 18:09:51 -040072import com.android.systemui.R;
Rajeev Kumar5c964a42017-06-13 19:41:35 -070073import com.android.systemui.plugins.VolumeDialog;
Jason Monk782cd672017-03-22 12:50:57 -040074import com.android.systemui.plugins.VolumeDialogController;
75import com.android.systemui.plugins.VolumeDialogController.State;
76import com.android.systemui.plugins.VolumeDialogController.StreamState;
Lucas Dupin7224c1e2017-07-06 14:35:30 -070077
John Spurlockf88d8082015-03-25 18:09:51 -040078import java.io.PrintWriter;
79import java.util.ArrayList;
80import java.util.List;
81
82/**
83 * Visual presentation of the volume dialog.
84 *
Jason Monk782cd672017-03-22 12:50:57 -040085 * A client of VolumeDialogControllerImpl and its state model.
John Spurlockf88d8082015-03-25 18:09:51 -040086 *
87 * Methods ending in "H" must be called on the (ui) handler.
88 */
Julia Reynolds66d28942017-11-07 09:43:54 -050089public class VolumeDialogImpl implements VolumeDialog {
Jason Monk782cd672017-03-22 12:50:57 -040090 private static final String TAG = Util.logTag(VolumeDialogImpl.class);
John Spurlockf88d8082015-03-25 18:09:51 -040091
92 private static final long USER_ATTEMPT_GRACE_PERIOD = 1000;
Julia Reynolds4b006a82016-04-20 16:09:06 -040093 private static final int UPDATE_ANIMATION_DURATION = 80;
John Spurlockf88d8082015-03-25 18:09:51 -040094
95 private final Context mContext;
96 private final H mHandler = new H();
Rajeev Kumar5c964a42017-06-13 19:41:35 -070097 private final VolumeDialogController mController;
John Spurlockf88d8082015-03-25 18:09:51 -040098
Julia Reynolds568585b2016-07-25 08:55:15 -040099 private Window mWindow;
Julia Reynolds42411922017-11-08 11:19:09 -0500100 private HardwareUiLayout mHardwareLayout;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500101 private CustomDialog mDialog;
102 private ViewGroup mDialogView;
Julia Reynolds568585b2016-07-25 08:55:15 -0400103 private ViewGroup mDialogRowsView;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500104 private ViewGroup mDialogContentView;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500105 private final List<VolumeRow> mRows = new ArrayList<>();
Julia Reynolds03c548f2016-12-14 15:02:38 -0500106 private ConfigurableTexts mConfigurableTexts;
John Spurlockf88d8082015-03-25 18:09:51 -0400107 private final SparseBooleanArray mDynamic = new SparseBooleanArray();
108 private final KeyguardManager mKeyguard;
Julia Reynolds2feab162016-04-21 14:58:52 -0400109 private final AccessibilityManager mAccessibilityMgr;
John Spurlock76b52b32015-04-03 00:00:12 -0400110 private final Object mSafetyWarningLock = new Object();
John Spurlock5adeabc2015-05-05 14:50:43 -0400111 private final Accessibility mAccessibility = new Accessibility();
John Spurlock77bb25af2015-05-11 12:12:07 -0400112 private final ColorStateList mActiveSliderTint;
113 private final ColorStateList mInactiveSliderTint;
John Spurlockf88d8082015-03-25 18:09:51 -0400114
115 private boolean mShowing;
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500116 private boolean mShowA11yStream;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500117
John Spurlockf88d8082015-03-25 18:09:51 -0400118 private int mActiveStream;
Beverlyfa4c8e72017-08-10 14:55:34 -0400119 private int mPrevActiveStream;
John Spurlockf55b7f22015-04-13 19:21:26 -0400120 private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE;
121 private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE;
John Spurlockf88d8082015-03-25 18:09:51 -0400122 private State mState;
John Spurlock76b52b32015-04-03 00:00:12 -0400123 private SafetyWarningDialog mSafetyWarning;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700124 private boolean mHovering = false;
John Spurlockf88d8082015-03-25 18:09:51 -0400125
Jason Monk782cd672017-03-22 12:50:57 -0400126 public VolumeDialogImpl(Context context) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700127 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk782cd672017-03-22 12:50:57 -0400128 mController = Dependency.get(VolumeDialogController.class);
129 mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Julia Reynolds568585b2016-07-25 08:55:15 -0400130 mAccessibilityMgr =
131 (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700132 mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext));
Julia Reynoldsc791e052016-02-19 16:02:01 -0500133 mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive);
Jason Monk782cd672017-03-22 12:50:57 -0400134 }
135
136 public void init(int windowType, Callback callback) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500137 initDialog();
138
139 mAccessibility.init();
140
Jason Monk782cd672017-03-22 12:50:57 -0400141 mController.addCallback(mControllerCallbackH, mHandler);
142 mController.getState();
Julia Reynoldsc791e052016-02-19 16:02:01 -0500143 }
144
Jason Monk782cd672017-03-22 12:50:57 -0400145 @Override
146 public void destroy() {
Jason Monk3b9357f2017-07-14 09:40:54 -0400147 mAccessibility.destroy();
Jason Monk782cd672017-03-22 12:50:57 -0400148 mController.removeCallback(mControllerCallbackH);
Jason Monk3b9357f2017-07-14 09:40:54 -0400149 mHandler.removeCallbacksAndMessages(null);
Jason Monk782cd672017-03-22 12:50:57 -0400150 }
151
Julia Reynoldsc791e052016-02-19 16:02:01 -0500152 private void initDialog() {
John Spurlockf88d8082015-03-25 18:09:51 -0400153 mDialog = new CustomDialog(mContext);
154
Julia Reynolds03c548f2016-12-14 15:02:38 -0500155 mConfigurableTexts = new ConfigurableTexts(mContext);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500156 mHovering = false;
157 mShowing = false;
Julia Reynolds568585b2016-07-25 08:55:15 -0400158 mWindow = mDialog.getWindow();
159 mWindow.requestFeature(Window.FEATURE_NO_TITLE);
Jason Monk98ad83c2017-06-13 14:03:45 -0400160 mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Julia Reynolds42411922017-11-08 11:19:09 -0500161 mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
162 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
163 mWindow.addFlags(
164 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
165 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
166 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
167 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
168 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
169 mWindow.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
170 mWindow.setWindowAnimations(com.android.internal.R.style.Animation_Toast);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500171
Jason Monk98ad83c2017-06-13 14:03:45 -0400172 mDialog.setContentView(R.layout.volume_dialog);
173 mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog);
174 mDialogView.setOnHoverListener(new View.OnHoverListener() {
175 @Override
176 public boolean onHover(View v, MotionEvent event) {
177 int action = event.getActionMasked();
178 mHovering = (action == MotionEvent.ACTION_HOVER_ENTER)
179 || (action == MotionEvent.ACTION_HOVER_MOVE);
180 rescheduleTimeoutH();
181 return true;
182 }
Jun Mukai3f5d7132015-07-21 14:10:09 -0700183 });
Julia Reynolds42411922017-11-08 11:19:09 -0500184 mHardwareLayout = HardwareUiLayout.get(mDialogView);
185 mHardwareLayout.setOutsideTouchListener(view -> dismiss(DISMISS_REASON_TOUCH_OUTSIDE));
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700186
Jason Monk16fbd9d2017-04-27 14:28:49 -0400187 mDialogContentView = mDialog.findViewById(R.id.volume_dialog_content);
188 mDialogRowsView = mDialogContentView.findViewById(R.id.volume_dialog_rows);
John Spurlockf88d8082015-03-25 18:09:51 -0400189
Julia Reynoldsc791e052016-02-19 16:02:01 -0500190 if (mRows.isEmpty()) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500191 addRow(AudioManager.STREAM_MUSIC,
192 R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700193 if (!AudioSystem.isSingleVolume(mContext)) {
194 addRow(AudioManager.STREAM_RING,
195 R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700196 addRow(AudioManager.STREAM_ALARM,
197 R.drawable.ic_volume_alarm, R.drawable.ic_volume_alarm_mute, false);
198 addRow(AudioManager.STREAM_VOICE_CALL,
199 R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, false);
200 addRow(AudioManager.STREAM_BLUETOOTH_SCO,
201 R.drawable.ic_volume_bt_sco, R.drawable.ic_volume_bt_sco, false);
202 addRow(AudioManager.STREAM_SYSTEM,
203 R.drawable.ic_volume_system, R.drawable.ic_volume_system_mute, false);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500204 addRow(AudioManager.STREAM_ACCESSIBILITY, R.drawable.ic_volume_accessibility,
205 R.drawable.ic_volume_accessibility, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700206 }
Julia Reynoldsc791e052016-02-19 16:02:01 -0500207 } else {
208 addExistingRows();
209 }
Julia Reynolds08ce3102017-11-16 09:29:55 -0500210 updateRowsH(getActiveRow());
John Spurlockf88d8082015-03-25 18:09:51 -0400211 }
212
John Spurlock77bb25af2015-05-11 12:12:07 -0400213 private ColorStateList loadColorStateList(int colorResId) {
214 return ColorStateList.valueOf(mContext.getColor(colorResId));
215 }
216
John Spurlockf88d8082015-03-25 18:09:51 -0400217 public void setStreamImportant(int stream, boolean important) {
218 mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget();
219 }
220
John Spurlockf88d8082015-03-25 18:09:51 -0400221 public void setAutomute(boolean automute) {
222 if (mAutomute == automute) return;
223 mAutomute = automute;
224 mHandler.sendEmptyMessage(H.RECHECK_ALL);
225 }
226
227 public void setSilentMode(boolean silentMode) {
228 if (mSilentMode == silentMode) return;
229 mSilentMode = silentMode;
230 mHandler.sendEmptyMessage(H.RECHECK_ALL);
231 }
232
233 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500234 addRow(stream, iconRes, iconMuteRes, important, false);
235 }
236
237 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important,
238 boolean dynamic) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500239 VolumeRow row = new VolumeRow();
240 initRow(row, stream, iconRes, iconMuteRes, important);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500241 int rowSize;
242 int viewSize;
243 if (mShowA11yStream && dynamic && (rowSize = mRows.size()) > 1
244 && (viewSize = mDialogRowsView.getChildCount()) > 1) {
245 // A11y Stream should be the last in the list
246 mDialogRowsView.addView(row.view, viewSize - 2);
247 mRows.add(rowSize - 2, row);
248 } else {
249 mDialogRowsView.addView(row.view);
250 mRows.add(row);
251 }
John Spurlockf88d8082015-03-25 18:09:51 -0400252 }
253
Julia Reynoldsc791e052016-02-19 16:02:01 -0500254 private void addExistingRows() {
255 int N = mRows.size();
256 for (int i = 0; i < N; i++) {
257 final VolumeRow row = mRows.get(i);
258 initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important);
Julia Reynolds568585b2016-07-25 08:55:15 -0400259 mDialogRowsView.addView(row.view);
Julia Reynolds42411922017-11-08 11:19:09 -0500260 updateVolumeRowH(row);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500261 }
262 }
263
John Spurlockf88d8082015-03-25 18:09:51 -0400264 private VolumeRow getActiveRow() {
265 for (VolumeRow row : mRows) {
266 if (row.stream == mActiveStream) {
267 return row;
268 }
269 }
270 return mRows.get(0);
271 }
272
273 private VolumeRow findRow(int stream) {
274 for (VolumeRow row : mRows) {
275 if (row.stream == stream) return row;
276 }
277 return null;
278 }
279
John Spurlockf88d8082015-03-25 18:09:51 -0400280 public void dump(PrintWriter writer) {
Jason Monk782cd672017-03-22 12:50:57 -0400281 writer.println(VolumeDialogImpl.class.getSimpleName() + " state:");
John Spurlockf88d8082015-03-25 18:09:51 -0400282 writer.print(" mShowing: "); writer.println(mShowing);
John Spurlockf88d8082015-03-25 18:09:51 -0400283 writer.print(" mActiveStream: "); writer.println(mActiveStream);
284 writer.print(" mDynamic: "); writer.println(mDynamic);
John Spurlockf88d8082015-03-25 18:09:51 -0400285 writer.print(" mAutomute: "); writer.println(mAutomute);
286 writer.print(" mSilentMode: "); writer.println(mSilentMode);
John Spurlock84ee67c2015-05-20 23:33:43 -0400287 writer.print(" mAccessibility.mFeedbackEnabled: ");
288 writer.println(mAccessibility.mFeedbackEnabled);
John Spurlockf88d8082015-03-25 18:09:51 -0400289 }
290
Julia Reynolds4b006a82016-04-20 16:09:06 -0400291 private static int getImpliedLevel(SeekBar seekBar, int progress) {
292 final int m = seekBar.getMax();
293 final int n = m / 100 - 1;
294 final int level = progress == 0 ? 0
295 : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n));
296 return level;
297 }
298
John Spurlockf88d8082015-03-25 18:09:51 -0400299 @SuppressLint("InflateParams")
Julia Reynoldsc791e052016-02-19 16:02:01 -0500300 private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes,
301 boolean important) {
John Spurlockf88d8082015-03-25 18:09:51 -0400302 row.stream = stream;
303 row.iconRes = iconRes;
304 row.iconMuteRes = iconMuteRes;
305 row.important = important;
306 row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
Julia Reynolds568585b2016-07-25 08:55:15 -0400307 row.view.setId(row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400308 row.view.setTag(row);
Julia Reynolds42411922017-11-08 11:19:09 -0500309 row.header = row.view.findViewById(R.id.volume_row_header);
Julia Reynolds568585b2016-07-25 08:55:15 -0400310 row.header.setId(20 * row.stream);
Julia Reynolds42411922017-11-08 11:19:09 -0500311 row.slider = row.view.findViewById(R.id.volume_row_slider);
John Spurlockf88d8082015-03-25 18:09:51 -0400312 row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
Julia Reynolds5774cf02016-05-26 13:43:38 -0400313 row.anim = null;
John Spurlockf88d8082015-03-25 18:09:51 -0400314
315 // forward events above the slider into the slider
316 row.view.setOnTouchListener(new OnTouchListener() {
317 private final Rect mSliderHitRect = new Rect();
318 private boolean mDragging;
319
320 @SuppressLint("ClickableViewAccessibility")
321 @Override
322 public boolean onTouch(View v, MotionEvent event) {
323 row.slider.getHitRect(mSliderHitRect);
324 if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN
325 && event.getY() < mSliderHitRect.top) {
326 mDragging = true;
327 }
328 if (mDragging) {
329 event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top);
330 row.slider.dispatchTouchEvent(event);
331 if (event.getActionMasked() == MotionEvent.ACTION_UP
332 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
333 mDragging = false;
334 }
335 return true;
336 }
337 return false;
338 }
339 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400340 row.icon = row.view.findViewById(R.id.volume_row_icon);
John Spurlockf88d8082015-03-25 18:09:51 -0400341 row.icon.setImageResource(iconRes);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500342 if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) {
343 row.icon.setOnClickListener(new OnClickListener() {
344 @Override
345 public void onClick(View v) {
346 Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState);
347 mController.setActiveStream(row.stream);
348 if (row.stream == AudioManager.STREAM_RING) {
349 final boolean hasVibrator = mController.hasVibrator();
350 if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {
351 if (hasVibrator) {
352 mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);
353 } else {
354 final boolean wasZero = row.ss.level == 0;
355 mController.setStreamVolume(stream,
356 wasZero ? row.lastAudibleLevel : 0);
357 }
John Spurlockf88d8082015-03-25 18:09:51 -0400358 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500359 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
360 if (row.ss.level == 0) {
361 mController.setStreamVolume(stream, 1);
362 }
John Spurlockf88d8082015-03-25 18:09:51 -0400363 }
364 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500365 final boolean vmute = row.ss.level == row.ss.levelMin;
366 mController.setStreamVolume(stream,
367 vmute ? row.lastAudibleLevel : row.ss.levelMin);
John Spurlockf88d8082015-03-25 18:09:51 -0400368 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500369 row.userAttempt = 0; // reset the grace period, slider updates immediately
John Spurlockf88d8082015-03-25 18:09:51 -0400370 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500371 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400372 } else {
373 row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500374 }
John Spurlockf88d8082015-03-25 18:09:51 -0400375 }
376
John Spurlockf88d8082015-03-25 18:09:51 -0400377 public void show(int reason) {
378 mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget();
379 }
380
381 public void dismiss(int reason) {
382 mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget();
383 }
384
John Spurlockf88d8082015-03-25 18:09:51 -0400385 private void showH(int reason) {
John Spurlock22def3d2015-06-17 11:56:12 -0400386 if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]);
John Spurlockf88d8082015-03-25 18:09:51 -0400387 mHandler.removeMessages(H.SHOW);
388 mHandler.removeMessages(H.DISMISS);
389 rescheduleTimeoutH();
390 if (mShowing) return;
391 mShowing = true;
Julia Reynolds42411922017-11-08 11:19:09 -0500392 mHardwareLayout.setTranslationX(getAnimTranslation());
393 mHardwareLayout.setAlpha(0);
394 mHardwareLayout.animate()
395 .alpha(1)
396 .translationX(0)
397 .setDuration(300)
398 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
399 .withEndAction(() -> {
400 mDialog.show();
401 mWindow.getDecorView().requestAccessibilityFocus();
402 })
403 .start();
Chris Wrene565ee62015-06-17 15:24:56 -0400404 Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
John Spurlockf88d8082015-03-25 18:09:51 -0400405 mController.notifyVisible(true);
406 }
407
Julia Reynolds42411922017-11-08 11:19:09 -0500408 private float getAnimTranslation() {
409 return mContext.getResources().getDimension(
410 R.dimen.volume_dialog_panel_width) / 2;
411 }
412
John Spurlockf88d8082015-03-25 18:09:51 -0400413 protected void rescheduleTimeoutH() {
414 mHandler.removeMessages(H.DISMISS);
John Spurlockf09838b2015-06-11 16:14:44 -0400415 final int timeout = computeTimeoutH();
416 mHandler.sendMessageDelayed(mHandler
417 .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout);
John Spurlock5adeabc2015-05-05 14:50:43 -0400418 if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller());
John Spurlockf88d8082015-03-25 18:09:51 -0400419 mController.userActivity();
420 }
421
422 private int computeTimeoutH() {
John Spurlockf09838b2015-06-11 16:14:44 -0400423 if (mAccessibility.mFeedbackEnabled) return 20000;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700424 if (mHovering) return 16000;
John Spurlock76b52b32015-04-03 00:00:12 -0400425 if (mSafetyWarning != null) return 5000;
John Spurlockf88d8082015-03-25 18:09:51 -0400426 if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500;
427 return 3000;
428 }
429
430 protected void dismissH(int reason) {
431 mHandler.removeMessages(H.DISMISS);
432 mHandler.removeMessages(H.SHOW);
433 if (!mShowing) return;
434 mShowing = false;
Julia Reynolds42411922017-11-08 11:19:09 -0500435 mHardwareLayout.setTranslationX(0);
436 mHardwareLayout.setAlpha(1);
437 mHardwareLayout.animate()
438 .alpha(0)
439 .translationX(getAnimTranslation())
440 .setDuration(300)
441 .withEndAction(() -> mDialog.dismiss())
442 .setInterpolator(new SystemUIInterpolators.LogAccelerateInterpolator())
443 .start();
Julia Reynolds2feab162016-04-21 14:58:52 -0400444 if (mAccessibilityMgr.isEnabled()) {
445 AccessibilityEvent event =
446 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
447 event.setPackageName(mContext.getPackageName());
448 event.setClassName(CustomDialog.class.getSuperclass().getName());
449 event.getText().add(mContext.getString(
450 R.string.volume_dialog_accessibility_dismissed_message));
451 mAccessibilityMgr.sendAccessibilityEvent(event);
452 }
Chris Wrene565ee62015-06-17 15:24:56 -0400453 Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason);
John Spurlockf88d8082015-03-25 18:09:51 -0400454 mController.notifyVisible(false);
John Spurlock76b52b32015-04-03 00:00:12 -0400455 synchronized (mSafetyWarningLock) {
456 if (mSafetyWarning != null) {
457 if (D.BUG) Log.d(TAG, "SafetyWarning dismissed");
458 mSafetyWarning.dismiss();
459 }
460 }
John Spurlockf88d8082015-03-25 18:09:51 -0400461 }
462
Beverlyfa4c8e72017-08-10 14:55:34 -0400463 private boolean shouldBeVisibleH(VolumeRow row, VolumeRow activeRow) {
464 boolean isActive = row == activeRow;
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500465 if (row.stream == AudioSystem.STREAM_ACCESSIBILITY) {
466 return mShowA11yStream;
467 }
Beverlyfa4c8e72017-08-10 14:55:34 -0400468
469 // if the active row is accessibility, then continue to display previous
Julia Reynolds66d28942017-11-07 09:43:54 -0500470 // active row since accessibility is displayed under it
Beverlyfa4c8e72017-08-10 14:55:34 -0400471 if (activeRow.stream == AudioSystem.STREAM_ACCESSIBILITY &&
472 row.stream == mPrevActiveStream) {
473 return true;
474 }
475
Julia Reynolds66d28942017-11-07 09:43:54 -0500476 return row.important || isActive;
John Spurlockf88d8082015-03-25 18:09:51 -0400477 }
478
Julia Reynolds568585b2016-07-25 08:55:15 -0400479 private void updateRowsH(final VolumeRow activeRow) {
John Spurlock22def3d2015-06-17 11:56:12 -0400480 if (D.BUG) Log.d(TAG, "updateRowsH");
John Spurlockf88d8082015-03-25 18:09:51 -0400481 if (!mShowing) {
482 trimObsoleteH();
483 }
John Spurlockf88d8082015-03-25 18:09:51 -0400484 // apply changes to all rows
Julia Reynolds568585b2016-07-25 08:55:15 -0400485 for (final VolumeRow row : mRows) {
John Spurlockf88d8082015-03-25 18:09:51 -0400486 final boolean isActive = row == activeRow;
Beverlyfa4c8e72017-08-10 14:55:34 -0400487 final boolean shouldBeVisible = shouldBeVisibleH(row, activeRow);
Julia Reynolds568585b2016-07-25 08:55:15 -0400488 Util.setVisOrGone(row.view, shouldBeVisible);
Jason Monk98ad83c2017-06-13 14:03:45 -0400489 Util.setVisOrGone(row.header, shouldBeVisible);
Julia Reynolds568585b2016-07-25 08:55:15 -0400490 if (row.view.isShown()) {
Julia Reynolds568585b2016-07-25 08:55:15 -0400491 updateVolumeRowSliderTintH(row, isActive);
492 }
John Spurlockf88d8082015-03-25 18:09:51 -0400493 }
494 }
495
496 private void trimObsoleteH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400497 if (D.BUG) Log.d(TAG, "trimObsoleteH");
Julia Reynolds568585b2016-07-25 08:55:15 -0400498 for (int i = mRows.size() - 1; i >= 0; i--) {
John Spurlockf88d8082015-03-25 18:09:51 -0400499 final VolumeRow row = mRows.get(i);
500 if (row.ss == null || !row.ss.dynamic) continue;
501 if (!mDynamic.get(row.stream)) {
502 mRows.remove(i);
Julia Reynolds568585b2016-07-25 08:55:15 -0400503 mDialogRowsView.removeView(row.view);
John Spurlockf88d8082015-03-25 18:09:51 -0400504 }
505 }
506 }
507
508 private void onStateChangedH(State state) {
509 mState = state;
510 mDynamic.clear();
511 // add any new dynamic rows
512 for (int i = 0; i < state.states.size(); i++) {
513 final int stream = state.states.keyAt(i);
514 final StreamState ss = state.states.valueAt(i);
515 if (!ss.dynamic) continue;
516 mDynamic.put(stream, true);
517 if (findRow(stream) == null) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500518 addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true,
519 true);
John Spurlockf88d8082015-03-25 18:09:51 -0400520 }
521 }
522
523 if (mActiveStream != state.activeStream) {
Beverlyfa4c8e72017-08-10 14:55:34 -0400524 mPrevActiveStream = mActiveStream;
John Spurlockf88d8082015-03-25 18:09:51 -0400525 mActiveStream = state.activeStream;
Julia Reynolds568585b2016-07-25 08:55:15 -0400526 updateRowsH(getActiveRow());
John Spurlockf88d8082015-03-25 18:09:51 -0400527 rescheduleTimeoutH();
528 }
529 for (VolumeRow row : mRows) {
530 updateVolumeRowH(row);
531 }
John Spurlockf88d8082015-03-25 18:09:51 -0400532 }
533
534 private void updateVolumeRowH(VolumeRow row) {
John Spurlock22def3d2015-06-17 11:56:12 -0400535 if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400536 if (mState == null) return;
537 final StreamState ss = mState.states.get(row.stream);
538 if (ss == null) return;
539 row.ss = ss;
540 if (ss.level > 0) {
541 row.lastAudibleLevel = ss.level;
542 }
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400543 if (ss.level == row.requestedLevel) {
544 row.requestedLevel = -1;
545 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500546 final boolean isA11yStream = row.stream == AudioManager.STREAM_ACCESSIBILITY;
John Spurlockf88d8082015-03-25 18:09:51 -0400547 final boolean isRingStream = row.stream == AudioManager.STREAM_RING;
548 final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM;
John Spurlockd9c75db2015-04-28 11:19:13 -0400549 final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM;
550 final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC;
John Spurlockf88d8082015-03-25 18:09:51 -0400551 final boolean isRingVibrate = isRingStream
552 && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE;
John Spurlockd9c75db2015-04-28 11:19:13 -0400553 final boolean isRingSilent = isRingStream
554 && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT;
555 final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS;
556 final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
John Spurlockd9c75db2015-04-28 11:19:13 -0400557 final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream)
558 : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream)
559 : false;
John Spurlockf88d8082015-03-25 18:09:51 -0400560
561 // update slider max
Julia Reynolds4b006a82016-04-20 16:09:06 -0400562 final int max = ss.levelMax * 100;
John Spurlockf88d8082015-03-25 18:09:51 -0400563 if (max != row.slider.getMax()) {
564 row.slider.setMax(max);
565 }
566
John Spurlockf88d8082015-03-25 18:09:51 -0400567 // update header text
Julia Reynolds03c548f2016-12-14 15:02:38 -0500568 Util.setText(row.header, getStreamLabelH(ss));
Lucas Dupin421b20c2017-08-01 10:36:10 -0700569 row.slider.setContentDescription(row.header.getText());
Julia Reynolds03c548f2016-12-14 15:02:38 -0500570 mConfigurableTexts.add(row.header, ss.name);
John Spurlockf88d8082015-03-25 18:09:51 -0400571
572 // update icon
John Spurlockd9c75db2015-04-28 11:19:13 -0400573 final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted;
John Spurlockf88d8082015-03-25 18:09:51 -0400574 row.icon.setEnabled(iconEnabled);
575 row.icon.setAlpha(iconEnabled ? 1 : 0.5f);
576 final int iconRes =
John Spurlock76b52b32015-04-03 00:00:12 -0400577 isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
Julia Reynolds42411922017-11-08 11:19:09 -0500578 : isRingSilent || zenMuted ? row.iconMuteRes
John Spurlockf88d8082015-03-25 18:09:51 -0400579 : ss.routedToBluetooth ?
John Spurlockf15ef112015-04-09 22:04:14 -0400580 (ss.muted ? R.drawable.ic_volume_media_bt_mute
581 : R.drawable.ic_volume_media_bt)
John Spurlock76b52b32015-04-03 00:00:12 -0400582 : mAutomute && ss.level == 0 ? row.iconMuteRes
John Spurlockf88d8082015-03-25 18:09:51 -0400583 : (ss.muted ? row.iconMuteRes : row.iconRes);
Julia Reynolds42411922017-11-08 11:19:09 -0500584 row.icon.setImageResource(iconRes);
John Spurlockf88d8082015-03-25 18:09:51 -0400585 row.iconState =
586 iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE
John Spurlockf15ef112015-04-09 22:04:14 -0400587 : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400588 ? Events.ICON_STATE_MUTE
John Spurlockf15ef112015-04-09 22:04:14 -0400589 : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400590 ? Events.ICON_STATE_UNMUTE
591 : Events.ICON_STATE_UNKNOWN;
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400592 if (iconEnabled) {
593 if (isRingStream) {
594 if (isRingVibrate) {
595 row.icon.setContentDescription(mContext.getString(
596 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500597 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400598 } else {
599 if (mController.hasVibrator()) {
600 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500601 mShowA11yStream
602 ? R.string.volume_stream_content_description_vibrate_a11y
603 : R.string.volume_stream_content_description_vibrate,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500604 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400605 } else {
606 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500607 mShowA11yStream
608 ? R.string.volume_stream_content_description_mute_a11y
609 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500610 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400611 }
612 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500613 } else if (isA11yStream) {
614 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400615 } else {
616 if (ss.muted || mAutomute && ss.level == 0) {
617 row.icon.setContentDescription(mContext.getString(
618 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500619 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400620 } else {
621 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500622 mShowA11yStream
623 ? R.string.volume_stream_content_description_mute_a11y
624 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500625 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400626 }
627 }
628 } else {
Julia Reynolds03c548f2016-12-14 15:02:38 -0500629 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400630 }
John Spurlockf88d8082015-03-25 18:09:51 -0400631
Beverlye421abe2017-08-22 14:47:28 -0400632 // ensure tracking is disabled if zenMuted
633 if (zenMuted) {
634 row.tracking = false;
635 }
636
John Spurlockf88d8082015-03-25 18:09:51 -0400637 // update slider
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400638 final boolean enableSlider = !zenMuted;
Julia Reynolds5f84c192016-11-14 13:13:08 -0500639 final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400640 : row.ss.level;
641 updateVolumeRowSliderH(row, enableSlider, vlevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400642 }
643
John Spurlockecc80582015-05-13 16:23:14 -0400644 private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) {
Julia Reynolds66d28942017-11-07 09:43:54 -0500645 if (isActive) {
John Spurlock67a0f852015-06-15 15:35:47 -0400646 row.slider.requestFocus();
John Spurlock67a0f852015-06-15 15:35:47 -0400647 }
John Spurlockecc80582015-05-13 16:23:14 -0400648 final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint
649 : mInactiveSliderTint;
650 if (tint == row.cachedSliderTint) return;
651 row.cachedSliderTint = tint;
652 row.slider.setProgressTintList(tint);
653 row.slider.setThumbTintList(tint);
654 }
655
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400656 private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) {
657 row.slider.setEnabled(enable);
John Spurlockecc80582015-05-13 16:23:14 -0400658 updateVolumeRowSliderTintH(row, row.stream == mActiveStream);
John Spurlockf88d8082015-03-25 18:09:51 -0400659 if (row.tracking) {
660 return; // don't update if user is sliding
661 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400662 final int progress = row.slider.getProgress();
663 final int level = getImpliedLevel(row.slider, progress);
John Spurlockf88d8082015-03-25 18:09:51 -0400664 final boolean rowVisible = row.view.getVisibility() == View.VISIBLE;
665 final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt)
666 < USER_ATTEMPT_GRACE_PERIOD;
667 mHandler.removeMessages(H.RECHECK, row);
668 if (mShowing && rowVisible && inGracePeriod) {
669 if (D.BUG) Log.d(TAG, "inGracePeriod");
670 mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row),
671 row.userAttempt + USER_ATTEMPT_GRACE_PERIOD);
672 return; // don't update if visible and in grace period
673 }
John Spurlockf88d8082015-03-25 18:09:51 -0400674 if (vlevel == level) {
675 if (mShowing && rowVisible) {
676 return; // don't clamp if visible
677 }
678 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400679 final int newProgress = vlevel * 100;
680 if (progress != newProgress) {
681 if (mShowing && rowVisible) {
682 // animate!
683 if (row.anim != null && row.anim.isRunning()
684 && row.animTargetProgress == newProgress) {
685 return; // already animating to the target progress
686 }
687 // start/update animation
688 if (row.anim == null) {
689 row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress);
690 row.anim.setInterpolator(new DecelerateInterpolator());
691 } else {
692 row.anim.cancel();
693 row.anim.setIntValues(progress, newProgress);
694 }
695 row.animTargetProgress = newProgress;
696 row.anim.setDuration(UPDATE_ANIMATION_DURATION);
697 row.anim.start();
698 } else {
699 // update slider directly to clamped value
700 if (row.anim != null) {
701 row.anim.cancel();
702 }
Julia Reynolds88dabde2016-08-09 13:59:44 -0400703 row.slider.setProgress(newProgress, true);
Julia Reynolds4b006a82016-04-20 16:09:06 -0400704 }
705 }
John Spurlockf88d8082015-03-25 18:09:51 -0400706 }
707
708 private void recheckH(VolumeRow row) {
709 if (row == null) {
710 if (D.BUG) Log.d(TAG, "recheckH ALL");
711 trimObsoleteH();
712 for (VolumeRow r : mRows) {
713 updateVolumeRowH(r);
714 }
715 } else {
716 if (D.BUG) Log.d(TAG, "recheckH " + row.stream);
717 updateVolumeRowH(row);
718 }
719 }
720
721 private void setStreamImportantH(int stream, boolean important) {
722 for (VolumeRow row : mRows) {
723 if (row.stream == stream) {
724 row.important = important;
725 return;
726 }
727 }
728 }
729
John Spurlock76b52b32015-04-03 00:00:12 -0400730 private void showSafetyWarningH(int flags) {
731 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
732 || mShowing) {
733 synchronized (mSafetyWarningLock) {
734 if (mSafetyWarning != null) {
735 return;
736 }
737 mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) {
738 @Override
739 protected void cleanUp() {
740 synchronized (mSafetyWarningLock) {
741 mSafetyWarning = null;
742 }
743 recheckH(null);
744 }
745 };
746 mSafetyWarning.show();
747 }
748 recheckH(null);
749 }
750 rescheduleTimeoutH();
751 }
752
Julia Reynolds03c548f2016-12-14 15:02:38 -0500753 private String getStreamLabelH(StreamState ss) {
754 if (ss.remoteLabel != null) {
755 return ss.remoteLabel;
756 }
757 try {
758 return mContext.getString(ss.name);
759 } catch (Resources.NotFoundException e) {
760 Slog.e(TAG, "Can't find translation for stream " + ss);
761 return "";
762 }
763 }
764
John Spurlockf88d8082015-03-25 18:09:51 -0400765 private final VolumeDialogController.Callbacks mControllerCallbackH
766 = new VolumeDialogController.Callbacks() {
767 @Override
768 public void onShowRequested(int reason) {
769 showH(reason);
770 }
771
772 @Override
773 public void onDismissRequested(int reason) {
774 dismissH(reason);
775 }
776
John Spurlock76b52b32015-04-03 00:00:12 -0400777 @Override
John Spurlockf88d8082015-03-25 18:09:51 -0400778 public void onScreenOff() {
779 dismissH(Events.DISMISS_REASON_SCREEN_OFF);
780 }
781
782 @Override
783 public void onStateChanged(State state) {
784 onStateChangedH(state);
785 }
786
787 @Override
788 public void onLayoutDirectionChanged(int layoutDirection) {
789 mDialogView.setLayoutDirection(layoutDirection);
790 }
791
792 @Override
793 public void onConfigurationChanged() {
Julia Reynolds42411922017-11-08 11:19:09 -0500794 mDialog.dismiss();
795 initDialog();
Julia Reynolds03c548f2016-12-14 15:02:38 -0500796 mConfigurableTexts.update();
John Spurlockf88d8082015-03-25 18:09:51 -0400797 }
798
799 @Override
800 public void onShowVibrateHint() {
801 if (mSilentMode) {
802 mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false);
803 }
804 }
805
John Spurlock76b52b32015-04-03 00:00:12 -0400806 @Override
John Spurlockf88d8082015-03-25 18:09:51 -0400807 public void onShowSilentHint() {
808 if (mSilentMode) {
809 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
810 }
811 }
John Spurlock76b52b32015-04-03 00:00:12 -0400812
813 @Override
814 public void onShowSafetyWarning(int flags) {
815 showSafetyWarningH(flags);
816 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500817
818 @Override
819 public void onAccessibilityModeChanged(Boolean showA11yStream) {
820 boolean show = showA11yStream == null ? false : showA11yStream;
821 mShowA11yStream = show;
Julia Reynolds74cc6502017-08-08 12:41:04 -0400822 VolumeRow activeRow = getActiveRow();
823 if (!mShowA11yStream && AudioManager.STREAM_ACCESSIBILITY == activeRow.stream) {
824 dismissH(Events.DISMISS_STREAM_GONE);
825 } else {
826 updateRowsH(activeRow);
827 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500828
829 }
John Spurlockf88d8082015-03-25 18:09:51 -0400830 };
831
John Spurlockf88d8082015-03-25 18:09:51 -0400832 private final class H extends Handler {
833 private static final int SHOW = 1;
834 private static final int DISMISS = 2;
835 private static final int RECHECK = 3;
836 private static final int RECHECK_ALL = 4;
837 private static final int SET_STREAM_IMPORTANT = 5;
838 private static final int RESCHEDULE_TIMEOUT = 6;
John Spurlock22def3d2015-06-17 11:56:12 -0400839 private static final int STATE_CHANGED = 7;
John Spurlockf88d8082015-03-25 18:09:51 -0400840
841 public H() {
842 super(Looper.getMainLooper());
843 }
844
845 @Override
846 public void handleMessage(Message msg) {
847 switch (msg.what) {
848 case SHOW: showH(msg.arg1); break;
849 case DISMISS: dismissH(msg.arg1); break;
850 case RECHECK: recheckH((VolumeRow) msg.obj); break;
851 case RECHECK_ALL: recheckH(null); break;
852 case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break;
853 case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break;
John Spurlock22def3d2015-06-17 11:56:12 -0400854 case STATE_CHANGED: onStateChangedH(mState); break;
John Spurlockf88d8082015-03-25 18:09:51 -0400855 }
856 }
857 }
858
Julia Reynolds42411922017-11-08 11:19:09 -0500859 private final class CustomDialog extends Dialog implements DialogInterface {
John Spurlockf88d8082015-03-25 18:09:51 -0400860 public CustomDialog(Context context) {
Julia Reynolds42411922017-11-08 11:19:09 -0500861 super(context, com.android.systemui.R.style.qs_theme);
John Spurlockf88d8082015-03-25 18:09:51 -0400862 }
863
864 @Override
865 public boolean dispatchTouchEvent(MotionEvent ev) {
866 rescheduleTimeoutH();
867 return super.dispatchTouchEvent(ev);
868 }
869
870 @Override
Julia Reynolds42411922017-11-08 11:19:09 -0500871 protected void onStart() {
872 super.setCanceledOnTouchOutside(true);
873 super.onStart();
John Spurlockf88d8082015-03-25 18:09:51 -0400874 }
875
876 @Override
Julia Reynolds42411922017-11-08 11:19:09 -0500877 protected void onStop() {
878 super.onStop();
879 mHandler.sendEmptyMessage(H.RECHECK_ALL);
John Spurlockf88d8082015-03-25 18:09:51 -0400880 }
Julia Reynolds2feab162016-04-21 14:58:52 -0400881
882 @Override
883 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
884 event.setClassName(getClass().getSuperclass().getName());
885 event.setPackageName(mContext.getPackageName());
886
887 ViewGroup.LayoutParams params = getWindow().getAttributes();
Jason Monk98ad83c2017-06-13 14:03:45 -0400888 boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
889 (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
Julia Reynolds2feab162016-04-21 14:58:52 -0400890 event.setFullScreen(isFullScreen);
891
892 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
893 if (mShowing) {
894 event.getText().add(mContext.getString(
895 R.string.volume_dialog_accessibility_shown_message,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500896 getStreamLabelH(getActiveRow().ss)));
Julia Reynolds2feab162016-04-21 14:58:52 -0400897 return true;
898 }
899 }
900 return false;
901 }
John Spurlockf88d8082015-03-25 18:09:51 -0400902 }
903
904 private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener {
905 private final VolumeRow mRow;
906
907 private VolumeSeekBarChangeListener(VolumeRow row) {
908 mRow = row;
909 }
910
911 @Override
912 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
913 if (mRow.ss == null) return;
914 if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
915 + " onProgressChanged " + progress + " fromUser=" + fromUser);
916 if (!fromUser) return;
917 if (mRow.ss.levelMin > 0) {
Julia Reynolds4b006a82016-04-20 16:09:06 -0400918 final int minProgress = mRow.ss.levelMin * 100;
John Spurlockf88d8082015-03-25 18:09:51 -0400919 if (progress < minProgress) {
920 seekBar.setProgress(minProgress);
Julia Reynolds798b0c32015-09-01 14:35:53 -0400921 progress = minProgress;
John Spurlockf88d8082015-03-25 18:09:51 -0400922 }
923 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400924 final int userLevel = getImpliedLevel(seekBar, progress);
925 if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) {
John Spurlockf88d8082015-03-25 18:09:51 -0400926 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -0400927 if (mRow.requestedLevel != userLevel) {
928 mController.setStreamVolume(mRow.stream, userLevel);
929 mRow.requestedLevel = userLevel;
Chris Wrene565ee62015-06-17 15:24:56 -0400930 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream,
Julia Reynolds4b006a82016-04-20 16:09:06 -0400931 userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400932 }
933 }
934 }
935
936 @Override
937 public void onStartTrackingTouch(SeekBar seekBar) {
938 if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream);
939 mController.setActiveStream(mRow.stream);
940 mRow.tracking = true;
941 }
942
943 @Override
944 public void onStopTrackingTouch(SeekBar seekBar) {
945 if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream);
946 mRow.tracking = false;
947 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -0400948 final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress());
Chris Wrene565ee62015-06-17 15:24:56 -0400949 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400950 if (mRow.ss.level != userLevel) {
951 mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow),
952 USER_ATTEMPT_GRACE_PERIOD);
953 }
954 }
955 }
956
John Spurlockf09838b2015-06-11 16:14:44 -0400957 private final class Accessibility extends AccessibilityDelegate {
John Spurlock5adeabc2015-05-05 14:50:43 -0400958 private boolean mFeedbackEnabled;
959
960 public void init() {
John Spurlock5adeabc2015-05-05 14:50:43 -0400961 mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
962 @Override
963 public void onViewDetachedFromWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -0400964 if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -0400965 }
966
967 @Override
968 public void onViewAttachedToWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -0400969 if (D.BUG) Log.d(TAG, "onViewAttachedToWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -0400970 updateFeedbackEnabled();
971 }
972 });
John Spurlockf09838b2015-06-11 16:14:44 -0400973 mDialogView.setAccessibilityDelegate(this);
Jason Monk3b9357f2017-07-14 09:40:54 -0400974 mAccessibilityMgr.addAccessibilityStateChangeListener(mListener);
John Spurlock5adeabc2015-05-05 14:50:43 -0400975 updateFeedbackEnabled();
976 }
977
Jason Monk3b9357f2017-07-14 09:40:54 -0400978 public void destroy() {
979 mAccessibilityMgr.removeAccessibilityStateChangeListener(mListener);
980 }
981
John Spurlockf09838b2015-06-11 16:14:44 -0400982 @Override
983 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
984 AccessibilityEvent event) {
985 rescheduleTimeoutH();
986 return super.onRequestSendAccessibilityEvent(host, child, event);
987 }
988
John Spurlock5adeabc2015-05-05 14:50:43 -0400989 private void updateFeedbackEnabled() {
990 mFeedbackEnabled = computeFeedbackEnabled();
991 }
992
993 private boolean computeFeedbackEnabled() {
John Spurlockf09838b2015-06-11 16:14:44 -0400994 // are there any enabled non-generic a11y services?
John Spurlock5adeabc2015-05-05 14:50:43 -0400995 final List<AccessibilityServiceInfo> services =
Julia Reynolds2feab162016-04-21 14:58:52 -0400996 mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK);
John Spurlock5adeabc2015-05-05 14:50:43 -0400997 for (AccessibilityServiceInfo asi : services) {
John Spurlockf09838b2015-06-11 16:14:44 -0400998 if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) {
John Spurlock5adeabc2015-05-05 14:50:43 -0400999 return true;
1000 }
1001 }
1002 return false;
1003 }
Jason Monk3b9357f2017-07-14 09:40:54 -04001004
1005 private final AccessibilityStateChangeListener mListener =
1006 enabled -> updateFeedbackEnabled();
John Spurlock5adeabc2015-05-05 14:50:43 -04001007 }
1008
John Spurlockf88d8082015-03-25 18:09:51 -04001009 private static class VolumeRow {
1010 private View view;
John Spurlockf88d8082015-03-25 18:09:51 -04001011 private TextView header;
1012 private ImageButton icon;
1013 private SeekBar slider;
John Spurlockf88d8082015-03-25 18:09:51 -04001014 private int stream;
1015 private StreamState ss;
1016 private long userAttempt; // last user-driven slider change
1017 private boolean tracking; // tracking slider touch
John Spurlockb0a2e5f2015-06-09 14:41:52 -04001018 private int requestedLevel = -1; // pending user-requested level via progress changed
John Spurlockf88d8082015-03-25 18:09:51 -04001019 private int iconRes;
1020 private int iconMuteRes;
1021 private boolean important;
John Spurlockecc80582015-05-13 16:23:14 -04001022 private ColorStateList cachedSliderTint;
John Spurlockf88d8082015-03-25 18:09:51 -04001023 private int iconState; // from Events
Julia Reynolds4b006a82016-04-20 16:09:06 -04001024 private ObjectAnimator anim; // slider progress animation for non-touch-related updates
1025 private int animTargetProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001026 private int lastAudibleLevel = 1;
1027 }
John Spurlockf88d8082015-03-25 18:09:51 -04001028}