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