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