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