blob: 694c72f2fe157b399e03806f4b4f375c8d8886a9 [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() {
301 final ViewGroup.LayoutParams lp = mDialogView.getLayoutParams();
302 final DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
303 if (D.BUG) Log.d(TAG, "updateWindowWidth dm.w=" + dm.widthPixels);
304 int w = dm.widthPixels;
305 final int max = mContext.getResources()
306 .getDimensionPixelSize(R.dimen.volume_dialog_panel_width);
307 if (w > max) {
308 w = max;
309 }
310 lp.width = w;
311 mDialogView.setLayoutParams(lp);
312 }
313
John Spurlockf88d8082015-03-25 18:09:51 -0400314 public void setStreamImportant(int stream, boolean important) {
315 mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget();
316 }
317
John Spurlockf88d8082015-03-25 18:09:51 -0400318 public void setAutomute(boolean automute) {
319 if (mAutomute == automute) return;
320 mAutomute = automute;
321 mHandler.sendEmptyMessage(H.RECHECK_ALL);
322 }
323
324 public void setSilentMode(boolean silentMode) {
325 if (mSilentMode == silentMode) return;
326 mSilentMode = silentMode;
327 mHandler.sendEmptyMessage(H.RECHECK_ALL);
328 }
329
330 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500331 addRow(stream, iconRes, iconMuteRes, important, false);
332 }
333
334 private void addRow(int stream, int iconRes, int iconMuteRes, boolean important,
335 boolean dynamic) {
Julia Reynoldsc791e052016-02-19 16:02:01 -0500336 VolumeRow row = new VolumeRow();
337 initRow(row, stream, iconRes, iconMuteRes, important);
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500338 int rowSize;
339 int viewSize;
340 if (mShowA11yStream && dynamic && (rowSize = mRows.size()) > 1
341 && (viewSize = mDialogRowsView.getChildCount()) > 1) {
342 // A11y Stream should be the last in the list
343 mDialogRowsView.addView(row.view, viewSize - 2);
344 mRows.add(rowSize - 2, row);
345 } else {
346 mDialogRowsView.addView(row.view);
347 mRows.add(row);
348 }
John Spurlockf88d8082015-03-25 18:09:51 -0400349 }
350
Julia Reynoldsc791e052016-02-19 16:02:01 -0500351 private void addExistingRows() {
352 int N = mRows.size();
353 for (int i = 0; i < N; i++) {
354 final VolumeRow row = mRows.get(i);
355 initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important);
Julia Reynolds568585b2016-07-25 08:55:15 -0400356 mDialogRowsView.addView(row.view);
Julia Reynoldsc791e052016-02-19 16:02:01 -0500357 }
358 }
359
Julia Reynoldsc791e052016-02-19 16:02:01 -0500360
John Spurlockf88d8082015-03-25 18:09:51 -0400361 private boolean isAttached() {
362 return mDialogContentView != null && mDialogContentView.isAttachedToWindow();
363 }
364
365 private VolumeRow getActiveRow() {
366 for (VolumeRow row : mRows) {
367 if (row.stream == mActiveStream) {
368 return row;
369 }
370 }
371 return mRows.get(0);
372 }
373
374 private VolumeRow findRow(int stream) {
375 for (VolumeRow row : mRows) {
376 if (row.stream == stream) return row;
377 }
378 return null;
379 }
380
John Spurlockf88d8082015-03-25 18:09:51 -0400381 public void dump(PrintWriter writer) {
Jason Monk782cd672017-03-22 12:50:57 -0400382 writer.println(VolumeDialogImpl.class.getSimpleName() + " state:");
John Spurlockf88d8082015-03-25 18:09:51 -0400383 writer.print(" mShowing: "); writer.println(mShowing);
384 writer.print(" mExpanded: "); writer.println(mExpanded);
John Spurlock22def3d2015-06-17 11:56:12 -0400385 writer.print(" mExpandButtonAnimationRunning: ");
386 writer.println(mExpandButtonAnimationRunning);
John Spurlockf88d8082015-03-25 18:09:51 -0400387 writer.print(" mActiveStream: "); writer.println(mActiveStream);
388 writer.print(" mDynamic: "); writer.println(mDynamic);
John Spurlockf88d8082015-03-25 18:09:51 -0400389 writer.print(" mAutomute: "); writer.println(mAutomute);
390 writer.print(" mSilentMode: "); writer.println(mSilentMode);
John Spurlock22def3d2015-06-17 11:56:12 -0400391 writer.print(" mCollapseTime: "); writer.println(mCollapseTime);
John Spurlock84ee67c2015-05-20 23:33:43 -0400392 writer.print(" mAccessibility.mFeedbackEnabled: ");
393 writer.println(mAccessibility.mFeedbackEnabled);
John Spurlockf88d8082015-03-25 18:09:51 -0400394 }
395
Julia Reynolds4b006a82016-04-20 16:09:06 -0400396 private static int getImpliedLevel(SeekBar seekBar, int progress) {
397 final int m = seekBar.getMax();
398 final int n = m / 100 - 1;
399 final int level = progress == 0 ? 0
400 : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n));
401 return level;
402 }
403
John Spurlockf88d8082015-03-25 18:09:51 -0400404 @SuppressLint("InflateParams")
Julia Reynoldsc791e052016-02-19 16:02:01 -0500405 private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes,
406 boolean important) {
John Spurlockf88d8082015-03-25 18:09:51 -0400407 row.stream = stream;
408 row.iconRes = iconRes;
409 row.iconMuteRes = iconMuteRes;
410 row.important = important;
411 row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
Julia Reynolds568585b2016-07-25 08:55:15 -0400412 row.view.setId(row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400413 row.view.setTag(row);
414 row.header = (TextView) row.view.findViewById(R.id.volume_row_header);
Julia Reynolds568585b2016-07-25 08:55:15 -0400415 row.header.setId(20 * row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400416 row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider);
417 row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
Julia Reynolds5774cf02016-05-26 13:43:38 -0400418 row.anim = null;
John Spurlockf88d8082015-03-25 18:09:51 -0400419
420 // forward events above the slider into the slider
421 row.view.setOnTouchListener(new OnTouchListener() {
422 private final Rect mSliderHitRect = new Rect();
423 private boolean mDragging;
424
425 @SuppressLint("ClickableViewAccessibility")
426 @Override
427 public boolean onTouch(View v, MotionEvent event) {
428 row.slider.getHitRect(mSliderHitRect);
429 if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN
430 && event.getY() < mSliderHitRect.top) {
431 mDragging = true;
432 }
433 if (mDragging) {
434 event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top);
435 row.slider.dispatchTouchEvent(event);
436 if (event.getActionMasked() == MotionEvent.ACTION_UP
437 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
438 mDragging = false;
439 }
440 return true;
441 }
442 return false;
443 }
444 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400445 row.icon = row.view.findViewById(R.id.volume_row_icon);
John Spurlockf88d8082015-03-25 18:09:51 -0400446 row.icon.setImageResource(iconRes);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500447 if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) {
448 row.icon.setOnClickListener(new OnClickListener() {
449 @Override
450 public void onClick(View v) {
451 Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState);
452 mController.setActiveStream(row.stream);
453 if (row.stream == AudioManager.STREAM_RING) {
454 final boolean hasVibrator = mController.hasVibrator();
455 if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {
456 if (hasVibrator) {
457 mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);
458 } else {
459 final boolean wasZero = row.ss.level == 0;
460 mController.setStreamVolume(stream,
461 wasZero ? row.lastAudibleLevel : 0);
462 }
John Spurlockf88d8082015-03-25 18:09:51 -0400463 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500464 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
465 if (row.ss.level == 0) {
466 mController.setStreamVolume(stream, 1);
467 }
John Spurlockf88d8082015-03-25 18:09:51 -0400468 }
469 } else {
Julia Reynolds7c502e02017-03-06 10:19:10 -0500470 final boolean vmute = row.ss.level == row.ss.levelMin;
471 mController.setStreamVolume(stream,
472 vmute ? row.lastAudibleLevel : row.ss.levelMin);
John Spurlockf88d8082015-03-25 18:09:51 -0400473 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500474 row.userAttempt = 0; // reset the grace period, slider updates immediately
John Spurlockf88d8082015-03-25 18:09:51 -0400475 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500476 });
Julia Reynoldse2503dd2017-04-18 11:21:13 -0400477 } else {
478 row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Julia Reynolds7c502e02017-03-06 10:19:10 -0500479 }
John Spurlockf88d8082015-03-25 18:09:51 -0400480 }
481
John Spurlockf88d8082015-03-25 18:09:51 -0400482 public void show(int reason) {
483 mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget();
484 }
485
486 public void dismiss(int reason) {
487 mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget();
488 }
489
John Spurlockf88d8082015-03-25 18:09:51 -0400490 private void showH(int reason) {
John Spurlock22def3d2015-06-17 11:56:12 -0400491 if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]);
John Spurlockf88d8082015-03-25 18:09:51 -0400492 mHandler.removeMessages(H.SHOW);
493 mHandler.removeMessages(H.DISMISS);
494 rescheduleTimeoutH();
495 if (mShowing) return;
496 mShowing = true;
John Spurlock22def3d2015-06-17 11:56:12 -0400497 mMotion.startShow();
Chris Wrene565ee62015-06-17 15:24:56 -0400498 Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
John Spurlockf88d8082015-03-25 18:09:51 -0400499 mController.notifyVisible(true);
500 }
501
502 protected void rescheduleTimeoutH() {
503 mHandler.removeMessages(H.DISMISS);
John Spurlockf09838b2015-06-11 16:14:44 -0400504 final int timeout = computeTimeoutH();
505 mHandler.sendMessageDelayed(mHandler
506 .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout);
John Spurlock5adeabc2015-05-05 14:50:43 -0400507 if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller());
John Spurlockf88d8082015-03-25 18:09:51 -0400508 mController.userActivity();
509 }
510
511 private int computeTimeoutH() {
John Spurlockf09838b2015-06-11 16:14:44 -0400512 if (mAccessibility.mFeedbackEnabled) return 20000;
Jun Mukai3f5d7132015-07-21 14:10:09 -0700513 if (mHovering) return 16000;
John Spurlock76b52b32015-04-03 00:00:12 -0400514 if (mSafetyWarning != null) return 5000;
John Spurlock22def3d2015-06-17 11:56:12 -0400515 if (mExpanded || mExpandButtonAnimationRunning) return 5000;
John Spurlockf88d8082015-03-25 18:09:51 -0400516 if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500;
517 return 3000;
518 }
519
520 protected void dismissH(int reason) {
Jason Monk305e0ba2015-07-06 15:53:59 -0400521 if (mMotion.isAnimating()) {
522 return;
523 }
John Spurlockf88d8082015-03-25 18:09:51 -0400524 mHandler.removeMessages(H.DISMISS);
525 mHandler.removeMessages(H.SHOW);
526 if (!mShowing) return;
527 mShowing = false;
John Spurlock22def3d2015-06-17 11:56:12 -0400528 mMotion.startDismiss(new Runnable() {
529 @Override
530 public void run() {
Julia Reynolds48242f02016-08-10 11:42:56 -0400531 updateExpandedH(false /* expanding */, true /* dismissing */);
John Spurlock22def3d2015-06-17 11:56:12 -0400532 }
533 });
Julia Reynolds2feab162016-04-21 14:58:52 -0400534 if (mAccessibilityMgr.isEnabled()) {
535 AccessibilityEvent event =
536 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
537 event.setPackageName(mContext.getPackageName());
538 event.setClassName(CustomDialog.class.getSuperclass().getName());
539 event.getText().add(mContext.getString(
540 R.string.volume_dialog_accessibility_dismissed_message));
541 mAccessibilityMgr.sendAccessibilityEvent(event);
542 }
Chris Wrene565ee62015-06-17 15:24:56 -0400543 Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason);
John Spurlockf88d8082015-03-25 18:09:51 -0400544 mController.notifyVisible(false);
John Spurlock76b52b32015-04-03 00:00:12 -0400545 synchronized (mSafetyWarningLock) {
546 if (mSafetyWarning != null) {
547 if (D.BUG) Log.d(TAG, "SafetyWarning dismissed");
548 mSafetyWarning.dismiss();
549 }
550 }
John Spurlockf88d8082015-03-25 18:09:51 -0400551 }
552
John Spurlock22def3d2015-06-17 11:56:12 -0400553 private void updateDialogBottomMarginH() {
Jason Monk98ad83c2017-06-13 14:03:45 -0400554 final long diff = System.currentTimeMillis() - mCollapseTime;
555 final boolean collapsing = mCollapseTime != 0 && diff < getConservativeCollapseDuration();
John Spurlock22def3d2015-06-17 11:56:12 -0400556 final ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) mDialogView.getLayoutParams();
Jason Monk98ad83c2017-06-13 14:03:45 -0400557 final int bottomMargin = collapsing ? mDialogContentView.getHeight() :
John Spurlock22def3d2015-06-17 11:56:12 -0400558 mContext.getResources().getDimensionPixelSize(R.dimen.volume_dialog_margin_bottom);
559 if (bottomMargin != mlp.bottomMargin) {
560 if (D.BUG) Log.d(TAG, "bottomMargin " + mlp.bottomMargin + " -> " + bottomMargin);
561 mlp.bottomMargin = bottomMargin;
562 mDialogView.setLayoutParams(mlp);
563 }
564 }
565
566 private long getConservativeCollapseDuration() {
567 return mExpandButtonAnimationDuration * 3;
568 }
569
570 private void prepareForCollapse() {
571 mHandler.removeMessages(H.UPDATE_BOTTOM_MARGIN);
572 mCollapseTime = System.currentTimeMillis();
573 updateDialogBottomMarginH();
574 mHandler.sendEmptyMessageDelayed(H.UPDATE_BOTTOM_MARGIN, getConservativeCollapseDuration());
575 }
576
Julia Reynolds48242f02016-08-10 11:42:56 -0400577 private void updateExpandedH(final boolean expanded, final boolean dismissing) {
John Spurlockf88d8082015-03-25 18:09:51 -0400578 if (mExpanded == expanded) return;
579 mExpanded = expanded;
John Spurlock22def3d2015-06-17 11:56:12 -0400580 mExpandButtonAnimationRunning = isAttached();
Julia Reynolds568585b2016-07-25 08:55:15 -0400581 if (D.BUG) Log.d(TAG, "updateExpandedH " + expanded);
582 updateExpandButtonH();
583 updateFooterH();
Julia Reynolds568585b2016-07-25 08:55:15 -0400584 TransitionManager.endTransitions(mDialogView);
Julia Reynolds48242f02016-08-10 11:42:56 -0400585 final VolumeRow activeRow = getActiveRow();
586 if (!dismissing) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400587 mWindow.setLayout(mWindow.getAttributes().width, ViewGroup.LayoutParams.MATCH_PARENT);
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700588 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
Julia Reynolds48242f02016-08-10 11:42:56 -0400589 }
Julia Reynolds568585b2016-07-25 08:55:15 -0400590 updateRowsH(activeRow);
591 rescheduleTimeoutH();
592 }
593
594 private void updateExpandButtonH() {
595 if (D.BUG) Log.d(TAG, "updateExpandButtonH");
596 mExpandButton.setClickable(!mExpandButtonAnimationRunning);
597 if (!(mExpandButtonAnimationRunning && isAttached())) {
598 final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
599 : R.drawable.ic_volume_expand_animation;
600 if (hasTouchFeature()) {
601 mExpandButton.setImageResource(res);
602 } else {
603 // if there is no touch feature, show the volume ringer instead
604 mExpandButton.setImageResource(R.drawable.ic_volume_ringer);
605 mExpandButton.setBackgroundResource(0); // remove gray background emphasis
606 }
607 mExpandButton.setContentDescription(mContext.getString(mExpanded ?
608 R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand));
John Spurlock22def3d2015-06-17 11:56:12 -0400609 }
John Spurlock22def3d2015-06-17 11:56:12 -0400610 if (mExpandButtonAnimationRunning) {
John Spurlockf88d8082015-03-25 18:09:51 -0400611 final Drawable d = mExpandButton.getDrawable();
612 if (d instanceof AnimatedVectorDrawable) {
613 // workaround to reset drawable
614 final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) d.getConstantState()
615 .newDrawable();
616 mExpandButton.setImageDrawable(avd);
617 avd.start();
618 mHandler.postDelayed(new Runnable() {
619 @Override
620 public void run() {
John Spurlock22def3d2015-06-17 11:56:12 -0400621 mExpandButtonAnimationRunning = false;
John Spurlockf88d8082015-03-25 18:09:51 -0400622 updateExpandButtonH();
623 rescheduleTimeoutH();
624 }
625 }, mExpandButtonAnimationDuration);
626 }
627 }
John Spurlockf88d8082015-03-25 18:09:51 -0400628 }
629
Beverlyfa4c8e72017-08-10 14:55:34 -0400630 private boolean shouldBeVisibleH(VolumeRow row, VolumeRow activeRow) {
631 boolean isActive = row == activeRow;
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500632 if (row.stream == AudioSystem.STREAM_ACCESSIBILITY) {
633 return mShowA11yStream;
634 }
Beverlyfa4c8e72017-08-10 14:55:34 -0400635
636 // if the active row is accessibility, then continue to display previous
637 // active row since accessibility is dispalyed under it
638 if (activeRow.stream == AudioSystem.STREAM_ACCESSIBILITY &&
639 row.stream == mPrevActiveStream) {
640 return true;
641 }
642
John Spurlockf88d8082015-03-25 18:09:51 -0400643 return mExpanded && row.view.getVisibility() == View.VISIBLE
644 || (mExpanded && (row.important || isActive))
645 || !mExpanded && isActive;
646 }
647
Julia Reynolds568585b2016-07-25 08:55:15 -0400648 private void updateRowsH(final VolumeRow activeRow) {
John Spurlock22def3d2015-06-17 11:56:12 -0400649 if (D.BUG) Log.d(TAG, "updateRowsH");
John Spurlockf88d8082015-03-25 18:09:51 -0400650 if (!mShowing) {
651 trimObsoleteH();
652 }
John Spurlockf88d8082015-03-25 18:09:51 -0400653 // apply changes to all rows
Julia Reynolds568585b2016-07-25 08:55:15 -0400654 for (final VolumeRow row : mRows) {
John Spurlockf88d8082015-03-25 18:09:51 -0400655 final boolean isActive = row == activeRow;
Beverlyfa4c8e72017-08-10 14:55:34 -0400656 final boolean shouldBeVisible = shouldBeVisibleH(row, activeRow);
Julia Reynolds568585b2016-07-25 08:55:15 -0400657 Util.setVisOrGone(row.view, shouldBeVisible);
Jason Monk98ad83c2017-06-13 14:03:45 -0400658 Util.setVisOrGone(row.header, shouldBeVisible);
Julia Reynolds568585b2016-07-25 08:55:15 -0400659 if (row.view.isShown()) {
Julia Reynolds568585b2016-07-25 08:55:15 -0400660 updateVolumeRowSliderTintH(row, isActive);
661 }
John Spurlockf88d8082015-03-25 18:09:51 -0400662 }
663 }
664
665 private void trimObsoleteH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400666 if (D.BUG) Log.d(TAG, "trimObsoleteH");
Julia Reynolds568585b2016-07-25 08:55:15 -0400667 for (int i = mRows.size() - 1; i >= 0; i--) {
John Spurlockf88d8082015-03-25 18:09:51 -0400668 final VolumeRow row = mRows.get(i);
669 if (row.ss == null || !row.ss.dynamic) continue;
670 if (!mDynamic.get(row.stream)) {
671 mRows.remove(i);
Julia Reynolds568585b2016-07-25 08:55:15 -0400672 mDialogRowsView.removeView(row.view);
John Spurlockf88d8082015-03-25 18:09:51 -0400673 }
674 }
675 }
676
677 private void onStateChangedH(State state) {
John Spurlock22def3d2015-06-17 11:56:12 -0400678 final boolean animating = mMotion.isAnimating();
679 if (D.BUG) Log.d(TAG, "onStateChangedH animating=" + animating);
John Spurlockf88d8082015-03-25 18:09:51 -0400680 mState = state;
John Spurlock22def3d2015-06-17 11:56:12 -0400681 if (animating) {
682 mPendingStateChanged = true;
683 return;
684 }
John Spurlockf88d8082015-03-25 18:09:51 -0400685 mDynamic.clear();
686 // add any new dynamic rows
687 for (int i = 0; i < state.states.size(); i++) {
688 final int stream = state.states.keyAt(i);
689 final StreamState ss = state.states.valueAt(i);
690 if (!ss.dynamic) continue;
691 mDynamic.put(stream, true);
692 if (findRow(stream) == null) {
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -0500693 addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true,
694 true);
John Spurlockf88d8082015-03-25 18:09:51 -0400695 }
696 }
697
698 if (mActiveStream != state.activeStream) {
Beverlyfa4c8e72017-08-10 14:55:34 -0400699 mPrevActiveStream = mActiveStream;
John Spurlockf88d8082015-03-25 18:09:51 -0400700 mActiveStream = state.activeStream;
Julia Reynolds568585b2016-07-25 08:55:15 -0400701 updateRowsH(getActiveRow());
John Spurlockf88d8082015-03-25 18:09:51 -0400702 rescheduleTimeoutH();
703 }
704 for (VolumeRow row : mRows) {
705 updateVolumeRowH(row);
706 }
707 updateFooterH();
708 }
709
John Spurlockf88d8082015-03-25 18:09:51 -0400710 private void updateFooterH() {
John Spurlock22def3d2015-06-17 11:56:12 -0400711 if (D.BUG) Log.d(TAG, "updateFooterH");
712 final boolean wasVisible = mZenFooter.getVisibility() == View.VISIBLE;
Julia Reynolds6aa83b42015-09-08 13:41:45 -0400713 final boolean visible = mState.zenMode != Global.ZEN_MODE_OFF
Jason Monke138f552016-01-18 09:21:45 -0500714 && (mAudioManager.isStreamAffectedByRingerMode(mActiveStream) || mExpanded)
Jason Monkaa911b32016-02-10 21:01:30 -0500715 && !mZenPanel.isEditing();
Beverlye421abe2017-08-22 14:47:28 -0400716
Beverly7db7e052017-08-23 13:05:42 -0400717 TransitionManager.endTransitions(mDialogView);
Jason Monk98ad83c2017-06-13 14:03:45 -0400718 TransitionManager.beginDelayedTransition(mDialogView, getTransition());
719 if (wasVisible != visible && !visible) {
720 prepareForCollapse();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400721 }
Jason Monk98ad83c2017-06-13 14:03:45 -0400722 Util.setVisOrGone(mZenFooter, visible);
723 mZenFooter.update();
Jason Monke138f552016-01-18 09:21:45 -0500724
725 final boolean fullWasVisible = mZenPanel.getVisibility() == View.VISIBLE;
Jason Monkaa911b32016-02-10 21:01:30 -0500726 final boolean fullVisible = mShowFullZen && !visible;
Julia Reynoldsd7a00aa2017-02-16 15:01:36 -0500727 if (fullWasVisible != fullVisible) {
728 Util.setVisOrGone(mZenPanel, fullVisible);
729 if (fullVisible) {
730 mZenPanel.setZenState(mState.zenMode);
731 mZenPanel.setDoneListener(new OnClickListener() {
732 @Override
733 public void onClick(View v) {
734 mHandler.sendEmptyMessage(H.UPDATE_FOOTER);
735 }
736 });
737 }
Jason Monkaa911b32016-02-10 21:01:30 -0500738 }
John Spurlockf88d8082015-03-25 18:09:51 -0400739 }
740
741 private void updateVolumeRowH(VolumeRow row) {
John Spurlock22def3d2015-06-17 11:56:12 -0400742 if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream);
John Spurlockf88d8082015-03-25 18:09:51 -0400743 if (mState == null) return;
744 final StreamState ss = mState.states.get(row.stream);
745 if (ss == null) return;
746 row.ss = ss;
747 if (ss.level > 0) {
748 row.lastAudibleLevel = ss.level;
749 }
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400750 if (ss.level == row.requestedLevel) {
751 row.requestedLevel = -1;
752 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500753 final boolean isA11yStream = row.stream == AudioManager.STREAM_ACCESSIBILITY;
John Spurlockf88d8082015-03-25 18:09:51 -0400754 final boolean isRingStream = row.stream == AudioManager.STREAM_RING;
755 final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM;
John Spurlockd9c75db2015-04-28 11:19:13 -0400756 final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM;
757 final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC;
John Spurlockf88d8082015-03-25 18:09:51 -0400758 final boolean isRingVibrate = isRingStream
759 && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE;
John Spurlockd9c75db2015-04-28 11:19:13 -0400760 final boolean isRingSilent = isRingStream
761 && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT;
762 final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS;
763 final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
John Spurlockd9c75db2015-04-28 11:19:13 -0400764 final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream)
765 : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream)
766 : false;
John Spurlockf88d8082015-03-25 18:09:51 -0400767
768 // update slider max
Julia Reynolds4b006a82016-04-20 16:09:06 -0400769 final int max = ss.levelMax * 100;
John Spurlockf88d8082015-03-25 18:09:51 -0400770 if (max != row.slider.getMax()) {
771 row.slider.setMax(max);
772 }
773
John Spurlockf88d8082015-03-25 18:09:51 -0400774 // update header text
Julia Reynolds03c548f2016-12-14 15:02:38 -0500775 Util.setText(row.header, getStreamLabelH(ss));
Lucas Dupin421b20c2017-08-01 10:36:10 -0700776 row.slider.setContentDescription(row.header.getText());
Julia Reynolds03c548f2016-12-14 15:02:38 -0500777 mConfigurableTexts.add(row.header, ss.name);
John Spurlockf88d8082015-03-25 18:09:51 -0400778
779 // update icon
John Spurlockd9c75db2015-04-28 11:19:13 -0400780 final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted;
John Spurlockf88d8082015-03-25 18:09:51 -0400781 row.icon.setEnabled(iconEnabled);
782 row.icon.setAlpha(iconEnabled ? 1 : 0.5f);
783 final int iconRes =
John Spurlock76b52b32015-04-03 00:00:12 -0400784 isRingVibrate ? R.drawable.ic_volume_ringer_vibrate
John Spurlockd9c75db2015-04-28 11:19:13 -0400785 : isRingSilent || zenMuted ? row.cachedIconRes
John Spurlockf88d8082015-03-25 18:09:51 -0400786 : ss.routedToBluetooth ?
John Spurlockf15ef112015-04-09 22:04:14 -0400787 (ss.muted ? R.drawable.ic_volume_media_bt_mute
788 : R.drawable.ic_volume_media_bt)
John Spurlock76b52b32015-04-03 00:00:12 -0400789 : mAutomute && ss.level == 0 ? row.iconMuteRes
John Spurlockf88d8082015-03-25 18:09:51 -0400790 : (ss.muted ? row.iconMuteRes : row.iconRes);
791 if (iconRes != row.cachedIconRes) {
792 if (row.cachedIconRes != 0 && isRingVibrate) {
793 mController.vibrate();
794 }
795 row.cachedIconRes = iconRes;
796 row.icon.setImageResource(iconRes);
797 }
798 row.iconState =
799 iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE
John Spurlockf15ef112015-04-09 22:04:14 -0400800 : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400801 ? Events.ICON_STATE_MUTE
John Spurlockf15ef112015-04-09 22:04:14 -0400802 : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes)
John Spurlockf88d8082015-03-25 18:09:51 -0400803 ? Events.ICON_STATE_UNMUTE
804 : Events.ICON_STATE_UNKNOWN;
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400805 if (iconEnabled) {
806 if (isRingStream) {
807 if (isRingVibrate) {
808 row.icon.setContentDescription(mContext.getString(
809 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500810 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400811 } else {
812 if (mController.hasVibrator()) {
813 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500814 mShowA11yStream
815 ? R.string.volume_stream_content_description_vibrate_a11y
816 : R.string.volume_stream_content_description_vibrate,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500817 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400818 } else {
819 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500820 mShowA11yStream
821 ? R.string.volume_stream_content_description_mute_a11y
822 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500823 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400824 }
825 }
Julia Reynolds7c502e02017-03-06 10:19:10 -0500826 } else if (isA11yStream) {
827 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400828 } else {
829 if (ss.muted || mAutomute && ss.level == 0) {
830 row.icon.setContentDescription(mContext.getString(
831 R.string.volume_stream_content_description_unmute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500832 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400833 } else {
834 row.icon.setContentDescription(mContext.getString(
Julia Reynolds7c502e02017-03-06 10:19:10 -0500835 mShowA11yStream
836 ? R.string.volume_stream_content_description_mute_a11y
837 : R.string.volume_stream_content_description_mute,
Julia Reynolds03c548f2016-12-14 15:02:38 -0500838 getStreamLabelH(ss)));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400839 }
840 }
841 } else {
Julia Reynolds03c548f2016-12-14 15:02:38 -0500842 row.icon.setContentDescription(getStreamLabelH(ss));
Julia Reynoldsdfe0b4d2016-04-08 17:14:15 -0400843 }
John Spurlockf88d8082015-03-25 18:09:51 -0400844
Beverlye421abe2017-08-22 14:47:28 -0400845 // ensure tracking is disabled if zenMuted
846 if (zenMuted) {
847 row.tracking = false;
848 }
849
John Spurlockf88d8082015-03-25 18:09:51 -0400850 // update slider
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400851 final boolean enableSlider = !zenMuted;
Julia Reynolds5f84c192016-11-14 13:13:08 -0500852 final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400853 : row.ss.level;
854 updateVolumeRowSliderH(row, enableSlider, vlevel);
John Spurlockf88d8082015-03-25 18:09:51 -0400855 }
856
John Spurlockecc80582015-05-13 16:23:14 -0400857 private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) {
John Spurlock67a0f852015-06-15 15:35:47 -0400858 if (isActive && mExpanded) {
John Spurlock67a0f852015-06-15 15:35:47 -0400859 row.slider.requestFocus();
John Spurlock67a0f852015-06-15 15:35:47 -0400860 }
John Spurlockecc80582015-05-13 16:23:14 -0400861 final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint
862 : mInactiveSliderTint;
863 if (tint == row.cachedSliderTint) return;
864 row.cachedSliderTint = tint;
865 row.slider.setProgressTintList(tint);
866 row.slider.setThumbTintList(tint);
867 }
868
John Spurlockb0a2e5f2015-06-09 14:41:52 -0400869 private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) {
870 row.slider.setEnabled(enable);
John Spurlockecc80582015-05-13 16:23:14 -0400871 updateVolumeRowSliderTintH(row, row.stream == mActiveStream);
John Spurlockf88d8082015-03-25 18:09:51 -0400872 if (row.tracking) {
873 return; // don't update if user is sliding
874 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400875 final int progress = row.slider.getProgress();
876 final int level = getImpliedLevel(row.slider, progress);
John Spurlockf88d8082015-03-25 18:09:51 -0400877 final boolean rowVisible = row.view.getVisibility() == View.VISIBLE;
878 final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt)
879 < USER_ATTEMPT_GRACE_PERIOD;
880 mHandler.removeMessages(H.RECHECK, row);
881 if (mShowing && rowVisible && inGracePeriod) {
882 if (D.BUG) Log.d(TAG, "inGracePeriod");
883 mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row),
884 row.userAttempt + USER_ATTEMPT_GRACE_PERIOD);
885 return; // don't update if visible and in grace period
886 }
John Spurlockf88d8082015-03-25 18:09:51 -0400887 if (vlevel == level) {
888 if (mShowing && rowVisible) {
889 return; // don't clamp if visible
890 }
891 }
Julia Reynolds4b006a82016-04-20 16:09:06 -0400892 final int newProgress = vlevel * 100;
893 if (progress != newProgress) {
894 if (mShowing && rowVisible) {
895 // animate!
896 if (row.anim != null && row.anim.isRunning()
897 && row.animTargetProgress == newProgress) {
898 return; // already animating to the target progress
899 }
900 // start/update animation
901 if (row.anim == null) {
902 row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress);
903 row.anim.setInterpolator(new DecelerateInterpolator());
904 } else {
905 row.anim.cancel();
906 row.anim.setIntValues(progress, newProgress);
907 }
908 row.animTargetProgress = newProgress;
909 row.anim.setDuration(UPDATE_ANIMATION_DURATION);
910 row.anim.start();
911 } else {
912 // update slider directly to clamped value
913 if (row.anim != null) {
914 row.anim.cancel();
915 }
Julia Reynolds88dabde2016-08-09 13:59:44 -0400916 row.slider.setProgress(newProgress, true);
Julia Reynolds4b006a82016-04-20 16:09:06 -0400917 }
918 }
John Spurlockf88d8082015-03-25 18:09:51 -0400919 }
920
921 private void recheckH(VolumeRow row) {
922 if (row == null) {
923 if (D.BUG) Log.d(TAG, "recheckH ALL");
924 trimObsoleteH();
925 for (VolumeRow r : mRows) {
926 updateVolumeRowH(r);
927 }
928 } else {
929 if (D.BUG) Log.d(TAG, "recheckH " + row.stream);
930 updateVolumeRowH(row);
931 }
932 }
933
934 private void setStreamImportantH(int stream, boolean important) {
935 for (VolumeRow row : mRows) {
936 if (row.stream == stream) {
937 row.important = important;
938 return;
939 }
940 }
941 }
942
John Spurlock76b52b32015-04-03 00:00:12 -0400943 private void showSafetyWarningH(int flags) {
944 if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0
945 || mShowing) {
946 synchronized (mSafetyWarningLock) {
947 if (mSafetyWarning != null) {
948 return;
949 }
950 mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) {
951 @Override
952 protected void cleanUp() {
953 synchronized (mSafetyWarningLock) {
954 mSafetyWarning = null;
955 }
956 recheckH(null);
957 }
958 };
959 mSafetyWarning.show();
960 }
961 recheckH(null);
962 }
963 rescheduleTimeoutH();
964 }
965
Julia Reynolds03c548f2016-12-14 15:02:38 -0500966 private String getStreamLabelH(StreamState ss) {
967 if (ss.remoteLabel != null) {
968 return ss.remoteLabel;
969 }
970 try {
971 return mContext.getString(ss.name);
972 } catch (Resources.NotFoundException e) {
973 Slog.e(TAG, "Can't find translation for stream " + ss);
974 return "";
975 }
976 }
977
Rajeev Kumar5c964a42017-06-13 19:41:35 -0700978 private AutoTransition getTransition() {
Julia Reynolds24f411d2016-08-11 15:57:10 -0400979 AutoTransition transition = new AutoTransition();
980 transition.setDuration(mExpandButtonAnimationDuration);
981 transition.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
982 transition.addListener(new Transition.TransitionListener() {
983 @Override
984 public void onTransitionStart(Transition transition) {
985 }
986
987 @Override
988 public void onTransitionEnd(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400989 mWindow.setLayout(
990 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -0400991 }
992
993 @Override
994 public void onTransitionCancel(Transition transition) {
995 }
996
997 @Override
998 public void onTransitionPause(Transition transition) {
Jason Monk98ad83c2017-06-13 14:03:45 -0400999 mWindow.setLayout(
1000 mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
Julia Reynolds24f411d2016-08-11 15:57:10 -04001001 }
1002
1003 @Override
1004 public void onTransitionResume(Transition transition) {
1005 }
1006 });
1007 return transition;
1008 }
1009
Takayuki Hoshibb0ec552015-12-17 20:50:25 +09001010 private boolean hasTouchFeature() {
1011 final PackageManager pm = mContext.getPackageManager();
1012 return pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
1013 }
1014
John Spurlockf88d8082015-03-25 18:09:51 -04001015 private final VolumeDialogController.Callbacks mControllerCallbackH
1016 = new VolumeDialogController.Callbacks() {
1017 @Override
1018 public void onShowRequested(int reason) {
1019 showH(reason);
1020 }
1021
1022 @Override
1023 public void onDismissRequested(int reason) {
1024 dismissH(reason);
1025 }
1026
John Spurlock76b52b32015-04-03 00:00:12 -04001027 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001028 public void onScreenOff() {
1029 dismissH(Events.DISMISS_REASON_SCREEN_OFF);
1030 }
1031
1032 @Override
1033 public void onStateChanged(State state) {
1034 onStateChangedH(state);
1035 }
1036
1037 @Override
1038 public void onLayoutDirectionChanged(int layoutDirection) {
1039 mDialogView.setLayoutDirection(layoutDirection);
1040 }
1041
1042 @Override
1043 public void onConfigurationChanged() {
Julia Reynoldsc791e052016-02-19 16:02:01 -05001044 Configuration newConfig = mContext.getResources().getConfiguration();
1045 final int density = newConfig.densityDpi;
1046 if (density != mDensity) {
1047 mDialog.dismiss();
Julia Reynolds8d0a4002016-06-14 15:24:39 -04001048 mZenFooter.cleanup();
Julia Reynoldsc791e052016-02-19 16:02:01 -05001049 initDialog();
Julia Reynolds494f0de2016-07-26 12:57:08 -04001050 mDensity = density;
Julia Reynoldsc791e052016-02-19 16:02:01 -05001051 }
Jason Monk98ad83c2017-06-13 14:03:45 -04001052 updateWindowWidthH();
Julia Reynolds03c548f2016-12-14 15:02:38 -05001053 mConfigurableTexts.update();
John Spurlock9ea3a1e2015-05-06 17:12:18 -04001054 mZenFooter.onConfigurationChanged();
John Spurlockf88d8082015-03-25 18:09:51 -04001055 }
1056
1057 @Override
1058 public void onShowVibrateHint() {
1059 if (mSilentMode) {
1060 mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false);
1061 }
1062 }
1063
John Spurlock76b52b32015-04-03 00:00:12 -04001064 @Override
John Spurlockf88d8082015-03-25 18:09:51 -04001065 public void onShowSilentHint() {
1066 if (mSilentMode) {
1067 mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);
1068 }
1069 }
John Spurlock76b52b32015-04-03 00:00:12 -04001070
1071 @Override
1072 public void onShowSafetyWarning(int flags) {
1073 showSafetyWarningH(flags);
1074 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001075
1076 @Override
1077 public void onAccessibilityModeChanged(Boolean showA11yStream) {
1078 boolean show = showA11yStream == null ? false : showA11yStream;
1079 mShowA11yStream = show;
Julia Reynolds74cc6502017-08-08 12:41:04 -04001080 VolumeRow activeRow = getActiveRow();
1081 if (!mShowA11yStream && AudioManager.STREAM_ACCESSIBILITY == activeRow.stream) {
1082 dismissH(Events.DISMISS_STREAM_GONE);
1083 } else {
1084 updateRowsH(activeRow);
1085 }
Julia Reynoldsdbfb40f2017-01-23 11:05:57 -05001086
1087 }
John Spurlockf88d8082015-03-25 18:09:51 -04001088 };
1089
Jason Monke138f552016-01-18 09:21:45 -05001090 private final ZenModePanel.Callback mZenPanelCallback = new ZenModePanel.Callback() {
1091 @Override
1092 public void onPrioritySettings() {
1093 mCallback.onZenPrioritySettingsClicked();
1094 }
1095
1096 @Override
1097 public void onInteraction() {
1098 mHandler.sendEmptyMessage(H.RESCHEDULE_TIMEOUT);
1099 }
1100
1101 @Override
1102 public void onExpanded(boolean expanded) {
1103 // noop.
1104 }
1105 };
1106
John Spurlockf88d8082015-03-25 18:09:51 -04001107 private final OnClickListener mClickExpand = new OnClickListener() {
1108 @Override
1109 public void onClick(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001110 if (mExpandButtonAnimationRunning) return;
John Spurlockf88d8082015-03-25 18:09:51 -04001111 final boolean newExpand = !mExpanded;
Chris Wrene565ee62015-06-17 15:24:56 -04001112 Events.writeEvent(mContext, Events.EVENT_EXPAND, newExpand);
Julia Reynolds48242f02016-08-10 11:42:56 -04001113 updateExpandedH(newExpand, false /* dismissing */);
John Spurlockf88d8082015-03-25 18:09:51 -04001114 }
1115 };
1116
John Spurlockf88d8082015-03-25 18:09:51 -04001117 private final class H extends Handler {
1118 private static final int SHOW = 1;
1119 private static final int DISMISS = 2;
1120 private static final int RECHECK = 3;
1121 private static final int RECHECK_ALL = 4;
1122 private static final int SET_STREAM_IMPORTANT = 5;
1123 private static final int RESCHEDULE_TIMEOUT = 6;
John Spurlock22def3d2015-06-17 11:56:12 -04001124 private static final int STATE_CHANGED = 7;
1125 private static final int UPDATE_BOTTOM_MARGIN = 8;
Jason Monkaa911b32016-02-10 21:01:30 -05001126 private static final int UPDATE_FOOTER = 9;
John Spurlockf88d8082015-03-25 18:09:51 -04001127
1128 public H() {
1129 super(Looper.getMainLooper());
1130 }
1131
1132 @Override
1133 public void handleMessage(Message msg) {
1134 switch (msg.what) {
1135 case SHOW: showH(msg.arg1); break;
1136 case DISMISS: dismissH(msg.arg1); break;
1137 case RECHECK: recheckH((VolumeRow) msg.obj); break;
1138 case RECHECK_ALL: recheckH(null); break;
1139 case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break;
1140 case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break;
John Spurlock22def3d2015-06-17 11:56:12 -04001141 case STATE_CHANGED: onStateChangedH(mState); break;
1142 case UPDATE_BOTTOM_MARGIN: updateDialogBottomMarginH(); break;
Jason Monkaa911b32016-02-10 21:01:30 -05001143 case UPDATE_FOOTER: updateFooterH(); break;
John Spurlockf88d8082015-03-25 18:09:51 -04001144 }
1145 }
1146 }
1147
1148 private final class CustomDialog extends Dialog {
1149 public CustomDialog(Context context) {
1150 super(context);
1151 }
1152
1153 @Override
1154 public boolean dispatchTouchEvent(MotionEvent ev) {
1155 rescheduleTimeoutH();
1156 return super.dispatchTouchEvent(ev);
1157 }
1158
1159 @Override
1160 protected void onStop() {
1161 super.onStop();
John Spurlock22def3d2015-06-17 11:56:12 -04001162 final boolean animating = mMotion.isAnimating();
1163 if (D.BUG) Log.d(TAG, "onStop animating=" + animating);
1164 if (animating) {
1165 mPendingRecheckAll = true;
1166 return;
1167 }
John Spurlockf88d8082015-03-25 18:09:51 -04001168 mHandler.sendEmptyMessage(H.RECHECK_ALL);
1169 }
1170
1171 @Override
1172 public boolean onTouchEvent(MotionEvent event) {
1173 if (isShowing()) {
1174 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
1175 dismissH(Events.DISMISS_REASON_TOUCH_OUTSIDE);
1176 return true;
1177 }
1178 }
1179 return false;
1180 }
Julia Reynolds2feab162016-04-21 14:58:52 -04001181
1182 @Override
1183 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
1184 event.setClassName(getClass().getSuperclass().getName());
1185 event.setPackageName(mContext.getPackageName());
1186
1187 ViewGroup.LayoutParams params = getWindow().getAttributes();
Jason Monk98ad83c2017-06-13 14:03:45 -04001188 boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
1189 (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
Julia Reynolds2feab162016-04-21 14:58:52 -04001190 event.setFullScreen(isFullScreen);
1191
1192 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
1193 if (mShowing) {
1194 event.getText().add(mContext.getString(
1195 R.string.volume_dialog_accessibility_shown_message,
Julia Reynolds03c548f2016-12-14 15:02:38 -05001196 getStreamLabelH(getActiveRow().ss)));
Julia Reynolds2feab162016-04-21 14:58:52 -04001197 return true;
1198 }
1199 }
1200 return false;
1201 }
John Spurlockf88d8082015-03-25 18:09:51 -04001202 }
1203
1204 private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener {
1205 private final VolumeRow mRow;
1206
1207 private VolumeSeekBarChangeListener(VolumeRow row) {
1208 mRow = row;
1209 }
1210
1211 @Override
1212 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
1213 if (mRow.ss == null) return;
1214 if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
1215 + " onProgressChanged " + progress + " fromUser=" + fromUser);
1216 if (!fromUser) return;
1217 if (mRow.ss.levelMin > 0) {
Julia Reynolds4b006a82016-04-20 16:09:06 -04001218 final int minProgress = mRow.ss.levelMin * 100;
John Spurlockf88d8082015-03-25 18:09:51 -04001219 if (progress < minProgress) {
1220 seekBar.setProgress(minProgress);
Julia Reynolds798b0c32015-09-01 14:35:53 -04001221 progress = minProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001222 }
1223 }
Julia Reynolds4b006a82016-04-20 16:09:06 -04001224 final int userLevel = getImpliedLevel(seekBar, progress);
1225 if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) {
John Spurlockf88d8082015-03-25 18:09:51 -04001226 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001227 if (mRow.requestedLevel != userLevel) {
1228 mController.setStreamVolume(mRow.stream, userLevel);
1229 mRow.requestedLevel = userLevel;
Chris Wrene565ee62015-06-17 15:24:56 -04001230 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream,
Julia Reynolds4b006a82016-04-20 16:09:06 -04001231 userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001232 }
1233 }
1234 }
1235
1236 @Override
1237 public void onStartTrackingTouch(SeekBar seekBar) {
1238 if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream);
1239 mController.setActiveStream(mRow.stream);
1240 mRow.tracking = true;
1241 }
1242
1243 @Override
1244 public void onStopTrackingTouch(SeekBar seekBar) {
1245 if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream);
1246 mRow.tracking = false;
1247 mRow.userAttempt = SystemClock.uptimeMillis();
Julia Reynolds4b006a82016-04-20 16:09:06 -04001248 final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress());
Chris Wrene565ee62015-06-17 15:24:56 -04001249 Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel);
John Spurlockf88d8082015-03-25 18:09:51 -04001250 if (mRow.ss.level != userLevel) {
1251 mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow),
1252 USER_ATTEMPT_GRACE_PERIOD);
1253 }
1254 }
1255 }
1256
John Spurlockf09838b2015-06-11 16:14:44 -04001257 private final class Accessibility extends AccessibilityDelegate {
John Spurlock5adeabc2015-05-05 14:50:43 -04001258 private boolean mFeedbackEnabled;
1259
1260 public void init() {
John Spurlock5adeabc2015-05-05 14:50:43 -04001261 mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
1262 @Override
1263 public void onViewDetachedFromWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001264 if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001265 }
1266
1267 @Override
1268 public void onViewAttachedToWindow(View v) {
John Spurlock22def3d2015-06-17 11:56:12 -04001269 if (D.BUG) Log.d(TAG, "onViewAttachedToWindow");
John Spurlock5adeabc2015-05-05 14:50:43 -04001270 updateFeedbackEnabled();
1271 }
1272 });
John Spurlockf09838b2015-06-11 16:14:44 -04001273 mDialogView.setAccessibilityDelegate(this);
Jason Monk3b9357f2017-07-14 09:40:54 -04001274 mAccessibilityMgr.addAccessibilityStateChangeListener(mListener);
John Spurlock5adeabc2015-05-05 14:50:43 -04001275 updateFeedbackEnabled();
1276 }
1277
Jason Monk3b9357f2017-07-14 09:40:54 -04001278 public void destroy() {
1279 mAccessibilityMgr.removeAccessibilityStateChangeListener(mListener);
1280 }
1281
John Spurlockf09838b2015-06-11 16:14:44 -04001282 @Override
1283 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
1284 AccessibilityEvent event) {
1285 rescheduleTimeoutH();
1286 return super.onRequestSendAccessibilityEvent(host, child, event);
1287 }
1288
John Spurlock5adeabc2015-05-05 14:50:43 -04001289 private void updateFeedbackEnabled() {
1290 mFeedbackEnabled = computeFeedbackEnabled();
1291 }
1292
1293 private boolean computeFeedbackEnabled() {
John Spurlockf09838b2015-06-11 16:14:44 -04001294 // are there any enabled non-generic a11y services?
John Spurlock5adeabc2015-05-05 14:50:43 -04001295 final List<AccessibilityServiceInfo> services =
Julia Reynolds2feab162016-04-21 14:58:52 -04001296 mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK);
John Spurlock5adeabc2015-05-05 14:50:43 -04001297 for (AccessibilityServiceInfo asi : services) {
John Spurlockf09838b2015-06-11 16:14:44 -04001298 if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) {
John Spurlock5adeabc2015-05-05 14:50:43 -04001299 return true;
1300 }
1301 }
1302 return false;
1303 }
Jason Monk3b9357f2017-07-14 09:40:54 -04001304
1305 private final AccessibilityStateChangeListener mListener =
1306 enabled -> updateFeedbackEnabled();
John Spurlock5adeabc2015-05-05 14:50:43 -04001307 }
1308
John Spurlockf88d8082015-03-25 18:09:51 -04001309 private static class VolumeRow {
1310 private View view;
John Spurlockf88d8082015-03-25 18:09:51 -04001311 private TextView header;
1312 private ImageButton icon;
1313 private SeekBar slider;
John Spurlockf88d8082015-03-25 18:09:51 -04001314 private int stream;
1315 private StreamState ss;
1316 private long userAttempt; // last user-driven slider change
1317 private boolean tracking; // tracking slider touch
John Spurlockb0a2e5f2015-06-09 14:41:52 -04001318 private int requestedLevel = -1; // pending user-requested level via progress changed
John Spurlockf88d8082015-03-25 18:09:51 -04001319 private int iconRes;
1320 private int iconMuteRes;
1321 private boolean important;
1322 private int cachedIconRes;
John Spurlockecc80582015-05-13 16:23:14 -04001323 private ColorStateList cachedSliderTint;
John Spurlockf88d8082015-03-25 18:09:51 -04001324 private int iconState; // from Events
Julia Reynolds4b006a82016-04-20 16:09:06 -04001325 private ObjectAnimator anim; // slider progress animation for non-touch-related updates
1326 private int animTargetProgress;
John Spurlockf88d8082015-03-25 18:09:51 -04001327 private int lastAudibleLevel = 1;
1328 }
John Spurlockf88d8082015-03-25 18:09:51 -04001329}