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 | |
| 19 | import android.app.NotificationManager; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.PackageManager.NameNotFoundException; |
| 28 | import android.database.ContentObserver; |
Julia Reynolds | 6692b47 | 2018-01-24 07:23:56 -0500 | [diff] [blame] | 29 | import android.media.AudioDeviceCallback; |
| 30 | import android.media.AudioDeviceInfo; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 31 | import android.media.AudioManager; |
| 32 | import android.media.AudioSystem; |
| 33 | import android.media.IVolumeController; |
| 34 | import android.media.VolumePolicy; |
| 35 | import android.media.session.MediaController.PlaybackInfo; |
| 36 | import android.media.session.MediaSession.Token; |
| 37 | import android.net.Uri; |
| 38 | import android.os.Handler; |
| 39 | import android.os.HandlerThread; |
| 40 | import android.os.Looper; |
| 41 | import android.os.Message; |
| 42 | import android.os.RemoteException; |
| 43 | import android.os.Vibrator; |
| 44 | import android.provider.Settings; |
| 45 | import android.service.notification.Condition; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 46 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 47 | import android.util.ArrayMap; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 48 | import android.util.Log; |
Jason Monk | ae30597 | 2017-06-27 14:46:03 -0400 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityManager; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 50 | |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 51 | import com.android.internal.annotations.GuardedBy; |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 52 | import com.android.systemui.Dumpable; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 53 | import com.android.systemui.R; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 54 | import com.android.systemui.SysUiServiceProvider; |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 55 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 56 | import com.android.systemui.plugins.VolumeDialogController; |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 57 | import com.android.systemui.qs.tiles.DndTile; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 58 | import com.android.systemui.statusbar.phone.StatusBar; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 59 | |
| 60 | import java.io.FileDescriptor; |
| 61 | import java.io.PrintWriter; |
Julia Reynolds | 24cc759 | 2018-01-25 15:51:45 -0500 | [diff] [blame] | 62 | import java.util.ArrayList; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 63 | import java.util.HashMap; |
Julia Reynolds | 24cc759 | 2018-01-25 15:51:45 -0500 | [diff] [blame] | 64 | import java.util.List; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 65 | import java.util.Map; |
| 66 | import java.util.Objects; |
| 67 | |
| 68 | /** |
| 69 | * Source of truth for all state / events related to the volume dialog. No presentation. |
| 70 | * |
| 71 | * All work done on a dedicated background worker thread & associated worker. |
| 72 | * |
| 73 | * Methods ending in "W" must be called on the worker thread. |
| 74 | */ |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 75 | public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpable { |
| 76 | private static final String TAG = Util.logTag(VolumeDialogControllerImpl.class); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 77 | |
| 78 | private static final int DYNAMIC_STREAM_START_INDEX = 100; |
| 79 | private static final int VIBRATE_HINT_DURATION = 50; |
| 80 | |
Julia Reynolds | 561d3f4 | 2018-01-26 11:31:02 -0500 | [diff] [blame] | 81 | static final ArrayMap<Integer, Integer> STREAMS = new ArrayMap<>(); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 82 | static { |
| 83 | STREAMS.put(AudioSystem.STREAM_ALARM, R.string.stream_alarm); |
| 84 | STREAMS.put(AudioSystem.STREAM_BLUETOOTH_SCO, R.string.stream_bluetooth_sco); |
| 85 | STREAMS.put(AudioSystem.STREAM_DTMF, R.string.stream_dtmf); |
| 86 | STREAMS.put(AudioSystem.STREAM_MUSIC, R.string.stream_music); |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 87 | STREAMS.put(AudioSystem.STREAM_ACCESSIBILITY, R.string.stream_accessibility); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 88 | STREAMS.put(AudioSystem.STREAM_NOTIFICATION, R.string.stream_notification); |
| 89 | STREAMS.put(AudioSystem.STREAM_RING, R.string.stream_ring); |
| 90 | STREAMS.put(AudioSystem.STREAM_SYSTEM, R.string.stream_system); |
| 91 | STREAMS.put(AudioSystem.STREAM_SYSTEM_ENFORCED, R.string.stream_system_enforced); |
| 92 | STREAMS.put(AudioSystem.STREAM_TTS, R.string.stream_tts); |
| 93 | STREAMS.put(AudioSystem.STREAM_VOICE_CALL, R.string.stream_voice_call); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 94 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 95 | |
| 96 | private final HandlerThread mWorkerThread; |
| 97 | private final W mWorker; |
| 98 | private final Context mContext; |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 99 | private AudioManager mAudio; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 100 | protected StatusBar mStatusBar; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 101 | private final NotificationManager mNoMan; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 102 | private final SettingObserver mObserver; |
| 103 | private final Receiver mReceiver = new Receiver(); |
| 104 | private final MediaSessions mMediaSessions; |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 105 | protected C mCallbacks = new C(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 106 | private final State mState = new State(); |
Beverly | 4e93661 | 2017-07-28 14:05:30 -0400 | [diff] [blame] | 107 | protected final MediaSessionsCallbacks mMediaSessionsCallbacksW = new MediaSessionsCallbacks(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 108 | private final Vibrator mVibrator; |
| 109 | private final boolean mHasVibrator; |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 110 | private boolean mShowA11yStream; |
Luke Song | e003666 | 2017-12-12 12:27:08 -0800 | [diff] [blame] | 111 | private boolean mShowVolumeDialog; |
| 112 | private boolean mShowSafetyWarning; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 113 | private final NotificationManager mNotificationManager; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 114 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 115 | private boolean mDestroyed; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 116 | private VolumePolicy mVolumePolicy; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 117 | private boolean mShowDndTile = true; |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 118 | @GuardedBy("this") |
| 119 | private UserActivityListener mUserActivityListener; |
| 120 | |
| 121 | protected final VC mVolumeController = new VC(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 122 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 123 | public VolumeDialogControllerImpl(Context context) { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 124 | mContext = context.getApplicationContext(); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 125 | mNotificationManager = (NotificationManager) mContext.getSystemService( |
| 126 | Context.NOTIFICATION_SERVICE); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 127 | Events.writeEvent(mContext, Events.EVENT_COLLECTION_STARTED); |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 128 | mWorkerThread = new HandlerThread(VolumeDialogControllerImpl.class.getSimpleName()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 129 | mWorkerThread.start(); |
| 130 | mWorker = new W(mWorkerThread.getLooper()); |
| 131 | mMediaSessions = createMediaSessions(mContext, mWorkerThread.getLooper(), |
| 132 | mMediaSessionsCallbacksW); |
| 133 | mAudio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 134 | mNoMan = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 135 | mObserver = new SettingObserver(mWorker); |
| 136 | mObserver.init(); |
| 137 | mReceiver.init(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 138 | mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 139 | mHasVibrator = mVibrator != null && mVibrator.hasVibrator(); |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 140 | updateStatusBar(); |
Jason Monk | ae30597 | 2017-06-27 14:46:03 -0400 | [diff] [blame] | 141 | |
| 142 | boolean accessibilityVolumeStreamActive = context.getSystemService( |
| 143 | AccessibilityManager.class).isAccessibilityVolumeStreamActive(); |
| 144 | mVolumeController.setA11yMode(accessibilityVolumeStreamActive ? |
| 145 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 146 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 147 | } |
| 148 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 149 | public AudioManager getAudioManager() { |
| 150 | return mAudio; |
| 151 | } |
| 152 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 153 | public void dismiss() { |
| 154 | mCallbacks.onDismissRequested(Events.DISMISS_REASON_VOLUME_CONTROLLER); |
| 155 | } |
| 156 | |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 157 | protected void setVolumeController() { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 158 | try { |
| 159 | mAudio.setVolumeController(mVolumeController); |
| 160 | } catch (SecurityException e) { |
| 161 | Log.w(TAG, "Unable to set the volume controller", e); |
| 162 | return; |
| 163 | } |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | protected void setAudioManagerStreamVolume(int stream, int level, int flag) { |
| 167 | mAudio.setStreamVolume(stream, level, flag); |
| 168 | } |
| 169 | |
| 170 | protected int getAudioManagerStreamVolume(int stream) { |
| 171 | return mAudio.getLastAudibleStreamVolume(stream); |
| 172 | } |
| 173 | |
| 174 | protected int getAudioManagerStreamMaxVolume(int stream) { |
| 175 | return mAudio.getStreamMaxVolume(stream); |
| 176 | } |
| 177 | |
| 178 | protected int getAudioManagerStreamMinVolume(int stream) { |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 179 | return mAudio.getStreamMinVolumeInt(stream); |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | public void register() { |
| 183 | setVolumeController(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 184 | setVolumePolicy(mVolumePolicy); |
| 185 | showDndTile(mShowDndTile); |
| 186 | try { |
| 187 | mMediaSessions.init(); |
| 188 | } catch (SecurityException e) { |
| 189 | Log.w(TAG, "No access to media sessions", e); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public void setVolumePolicy(VolumePolicy policy) { |
| 194 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 195 | if (mVolumePolicy == null) return; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 196 | try { |
| 197 | mAudio.setVolumePolicy(mVolumePolicy); |
| 198 | } catch (NoSuchMethodError e) { |
| 199 | Log.w(TAG, "No volume policy api"); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | protected MediaSessions createMediaSessions(Context context, Looper looper, |
| 204 | MediaSessions.Callbacks callbacks) { |
| 205 | return new MediaSessions(context, looper, callbacks); |
| 206 | } |
| 207 | |
| 208 | public void destroy() { |
| 209 | if (D.BUG) Log.d(TAG, "destroy"); |
| 210 | if (mDestroyed) return; |
| 211 | mDestroyed = true; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 212 | Events.writeEvent(mContext, Events.EVENT_COLLECTION_STOPPED); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 213 | mMediaSessions.destroy(); |
| 214 | mObserver.destroy(); |
| 215 | mReceiver.destroy(); |
| 216 | mWorkerThread.quitSafely(); |
| 217 | } |
| 218 | |
| 219 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 220 | pw.println(VolumeDialogControllerImpl.class.getSimpleName() + " state:"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 221 | pw.print(" mDestroyed: "); pw.println(mDestroyed); |
| 222 | pw.print(" mVolumePolicy: "); pw.println(mVolumePolicy); |
John Spurlock | e56efa71 | 2015-05-19 12:26:25 -0400 | [diff] [blame] | 223 | pw.print(" mState: "); pw.println(mState.toString(4)); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 224 | pw.print(" mShowDndTile: "); pw.println(mShowDndTile); |
| 225 | pw.print(" mHasVibrator: "); pw.println(mHasVibrator); |
| 226 | pw.print(" mRemoteStreams: "); pw.println(mMediaSessionsCallbacksW.mRemoteStreams |
| 227 | .values()); |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 228 | pw.print(" mShowA11yStream: "); pw.println(mShowA11yStream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 229 | pw.println(); |
| 230 | mMediaSessions.dump(pw); |
| 231 | } |
| 232 | |
| 233 | public void addCallback(Callbacks callback, Handler handler) { |
| 234 | mCallbacks.add(callback, handler); |
Jason Monk | ae30597 | 2017-06-27 14:46:03 -0400 | [diff] [blame] | 235 | callback.onAccessibilityModeChanged(mShowA11yStream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 238 | public void setUserActivityListener(UserActivityListener listener) { |
| 239 | if (mDestroyed) return; |
| 240 | synchronized (this) { |
| 241 | mUserActivityListener = listener; |
| 242 | } |
| 243 | } |
| 244 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 245 | public void removeCallback(Callbacks callback) { |
| 246 | mCallbacks.remove(callback); |
| 247 | } |
| 248 | |
| 249 | public void getState() { |
| 250 | if (mDestroyed) return; |
| 251 | mWorker.sendEmptyMessage(W.GET_STATE); |
| 252 | } |
| 253 | |
| 254 | public void notifyVisible(boolean visible) { |
| 255 | if (mDestroyed) return; |
| 256 | mWorker.obtainMessage(W.NOTIFY_VISIBLE, visible ? 1 : 0, 0).sendToTarget(); |
| 257 | } |
| 258 | |
| 259 | public void userActivity() { |
| 260 | if (mDestroyed) return; |
| 261 | mWorker.removeMessages(W.USER_ACTIVITY); |
| 262 | mWorker.sendEmptyMessage(W.USER_ACTIVITY); |
| 263 | } |
| 264 | |
| 265 | public void setRingerMode(int value, boolean external) { |
| 266 | if (mDestroyed) return; |
| 267 | mWorker.obtainMessage(W.SET_RINGER_MODE, value, external ? 1 : 0).sendToTarget(); |
| 268 | } |
| 269 | |
| 270 | public void setZenMode(int value) { |
| 271 | if (mDestroyed) return; |
| 272 | mWorker.obtainMessage(W.SET_ZEN_MODE, value, 0).sendToTarget(); |
| 273 | } |
| 274 | |
| 275 | public void setExitCondition(Condition condition) { |
| 276 | if (mDestroyed) return; |
| 277 | mWorker.obtainMessage(W.SET_EXIT_CONDITION, condition).sendToTarget(); |
| 278 | } |
| 279 | |
| 280 | public void setStreamMute(int stream, boolean mute) { |
| 281 | if (mDestroyed) return; |
| 282 | mWorker.obtainMessage(W.SET_STREAM_MUTE, stream, mute ? 1 : 0).sendToTarget(); |
| 283 | } |
| 284 | |
| 285 | public void setStreamVolume(int stream, int level) { |
| 286 | if (mDestroyed) return; |
| 287 | mWorker.obtainMessage(W.SET_STREAM_VOLUME, stream, level).sendToTarget(); |
| 288 | } |
| 289 | |
| 290 | public void setActiveStream(int stream) { |
| 291 | if (mDestroyed) return; |
| 292 | mWorker.obtainMessage(W.SET_ACTIVE_STREAM, stream, 0).sendToTarget(); |
| 293 | } |
| 294 | |
Luke Song | e003666 | 2017-12-12 12:27:08 -0800 | [diff] [blame] | 295 | public void setEnableDialogs(boolean volumeUi, boolean safetyWarning) { |
| 296 | mShowVolumeDialog = volumeUi; |
| 297 | mShowSafetyWarning = safetyWarning; |
| 298 | } |
| 299 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 300 | public void vibrate() { |
| 301 | if (mHasVibrator) { |
| 302 | mVibrator.vibrate(VIBRATE_HINT_DURATION); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | public boolean hasVibrator() { |
| 307 | return mHasVibrator; |
| 308 | } |
| 309 | |
| 310 | private void onNotifyVisibleW(boolean visible) { |
| 311 | if (mDestroyed) return; |
| 312 | mAudio.notifyVolumeControllerVisible(mVolumeController, visible); |
| 313 | if (!visible) { |
| 314 | if (updateActiveStreamW(-1)) { |
| 315 | mCallbacks.onStateChanged(mState); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 320 | private void onUserActivityW() { |
| 321 | synchronized (this) { |
| 322 | if (mUserActivityListener != null) { |
| 323 | mUserActivityListener.onUserActivity(); |
| 324 | } |
| 325 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 326 | } |
| 327 | |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 328 | private void onShowSafetyWarningW(int flags) { |
Luke Song | e003666 | 2017-12-12 12:27:08 -0800 | [diff] [blame] | 329 | if (mShowSafetyWarning) { |
| 330 | mCallbacks.onShowSafetyWarning(flags); |
| 331 | } |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 334 | private void onAccessibilityModeChanged(Boolean showA11yStream) { |
| 335 | mCallbacks.onAccessibilityModeChanged(showA11yStream); |
| 336 | } |
| 337 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 338 | private boolean checkRoutedToBluetoothW(int stream) { |
| 339 | boolean changed = false; |
| 340 | if (stream == AudioManager.STREAM_MUSIC) { |
| 341 | final boolean routedToBluetooth = |
| 342 | (mAudio.getDevicesForStream(AudioManager.STREAM_MUSIC) & |
| 343 | (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP | |
| 344 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 345 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0; |
| 346 | changed |= updateStreamRoutedToBluetoothW(stream, routedToBluetooth); |
| 347 | } |
| 348 | return changed; |
| 349 | } |
| 350 | |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 351 | private void updateStatusBar() { |
| 352 | if (mStatusBar == null) { |
| 353 | mStatusBar = SysUiServiceProvider.getComponent(mContext, StatusBar.class); |
| 354 | } |
| 355 | } |
| 356 | |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 357 | private boolean shouldShowUI(int flags) { |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 358 | updateStatusBar(); |
Beverly | a2f682f | 2018-02-01 11:29:25 -0500 | [diff] [blame] | 359 | // if status bar isn't null, check if phone is in AOD, else check flags |
| 360 | // since we could be using a different status bar |
| 361 | return mStatusBar != null ? |
| 362 | mStatusBar.getWakefulnessState() != WakefulnessLifecycle.WAKEFULNESS_ASLEEP |
| 363 | && mStatusBar.getWakefulnessState() != |
| 364 | WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 365 | && mStatusBar.isDeviceInteractive() |
Beverly | a2f682f | 2018-02-01 11:29:25 -0500 | [diff] [blame] | 366 | && (flags & AudioManager.FLAG_SHOW_UI) != 0 && mShowVolumeDialog |
| 367 | : mShowVolumeDialog && (flags & AudioManager.FLAG_SHOW_UI) != 0; |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 368 | } |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 369 | |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 370 | boolean onVolumeChangedW(int stream, int flags) { |
| 371 | final boolean showUI = shouldShowUI(flags); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 372 | final boolean fromKey = (flags & AudioManager.FLAG_FROM_KEY) != 0; |
| 373 | final boolean showVibrateHint = (flags & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0; |
| 374 | final boolean showSilentHint = (flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0; |
| 375 | boolean changed = false; |
| 376 | if (showUI) { |
| 377 | changed |= updateActiveStreamW(stream); |
| 378 | } |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 379 | int lastAudibleStreamVolume = getAudioManagerStreamVolume(stream); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 380 | changed |= updateStreamLevelW(stream, lastAudibleStreamVolume); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 381 | changed |= checkRoutedToBluetoothW(showUI ? AudioManager.STREAM_MUSIC : stream); |
| 382 | if (changed) { |
| 383 | mCallbacks.onStateChanged(mState); |
| 384 | } |
| 385 | if (showUI) { |
| 386 | mCallbacks.onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); |
| 387 | } |
| 388 | if (showVibrateHint) { |
| 389 | mCallbacks.onShowVibrateHint(); |
| 390 | } |
| 391 | if (showSilentHint) { |
| 392 | mCallbacks.onShowSilentHint(); |
| 393 | } |
| 394 | if (changed && fromKey) { |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 395 | Events.writeEvent(mContext, Events.EVENT_KEY, stream, lastAudibleStreamVolume); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 396 | } |
Julia Reynolds | 8ae994f | 2015-09-14 11:12:42 -0400 | [diff] [blame] | 397 | return changed; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | private boolean updateActiveStreamW(int activeStream) { |
| 401 | if (activeStream == mState.activeStream) return false; |
| 402 | mState.activeStream = activeStream; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 403 | Events.writeEvent(mContext, Events.EVENT_ACTIVE_STREAM_CHANGED, activeStream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 404 | if (D.BUG) Log.d(TAG, "updateActiveStreamW " + activeStream); |
| 405 | final int s = activeStream < DYNAMIC_STREAM_START_INDEX ? activeStream : -1; |
| 406 | if (D.BUG) Log.d(TAG, "forceVolumeControlStream " + s); |
| 407 | mAudio.forceVolumeControlStream(s); |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | private StreamState streamStateW(int stream) { |
| 412 | StreamState ss = mState.states.get(stream); |
| 413 | if (ss == null) { |
| 414 | ss = new StreamState(); |
| 415 | mState.states.put(stream, ss); |
| 416 | } |
| 417 | return ss; |
| 418 | } |
| 419 | |
| 420 | private void onGetStateW() { |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 421 | for (int stream : STREAMS.keySet()) { |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 422 | updateStreamLevelW(stream, getAudioManagerStreamVolume(stream)); |
| 423 | streamStateW(stream).levelMin = getAudioManagerStreamMinVolume(stream); |
| 424 | streamStateW(stream).levelMax = getAudioManagerStreamMaxVolume(stream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 425 | updateStreamMuteW(stream, mAudio.isStreamMute(stream)); |
| 426 | final StreamState ss = streamStateW(stream); |
| 427 | ss.muteSupported = mAudio.isStreamAffectedByMute(stream); |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 428 | ss.name = STREAMS.get(stream); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 429 | checkRoutedToBluetoothW(stream); |
| 430 | } |
| 431 | updateRingerModeExternalW(mAudio.getRingerMode()); |
| 432 | updateZenModeW(); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 433 | updateZenConfig(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 434 | updateEffectsSuppressorW(mNoMan.getEffectsSuppressor()); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 435 | mCallbacks.onStateChanged(mState); |
| 436 | } |
| 437 | |
| 438 | private boolean updateStreamRoutedToBluetoothW(int stream, boolean routedToBluetooth) { |
| 439 | final StreamState ss = streamStateW(stream); |
| 440 | if (ss.routedToBluetooth == routedToBluetooth) return false; |
| 441 | ss.routedToBluetooth = routedToBluetooth; |
| 442 | if (D.BUG) Log.d(TAG, "updateStreamRoutedToBluetoothW stream=" + stream |
| 443 | + " routedToBluetooth=" + routedToBluetooth); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | private boolean updateStreamLevelW(int stream, int level) { |
| 448 | final StreamState ss = streamStateW(stream); |
| 449 | if (ss.level == level) return false; |
| 450 | ss.level = level; |
| 451 | if (isLogWorthy(stream)) { |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 452 | Events.writeEvent(mContext, Events.EVENT_LEVEL_CHANGED, stream, level); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | private static boolean isLogWorthy(int stream) { |
| 458 | switch (stream) { |
| 459 | case AudioSystem.STREAM_ALARM: |
| 460 | case AudioSystem.STREAM_BLUETOOTH_SCO: |
| 461 | case AudioSystem.STREAM_MUSIC: |
| 462 | case AudioSystem.STREAM_RING: |
| 463 | case AudioSystem.STREAM_SYSTEM: |
| 464 | case AudioSystem.STREAM_VOICE_CALL: |
| 465 | return true; |
| 466 | } |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | private boolean updateStreamMuteW(int stream, boolean muted) { |
| 471 | final StreamState ss = streamStateW(stream); |
| 472 | if (ss.muted == muted) return false; |
| 473 | ss.muted = muted; |
| 474 | if (isLogWorthy(stream)) { |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 475 | Events.writeEvent(mContext, Events.EVENT_MUTE_CHANGED, stream, muted); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 476 | } |
| 477 | if (muted && isRinger(stream)) { |
| 478 | updateRingerModeInternalW(mAudio.getRingerModeInternal()); |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | private static boolean isRinger(int stream) { |
| 484 | return stream == AudioManager.STREAM_RING || stream == AudioManager.STREAM_NOTIFICATION; |
| 485 | } |
| 486 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 487 | private boolean updateEffectsSuppressorW(ComponentName effectsSuppressor) { |
| 488 | if (Objects.equals(mState.effectsSuppressor, effectsSuppressor)) return false; |
| 489 | mState.effectsSuppressor = effectsSuppressor; |
| 490 | mState.effectsSuppressorName = getApplicationName(mContext, mState.effectsSuppressor); |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 491 | Events.writeEvent(mContext, Events.EVENT_SUPPRESSOR_CHANGED, mState.effectsSuppressor, |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 492 | mState.effectsSuppressorName); |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | private static String getApplicationName(Context context, ComponentName component) { |
| 497 | if (component == null) return null; |
| 498 | final PackageManager pm = context.getPackageManager(); |
| 499 | final String pkg = component.getPackageName(); |
| 500 | try { |
| 501 | final ApplicationInfo ai = pm.getApplicationInfo(pkg, 0); |
| 502 | final String rt = Objects.toString(ai.loadLabel(pm), "").trim(); |
| 503 | if (rt.length() > 0) { |
| 504 | return rt; |
| 505 | } |
| 506 | } catch (NameNotFoundException e) {} |
| 507 | return pkg; |
| 508 | } |
| 509 | |
| 510 | private boolean updateZenModeW() { |
| 511 | final int zen = Settings.Global.getInt(mContext.getContentResolver(), |
| 512 | Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF); |
| 513 | if (mState.zenMode == zen) return false; |
| 514 | mState.zenMode = zen; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 515 | Events.writeEvent(mContext, Events.EVENT_ZEN_MODE_CHANGED, zen); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 516 | return true; |
| 517 | } |
| 518 | |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 519 | private boolean updateZenConfig() { |
| 520 | final NotificationManager.Policy policy = mNotificationManager.getNotificationPolicy(); |
| 521 | boolean disallowAlarms = (policy.priorityCategories & NotificationManager.Policy |
| 522 | .PRIORITY_CATEGORY_ALARMS) == 0; |
| 523 | boolean disallowMedia = (policy.priorityCategories & NotificationManager.Policy |
| 524 | .PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER) == 0; |
| 525 | boolean disallowRinger = ZenModeConfig.areAllPriorityOnlyNotificationZenSoundsMuted(policy); |
| 526 | if (mState.disallowAlarms == disallowAlarms && mState.disallowMedia == disallowMedia |
| 527 | && mState.disallowRinger == disallowRinger) { |
| 528 | return false; |
| 529 | } |
| 530 | mState.disallowAlarms = disallowAlarms; |
| 531 | mState.disallowMedia = disallowMedia; |
| 532 | mState.disallowRinger = disallowRinger; |
| 533 | Events.writeEvent(mContext, Events.EVENT_ZEN_CONFIG_CHANGED, "disallowAlarms=" + |
| 534 | disallowAlarms + " disallowMedia=" + disallowMedia + " disallowRinger=" + |
| 535 | disallowRinger); |
| 536 | return true; |
| 537 | } |
| 538 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 539 | private boolean updateRingerModeExternalW(int rm) { |
| 540 | if (rm == mState.ringerModeExternal) return false; |
| 541 | mState.ringerModeExternal = rm; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 542 | Events.writeEvent(mContext, Events.EVENT_EXTERNAL_RINGER_MODE_CHANGED, rm); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
| 546 | private boolean updateRingerModeInternalW(int rm) { |
| 547 | if (rm == mState.ringerModeInternal) return false; |
| 548 | mState.ringerModeInternal = rm; |
Chris Wren | e565ee6 | 2015-06-17 15:24:56 -0400 | [diff] [blame] | 549 | Events.writeEvent(mContext, Events.EVENT_INTERNAL_RINGER_MODE_CHANGED, rm); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 550 | return true; |
| 551 | } |
| 552 | |
| 553 | private void onSetRingerModeW(int mode, boolean external) { |
| 554 | if (external) { |
| 555 | mAudio.setRingerMode(mode); |
| 556 | } else { |
| 557 | mAudio.setRingerModeInternal(mode); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | private void onSetStreamMuteW(int stream, boolean mute) { |
| 562 | mAudio.adjustStreamVolume(stream, mute ? AudioManager.ADJUST_MUTE |
| 563 | : AudioManager.ADJUST_UNMUTE, 0); |
| 564 | } |
| 565 | |
| 566 | private void onSetStreamVolumeW(int stream, int level) { |
| 567 | if (D.BUG) Log.d(TAG, "onSetStreamVolume " + stream + " level=" + level); |
| 568 | if (stream >= DYNAMIC_STREAM_START_INDEX) { |
| 569 | mMediaSessionsCallbacksW.setStreamVolume(stream, level); |
| 570 | return; |
| 571 | } |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 572 | setAudioManagerStreamVolume(stream, level, 0); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | private void onSetActiveStreamW(int stream) { |
| 576 | boolean changed = updateActiveStreamW(stream); |
| 577 | if (changed) { |
| 578 | mCallbacks.onStateChanged(mState); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | private void onSetExitConditionW(Condition condition) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 583 | mNoMan.setZenMode(mState.zenMode, condition != null ? condition.id : null, TAG); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | private void onSetZenModeW(int mode) { |
| 587 | if (D.BUG) Log.d(TAG, "onSetZenModeW " + mode); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 588 | mNoMan.setZenMode(mode, null, TAG); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | private void onDismissRequestedW(int reason) { |
| 592 | mCallbacks.onDismissRequested(reason); |
| 593 | } |
| 594 | |
| 595 | public void showDndTile(boolean visible) { |
| 596 | if (D.BUG) Log.d(TAG, "showDndTile"); |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 597 | DndTile.setVisible(mContext, visible); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | private final class VC extends IVolumeController.Stub { |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 601 | private final String TAG = VolumeDialogControllerImpl.TAG + ".VC"; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 602 | |
| 603 | @Override |
| 604 | public void displaySafeVolumeWarning(int flags) throws RemoteException { |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 605 | if (D.BUG) Log.d(TAG, "displaySafeVolumeWarning " |
| 606 | + Util.audioManagerFlagsToString(flags)); |
| 607 | if (mDestroyed) return; |
| 608 | mWorker.obtainMessage(W.SHOW_SAFETY_WARNING, flags, 0).sendToTarget(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public void volumeChanged(int streamType, int flags) throws RemoteException { |
| 613 | if (D.BUG) Log.d(TAG, "volumeChanged " + AudioSystem.streamToString(streamType) |
| 614 | + " " + Util.audioManagerFlagsToString(flags)); |
| 615 | if (mDestroyed) return; |
| 616 | mWorker.obtainMessage(W.VOLUME_CHANGED, streamType, flags).sendToTarget(); |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void masterMuteChanged(int flags) throws RemoteException { |
| 621 | if (D.BUG) Log.d(TAG, "masterMuteChanged"); |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void setLayoutDirection(int layoutDirection) throws RemoteException { |
| 626 | if (D.BUG) Log.d(TAG, "setLayoutDirection"); |
| 627 | if (mDestroyed) return; |
| 628 | mWorker.obtainMessage(W.LAYOUT_DIRECTION_CHANGED, layoutDirection, 0).sendToTarget(); |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public void dismiss() throws RemoteException { |
| 633 | if (D.BUG) Log.d(TAG, "dismiss requested"); |
| 634 | if (mDestroyed) return; |
| 635 | mWorker.obtainMessage(W.DISMISS_REQUESTED, Events.DISMISS_REASON_VOLUME_CONTROLLER, 0) |
| 636 | .sendToTarget(); |
| 637 | mWorker.sendEmptyMessage(W.DISMISS_REQUESTED); |
| 638 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 639 | |
| 640 | @Override |
| 641 | public void setA11yMode(int mode) { |
| 642 | if (D.BUG) Log.d(TAG, "setA11yMode to " + mode); |
| 643 | if (mDestroyed) return; |
| 644 | switch (mode) { |
| 645 | case VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME: |
| 646 | // "legacy" mode |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 647 | mShowA11yStream = false; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 648 | break; |
| 649 | case VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME: |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 650 | mShowA11yStream = true; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 651 | break; |
| 652 | default: |
| 653 | Log.e(TAG, "Invalid accessibility mode " + mode); |
| 654 | break; |
| 655 | } |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 656 | mWorker.obtainMessage(W.ACCESSIBILITY_MODE_CHANGED, mShowA11yStream).sendToTarget(); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 657 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | private final class W extends Handler { |
| 661 | private static final int VOLUME_CHANGED = 1; |
| 662 | private static final int DISMISS_REQUESTED = 2; |
| 663 | private static final int GET_STATE = 3; |
| 664 | private static final int SET_RINGER_MODE = 4; |
| 665 | private static final int SET_ZEN_MODE = 5; |
| 666 | private static final int SET_EXIT_CONDITION = 6; |
| 667 | private static final int SET_STREAM_MUTE = 7; |
| 668 | private static final int LAYOUT_DIRECTION_CHANGED = 8; |
| 669 | private static final int CONFIGURATION_CHANGED = 9; |
| 670 | private static final int SET_STREAM_VOLUME = 10; |
| 671 | private static final int SET_ACTIVE_STREAM = 11; |
| 672 | private static final int NOTIFY_VISIBLE = 12; |
| 673 | private static final int USER_ACTIVITY = 13; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 674 | private static final int SHOW_SAFETY_WARNING = 14; |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 675 | private static final int ACCESSIBILITY_MODE_CHANGED = 15; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 676 | |
| 677 | W(Looper looper) { |
| 678 | super(looper); |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public void handleMessage(Message msg) { |
| 683 | switch (msg.what) { |
| 684 | case VOLUME_CHANGED: onVolumeChangedW(msg.arg1, msg.arg2); break; |
| 685 | case DISMISS_REQUESTED: onDismissRequestedW(msg.arg1); break; |
| 686 | case GET_STATE: onGetStateW(); break; |
| 687 | case SET_RINGER_MODE: onSetRingerModeW(msg.arg1, msg.arg2 != 0); break; |
| 688 | case SET_ZEN_MODE: onSetZenModeW(msg.arg1); break; |
| 689 | case SET_EXIT_CONDITION: onSetExitConditionW((Condition) msg.obj); break; |
| 690 | case SET_STREAM_MUTE: onSetStreamMuteW(msg.arg1, msg.arg2 != 0); break; |
| 691 | case LAYOUT_DIRECTION_CHANGED: mCallbacks.onLayoutDirectionChanged(msg.arg1); break; |
| 692 | case CONFIGURATION_CHANGED: mCallbacks.onConfigurationChanged(); break; |
| 693 | case SET_STREAM_VOLUME: onSetStreamVolumeW(msg.arg1, msg.arg2); break; |
| 694 | case SET_ACTIVE_STREAM: onSetActiveStreamW(msg.arg1); break; |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 695 | case NOTIFY_VISIBLE: onNotifyVisibleW(msg.arg1 != 0); break; |
| 696 | case USER_ACTIVITY: onUserActivityW(); break; |
| 697 | case SHOW_SAFETY_WARNING: onShowSafetyWarningW(msg.arg1); break; |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 698 | case ACCESSIBILITY_MODE_CHANGED: onAccessibilityModeChanged((Boolean) msg.obj); |
Julia Reynolds | 6692b47 | 2018-01-24 07:23:56 -0500 | [diff] [blame] | 699 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
Beverly | 8ebef84 | 2017-07-12 10:58:22 -0400 | [diff] [blame] | 704 | class C implements Callbacks { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 705 | private final HashMap<Callbacks, Handler> mCallbackMap = new HashMap<>(); |
| 706 | |
| 707 | public void add(Callbacks callback, Handler handler) { |
| 708 | if (callback == null || handler == null) throw new IllegalArgumentException(); |
| 709 | mCallbackMap.put(callback, handler); |
| 710 | } |
| 711 | |
| 712 | public void remove(Callbacks callback) { |
| 713 | mCallbackMap.remove(callback); |
| 714 | } |
| 715 | |
| 716 | @Override |
| 717 | public void onShowRequested(final int reason) { |
| 718 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 719 | entry.getValue().post(new Runnable() { |
| 720 | @Override |
| 721 | public void run() { |
| 722 | entry.getKey().onShowRequested(reason); |
| 723 | } |
| 724 | }); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | @Override |
| 729 | public void onDismissRequested(final int reason) { |
| 730 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 731 | entry.getValue().post(new Runnable() { |
| 732 | @Override |
| 733 | public void run() { |
| 734 | entry.getKey().onDismissRequested(reason); |
| 735 | } |
| 736 | }); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | @Override |
| 741 | public void onStateChanged(final State state) { |
| 742 | final long time = System.currentTimeMillis(); |
| 743 | final State copy = state.copy(); |
| 744 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 745 | entry.getValue().post(new Runnable() { |
| 746 | @Override |
| 747 | public void run() { |
| 748 | entry.getKey().onStateChanged(copy); |
| 749 | } |
| 750 | }); |
| 751 | } |
| 752 | Events.writeState(time, copy); |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public void onLayoutDirectionChanged(final int layoutDirection) { |
| 757 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 758 | entry.getValue().post(new Runnable() { |
| 759 | @Override |
| 760 | public void run() { |
| 761 | entry.getKey().onLayoutDirectionChanged(layoutDirection); |
| 762 | } |
| 763 | }); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public void onConfigurationChanged() { |
| 769 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 770 | entry.getValue().post(new Runnable() { |
| 771 | @Override |
| 772 | public void run() { |
| 773 | entry.getKey().onConfigurationChanged(); |
| 774 | } |
| 775 | }); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void onShowVibrateHint() { |
| 781 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 782 | entry.getValue().post(new Runnable() { |
| 783 | @Override |
| 784 | public void run() { |
| 785 | entry.getKey().onShowVibrateHint(); |
| 786 | } |
| 787 | }); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public void onShowSilentHint() { |
| 793 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 794 | entry.getValue().post(new Runnable() { |
| 795 | @Override |
| 796 | public void run() { |
| 797 | entry.getKey().onShowSilentHint(); |
| 798 | } |
| 799 | }); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | @Override |
| 804 | public void onScreenOff() { |
| 805 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 806 | entry.getValue().post(new Runnable() { |
| 807 | @Override |
| 808 | public void run() { |
| 809 | entry.getKey().onScreenOff(); |
| 810 | } |
| 811 | }); |
| 812 | } |
| 813 | } |
John Spurlock | 76b52b3 | 2015-04-03 00:00:12 -0400 | [diff] [blame] | 814 | |
| 815 | @Override |
| 816 | public void onShowSafetyWarning(final int flags) { |
| 817 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 818 | entry.getValue().post(new Runnable() { |
| 819 | @Override |
| 820 | public void run() { |
| 821 | entry.getKey().onShowSafetyWarning(flags); |
| 822 | } |
| 823 | }); |
| 824 | } |
| 825 | } |
Julia Reynolds | dbfb40f | 2017-01-23 11:05:57 -0500 | [diff] [blame] | 826 | |
| 827 | @Override |
| 828 | public void onAccessibilityModeChanged(Boolean showA11yStream) { |
| 829 | boolean show = showA11yStream == null ? false : showA11yStream; |
| 830 | for (final Map.Entry<Callbacks, Handler> entry : mCallbackMap.entrySet()) { |
| 831 | entry.getValue().post(new Runnable() { |
| 832 | @Override |
| 833 | public void run() { |
| 834 | entry.getKey().onAccessibilityModeChanged(show); |
| 835 | } |
| 836 | }); |
| 837 | } |
| 838 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | |
| 842 | private final class SettingObserver extends ContentObserver { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 843 | private final Uri ZEN_MODE_URI = |
| 844 | Settings.Global.getUriFor(Settings.Global.ZEN_MODE); |
| 845 | private final Uri ZEN_MODE_CONFIG_URI = |
| 846 | Settings.Global.getUriFor(Settings.Global.ZEN_MODE_CONFIG_ETAG); |
| 847 | |
| 848 | public SettingObserver(Handler handler) { |
| 849 | super(handler); |
| 850 | } |
| 851 | |
| 852 | public void init() { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 853 | mContext.getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this); |
| 854 | mContext.getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_URI, false, this); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | public void destroy() { |
| 858 | mContext.getContentResolver().unregisterContentObserver(this); |
| 859 | } |
| 860 | |
| 861 | @Override |
| 862 | public void onChange(boolean selfChange, Uri uri) { |
| 863 | boolean changed = false; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 864 | if (ZEN_MODE_URI.equals(uri)) { |
| 865 | changed = updateZenModeW(); |
| 866 | } |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 867 | if (ZEN_MODE_CONFIG_URI.equals(uri)) { |
| 868 | changed |= updateZenConfig(); |
| 869 | } |
| 870 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 871 | if (changed) { |
| 872 | mCallbacks.onStateChanged(mState); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | private final class Receiver extends BroadcastReceiver { |
| 878 | |
| 879 | public void init() { |
| 880 | final IntentFilter filter = new IntentFilter(); |
| 881 | filter.addAction(AudioManager.VOLUME_CHANGED_ACTION); |
| 882 | filter.addAction(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 883 | filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 884 | filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); |
| 885 | filter.addAction(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 886 | filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
| 887 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 888 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
John Spurlock | bc7233a | 2015-06-29 15:34:18 -0400 | [diff] [blame] | 889 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 890 | mContext.registerReceiver(this, filter, null, mWorker); |
| 891 | } |
| 892 | |
| 893 | public void destroy() { |
| 894 | mContext.unregisterReceiver(this); |
| 895 | } |
| 896 | |
| 897 | @Override |
| 898 | public void onReceive(Context context, Intent intent) { |
| 899 | final String action = intent.getAction(); |
| 900 | boolean changed = false; |
| 901 | if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) { |
| 902 | final int stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 903 | final int level = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1); |
| 904 | final int oldLevel = intent |
| 905 | .getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, -1); |
| 906 | if (D.BUG) Log.d(TAG, "onReceive VOLUME_CHANGED_ACTION stream=" + stream |
| 907 | + " level=" + level + " oldLevel=" + oldLevel); |
| 908 | changed = updateStreamLevelW(stream, level); |
| 909 | } else if (action.equals(AudioManager.STREAM_DEVICES_CHANGED_ACTION)) { |
| 910 | final int stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 911 | final int devices = intent |
| 912 | .getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, -1); |
| 913 | final int oldDevices = intent |
| 914 | .getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, -1); |
| 915 | if (D.BUG) Log.d(TAG, "onReceive STREAM_DEVICES_CHANGED_ACTION stream=" |
| 916 | + stream + " devices=" + devices + " oldDevices=" + oldDevices); |
| 917 | changed = checkRoutedToBluetoothW(stream); |
Julia Reynolds | 8ae994f | 2015-09-14 11:12:42 -0400 | [diff] [blame] | 918 | changed |= onVolumeChangedW(stream, 0); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 919 | } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { |
| 920 | final int rm = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1); |
| 921 | if (D.BUG) Log.d(TAG, "onReceive RINGER_MODE_CHANGED_ACTION rm=" |
| 922 | + Util.ringerModeToString(rm)); |
| 923 | changed = updateRingerModeExternalW(rm); |
| 924 | } else if (action.equals(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION)) { |
| 925 | final int rm = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1); |
| 926 | if (D.BUG) Log.d(TAG, "onReceive INTERNAL_RINGER_MODE_CHANGED_ACTION rm=" |
| 927 | + Util.ringerModeToString(rm)); |
| 928 | changed = updateRingerModeInternalW(rm); |
| 929 | } else if (action.equals(AudioManager.STREAM_MUTE_CHANGED_ACTION)) { |
| 930 | final int stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 931 | final boolean muted = intent |
| 932 | .getBooleanExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, false); |
| 933 | if (D.BUG) Log.d(TAG, "onReceive STREAM_MUTE_CHANGED_ACTION stream=" + stream |
| 934 | + " muted=" + muted); |
| 935 | changed = updateStreamMuteW(stream, muted); |
| 936 | } else if (action.equals(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED)) { |
| 937 | if (D.BUG) Log.d(TAG, "onReceive ACTION_EFFECTS_SUPPRESSOR_CHANGED"); |
| 938 | changed = updateEffectsSuppressorW(mNoMan.getEffectsSuppressor()); |
| 939 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 940 | if (D.BUG) Log.d(TAG, "onReceive ACTION_CONFIGURATION_CHANGED"); |
| 941 | mCallbacks.onConfigurationChanged(); |
| 942 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 943 | if (D.BUG) Log.d(TAG, "onReceive ACTION_SCREEN_OFF"); |
| 944 | mCallbacks.onScreenOff(); |
John Spurlock | bc7233a | 2015-06-29 15:34:18 -0400 | [diff] [blame] | 945 | } else if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
| 946 | if (D.BUG) Log.d(TAG, "onReceive ACTION_CLOSE_SYSTEM_DIALOGS"); |
| 947 | dismiss(); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 948 | } |
| 949 | if (changed) { |
| 950 | mCallbacks.onStateChanged(mState); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Beverly | 4e93661 | 2017-07-28 14:05:30 -0400 | [diff] [blame] | 955 | protected final class MediaSessionsCallbacks implements MediaSessions.Callbacks { |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 956 | private final HashMap<Token, Integer> mRemoteStreams = new HashMap<>(); |
| 957 | |
| 958 | private int mNextStream = DYNAMIC_STREAM_START_INDEX; |
| 959 | |
| 960 | @Override |
| 961 | public void onRemoteUpdate(Token token, String name, PlaybackInfo pi) { |
Beverly | b5a2781 | 2017-07-24 11:34:48 -0400 | [diff] [blame] | 962 | addStream(token, "onRemoteUpdate"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 963 | final int stream = mRemoteStreams.get(token); |
| 964 | boolean changed = mState.states.indexOfKey(stream) < 0; |
| 965 | final StreamState ss = streamStateW(stream); |
| 966 | ss.dynamic = true; |
| 967 | ss.levelMin = 0; |
| 968 | ss.levelMax = pi.getMaxVolume(); |
| 969 | if (ss.level != pi.getCurrentVolume()) { |
| 970 | ss.level = pi.getCurrentVolume(); |
| 971 | changed = true; |
| 972 | } |
Julia Reynolds | 03c548f | 2016-12-14 15:02:38 -0500 | [diff] [blame] | 973 | if (!Objects.equals(ss.remoteLabel, name)) { |
| 974 | ss.name = -1; |
| 975 | ss.remoteLabel = name; |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 976 | changed = true; |
| 977 | } |
| 978 | if (changed) { |
| 979 | if (D.BUG) Log.d(TAG, "onRemoteUpdate: " + name + ": " + ss.level |
| 980 | + " of " + ss.levelMax); |
| 981 | mCallbacks.onStateChanged(mState); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | @Override |
| 986 | public void onRemoteVolumeChanged(Token token, int flags) { |
Beverly | b5a2781 | 2017-07-24 11:34:48 -0400 | [diff] [blame] | 987 | addStream(token, "onRemoteVolumeChanged"); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 988 | final int stream = mRemoteStreams.get(token); |
Beverly | 85e05eb | 2017-07-21 14:49:27 -0400 | [diff] [blame] | 989 | final boolean showUI = shouldShowUI(flags); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 990 | boolean changed = updateActiveStreamW(stream); |
| 991 | if (showUI) { |
| 992 | changed |= checkRoutedToBluetoothW(AudioManager.STREAM_MUSIC); |
| 993 | } |
| 994 | if (changed) { |
| 995 | mCallbacks.onStateChanged(mState); |
| 996 | } |
| 997 | if (showUI) { |
| 998 | mCallbacks.onShowRequested(Events.SHOW_REASON_REMOTE_VOLUME_CHANGED); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public void onRemoteRemoved(Token token) { |
Beverly | b5a2781 | 2017-07-24 11:34:48 -0400 | [diff] [blame] | 1004 | if (!mRemoteStreams.containsKey(token)) { |
| 1005 | if (D.BUG) Log.d(TAG, "onRemoteRemoved: stream doesn't exist, " |
| 1006 | + "aborting remote removed for token:" + token.toString()); |
| 1007 | return; |
| 1008 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1009 | final int stream = mRemoteStreams.get(token); |
| 1010 | mState.states.remove(stream); |
| 1011 | if (mState.activeStream == stream) { |
| 1012 | updateActiveStreamW(-1); |
| 1013 | } |
| 1014 | mCallbacks.onStateChanged(mState); |
| 1015 | } |
| 1016 | |
| 1017 | public void setStreamVolume(int stream, int level) { |
| 1018 | final Token t = findToken(stream); |
| 1019 | if (t == null) { |
| 1020 | Log.w(TAG, "setStreamVolume: No token found for stream: " + stream); |
| 1021 | return; |
| 1022 | } |
| 1023 | mMediaSessions.setVolume(t, level); |
| 1024 | } |
| 1025 | |
| 1026 | private Token findToken(int stream) { |
| 1027 | for (Map.Entry<Token, Integer> entry : mRemoteStreams.entrySet()) { |
| 1028 | if (entry.getValue().equals(stream)) { |
| 1029 | return entry.getKey(); |
| 1030 | } |
| 1031 | } |
| 1032 | return null; |
| 1033 | } |
Beverly | b5a2781 | 2017-07-24 11:34:48 -0400 | [diff] [blame] | 1034 | |
| 1035 | private void addStream(Token token, String triggeringMethod) { |
| 1036 | if (!mRemoteStreams.containsKey(token)) { |
| 1037 | mRemoteStreams.put(token, mNextStream); |
| 1038 | if (D.BUG) Log.d(TAG, triggeringMethod + ": added stream " + mNextStream |
| 1039 | + " from token + "+ token.toString()); |
| 1040 | mNextStream++; |
| 1041 | } |
| 1042 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
Yao Chen | 634acb9 | 2016-04-13 16:17:47 -0700 | [diff] [blame] | 1045 | public interface UserActivityListener { |
| 1046 | void onUserActivity(); |
| 1047 | } |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 1048 | } |