Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 20 | import android.app.Dialog; |
| 21 | import android.app.KeyguardManager; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 24 | import android.graphics.Color; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.ColorDrawable; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 26 | import android.graphics.PixelFormat; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 27 | import android.media.AudioManager; |
| 28 | import android.media.AudioSystem; |
| 29 | import android.os.Debug; |
| 30 | import android.os.Handler; |
| 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 33 | import android.util.Log; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 34 | import android.util.SparseBooleanArray; |
| 35 | import android.view.ContextThemeWrapper; |
| 36 | import android.view.Gravity; |
| 37 | import android.view.MotionEvent; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewGroup; |
| 40 | import android.view.Window; |
| 41 | import android.view.WindowManager; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 42 | import android.widget.SeekBar; |
| 43 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 44 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 45 | import androidx.car.widget.ListItem; |
| 46 | import androidx.car.widget.ListItemAdapter; |
| 47 | import androidx.car.widget.ListItemAdapter.BackgroundStyle; |
| 48 | import androidx.car.widget.ListItemProvider.ListProvider; |
| 49 | import androidx.car.widget.PagedListView; |
| 50 | import androidx.car.widget.SeekbarListItem; |
| 51 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 52 | import java.io.PrintWriter; |
| 53 | import java.util.ArrayList; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 54 | import java.util.Iterator; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 55 | import java.util.List; |
| 56 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 57 | import com.android.systemui.Dependency; |
| 58 | import com.android.systemui.R; |
| 59 | import com.android.systemui.plugins.VolumeDialog; |
| 60 | import com.android.systemui.plugins.VolumeDialogController; |
| 61 | import com.android.systemui.plugins.VolumeDialogController.State; |
| 62 | import com.android.systemui.plugins.VolumeDialogController.StreamState; |
| 63 | |
| 64 | /** |
| 65 | * Car version of the volume dialog. |
| 66 | * |
| 67 | * A client of VolumeDialogControllerImpl and its state model. |
| 68 | * |
| 69 | * Methods ending in "H" must be called on the (ui) handler. |
| 70 | */ |
| 71 | public class CarVolumeDialogImpl implements VolumeDialog { |
| 72 | private static final String TAG = Util.logTag(CarVolumeDialogImpl.class); |
| 73 | |
| 74 | private static final long USER_ATTEMPT_GRACE_PERIOD = 1000; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 75 | |
| 76 | private final Context mContext; |
| 77 | private final H mHandler = new H(); |
| 78 | private final VolumeDialogController mController; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 79 | private final AudioManager mAudioManager; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 80 | |
| 81 | private Window mWindow; |
| 82 | private CustomDialog mDialog; |
| 83 | private ViewGroup mDialogView; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 84 | private PagedListView mListView; |
| 85 | private ListItemAdapter mPagedListAdapter; |
| 86 | private final List<ListItem> mVolumeLineItems = new ArrayList<>(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 87 | private final List<VolumeRow> mRows = new ArrayList<>(); |
| 88 | private ConfigurableTexts mConfigurableTexts; |
| 89 | private final SparseBooleanArray mDynamic = new SparseBooleanArray(); |
| 90 | private final KeyguardManager mKeyguard; |
| 91 | private final Object mSafetyWarningLock = new Object(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 92 | |
| 93 | private boolean mShowing; |
| 94 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 95 | private boolean mAutomute = VolumePrefs.DEFAULT_ENABLE_AUTOMUTE; |
| 96 | private boolean mSilentMode = VolumePrefs.DEFAULT_ENABLE_SILENT_MODE; |
| 97 | private State mState; |
| 98 | private SafetyWarningDialog mSafetyWarning; |
| 99 | private boolean mHovering = false; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 100 | private boolean mExpanded; |
| 101 | |
| 102 | private final View.OnClickListener mSupplementalIconListener = v -> { |
| 103 | mExpanded = !mExpanded; |
| 104 | if (mExpanded) { |
| 105 | for (VolumeRow row : mRows) { |
| 106 | // Adding the items which are not coming from default stream. |
| 107 | if (!row.defaultStream) { |
| 108 | addSeekbarListItem(row, null); |
| 109 | } |
| 110 | } |
| 111 | } else { |
| 112 | // Only keeping the default stream if it is not expended. |
| 113 | Iterator itr = mVolumeLineItems.iterator(); |
| 114 | while (itr.hasNext()) { |
| 115 | SeekbarListItem item = (SeekbarListItem) itr.next(); |
| 116 | VolumeRow row = findRow(item); |
| 117 | if (!row.defaultStream) { |
| 118 | itr.remove(); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | mPagedListAdapter.notifyDataSetChanged(); |
| 123 | }; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 124 | |
| 125 | public CarVolumeDialogImpl(Context context) { |
| 126 | mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme); |
| 127 | mController = Dependency.get(VolumeDialogController.class); |
| 128 | mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 129 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public void init(int windowType, Callback callback) { |
| 133 | initDialog(); |
| 134 | |
| 135 | mController.addCallback(mControllerCallbackH, mHandler); |
| 136 | mController.getState(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void destroy() { |
| 141 | mController.removeCallback(mControllerCallbackH); |
| 142 | mHandler.removeCallbacksAndMessages(null); |
| 143 | } |
| 144 | |
| 145 | private void initDialog() { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 146 | mRows.clear(); |
| 147 | mVolumeLineItems.clear(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 148 | mDialog = new CustomDialog(mContext); |
| 149 | |
| 150 | mConfigurableTexts = new ConfigurableTexts(mContext); |
| 151 | mHovering = false; |
| 152 | mShowing = false; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 153 | mExpanded = false; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 154 | mWindow = mDialog.getWindow(); |
| 155 | mWindow.requestFeature(Window.FEATURE_NO_TITLE); |
| 156 | mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 157 | mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 158 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); |
| 159 | mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 160 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 161 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 162 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 163 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 164 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 165 | mWindow.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); |
| 166 | mWindow.setWindowAnimations(com.android.internal.R.style.Animation_Toast); |
| 167 | final WindowManager.LayoutParams lp = mWindow.getAttributes(); |
| 168 | lp.format = PixelFormat.TRANSLUCENT; |
| 169 | lp.setTitle(VolumeDialogImpl.class.getSimpleName()); |
| 170 | lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; |
| 171 | lp.windowAnimations = -1; |
| 172 | mWindow.setAttributes(lp); |
| 173 | mWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 174 | |
| 175 | mDialog.setCanceledOnTouchOutside(true); |
| 176 | mDialog.setContentView(R.layout.car_volume_dialog); |
| 177 | mDialog.setOnShowListener(dialog -> { |
| 178 | mDialogView.setTranslationY(-mDialogView.getHeight()); |
| 179 | mDialogView.setAlpha(0); |
| 180 | mDialogView.animate() |
| 181 | .alpha(1) |
| 182 | .translationY(0) |
| 183 | .setDuration(300) |
| 184 | .setInterpolator(new SystemUIInterpolators.LogDecelerateInterpolator()) |
| 185 | .start(); |
| 186 | }); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 187 | mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 188 | mDialogView.setOnHoverListener((v, event) -> { |
| 189 | int action = event.getActionMasked(); |
| 190 | mHovering = (action == MotionEvent.ACTION_HOVER_ENTER) |
| 191 | || (action == MotionEvent.ACTION_HOVER_MOVE); |
| 192 | rescheduleTimeoutH(); |
| 193 | return true; |
| 194 | }); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 195 | mListView = (PagedListView) mWindow.findViewById(R.id.volume_list); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 196 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 197 | // TODO: apply tint to the supplement icon. |
| 198 | addSeekbarListItem(addVolumeRow(AudioManager.STREAM_MUSIC, R.drawable.ic_volume_media, |
| 199 | R.drawable.car_ic_arrow_drop_up, true, true), mSupplementalIconListener); |
| 200 | addVolumeRow(AudioManager.STREAM_RING, R.drawable.ic_volume_ringer, 0, |
| 201 | true, false); |
| 202 | addVolumeRow(AudioManager.STREAM_ALARM, R.drawable.ic_volume_alarm, 0, |
| 203 | true, false); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 204 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 205 | mPagedListAdapter = new ListItemAdapter(mContext, new ListProvider(mVolumeLineItems), |
| 206 | BackgroundStyle.PANEL); |
| 207 | mListView.setAdapter(mPagedListAdapter); |
| 208 | mListView.setMaxPages(PagedListView.UNLIMITED_PAGES); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public void setStreamImportant(int stream, boolean important) { |
| 212 | mHandler.obtainMessage(H.SET_STREAM_IMPORTANT, stream, important ? 1 : 0).sendToTarget(); |
| 213 | } |
| 214 | |
| 215 | public void setAutomute(boolean automute) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 216 | if (mAutomute == automute) { |
| 217 | return; |
| 218 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 219 | mAutomute = automute; |
| 220 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 221 | } |
| 222 | |
| 223 | public void setSilentMode(boolean silentMode) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 224 | if (mSilentMode == silentMode) { |
| 225 | return; |
| 226 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 227 | mSilentMode = silentMode; |
| 228 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 229 | } |
| 230 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 231 | private VolumeRow addVolumeRow(int stream, int primaryActionIcon, int supplementalIcon, |
| 232 | boolean important, boolean defaultStream) { |
| 233 | VolumeRow volumeRow = new VolumeRow(); |
| 234 | volumeRow.stream = stream; |
| 235 | volumeRow.primaryActionIcon = primaryActionIcon; |
| 236 | volumeRow.supplementalIcon = supplementalIcon; |
| 237 | volumeRow.important = important; |
| 238 | volumeRow.defaultStream = defaultStream; |
| 239 | volumeRow.listItem = null; |
| 240 | mRows.add(volumeRow); |
| 241 | return volumeRow; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 244 | private SeekbarListItem addSeekbarListItem( |
| 245 | VolumeRow volumeRow, @Nullable View.OnClickListener supplementalIconOnClickListener) { |
| 246 | int volumeMax = mAudioManager.getStreamMaxVolume(volumeRow.stream); |
| 247 | int currentVolume = mAudioManager.getStreamVolume(volumeRow.stream); |
| 248 | SeekbarListItem listItem = |
| 249 | new SeekbarListItem(mContext, volumeMax, currentVolume, |
| 250 | new VolumeSeekBarChangeListener(volumeRow), null); |
| 251 | listItem.setPrimaryActionIcon(volumeRow.primaryActionIcon); |
| 252 | if (volumeRow.supplementalIcon != 0) { |
| 253 | listItem.setSupplementalIcon(volumeRow.supplementalIcon, true, supplementalIconOnClickListener); |
| 254 | } else { |
| 255 | listItem.setSupplementalEmptyIcon(true); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 256 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 257 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 258 | mVolumeLineItems.add(listItem); |
| 259 | volumeRow.listItem = listItem; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 260 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 261 | return listItem; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private static int getImpliedLevel(SeekBar seekBar, int progress) { |
| 265 | final int m = seekBar.getMax(); |
| 266 | final int n = m / 100 - 1; |
| 267 | final int level = progress == 0 ? 0 |
| 268 | : progress == m ? (m / 100) : (1 + (int)((progress / (float) m) * n)); |
| 269 | return level; |
| 270 | } |
| 271 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 272 | public void show(int reason) { |
| 273 | mHandler.obtainMessage(H.SHOW, reason, 0).sendToTarget(); |
| 274 | } |
| 275 | |
| 276 | public void dismiss(int reason) { |
| 277 | mHandler.obtainMessage(H.DISMISS, reason, 0).sendToTarget(); |
| 278 | } |
| 279 | |
| 280 | private void showH(int reason) { |
| 281 | if (D.BUG) Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]); |
| 282 | mHandler.removeMessages(H.SHOW); |
| 283 | mHandler.removeMessages(H.DISMISS); |
| 284 | rescheduleTimeoutH(); |
| 285 | if (mShowing) return; |
| 286 | mShowing = true; |
| 287 | |
| 288 | mDialog.show(); |
| 289 | Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked()); |
| 290 | mController.notifyVisible(true); |
| 291 | } |
| 292 | |
| 293 | protected void rescheduleTimeoutH() { |
| 294 | mHandler.removeMessages(H.DISMISS); |
| 295 | final int timeout = computeTimeoutH(); |
| 296 | mHandler.sendMessageDelayed(mHandler |
| 297 | .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT, 0), timeout); |
| 298 | if (D.BUG) Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller()); |
| 299 | mController.userActivity(); |
| 300 | } |
| 301 | |
| 302 | private int computeTimeoutH() { |
| 303 | if (mHovering) return 16000; |
| 304 | if (mSafetyWarning != null) return 5000; |
| 305 | return 3000; |
| 306 | } |
| 307 | |
| 308 | protected void dismissH(int reason) { |
| 309 | mHandler.removeMessages(H.DISMISS); |
| 310 | mHandler.removeMessages(H.SHOW); |
| 311 | if (!mShowing) return; |
| 312 | mDialogView.animate().cancel(); |
| 313 | mShowing = false; |
| 314 | |
| 315 | mDialogView.setTranslationY(0); |
| 316 | mDialogView.setAlpha(1); |
| 317 | mDialogView.animate() |
| 318 | .alpha(0) |
| 319 | .translationY(-mDialogView.getHeight()) |
| 320 | .setDuration(250) |
| 321 | .setInterpolator(new SystemUIInterpolators.LogAccelerateInterpolator()) |
| 322 | .withEndAction(() -> mHandler.postDelayed(() -> { |
| 323 | if (D.BUG) Log.d(TAG, "mDialog.dismiss()"); |
| 324 | mDialog.dismiss(); |
| 325 | }, 50)) |
| 326 | .start(); |
| 327 | |
| 328 | Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason); |
| 329 | mController.notifyVisible(false); |
| 330 | synchronized (mSafetyWarningLock) { |
| 331 | if (mSafetyWarning != null) { |
| 332 | if (D.BUG) Log.d(TAG, "SafetyWarning dismissed"); |
| 333 | mSafetyWarning.dismiss(); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 338 | private void trimObsoleteH() { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 339 | int initialVolumeItemSize = mVolumeLineItems.size(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 340 | for (int i = mRows.size() - 1; i >= 0; i--) { |
| 341 | final VolumeRow row = mRows.get(i); |
| 342 | if (row.ss == null || !row.ss.dynamic) continue; |
| 343 | if (!mDynamic.get(row.stream)) { |
| 344 | mRows.remove(i); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 345 | mVolumeLineItems.remove(row.listItem); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 348 | |
| 349 | if (mVolumeLineItems.size() != initialVolumeItemSize) { |
| 350 | mPagedListAdapter.notifyDataSetChanged(); |
| 351 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 354 | private void onStateChangedH(State state) { |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 355 | mState = state; |
| 356 | mDynamic.clear(); |
| 357 | // add any new dynamic rows |
| 358 | for (int i = 0; i < state.states.size(); i++) { |
| 359 | final int stream = state.states.keyAt(i); |
| 360 | final StreamState ss = state.states.valueAt(i); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 361 | if (!ss.dynamic) { |
| 362 | continue; |
| 363 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 364 | mDynamic.put(stream, true); |
| 365 | if (findRow(stream) == null) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 366 | VolumeRow row = addVolumeRow(stream, R.drawable.ic_volume_remote, |
| 367 | 0, true,false); |
| 368 | if (mExpanded) { |
| 369 | addSeekbarListItem(row, null); |
| 370 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 374 | for (VolumeRow row : mRows) { |
| 375 | updateVolumeRowH(row); |
| 376 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | private void updateVolumeRowH(VolumeRow row) { |
| 380 | if (D.BUG) Log.d(TAG, "updateVolumeRowH s=" + row.stream); |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 381 | if (mState == null) { |
| 382 | return; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 383 | } |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 384 | final StreamState ss = mState.states.get(row.stream); |
| 385 | if (ss == null) { |
| 386 | return; |
| 387 | } |
| 388 | row.ss = ss; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 389 | if (ss.level == row.requestedLevel) { |
| 390 | row.requestedLevel = -1; |
| 391 | } |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 392 | // TODO: update Seekbar progress and change the mute icon if necessary. |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 395 | private VolumeRow findRow(int stream) { |
| 396 | for (VolumeRow row : mRows) { |
| 397 | if (row.stream == stream) { |
| 398 | return row; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 401 | return null; |
| 402 | } |
| 403 | |
| 404 | private VolumeRow findRow(SeekbarListItem targetItem) { |
| 405 | for (VolumeRow row : mRows) { |
| 406 | if (row.listItem == targetItem) { |
| 407 | return row; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 410 | return null; |
| 411 | } |
| 412 | |
| 413 | public void dump(PrintWriter writer) { |
| 414 | writer.println(VolumeDialogImpl.class.getSimpleName() + " state:"); |
| 415 | writer.print(" mShowing: "); writer.println(mShowing); |
| 416 | writer.print(" mDynamic: "); writer.println(mDynamic); |
| 417 | writer.print(" mAutomute: "); writer.println(mAutomute); |
| 418 | writer.print(" mSilentMode: "); writer.println(mSilentMode); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | private void recheckH(VolumeRow row) { |
| 422 | if (row == null) { |
| 423 | if (D.BUG) Log.d(TAG, "recheckH ALL"); |
| 424 | trimObsoleteH(); |
| 425 | for (VolumeRow r : mRows) { |
| 426 | updateVolumeRowH(r); |
| 427 | } |
| 428 | } else { |
| 429 | if (D.BUG) Log.d(TAG, "recheckH " + row.stream); |
| 430 | updateVolumeRowH(row); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | private void setStreamImportantH(int stream, boolean important) { |
| 435 | for (VolumeRow row : mRows) { |
| 436 | if (row.stream == stream) { |
| 437 | row.important = important; |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | private void showSafetyWarningH(int flags) { |
| 444 | if ((flags & (AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_SHOW_UI_WARNINGS)) != 0 |
| 445 | || mShowing) { |
| 446 | synchronized (mSafetyWarningLock) { |
| 447 | if (mSafetyWarning != null) { |
| 448 | return; |
| 449 | } |
| 450 | mSafetyWarning = new SafetyWarningDialog(mContext, mController.getAudioManager()) { |
| 451 | @Override |
| 452 | protected void cleanUp() { |
| 453 | synchronized (mSafetyWarningLock) { |
| 454 | mSafetyWarning = null; |
| 455 | } |
| 456 | recheckH(null); |
| 457 | } |
| 458 | }; |
| 459 | mSafetyWarning.show(); |
| 460 | } |
| 461 | recheckH(null); |
| 462 | } |
| 463 | rescheduleTimeoutH(); |
| 464 | } |
| 465 | |
| 466 | private final VolumeDialogController.Callbacks mControllerCallbackH |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 467 | = new VolumeDialogController.Callbacks() { |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 468 | @Override |
| 469 | public void onShowRequested(int reason) { |
| 470 | showH(reason); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void onDismissRequested(int reason) { |
| 475 | dismissH(reason); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public void onScreenOff() { |
| 480 | dismissH(Events.DISMISS_REASON_SCREEN_OFF); |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void onStateChanged(State state) { |
| 485 | onStateChangedH(state); |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void onLayoutDirectionChanged(int layoutDirection) { |
| 490 | mDialogView.setLayoutDirection(layoutDirection); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onConfigurationChanged() { |
| 495 | mDialog.dismiss(); |
| 496 | initDialog(); |
| 497 | mConfigurableTexts.update(); |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void onShowVibrateHint() { |
| 502 | if (mSilentMode) { |
| 503 | mController.setRingerMode(AudioManager.RINGER_MODE_SILENT, false); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public void onShowSilentHint() { |
| 509 | if (mSilentMode) { |
| 510 | mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void onShowSafetyWarning(int flags) { |
| 516 | showSafetyWarningH(flags); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void onAccessibilityModeChanged(Boolean showA11yStream) { |
| 521 | } |
| 522 | }; |
| 523 | |
| 524 | private final class H extends Handler { |
| 525 | private static final int SHOW = 1; |
| 526 | private static final int DISMISS = 2; |
| 527 | private static final int RECHECK = 3; |
| 528 | private static final int RECHECK_ALL = 4; |
| 529 | private static final int SET_STREAM_IMPORTANT = 5; |
| 530 | private static final int RESCHEDULE_TIMEOUT = 6; |
| 531 | private static final int STATE_CHANGED = 7; |
| 532 | |
| 533 | public H() { |
| 534 | super(Looper.getMainLooper()); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void handleMessage(Message msg) { |
| 539 | switch (msg.what) { |
| 540 | case SHOW: showH(msg.arg1); break; |
| 541 | case DISMISS: dismissH(msg.arg1); break; |
| 542 | case RECHECK: recheckH((VolumeRow) msg.obj); break; |
| 543 | case RECHECK_ALL: recheckH(null); break; |
| 544 | case SET_STREAM_IMPORTANT: setStreamImportantH(msg.arg1, msg.arg2 != 0); break; |
| 545 | case RESCHEDULE_TIMEOUT: rescheduleTimeoutH(); break; |
| 546 | case STATE_CHANGED: onStateChangedH(mState); break; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | private final class CustomDialog extends Dialog implements DialogInterface { |
| 552 | public CustomDialog(Context context) { |
| 553 | super(context, com.android.systemui.R.style.qs_theme); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 558 | rescheduleTimeoutH(); |
| 559 | return super.dispatchTouchEvent(ev); |
| 560 | } |
| 561 | |
| 562 | @Override |
| 563 | protected void onStart() { |
| 564 | super.setCanceledOnTouchOutside(true); |
| 565 | super.onStart(); |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | protected void onStop() { |
| 570 | super.onStop(); |
| 571 | mHandler.sendEmptyMessage(H.RECHECK_ALL); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public boolean onTouchEvent(MotionEvent event) { |
| 576 | if (isShowing()) { |
| 577 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
| 578 | dismissH(Events.DISMISS_REASON_TOUCH_OUTSIDE); |
| 579 | return true; |
| 580 | } |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener { |
| 587 | private final VolumeRow mRow; |
| 588 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 589 | private VolumeSeekBarChangeListener(VolumeRow volumeRow) { |
| 590 | mRow = volumeRow; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 595 | if (mRow.ss == null) { |
| 596 | return; |
| 597 | } |
| 598 | if (D.BUG) { |
| 599 | Log.d(TAG, AudioSystem.streamToString(mRow.stream) |
| 600 | + " onProgressChanged " + progress + " fromUser=" + fromUser); |
| 601 | } |
| 602 | if (!fromUser) { |
| 603 | return; |
| 604 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 605 | if (mRow.ss.levelMin > 0) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 606 | final int minProgress = mRow.ss.levelMin; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 607 | if (progress < minProgress) { |
| 608 | seekBar.setProgress(minProgress); |
| 609 | progress = minProgress; |
| 610 | } |
| 611 | } |
| 612 | final int userLevel = getImpliedLevel(seekBar, progress); |
| 613 | if (mRow.ss.level != userLevel || mRow.ss.muted && userLevel > 0) { |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 614 | if (mRow.requestedLevel != userLevel) { |
| 615 | mController.setStreamVolume(mRow.stream, userLevel); |
| 616 | mRow.requestedLevel = userLevel; |
| 617 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_CHANGED, mRow.stream, |
| 618 | userLevel); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | @Override |
| 624 | public void onStartTrackingTouch(SeekBar seekBar) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 625 | if (D.BUG) { |
| 626 | Log.d(TAG, "onStartTrackingTouch"+ " " + mRow.stream); |
| 627 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 628 | mController.setActiveStream(mRow.stream); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public void onStopTrackingTouch(SeekBar seekBar) { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 633 | if (D.BUG) { |
| 634 | Log.d(TAG, "onStopTrackingTouch"+ " " + mRow.stream); |
| 635 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 636 | final int userLevel = getImpliedLevel(seekBar, seekBar.getProgress()); |
| 637 | Events.writeEvent(mContext, Events.EVENT_TOUCH_LEVEL_DONE, mRow.stream, userLevel); |
| 638 | if (mRow.ss.level != userLevel) { |
| 639 | mHandler.sendMessageDelayed(mHandler.obtainMessage(H.RECHECK, mRow), |
| 640 | USER_ATTEMPT_GRACE_PERIOD); |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | private static class VolumeRow { |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 646 | private int stream; |
| 647 | private StreamState ss; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 648 | private boolean important; |
| 649 | private boolean defaultStream; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 650 | private int primaryActionIcon; |
| 651 | private int supplementalIcon; |
| 652 | private SeekbarListItem listItem; |
| 653 | private int requestedLevel = -1; // pending user-requested level via progress changed |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 654 | } |
| 655 | } |