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 android.accessibilityservice.AccessibilityServiceInfo; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 20 | import android.animation.LayoutTransition; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 23 | import android.annotation.NonNull; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 24 | import android.annotation.SuppressLint; |
| 25 | import android.app.Dialog; |
| 26 | import android.app.KeyguardManager; |
| 27 | import android.content.Context; |
Takayuki Hoshi | bb0ec55 | 2015-12-17 20:50:25 +0900 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 29 | import android.content.res.ColorStateList; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 30 | import android.content.res.Configuration; |
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; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 48 | import android.util.Slog; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 49 | import android.util.SparseBooleanArray; |
| 50 | import android.view.Gravity; |
| 51 | import android.view.MotionEvent; |
| 52 | import android.view.View; |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 53 | import android.view.View.AccessibilityDelegate; |
| 54 | import android.view.View.OnAttachStateChangeListener; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 55 | import android.view.View.OnClickListener; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 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; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -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; |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 73 | import com.android.systemui.tuner.TunerService; |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 74 | import com.android.systemui.tuner.TunerZenModePanel; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 75 | import com.android.systemui.volume.VolumeDialogController.State; |
| 76 | import com.android.systemui.volume.VolumeDialogController.StreamState; |
| 77 | |
| 78 | import java.io.PrintWriter; |
| 79 | import java.util.ArrayList; |
| 80 | import java.util.List; |
| 81 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 82 | import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_ALL_MASK; |
| 83 | import static android.accessibilityservice.AccessibilityServiceInfo.FEEDBACK_GENERIC; |
| 84 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 85 | /** |
| 86 | * Visual presentation of the volume dialog. |
| 87 | * |
| 88 | * A client of VolumeDialogController and its state model. |
| 89 | * |
| 90 | * Methods ending in "H" must be called on the (ui) handler. |
| 91 | */ |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 92 | public class VolumeDialog implements TunerService.Tunable { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 93 | private static final String TAG = Util.logTag(VolumeDialog.class); |
| 94 | |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 95 | public static final String SHOW_FULL_ZEN = "sysui_show_full_zen"; |
| 96 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 97 | private static final long USER_ATTEMPT_GRACE_PERIOD = 1000; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 98 | private static final int UPDATE_ANIMATION_DURATION = 80; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 99 | |
| 100 | private final Context mContext; |
| 101 | private final H mHandler = new H(); |
| 102 | private final VolumeDialogController mController; |
| 103 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 104 | private CustomDialog mDialog; |
| 105 | private ViewGroup mDialogView; |
| 106 | private ViewGroup mDialogContentView; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 107 | private ImageButton mExpandButton; |
| 108 | private final List<VolumeRow> mRows = new ArrayList<>(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 109 | private final SpTexts mSpTexts; |
| 110 | private final SparseBooleanArray mDynamic = new SparseBooleanArray(); |
| 111 | private final KeyguardManager mKeyguard; |
Julia Reynolds | 6aa83b4 | 2015-09-08 13:41:45 -0400 | [diff] [blame] | 112 | private final AudioManager mAudioManager; |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 113 | private final AccessibilityManager mAccessibilityMgr; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 114 | private int mExpandButtonAnimationDuration; |
| 115 | private ZenFooter mZenFooter; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 116 | private final LayoutTransition mLayoutTransition; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 117 | private final Object mSafetyWarningLock = new Object(); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 118 | private final Accessibility mAccessibility = new Accessibility(); |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 119 | private final ColorStateList mActiveSliderTint; |
| 120 | private final ColorStateList mInactiveSliderTint; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 121 | private VolumeDialogMotion mMotion; |
| 122 | private final int mWindowType; |
| 123 | private final ZenModeController mZenModeController; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 124 | |
| 125 | private boolean mShowing; |
| 126 | private boolean mExpanded; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 127 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 128 | private int mActiveStream; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 129 | private boolean mShowHeaders = VolumePrefs.DEFAULT_SHOW_HEADERS; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 130 | private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE; |
| 131 | private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 132 | private State mState; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 133 | private boolean mExpandButtonAnimationRunning; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 134 | private SafetyWarningDialog mSafetyWarning; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 135 | private Callback mCallback; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 136 | private boolean mPendingStateChanged; |
| 137 | private boolean mPendingRecheckAll; |
| 138 | private long mCollapseTime; |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 139 | private boolean mHovering = false; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 140 | private int mDensity; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 141 | |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 142 | private boolean mShowFullZen; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 143 | private TunerZenModePanel mZenPanel; |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 144 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 145 | public VolumeDialog(Context context, int windowType, VolumeDialogController controller, |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 146 | ZenModeController zenModeController, Callback callback) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 147 | mContext = context; |
| 148 | mController = controller; |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 149 | mCallback = callback; |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 150 | mWindowType = windowType; |
| 151 | mZenModeController = zenModeController; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 152 | mSpTexts = new SpTexts(mContext); |
| 153 | mKeyguard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); |
Julia Reynolds | 6aa83b4 | 2015-09-08 13:41:45 -0400 | [diff] [blame] | 154 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 155 | mAccessibilityMgr = (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 156 | mActiveSliderTint = loadColorStateList(R.color.system_accent_color); |
| 157 | mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive); |
| 158 | mLayoutTransition = new LayoutTransition(); |
| 159 | mLayoutTransition.setDuration(new ValueAnimator().getDuration() / 2); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 160 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 161 | initDialog(); |
| 162 | |
| 163 | mAccessibility.init(); |
| 164 | |
| 165 | controller.addCallback(mControllerCallbackH, mHandler); |
| 166 | controller.getState(); |
| 167 | TunerService.get(mContext).addTunable(this, SHOW_FULL_ZEN); |
| 168 | |
| 169 | final Configuration currentConfig = mContext.getResources().getConfiguration(); |
| 170 | mDensity = currentConfig.densityDpi; |
| 171 | } |
| 172 | |
| 173 | private void initDialog() { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 174 | mDialog = new CustomDialog(mContext); |
| 175 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 176 | mHovering = false; |
| 177 | mShowing = false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 178 | final Window window = mDialog.getWindow(); |
| 179 | window.requestFeature(Window.FEATURE_NO_TITLE); |
| 180 | window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 181 | window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 182 | window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 183 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 184 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 185 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 186 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 187 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 188 | mDialog.setCanceledOnTouchOutside(true); |
| 189 | final Resources res = mContext.getResources(); |
| 190 | final WindowManager.LayoutParams lp = window.getAttributes(); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 191 | lp.type = mWindowType; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 192 | lp.format = PixelFormat.TRANSLUCENT; |
| 193 | lp.setTitle(VolumeDialog.class.getSimpleName()); |
| 194 | lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 195 | lp.y = res.getDimensionPixelSize(R.dimen.volume_offset_top); |
| 196 | lp.gravity = Gravity.TOP; |
Jorim Jaggi | 6d712e0 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 197 | lp.windowAnimations = -1; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 198 | window.setAttributes(lp); |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 199 | window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 200 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 201 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 202 | mDialog.setContentView(R.layout.volume_dialog); |
| 203 | mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog); |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 204 | mDialogView.setOnHoverListener(new View.OnHoverListener() { |
| 205 | @Override |
| 206 | public boolean onHover(View v, MotionEvent event) { |
| 207 | int action = event.getActionMasked(); |
| 208 | mHovering = (action == MotionEvent.ACTION_HOVER_ENTER) |
| 209 | || (action == MotionEvent.ACTION_HOVER_MOVE); |
| 210 | rescheduleTimeoutH(); |
| 211 | return true; |
| 212 | } |
| 213 | }); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 214 | mDialogContentView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog_content); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 215 | mExpanded = false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 216 | mExpandButton = (ImageButton) mDialogView.findViewById(R.id.volume_expand_button); |
| 217 | mExpandButton.setOnClickListener(mClickExpand); |
| 218 | updateWindowWidthH(); |
| 219 | updateExpandButtonH(); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 220 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 221 | mDialogContentView.setLayoutTransition(mLayoutTransition); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 222 | mMotion = new VolumeDialogMotion(mDialog, mDialogView, mDialogContentView, mExpandButton, |
| 223 | new VolumeDialogMotion.Callback() { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 224 | @Override |
| 225 | public void onAnimatingChanged(boolean animating) { |
| 226 | if (animating) return; |
| 227 | if (mPendingStateChanged) { |
| 228 | mHandler.sendEmptyMessage(H.STATE_CHANGED); |
| 229 | mPendingStateChanged = false; |
| 230 | } |
| 231 | if (mPendingRecheckAll) { |
| 232 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 233 | mPendingRecheckAll = false; |
| 234 | } |
| 235 | } |
| 236 | }); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 237 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 238 | if (mRows.isEmpty()) { |
| 239 | addRow(AudioManager.STREAM_RING, |
| 240 | R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true); |
| 241 | addRow(AudioManager.STREAM_MUSIC, |
| 242 | R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true); |
| 243 | addRow(AudioManager.STREAM_ALARM, |
| 244 | R.drawable.ic_volume_alarm, R.drawable.ic_volume_alarm_mute, false); |
| 245 | addRow(AudioManager.STREAM_VOICE_CALL, |
| 246 | R.drawable.ic_volume_voice, R.drawable.ic_volume_voice, false); |
| 247 | addRow(AudioManager.STREAM_BLUETOOTH_SCO, |
| 248 | R.drawable.ic_volume_bt_sco, R.drawable.ic_volume_bt_sco, false); |
| 249 | addRow(AudioManager.STREAM_SYSTEM, |
| 250 | R.drawable.ic_volume_system, R.drawable.ic_volume_system_mute, false); |
| 251 | } else { |
| 252 | addExistingRows(); |
| 253 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 254 | mExpandButtonAnimationDuration = res.getInteger(R.integer.volume_expand_animation_duration); |
| 255 | mZenFooter = (ZenFooter) mDialog.findViewById(R.id.volume_zen_footer); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 256 | mZenFooter.init(mZenModeController); |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 257 | mZenPanel = (TunerZenModePanel) mDialog.findViewById(R.id.tuner_zen_mode_panel); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 258 | mZenPanel.init(mZenModeController); |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 259 | mZenPanel.setCallback(mZenPanelCallback); |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onTuningChanged(String key, String newValue) { |
| 264 | if (SHOW_FULL_ZEN.equals(key)) { |
| 265 | mShowFullZen = newValue != null && Integer.parseInt(newValue) != 0; |
| 266 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 267 | } |
| 268 | |
John Spurlock | 77bb25af | 2015-05-11 12:12:07 -0400 | [diff] [blame] | 269 | private ColorStateList loadColorStateList(int colorResId) { |
| 270 | return ColorStateList.valueOf(mContext.getColor(colorResId)); |
| 271 | } |
| 272 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 273 | private void updateWindowWidthH() { |
| 274 | final ViewGroup.LayoutParams lp = mDialogView.getLayoutParams(); |
| 275 | final DisplayMetrics dm = mContext.getResources().getDisplayMetrics(); |
| 276 | if (D.BUG) Log.d(TAG, "updateWindowWidth dm.w=" + dm.widthPixels); |
| 277 | int w = dm.widthPixels; |
| 278 | final int max = mContext.getResources() |
Anthony Chen | 2f60d11 | 2016-01-26 15:34:12 -0800 | [diff] [blame] | 279 | .getDimensionPixelSize(R.dimen.volume_dialog_panel_width); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 280 | if (w > max) { |
| 281 | w = max; |
| 282 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 283 | lp.width = w; |
| 284 | mDialogView.setLayoutParams(lp); |
| 285 | } |
| 286 | |
| 287 | public void setStreamImportant(int stream, boolean important) { |
| 288 | mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget(); |
| 289 | } |
| 290 | |
| 291 | public void setShowHeaders(boolean showHeaders) { |
| 292 | if (showHeaders == mShowHeaders) return; |
| 293 | mShowHeaders = showHeaders; |
| 294 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 295 | } |
| 296 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 297 | public void setAutomute(boolean automute) { |
| 298 | if (mAutomute == automute) return; |
| 299 | mAutomute = automute; |
| 300 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 301 | } |
| 302 | |
| 303 | public void setSilentMode(boolean silentMode) { |
| 304 | if (mSilentMode == silentMode) return; |
| 305 | mSilentMode = silentMode; |
| 306 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 307 | } |
| 308 | |
| 309 | private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 310 | VolumeRow row = new VolumeRow(); |
| 311 | initRow(row, stream, iconRes, iconMuteRes, important); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 312 | if (!mRows.isEmpty()) { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 313 | addSpacer(row); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 314 | } |
Julia Reynolds | e80f675 | 2016-04-20 14:37:39 -0400 | [diff] [blame] | 315 | mDialogContentView.addView(row.view, mDialogContentView.getChildCount() - 2); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 316 | mRows.add(row); |
| 317 | } |
| 318 | |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 319 | private void addExistingRows() { |
| 320 | int N = mRows.size(); |
| 321 | for (int i = 0; i < N; i++) { |
| 322 | final VolumeRow row = mRows.get(i); |
| 323 | initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important); |
| 324 | if (i > 0) { |
| 325 | addSpacer(row); |
| 326 | } |
Julia Reynolds | e80f675 | 2016-04-20 14:37:39 -0400 | [diff] [blame] | 327 | mDialogContentView.addView(row.view, mDialogContentView.getChildCount() - 2); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | private void addSpacer(VolumeRow row) { |
| 332 | final View v = new View(mContext); |
| 333 | v.setId(android.R.id.background); |
| 334 | final int h = mContext.getResources() |
| 335 | .getDimensionPixelSize(R.dimen.volume_slider_interspacing); |
| 336 | final LinearLayout.LayoutParams lp = |
| 337 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, h); |
Julia Reynolds | e80f675 | 2016-04-20 14:37:39 -0400 | [diff] [blame] | 338 | mDialogContentView.addView(v, mDialogContentView.getChildCount() - 2, lp); |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 339 | row.space = v; |
| 340 | } |
| 341 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 342 | private boolean isAttached() { |
| 343 | return mDialogContentView != null && mDialogContentView.isAttachedToWindow(); |
| 344 | } |
| 345 | |
| 346 | private VolumeRow getActiveRow() { |
| 347 | for (VolumeRow row : mRows) { |
| 348 | if (row.stream == mActiveStream) { |
| 349 | return row; |
| 350 | } |
| 351 | } |
| 352 | return mRows.get(0); |
| 353 | } |
| 354 | |
| 355 | private VolumeRow findRow(int stream) { |
| 356 | for (VolumeRow row : mRows) { |
| 357 | if (row.stream == stream) return row; |
| 358 | } |
| 359 | return null; |
| 360 | } |
| 361 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 362 | public void dump(PrintWriter writer) { |
| 363 | writer.println(VolumeDialog.class.getSimpleName() + " state:"); |
| 364 | writer.print(" mShowing: "); writer.println(mShowing); |
| 365 | writer.print(" mExpanded: "); writer.println(mExpanded); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 366 | writer.print(" mExpandButtonAnimationRunning: "); |
| 367 | writer.println(mExpandButtonAnimationRunning); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 368 | writer.print(" mActiveStream: "); writer.println(mActiveStream); |
| 369 | writer.print(" mDynamic: "); writer.println(mDynamic); |
| 370 | writer.print(" mShowHeaders: "); writer.println(mShowHeaders); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 371 | writer.print(" mAutomute: "); writer.println(mAutomute); |
| 372 | writer.print(" mSilentMode: "); writer.println(mSilentMode); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 373 | writer.print(" mCollapseTime: "); writer.println(mCollapseTime); |
John Spurlock | 84ee67c | 2015-05-20 23:33:43 -0400 | [diff] [blame] | 374 | writer.print(" mAccessibility.mFeedbackEnabled: "); |
| 375 | writer.println(mAccessibility.mFeedbackEnabled); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 378 | private static int getImpliedLevel(SeekBar seekBar, int progress) { |
| 379 | final int m = seekBar.getMax(); |
| 380 | final int n = m / 100 - 1; |
| 381 | final int level = progress == 0 ? 0 |
| 382 | : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n)); |
| 383 | return level; |
| 384 | } |
| 385 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 386 | @SuppressLint("InflateParams") |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 387 | private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes, |
| 388 | boolean important) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 389 | row.stream = stream; |
| 390 | row.iconRes = iconRes; |
| 391 | row.iconMuteRes = iconMuteRes; |
| 392 | row.important = important; |
| 393 | row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null); |
| 394 | row.view.setTag(row); |
| 395 | row.header = (TextView) row.view.findViewById(R.id.volume_row_header); |
| 396 | mSpTexts.add(row.header); |
| 397 | row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider); |
| 398 | row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row)); |
| 399 | |
| 400 | // forward events above the slider into the slider |
| 401 | row.view.setOnTouchListener(new OnTouchListener() { |
| 402 | private final Rect mSliderHitRect = new Rect(); |
| 403 | private boolean mDragging; |
| 404 | |
| 405 | @SuppressLint("ClickableViewAccessibility") |
| 406 | @Override |
| 407 | public boolean onTouch(View v, MotionEvent event) { |
| 408 | row.slider.getHitRect(mSliderHitRect); |
| 409 | if (!mDragging && event.getActionMasked() == MotionEvent.ACTION_DOWN |
| 410 | && event.getY() < mSliderHitRect.top) { |
| 411 | mDragging = true; |
| 412 | } |
| 413 | if (mDragging) { |
| 414 | event.offsetLocation(-mSliderHitRect.left, -mSliderHitRect.top); |
| 415 | row.slider.dispatchTouchEvent(event); |
| 416 | if (event.getActionMasked() == MotionEvent.ACTION_UP |
| 417 | || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { |
| 418 | mDragging = false; |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | }); |
| 425 | row.icon = (ImageButton) row.view.findViewById(R.id.volume_row_icon); |
| 426 | row.icon.setImageResource(iconRes); |
| 427 | row.icon.setOnClickListener(new OnClickListener() { |
| 428 | @Override |
| 429 | public void onClick(View v) { |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 430 | Events.writeEvent(mContext, Events.EVENT_ICON_CLICK, row.stream, row.iconState); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 431 | mController.setActiveStream(row.stream); |
| 432 | if (row.stream == AudioManager.STREAM_RING) { |
| 433 | final boolean hasVibrator = mController.hasVibrator(); |
| 434 | if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) { |
| 435 | if (hasVibrator) { |
| 436 | mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false); |
| 437 | } else { |
| 438 | final boolean wasZero = row.ss.level == 0; |
| 439 | mController.setStreamVolume(stream, wasZero ? row.lastAudibleLevel : 0); |
| 440 | } |
| 441 | } else { |
| 442 | mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false); |
| 443 | if (row.ss.level == 0) { |
| 444 | mController.setStreamVolume(stream, 1); |
| 445 | } |
| 446 | } |
| 447 | } else { |
Julia Reynolds | 798b0c3 | 2015-09-01 14:35:53 -0400 | [diff] [blame] | 448 | final boolean vmute = row.ss.level == row.ss.levelMin; |
| 449 | mController.setStreamVolume(stream, |
| 450 | vmute ? row.lastAudibleLevel : row.ss.levelMin); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 451 | } |
| 452 | row.userAttempt = 0; // reset the grace period, slider should update immediately |
| 453 | } |
| 454 | }); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | public void destroy() { |
| 458 | mController.removeCallback(mControllerCallbackH); |
| 459 | } |
| 460 | |
| 461 | public void show(int reason) { |
| 462 | mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget(); |
| 463 | } |
| 464 | |
| 465 | public void dismiss(int reason) { |
| 466 | mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget(); |
| 467 | } |
| 468 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 469 | private void showH(int reason) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 470 | if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 471 | mHandler.removeMessages(H.SHOW); |
| 472 | mHandler.removeMessages(H.DISMISS); |
| 473 | rescheduleTimeoutH(); |
| 474 | if (mShowing) return; |
| 475 | mShowing = true; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 476 | mMotion.startShow(); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 477 | Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 478 | mController.notifyVisible(true); |
| 479 | } |
| 480 | |
| 481 | protected void rescheduleTimeoutH() { |
| 482 | mHandler.removeMessages(H.DISMISS); |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 483 | final int timeout = computeTimeoutH(); |
| 484 | mHandler.sendMessageDelayed(mHandler |
| 485 | .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 486 | if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 487 | mController.userActivity(); |
| 488 | } |
| 489 | |
| 490 | private int computeTimeoutH() { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 491 | if (mAccessibility.mFeedbackEnabled) return 20000; |
Jun Mukai | 3f5d713 | 2015-07-21 14:10:09 -0700 | [diff] [blame] | 492 | if (mHovering) return 16000; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 493 | if (mSafetyWarning != null) return 5000; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 494 | if (mExpanded || mExpandButtonAnimationRunning) return 5000; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 495 | if (mActiveStream == AudioManager.STREAM_MUSIC) return 1500; |
| 496 | return 3000; |
| 497 | } |
| 498 | |
| 499 | protected void dismissH(int reason) { |
Jason Monk | 305e0ba | 2015-07-06 15:53:59 -0400 | [diff] [blame] | 500 | if (mMotion.isAnimating()) { |
| 501 | return; |
| 502 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 503 | mHandler.removeMessages(H.DISMISS); |
| 504 | mHandler.removeMessages(H.SHOW); |
| 505 | if (!mShowing) return; |
| 506 | mShowing = false; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 507 | mMotion.startDismiss(new Runnable() { |
| 508 | @Override |
| 509 | public void run() { |
| 510 | setExpandedH(false); |
| 511 | } |
| 512 | }); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 513 | if (mAccessibilityMgr.isEnabled()) { |
| 514 | AccessibilityEvent event = |
| 515 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 516 | event.setPackageName(mContext.getPackageName()); |
| 517 | event.setClassName(CustomDialog.class.getSuperclass().getName()); |
| 518 | event.getText().add(mContext.getString( |
| 519 | R.string.volume_dialog_accessibility_dismissed_message)); |
| 520 | mAccessibilityMgr.sendAccessibilityEvent(event); |
| 521 | } |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 522 | Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 523 | mController.notifyVisible(false); |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 524 | synchronized (mSafetyWarningLock) { |
| 525 | if (mSafetyWarning != null) { |
| 526 | if (D.BUG) Log.d(TAG, "SafetyWarning dismissed"); |
| 527 | mSafetyWarning.dismiss(); |
| 528 | } |
| 529 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 530 | } |
| 531 | |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 532 | private void updateDialogBottomMarginH() { |
| 533 | final long diff = System.currentTimeMillis() - mCollapseTime; |
| 534 | final boolean collapsing = mCollapseTime != 0 && diff < getConservativeCollapseDuration(); |
| 535 | final ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) mDialogView.getLayoutParams(); |
| 536 | final int bottomMargin = collapsing ? mDialogContentView.getHeight() : |
| 537 | mContext.getResources().getDimensionPixelSize(R.dimen.volume_dialog_margin_bottom); |
| 538 | if (bottomMargin != mlp.bottomMargin) { |
| 539 | if (D.BUG) Log.d(TAG, "bottomMargin " + mlp.bottomMargin + " -> " + bottomMargin); |
| 540 | mlp.bottomMargin = bottomMargin; |
| 541 | mDialogView.setLayoutParams(mlp); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | private long getConservativeCollapseDuration() { |
| 546 | return mExpandButtonAnimationDuration * 3; |
| 547 | } |
| 548 | |
| 549 | private void prepareForCollapse() { |
| 550 | mHandler.removeMessages(H.UPDATE_BOTTOM_MARGIN); |
| 551 | mCollapseTime = System.currentTimeMillis(); |
| 552 | updateDialogBottomMarginH(); |
| 553 | mHandler.sendEmptyMessageDelayed(H.UPDATE_BOTTOM_MARGIN, getConservativeCollapseDuration()); |
| 554 | } |
| 555 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 556 | private void setExpandedH(boolean expanded) { |
| 557 | if (mExpanded == expanded) return; |
| 558 | mExpanded = expanded; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 559 | mExpandButtonAnimationRunning = isAttached(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 560 | if (D.BUG) Log.d(TAG, "setExpandedH " + expanded); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 561 | if (!mExpanded && mExpandButtonAnimationRunning) { |
| 562 | prepareForCollapse(); |
| 563 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 564 | updateRowsH(); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 565 | if (mExpandButtonAnimationRunning) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 566 | final Drawable d = mExpandButton.getDrawable(); |
| 567 | if (d instanceof AnimatedVectorDrawable) { |
| 568 | // workaround to reset drawable |
| 569 | final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) d.getConstantState() |
| 570 | .newDrawable(); |
| 571 | mExpandButton.setImageDrawable(avd); |
| 572 | avd.start(); |
| 573 | mHandler.postDelayed(new Runnable() { |
| 574 | @Override |
| 575 | public void run() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 576 | mExpandButtonAnimationRunning = false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 577 | updateExpandButtonH(); |
| 578 | rescheduleTimeoutH(); |
| 579 | } |
| 580 | }, mExpandButtonAnimationDuration); |
| 581 | } |
| 582 | } |
| 583 | rescheduleTimeoutH(); |
| 584 | } |
| 585 | |
| 586 | private void updateExpandButtonH() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 587 | if (D.BUG) Log.d(TAG, "updateExpandButtonH"); |
| 588 | mExpandButton.setClickable(!mExpandButtonAnimationRunning); |
| 589 | if (mExpandButtonAnimationRunning && isAttached()) return; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 590 | final int res = mExpanded ? R.drawable.ic_volume_collapse_animation |
| 591 | : R.drawable.ic_volume_expand_animation; |
Takayuki Hoshi | bb0ec55 | 2015-12-17 20:50:25 +0900 | [diff] [blame] | 592 | if (hasTouchFeature()) { |
| 593 | mExpandButton.setImageResource(res); |
| 594 | } else { |
| 595 | // if there is no touch feature, show the volume ringer instead |
| 596 | mExpandButton.setImageResource(R.drawable.ic_volume_ringer); |
| 597 | mExpandButton.setBackgroundResource(0); // remove gray background emphasis |
| 598 | } |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 599 | mExpandButton.setContentDescription(mContext.getString(mExpanded ? |
| 600 | R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand)); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | private boolean isVisibleH(VolumeRow row, boolean isActive) { |
| 604 | return mExpanded && row.view.getVisibility() == View.VISIBLE |
| 605 | || (mExpanded && (row.important || isActive)) |
| 606 | || !mExpanded && isActive; |
| 607 | } |
| 608 | |
| 609 | private void updateRowsH() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 610 | if (D.BUG) Log.d(TAG, "updateRowsH"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 611 | final VolumeRow activeRow = getActiveRow(); |
| 612 | updateFooterH(); |
| 613 | updateExpandButtonH(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 614 | if (!mShowing) { |
| 615 | trimObsoleteH(); |
| 616 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 617 | // apply changes to all rows |
| 618 | for (VolumeRow row : mRows) { |
| 619 | final boolean isActive = row == activeRow; |
| 620 | final boolean visible = isVisibleH(row, isActive); |
| 621 | Util.setVisOrGone(row.view, visible); |
| 622 | Util.setVisOrGone(row.space, visible && mExpanded); |
John Spurlock | 0265657 | 2015-06-10 09:33:34 -0400 | [diff] [blame] | 623 | updateVolumeRowHeaderVisibleH(row); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 624 | row.header.setAlpha(mExpanded && isActive ? 1 : 0.5f); |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 625 | updateVolumeRowSliderTintH(row, isActive); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | private void trimObsoleteH() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 630 | if (D.BUG) Log.d(TAG, "trimObsoleteH"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 631 | for (int i = mRows.size() -1; i >= 0; i--) { |
| 632 | final VolumeRow row = mRows.get(i); |
| 633 | if (row.ss == null || !row.ss.dynamic) continue; |
| 634 | if (!mDynamic.get(row.stream)) { |
| 635 | mRows.remove(i); |
Julia Reynolds | e80f675 | 2016-04-20 14:37:39 -0400 | [diff] [blame] | 636 | mDialogContentView.removeView(row.view); |
| 637 | mDialogContentView.removeView(row.space); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | private void onStateChangedH(State state) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 643 | final boolean animating = mMotion.isAnimating(); |
| 644 | if (D.BUG) Log.d(TAG, "onStateChangedH animating=" + animating); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 645 | mState = state; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 646 | if (animating) { |
| 647 | mPendingStateChanged = true; |
| 648 | return; |
| 649 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 650 | mDynamic.clear(); |
| 651 | // add any new dynamic rows |
| 652 | for (int i = 0; i < state.states.size(); i++) { |
| 653 | final int stream = state.states.keyAt(i); |
| 654 | final StreamState ss = state.states.valueAt(i); |
| 655 | if (!ss.dynamic) continue; |
| 656 | mDynamic.put(stream, true); |
| 657 | if (findRow(stream) == null) { |
| 658 | addRow(stream, R.drawable.ic_volume_remote, R.drawable.ic_volume_remote_mute, true); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (mActiveStream != state.activeStream) { |
| 663 | mActiveStream = state.activeStream; |
| 664 | updateRowsH(); |
| 665 | rescheduleTimeoutH(); |
| 666 | } |
| 667 | for (VolumeRow row : mRows) { |
| 668 | updateVolumeRowH(row); |
| 669 | } |
| 670 | updateFooterH(); |
| 671 | } |
| 672 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 673 | private void updateFooterH() { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 674 | if (D.BUG) Log.d(TAG, "updateFooterH"); |
| 675 | final boolean wasVisible = mZenFooter.getVisibility() == View.VISIBLE; |
Julia Reynolds | 6aa83b4 | 2015-09-08 13:41:45 -0400 | [diff] [blame] | 676 | final boolean visible = mState.zenMode != Global.ZEN_MODE_OFF |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 677 | && (mAudioManager.isStreamAffectedByRingerMode(mActiveStream) || mExpanded) |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 678 | && !mZenPanel.isEditing(); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 679 | if (wasVisible != visible && !visible) { |
| 680 | prepareForCollapse(); |
| 681 | } |
| 682 | Util.setVisOrGone(mZenFooter, visible); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 683 | mZenFooter.update(); |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 684 | |
| 685 | final boolean fullWasVisible = mZenPanel.getVisibility() == View.VISIBLE; |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 686 | final boolean fullVisible = mShowFullZen && !visible; |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 687 | if (fullWasVisible != fullVisible && !fullVisible) { |
| 688 | prepareForCollapse(); |
| 689 | } |
| 690 | Util.setVisOrGone(mZenPanel, fullVisible); |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 691 | if (fullVisible) { |
| 692 | mZenPanel.setZenState(mState.zenMode); |
| 693 | mZenPanel.setDoneListener(new OnClickListener() { |
| 694 | @Override |
| 695 | public void onClick(View v) { |
| 696 | prepareForCollapse(); |
| 697 | mHandler.sendEmptyMessage(H.UPDATE_FOOTER); |
| 698 | } |
| 699 | }); |
| 700 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | private void updateVolumeRowH(VolumeRow row) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 704 | if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 705 | if (mState == null) return; |
| 706 | final StreamState ss = mState.states.get(row.stream); |
| 707 | if (ss == null) return; |
| 708 | row.ss = ss; |
| 709 | if (ss.level > 0) { |
| 710 | row.lastAudibleLevel = ss.level; |
| 711 | } |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 712 | if (ss.level == row.requestedLevel) { |
| 713 | row.requestedLevel = -1; |
| 714 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 715 | final boolean isRingStream = row.stream == AudioManager.STREAM_RING; |
| 716 | final boolean isSystemStream = row.stream == AudioManager.STREAM_SYSTEM; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 717 | final boolean isAlarmStream = row.stream == AudioManager.STREAM_ALARM; |
| 718 | final boolean isMusicStream = row.stream == AudioManager.STREAM_MUSIC; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 719 | final boolean isRingVibrate = isRingStream |
| 720 | && mState.ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 721 | final boolean isRingSilent = isRingStream |
| 722 | && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT; |
| 723 | final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS; |
| 724 | final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 725 | final boolean isZenPriority = mState.zenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 726 | final boolean isRingZenNone = (isRingStream || isSystemStream) && isZenNone; |
| 727 | final boolean isRingLimited = isRingStream && isZenPriority; |
| 728 | final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream) |
| 729 | : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream) |
| 730 | : false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 731 | |
| 732 | // update slider max |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 733 | final int max = ss.levelMax * 100; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 734 | if (max != row.slider.getMax()) { |
| 735 | row.slider.setMax(max); |
| 736 | } |
| 737 | |
| 738 | // update header visible |
John Spurlock | 0265657 | 2015-06-10 09:33:34 -0400 | [diff] [blame] | 739 | updateVolumeRowHeaderVisibleH(row); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 740 | |
| 741 | // update header text |
John Spurlock | 0265657 | 2015-06-10 09:33:34 -0400 | [diff] [blame] | 742 | String text = ss.name; |
| 743 | if (mShowHeaders) { |
| 744 | if (isRingZenNone) { |
| 745 | text = mContext.getString(R.string.volume_stream_muted_dnd, ss.name); |
| 746 | } else if (isRingVibrate && isRingLimited) { |
| 747 | text = mContext.getString(R.string.volume_stream_vibrate_dnd, ss.name); |
| 748 | } else if (isRingVibrate) { |
| 749 | text = mContext.getString(R.string.volume_stream_vibrate, ss.name); |
| 750 | } else if (ss.muted || mAutomute && ss.level == 0) { |
| 751 | text = mContext.getString(R.string.volume_stream_muted, ss.name); |
| 752 | } else if (isRingLimited) { |
| 753 | text = mContext.getString(R.string.volume_stream_limited_dnd, ss.name); |
| 754 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 755 | } |
| 756 | Util.setText(row.header, text); |
| 757 | |
| 758 | // update icon |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 759 | final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 760 | row.icon.setEnabled(iconEnabled); |
| 761 | row.icon.setAlpha(iconEnabled ? 1 : 0.5f); |
| 762 | final int iconRes = |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 763 | isRingVibrate ? R.drawable.ic_volume_ringer_vibrate |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 764 | : isRingSilent || zenMuted ? row.cachedIconRes |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 765 | : ss.routedToBluetooth ? |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 766 | (ss.muted ? R.drawable.ic_volume_media_bt_mute |
| 767 | : R.drawable.ic_volume_media_bt) |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 768 | : mAutomute && ss.level == 0 ? row.iconMuteRes |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 769 | : (ss.muted ? row.iconMuteRes : row.iconRes); |
| 770 | if (iconRes != row.cachedIconRes) { |
| 771 | if (row.cachedIconRes != 0 && isRingVibrate) { |
| 772 | mController.vibrate(); |
| 773 | } |
| 774 | row.cachedIconRes = iconRes; |
| 775 | row.icon.setImageResource(iconRes); |
| 776 | } |
| 777 | row.iconState = |
| 778 | iconRes == R.drawable.ic_volume_ringer_vibrate ? Events.ICON_STATE_VIBRATE |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 779 | : (iconRes == R.drawable.ic_volume_media_bt_mute || iconRes == row.iconMuteRes) |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 780 | ? Events.ICON_STATE_MUTE |
John Spurlock | f15ef11 | 2015-04-09 22:04:14 -0400 | [diff] [blame] | 781 | : (iconRes == R.drawable.ic_volume_media_bt || iconRes == row.iconRes) |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 782 | ? Events.ICON_STATE_UNMUTE |
| 783 | : Events.ICON_STATE_UNKNOWN; |
Julia Reynolds | dfe0b4d | 2016-04-08 17:14:15 -0400 | [diff] [blame] | 784 | if (iconEnabled) { |
| 785 | if (isRingStream) { |
| 786 | if (isRingVibrate) { |
| 787 | row.icon.setContentDescription(mContext.getString( |
| 788 | R.string.volume_stream_content_description_unmute, |
| 789 | ss.name)); |
| 790 | } else { |
| 791 | if (mController.hasVibrator()) { |
| 792 | row.icon.setContentDescription(mContext.getString( |
| 793 | R.string.volume_stream_content_description_vibrate, |
| 794 | ss.name)); |
| 795 | } else { |
| 796 | row.icon.setContentDescription(mContext.getString( |
| 797 | R.string.volume_stream_content_description_mute, |
| 798 | ss.name)); |
| 799 | } |
| 800 | } |
| 801 | } else { |
| 802 | if (ss.muted || mAutomute && ss.level == 0) { |
| 803 | row.icon.setContentDescription(mContext.getString( |
| 804 | R.string.volume_stream_content_description_unmute, |
| 805 | ss.name)); |
| 806 | } else { |
| 807 | row.icon.setContentDescription(mContext.getString( |
| 808 | R.string.volume_stream_content_description_mute, |
| 809 | ss.name)); |
| 810 | } |
| 811 | } |
| 812 | } else { |
| 813 | row.icon.setContentDescription(ss.name); |
| 814 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 815 | |
| 816 | // update slider |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 817 | final boolean enableSlider = !zenMuted; |
| 818 | final int vlevel = row.ss.muted && (isRingVibrate || !isRingStream && !zenMuted) ? 0 |
| 819 | : row.ss.level; |
| 820 | updateVolumeRowSliderH(row, enableSlider, vlevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 821 | } |
| 822 | |
John Spurlock | 0265657 | 2015-06-10 09:33:34 -0400 | [diff] [blame] | 823 | private void updateVolumeRowHeaderVisibleH(VolumeRow row) { |
| 824 | final boolean dynamic = row.ss != null && row.ss.dynamic; |
| 825 | final boolean showHeaders = mShowHeaders || mExpanded && dynamic; |
| 826 | if (row.cachedShowHeaders != showHeaders) { |
| 827 | row.cachedShowHeaders = showHeaders; |
| 828 | Util.setVisOrGone(row.header, showHeaders); |
| 829 | } |
| 830 | } |
| 831 | |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 832 | private void updateVolumeRowSliderTintH(VolumeRow row, boolean isActive) { |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 833 | if (isActive && mExpanded) { |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 834 | row.slider.requestFocus(); |
John Spurlock | 67a0f85 | 2015-06-15 15:35:47 -0400 | [diff] [blame] | 835 | } |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 836 | final ColorStateList tint = isActive && row.slider.isEnabled() ? mActiveSliderTint |
| 837 | : mInactiveSliderTint; |
| 838 | if (tint == row.cachedSliderTint) return; |
| 839 | row.cachedSliderTint = tint; |
| 840 | row.slider.setProgressTintList(tint); |
| 841 | row.slider.setThumbTintList(tint); |
| 842 | } |
| 843 | |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 844 | private void updateVolumeRowSliderH(VolumeRow row, boolean enable, int vlevel) { |
| 845 | row.slider.setEnabled(enable); |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 846 | updateVolumeRowSliderTintH(row, row.stream == mActiveStream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 847 | if (row.tracking) { |
| 848 | return; // don't update if user is sliding |
| 849 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 850 | final int progress = row.slider.getProgress(); |
| 851 | final int level = getImpliedLevel(row.slider, progress); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 852 | final boolean rowVisible = row.view.getVisibility() == View.VISIBLE; |
| 853 | final boolean inGracePeriod = (SystemClock.uptimeMillis() - row.userAttempt) |
| 854 | < USER_ATTEMPT_GRACE_PERIOD; |
| 855 | mHandler.removeMessages(H.RECHECK, row); |
| 856 | if (mShowing && rowVisible && inGracePeriod) { |
| 857 | if (D.BUG) Log.d(TAG, "inGracePeriod"); |
| 858 | mHandler.sendMessageAtTime(mHandler.obtainMessage(H.RECHECK, row), |
| 859 | row.userAttempt + USER_ATTEMPT_GRACE_PERIOD); |
| 860 | return; // don't update if visible and in grace period |
| 861 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 862 | if (vlevel == level) { |
| 863 | if (mShowing && rowVisible) { |
| 864 | return; // don't clamp if visible |
| 865 | } |
| 866 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 867 | final int newProgress = vlevel * 100; |
| 868 | if (progress != newProgress) { |
| 869 | if (mShowing && rowVisible) { |
| 870 | // animate! |
| 871 | if (row.anim != null && row.anim.isRunning() |
| 872 | && row.animTargetProgress == newProgress) { |
| 873 | return; // already animating to the target progress |
| 874 | } |
| 875 | // start/update animation |
| 876 | if (row.anim == null) { |
| 877 | row.anim = ObjectAnimator.ofInt(row.slider, "progress", progress, newProgress); |
| 878 | row.anim.setInterpolator(new DecelerateInterpolator()); |
| 879 | } else { |
| 880 | row.anim.cancel(); |
| 881 | row.anim.setIntValues(progress, newProgress); |
| 882 | } |
| 883 | row.animTargetProgress = newProgress; |
| 884 | row.anim.setDuration(UPDATE_ANIMATION_DURATION); |
| 885 | row.anim.start(); |
| 886 | } else { |
| 887 | // update slider directly to clamped value |
| 888 | if (row.anim != null) { |
| 889 | row.anim.cancel(); |
| 890 | } |
| 891 | row.slider.setProgress(newProgress); |
| 892 | } |
| 893 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | private void recheckH(VolumeRow row) { |
| 897 | if (row == null) { |
| 898 | if (D.BUG) Log.d(TAG, "recheckH ALL"); |
| 899 | trimObsoleteH(); |
| 900 | for (VolumeRow r : mRows) { |
| 901 | updateVolumeRowH(r); |
| 902 | } |
| 903 | } else { |
| 904 | if (D.BUG) Log.d(TAG, "recheckH " + row.stream); |
| 905 | updateVolumeRowH(row); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | private void setStreamImportantH(int stream, boolean important) { |
| 910 | for (VolumeRow row : mRows) { |
| 911 | if (row.stream == stream) { |
| 912 | row.important = important; |
| 913 | return; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 918 | private void showSafetyWarningH(int flags) { |
| 919 | if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0 |
| 920 | || mShowing) { |
| 921 | synchronized (mSafetyWarningLock) { |
| 922 | if (mSafetyWarning != null) { |
| 923 | return; |
| 924 | } |
| 925 | mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) { |
| 926 | @Override |
| 927 | protected void cleanUp() { |
| 928 | synchronized (mSafetyWarningLock) { |
| 929 | mSafetyWarning = null; |
| 930 | } |
| 931 | recheckH(null); |
| 932 | } |
| 933 | }; |
| 934 | mSafetyWarning.show(); |
| 935 | } |
| 936 | recheckH(null); |
| 937 | } |
| 938 | rescheduleTimeoutH(); |
| 939 | } |
| 940 | |
Takayuki Hoshi | bb0ec55 | 2015-12-17 20:50:25 +0900 | [diff] [blame] | 941 | private boolean hasTouchFeature() { |
| 942 | final PackageManager pm = mContext.getPackageManager(); |
| 943 | return pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 944 | } |
| 945 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 946 | private final VolumeDialogController.Callbacks mControllerCallbackH |
| 947 | = new VolumeDialogController.Callbacks() { |
| 948 | @Override |
| 949 | public void onShowRequested(int reason) { |
| 950 | showH(reason); |
| 951 | } |
| 952 | |
| 953 | @Override |
| 954 | public void onDismissRequested(int reason) { |
| 955 | dismissH(reason); |
| 956 | } |
| 957 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 958 | @Override |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 959 | public void onScreenOff() { |
| 960 | dismissH(Events.DISMISS_REASON_SCREEN_OFF); |
| 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public void onStateChanged(State state) { |
| 965 | onStateChangedH(state); |
| 966 | } |
| 967 | |
| 968 | @Override |
| 969 | public void onLayoutDirectionChanged(int layoutDirection) { |
| 970 | mDialogView.setLayoutDirection(layoutDirection); |
| 971 | } |
| 972 | |
| 973 | @Override |
| 974 | public void onConfigurationChanged() { |
Julia Reynolds | c791e05 | 2016-02-19 16:02:01 -0500 | [diff] [blame] | 975 | Configuration newConfig = mContext.getResources().getConfiguration(); |
| 976 | final int density = newConfig.densityDpi; |
| 977 | if (density != mDensity) { |
| 978 | mDialog.dismiss(); |
| 979 | initDialog(); |
| 980 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 981 | updateWindowWidthH(); |
| 982 | mSpTexts.update(); |
John Spurlock | 9ea3a1e | 2015-05-06 17:12:18 -0400 | [diff] [blame] | 983 | mZenFooter.onConfigurationChanged(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public void onShowVibrateHint() { |
| 988 | if (mSilentMode) { |
| 989 | mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false); |
| 990 | } |
| 991 | } |
| 992 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 993 | @Override |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 994 | public void onShowSilentHint() { |
| 995 | if (mSilentMode) { |
| 996 | mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false); |
| 997 | } |
| 998 | } |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 999 | |
| 1000 | @Override |
| 1001 | public void onShowSafetyWarning(int flags) { |
| 1002 | showSafetyWarningH(flags); |
| 1003 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1004 | }; |
| 1005 | |
Jason Monk | e138f55 | 2016-01-18 09:21:45 -0500 | [diff] [blame] | 1006 | private final ZenModePanel.Callback mZenPanelCallback = new ZenModePanel.Callback() { |
| 1007 | @Override |
| 1008 | public void onPrioritySettings() { |
| 1009 | mCallback.onZenPrioritySettingsClicked(); |
| 1010 | } |
| 1011 | |
| 1012 | @Override |
| 1013 | public void onInteraction() { |
| 1014 | mHandler.sendEmptyMessage(H.RESCHEDULE_TIMEOUT); |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public void onExpanded(boolean expanded) { |
| 1019 | // noop. |
| 1020 | } |
| 1021 | }; |
| 1022 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1023 | private final OnClickListener mClickExpand = new OnClickListener() { |
| 1024 | @Override |
| 1025 | public void onClick(View v) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1026 | if (mExpandButtonAnimationRunning) return; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1027 | final boolean newExpand = !mExpanded; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 1028 | Events.writeEvent(mContext, Events.EVENT_EXPAND, newExpand); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1029 | setExpandedH(newExpand); |
| 1030 | } |
| 1031 | }; |
| 1032 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1033 | private final class H extends Handler { |
| 1034 | private static final int SHOW = 1; |
| 1035 | private static final int DISMISS = 2; |
| 1036 | private static final int RECHECK = 3; |
| 1037 | private static final int RECHECK_ALL = 4; |
| 1038 | private static final int SET_STREAM_IMPORTANT = 5; |
| 1039 | private static final int RESCHEDULE_TIMEOUT = 6; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1040 | private static final int STATE_CHANGED = 7; |
| 1041 | private static final int UPDATE_BOTTOM_MARGIN = 8; |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 1042 | private static final int UPDATE_FOOTER = 9; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1043 | |
| 1044 | public H() { |
| 1045 | super(Looper.getMainLooper()); |
| 1046 | } |
| 1047 | |
| 1048 | @Override |
| 1049 | public void handleMessage(Message msg) { |
| 1050 | switch (msg.what) { |
| 1051 | case SHOW: showH(msg.arg1); break; |
| 1052 | case DISMISS: dismissH(msg.arg1); break; |
| 1053 | case RECHECK: recheckH((VolumeRow) msg.obj); break; |
| 1054 | case RECHECK_ALL: recheckH(null); break; |
| 1055 | case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break; |
| 1056 | case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break; |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1057 | case STATE_CHANGED: onStateChangedH(mState); break; |
| 1058 | case UPDATE_BOTTOM_MARGIN: updateDialogBottomMarginH(); break; |
Jason Monk | aa911b3 | 2016-02-10 21:01:30 -0500 | [diff] [blame] | 1059 | case UPDATE_FOOTER: updateFooterH(); break; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | private final class CustomDialog extends Dialog { |
| 1065 | public CustomDialog(Context context) { |
| 1066 | super(context); |
| 1067 | } |
| 1068 | |
| 1069 | @Override |
| 1070 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 1071 | rescheduleTimeoutH(); |
| 1072 | return super.dispatchTouchEvent(ev); |
| 1073 | } |
| 1074 | |
| 1075 | @Override |
| 1076 | protected void onStop() { |
| 1077 | super.onStop(); |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1078 | final boolean animating = mMotion.isAnimating(); |
| 1079 | if (D.BUG) Log.d(TAG, "onStop animating=" + animating); |
| 1080 | if (animating) { |
| 1081 | mPendingRecheckAll = true; |
| 1082 | return; |
| 1083 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1084 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public boolean onTouchEvent(MotionEvent event) { |
| 1089 | if (isShowing()) { |
| 1090 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
| 1091 | dismissH(Events.DISMISS_REASON_TOUCH_OUTSIDE); |
| 1092 | return true; |
| 1093 | } |
| 1094 | } |
| 1095 | return false; |
| 1096 | } |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 1097 | |
| 1098 | @Override |
| 1099 | public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) { |
| 1100 | event.setClassName(getClass().getSuperclass().getName()); |
| 1101 | event.setPackageName(mContext.getPackageName()); |
| 1102 | |
| 1103 | ViewGroup.LayoutParams params = getWindow().getAttributes(); |
| 1104 | boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) && |
| 1105 | (params.height == ViewGroup.LayoutParams.MATCH_PARENT); |
| 1106 | event.setFullScreen(isFullScreen); |
| 1107 | |
| 1108 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 1109 | if (mShowing) { |
| 1110 | event.getText().add(mContext.getString( |
| 1111 | R.string.volume_dialog_accessibility_shown_message, |
| 1112 | getActiveRow().ss.name)); |
| 1113 | return true; |
| 1114 | } |
| 1115 | } |
| 1116 | return false; |
| 1117 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener { |
| 1121 | private final VolumeRow mRow; |
| 1122 | |
| 1123 | private VolumeSeekBarChangeListener(VolumeRow row) { |
| 1124 | mRow = row; |
| 1125 | } |
| 1126 | |
| 1127 | @Override |
| 1128 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 1129 | if (mRow.ss == null) return; |
| 1130 | if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream) |
| 1131 | + " onProgressChanged " + progress + " fromUser=" + fromUser); |
| 1132 | if (!fromUser) return; |
| 1133 | if (mRow.ss.levelMin > 0) { |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1134 | final int minProgress = mRow.ss.levelMin * 100; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1135 | if (progress < minProgress) { |
| 1136 | seekBar.setProgress(minProgress); |
Julia Reynolds | 798b0c3 | 2015-09-01 14:35:53 -0400 | [diff] [blame] | 1137 | progress = minProgress; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1138 | } |
| 1139 | } |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1140 | final int userLevel = getImpliedLevel(seekBar, progress); |
| 1141 | if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1142 | mRow.userAttempt = SystemClock.uptimeMillis(); |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1143 | if (mRow.requestedLevel != userLevel) { |
| 1144 | mController.setStreamVolume(mRow.stream, userLevel); |
| 1145 | mRow.requestedLevel = userLevel; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 1146 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream, |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1147 | userLevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | @Override |
| 1153 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 1154 | if (D.BUG) Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream); |
| 1155 | mController.setActiveStream(mRow.stream); |
| 1156 | mRow.tracking = true; |
| 1157 | } |
| 1158 | |
| 1159 | @Override |
| 1160 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 1161 | if (D.BUG) Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream); |
| 1162 | mRow.tracking = false; |
| 1163 | mRow.userAttempt = SystemClock.uptimeMillis(); |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1164 | final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress()); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 1165 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1166 | if (mRow.ss.level != userLevel) { |
| 1167 | mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow), |
| 1168 | USER_ATTEMPT_GRACE_PERIOD); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 1173 | private final class Accessibility extends AccessibilityDelegate { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1174 | private boolean mFeedbackEnabled; |
| 1175 | |
| 1176 | public void init() { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1177 | mDialogView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { |
| 1178 | @Override |
| 1179 | public void onViewDetachedFromWindow(View v) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1180 | if (D.BUG) Log.d(TAG, "onViewDetachedFromWindow"); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | @Override |
| 1184 | public void onViewAttachedToWindow(View v) { |
John Spurlock | 22def3d | 2015-06-17 11:56:12 -0400 | [diff] [blame] | 1185 | if (D.BUG) Log.d(TAG, "onViewAttachedToWindow"); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1186 | updateFeedbackEnabled(); |
| 1187 | } |
| 1188 | }); |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 1189 | mDialogView.setAccessibilityDelegate(this); |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 1190 | mAccessibilityMgr.addAccessibilityStateChangeListener( |
| 1191 | new AccessibilityStateChangeListener() { |
| 1192 | @Override |
| 1193 | public void onAccessibilityStateChanged(boolean enabled) { |
| 1194 | updateFeedbackEnabled(); |
| 1195 | } |
| 1196 | }); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1197 | updateFeedbackEnabled(); |
| 1198 | } |
| 1199 | |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 1200 | @Override |
| 1201 | public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, |
| 1202 | AccessibilityEvent event) { |
| 1203 | rescheduleTimeoutH(); |
| 1204 | return super.onRequestSendAccessibilityEvent(host, child, event); |
| 1205 | } |
| 1206 | |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1207 | private void updateFeedbackEnabled() { |
| 1208 | mFeedbackEnabled = computeFeedbackEnabled(); |
| 1209 | } |
| 1210 | |
| 1211 | private boolean computeFeedbackEnabled() { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 1212 | // are there any enabled non-generic a11y services? |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1213 | final List<AccessibilityServiceInfo> services = |
Julia Reynolds | 2feab16 | 2016-04-21 14:58:52 -0400 | [diff] [blame^] | 1214 | mAccessibilityMgr.getEnabledAccessibilityServiceList(FEEDBACK_ALL_MASK); |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1215 | for (AccessibilityServiceInfo asi : services) { |
John Spurlock | f09838b | 2015-06-11 16:14:44 -0400 | [diff] [blame] | 1216 | if (asi.feedbackType != 0 && asi.feedbackType != FEEDBACK_GENERIC) { |
John Spurlock | 5adeabc | 2015-05-05 14:50:43 -0400 | [diff] [blame] | 1217 | return true; |
| 1218 | } |
| 1219 | } |
| 1220 | return false; |
| 1221 | } |
| 1222 | } |
| 1223 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1224 | private static class VolumeRow { |
| 1225 | private View view; |
| 1226 | private View space; |
| 1227 | private TextView header; |
| 1228 | private ImageButton icon; |
| 1229 | private SeekBar slider; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1230 | private int stream; |
| 1231 | private StreamState ss; |
| 1232 | private long userAttempt; // last user-driven slider change |
| 1233 | private boolean tracking; // tracking slider touch |
John Spurlock | b0a2e5f | 2015-06-09 14:41:52 -0400 | [diff] [blame] | 1234 | private int requestedLevel = -1; // pending user-requested level via progress changed |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1235 | private int iconRes; |
| 1236 | private int iconMuteRes; |
| 1237 | private boolean important; |
| 1238 | private int cachedIconRes; |
John Spurlock | ecc8058 | 2015-05-13 16:23:14 -0400 | [diff] [blame] | 1239 | private ColorStateList cachedSliderTint; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1240 | private int iconState; // from Events |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1241 | private boolean cachedShowHeaders = VolumePrefs.DEFAULT_SHOW_HEADERS; |
Julia Reynolds | 4b006a8 | 2016-04-20 16:09:06 -0400 | [diff] [blame] | 1242 | private ObjectAnimator anim; // slider progress animation for non-touch-related updates |
| 1243 | private int animTargetProgress; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1244 | private int lastAudibleLevel = 1; |
| 1245 | } |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1246 | |
| 1247 | public interface Callback { |
John Spurlock | f55b7f2 | 2015-04-13 19:21:26 -0400 | [diff] [blame] | 1248 | void onZenSettingsClicked(); |
| 1249 | void onZenPrioritySettingsClicked(); |
| 1250 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1251 | } |