John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.volume; |
| 18 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 19 | import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_ALL_MASK; |
| 20 | import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_GENERIC; |
| 21 | |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 22 | import static com.android.systemui.volume.Events.DISMISS_REASON_TOUCH_OUTSIDE; |
| 23 | |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 25 | import android.animation.ObjectAnimator; |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 26 | import android.annotation.NonNull; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 27 | import android.annotation.SuppressLint; |
| 28 | import android.app.Dialog; |
| 29 | import android.app.KeyguardManager; |
| 30 | import android.content.Context; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 31 | import android.content.DialogInterface; |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 32 | import android.content.res.ColorStateList; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 33 | import android.content.res.Configuration; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 34 | import android.content.res.Resources; |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 35 | import android.graphics.Color; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 36 | import android.graphics.Rect; |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 37 | import android.graphics.drawable.ColorDrawable; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 38 | import android.media.AudioManager; |
| 39 | import android.media.AudioSystem; |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 40 | import android.os.Debug; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 41 | import android.os.Handler; |
| 42 | import android.os.Looper; |
| 43 | import android.os.Message; |
| 44 | import android.os.SystemClock; |
| 45 | import android.provider.Settings.Global; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 46 | import android.util.Log; |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 47 | import android.util.Slog; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 48 | import android.util.SparseBooleanArray; |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 49 | import android.view.ContextThemeWrapper; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 50 | import android.view.MotionEvent; |
| 51 | import android.view.View; |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 52 | import android.view.View.AccessibilityDelegate; |
| 53 | import android.view.View.OnAttachStateChangeListener; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 54 | import android.view.View.OnClickListener; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 55 | import android.view.View.OnTouchListener; |
| 56 | import android.view.ViewGroup; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 57 | import android.view.Window; |
| 58 | import android.view.WindowManager; |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 59 | import android.view.accessibility.AccessibilityEvent; |
| 60 | import android.view.accessibility.AccessibilityManager; |
| 61 | import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 62 | import android.view.animation.DecelerateInterpolator; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 63 | import android.widget.ImageButton; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 64 | import android.widget.SeekBar; |
| 65 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 66 | import android.widget.TextView; |
Lucas Dupin | 7224c1e | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 67 | |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 68 | import com.android.settingslib.Utils; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 69 | import com.android.systemui.Dependency; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 70 | import com.android.systemui.HardwareUiLayout; |
| 71 | import com.android.systemui.Interpolators; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 72 | import com.android.systemui.R; |
Rajeev Kumar | 5c964a4 | 2017-06-13 19:41:35 -0700 | [diff] [blame] | 73 | import com.android.systemui.plugins.VolumeDialog; |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 74 | import com.android.systemui.plugins.VolumeDialogController; |
| 75 | import com.android.systemui.plugins.VolumeDialogController.State; |
| 76 | import com.android.systemui.plugins.VolumeDialogController.StreamState; |
Lucas Dupin | 7224c1e | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 77 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 78 | import java.io.PrintWriter; |
| 79 | import java.util.ArrayList; |
| 80 | import java.util.List; |
| 81 | |
| 82 | /** |
| 83 | * Visual presentation of the volume dialog. |
| 84 | * |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 85 | * A client of VolumeDialogControllerImpl and its state model. |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 86 | * |
| 87 | * Methods ending in "H" must be called on the (ui) handler. |
| 88 | */ |
Julia Reynolds | 66d2894 | 2017-11-07 09:43:54 -0500 | [diff] [blame] | 89 | public class VolumeDialogImpl implements VolumeDialog { |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 90 | private static final String TAG = Util.logTag(VolumeDialogImpl.class); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 91 | |
| 92 | private static final long USER_ATTEMPT_GRACE_PERIOD = 1000; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 93 | private static final int UPDATE_ANIMATION_DURATION = 80; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 94 | |
| 95 | private final Context mContext; |
| 96 | private final H mHandler = new H(); |
Rajeev Kumar | 5c964a4 | 2017-06-13 19:41:35 -0700 | [diff] [blame] | 97 | private final VolumeDialogController mController; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 98 | |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 99 | private Window mWindow; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 100 | private HardwareUiLayout mHardwareLayout; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 101 | private CustomDialog mDialog; |
| 102 | private ViewGroup mDialogView; |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 103 | private ViewGroup mDialogRowsView; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 104 | private ViewGroup mDialogContentView; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 105 | private final List<VolumeRow> mRows = new ArrayList<>(); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 106 | private ConfigurableTexts mConfigurableTexts; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 107 | private final SparseBooleanArray mDynamic = new SparseBooleanArray(); |
| 108 | private final KeyguardManager mKeyguard; |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 109 | private final AccessibilityManager mAccessibilityMgr; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 110 | private final Object mSafetyWarningLock = new Object(); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 111 | private final Accessibility mAccessibility = new Accessibility(); |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 112 | private final ColorStateList mActiveSliderTint; |
| 113 | private final ColorStateList mInactiveSliderTint; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 114 | |
| 115 | private boolean mShowing; |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 116 | private boolean mShowA11yStream; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 117 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 118 | private int mActiveStream; |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 119 | private int mPrevActiveStream; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 120 | private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE; |
| 121 | private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 122 | private State mState; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 123 | private SafetyWarningDialog mSafetyWarning; |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 124 | private boolean mHovering = false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 125 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 126 | public VolumeDialogImpl(Context context) { |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 127 | mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme); |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 128 | mController = Dependency.get(VolumeDialogController.class); |
| 129 | mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 130 | mAccessibilityMgr = |
| 131 | (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 132 | mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext)); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 133 | mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive); |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public void init(int windowType, Callback callback) { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 137 | initDialog(); |
| 138 | |
| 139 | mAccessibility.init(); |
| 140 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 141 | mController.addCallback(mControllerCallbackH, mHandler); |
| 142 | mController.getState(); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 145 | @Override |
| 146 | public void destroy() { |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 147 | mAccessibility.destroy(); |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 148 | mController.removeCallback(mControllerCallbackH); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 149 | mHandler.removeCallbacksAndMessages(null); |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 152 | private void initDialog() { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 153 | mDialog = new CustomDialog(mContext); |
| 154 | |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 155 | mConfigurableTexts = new ConfigurableTexts(mContext); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 156 | mHovering = false; |
| 157 | mShowing = false; |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 158 | mWindow = mDialog.getWindow(); |
| 159 | mWindow.requestFeature(Window.FEATURE_NO_TITLE); |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 160 | mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 161 | mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 162 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); |
| 163 | mWindow.addFlags( |
| 164 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 165 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 166 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 167 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 168 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 169 | mWindow.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); |
| 170 | mWindow.setWindowAnimations(com.android.internal.R.style.Animation_Toast); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 171 | |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 172 | mDialog.setContentView(R.layout.volume_dialog); |
| 173 | mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog); |
| 174 | mDialogView.setOnHoverListener(new View.OnHoverListener() { |
| 175 | @Override |
| 176 | public boolean onHover(View v, MotionEvent event) { |
| 177 | int action = event.getActionMasked(); |
| 178 | mHovering = (action == MotionEvent.ACTION_HOVER_ENTER) |
| 179 | || (action == MotionEvent.ACTION_HOVER_MOVE); |
| 180 | rescheduleTimeoutH(); |
| 181 | return true; |
| 182 | } |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 183 | }); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 184 | mHardwareLayout = HardwareUiLayout.get(mDialogView); |
| 185 | mHardwareLayout.setOutsideTouchListener(view -> dismiss(DISMISS_REASON_TOUCH_OUTSIDE)); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 186 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 187 | mDialogContentView = mDialog.findViewById(R.id.volume_dialog_content); |
| 188 | mDialogRowsView = mDialogContentView.findViewById(R.id.volume_dialog_rows); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 189 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 190 | if (mRows.isEmpty()) { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 191 | addRow(AudioManager.STREAM_MUSIC, |
| 192 | R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true); |
Muyuan Li | 36007d2 | 2016-06-21 14:21:48 -0700 | [diff] [blame] | 193 | if (!AudioSystem.isSingleVolume(mContext)) { |
| 194 | addRow(AudioManager.STREAM_RING, |
| 195 | R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true); |
Muyuan Li | 36007d2 | 2016-06-21 14:21:48 -0700 | [diff] [blame] | 196 | addRow(AudioManager.STREAM_ALARM, |
| 197 | R.drawable.ic_volume_alarm, R.drawable.ic_volume_alarm_mute, false); |
| 198 | addRow(AudioManager.STREAM_VOICE_CALL, |
| 199 | R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, false); |
| 200 | addRow(AudioManager.STREAM_BLUETOOTH_SCO, |
| 201 | R.drawable.ic_volume_bt_sco, R.drawable.ic_volume_bt_sco, false); |
| 202 | addRow(AudioManager.STREAM_SYSTEM, |
| 203 | R.drawable.ic_volume_system, R.drawable.ic_volume_system_mute, false); |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 204 | addRow(AudioManager.STREAM_ACCESSIBILITY, R.drawable.ic_volume_accessibility, |
| 205 | R.drawable.ic_volume_accessibility, true); |
Muyuan Li | 36007d2 | 2016-06-21 14:21:48 -0700 | [diff] [blame] | 206 | } |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 207 | } else { |
| 208 | addExistingRows(); |
| 209 | } |
Julia Reynolds | 08ce310 | 2017-11-16 09:29:55 -0500 | [diff] [blame^] | 210 | updateRowsH(getActiveRow()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 211 | } |
| 212 | |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 213 | private ColorStateList loadColorStateList(int colorResId) { |
| 214 | return ColorStateList.valueOf(mContext.getColor(colorResId)); |
| 215 | } |
| 216 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 217 | public void setStreamImportant(int stream, boolean important) { |
| 218 | mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget(); |
| 219 | } |
| 220 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 221 | public void setAutomute(boolean automute) { |
| 222 | if (mAutomute == automute) return; |
| 223 | mAutomute = automute; |
| 224 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 225 | } |
| 226 | |
| 227 | public void setSilentMode(boolean silentMode) { |
| 228 | if (mSilentMode == silentMode) return; |
| 229 | mSilentMode = silentMode; |
| 230 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 231 | } |
| 232 | |
| 233 | private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) { |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 234 | addRow(stream, iconRes, iconMuteRes, important, false); |
| 235 | } |
| 236 | |
| 237 | private void addRow(int stream, int iconRes, int iconMuteRes, boolean important, |
| 238 | boolean dynamic) { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 239 | VolumeRow row = new VolumeRow(); |
| 240 | initRow(row, stream, iconRes, iconMuteRes, important); |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 241 | int rowSize; |
| 242 | int viewSize; |
| 243 | if (mShowA11yStream && dynamic && (rowSize = mRows.size()) > 1 |
| 244 | && (viewSize = mDialogRowsView.getChildCount()) > 1) { |
| 245 | // A11y Stream should be the last in the list |
| 246 | mDialogRowsView.addView(row.view, viewSize - 2); |
| 247 | mRows.add(rowSize - 2, row); |
| 248 | } else { |
| 249 | mDialogRowsView.addView(row.view); |
| 250 | mRows.add(row); |
| 251 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 254 | private void addExistingRows() { |
| 255 | int N = mRows.size(); |
| 256 | for (int i = 0; i < N; i++) { |
| 257 | final VolumeRow row = mRows.get(i); |
| 258 | initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 259 | mDialogRowsView.addView(row.view); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 260 | updateVolumeRowH(row); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 264 | private VolumeRow getActiveRow() { |
| 265 | for (VolumeRow row : mRows) { |
| 266 | if (row.stream == mActiveStream) { |
| 267 | return row; |
| 268 | } |
| 269 | } |
| 270 | return mRows.get(0); |
| 271 | } |
| 272 | |
| 273 | private VolumeRow findRow(int stream) { |
| 274 | for (VolumeRow row : mRows) { |
| 275 | if (row.stream == stream) return row; |
| 276 | } |
| 277 | return null; |
| 278 | } |
| 279 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 280 | public void dump(PrintWriter writer) { |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 281 | writer.println(VolumeDialogImpl.class.getSimpleName() + " state:"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 282 | writer.print(" mShowing: "); writer.println(mShowing); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 283 | writer.print(" mActiveStream: "); writer.println(mActiveStream); |
| 284 | writer.print(" mDynamic: "); writer.println(mDynamic); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 285 | writer.print(" mAutomute: "); writer.println(mAutomute); |
| 286 | writer.print(" mSilentMode: "); writer.println(mSilentMode); |
John Spurlock | 84ee67c | 2015-05-20 23:33:43 -0400 | [diff] [blame] | 287 | writer.print(" mAccessibility.mFeedbackEnabled: "); |
| 288 | writer.println(mAccessibility.mFeedbackEnabled); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 291 | private static int getImpliedLevel(SeekBar seekBar, int progress) { |
| 292 | final int m = seekBar.getMax(); |
| 293 | final int n = m / 100 - 1; |
| 294 | final int level = progress == 0 ? 0 |
| 295 | : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n)); |
| 296 | return level; |
| 297 | } |
| 298 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 299 | @SuppressLint("InflateParams") |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 300 | private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes, |
| 301 | boolean important) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 302 | row.stream = stream; |
| 303 | row.iconRes = iconRes; |
| 304 | row.iconMuteRes = iconMuteRes; |
| 305 | row.important = important; |
| 306 | row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 307 | row.view.setId(row.stream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 308 | row.view.setTag(row); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 309 | row.header = row.view.findViewById(R.id.volume_row_header); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 310 | row.header.setId(20 * row.stream); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 311 | row.slider = row.view.findViewById(R.id.volume_row_slider); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 312 | row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row)); |
Julia Reynolds | 5774cf0 | 2016-05-26 13:43:38 -0400 | [diff] [blame] | 313 | row.anim = null; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 314 | |
| 315 | // forward events above the slider into the slider |
| 316 | row.view.setOnTouchListener(new OnTouchListener() { |
| 317 | private final Rect mSliderHitRect = new Rect(); |
| 318 | private boolean mDragging; |
| 319 | |
| 320 | @SuppressLint("ClickableViewAccessibility") |
| 321 | @Override |
| 322 | public boolean onTouch(View v, MotionEvent event) { |
| 323 | row.slider.getHitRect(mSliderHitRect); |
| 324 | if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN |
| 325 | && event.getY() < mSliderHitRect.top) { |
| 326 | mDragging = true; |
| 327 | } |
| 328 | if (mDragging) { |
| 329 | event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top); |
| 330 | row.slider.dispatchTouchEvent(event); |
| 331 | if (event.getActionMasked() == MotionEvent.ACTION_UP |
| 332 | || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { |
| 333 | mDragging = false; |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | return false; |
| 338 | } |
| 339 | }); |
Julia Reynolds | e2503dd | 2017-04-18 11:21:13 -0400 | [diff] [blame] | 340 | row.icon = row.view.findViewById(R.id.volume_row_icon); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 341 | row.icon.setImageResource(iconRes); |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 342 | if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) { |
| 343 | row.icon.setOnClickListener(new OnClickListener() { |
| 344 | @Override |
| 345 | public void onClick(View v) { |
| 346 | Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState); |
| 347 | mController.setActiveStream(row.stream); |
| 348 | if (row.stream == AudioManager.STREAM_RING) { |
| 349 | final boolean hasVibrator = mController.hasVibrator(); |
| 350 | if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) { |
| 351 | if (hasVibrator) { |
| 352 | mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false); |
| 353 | } else { |
| 354 | final boolean wasZero = row.ss.level == 0; |
| 355 | mController.setStreamVolume(stream, |
| 356 | wasZero ? row.lastAudibleLevel : 0); |
| 357 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 358 | } else { |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 359 | mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false); |
| 360 | if (row.ss.level == 0) { |
| 361 | mController.setStreamVolume(stream, 1); |
| 362 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 363 | } |
| 364 | } else { |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 365 | final boolean vmute = row.ss.level == row.ss.levelMin; |
| 366 | mController.setStreamVolume(stream, |
| 367 | vmute ? row.lastAudibleLevel : row.ss.levelMin); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 368 | } |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 369 | row.userAttempt = 0; // reset the grace period, slider updates immediately |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 370 | } |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 371 | }); |
Julia Reynolds | e2503dd | 2017-04-18 11:21:13 -0400 | [diff] [blame] | 372 | } else { |
| 373 | row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 374 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 375 | } |
| 376 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 377 | public void show(int reason) { |
| 378 | mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget(); |
| 379 | } |
| 380 | |
| 381 | public void dismiss(int reason) { |
| 382 | mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget(); |
| 383 | } |
| 384 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 385 | private void showH(int reason) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 386 | if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 387 | mHandler.removeMessages(H.SHOW); |
| 388 | mHandler.removeMessages(H.DISMISS); |
| 389 | rescheduleTimeoutH(); |
| 390 | if (mShowing) return; |
| 391 | mShowing = true; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 392 | mHardwareLayout.setTranslationX(getAnimTranslation()); |
| 393 | mHardwareLayout.setAlpha(0); |
| 394 | mHardwareLayout.animate() |
| 395 | .alpha(1) |
| 396 | .translationX(0) |
| 397 | .setDuration(300) |
| 398 | .setInterpolator(Interpolators.FAST_OUT_SLOW_IN) |
| 399 | .withEndAction(() -> { |
| 400 | mDialog.show(); |
| 401 | mWindow.getDecorView().requestAccessibilityFocus(); |
| 402 | }) |
| 403 | .start(); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 404 | Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 405 | mController.notifyVisible(true); |
| 406 | } |
| 407 | |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 408 | private float getAnimTranslation() { |
| 409 | return mContext.getResources().getDimension( |
| 410 | R.dimen.volume_dialog_panel_width) / 2; |
| 411 | } |
| 412 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 413 | protected void rescheduleTimeoutH() { |
| 414 | mHandler.removeMessages(H.DISMISS); |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 415 | final int timeout = computeTimeoutH(); |
| 416 | mHandler.sendMessageDelayed(mHandler |
| 417 | .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 418 | if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 419 | mController.userActivity(); |
| 420 | } |
| 421 | |
| 422 | private int computeTimeoutH() { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 423 | if (mAccessibility.mFeedbackEnabled) return 20000; |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 424 | if (mHovering) return 16000; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 425 | if (mSafetyWarning != null) return 5000; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 426 | if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500; |
| 427 | return 3000; |
| 428 | } |
| 429 | |
| 430 | protected void dismissH(int reason) { |
| 431 | mHandler.removeMessages(H.DISMISS); |
| 432 | mHandler.removeMessages(H.SHOW); |
| 433 | if (!mShowing) return; |
| 434 | mShowing = false; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 435 | mHardwareLayout.setTranslationX(0); |
| 436 | mHardwareLayout.setAlpha(1); |
| 437 | mHardwareLayout.animate() |
| 438 | .alpha(0) |
| 439 | .translationX(getAnimTranslation()) |
| 440 | .setDuration(300) |
| 441 | .withEndAction(() -> mDialog.dismiss()) |
| 442 | .setInterpolator(new SystemUIInterpolators.LogAccelerateInterpolator()) |
| 443 | .start(); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 444 | if (mAccessibilityMgr.isEnabled()) { |
| 445 | AccessibilityEvent event = |
| 446 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 447 | event.setPackageName(mContext.getPackageName()); |
| 448 | event.setClassName(CustomDialog.class.getSuperclass().getName()); |
| 449 | event.getText().add(mContext.getString( |
| 450 | R.string.volume_dialog_accessibility_dismissed_message)); |
| 451 | mAccessibilityMgr.sendAccessibilityEvent(event); |
| 452 | } |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 453 | Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 454 | mController.notifyVisible(false); |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 455 | synchronized (mSafetyWarningLock) { |
| 456 | if (mSafetyWarning != null) { |
| 457 | if (D.BUG) Log.d(TAG, "SafetyWarning dismissed"); |
| 458 | mSafetyWarning.dismiss(); |
| 459 | } |
| 460 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 463 | private boolean shouldBeVisibleH(VolumeRow row, VolumeRow activeRow) { |
| 464 | boolean isActive = row == activeRow; |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 465 | if (row.stream == AudioSystem.STREAM_ACCESSIBILITY) { |
| 466 | return mShowA11yStream; |
| 467 | } |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 468 | |
| 469 | // if the active row is accessibility, then continue to display previous |
Julia Reynolds | 66d2894 | 2017-11-07 09:43:54 -0500 | [diff] [blame] | 470 | // active row since accessibility is displayed under it |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 471 | if (activeRow.stream == AudioSystem.STREAM_ACCESSIBILITY && |
| 472 | row.stream == mPrevActiveStream) { |
| 473 | return true; |
| 474 | } |
| 475 | |
Julia Reynolds | 66d2894 | 2017-11-07 09:43:54 -0500 | [diff] [blame] | 476 | return row.important || isActive; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 479 | private void updateRowsH(final VolumeRow activeRow) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 480 | if (D.BUG) Log.d(TAG, "updateRowsH"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 481 | if (!mShowing) { |
| 482 | trimObsoleteH(); |
| 483 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 484 | // apply changes to all rows |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 485 | for (final VolumeRow row : mRows) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 486 | final boolean isActive = row == activeRow; |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 487 | final boolean shouldBeVisible = shouldBeVisibleH(row, activeRow); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 488 | Util.setVisOrGone(row.view, shouldBeVisible); |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 489 | Util.setVisOrGone(row.header, shouldBeVisible); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 490 | if (row.view.isShown()) { |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 491 | updateVolumeRowSliderTintH(row, isActive); |
| 492 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | private void trimObsoleteH() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 497 | if (D.BUG) Log.d(TAG, "trimObsoleteH"); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 498 | for (int i = mRows.size() - 1; i >= 0; i--) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 499 | final VolumeRow row = mRows.get(i); |
| 500 | if (row.ss == null || !row.ss.dynamic) continue; |
| 501 | if (!mDynamic.get(row.stream)) { |
| 502 | mRows.remove(i); |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 503 | mDialogRowsView.removeView(row.view); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | private void onStateChangedH(State state) { |
| 509 | mState = state; |
| 510 | mDynamic.clear(); |
| 511 | // add any new dynamic rows |
| 512 | for (int i = 0; i < state.states.size(); i++) { |
| 513 | final int stream = state.states.keyAt(i); |
| 514 | final StreamState ss = state.states.valueAt(i); |
| 515 | if (!ss.dynamic) continue; |
| 516 | mDynamic.put(stream, true); |
| 517 | if (findRow(stream) == null) { |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 518 | addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true, |
| 519 | true); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | if (mActiveStream != state.activeStream) { |
Beverly | fa4c8e7 | 2017-08-10 14:55:34 -0400 | [diff] [blame] | 524 | mPrevActiveStream = mActiveStream; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 525 | mActiveStream = state.activeStream; |
Julia Reynolds | 568585b | 2016-07-25 08:55:15 -0400 | [diff] [blame] | 526 | updateRowsH(getActiveRow()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 527 | rescheduleTimeoutH(); |
| 528 | } |
| 529 | for (VolumeRow row : mRows) { |
| 530 | updateVolumeRowH(row); |
| 531 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | private void updateVolumeRowH(VolumeRow row) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 535 | if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 536 | if (mState == null) return; |
| 537 | final StreamState ss = mState.states.get(row.stream); |
| 538 | if (ss == null) return; |
| 539 | row.ss = ss; |
| 540 | if (ss.level > 0) { |
| 541 | row.lastAudibleLevel = ss.level; |
| 542 | } |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 543 | if (ss.level == row.requestedLevel) { |
| 544 | row.requestedLevel = -1; |
| 545 | } |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 546 | final boolean isA11yStream = row.stream == AudioManager.STREAM_ACCESSIBILITY; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 547 | final boolean isRingStream = row.stream == AudioManager.STREAM_RING; |
| 548 | final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 549 | final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM; |
| 550 | final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 551 | final boolean isRingVibrate = isRingStream |
| 552 | && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 553 | final boolean isRingSilent = isRingStream |
| 554 | && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT; |
| 555 | final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS; |
| 556 | final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 557 | final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream) |
| 558 | : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream) |
| 559 | : false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 560 | |
| 561 | // update slider max |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 562 | final int max = ss.levelMax * 100; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 563 | if (max != row.slider.getMax()) { |
| 564 | row.slider.setMax(max); |
| 565 | } |
| 566 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 567 | // update header text |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 568 | Util.setText(row.header, getStreamLabelH(ss)); |
Lucas Dupin | 421b20c | 2017-08-01 10:36:10 -0700 | [diff] [blame] | 569 | row.slider.setContentDescription(row.header.getText()); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 570 | mConfigurableTexts.add(row.header, ss.name); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 571 | |
| 572 | // update icon |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 573 | final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 574 | row.icon.setEnabled(iconEnabled); |
| 575 | row.icon.setAlpha(iconEnabled ? 1 : 0.5f); |
| 576 | final int iconRes = |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 577 | isRingVibrate ? R.drawable.ic_volume_ringer_vibrate |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 578 | : isRingSilent || zenMuted ? row.iconMuteRes |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 579 | : ss.routedToBluetooth ? |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 580 | (ss.muted ? R.drawable.ic_volume_media_bt_mute |
| 581 | : R.drawable.ic_volume_media_bt) |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 582 | : mAutomute && ss.level == 0 ? row.iconMuteRes |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 583 | : (ss.muted ? row.iconMuteRes : row.iconRes); |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 584 | row.icon.setImageResource(iconRes); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 585 | row.iconState = |
| 586 | iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 587 | : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes) |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 588 | ? Events.ICON_STATE_MUTE |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 589 | : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes) |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 590 | ? Events.ICON_STATE_UNMUTE |
| 591 | : Events.ICON_STATE_UNKNOWN; |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 592 | if (iconEnabled) { |
| 593 | if (isRingStream) { |
| 594 | if (isRingVibrate) { |
| 595 | row.icon.setContentDescription(mContext.getString( |
| 596 | R.string.volume_stream_content_description_unmute, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 597 | getStreamLabelH(ss))); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 598 | } else { |
| 599 | if (mController.hasVibrator()) { |
| 600 | row.icon.setContentDescription(mContext.getString( |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 601 | mShowA11yStream |
| 602 | ? R.string.volume_stream_content_description_vibrate_a11y |
| 603 | : R.string.volume_stream_content_description_vibrate, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 604 | getStreamLabelH(ss))); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 605 | } else { |
| 606 | row.icon.setContentDescription(mContext.getString( |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 607 | mShowA11yStream |
| 608 | ? R.string.volume_stream_content_description_mute_a11y |
| 609 | : R.string.volume_stream_content_description_mute, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 610 | getStreamLabelH(ss))); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 611 | } |
| 612 | } |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 613 | } else if (isA11yStream) { |
| 614 | row.icon.setContentDescription(getStreamLabelH(ss)); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 615 | } else { |
| 616 | if (ss.muted || mAutomute && ss.level == 0) { |
| 617 | row.icon.setContentDescription(mContext.getString( |
| 618 | R.string.volume_stream_content_description_unmute, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 619 | getStreamLabelH(ss))); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 620 | } else { |
| 621 | row.icon.setContentDescription(mContext.getString( |
Julia Reynolds | 7c502e0 | 2017-03-06 10:19:10 -0500 | [diff] [blame] | 622 | mShowA11yStream |
| 623 | ? R.string.volume_stream_content_description_mute_a11y |
| 624 | : R.string.volume_stream_content_description_mute, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 625 | getStreamLabelH(ss))); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } else { |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 629 | row.icon.setContentDescription(getStreamLabelH(ss)); |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 630 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 631 | |
Beverly | e421abe | 2017-08-22 14:47:28 -0400 | [diff] [blame] | 632 | // ensure tracking is disabled if zenMuted |
| 633 | if (zenMuted) { |
| 634 | row.tracking = false; |
| 635 | } |
| 636 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 637 | // update slider |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 638 | final boolean enableSlider = !zenMuted; |
Julia Reynolds | 5f84c19 | 2016-11-14 13:13:08 -0500 | [diff] [blame] | 639 | final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0 |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 640 | : row.ss.level; |
| 641 | updateVolumeRowSliderH(row, enableSlider, vlevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 642 | } |
| 643 | |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 644 | private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) { |
Julia Reynolds | 66d2894 | 2017-11-07 09:43:54 -0500 | [diff] [blame] | 645 | if (isActive) { |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 646 | row.slider.requestFocus(); |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 647 | } |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 648 | final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint |
| 649 | : mInactiveSliderTint; |
| 650 | if (tint == row.cachedSliderTint) return; |
| 651 | row.cachedSliderTint = tint; |
| 652 | row.slider.setProgressTintList(tint); |
| 653 | row.slider.setThumbTintList(tint); |
| 654 | } |
| 655 | |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 656 | private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) { |
| 657 | row.slider.setEnabled(enable); |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 658 | updateVolumeRowSliderTintH(row, row.stream == mActiveStream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 659 | if (row.tracking) { |
| 660 | return; // don't update if user is sliding |
| 661 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 662 | final int progress = row.slider.getProgress(); |
| 663 | final int level = getImpliedLevel(row.slider, progress); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 664 | final boolean rowVisible = row.view.getVisibility() == View.VISIBLE; |
| 665 | final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt) |
| 666 | < USER_ATTEMPT_GRACE_PERIOD; |
| 667 | mHandler.removeMessages(H.RECHECK, row); |
| 668 | if (mShowing && rowVisible && inGracePeriod) { |
| 669 | if (D.BUG) Log.d(TAG, "inGracePeriod"); |
| 670 | mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row), |
| 671 | row.userAttempt + USER_ATTEMPT_GRACE_PERIOD); |
| 672 | return; // don't update if visible and in grace period |
| 673 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 674 | if (vlevel == level) { |
| 675 | if (mShowing && rowVisible) { |
| 676 | return; // don't clamp if visible |
| 677 | } |
| 678 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 679 | final int newProgress = vlevel * 100; |
| 680 | if (progress != newProgress) { |
| 681 | if (mShowing && rowVisible) { |
| 682 | // animate! |
| 683 | if (row.anim != null && row.anim.isRunning() |
| 684 | && row.animTargetProgress == newProgress) { |
| 685 | return; // already animating to the target progress |
| 686 | } |
| 687 | // start/update animation |
| 688 | if (row.anim == null) { |
| 689 | row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress); |
| 690 | row.anim.setInterpolator(new DecelerateInterpolator()); |
| 691 | } else { |
| 692 | row.anim.cancel(); |
| 693 | row.anim.setIntValues(progress, newProgress); |
| 694 | } |
| 695 | row.animTargetProgress = newProgress; |
| 696 | row.anim.setDuration(UPDATE_ANIMATION_DURATION); |
| 697 | row.anim.start(); |
| 698 | } else { |
| 699 | // update slider directly to clamped value |
| 700 | if (row.anim != null) { |
| 701 | row.anim.cancel(); |
| 702 | } |
Julia Reynolds | 88dabde | 2016-08-09 13:59:44 -0400 | [diff] [blame] | 703 | row.slider.setProgress(newProgress, true); |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 704 | } |
| 705 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | private void recheckH(VolumeRow row) { |
| 709 | if (row == null) { |
| 710 | if (D.BUG) Log.d(TAG, "recheckH ALL"); |
| 711 | trimObsoleteH(); |
| 712 | for (VolumeRow r : mRows) { |
| 713 | updateVolumeRowH(r); |
| 714 | } |
| 715 | } else { |
| 716 | if (D.BUG) Log.d(TAG, "recheckH " + row.stream); |
| 717 | updateVolumeRowH(row); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | private void setStreamImportantH(int stream, boolean important) { |
| 722 | for (VolumeRow row : mRows) { |
| 723 | if (row.stream == stream) { |
| 724 | row.important = important; |
| 725 | return; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 730 | private void showSafetyWarningH(int flags) { |
| 731 | if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0 |
| 732 | || mShowing) { |
| 733 | synchronized (mSafetyWarningLock) { |
| 734 | if (mSafetyWarning != null) { |
| 735 | return; |
| 736 | } |
| 737 | mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) { |
| 738 | @Override |
| 739 | protected void cleanUp() { |
| 740 | synchronized (mSafetyWarningLock) { |
| 741 | mSafetyWarning = null; |
| 742 | } |
| 743 | recheckH(null); |
| 744 | } |
| 745 | }; |
| 746 | mSafetyWarning.show(); |
| 747 | } |
| 748 | recheckH(null); |
| 749 | } |
| 750 | rescheduleTimeoutH(); |
| 751 | } |
| 752 | |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 753 | private String getStreamLabelH(StreamState ss) { |
| 754 | if (ss.remoteLabel != null) { |
| 755 | return ss.remoteLabel; |
| 756 | } |
| 757 | try { |
| 758 | return mContext.getString(ss.name); |
| 759 | } catch (Resources.NotFoundException e) { |
| 760 | Slog.e(TAG, "Can't find translation for stream " + ss); |
| 761 | return ""; |
| 762 | } |
| 763 | } |
| 764 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 765 | private final VolumeDialogController.Callbacks mControllerCallbackH |
| 766 | = new VolumeDialogController.Callbacks() { |
| 767 | @Override |
| 768 | public void onShowRequested(int reason) { |
| 769 | showH(reason); |
| 770 | } |
| 771 | |
| 772 | @Override |
| 773 | public void onDismissRequested(int reason) { |
| 774 | dismissH(reason); |
| 775 | } |
| 776 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 777 | @Override |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 778 | public void onScreenOff() { |
| 779 | dismissH(Events.DISMISS_REASON_SCREEN_OFF); |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public void onStateChanged(State state) { |
| 784 | onStateChangedH(state); |
| 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public void onLayoutDirectionChanged(int layoutDirection) { |
| 789 | mDialogView.setLayoutDirection(layoutDirection); |
| 790 | } |
| 791 | |
| 792 | @Override |
| 793 | public void onConfigurationChanged() { |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 794 | mDialog.dismiss(); |
| 795 | initDialog(); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 796 | mConfigurableTexts.update(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | @Override |
| 800 | public void onShowVibrateHint() { |
| 801 | if (mSilentMode) { |
| 802 | mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false); |
| 803 | } |
| 804 | } |
| 805 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 806 | @Override |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 807 | public void onShowSilentHint() { |
| 808 | if (mSilentMode) { |
| 809 | mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false); |
| 810 | } |
| 811 | } |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 812 | |
| 813 | @Override |
| 814 | public void onShowSafetyWarning(int flags) { |
| 815 | showSafetyWarningH(flags); |
| 816 | } |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 817 | |
| 818 | @Override |
| 819 | public void onAccessibilityModeChanged(Boolean showA11yStream) { |
| 820 | boolean show = showA11yStream == null ? false : showA11yStream; |
| 821 | mShowA11yStream = show; |
Julia Reynolds | 74cc650 | 2017-08-08 12:41:04 -0400 | [diff] [blame] | 822 | VolumeRow activeRow = getActiveRow(); |
| 823 | if (!mShowA11yStream && AudioManager.STREAM_ACCESSIBILITY == activeRow.stream) { |
| 824 | dismissH(Events.DISMISS_STREAM_GONE); |
| 825 | } else { |
| 826 | updateRowsH(activeRow); |
| 827 | } |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 828 | |
| 829 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 830 | }; |
| 831 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 832 | private final class H extends Handler { |
| 833 | private static final int SHOW = 1; |
| 834 | private static final int DISMISS = 2; |
| 835 | private static final int RECHECK = 3; |
| 836 | private static final int RECHECK_ALL = 4; |
| 837 | private static final int SET_STREAM_IMPORTANT = 5; |
| 838 | private static final int RESCHEDULE_TIMEOUT = 6; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 839 | private static final int STATE_CHANGED = 7; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 840 | |
| 841 | public H() { |
| 842 | super(Looper.getMainLooper()); |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public void handleMessage(Message msg) { |
| 847 | switch (msg.what) { |
| 848 | case SHOW: showH(msg.arg1); break; |
| 849 | case DISMISS: dismissH(msg.arg1); break; |
| 850 | case RECHECK: recheckH((VolumeRow) msg.obj); break; |
| 851 | case RECHECK_ALL: recheckH(null); break; |
| 852 | case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break; |
| 853 | case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 854 | case STATE_CHANGED: onStateChangedH(mState); break; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 859 | private final class CustomDialog extends Dialog implements DialogInterface { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 860 | public CustomDialog(Context context) { |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 861 | super(context, com.android.systemui.R.style.qs_theme); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | @Override |
| 865 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 866 | rescheduleTimeoutH(); |
| 867 | return super.dispatchTouchEvent(ev); |
| 868 | } |
| 869 | |
| 870 | @Override |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 871 | protected void onStart() { |
| 872 | super.setCanceledOnTouchOutside(true); |
| 873 | super.onStart(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | @Override |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 877 | protected void onStop() { |
| 878 | super.onStop(); |
| 879 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 880 | } |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 881 | |
| 882 | @Override |
| 883 | public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) { |
| 884 | event.setClassName(getClass().getSuperclass().getName()); |
| 885 | event.setPackageName(mContext.getPackageName()); |
| 886 | |
| 887 | ViewGroup.LayoutParams params = getWindow().getAttributes(); |
Jason Monk | 98ad83c | 2017-06-13 14:03:45 -0400 | [diff] [blame] | 888 | boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) && |
| 889 | (params.height == ViewGroup.LayoutParams.MATCH_PARENT); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 890 | event.setFullScreen(isFullScreen); |
| 891 | |
| 892 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 893 | if (mShowing) { |
| 894 | event.getText().add(mContext.getString( |
| 895 | R.string.volume_dialog_accessibility_shown_message, |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 896 | getStreamLabelH(getActiveRow().ss))); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 897 | return true; |
| 898 | } |
| 899 | } |
| 900 | return false; |
| 901 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener { |
| 905 | private final VolumeRow mRow; |
| 906 | |
| 907 | private VolumeSeekBarChangeListener(VolumeRow row) { |
| 908 | mRow = row; |
| 909 | } |
| 910 | |
| 911 | @Override |
| 912 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 913 | if (mRow.ss == null) return; |
| 914 | if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream) |
| 915 | + " onProgressChanged " + progress + " fromUser=" + fromUser); |
| 916 | if (!fromUser) return; |
| 917 | if (mRow.ss.levelMin > 0) { |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 918 | final int minProgress = mRow.ss.levelMin * 100; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 919 | if (progress < minProgress) { |
| 920 | seekBar.setProgress(minProgress); |
Julia Reynolds | 798b0c3 | 2015-09-01 14:35:53 -0400 | [diff] [blame] | 921 | progress = minProgress; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 922 | } |
| 923 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 924 | final int userLevel = getImpliedLevel(seekBar, progress); |
| 925 | if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 926 | mRow.userAttempt = SystemClock.uptimeMillis(); |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 927 | if (mRow.requestedLevel != userLevel) { |
| 928 | mController.setStreamVolume(mRow.stream, userLevel); |
| 929 | mRow.requestedLevel = userLevel; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 930 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream, |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 931 | userLevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | @Override |
| 937 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 938 | if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream); |
| 939 | mController.setActiveStream(mRow.stream); |
| 940 | mRow.tracking = true; |
| 941 | } |
| 942 | |
| 943 | @Override |
| 944 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 945 | if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream); |
| 946 | mRow.tracking = false; |
| 947 | mRow.userAttempt = SystemClock.uptimeMillis(); |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 948 | final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress()); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 949 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 950 | if (mRow.ss.level != userLevel) { |
| 951 | mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow), |
| 952 | USER_ATTEMPT_GRACE_PERIOD); |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 957 | private final class Accessibility extends AccessibilityDelegate { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 958 | private boolean mFeedbackEnabled; |
| 959 | |
| 960 | public void init() { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 961 | mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { |
| 962 | @Override |
| 963 | public void onViewDetachedFromWindow(View v) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 964 | if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow"); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void onViewAttachedToWindow(View v) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 969 | if (D.BUG) Log.d(TAG, "onViewAttachedToWindow"); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 970 | updateFeedbackEnabled(); |
| 971 | } |
| 972 | }); |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 973 | mDialogView.setAccessibilityDelegate(this); |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 974 | mAccessibilityMgr.addAccessibilityStateChangeListener(mListener); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 975 | updateFeedbackEnabled(); |
| 976 | } |
| 977 | |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 978 | public void destroy() { |
| 979 | mAccessibilityMgr.removeAccessibilityStateChangeListener(mListener); |
| 980 | } |
| 981 | |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 982 | @Override |
| 983 | public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, |
| 984 | AccessibilityEvent event) { |
| 985 | rescheduleTimeoutH(); |
| 986 | return super.onRequestSendAccessibilityEvent(host, child, event); |
| 987 | } |
| 988 | |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 989 | private void updateFeedbackEnabled() { |
| 990 | mFeedbackEnabled = computeFeedbackEnabled(); |
| 991 | } |
| 992 | |
| 993 | private boolean computeFeedbackEnabled() { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 994 | // are there any enabled non-generic a11y services? |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 995 | final List<AccessibilityServiceInfo> services = |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame] | 996 | mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 997 | for (AccessibilityServiceInfo asi : services) { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 998 | if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 999 | return true; |
| 1000 | } |
| 1001 | } |
| 1002 | return false; |
| 1003 | } |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 1004 | |
| 1005 | private final AccessibilityStateChangeListener mListener = |
| 1006 | enabled -> updateFeedbackEnabled(); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1009 | private static class VolumeRow { |
| 1010 | private View view; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1011 | private TextView header; |
| 1012 | private ImageButton icon; |
| 1013 | private SeekBar slider; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1014 | private int stream; |
| 1015 | private StreamState ss; |
| 1016 | private long userAttempt; // last user-driven slider change |
| 1017 | private boolean tracking; // tracking slider touch |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 1018 | private int requestedLevel = -1; // pending user-requested level via progress changed |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1019 | private int iconRes; |
| 1020 | private int iconMuteRes; |
| 1021 | private boolean important; |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 1022 | private ColorStateList cachedSliderTint; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1023 | private int iconState; // from Events |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1024 | private ObjectAnimator anim; // slider progress animation for non-touch-related updates |
| 1025 | private int animTargetProgress; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1026 | private int lastAudibleLevel = 1; |
| 1027 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1028 | } |