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