blob: 7f5c5958564936db90fe5c6da52e2a0a54bc876b [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
John Spurlock5adeabc2015-05-05 14:50:43 -040022import android.accessibilityservice.AccessibilityServiceInfo;
Julia Reynolds4b006a82016-04-20 16:09:06 -040023import android.animation.ObjectAnimator;
Julia Reynolds2feab162016-04-21 14:58:52 -040024import android.annotation.NonNull;
John Spurlockf88d8082015-03-25 18:09:51 -040025import android.annotation.SuppressLint;
26import android.app.Dialog;
27import android.app.KeyguardManager;
28import android.content.Context;
Takayuki Hoshibb0ec552015-12-17 20:50:25 +090029import android.content.pm.PackageManager;
John Spurlock77bb25af2015-05-11 12:12:07 -040030import android.content.res.ColorStateList;
Julia Reynoldsc791e052016-02-19 16:02:01 -050031import android.content.res.Configuration;
John Spurlockf88d8082015-03-25 18:09:51 -040032import android.content.res.Resources;
Jason Monk98ad83c2017-06-13 14:03:45 -040033import android.graphics.Color;
John Spurlockf88d8082015-03-25 18:09:51 -040034import android.graphics.PixelFormat;
35import android.graphics.Rect;
36import android.graphics.drawable.AnimatedVectorDrawable;
Jason Monk98ad83c2017-06-13 14:03:45 -040037import android.graphics.drawable.ColorDrawable;
John Spurlockf88d8082015-03-25 18:09:51 -040038import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.media.AudioSystem;
John Spurlock5adeabc2015-05-05 14:50:43 -040041import android.os.Debug;
John Spurlockf88d8082015-03-25 18:09:51 -040042import android.os.Handler;
43import android.os.Looper;
44import android.os.Message;
45import android.os.SystemClock;
46import android.provider.Settings.Global;
Julia Reynolds568585b2016-07-25 08:55:15 -040047import android.transition.AutoTransition;
48import android.transition.Transition;
49import android.transition.TransitionManager;
Jason Monk98ad83c2017-06-13 14:03:45 -040050import android.util.DisplayMetrics;
John Spurlockf88d8082015-03-25 18:09:51 -040051import android.util.Log;
Julia Reynolds03c548f2016-12-14 15:02:38 -050052import android.util.Slog;
John Spurlockf88d8082015-03-25 18:09:51 -040053import android.util.SparseBooleanArray;
Lucas Dupin448786c2017-07-24 17:44:25 -070054import android.view.ContextThemeWrapper;
Jason Monk98ad83c2017-06-13 14:03:45 -040055import android.view.Gravity;
John Spurlockf88d8082015-03-25 18:09:51 -040056import android.view.MotionEvent;
57import android.view.View;
John Spurlock5adeabc2015-05-05 14:50:43 -040058import android.view.View.AccessibilityDelegate;
59import android.view.View.OnAttachStateChangeListener;
John Spurlockf88d8082015-03-25 18:09:51 -040060import android.view.View.OnClickListener;
John Spurlockf88d8082015-03-25 18:09:51 -040061import android.view.View.OnTouchListener;
62import android.view.ViewGroup;
63import android.view.ViewGroup.MarginLayoutParams;
64import android.view.Window;
65import android.view.WindowManager;
John Spurlock5adeabc2015-05-05 14:50:43 -040066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityManager;
68import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
Julia Reynolds4b006a82016-04-20 16:09:06 -040069import android.view.animation.DecelerateInterpolator;
John Spurlockf88d8082015-03-25 18:09:51 -040070import android.widget.ImageButton;
John Spurlockf88d8082015-03-25 18:09:51 -040071import android.widget.SeekBar;
72import android.widget.SeekBar.OnSeekBarChangeListener;
73import android.widget.TextView;
Lucas Dupin7224c1e2017-07-06 14:35:30 -070074
Andrew Sapperstein5c373442016-06-12 13:17:16 -070075import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050076import com.android.systemui.Dependency;
Julia Reynolds568585b2016-07-25 08:55:15 -040077import com.android.systemui.Interpolators;
John Spurlockf88d8082015-03-25 18:09:51 -040078import com.android.systemui.R;
Rajeev Kumar5c964a42017-06-13 19:41:35 -070079import com.android.systemui.plugins.VolumeDialog;
Jason Monk782cd672017-03-22 12:50:57 -040080import com.android.systemui.plugins.VolumeDialogController;
81import com.android.systemui.plugins.VolumeDialogController.State;
82import com.android.systemui.plugins.VolumeDialogController.StreamState;
John Spurlockf88d8082015-03-25 18:09:51 -040083import com.android.systemui.statusbar.policy.ZenModeController;
Jason Monke138f552016-01-18 09:21:45 -050084import com.android.systemui.tuner.TunerService;
Jason Monkaa911b32016-02-10 21:01:30 -050085import com.android.systemui.tuner.TunerZenModePanel;
Lucas Dupin7224c1e2017-07-06 14:35:30 -070086
John Spurlockf88d8082015-03-25 18:09:51 -040087import java.io.PrintWriter;
88import java.util.ArrayList;
89import java.util.List;
90
91/**
92 * Visual presentation of the volume dialog.
93 *
Jason Monk782cd672017-03-22 12:50:57 -040094 * A client of VolumeDialogControllerImpl and its state model.
John Spurlockf88d8082015-03-25 18:09:51 -040095 *
96 * Methods ending in "H" must be called on the (ui) handler.
97 */
Jason Monk98ad83c2017-06-13 14:03:45 -040098public class VolumeDialogImpl implements VolumeDialog, TunerService.Tunable {
Jason Monk782cd672017-03-22 12:50:57 -040099 private static final String TAG = Util.logTag(VolumeDialogImpl.class);
John Spurlockf88d8082015-03-25 18:09:51 -0400100
Jason Monke138f552016-01-18 09:21:45 -0500101 public static final String SHOW_FULL_ZEN = "sysui_show_full_zen";
102
John Spurlockf88d8082015-03-25 18:09:51 -0400103 private static final long USER_ATTEMPT_GRACE_PERIOD = 1000;
Julia Reynolds4b006a82016-04-20 16:09:06 -0400104 private static final int UPDATE_ANIMATION_DURATION = 80;
John Spurlockf88d8082015-03-25 18:09:51 -0400105
106 private final Context mContext;
107 private final H mHandler = new H();
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700108 private final VolumeDialogController mController;
John Spurlockf88d8082015-03-25 18:09:51 -0400109
Julia Reynolds568585b2016-07-25 08:55:15 -0400110 private Window mWindow;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500111 private CustomDialog mDialog;
112 private ViewGroup mDialogView;
Julia Reynolds568585b2016-07-25 08:55:15 -0400113 private ViewGroup mDialogRowsView;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500114 private ViewGroup mDialogContentView;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500115 private ImageButton mExpandButton;
116 private final List<VolumeRow> mRows = new ArrayList<>();
Julia Reynolds03c548f2016-12-14 15:02:38 -0500117 private ConfigurableTexts mConfigurableTexts;
John Spurlockf88d8082015-03-25 18:09:51 -0400118 private final SparseBooleanArray mDynamic = new SparseBooleanArray();
119 private final KeyguardManager mKeyguard;
Julia Reynolds6aa83b42015-09-08 13:41:45 -0400120 private final AudioManager mAudioManager;
Julia Reynolds2feab162016-04-21 14:58:52 -0400121 private final AccessibilityManager mAccessibilityMgr;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500122 private int mExpandButtonAnimationDuration;
123 private ZenFooter mZenFooter;
John Spurlock76b52b32015-04-03 00:00:12 -0400124 private final Object mSafetyWarningLock = new Object();
John Spurlock5adeabc2015-05-05 14:50:43 -0400125 private final Accessibility mAccessibility = new Accessibility();
John Spurlock77bb25af2015-05-11 12:12:07 -0400126 private final ColorStateList mActiveSliderTint;
127 private final ColorStateList mInactiveSliderTint;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500128 private VolumeDialogMotion mMotion;
Jason Monk782cd672017-03-22 12:50:57 -0400129 private int mWindowType;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500130 private final ZenModeController mZenModeController;
John Spurlockf88d8082015-03-25 18:09:51 -0400131
132 private boolean mShowing;
133 private boolean mExpanded;
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500134 private boolean mShowA11yStream;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500135
John Spurlockf88d8082015-03-25 18:09:51 -0400136 private int mActiveStream;
John Spurlockf55b7f22015-04-13 19:21:26 -0400137 private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE;
138 private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE;
John Spurlockf88d8082015-03-25 18:09:51 -0400139 private State mState;
John Spurlock22def3d2015-06-17 11:56:12 -0400140 private boolean mExpandButtonAnimationRunning;
John Spurlock76b52b32015-04-03 00:00:12 -0400141 private SafetyWarningDialog mSafetyWarning;
John Spurlockf55b7f22015-04-13 19:21:26 -0400142 private Callback mCallback;
John Spurlock22def3d2015-06-17 11:56:12 -0400143 private boolean mPendingStateChanged;
144 private boolean mPendingRecheckAll;
145 private long mCollapseTime;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700146 private boolean mHovering = false;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500147 private int mDensity;
John Spurlockf88d8082015-03-25 18:09:51 -0400148
Jason Monke138f552016-01-18 09:21:45 -0500149 private boolean mShowFullZen;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500150 private TunerZenModePanel mZenPanel;
Jason Monke138f552016-01-18 09:21:45 -0500151
Jason Monk782cd672017-03-22 12:50:57 -0400152 public VolumeDialogImpl(Context context) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700153 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk782cd672017-03-22 12:50:57 -0400154 mZenModeController = Dependency.get(ZenModeController.class);
155 mController = Dependency.get(VolumeDialogController.class);
156 mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
157 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
Julia Reynolds568585b2016-07-25 08:55:15 -0400158 mAccessibilityMgr =
159 (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700160 mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext));
Julia Reynoldsc791e052016-02-19 16:02:01 -0500161 mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive);
Jason Monk782cd672017-03-22 12:50:57 -0400162 }
163
164 public void init(int windowType, Callback callback) {
165 mCallback = callback;
166 mWindowType = windowType;
John Spurlockf88d8082015-03-25 18:09:51 -0400167
Julia Reynoldsc791e052016-02-19 16:02:01 -0500168 initDialog();
169
170 mAccessibility.init();
171
Jason Monk782cd672017-03-22 12:50:57 -0400172 mController.addCallback(mControllerCallbackH, mHandler);
173 mController.getState();
Jason Monkde850bb2017-02-01 19:26:30 -0500174 Dependency.get(TunerService.class).addTunable(this, SHOW_FULL_ZEN);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500175
176 final Configuration currentConfig = mContext.getResources().getConfiguration();
177 mDensity = currentConfig.densityDpi;
178 }
179
Jason Monk782cd672017-03-22 12:50:57 -0400180 @Override
181 public void destroy() {
Jason Monk3b9357f2017-07-14 09:40:54 -0400182 mAccessibility.destroy();
Jason Monk782cd672017-03-22 12:50:57 -0400183 mController.removeCallback(mControllerCallbackH);
Jason Monk3b9357f2017-07-14 09:40:54 -0400184 if (mZenFooter != null) {
185 mZenFooter.cleanup();
186 }
187 Dependency.get(TunerService.class).removeTunable(this);
188 mHandler.removeCallbacksAndMessages(null);
Jason Monk782cd672017-03-22 12:50:57 -0400189 }
190
Julia Reynoldsc791e052016-02-19 16:02:01 -0500191 private void initDialog() {
John Spurlockf88d8082015-03-25 18:09:51 -0400192 mDialog = new CustomDialog(mContext);
193
Julia Reynolds03c548f2016-12-14 15:02:38 -0500194 mConfigurableTexts = new ConfigurableTexts(mContext);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500195 mHovering = false;
196 mShowing = false;
Julia Reynolds568585b2016-07-25 08:55:15 -0400197 mWindow = mDialog.getWindow();
198 mWindow.requestFeature(Window.FEATURE_NO_TITLE);
Jason Monk98ad83c2017-06-13 14:03:45 -0400199 mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Julia Reynolds568585b2016-07-25 08:55:15 -0400200 mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
201 mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
John Spurlockf88d8082015-03-25 18:09:51 -0400202 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
203 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
204 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Jason Monk98ad83c2017-06-13 14:03:45 -0400205 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
206 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockf88d8082015-03-25 18:09:51 -0400207 mDialog.setCanceledOnTouchOutside(true);
208 final Resources res = mContext.getResources();
Jason Monk98ad83c2017-06-13 14:03:45 -0400209 final WindowManager.LayoutParams lp = mWindow.getAttributes();
210 lp.type = mWindowType;
211 lp.format = PixelFormat.TRANSLUCENT;
212 lp.setTitle(VolumeDialogImpl.class.getSimpleName());
213 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
214 lp.y = res.getDimensionPixelSize(R.dimen.volume_offset_top);
215 lp.gravity = Gravity.TOP;
216 lp.windowAnimations = -1;
217 mWindow.setAttributes(lp);
Julia Reynolds568585b2016-07-25 08:55:15 -0400218 mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500219
Jason Monk98ad83c2017-06-13 14:03:45 -0400220 mDialog.setContentView(R.layout.volume_dialog);
221 mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog);
222 mDialogView.setOnHoverListener(new View.OnHoverListener() {
223 @Override
224 public boolean onHover(View v, MotionEvent event) {
225 int action = event.getActionMasked();
226 mHovering = (action == MotionEvent.ACTION_HOVER_ENTER)
227 || (action == MotionEvent.ACTION_HOVER_MOVE);
228 rescheduleTimeoutH();
229 return true;
230 }
Jun Mukai3f5d7132015-07-21 14:10:09 -0700231 });
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700232
Jason Monk16fbd9d2017-04-27 14:28:49 -0400233 mDialogContentView = mDialog.findViewById(R.id.volume_dialog_content);
234 mDialogRowsView = mDialogContentView.findViewById(R.id.volume_dialog_rows);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500235 mExpanded = false;
Jason Monk98ad83c2017-06-13 14:03:45 -0400236 mExpandButton = (ImageButton) mDialogView.findViewById(R.id.volume_expand_button);
John Spurlockf88d8082015-03-25 18:09:51 -0400237 mExpandButton.setOnClickListener(mClickExpand);
Muyuan Li36007d22016-06-21 14:21:48 -0700238
239 mExpandButton.setVisibility(
240 AudioSystem.isSingleVolume(mContext) ? View.GONE : View.VISIBLE);
Jason Monk98ad83c2017-06-13 14:03:45 -0400241 updateWindowWidthH();
John Spurlockf88d8082015-03-25 18:09:51 -0400242 updateExpandButtonH();
Julia Reynoldsc791e052016-02-19 16:02:01 -0500243
Jason Monk98ad83c2017-06-13 14:03:45 -0400244 mMotion = new VolumeDialogMotion(mDialog, mDialogView, mDialogContentView, mExpandButton,
245 new VolumeDialogMotion.Callback() {
246 @Override
247 public void onAnimatingChanged(boolean animating) {
248 if (animating) return;
249 if (mPendingStateChanged) {
250 mHandler.sendEmptyMessage(H.STATE_CHANGED);
251 mPendingStateChanged = false;
252 }
253 if (mPendingRecheckAll) {
254 mHandler.sendEmptyMessage(H.RECHECK_ALL);
255 mPendingRecheckAll = false;
256 }
Julia Reynoldsc791e052016-02-19 16:02:01 -0500257 }
258 });
John Spurlockf88d8082015-03-25 18:09:51 -0400259
Julia Reynoldsc791e052016-02-19 16:02:01 -0500260 if (mRows.isEmpty()) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500261 addRow(AudioManager.STREAM_MUSIC,
262 R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700263 if (!AudioSystem.isSingleVolume(mContext)) {
264 addRow(AudioManager.STREAM_RING,
265 R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700266 addRow(AudioManager.STREAM_ALARM,
267 R.drawable.ic_volume_alarm, R.drawable.ic_volume_alarm_mute, false);
268 addRow(AudioManager.STREAM_VOICE_CALL,
269 R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, false);
270 addRow(AudioManager.STREAM_BLUETOOTH_SCO,
271 R.drawable.ic_volume_bt_sco, R.drawable.ic_volume_bt_sco, false);
272 addRow(AudioManager.STREAM_SYSTEM,
273 R.drawable.ic_volume_system, R.drawable.ic_volume_system_mute, false);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500274 addRow(AudioManager.STREAM_ACCESSIBILITY, R.drawable.ic_volume_accessibility,
275 R.drawable.ic_volume_accessibility, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700276 }
Julia Reynoldsc791e052016-02-19 16:02:01 -0500277 } else {
278 addExistingRows();
279 }
John Spurlockf88d8082015-03-25 18:09:51 -0400280 mExpandButtonAnimationDuration = res.getInteger(R.integer.volume_expand_animation_duration);
Jason Monk98ad83c2017-06-13 14:03:45 -0400281 mZenFooter = (ZenFooter) mDialog.findViewById(R.id.volume_zen_footer);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500282 mZenFooter.init(mZenModeController);
Jason Monk98ad83c2017-06-13 14:03:45 -0400283 mZenPanel = (TunerZenModePanel) mDialog.findViewById(R.id.tuner_zen_mode_panel);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500284 mZenPanel.init(mZenModeController);
Jason Monke138f552016-01-18 09:21:45 -0500285 mZenPanel.setCallback(mZenPanelCallback);
Jason Monke138f552016-01-18 09:21:45 -0500286 }
287
288 @Override
289 public void onTuningChanged(String key, String newValue) {
290 if (SHOW_FULL_ZEN.equals(key)) {
291 mShowFullZen = newValue != null && Integer.parseInt(newValue) != 0;
292 }
John Spurlockf88d8082015-03-25 18:09:51 -0400293 }
294
John Spurlock77bb25af2015-05-11 12:12:07 -0400295 private ColorStateList loadColorStateList(int colorResId) {
296 return ColorStateList.valueOf(mContext.getColor(colorResId));
297 }
298
Jason Monk98ad83c2017-06-13 14:03:45 -0400299 private void updateWindowWidthH() {
300 final ViewGroup.LayoutParams lp = mDialogView.getLayoutParams();
301 final DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
302 if (D.BUG) Log.d(TAG, "updateWindowWidth dm.w=" + dm.widthPixels);
303 int w = dm.widthPixels;
304 final int max = mContext.getResources()
305 .getDimensionPixelSize(R.dimen.volume_dialog_panel_width);
306 if (w > max) {
307 w = max;
308 }
309 lp.width = w;
310 mDialogView.setLayoutParams(lp);
311 }
312
John Spurlockf88d8082015-03-25 18:09:51 -0400313 public void setStreamImportant(int stream, boolean important) {
314 mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget();
315 }
316
John Spurlockf88d8082015-03-25 18:09:51 -0400317 public void setAutomute(boolean automute) {
318 if (mAutomute == automute) return;
319 mAutomute = automute;
320 mHandler.sendEmptyMessage(H.RECHECK_ALL);
321 }
322
323 public void setSilentMode(boolean silentMode) {
324 if (mSilentMode == silentMode) return;
325 mSilentMode = silentMode;
326 mHandler.sendEmptyMessage(H.RECHECK_ALL);
327 }
328
329 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500330 addRow(stream, iconRes, iconMuteRes, important, false);
331 }
332
333 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important,
334 boolean dynamic) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500335 VolumeRow row = new VolumeRow();
336 initRow(row, stream, iconRes, iconMuteRes, important);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500337 int rowSize;
338 int viewSize;
339 if (mShowA11yStream && dynamic && (rowSize = mRows.size()) > 1
340 && (viewSize = mDialogRowsView.getChildCount()) > 1) {
341 // A11y Stream should be the last in the list
342 mDialogRowsView.addView(row.view, viewSize - 2);
343 mRows.add(rowSize - 2, row);
344 } else {
345 mDialogRowsView.addView(row.view);
346 mRows.add(row);
347 }
John Spurlockf88d8082015-03-25 18:09:51 -0400348 }
349
Julia Reynoldsc791e052016-02-19 16:02:01 -0500350 private void addExistingRows() {
351 int N = mRows.size();
352 for (int i = 0; i < N; i++) {
353 final VolumeRow row = mRows.get(i);
354 initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important);
Julia Reynolds568585b2016-07-25 08:55:15 -0400355 mDialogRowsView.addView(row.view);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500356 }
357 }
358
Julia Reynoldsc791e052016-02-19 16:02:01 -0500359
John Spurlockf88d8082015-03-25 18:09:51 -0400360 private boolean isAttached() {
361 return mDialogContentView != null && mDialogContentView.isAttachedToWindow();
362 }
363
364 private VolumeRow getActiveRow() {
365 for (VolumeRow row : mRows) {
366 if (row.stream == mActiveStream) {
367 return row;
368 }
369 }
370 return mRows.get(0);
371 }
372
373 private VolumeRow findRow(int stream) {
374 for (VolumeRow row : mRows) {
375 if (row.stream == stream) return row;
376 }
377 return null;
378 }
379
John Spurlockf88d8082015-03-25 18:09:51 -0400380 public void dump(PrintWriter writer) {
Jason Monk782cd672017-03-22 12:50:57 -0400381 writer.println(VolumeDialogImpl.class.getSimpleName() + " state:");
John Spurlockf88d8082015-03-25 18:09:51 -0400382 writer.print(" mShowing: "); writer.println(mShowing);
383 writer.print(" mExpanded: "); writer.println(mExpanded);
John Spurlock22def3d2015-06-17 11:56:12 -0400384 writer.print(" mExpandButtonAnimationRunning: ");
385 writer.println(mExpandButtonAnimationRunning);
John Spurlockf88d8082015-03-25 18:09:51 -0400386 writer.print(" mActiveStream: "); writer.println(mActiveStream);
387 writer.print(" mDynamic: "); writer.println(mDynamic);
John Spurlockf88d8082015-03-25 18:09:51 -0400388 writer.print(" mAutomute: "); writer.println(mAutomute);
389 writer.print(" mSilentMode: "); writer.println(mSilentMode);
John Spurlock22def3d2015-06-17 11:56:12 -0400390 writer.print(" mCollapseTime: "); writer.println(mCollapseTime);
John Spurlock84ee67c2015-05-20 23:33:43 -0400391 writer.print(" mAccessibility.mFeedbackEnabled: ");
392 writer.println(mAccessibility.mFeedbackEnabled);
John Spurlockf88d8082015-03-25 18:09:51 -0400393 }
394
Julia Reynolds4b006a82016-04-20 16:09:06 -0400395 private static int getImpliedLevel(SeekBar seekBar, int progress) {
396 final int m = seekBar.getMax();
397 final int n = m / 100 - 1;
398 final int level = progress == 0 ? 0
399 : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n));
400 return level;
401 }
402
John Spurlockf88d8082015-03-25 18:09:51 -0400403 @SuppressLint("InflateParams")
Julia Reynoldsc791e052016-02-19 16:02:01 -0500404 private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes,
405 boolean important) {
John Spurlockf88d8082015-03-25 18:09:51 -0400406 row.stream = stream;
407 row.iconRes = iconRes;
408 row.iconMuteRes = iconMuteRes;
409 row.important = important;
410 row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
Julia Reynolds568585b2016-07-25 08:55:15 -0400411 row.view.setId(row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400412 row.view.setTag(row);
413 row.header = (TextView) row.view.findViewById(R.id.volume_row_header);
Julia Reynolds568585b2016-07-25 08:55:15 -0400414 row.header.setId(20 * row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400415 row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider);
416 row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
Julia Reynolds5774cf02016-05-26 13:43:38 -0400417 row.anim = null;
John Spurlockf88d8082015-03-25 18:09:51 -0400418
419 // forward events above the slider into the slider
420 row.view.setOnTouchListener(new OnTouchListener() {
421 private final Rect mSliderHitRect = new Rect();
422 private boolean mDragging;
423
424 @SuppressLint("ClickableViewAccessibility")
425 @Override
426 public boolean onTouch(View v, MotionEvent event) {
427 row.slider.getHitRect(mSliderHitRect);
428 if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN
429 && event.getY() < mSliderHitRect.top) {
430 mDragging = true;
431 }
432 if (mDragging) {
433 event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top);
434 row.slider.dispatchTouchEvent(event);
435 if (event.getActionMasked() == MotionEvent.ACTION_UP
436 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
437 mDragging = false;
438 }
439 return true;
440 }
441 return false;
442 }
443 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400444 row.icon = row.view.findViewById(R.id.volume_row_icon);
John Spurlockf88d8082015-03-25 18:09:51 -0400445 row.icon.setImageResource(iconRes);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500446 if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) {
447 row.icon.setOnClickListener(new OnClickListener() {
448 @Override
449 public void onClick(View v) {
450 Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState);
451 mController.setActiveStream(row.stream);
452 if (row.stream == AudioManager.STREAM_RING) {
453 final boolean hasVibrator = mController.hasVibrator();
454 if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {
455 if (hasVibrator) {
456 mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);
457 } else {
458 final boolean wasZero = row.ss.level == 0;
459 mController.setStreamVolume(stream,
460 wasZero ? row.lastAudibleLevel : 0);
461 }
John Spurlockf88d8082015-03-25 18:09:51 -0400462 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500463 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
464 if (row.ss.level == 0) {
465 mController.setStreamVolume(stream, 1);
466 }
John Spurlockf88d8082015-03-25 18:09:51 -0400467 }
468 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500469 final boolean vmute = row.ss.level == row.ss.levelMin;
470 mController.setStreamVolume(stream,
471 vmute ? row.lastAudibleLevel : row.ss.levelMin);
John Spurlockf88d8082015-03-25 18:09:51 -0400472 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500473 row.userAttempt = 0; // reset the grace period, slider updates immediately
John Spurlockf88d8082015-03-25 18:09:51 -0400474 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500475 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400476 } else {
477 row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500478 }
John Spurlockf88d8082015-03-25 18:09:51 -0400479 }
480
John Spurlockf88d8082015-03-25 18:09:51 -0400481 public void show(int reason) {
482 mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget();
483 }
484
485 public void dismiss(int reason) {
486 mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget();
487 }
488
John Spurlockf88d8082015-03-25 18:09:51 -0400489 private void showH(int reason) {
John Spurlock22def3d2015-06-17 11:56:12 -0400490 if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]);
John Spurlockf88d8082015-03-25 18:09:51 -0400491 mHandler.removeMessages(H.SHOW);
492 mHandler.removeMessages(H.DISMISS);
493 rescheduleTimeoutH();
494 if (mShowing) return;
495 mShowing = true;
John Spurlock22def3d2015-06-17 11:56:12 -0400496 mMotion.startShow();
Chris Wrene565ee62015-06-17 15:24:56 -0400497 Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
John Spurlockf88d8082015-03-25 18:09:51 -0400498 mController.notifyVisible(true);
499 }
500
501 protected void rescheduleTimeoutH() {
502 mHandler.removeMessages(H.DISMISS);
John Spurlockf09838b2015-06-11 16:14:44 -0400503 final int timeout = computeTimeoutH();
504 mHandler.sendMessageDelayed(mHandler
505 .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout);
John Spurlock5adeabc2015-05-05 14:50:43 -0400506 if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller());
John Spurlockf88d8082015-03-25 18:09:51 -0400507 mController.userActivity();
508 }
509
510 private int computeTimeoutH() {
John Spurlockf09838b2015-06-11 16:14:44 -0400511 if (mAccessibility.mFeedbackEnabled) return 20000;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700512 if (mHovering) return 16000;
John Spurlock76b52b32015-04-03 00:00:12 -0400513 if (mSafetyWarning != null) return 5000;
John Spurlock22def3d2015-06-17 11:56:12 -0400514 if (mExpanded || mExpandButtonAnimationRunning) return 5000;
John Spurlockf88d8082015-03-25 18:09:51 -0400515 if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500;
516 return 3000;
517 }
518
519 protected void dismissH(int reason) {
Jason Monk305e0ba2015-07-06 15:53:59 -0400520 if (mMotion.isAnimating()) {
521 return;
522 }
John Spurlockf88d8082015-03-25 18:09:51 -0400523 mHandler.removeMessages(H.DISMISS);
524 mHandler.removeMessages(H.SHOW);
525 if (!mShowing) return;
526 mShowing = false;
John Spurlock22def3d2015-06-17 11:56:12 -0400527 mMotion.startDismiss(new Runnable() {
528 @Override
529 public void run() {
Julia Reynolds48242f02016-08-10 11:42:56 -0400530 updateExpandedH(false /* expanding */, true /* dismissing */);
John Spurlock22def3d2015-06-17 11:56:12 -0400531 }
532 });
Julia Reynolds2feab162016-04-21 14:58:52 -0400533 if (mAccessibilityMgr.isEnabled()) {
534 AccessibilityEvent event =
535 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
536 event.setPackageName(mContext.getPackageName());
537 event.setClassName(CustomDialog.class.getSuperclass().getName());
538 event.getText().add(mContext.getString(
539 R.string.volume_dialog_accessibility_dismissed_message));
540 mAccessibilityMgr.sendAccessibilityEvent(event);
541 }
Chris Wrene565ee62015-06-17 15:24:56 -0400542 Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason);
John Spurlockf88d8082015-03-25 18:09:51 -0400543 mController.notifyVisible(false);
John Spurlock76b52b32015-04-03 00:00:12 -0400544 synchronized (mSafetyWarningLock) {
545 if (mSafetyWarning != null) {
546 if (D.BUG) Log.d(TAG, "SafetyWarning dismissed");
547 mSafetyWarning.dismiss();
548 }
549 }
John Spurlockf88d8082015-03-25 18:09:51 -0400550 }
551
John Spurlock22def3d2015-06-17 11:56:12 -0400552 private void updateDialogBottomMarginH() {
Jason Monk98ad83c2017-06-13 14:03:45 -0400553 final long diff = System.currentTimeMillis() - mCollapseTime;
554 final boolean collapsing = mCollapseTime != 0 && diff < getConservativeCollapseDuration();
John Spurlock22def3d2015-06-17 11:56:12 -0400555 final ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) mDialogView.getLayoutParams();
Jason Monk98ad83c2017-06-13 14:03:45 -0400556 final int bottomMargin = collapsing ? mDialogContentView.getHeight() :
John Spurlock22def3d2015-06-17 11:56:12 -0400557 mContext.getResources().getDimensionPixelSize(R.dimen.volume_dialog_margin_bottom);
558 if (bottomMargin != mlp.bottomMargin) {
559 if (D.BUG) Log.d(TAG, "bottomMargin " + mlp.bottomMargin + " -> " + bottomMargin);
560 mlp.bottomMargin = bottomMargin;
561 mDialogView.setLayoutParams(mlp);
562 }
563 }
564
565 private long getConservativeCollapseDuration() {
566 return mExpandButtonAnimationDuration * 3;
567 }
568
569 private void prepareForCollapse() {
570 mHandler.removeMessages(H.UPDATE_BOTTOM_MARGIN);
571 mCollapseTime = System.currentTimeMillis();
572 updateDialogBottomMarginH();
573 mHandler.sendEmptyMessageDelayed(H.UPDATE_BOTTOM_MARGIN, getConservativeCollapseDuration());
574 }
575
Julia Reynolds48242f02016-08-10 11:42:56 -0400576 private void updateExpandedH(final boolean expanded, final boolean dismissing) {
John Spurlockf88d8082015-03-25 18:09:51 -0400577 if (mExpanded == expanded) return;
578 mExpanded = expanded;
John Spurlock22def3d2015-06-17 11:56:12 -0400579 mExpandButtonAnimationRunning = isAttached();
Julia Reynolds568585b2016-07-25 08:55:15 -0400580 if (D.BUG) Log.d(TAG, "updateExpandedH " + expanded);
581 updateExpandButtonH();
582 updateFooterH();
Julia Reynolds568585b2016-07-25 08:55:15 -0400583 TransitionManager.endTransitions(mDialogView);
Julia Reynolds48242f02016-08-10 11:42:56 -0400584 final VolumeRow activeRow = getActiveRow();
585 if (!dismissing) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400586 mWindow.setLayout(mWindow.getAttributes().width, ViewGroup.LayoutParams.MATCH_PARENT);
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700587 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
Julia Reynolds48242f02016-08-10 11:42:56 -0400588 }
Julia Reynolds568585b2016-07-25 08:55:15 -0400589 updateRowsH(activeRow);
590 rescheduleTimeoutH();
591 }
592
593 private void updateExpandButtonH() {
594 if (D.BUG) Log.d(TAG, "updateExpandButtonH");
595 mExpandButton.setClickable(!mExpandButtonAnimationRunning);
596 if (!(mExpandButtonAnimationRunning && isAttached())) {
597 final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
598 : R.drawable.ic_volume_expand_animation;
599 if (hasTouchFeature()) {
600 mExpandButton.setImageResource(res);
601 } else {
602 // if there is no touch feature, show the volume ringer instead
603 mExpandButton.setImageResource(R.drawable.ic_volume_ringer);
604 mExpandButton.setBackgroundResource(0); // remove gray background emphasis
605 }
606 mExpandButton.setContentDescription(mContext.getString(mExpanded ?
607 R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand));
John Spurlock22def3d2015-06-17 11:56:12 -0400608 }
John Spurlock22def3d2015-06-17 11:56:12 -0400609 if (mExpandButtonAnimationRunning) {
John Spurlockf88d8082015-03-25 18:09:51 -0400610 final Drawable d = mExpandButton.getDrawable();
611 if (d instanceof AnimatedVectorDrawable) {
612 // workaround to reset drawable
613 final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) d.getConstantState()
614 .newDrawable();
615 mExpandButton.setImageDrawable(avd);
616 avd.start();
617 mHandler.postDelayed(new Runnable() {
618 @Override
619 public void run() {
John Spurlock22def3d2015-06-17 11:56:12 -0400620 mExpandButtonAnimationRunning = false;
John Spurlockf88d8082015-03-25 18:09:51 -0400621 updateExpandButtonH();
622 rescheduleTimeoutH();
623 }
624 }, mExpandButtonAnimationDuration);
625 }
626 }
John Spurlockf88d8082015-03-25 18:09:51 -0400627 }
628
Julia Reynolds568585b2016-07-25 08:55:15 -0400629 private boolean shouldBeVisibleH(VolumeRow row, boolean isActive) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500630 if (row.stream == AudioSystem.STREAM_ACCESSIBILITY) {
631 return mShowA11yStream;
632 }
John Spurlockf88d8082015-03-25 18:09:51 -0400633 return mExpanded && row.view.getVisibility() == View.VISIBLE
634 || (mExpanded && (row.important || isActive))
635 || !mExpanded && isActive;
636 }
637
Julia Reynolds568585b2016-07-25 08:55:15 -0400638 private void updateRowsH(final VolumeRow activeRow) {
John Spurlock22def3d2015-06-17 11:56:12 -0400639 if (D.BUG) Log.d(TAG, "updateRowsH");
John Spurlockf88d8082015-03-25 18:09:51 -0400640 if (!mShowing) {
641 trimObsoleteH();
642 }
John Spurlockf88d8082015-03-25 18:09:51 -0400643 // apply changes to all rows
Julia Reynolds568585b2016-07-25 08:55:15 -0400644 for (final VolumeRow row : mRows) {
John Spurlockf88d8082015-03-25 18:09:51 -0400645 final boolean isActive = row == activeRow;
Julia Reynolds568585b2016-07-25 08:55:15 -0400646 final boolean shouldBeVisible = shouldBeVisibleH(row, isActive);
647 Util.setVisOrGone(row.view, shouldBeVisible);
Jason Monk98ad83c2017-06-13 14:03:45 -0400648 Util.setVisOrGone(row.header, shouldBeVisible);
Julia Reynolds568585b2016-07-25 08:55:15 -0400649 if (row.view.isShown()) {
Julia Reynolds568585b2016-07-25 08:55:15 -0400650 updateVolumeRowSliderTintH(row, isActive);
651 }
John Spurlockf88d8082015-03-25 18:09:51 -0400652 }
653 }
654
655 private void trimObsoleteH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400656 if (D.BUG) Log.d(TAG, "trimObsoleteH");
Julia Reynolds568585b2016-07-25 08:55:15 -0400657 for (int i = mRows.size() - 1; i >= 0; i--) {
John Spurlockf88d8082015-03-25 18:09:51 -0400658 final VolumeRow row = mRows.get(i);
659 if (row.ss == null || !row.ss.dynamic) continue;
660 if (!mDynamic.get(row.stream)) {
661 mRows.remove(i);
Julia Reynolds568585b2016-07-25 08:55:15 -0400662 mDialogRowsView.removeView(row.view);
John Spurlockf88d8082015-03-25 18:09:51 -0400663 }
664 }
665 }
666
667 private void onStateChangedH(State state) {
John Spurlock22def3d2015-06-17 11:56:12 -0400668 final boolean animating = mMotion.isAnimating();
669 if (D.BUG) Log.d(TAG, "onStateChangedH animating=" + animating);
John Spurlockf88d8082015-03-25 18:09:51 -0400670 mState = state;
John Spurlock22def3d2015-06-17 11:56:12 -0400671 if (animating) {
672 mPendingStateChanged = true;
673 return;
674 }
John Spurlockf88d8082015-03-25 18:09:51 -0400675 mDynamic.clear();
676 // add any new dynamic rows
677 for (int i = 0; i < state.states.size(); i++) {
678 final int stream = state.states.keyAt(i);
679 final StreamState ss = state.states.valueAt(i);
680 if (!ss.dynamic) continue;
681 mDynamic.put(stream, true);
682 if (findRow(stream) == null) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500683 addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true,
684 true);
John Spurlockf88d8082015-03-25 18:09:51 -0400685 }
686 }
687
688 if (mActiveStream != state.activeStream) {
689 mActiveStream = state.activeStream;
Julia Reynolds568585b2016-07-25 08:55:15 -0400690 updateRowsH(getActiveRow());
John Spurlockf88d8082015-03-25 18:09:51 -0400691 rescheduleTimeoutH();
692 }
693 for (VolumeRow row : mRows) {
694 updateVolumeRowH(row);
695 }
696 updateFooterH();
697 }
698
John Spurlockf88d8082015-03-25 18:09:51 -0400699 private void updateFooterH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400700 if (D.BUG) Log.d(TAG, "updateFooterH");
701 final boolean wasVisible = mZenFooter.getVisibility() == View.VISIBLE;
Julia Reynolds6aa83b42015-09-08 13:41:45 -0400702 final boolean visible = mState.zenMode != Global.ZEN_MODE_OFF
Jason Monke138f552016-01-18 09:21:45 -0500703 && (mAudioManager.isStreamAffectedByRingerMode(mActiveStream) || mExpanded)
Jason Monkaa911b32016-02-10 21:01:30 -0500704 && !mZenPanel.isEditing();
Jason Monk98ad83c2017-06-13 14:03:45 -0400705 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
706 if (wasVisible != visible && !visible) {
707 prepareForCollapse();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400708 }
Jason Monk98ad83c2017-06-13 14:03:45 -0400709 Util.setVisOrGone(mZenFooter, visible);
710 mZenFooter.update();
Jason Monke138f552016-01-18 09:21:45 -0500711
712 final boolean fullWasVisible = mZenPanel.getVisibility() == View.VISIBLE;
Jason Monkaa911b32016-02-10 21:01:30 -0500713 final boolean fullVisible = mShowFullZen && !visible;
Julia Reynoldsd7a00aa2017-02-16 15:01:36 -0500714 if (fullWasVisible != fullVisible) {
715 Util.setVisOrGone(mZenPanel, fullVisible);
716 if (fullVisible) {
717 mZenPanel.setZenState(mState.zenMode);
718 mZenPanel.setDoneListener(new OnClickListener() {
719 @Override
720 public void onClick(View v) {
721 mHandler.sendEmptyMessage(H.UPDATE_FOOTER);
722 }
723 });
724 }
Jason Monkaa911b32016-02-10 21:01:30 -0500725 }
John Spurlockf88d8082015-03-25 18:09:51 -0400726 }
727
728 private void updateVolumeRowH(VolumeRow row) {
John Spurlock22def3d2015-06-17 11:56:12 -0400729 if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400730 if (mState == null) return;
731 final StreamState ss = mState.states.get(row.stream);
732 if (ss == null) return;
733 row.ss = ss;
734 if (ss.level > 0) {
735 row.lastAudibleLevel = ss.level;
736 }
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400737 if (ss.level == row.requestedLevel) {
738 row.requestedLevel = -1;
739 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500740 final boolean isA11yStream = row.stream == AudioManager.STREAM_ACCESSIBILITY;
John Spurlockf88d8082015-03-25 18:09:51 -0400741 final boolean isRingStream = row.stream == AudioManager.STREAM_RING;
742 final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM;
John Spurlockd9c75db2015-04-28 11:19:13 -0400743 final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM;
744 final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC;
John Spurlockf88d8082015-03-25 18:09:51 -0400745 final boolean isRingVibrate = isRingStream
746 && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE;
John Spurlockd9c75db2015-04-28 11:19:13 -0400747 final boolean isRingSilent = isRingStream
748 && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT;
749 final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS;
750 final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
John Spurlockd9c75db2015-04-28 11:19:13 -0400751 final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream)
752 : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream)
753 : false;
John Spurlockf88d8082015-03-25 18:09:51 -0400754
755 // update slider max
Julia Reynolds4b006a82016-04-20 16:09:06 -0400756 final int max = ss.levelMax * 100;
John Spurlockf88d8082015-03-25 18:09:51 -0400757 if (max != row.slider.getMax()) {
758 row.slider.setMax(max);
759 }
760
John Spurlockf88d8082015-03-25 18:09:51 -0400761 // update header text
Julia Reynolds03c548f2016-12-14 15:02:38 -0500762 Util.setText(row.header, getStreamLabelH(ss));
Lucas Dupin421b20c2017-08-01 10:36:10 -0700763 row.slider.setContentDescription(row.header.getText());
Julia Reynolds03c548f2016-12-14 15:02:38 -0500764 mConfigurableTexts.add(row.header, ss.name);
John Spurlockf88d8082015-03-25 18:09:51 -0400765
766 // update icon
John Spurlockd9c75db2015-04-28 11:19:13 -0400767 final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted;
John Spurlockf88d8082015-03-25 18:09:51 -0400768 row.icon.setEnabled(iconEnabled);
769 row.icon.setAlpha(iconEnabled ? 1 : 0.5f);
770 final int iconRes =
John Spurlock76b52b32015-04-03 00:00:12 -0400771 isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
John Spurlockd9c75db2015-04-28 11:19:13 -0400772 : isRingSilent || zenMuted ? row.cachedIconRes
John Spurlockf88d8082015-03-25 18:09:51 -0400773 : ss.routedToBluetooth ?
John Spurlockf15ef112015-04-09 22:04:14 -0400774 (ss.muted ? R.drawable.ic_volume_media_bt_mute
775 : R.drawable.ic_volume_media_bt)
John Spurlock76b52b32015-04-03 00:00:12 -0400776 : mAutomute && ss.level == 0 ? row.iconMuteRes
John Spurlockf88d8082015-03-25 18:09:51 -0400777 : (ss.muted ? row.iconMuteRes : row.iconRes);
778 if (iconRes != row.cachedIconRes) {
779 if (row.cachedIconRes != 0 && isRingVibrate) {
780 mController.vibrate();
781 }
782 row.cachedIconRes = iconRes;
783 row.icon.setImageResource(iconRes);
784 }
785 row.iconState =
786 iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE
John Spurlockf15ef112015-04-09 22:04:14 -0400787 : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400788 ? Events.ICON_STATE_MUTE
John Spurlockf15ef112015-04-09 22:04:14 -0400789 : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400790 ? Events.ICON_STATE_UNMUTE
791 : Events.ICON_STATE_UNKNOWN;
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400792 if (iconEnabled) {
793 if (isRingStream) {
794 if (isRingVibrate) {
795 row.icon.setContentDescription(mContext.getString(
796 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500797 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400798 } else {
799 if (mController.hasVibrator()) {
800 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500801 mShowA11yStream
802 ? R.string.volume_stream_content_description_vibrate_a11y
803 : R.string.volume_stream_content_description_vibrate,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500804 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400805 } else {
806 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500807 mShowA11yStream
808 ? R.string.volume_stream_content_description_mute_a11y
809 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500810 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400811 }
812 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500813 } else if (isA11yStream) {
814 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400815 } else {
816 if (ss.muted || mAutomute && ss.level == 0) {
817 row.icon.setContentDescription(mContext.getString(
818 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500819 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400820 } else {
821 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500822 mShowA11yStream
823 ? R.string.volume_stream_content_description_mute_a11y
824 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500825 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400826 }
827 }
828 } else {
Julia Reynolds03c548f2016-12-14 15:02:38 -0500829 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400830 }
John Spurlockf88d8082015-03-25 18:09:51 -0400831
832 // update slider
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400833 final boolean enableSlider = !zenMuted;
Julia Reynolds5f84c192016-11-14 13:13:08 -0500834 final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400835 : row.ss.level;
836 updateVolumeRowSliderH(row, enableSlider, vlevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400837 }
838
John Spurlockecc80582015-05-13 16:23:14 -0400839 private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) {
John Spurlock67a0f852015-06-15 15:35:47 -0400840 if (isActive && mExpanded) {
John Spurlock67a0f852015-06-15 15:35:47 -0400841 row.slider.requestFocus();
John Spurlock67a0f852015-06-15 15:35:47 -0400842 }
John Spurlockecc80582015-05-13 16:23:14 -0400843 final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint
844 : mInactiveSliderTint;
845 if (tint == row.cachedSliderTint) return;
846 row.cachedSliderTint = tint;
847 row.slider.setProgressTintList(tint);
848 row.slider.setThumbTintList(tint);
849 }
850
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400851 private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) {
852 row.slider.setEnabled(enable);
John Spurlockecc80582015-05-13 16:23:14 -0400853 updateVolumeRowSliderTintH(row, row.stream == mActiveStream);
John Spurlockf88d8082015-03-25 18:09:51 -0400854 if (row.tracking) {
855 return; // don't update if user is sliding
856 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400857 final int progress = row.slider.getProgress();
858 final int level = getImpliedLevel(row.slider, progress);
John Spurlockf88d8082015-03-25 18:09:51 -0400859 final boolean rowVisible = row.view.getVisibility() == View.VISIBLE;
860 final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt)
861 < USER_ATTEMPT_GRACE_PERIOD;
862 mHandler.removeMessages(H.RECHECK, row);
863 if (mShowing && rowVisible && inGracePeriod) {
864 if (D.BUG) Log.d(TAG, "inGracePeriod");
865 mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row),
866 row.userAttempt + USER_ATTEMPT_GRACE_PERIOD);
867 return; // don't update if visible and in grace period
868 }
John Spurlockf88d8082015-03-25 18:09:51 -0400869 if (vlevel == level) {
870 if (mShowing && rowVisible) {
871 return; // don't clamp if visible
872 }
873 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400874 final int newProgress = vlevel * 100;
875 if (progress != newProgress) {
876 if (mShowing && rowVisible) {
877 // animate!
878 if (row.anim != null && row.anim.isRunning()
879 && row.animTargetProgress == newProgress) {
880 return; // already animating to the target progress
881 }
882 // start/update animation
883 if (row.anim == null) {
884 row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress);
885 row.anim.setInterpolator(new DecelerateInterpolator());
886 } else {
887 row.anim.cancel();
888 row.anim.setIntValues(progress, newProgress);
889 }
890 row.animTargetProgress = newProgress;
891 row.anim.setDuration(UPDATE_ANIMATION_DURATION);
892 row.anim.start();
893 } else {
894 // update slider directly to clamped value
895 if (row.anim != null) {
896 row.anim.cancel();
897 }
Julia Reynolds88dabde2016-08-09 13:59:44 -0400898 row.slider.setProgress(newProgress, true);
Julia Reynolds4b006a82016-04-20 16:09:06 -0400899 }
900 }
John Spurlockf88d8082015-03-25 18:09:51 -0400901 }
902
903 private void recheckH(VolumeRow row) {
904 if (row == null) {
905 if (D.BUG) Log.d(TAG, "recheckH ALL");
906 trimObsoleteH();
907 for (VolumeRow r : mRows) {
908 updateVolumeRowH(r);
909 }
910 } else {
911 if (D.BUG) Log.d(TAG, "recheckH " + row.stream);
912 updateVolumeRowH(row);
913 }
914 }
915
916 private void setStreamImportantH(int stream, boolean important) {
917 for (VolumeRow row : mRows) {
918 if (row.stream == stream) {
919 row.important = important;
920 return;
921 }
922 }
923 }
924
John Spurlock76b52b32015-04-03 00:00:12 -0400925 private void showSafetyWarningH(int flags) {
926 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
927 || mShowing) {
928 synchronized (mSafetyWarningLock) {
929 if (mSafetyWarning != null) {
930 return;
931 }
932 mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) {
933 @Override
934 protected void cleanUp() {
935 synchronized (mSafetyWarningLock) {
936 mSafetyWarning = null;
937 }
938 recheckH(null);
939 }
940 };
941 mSafetyWarning.show();
942 }
943 recheckH(null);
944 }
945 rescheduleTimeoutH();
946 }
947
Julia Reynolds03c548f2016-12-14 15:02:38 -0500948 private String getStreamLabelH(StreamState ss) {
949 if (ss.remoteLabel != null) {
950 return ss.remoteLabel;
951 }
952 try {
953 return mContext.getString(ss.name);
954 } catch (Resources.NotFoundException e) {
955 Slog.e(TAG, "Can't find translation for stream " + ss);
956 return "";
957 }
958 }
959
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700960 private AutoTransition getTransition() {
Julia Reynolds24f411d2016-08-11 15:57:10 -0400961 AutoTransition transition = new AutoTransition();
962 transition.setDuration(mExpandButtonAnimationDuration);
963 transition.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
964 transition.addListener(new Transition.TransitionListener() {
965 @Override
966 public void onTransitionStart(Transition transition) {
967 }
968
969 @Override
970 public void onTransitionEnd(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400971 mWindow.setLayout(
972 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -0400973 }
974
975 @Override
976 public void onTransitionCancel(Transition transition) {
977 }
978
979 @Override
980 public void onTransitionPause(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400981 mWindow.setLayout(
982 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -0400983 }
984
985 @Override
986 public void onTransitionResume(Transition transition) {
987 }
988 });
989 return transition;
990 }
991
Takayuki Hoshibb0ec552015-12-17 20:50:25 +0900992 private boolean hasTouchFeature() {
993 final PackageManager pm = mContext.getPackageManager();
994 return pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
995 }
996
John Spurlockf88d8082015-03-25 18:09:51 -0400997 private final VolumeDialogController.Callbacks mControllerCallbackH
998 = new VolumeDialogController.Callbacks() {
999 @Override
1000 public void onShowRequested(int reason) {
1001 showH(reason);
1002 }
1003
1004 @Override
1005 public void onDismissRequested(int reason) {
1006 dismissH(reason);
1007 }
1008
John Spurlock76b52b32015-04-03 00:00:12 -04001009 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001010 public void onScreenOff() {
1011 dismissH(Events.DISMISS_REASON_SCREEN_OFF);
1012 }
1013
1014 @Override
1015 public void onStateChanged(State state) {
1016 onStateChangedH(state);
1017 }
1018
1019 @Override
1020 public void onLayoutDirectionChanged(int layoutDirection) {
1021 mDialogView.setLayoutDirection(layoutDirection);
1022 }
1023
1024 @Override
1025 public void onConfigurationChanged() {
Julia Reynoldsc791e052016-02-19 16:02:01 -05001026 Configuration newConfig = mContext.getResources().getConfiguration();
1027 final int density = newConfig.densityDpi;
1028 if (density != mDensity) {
1029 mDialog.dismiss();
Julia Reynolds8d0a4002016-06-14 15:24:39 -04001030 mZenFooter.cleanup();
Julia Reynoldsc791e052016-02-19 16:02:01 -05001031 initDialog();
Julia Reynolds494f0de2016-07-26 12:57:08 -04001032 mDensity = density;
Julia Reynoldsc791e052016-02-19 16:02:01 -05001033 }
Jason Monk98ad83c2017-06-13 14:03:45 -04001034 updateWindowWidthH();
Julia Reynolds03c548f2016-12-14 15:02:38 -05001035 mConfigurableTexts.update();
John Spurlock9ea3a1e2015-05-06 17:12:18 -04001036 mZenFooter.onConfigurationChanged();
John Spurlockf88d8082015-03-25 18:09:51 -04001037 }
1038
1039 @Override
1040 public void onShowVibrateHint() {
1041 if (mSilentMode) {
1042 mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false);
1043 }
1044 }
1045
John Spurlock76b52b32015-04-03 00:00:12 -04001046 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001047 public void onShowSilentHint() {
1048 if (mSilentMode) {
1049 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
1050 }
1051 }
John Spurlock76b52b32015-04-03 00:00:12 -04001052
1053 @Override
1054 public void onShowSafetyWarning(int flags) {
1055 showSafetyWarningH(flags);
1056 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001057
1058 @Override
1059 public void onAccessibilityModeChanged(Boolean showA11yStream) {
1060 boolean show = showA11yStream == null ? false : showA11yStream;
1061 mShowA11yStream = show;
Julia Reynolds74cc6502017-08-08 12:41:04 -04001062 VolumeRow activeRow = getActiveRow();
1063 if (!mShowA11yStream && AudioManager.STREAM_ACCESSIBILITY == activeRow.stream) {
1064 dismissH(Events.DISMISS_STREAM_GONE);
1065 } else {
1066 updateRowsH(activeRow);
1067 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001068
1069 }
John Spurlockf88d8082015-03-25 18:09:51 -04001070 };
1071
Jason Monke138f552016-01-18 09:21:45 -05001072 private final ZenModePanel.Callback mZenPanelCallback = new ZenModePanel.Callback() {
1073 @Override
1074 public void onPrioritySettings() {
1075 mCallback.onZenPrioritySettingsClicked();
1076 }
1077
1078 @Override
1079 public void onInteraction() {
1080 mHandler.sendEmptyMessage(H.RESCHEDULE_TIMEOUT);
1081 }
1082
1083 @Override
1084 public void onExpanded(boolean expanded) {
1085 // noop.
1086 }
1087 };
1088
John Spurlockf88d8082015-03-25 18:09:51 -04001089 private final OnClickListener mClickExpand = new OnClickListener() {
1090 @Override
1091 public void onClick(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001092 if (mExpandButtonAnimationRunning) return;
John Spurlockf88d8082015-03-25 18:09:51 -04001093 final boolean newExpand = !mExpanded;
Chris Wrene565ee62015-06-17 15:24:56 -04001094 Events.writeEvent(mContext, Events.EVENT_EXPAND, newExpand);
Julia Reynolds48242f02016-08-10 11:42:56 -04001095 updateExpandedH(newExpand, false /* dismissing */);
John Spurlockf88d8082015-03-25 18:09:51 -04001096 }
1097 };
1098
John Spurlockf88d8082015-03-25 18:09:51 -04001099 private final class H extends Handler {
1100 private static final int SHOW = 1;
1101 private static final int DISMISS = 2;
1102 private static final int RECHECK = 3;
1103 private static final int RECHECK_ALL = 4;
1104 private static final int SET_STREAM_IMPORTANT = 5;
1105 private static final int RESCHEDULE_TIMEOUT = 6;
John Spurlock22def3d2015-06-17 11:56:12 -04001106 private static final int STATE_CHANGED = 7;
1107 private static final int UPDATE_BOTTOM_MARGIN = 8;
Jason Monkaa911b32016-02-10 21:01:30 -05001108 private static final int UPDATE_FOOTER = 9;
John Spurlockf88d8082015-03-25 18:09:51 -04001109
1110 public H() {
1111 super(Looper.getMainLooper());
1112 }
1113
1114 @Override
1115 public void handleMessage(Message msg) {
1116 switch (msg.what) {
1117 case SHOW: showH(msg.arg1); break;
1118 case DISMISS: dismissH(msg.arg1); break;
1119 case RECHECK: recheckH((VolumeRow) msg.obj); break;
1120 case RECHECK_ALL: recheckH(null); break;
1121 case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break;
1122 case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break;
John Spurlock22def3d2015-06-17 11:56:12 -04001123 case STATE_CHANGED: onStateChangedH(mState); break;
1124 case UPDATE_BOTTOM_MARGIN: updateDialogBottomMarginH(); break;
Jason Monkaa911b32016-02-10 21:01:30 -05001125 case UPDATE_FOOTER: updateFooterH(); break;
John Spurlockf88d8082015-03-25 18:09:51 -04001126 }
1127 }
1128 }
1129
1130 private final class CustomDialog extends Dialog {
1131 public CustomDialog(Context context) {
1132 super(context);
1133 }
1134
1135 @Override
1136 public boolean dispatchTouchEvent(MotionEvent ev) {
1137 rescheduleTimeoutH();
1138 return super.dispatchTouchEvent(ev);
1139 }
1140
1141 @Override
1142 protected void onStop() {
1143 super.onStop();
John Spurlock22def3d2015-06-17 11:56:12 -04001144 final boolean animating = mMotion.isAnimating();
1145 if (D.BUG) Log.d(TAG, "onStop animating=" + animating);
1146 if (animating) {
1147 mPendingRecheckAll = true;
1148 return;
1149 }
John Spurlockf88d8082015-03-25 18:09:51 -04001150 mHandler.sendEmptyMessage(H.RECHECK_ALL);
1151 }
1152
1153 @Override
1154 public boolean onTouchEvent(MotionEvent event) {
1155 if (isShowing()) {
1156 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
1157 dismissH(Events.DISMISS_REASON_TOUCH_OUTSIDE);
1158 return true;
1159 }
1160 }
1161 return false;
1162 }
Julia Reynolds2feab162016-04-21 14:58:52 -04001163
1164 @Override
1165 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
1166 event.setClassName(getClass().getSuperclass().getName());
1167 event.setPackageName(mContext.getPackageName());
1168
1169 ViewGroup.LayoutParams params = getWindow().getAttributes();
Jason Monk98ad83c2017-06-13 14:03:45 -04001170 boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
1171 (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
Julia Reynolds2feab162016-04-21 14:58:52 -04001172 event.setFullScreen(isFullScreen);
1173
1174 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
1175 if (mShowing) {
1176 event.getText().add(mContext.getString(
1177 R.string.volume_dialog_accessibility_shown_message,
Julia Reynolds03c548f2016-12-14 15:02:38 -05001178 getStreamLabelH(getActiveRow().ss)));
Julia Reynolds2feab162016-04-21 14:58:52 -04001179 return true;
1180 }
1181 }
1182 return false;
1183 }
John Spurlockf88d8082015-03-25 18:09:51 -04001184 }
1185
1186 private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener {
1187 private final VolumeRow mRow;
1188
1189 private VolumeSeekBarChangeListener(VolumeRow row) {
1190 mRow = row;
1191 }
1192
1193 @Override
1194 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1195 if (mRow.ss == null) return;
1196 if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
1197 + " onProgressChanged " + progress + " fromUser=" + fromUser);
1198 if (!fromUser) return;
1199 if (mRow.ss.levelMin > 0) {
Julia Reynolds4b006a82016-04-20 16:09:06 -04001200 final int minProgress = mRow.ss.levelMin * 100;
John Spurlockf88d8082015-03-25 18:09:51 -04001201 if (progress < minProgress) {
1202 seekBar.setProgress(minProgress);
Julia Reynolds798b0c32015-09-01 14:35:53 -04001203 progress = minProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001204 }
1205 }
Julia Reynolds4b006a82016-04-20 16:09:06 -04001206 final int userLevel = getImpliedLevel(seekBar, progress);
1207 if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) {
John Spurlockf88d8082015-03-25 18:09:51 -04001208 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001209 if (mRow.requestedLevel != userLevel) {
1210 mController.setStreamVolume(mRow.stream, userLevel);
1211 mRow.requestedLevel = userLevel;
Chris Wrene565ee62015-06-17 15:24:56 -04001212 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream,
Julia Reynolds4b006a82016-04-20 16:09:06 -04001213 userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001214 }
1215 }
1216 }
1217
1218 @Override
1219 public void onStartTrackingTouch(SeekBar seekBar) {
1220 if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream);
1221 mController.setActiveStream(mRow.stream);
1222 mRow.tracking = true;
1223 }
1224
1225 @Override
1226 public void onStopTrackingTouch(SeekBar seekBar) {
1227 if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream);
1228 mRow.tracking = false;
1229 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001230 final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress());
Chris Wrene565ee62015-06-17 15:24:56 -04001231 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001232 if (mRow.ss.level != userLevel) {
1233 mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow),
1234 USER_ATTEMPT_GRACE_PERIOD);
1235 }
1236 }
1237 }
1238
John Spurlockf09838b2015-06-11 16:14:44 -04001239 private final class Accessibility extends AccessibilityDelegate {
John Spurlock5adeabc2015-05-05 14:50:43 -04001240 private boolean mFeedbackEnabled;
1241
1242 public void init() {
John Spurlock5adeabc2015-05-05 14:50:43 -04001243 mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
1244 @Override
1245 public void onViewDetachedFromWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001246 if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001247 }
1248
1249 @Override
1250 public void onViewAttachedToWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001251 if (D.BUG) Log.d(TAG, "onViewAttachedToWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001252 updateFeedbackEnabled();
1253 }
1254 });
John Spurlockf09838b2015-06-11 16:14:44 -04001255 mDialogView.setAccessibilityDelegate(this);
Jason Monk3b9357f2017-07-14 09:40:54 -04001256 mAccessibilityMgr.addAccessibilityStateChangeListener(mListener);
John Spurlock5adeabc2015-05-05 14:50:43 -04001257 updateFeedbackEnabled();
1258 }
1259
Jason Monk3b9357f2017-07-14 09:40:54 -04001260 public void destroy() {
1261 mAccessibilityMgr.removeAccessibilityStateChangeListener(mListener);
1262 }
1263
John Spurlockf09838b2015-06-11 16:14:44 -04001264 @Override
1265 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
1266 AccessibilityEvent event) {
1267 rescheduleTimeoutH();
1268 return super.onRequestSendAccessibilityEvent(host, child, event);
1269 }
1270
John Spurlock5adeabc2015-05-05 14:50:43 -04001271 private void updateFeedbackEnabled() {
1272 mFeedbackEnabled = computeFeedbackEnabled();
1273 }
1274
1275 private boolean computeFeedbackEnabled() {
John Spurlockf09838b2015-06-11 16:14:44 -04001276 // are there any enabled non-generic a11y services?
John Spurlock5adeabc2015-05-05 14:50:43 -04001277 final List<AccessibilityServiceInfo> services =
Julia Reynolds2feab162016-04-21 14:58:52 -04001278 mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK);
John Spurlock5adeabc2015-05-05 14:50:43 -04001279 for (AccessibilityServiceInfo asi : services) {
John Spurlockf09838b2015-06-11 16:14:44 -04001280 if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) {
John Spurlock5adeabc2015-05-05 14:50:43 -04001281 return true;
1282 }
1283 }
1284 return false;
1285 }
Jason Monk3b9357f2017-07-14 09:40:54 -04001286
1287 private final AccessibilityStateChangeListener mListener =
1288 enabled -> updateFeedbackEnabled();
John Spurlock5adeabc2015-05-05 14:50:43 -04001289 }
1290
John Spurlockf88d8082015-03-25 18:09:51 -04001291 private static class VolumeRow {
1292 private View view;
John Spurlockf88d8082015-03-25 18:09:51 -04001293 private TextView header;
1294 private ImageButton icon;
1295 private SeekBar slider;
John Spurlockf88d8082015-03-25 18:09:51 -04001296 private int stream;
1297 private StreamState ss;
1298 private long userAttempt; // last user-driven slider change
1299 private boolean tracking; // tracking slider touch
John Spurlockb0a2e5f2015-06-09 14:41:52 -04001300 private int requestedLevel = -1; // pending user-requested level via progress changed
John Spurlockf88d8082015-03-25 18:09:51 -04001301 private int iconRes;
1302 private int iconMuteRes;
1303 private boolean important;
1304 private int cachedIconRes;
John Spurlockecc80582015-05-13 16:23:14 -04001305 private ColorStateList cachedSliderTint;
John Spurlockf88d8082015-03-25 18:09:51 -04001306 private int iconState; // from Events
Julia Reynolds4b006a82016-04-20 16:09:06 -04001307 private ObjectAnimator anim; // slider progress animation for non-touch-related updates
1308 private int animTargetProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001309 private int lastAudibleLevel = 1;
1310 }
John Spurlockf88d8082015-03-25 18:09:51 -04001311}