blob: 0fd6c7440c94698983fa7b95b8ac4672d12e6d8f [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;
Beverlyfa4c8e72017-08-10 14:55:34 -0400137 private int mPrevActiveStream;
John Spurlockf55b7f22015-04-13 19:21:26 -0400138 private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE;
139 private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE;
John Spurlockf88d8082015-03-25 18:09:51 -0400140 private State mState;
John Spurlock22def3d2015-06-17 11:56:12 -0400141 private boolean mExpandButtonAnimationRunning;
John Spurlock76b52b32015-04-03 00:00:12 -0400142 private SafetyWarningDialog mSafetyWarning;
John Spurlockf55b7f22015-04-13 19:21:26 -0400143 private Callback mCallback;
John Spurlock22def3d2015-06-17 11:56:12 -0400144 private boolean mPendingStateChanged;
145 private boolean mPendingRecheckAll;
146 private long mCollapseTime;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700147 private boolean mHovering = false;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500148 private int mDensity;
John Spurlockf88d8082015-03-25 18:09:51 -0400149
Jason Monke138f552016-01-18 09:21:45 -0500150 private boolean mShowFullZen;
Julia Reynoldsc791e052016-02-19 16:02:01 -0500151 private TunerZenModePanel mZenPanel;
Jason Monke138f552016-01-18 09:21:45 -0500152
Jason Monk782cd672017-03-22 12:50:57 -0400153 public VolumeDialogImpl(Context context) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700154 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk782cd672017-03-22 12:50:57 -0400155 mZenModeController = Dependency.get(ZenModeController.class);
156 mController = Dependency.get(VolumeDialogController.class);
157 mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
158 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
Julia Reynolds568585b2016-07-25 08:55:15 -0400159 mAccessibilityMgr =
160 (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700161 mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext));
Julia Reynoldsc791e052016-02-19 16:02:01 -0500162 mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive);
Jason Monk782cd672017-03-22 12:50:57 -0400163 }
164
165 public void init(int windowType, Callback callback) {
166 mCallback = callback;
167 mWindowType = windowType;
John Spurlockf88d8082015-03-25 18:09:51 -0400168
Julia Reynoldsc791e052016-02-19 16:02:01 -0500169 initDialog();
170
171 mAccessibility.init();
172
Jason Monk782cd672017-03-22 12:50:57 -0400173 mController.addCallback(mControllerCallbackH, mHandler);
174 mController.getState();
Jason Monkde850bb2017-02-01 19:26:30 -0500175 Dependency.get(TunerService.class).addTunable(this, SHOW_FULL_ZEN);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500176
177 final Configuration currentConfig = mContext.getResources().getConfiguration();
178 mDensity = currentConfig.densityDpi;
179 }
180
Jason Monk782cd672017-03-22 12:50:57 -0400181 @Override
182 public void destroy() {
Jason Monk3b9357f2017-07-14 09:40:54 -0400183 mAccessibility.destroy();
Jason Monk782cd672017-03-22 12:50:57 -0400184 mController.removeCallback(mControllerCallbackH);
Jason Monk3b9357f2017-07-14 09:40:54 -0400185 if (mZenFooter != null) {
186 mZenFooter.cleanup();
187 }
188 Dependency.get(TunerService.class).removeTunable(this);
189 mHandler.removeCallbacksAndMessages(null);
Jason Monk782cd672017-03-22 12:50:57 -0400190 }
191
Julia Reynoldsc791e052016-02-19 16:02:01 -0500192 private void initDialog() {
John Spurlockf88d8082015-03-25 18:09:51 -0400193 mDialog = new CustomDialog(mContext);
194
Julia Reynolds03c548f2016-12-14 15:02:38 -0500195 mConfigurableTexts = new ConfigurableTexts(mContext);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500196 mHovering = false;
197 mShowing = false;
Julia Reynolds568585b2016-07-25 08:55:15 -0400198 mWindow = mDialog.getWindow();
199 mWindow.requestFeature(Window.FEATURE_NO_TITLE);
Jason Monk98ad83c2017-06-13 14:03:45 -0400200 mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Julia Reynolds568585b2016-07-25 08:55:15 -0400201 mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
202 mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
John Spurlockf88d8082015-03-25 18:09:51 -0400203 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
204 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
205 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Jason Monk98ad83c2017-06-13 14:03:45 -0400206 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
207 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
John Spurlockf88d8082015-03-25 18:09:51 -0400208 mDialog.setCanceledOnTouchOutside(true);
209 final Resources res = mContext.getResources();
Jason Monk98ad83c2017-06-13 14:03:45 -0400210 final WindowManager.LayoutParams lp = mWindow.getAttributes();
211 lp.type = mWindowType;
212 lp.format = PixelFormat.TRANSLUCENT;
213 lp.setTitle(VolumeDialogImpl.class.getSimpleName());
214 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
215 lp.y = res.getDimensionPixelSize(R.dimen.volume_offset_top);
216 lp.gravity = Gravity.TOP;
217 lp.windowAnimations = -1;
218 mWindow.setAttributes(lp);
Julia Reynolds568585b2016-07-25 08:55:15 -0400219 mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500220
Jason Monk98ad83c2017-06-13 14:03:45 -0400221 mDialog.setContentView(R.layout.volume_dialog);
222 mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog);
223 mDialogView.setOnHoverListener(new View.OnHoverListener() {
224 @Override
225 public boolean onHover(View v, MotionEvent event) {
226 int action = event.getActionMasked();
227 mHovering = (action == MotionEvent.ACTION_HOVER_ENTER)
228 || (action == MotionEvent.ACTION_HOVER_MOVE);
229 rescheduleTimeoutH();
230 return true;
231 }
Jun Mukai3f5d7132015-07-21 14:10:09 -0700232 });
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700233
Jason Monk16fbd9d2017-04-27 14:28:49 -0400234 mDialogContentView = mDialog.findViewById(R.id.volume_dialog_content);
235 mDialogRowsView = mDialogContentView.findViewById(R.id.volume_dialog_rows);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500236 mExpanded = false;
Jason Monk98ad83c2017-06-13 14:03:45 -0400237 mExpandButton = (ImageButton) mDialogView.findViewById(R.id.volume_expand_button);
John Spurlockf88d8082015-03-25 18:09:51 -0400238 mExpandButton.setOnClickListener(mClickExpand);
Muyuan Li36007d22016-06-21 14:21:48 -0700239
240 mExpandButton.setVisibility(
241 AudioSystem.isSingleVolume(mContext) ? View.GONE : View.VISIBLE);
Jason Monk98ad83c2017-06-13 14:03:45 -0400242 updateWindowWidthH();
John Spurlockf88d8082015-03-25 18:09:51 -0400243 updateExpandButtonH();
Julia Reynoldsc791e052016-02-19 16:02:01 -0500244
Jason Monk98ad83c2017-06-13 14:03:45 -0400245 mMotion = new VolumeDialogMotion(mDialog, mDialogView, mDialogContentView, mExpandButton,
246 new VolumeDialogMotion.Callback() {
247 @Override
248 public void onAnimatingChanged(boolean animating) {
249 if (animating) return;
250 if (mPendingStateChanged) {
251 mHandler.sendEmptyMessage(H.STATE_CHANGED);
252 mPendingStateChanged = false;
253 }
254 if (mPendingRecheckAll) {
255 mHandler.sendEmptyMessage(H.RECHECK_ALL);
256 mPendingRecheckAll = false;
257 }
Julia Reynoldsc791e052016-02-19 16:02:01 -0500258 }
259 });
John Spurlockf88d8082015-03-25 18:09:51 -0400260
Julia Reynoldsc791e052016-02-19 16:02:01 -0500261 if (mRows.isEmpty()) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500262 addRow(AudioManager.STREAM_MUSIC,
263 R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700264 if (!AudioSystem.isSingleVolume(mContext)) {
265 addRow(AudioManager.STREAM_RING,
266 R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700267 addRow(AudioManager.STREAM_ALARM,
268 R.drawable.ic_volume_alarm, R.drawable.ic_volume_alarm_mute, false);
269 addRow(AudioManager.STREAM_VOICE_CALL,
270 R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, false);
271 addRow(AudioManager.STREAM_BLUETOOTH_SCO,
272 R.drawable.ic_volume_bt_sco, R.drawable.ic_volume_bt_sco, false);
273 addRow(AudioManager.STREAM_SYSTEM,
274 R.drawable.ic_volume_system, R.drawable.ic_volume_system_mute, false);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500275 addRow(AudioManager.STREAM_ACCESSIBILITY, R.drawable.ic_volume_accessibility,
276 R.drawable.ic_volume_accessibility, true);
Muyuan Li36007d22016-06-21 14:21:48 -0700277 }
Julia Reynoldsc791e052016-02-19 16:02:01 -0500278 } else {
279 addExistingRows();
280 }
John Spurlockf88d8082015-03-25 18:09:51 -0400281 mExpandButtonAnimationDuration = res.getInteger(R.integer.volume_expand_animation_duration);
Jason Monk98ad83c2017-06-13 14:03:45 -0400282 mZenFooter = (ZenFooter) mDialog.findViewById(R.id.volume_zen_footer);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500283 mZenFooter.init(mZenModeController);
Jason Monk98ad83c2017-06-13 14:03:45 -0400284 mZenPanel = (TunerZenModePanel) mDialog.findViewById(R.id.tuner_zen_mode_panel);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500285 mZenPanel.init(mZenModeController);
Jason Monke138f552016-01-18 09:21:45 -0500286 mZenPanel.setCallback(mZenPanelCallback);
Jason Monke138f552016-01-18 09:21:45 -0500287 }
288
289 @Override
290 public void onTuningChanged(String key, String newValue) {
291 if (SHOW_FULL_ZEN.equals(key)) {
292 mShowFullZen = newValue != null && Integer.parseInt(newValue) != 0;
293 }
John Spurlockf88d8082015-03-25 18:09:51 -0400294 }
295
John Spurlock77bb25af2015-05-11 12:12:07 -0400296 private ColorStateList loadColorStateList(int colorResId) {
297 return ColorStateList.valueOf(mContext.getColor(colorResId));
298 }
299
Jason Monk98ad83c2017-06-13 14:03:45 -0400300 private void updateWindowWidthH() {
Lujiang Xuea8aae282017-08-15 12:29:07 -0700301 final ViewGroup.MarginLayoutParams lp =
302 (ViewGroup.MarginLayoutParams) mDialogView.getLayoutParams();
Jason Monk98ad83c2017-06-13 14:03:45 -0400303 final DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
304 if (D.BUG) Log.d(TAG, "updateWindowWidth dm.w=" + dm.widthPixels);
305 int w = dm.widthPixels;
306 final int max = mContext.getResources()
307 .getDimensionPixelSize(R.dimen.volume_dialog_panel_width);
308 if (w > max) {
309 w = max;
310 }
Lujiang Xuea8aae282017-08-15 12:29:07 -0700311 lp.width = w - lp.getMarginEnd() - lp.getMarginStart();
Jason Monk98ad83c2017-06-13 14:03:45 -0400312 mDialogView.setLayoutParams(lp);
313 }
314
John Spurlockf88d8082015-03-25 18:09:51 -0400315 public void setStreamImportant(int stream, boolean important) {
316 mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget();
317 }
318
John Spurlockf88d8082015-03-25 18:09:51 -0400319 public void setAutomute(boolean automute) {
320 if (mAutomute == automute) return;
321 mAutomute = automute;
322 mHandler.sendEmptyMessage(H.RECHECK_ALL);
323 }
324
325 public void setSilentMode(boolean silentMode) {
326 if (mSilentMode == silentMode) return;
327 mSilentMode = silentMode;
328 mHandler.sendEmptyMessage(H.RECHECK_ALL);
329 }
330
331 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500332 addRow(stream, iconRes, iconMuteRes, important, false);
333 }
334
335 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important,
336 boolean dynamic) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500337 VolumeRow row = new VolumeRow();
338 initRow(row, stream, iconRes, iconMuteRes, important);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500339 int rowSize;
340 int viewSize;
341 if (mShowA11yStream && dynamic && (rowSize = mRows.size()) > 1
342 && (viewSize = mDialogRowsView.getChildCount()) > 1) {
343 // A11y Stream should be the last in the list
344 mDialogRowsView.addView(row.view, viewSize - 2);
345 mRows.add(rowSize - 2, row);
346 } else {
347 mDialogRowsView.addView(row.view);
348 mRows.add(row);
349 }
John Spurlockf88d8082015-03-25 18:09:51 -0400350 }
351
Julia Reynoldsc791e052016-02-19 16:02:01 -0500352 private void addExistingRows() {
353 int N = mRows.size();
354 for (int i = 0; i < N; i++) {
355 final VolumeRow row = mRows.get(i);
356 initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important);
Julia Reynolds568585b2016-07-25 08:55:15 -0400357 mDialogRowsView.addView(row.view);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500358 }
359 }
360
Julia Reynoldsc791e052016-02-19 16:02:01 -0500361
John Spurlockf88d8082015-03-25 18:09:51 -0400362 private boolean isAttached() {
363 return mDialogContentView != null && mDialogContentView.isAttachedToWindow();
364 }
365
366 private VolumeRow getActiveRow() {
367 for (VolumeRow row : mRows) {
368 if (row.stream == mActiveStream) {
369 return row;
370 }
371 }
372 return mRows.get(0);
373 }
374
375 private VolumeRow findRow(int stream) {
376 for (VolumeRow row : mRows) {
377 if (row.stream == stream) return row;
378 }
379 return null;
380 }
381
John Spurlockf88d8082015-03-25 18:09:51 -0400382 public void dump(PrintWriter writer) {
Jason Monk782cd672017-03-22 12:50:57 -0400383 writer.println(VolumeDialogImpl.class.getSimpleName() + " state:");
John Spurlockf88d8082015-03-25 18:09:51 -0400384 writer.print(" mShowing: "); writer.println(mShowing);
385 writer.print(" mExpanded: "); writer.println(mExpanded);
John Spurlock22def3d2015-06-17 11:56:12 -0400386 writer.print(" mExpandButtonAnimationRunning: ");
387 writer.println(mExpandButtonAnimationRunning);
John Spurlockf88d8082015-03-25 18:09:51 -0400388 writer.print(" mActiveStream: "); writer.println(mActiveStream);
389 writer.print(" mDynamic: "); writer.println(mDynamic);
John Spurlockf88d8082015-03-25 18:09:51 -0400390 writer.print(" mAutomute: "); writer.println(mAutomute);
391 writer.print(" mSilentMode: "); writer.println(mSilentMode);
John Spurlock22def3d2015-06-17 11:56:12 -0400392 writer.print(" mCollapseTime: "); writer.println(mCollapseTime);
John Spurlock84ee67c2015-05-20 23:33:43 -0400393 writer.print(" mAccessibility.mFeedbackEnabled: ");
394 writer.println(mAccessibility.mFeedbackEnabled);
John Spurlockf88d8082015-03-25 18:09:51 -0400395 }
396
Julia Reynolds4b006a82016-04-20 16:09:06 -0400397 private static int getImpliedLevel(SeekBar seekBar, int progress) {
398 final int m = seekBar.getMax();
399 final int n = m / 100 - 1;
400 final int level = progress == 0 ? 0
401 : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n));
402 return level;
403 }
404
John Spurlockf88d8082015-03-25 18:09:51 -0400405 @SuppressLint("InflateParams")
Julia Reynoldsc791e052016-02-19 16:02:01 -0500406 private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes,
407 boolean important) {
John Spurlockf88d8082015-03-25 18:09:51 -0400408 row.stream = stream;
409 row.iconRes = iconRes;
410 row.iconMuteRes = iconMuteRes;
411 row.important = important;
412 row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
Julia Reynolds568585b2016-07-25 08:55:15 -0400413 row.view.setId(row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400414 row.view.setTag(row);
415 row.header = (TextView) row.view.findViewById(R.id.volume_row_header);
Julia Reynolds568585b2016-07-25 08:55:15 -0400416 row.header.setId(20 * row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400417 row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider);
418 row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
Julia Reynolds5774cf02016-05-26 13:43:38 -0400419 row.anim = null;
John Spurlockf88d8082015-03-25 18:09:51 -0400420
421 // forward events above the slider into the slider
422 row.view.setOnTouchListener(new OnTouchListener() {
423 private final Rect mSliderHitRect = new Rect();
424 private boolean mDragging;
425
426 @SuppressLint("ClickableViewAccessibility")
427 @Override
428 public boolean onTouch(View v, MotionEvent event) {
429 row.slider.getHitRect(mSliderHitRect);
430 if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN
431 && event.getY() < mSliderHitRect.top) {
432 mDragging = true;
433 }
434 if (mDragging) {
435 event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top);
436 row.slider.dispatchTouchEvent(event);
437 if (event.getActionMasked() == MotionEvent.ACTION_UP
438 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
439 mDragging = false;
440 }
441 return true;
442 }
443 return false;
444 }
445 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400446 row.icon = row.view.findViewById(R.id.volume_row_icon);
John Spurlockf88d8082015-03-25 18:09:51 -0400447 row.icon.setImageResource(iconRes);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500448 if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) {
449 row.icon.setOnClickListener(new OnClickListener() {
450 @Override
451 public void onClick(View v) {
452 Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState);
453 mController.setActiveStream(row.stream);
454 if (row.stream == AudioManager.STREAM_RING) {
455 final boolean hasVibrator = mController.hasVibrator();
456 if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {
457 if (hasVibrator) {
458 mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);
459 } else {
460 final boolean wasZero = row.ss.level == 0;
461 mController.setStreamVolume(stream,
462 wasZero ? row.lastAudibleLevel : 0);
463 }
John Spurlockf88d8082015-03-25 18:09:51 -0400464 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500465 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
466 if (row.ss.level == 0) {
467 mController.setStreamVolume(stream, 1);
468 }
John Spurlockf88d8082015-03-25 18:09:51 -0400469 }
470 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500471 final boolean vmute = row.ss.level == row.ss.levelMin;
472 mController.setStreamVolume(stream,
473 vmute ? row.lastAudibleLevel : row.ss.levelMin);
John Spurlockf88d8082015-03-25 18:09:51 -0400474 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500475 row.userAttempt = 0; // reset the grace period, slider updates immediately
John Spurlockf88d8082015-03-25 18:09:51 -0400476 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500477 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400478 } else {
479 row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500480 }
John Spurlockf88d8082015-03-25 18:09:51 -0400481 }
482
John Spurlockf88d8082015-03-25 18:09:51 -0400483 public void show(int reason) {
484 mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget();
485 }
486
487 public void dismiss(int reason) {
488 mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget();
489 }
490
John Spurlockf88d8082015-03-25 18:09:51 -0400491 private void showH(int reason) {
John Spurlock22def3d2015-06-17 11:56:12 -0400492 if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]);
John Spurlockf88d8082015-03-25 18:09:51 -0400493 mHandler.removeMessages(H.SHOW);
494 mHandler.removeMessages(H.DISMISS);
495 rescheduleTimeoutH();
496 if (mShowing) return;
497 mShowing = true;
John Spurlock22def3d2015-06-17 11:56:12 -0400498 mMotion.startShow();
Chris Wrene565ee62015-06-17 15:24:56 -0400499 Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
John Spurlockf88d8082015-03-25 18:09:51 -0400500 mController.notifyVisible(true);
501 }
502
503 protected void rescheduleTimeoutH() {
504 mHandler.removeMessages(H.DISMISS);
John Spurlockf09838b2015-06-11 16:14:44 -0400505 final int timeout = computeTimeoutH();
506 mHandler.sendMessageDelayed(mHandler
507 .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout);
John Spurlock5adeabc2015-05-05 14:50:43 -0400508 if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller());
John Spurlockf88d8082015-03-25 18:09:51 -0400509 mController.userActivity();
510 }
511
512 private int computeTimeoutH() {
John Spurlockf09838b2015-06-11 16:14:44 -0400513 if (mAccessibility.mFeedbackEnabled) return 20000;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700514 if (mHovering) return 16000;
John Spurlock76b52b32015-04-03 00:00:12 -0400515 if (mSafetyWarning != null) return 5000;
John Spurlock22def3d2015-06-17 11:56:12 -0400516 if (mExpanded || mExpandButtonAnimationRunning) return 5000;
John Spurlockf88d8082015-03-25 18:09:51 -0400517 if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500;
518 return 3000;
519 }
520
521 protected void dismissH(int reason) {
Jason Monk305e0ba2015-07-06 15:53:59 -0400522 if (mMotion.isAnimating()) {
523 return;
524 }
John Spurlockf88d8082015-03-25 18:09:51 -0400525 mHandler.removeMessages(H.DISMISS);
526 mHandler.removeMessages(H.SHOW);
527 if (!mShowing) return;
528 mShowing = false;
John Spurlock22def3d2015-06-17 11:56:12 -0400529 mMotion.startDismiss(new Runnable() {
530 @Override
531 public void run() {
Julia Reynolds48242f02016-08-10 11:42:56 -0400532 updateExpandedH(false /* expanding */, true /* dismissing */);
John Spurlock22def3d2015-06-17 11:56:12 -0400533 }
534 });
Julia Reynolds2feab162016-04-21 14:58:52 -0400535 if (mAccessibilityMgr.isEnabled()) {
536 AccessibilityEvent event =
537 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
538 event.setPackageName(mContext.getPackageName());
539 event.setClassName(CustomDialog.class.getSuperclass().getName());
540 event.getText().add(mContext.getString(
541 R.string.volume_dialog_accessibility_dismissed_message));
542 mAccessibilityMgr.sendAccessibilityEvent(event);
543 }
Chris Wrene565ee62015-06-17 15:24:56 -0400544 Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason);
John Spurlockf88d8082015-03-25 18:09:51 -0400545 mController.notifyVisible(false);
John Spurlock76b52b32015-04-03 00:00:12 -0400546 synchronized (mSafetyWarningLock) {
547 if (mSafetyWarning != null) {
548 if (D.BUG) Log.d(TAG, "SafetyWarning dismissed");
549 mSafetyWarning.dismiss();
550 }
551 }
John Spurlockf88d8082015-03-25 18:09:51 -0400552 }
553
John Spurlock22def3d2015-06-17 11:56:12 -0400554 private void updateDialogBottomMarginH() {
Jason Monk98ad83c2017-06-13 14:03:45 -0400555 final long diff = System.currentTimeMillis() - mCollapseTime;
556 final boolean collapsing = mCollapseTime != 0 && diff < getConservativeCollapseDuration();
John Spurlock22def3d2015-06-17 11:56:12 -0400557 final ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) mDialogView.getLayoutParams();
Jason Monk98ad83c2017-06-13 14:03:45 -0400558 final int bottomMargin = collapsing ? mDialogContentView.getHeight() :
John Spurlock22def3d2015-06-17 11:56:12 -0400559 mContext.getResources().getDimensionPixelSize(R.dimen.volume_dialog_margin_bottom);
560 if (bottomMargin != mlp.bottomMargin) {
561 if (D.BUG) Log.d(TAG, "bottomMargin " + mlp.bottomMargin + " -> " + bottomMargin);
562 mlp.bottomMargin = bottomMargin;
563 mDialogView.setLayoutParams(mlp);
564 }
565 }
566
567 private long getConservativeCollapseDuration() {
568 return mExpandButtonAnimationDuration * 3;
569 }
570
571 private void prepareForCollapse() {
572 mHandler.removeMessages(H.UPDATE_BOTTOM_MARGIN);
573 mCollapseTime = System.currentTimeMillis();
574 updateDialogBottomMarginH();
575 mHandler.sendEmptyMessageDelayed(H.UPDATE_BOTTOM_MARGIN, getConservativeCollapseDuration());
576 }
577
Julia Reynolds48242f02016-08-10 11:42:56 -0400578 private void updateExpandedH(final boolean expanded, final boolean dismissing) {
John Spurlockf88d8082015-03-25 18:09:51 -0400579 if (mExpanded == expanded) return;
580 mExpanded = expanded;
John Spurlock22def3d2015-06-17 11:56:12 -0400581 mExpandButtonAnimationRunning = isAttached();
Julia Reynolds568585b2016-07-25 08:55:15 -0400582 if (D.BUG) Log.d(TAG, "updateExpandedH " + expanded);
583 updateExpandButtonH();
584 updateFooterH();
Julia Reynolds568585b2016-07-25 08:55:15 -0400585 TransitionManager.endTransitions(mDialogView);
Julia Reynolds48242f02016-08-10 11:42:56 -0400586 final VolumeRow activeRow = getActiveRow();
587 if (!dismissing) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400588 mWindow.setLayout(mWindow.getAttributes().width, ViewGroup.LayoutParams.MATCH_PARENT);
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700589 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
Julia Reynolds48242f02016-08-10 11:42:56 -0400590 }
Julia Reynolds568585b2016-07-25 08:55:15 -0400591 updateRowsH(activeRow);
592 rescheduleTimeoutH();
593 }
594
595 private void updateExpandButtonH() {
596 if (D.BUG) Log.d(TAG, "updateExpandButtonH");
597 mExpandButton.setClickable(!mExpandButtonAnimationRunning);
598 if (!(mExpandButtonAnimationRunning && isAttached())) {
599 final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
600 : R.drawable.ic_volume_expand_animation;
601 if (hasTouchFeature()) {
602 mExpandButton.setImageResource(res);
603 } else {
604 // if there is no touch feature, show the volume ringer instead
605 mExpandButton.setImageResource(R.drawable.ic_volume_ringer);
606 mExpandButton.setBackgroundResource(0); // remove gray background emphasis
607 }
608 mExpandButton.setContentDescription(mContext.getString(mExpanded ?
609 R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand));
John Spurlock22def3d2015-06-17 11:56:12 -0400610 }
John Spurlock22def3d2015-06-17 11:56:12 -0400611 if (mExpandButtonAnimationRunning) {
John Spurlockf88d8082015-03-25 18:09:51 -0400612 final Drawable d = mExpandButton.getDrawable();
613 if (d instanceof AnimatedVectorDrawable) {
614 // workaround to reset drawable
615 final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) d.getConstantState()
616 .newDrawable();
617 mExpandButton.setImageDrawable(avd);
618 avd.start();
619 mHandler.postDelayed(new Runnable() {
620 @Override
621 public void run() {
John Spurlock22def3d2015-06-17 11:56:12 -0400622 mExpandButtonAnimationRunning = false;
John Spurlockf88d8082015-03-25 18:09:51 -0400623 updateExpandButtonH();
624 rescheduleTimeoutH();
625 }
626 }, mExpandButtonAnimationDuration);
627 }
628 }
John Spurlockf88d8082015-03-25 18:09:51 -0400629 }
630
Beverlyfa4c8e72017-08-10 14:55:34 -0400631 private boolean shouldBeVisibleH(VolumeRow row, VolumeRow activeRow) {
632 boolean isActive = row == activeRow;
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500633 if (row.stream == AudioSystem.STREAM_ACCESSIBILITY) {
634 return mShowA11yStream;
635 }
Beverlyfa4c8e72017-08-10 14:55:34 -0400636
637 // if the active row is accessibility, then continue to display previous
638 // active row since accessibility is dispalyed under it
639 if (activeRow.stream == AudioSystem.STREAM_ACCESSIBILITY &&
640 row.stream == mPrevActiveStream) {
641 return true;
642 }
643
John Spurlockf88d8082015-03-25 18:09:51 -0400644 return mExpanded && row.view.getVisibility() == View.VISIBLE
645 || (mExpanded && (row.important || isActive))
646 || !mExpanded && isActive;
647 }
648
Julia Reynolds568585b2016-07-25 08:55:15 -0400649 private void updateRowsH(final VolumeRow activeRow) {
John Spurlock22def3d2015-06-17 11:56:12 -0400650 if (D.BUG) Log.d(TAG, "updateRowsH");
John Spurlockf88d8082015-03-25 18:09:51 -0400651 if (!mShowing) {
652 trimObsoleteH();
653 }
John Spurlockf88d8082015-03-25 18:09:51 -0400654 // apply changes to all rows
Julia Reynolds568585b2016-07-25 08:55:15 -0400655 for (final VolumeRow row : mRows) {
John Spurlockf88d8082015-03-25 18:09:51 -0400656 final boolean isActive = row == activeRow;
Beverlyfa4c8e72017-08-10 14:55:34 -0400657 final boolean shouldBeVisible = shouldBeVisibleH(row, activeRow);
Julia Reynolds568585b2016-07-25 08:55:15 -0400658 Util.setVisOrGone(row.view, shouldBeVisible);
Jason Monk98ad83c2017-06-13 14:03:45 -0400659 Util.setVisOrGone(row.header, shouldBeVisible);
Julia Reynolds568585b2016-07-25 08:55:15 -0400660 if (row.view.isShown()) {
Julia Reynolds568585b2016-07-25 08:55:15 -0400661 updateVolumeRowSliderTintH(row, isActive);
662 }
John Spurlockf88d8082015-03-25 18:09:51 -0400663 }
664 }
665
666 private void trimObsoleteH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400667 if (D.BUG) Log.d(TAG, "trimObsoleteH");
Julia Reynolds568585b2016-07-25 08:55:15 -0400668 for (int i = mRows.size() - 1; i >= 0; i--) {
John Spurlockf88d8082015-03-25 18:09:51 -0400669 final VolumeRow row = mRows.get(i);
670 if (row.ss == null || !row.ss.dynamic) continue;
671 if (!mDynamic.get(row.stream)) {
672 mRows.remove(i);
Julia Reynolds568585b2016-07-25 08:55:15 -0400673 mDialogRowsView.removeView(row.view);
John Spurlockf88d8082015-03-25 18:09:51 -0400674 }
675 }
676 }
677
678 private void onStateChangedH(State state) {
John Spurlock22def3d2015-06-17 11:56:12 -0400679 final boolean animating = mMotion.isAnimating();
680 if (D.BUG) Log.d(TAG, "onStateChangedH animating=" + animating);
John Spurlockf88d8082015-03-25 18:09:51 -0400681 mState = state;
John Spurlock22def3d2015-06-17 11:56:12 -0400682 if (animating) {
683 mPendingStateChanged = true;
684 return;
685 }
John Spurlockf88d8082015-03-25 18:09:51 -0400686 mDynamic.clear();
687 // add any new dynamic rows
688 for (int i = 0; i < state.states.size(); i++) {
689 final int stream = state.states.keyAt(i);
690 final StreamState ss = state.states.valueAt(i);
691 if (!ss.dynamic) continue;
692 mDynamic.put(stream, true);
693 if (findRow(stream) == null) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500694 addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true,
695 true);
John Spurlockf88d8082015-03-25 18:09:51 -0400696 }
697 }
698
699 if (mActiveStream != state.activeStream) {
Beverlyfa4c8e72017-08-10 14:55:34 -0400700 mPrevActiveStream = mActiveStream;
John Spurlockf88d8082015-03-25 18:09:51 -0400701 mActiveStream = state.activeStream;
Julia Reynolds568585b2016-07-25 08:55:15 -0400702 updateRowsH(getActiveRow());
John Spurlockf88d8082015-03-25 18:09:51 -0400703 rescheduleTimeoutH();
704 }
705 for (VolumeRow row : mRows) {
706 updateVolumeRowH(row);
707 }
708 updateFooterH();
709 }
710
John Spurlockf88d8082015-03-25 18:09:51 -0400711 private void updateFooterH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400712 if (D.BUG) Log.d(TAG, "updateFooterH");
713 final boolean wasVisible = mZenFooter.getVisibility() == View.VISIBLE;
Julia Reynolds6aa83b42015-09-08 13:41:45 -0400714 final boolean visible = mState.zenMode != Global.ZEN_MODE_OFF
Jason Monke138f552016-01-18 09:21:45 -0500715 && (mAudioManager.isStreamAffectedByRingerMode(mActiveStream) || mExpanded)
Jason Monkaa911b32016-02-10 21:01:30 -0500716 && !mZenPanel.isEditing();
Beverlye421abe2017-08-22 14:47:28 -0400717
Beverly7db7e052017-08-23 13:05:42 -0400718 TransitionManager.endTransitions(mDialogView);
Jason Monk98ad83c2017-06-13 14:03:45 -0400719 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
720 if (wasVisible != visible && !visible) {
721 prepareForCollapse();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400722 }
Jason Monk98ad83c2017-06-13 14:03:45 -0400723 Util.setVisOrGone(mZenFooter, visible);
724 mZenFooter.update();
Jason Monke138f552016-01-18 09:21:45 -0500725
726 final boolean fullWasVisible = mZenPanel.getVisibility() == View.VISIBLE;
Jason Monkaa911b32016-02-10 21:01:30 -0500727 final boolean fullVisible = mShowFullZen && !visible;
Julia Reynoldsd7a00aa2017-02-16 15:01:36 -0500728 if (fullWasVisible != fullVisible) {
729 Util.setVisOrGone(mZenPanel, fullVisible);
730 if (fullVisible) {
731 mZenPanel.setZenState(mState.zenMode);
732 mZenPanel.setDoneListener(new OnClickListener() {
733 @Override
734 public void onClick(View v) {
735 mHandler.sendEmptyMessage(H.UPDATE_FOOTER);
736 }
737 });
738 }
Jason Monkaa911b32016-02-10 21:01:30 -0500739 }
John Spurlockf88d8082015-03-25 18:09:51 -0400740 }
741
742 private void updateVolumeRowH(VolumeRow row) {
John Spurlock22def3d2015-06-17 11:56:12 -0400743 if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400744 if (mState == null) return;
745 final StreamState ss = mState.states.get(row.stream);
746 if (ss == null) return;
747 row.ss = ss;
748 if (ss.level > 0) {
749 row.lastAudibleLevel = ss.level;
750 }
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400751 if (ss.level == row.requestedLevel) {
752 row.requestedLevel = -1;
753 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500754 final boolean isA11yStream = row.stream == AudioManager.STREAM_ACCESSIBILITY;
John Spurlockf88d8082015-03-25 18:09:51 -0400755 final boolean isRingStream = row.stream == AudioManager.STREAM_RING;
756 final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM;
John Spurlockd9c75db2015-04-28 11:19:13 -0400757 final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM;
758 final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC;
John Spurlockf88d8082015-03-25 18:09:51 -0400759 final boolean isRingVibrate = isRingStream
760 && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE;
John Spurlockd9c75db2015-04-28 11:19:13 -0400761 final boolean isRingSilent = isRingStream
762 && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT;
763 final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS;
764 final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
John Spurlockd9c75db2015-04-28 11:19:13 -0400765 final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream)
766 : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream)
767 : false;
John Spurlockf88d8082015-03-25 18:09:51 -0400768
769 // update slider max
Julia Reynolds4b006a82016-04-20 16:09:06 -0400770 final int max = ss.levelMax * 100;
John Spurlockf88d8082015-03-25 18:09:51 -0400771 if (max != row.slider.getMax()) {
772 row.slider.setMax(max);
773 }
774
John Spurlockf88d8082015-03-25 18:09:51 -0400775 // update header text
Julia Reynolds03c548f2016-12-14 15:02:38 -0500776 Util.setText(row.header, getStreamLabelH(ss));
Lucas Dupin421b20c2017-08-01 10:36:10 -0700777 row.slider.setContentDescription(row.header.getText());
Julia Reynolds03c548f2016-12-14 15:02:38 -0500778 mConfigurableTexts.add(row.header, ss.name);
John Spurlockf88d8082015-03-25 18:09:51 -0400779
780 // update icon
John Spurlockd9c75db2015-04-28 11:19:13 -0400781 final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted;
John Spurlockf88d8082015-03-25 18:09:51 -0400782 row.icon.setEnabled(iconEnabled);
783 row.icon.setAlpha(iconEnabled ? 1 : 0.5f);
784 final int iconRes =
John Spurlock76b52b32015-04-03 00:00:12 -0400785 isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
John Spurlockd9c75db2015-04-28 11:19:13 -0400786 : isRingSilent || zenMuted ? row.cachedIconRes
John Spurlockf88d8082015-03-25 18:09:51 -0400787 : ss.routedToBluetooth ?
John Spurlockf15ef112015-04-09 22:04:14 -0400788 (ss.muted ? R.drawable.ic_volume_media_bt_mute
789 : R.drawable.ic_volume_media_bt)
John Spurlock76b52b32015-04-03 00:00:12 -0400790 : mAutomute && ss.level == 0 ? row.iconMuteRes
John Spurlockf88d8082015-03-25 18:09:51 -0400791 : (ss.muted ? row.iconMuteRes : row.iconRes);
792 if (iconRes != row.cachedIconRes) {
793 if (row.cachedIconRes != 0 && isRingVibrate) {
794 mController.vibrate();
795 }
796 row.cachedIconRes = iconRes;
797 row.icon.setImageResource(iconRes);
798 }
799 row.iconState =
800 iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE
John Spurlockf15ef112015-04-09 22:04:14 -0400801 : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400802 ? Events.ICON_STATE_MUTE
John Spurlockf15ef112015-04-09 22:04:14 -0400803 : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400804 ? Events.ICON_STATE_UNMUTE
805 : Events.ICON_STATE_UNKNOWN;
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400806 if (iconEnabled) {
807 if (isRingStream) {
808 if (isRingVibrate) {
809 row.icon.setContentDescription(mContext.getString(
810 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500811 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400812 } else {
813 if (mController.hasVibrator()) {
814 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500815 mShowA11yStream
816 ? R.string.volume_stream_content_description_vibrate_a11y
817 : R.string.volume_stream_content_description_vibrate,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500818 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400819 } else {
820 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500821 mShowA11yStream
822 ? R.string.volume_stream_content_description_mute_a11y
823 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500824 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400825 }
826 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500827 } else if (isA11yStream) {
828 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400829 } else {
830 if (ss.muted || mAutomute && ss.level == 0) {
831 row.icon.setContentDescription(mContext.getString(
832 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500833 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400834 } else {
835 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500836 mShowA11yStream
837 ? R.string.volume_stream_content_description_mute_a11y
838 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500839 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400840 }
841 }
842 } else {
Julia Reynolds03c548f2016-12-14 15:02:38 -0500843 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400844 }
John Spurlockf88d8082015-03-25 18:09:51 -0400845
Beverlye421abe2017-08-22 14:47:28 -0400846 // ensure tracking is disabled if zenMuted
847 if (zenMuted) {
848 row.tracking = false;
849 }
850
John Spurlockf88d8082015-03-25 18:09:51 -0400851 // update slider
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400852 final boolean enableSlider = !zenMuted;
Julia Reynolds5f84c192016-11-14 13:13:08 -0500853 final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400854 : row.ss.level;
855 updateVolumeRowSliderH(row, enableSlider, vlevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400856 }
857
John Spurlockecc80582015-05-13 16:23:14 -0400858 private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) {
John Spurlock67a0f852015-06-15 15:35:47 -0400859 if (isActive && mExpanded) {
John Spurlock67a0f852015-06-15 15:35:47 -0400860 row.slider.requestFocus();
John Spurlock67a0f852015-06-15 15:35:47 -0400861 }
John Spurlockecc80582015-05-13 16:23:14 -0400862 final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint
863 : mInactiveSliderTint;
864 if (tint == row.cachedSliderTint) return;
865 row.cachedSliderTint = tint;
866 row.slider.setProgressTintList(tint);
867 row.slider.setThumbTintList(tint);
868 }
869
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400870 private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) {
871 row.slider.setEnabled(enable);
John Spurlockecc80582015-05-13 16:23:14 -0400872 updateVolumeRowSliderTintH(row, row.stream == mActiveStream);
John Spurlockf88d8082015-03-25 18:09:51 -0400873 if (row.tracking) {
874 return; // don't update if user is sliding
875 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400876 final int progress = row.slider.getProgress();
877 final int level = getImpliedLevel(row.slider, progress);
John Spurlockf88d8082015-03-25 18:09:51 -0400878 final boolean rowVisible = row.view.getVisibility() == View.VISIBLE;
879 final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt)
880 < USER_ATTEMPT_GRACE_PERIOD;
881 mHandler.removeMessages(H.RECHECK, row);
882 if (mShowing && rowVisible && inGracePeriod) {
883 if (D.BUG) Log.d(TAG, "inGracePeriod");
884 mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row),
885 row.userAttempt + USER_ATTEMPT_GRACE_PERIOD);
886 return; // don't update if visible and in grace period
887 }
John Spurlockf88d8082015-03-25 18:09:51 -0400888 if (vlevel == level) {
889 if (mShowing && rowVisible) {
890 return; // don't clamp if visible
891 }
892 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400893 final int newProgress = vlevel * 100;
894 if (progress != newProgress) {
895 if (mShowing && rowVisible) {
896 // animate!
897 if (row.anim != null && row.anim.isRunning()
898 && row.animTargetProgress == newProgress) {
899 return; // already animating to the target progress
900 }
901 // start/update animation
902 if (row.anim == null) {
903 row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress);
904 row.anim.setInterpolator(new DecelerateInterpolator());
905 } else {
906 row.anim.cancel();
907 row.anim.setIntValues(progress, newProgress);
908 }
909 row.animTargetProgress = newProgress;
910 row.anim.setDuration(UPDATE_ANIMATION_DURATION);
911 row.anim.start();
912 } else {
913 // update slider directly to clamped value
914 if (row.anim != null) {
915 row.anim.cancel();
916 }
Julia Reynolds88dabde2016-08-09 13:59:44 -0400917 row.slider.setProgress(newProgress, true);
Julia Reynolds4b006a82016-04-20 16:09:06 -0400918 }
919 }
John Spurlockf88d8082015-03-25 18:09:51 -0400920 }
921
922 private void recheckH(VolumeRow row) {
923 if (row == null) {
924 if (D.BUG) Log.d(TAG, "recheckH ALL");
925 trimObsoleteH();
926 for (VolumeRow r : mRows) {
927 updateVolumeRowH(r);
928 }
929 } else {
930 if (D.BUG) Log.d(TAG, "recheckH " + row.stream);
931 updateVolumeRowH(row);
932 }
933 }
934
935 private void setStreamImportantH(int stream, boolean important) {
936 for (VolumeRow row : mRows) {
937 if (row.stream == stream) {
938 row.important = important;
939 return;
940 }
941 }
942 }
943
John Spurlock76b52b32015-04-03 00:00:12 -0400944 private void showSafetyWarningH(int flags) {
945 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
946 || mShowing) {
947 synchronized (mSafetyWarningLock) {
948 if (mSafetyWarning != null) {
949 return;
950 }
951 mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) {
952 @Override
953 protected void cleanUp() {
954 synchronized (mSafetyWarningLock) {
955 mSafetyWarning = null;
956 }
957 recheckH(null);
958 }
959 };
960 mSafetyWarning.show();
961 }
962 recheckH(null);
963 }
964 rescheduleTimeoutH();
965 }
966
Julia Reynolds03c548f2016-12-14 15:02:38 -0500967 private String getStreamLabelH(StreamState ss) {
968 if (ss.remoteLabel != null) {
969 return ss.remoteLabel;
970 }
971 try {
972 return mContext.getString(ss.name);
973 } catch (Resources.NotFoundException e) {
974 Slog.e(TAG, "Can't find translation for stream " + ss);
975 return "";
976 }
977 }
978
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700979 private AutoTransition getTransition() {
Julia Reynolds24f411d2016-08-11 15:57:10 -0400980 AutoTransition transition = new AutoTransition();
981 transition.setDuration(mExpandButtonAnimationDuration);
982 transition.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
983 transition.addListener(new Transition.TransitionListener() {
984 @Override
985 public void onTransitionStart(Transition transition) {
986 }
987
988 @Override
989 public void onTransitionEnd(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400990 mWindow.setLayout(
991 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -0400992 }
993
994 @Override
995 public void onTransitionCancel(Transition transition) {
996 }
997
998 @Override
999 public void onTransitionPause(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -04001000 mWindow.setLayout(
1001 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -04001002 }
1003
1004 @Override
1005 public void onTransitionResume(Transition transition) {
1006 }
1007 });
1008 return transition;
1009 }
1010
Takayuki Hoshibb0ec552015-12-17 20:50:25 +09001011 private boolean hasTouchFeature() {
1012 final PackageManager pm = mContext.getPackageManager();
1013 return pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
1014 }
1015
John Spurlockf88d8082015-03-25 18:09:51 -04001016 private final VolumeDialogController.Callbacks mControllerCallbackH
1017 = new VolumeDialogController.Callbacks() {
1018 @Override
1019 public void onShowRequested(int reason) {
1020 showH(reason);
1021 }
1022
1023 @Override
1024 public void onDismissRequested(int reason) {
1025 dismissH(reason);
1026 }
1027
John Spurlock76b52b32015-04-03 00:00:12 -04001028 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001029 public void onScreenOff() {
1030 dismissH(Events.DISMISS_REASON_SCREEN_OFF);
1031 }
1032
1033 @Override
1034 public void onStateChanged(State state) {
1035 onStateChangedH(state);
1036 }
1037
1038 @Override
1039 public void onLayoutDirectionChanged(int layoutDirection) {
1040 mDialogView.setLayoutDirection(layoutDirection);
1041 }
1042
1043 @Override
1044 public void onConfigurationChanged() {
Julia Reynoldsc791e052016-02-19 16:02:01 -05001045 Configuration newConfig = mContext.getResources().getConfiguration();
1046 final int density = newConfig.densityDpi;
1047 if (density != mDensity) {
1048 mDialog.dismiss();
Julia Reynolds8d0a4002016-06-14 15:24:39 -04001049 mZenFooter.cleanup();
Julia Reynoldsc791e052016-02-19 16:02:01 -05001050 initDialog();
Julia Reynolds494f0de2016-07-26 12:57:08 -04001051 mDensity = density;
Julia Reynoldsc791e052016-02-19 16:02:01 -05001052 }
Jason Monk98ad83c2017-06-13 14:03:45 -04001053 updateWindowWidthH();
Julia Reynolds03c548f2016-12-14 15:02:38 -05001054 mConfigurableTexts.update();
John Spurlock9ea3a1e2015-05-06 17:12:18 -04001055 mZenFooter.onConfigurationChanged();
John Spurlockf88d8082015-03-25 18:09:51 -04001056 }
1057
1058 @Override
1059 public void onShowVibrateHint() {
1060 if (mSilentMode) {
1061 mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false);
1062 }
1063 }
1064
John Spurlock76b52b32015-04-03 00:00:12 -04001065 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001066 public void onShowSilentHint() {
1067 if (mSilentMode) {
1068 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
1069 }
1070 }
John Spurlock76b52b32015-04-03 00:00:12 -04001071
1072 @Override
1073 public void onShowSafetyWarning(int flags) {
1074 showSafetyWarningH(flags);
1075 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001076
1077 @Override
1078 public void onAccessibilityModeChanged(Boolean showA11yStream) {
1079 boolean show = showA11yStream == null ? false : showA11yStream;
1080 mShowA11yStream = show;
Julia Reynolds74cc6502017-08-08 12:41:04 -04001081 VolumeRow activeRow = getActiveRow();
1082 if (!mShowA11yStream && AudioManager.STREAM_ACCESSIBILITY == activeRow.stream) {
1083 dismissH(Events.DISMISS_STREAM_GONE);
1084 } else {
1085 updateRowsH(activeRow);
1086 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001087
1088 }
John Spurlockf88d8082015-03-25 18:09:51 -04001089 };
1090
Jason Monke138f552016-01-18 09:21:45 -05001091 private final ZenModePanel.Callback mZenPanelCallback = new ZenModePanel.Callback() {
1092 @Override
1093 public void onPrioritySettings() {
1094 mCallback.onZenPrioritySettingsClicked();
1095 }
1096
1097 @Override
1098 public void onInteraction() {
1099 mHandler.sendEmptyMessage(H.RESCHEDULE_TIMEOUT);
1100 }
1101
1102 @Override
1103 public void onExpanded(boolean expanded) {
1104 // noop.
1105 }
1106 };
1107
John Spurlockf88d8082015-03-25 18:09:51 -04001108 private final OnClickListener mClickExpand = new OnClickListener() {
1109 @Override
1110 public void onClick(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001111 if (mExpandButtonAnimationRunning) return;
John Spurlockf88d8082015-03-25 18:09:51 -04001112 final boolean newExpand = !mExpanded;
Chris Wrene565ee62015-06-17 15:24:56 -04001113 Events.writeEvent(mContext, Events.EVENT_EXPAND, newExpand);
Julia Reynolds48242f02016-08-10 11:42:56 -04001114 updateExpandedH(newExpand, false /* dismissing */);
John Spurlockf88d8082015-03-25 18:09:51 -04001115 }
1116 };
1117
John Spurlockf88d8082015-03-25 18:09:51 -04001118 private final class H extends Handler {
1119 private static final int SHOW = 1;
1120 private static final int DISMISS = 2;
1121 private static final int RECHECK = 3;
1122 private static final int RECHECK_ALL = 4;
1123 private static final int SET_STREAM_IMPORTANT = 5;
1124 private static final int RESCHEDULE_TIMEOUT = 6;
John Spurlock22def3d2015-06-17 11:56:12 -04001125 private static final int STATE_CHANGED = 7;
1126 private static final int UPDATE_BOTTOM_MARGIN = 8;
Jason Monkaa911b32016-02-10 21:01:30 -05001127 private static final int UPDATE_FOOTER = 9;
John Spurlockf88d8082015-03-25 18:09:51 -04001128
1129 public H() {
1130 super(Looper.getMainLooper());
1131 }
1132
1133 @Override
1134 public void handleMessage(Message msg) {
1135 switch (msg.what) {
1136 case SHOW: showH(msg.arg1); break;
1137 case DISMISS: dismissH(msg.arg1); break;
1138 case RECHECK: recheckH((VolumeRow) msg.obj); break;
1139 case RECHECK_ALL: recheckH(null); break;
1140 case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break;
1141 case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break;
John Spurlock22def3d2015-06-17 11:56:12 -04001142 case STATE_CHANGED: onStateChangedH(mState); break;
1143 case UPDATE_BOTTOM_MARGIN: updateDialogBottomMarginH(); break;
Jason Monkaa911b32016-02-10 21:01:30 -05001144 case UPDATE_FOOTER: updateFooterH(); break;
John Spurlockf88d8082015-03-25 18:09:51 -04001145 }
1146 }
1147 }
1148
1149 private final class CustomDialog extends Dialog {
1150 public CustomDialog(Context context) {
1151 super(context);
1152 }
1153
1154 @Override
1155 public boolean dispatchTouchEvent(MotionEvent ev) {
1156 rescheduleTimeoutH();
1157 return super.dispatchTouchEvent(ev);
1158 }
1159
1160 @Override
1161 protected void onStop() {
1162 super.onStop();
John Spurlock22def3d2015-06-17 11:56:12 -04001163 final boolean animating = mMotion.isAnimating();
1164 if (D.BUG) Log.d(TAG, "onStop animating=" + animating);
1165 if (animating) {
1166 mPendingRecheckAll = true;
1167 return;
1168 }
John Spurlockf88d8082015-03-25 18:09:51 -04001169 mHandler.sendEmptyMessage(H.RECHECK_ALL);
1170 }
1171
1172 @Override
1173 public boolean onTouchEvent(MotionEvent event) {
1174 if (isShowing()) {
1175 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
1176 dismissH(Events.DISMISS_REASON_TOUCH_OUTSIDE);
1177 return true;
1178 }
1179 }
1180 return false;
1181 }
Julia Reynolds2feab162016-04-21 14:58:52 -04001182
1183 @Override
1184 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
1185 event.setClassName(getClass().getSuperclass().getName());
1186 event.setPackageName(mContext.getPackageName());
1187
1188 ViewGroup.LayoutParams params = getWindow().getAttributes();
Jason Monk98ad83c2017-06-13 14:03:45 -04001189 boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
1190 (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
Julia Reynolds2feab162016-04-21 14:58:52 -04001191 event.setFullScreen(isFullScreen);
1192
1193 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
1194 if (mShowing) {
1195 event.getText().add(mContext.getString(
1196 R.string.volume_dialog_accessibility_shown_message,
Julia Reynolds03c548f2016-12-14 15:02:38 -05001197 getStreamLabelH(getActiveRow().ss)));
Julia Reynolds2feab162016-04-21 14:58:52 -04001198 return true;
1199 }
1200 }
1201 return false;
1202 }
John Spurlockf88d8082015-03-25 18:09:51 -04001203 }
1204
1205 private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener {
1206 private final VolumeRow mRow;
1207
1208 private VolumeSeekBarChangeListener(VolumeRow row) {
1209 mRow = row;
1210 }
1211
1212 @Override
1213 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1214 if (mRow.ss == null) return;
1215 if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
1216 + " onProgressChanged " + progress + " fromUser=" + fromUser);
1217 if (!fromUser) return;
1218 if (mRow.ss.levelMin > 0) {
Julia Reynolds4b006a82016-04-20 16:09:06 -04001219 final int minProgress = mRow.ss.levelMin * 100;
John Spurlockf88d8082015-03-25 18:09:51 -04001220 if (progress < minProgress) {
1221 seekBar.setProgress(minProgress);
Julia Reynolds798b0c32015-09-01 14:35:53 -04001222 progress = minProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001223 }
1224 }
Julia Reynolds4b006a82016-04-20 16:09:06 -04001225 final int userLevel = getImpliedLevel(seekBar, progress);
1226 if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) {
John Spurlockf88d8082015-03-25 18:09:51 -04001227 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001228 if (mRow.requestedLevel != userLevel) {
1229 mController.setStreamVolume(mRow.stream, userLevel);
1230 mRow.requestedLevel = userLevel;
Chris Wrene565ee62015-06-17 15:24:56 -04001231 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream,
Julia Reynolds4b006a82016-04-20 16:09:06 -04001232 userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001233 }
1234 }
1235 }
1236
1237 @Override
1238 public void onStartTrackingTouch(SeekBar seekBar) {
1239 if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream);
1240 mController.setActiveStream(mRow.stream);
1241 mRow.tracking = true;
1242 }
1243
1244 @Override
1245 public void onStopTrackingTouch(SeekBar seekBar) {
1246 if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream);
1247 mRow.tracking = false;
1248 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001249 final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress());
Chris Wrene565ee62015-06-17 15:24:56 -04001250 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001251 if (mRow.ss.level != userLevel) {
1252 mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow),
1253 USER_ATTEMPT_GRACE_PERIOD);
1254 }
1255 }
1256 }
1257
John Spurlockf09838b2015-06-11 16:14:44 -04001258 private final class Accessibility extends AccessibilityDelegate {
John Spurlock5adeabc2015-05-05 14:50:43 -04001259 private boolean mFeedbackEnabled;
1260
1261 public void init() {
John Spurlock5adeabc2015-05-05 14:50:43 -04001262 mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
1263 @Override
1264 public void onViewDetachedFromWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001265 if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001266 }
1267
1268 @Override
1269 public void onViewAttachedToWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001270 if (D.BUG) Log.d(TAG, "onViewAttachedToWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001271 updateFeedbackEnabled();
1272 }
1273 });
John Spurlockf09838b2015-06-11 16:14:44 -04001274 mDialogView.setAccessibilityDelegate(this);
Jason Monk3b9357f2017-07-14 09:40:54 -04001275 mAccessibilityMgr.addAccessibilityStateChangeListener(mListener);
John Spurlock5adeabc2015-05-05 14:50:43 -04001276 updateFeedbackEnabled();
1277 }
1278
Jason Monk3b9357f2017-07-14 09:40:54 -04001279 public void destroy() {
1280 mAccessibilityMgr.removeAccessibilityStateChangeListener(mListener);
1281 }
1282
John Spurlockf09838b2015-06-11 16:14:44 -04001283 @Override
1284 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
1285 AccessibilityEvent event) {
1286 rescheduleTimeoutH();
1287 return super.onRequestSendAccessibilityEvent(host, child, event);
1288 }
1289
John Spurlock5adeabc2015-05-05 14:50:43 -04001290 private void updateFeedbackEnabled() {
1291 mFeedbackEnabled = computeFeedbackEnabled();
1292 }
1293
1294 private boolean computeFeedbackEnabled() {
John Spurlockf09838b2015-06-11 16:14:44 -04001295 // are there any enabled non-generic a11y services?
John Spurlock5adeabc2015-05-05 14:50:43 -04001296 final List<AccessibilityServiceInfo> services =
Julia Reynolds2feab162016-04-21 14:58:52 -04001297 mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK);
John Spurlock5adeabc2015-05-05 14:50:43 -04001298 for (AccessibilityServiceInfo asi : services) {
John Spurlockf09838b2015-06-11 16:14:44 -04001299 if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) {
John Spurlock5adeabc2015-05-05 14:50:43 -04001300 return true;
1301 }
1302 }
1303 return false;
1304 }
Jason Monk3b9357f2017-07-14 09:40:54 -04001305
1306 private final AccessibilityStateChangeListener mListener =
1307 enabled -> updateFeedbackEnabled();
John Spurlock5adeabc2015-05-05 14:50:43 -04001308 }
1309
John Spurlockf88d8082015-03-25 18:09:51 -04001310 private static class VolumeRow {
1311 private View view;
John Spurlockf88d8082015-03-25 18:09:51 -04001312 private TextView header;
1313 private ImageButton icon;
1314 private SeekBar slider;
John Spurlockf88d8082015-03-25 18:09:51 -04001315 private int stream;
1316 private StreamState ss;
1317 private long userAttempt; // last user-driven slider change
1318 private boolean tracking; // tracking slider touch
John Spurlockb0a2e5f2015-06-09 14:41:52 -04001319 private int requestedLevel = -1; // pending user-requested level via progress changed
John Spurlockf88d8082015-03-25 18:09:51 -04001320 private int iconRes;
1321 private int iconMuteRes;
1322 private boolean important;
1323 private int cachedIconRes;
John Spurlockecc80582015-05-13 16:23:14 -04001324 private ColorStateList cachedSliderTint;
John Spurlockf88d8082015-03-25 18:09:51 -04001325 private int iconState; // from Events
Julia Reynolds4b006a82016-04-20 16:09:06 -04001326 private ObjectAnimator anim; // slider progress animation for non-touch-related updates
1327 private int animTargetProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001328 private int lastAudibleLevel = 1;
1329 }
John Spurlockf88d8082015-03-25 18:09:51 -04001330}