RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.media; |
| 18 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 20 | import android.annotation.NonNull; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 24 | import android.app.PendingIntent; |
| 25 | import android.app.PendingIntent.CanceledException; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 26 | import android.content.ActivityNotFoundException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 28 | import android.content.ComponentName; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 30 | import android.content.Context; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 33 | import android.content.pm.UserInfo; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 34 | import android.database.ContentObserver; |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 35 | import android.media.AudioManager; |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 36 | import android.media.AudioManagerInternal; |
RoboErik | 94c716e | 2014-09-14 13:54:31 -0700 | [diff] [blame] | 37 | import android.media.AudioSystem; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 38 | import android.media.IAudioService; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 39 | import android.media.IRemoteVolumeController; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 40 | import android.media.session.IActiveSessionsListener; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 41 | import android.media.session.ICallback; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 42 | import android.media.session.IOnMediaKeyListener; |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 43 | import android.media.session.IOnVolumeKeyLongPressListener; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 44 | import android.media.session.ISession; |
| 45 | import android.media.session.ISessionCallback; |
| 46 | import android.media.session.ISessionManager; |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 47 | import android.media.session.MediaSession; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 48 | import android.media.session.MediaSessionManager; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 49 | import android.net.Uri; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 50 | import android.os.Binder; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 51 | import android.os.Bundle; |
RoboErik | 8ae0f34 | 2014-02-24 18:02:08 -0800 | [diff] [blame] | 52 | import android.os.Handler; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 53 | import android.os.IBinder; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 54 | import android.os.Message; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 55 | import android.os.PowerManager; |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 56 | import android.os.Process; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 57 | import android.os.RemoteException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 58 | import android.os.ResultReceiver; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 59 | import android.os.ServiceManager; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 60 | import android.os.SystemProperties; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 61 | import android.os.UserHandle; |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 62 | import android.os.UserManager; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 63 | import android.provider.Settings; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 64 | import android.speech.RecognizerIntent; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 65 | import android.text.TextUtils; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 66 | import android.util.IntArray; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 67 | import android.util.Log; |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 68 | import android.util.Slog; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 69 | import android.util.SparseArray; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 70 | import android.util.SparseIntArray; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 71 | import android.view.KeyEvent; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 72 | import android.view.ViewConfiguration; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 73 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 74 | import com.android.internal.util.DumpUtils; |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 75 | import com.android.server.LocalServices; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 76 | import com.android.server.SystemService; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 77 | import com.android.server.Watchdog; |
| 78 | import com.android.server.Watchdog.Monitor; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 79 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 80 | import java.io.FileDescriptor; |
| 81 | import java.io.PrintWriter; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 83 | import java.util.Arrays; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 84 | import java.util.List; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * System implementation of MediaSessionManager |
| 88 | */ |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 89 | public class MediaSessionService extends SystemService implements Monitor { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 90 | private static final String TAG = "MediaSessionService"; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 91 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 92 | // Leave log for key event always. |
| 93 | private static final boolean DEBUG_KEY_EVENT = true; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 94 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 95 | private static final int WAKELOCK_TIMEOUT = 5000; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 96 | private static final int MEDIA_KEY_LISTENER_TIMEOUT = 1000; |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 97 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 98 | private final SessionManagerImpl mSessionManagerImpl; |
| 99 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 100 | // Keeps the full user id for each user. |
| 101 | private final SparseIntArray mFullUserIds = new SparseIntArray(); |
| 102 | private final SparseArray<FullUserRecord> mUserRecords = new SparseArray<FullUserRecord>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 103 | private final ArrayList<SessionsListenerRecord> mSessionsListeners |
| 104 | = new ArrayList<SessionsListenerRecord>(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 105 | private final Object mLock = new Object(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 106 | private final MessageHandler mHandler = new MessageHandler(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 107 | private final PowerManager.WakeLock mMediaEventWakeLock; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 108 | private final int mLongPressTimeout; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 109 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 110 | private KeyguardManager mKeyguardManager; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 111 | private IAudioService mAudioService; |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 112 | private AudioManagerInternal mAudioManagerInternal; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 113 | private ContentResolver mContentResolver; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 114 | private SettingsObserver mSettingsObserver; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 115 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 116 | // The FullUserRecord of the current users. (i.e. The foreground user that isn't a profile) |
| 117 | // It's always not null after the MediaSessionService is started. |
| 118 | private FullUserRecord mCurrentFullUserRecord; |
| 119 | private MediaSessionRecord mGlobalPrioritySession; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 120 | private AudioPlaybackMonitor mAudioPlaybackMonitor; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 121 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 122 | // Used to notify system UI when remote volume was changed. TODO find a |
| 123 | // better way to handle this. |
| 124 | private IRemoteVolumeController mRvc; |
| 125 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 126 | public MediaSessionService(Context context) { |
| 127 | super(context); |
| 128 | mSessionManagerImpl = new SessionManagerImpl(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 129 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 130 | mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent"); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 131 | mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onStart() { |
| 136 | publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 137 | Watchdog.getInstance().addMonitor(this); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 138 | mKeyguardManager = |
| 139 | (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 140 | mAudioService = getAudioService(); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 141 | mAudioPlaybackMonitor = new AudioPlaybackMonitor(getContext(), mAudioService, |
| 142 | new AudioPlaybackMonitor.OnAudioPlaybackStartedListener() { |
| 143 | @Override |
| 144 | public void onAudioPlaybackStarted(int uid) { |
| 145 | synchronized (mLock) { |
| 146 | FullUserRecord user = |
| 147 | getFullUserRecordLocked(UserHandle.getUserId(uid)); |
| 148 | if (user != null) { |
| 149 | user.mPriorityStack.updateMediaButtonSessionIfNeeded(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | }); |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 154 | mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class); |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 155 | mContentResolver = getContext().getContentResolver(); |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 156 | mSettingsObserver = new SettingsObserver(); |
| 157 | mSettingsObserver.observe(); |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 158 | |
| 159 | updateUser(); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private IAudioService getAudioService() { |
| 163 | IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); |
| 164 | return IAudioService.Stub.asInterface(b); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 167 | private boolean isGlobalPriorityActiveLocked() { |
| 168 | return mGlobalPrioritySession != null && mGlobalPrioritySession.isActive(); |
| 169 | } |
| 170 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 171 | public void updateSession(MediaSessionRecord record) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 172 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 173 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 174 | if (user == null || !user.mPriorityStack.contains(record)) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 175 | Log.d(TAG, "Unknown session updated. Ignoring."); |
| 176 | return; |
| 177 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 178 | user.mPriorityStack.onSessionStateChange(record); |
| 179 | if ((record.getFlags() & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) { |
| 180 | mGlobalPrioritySession = record; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 181 | user.pushAddressedPlayerChangedLocked(); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 182 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 183 | mHandler.postSessionsChanged(record.getUserId()); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 187 | /** |
Hyundo Moon | a055f13 | 2017-01-13 15:31:06 +0900 | [diff] [blame] | 188 | * Tells the system UI that volume has changed on an active remote session. |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 189 | */ |
| 190 | public void notifyRemoteVolumeChanged(int flags, MediaSessionRecord session) { |
Hyundo Moon | a055f13 | 2017-01-13 15:31:06 +0900 | [diff] [blame] | 191 | if (mRvc == null || !session.isActive()) { |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | try { |
| 195 | mRvc.remoteVolumeChanged(session.getControllerBinder(), flags); |
| 196 | } catch (Exception e) { |
| 197 | Log.wtf(TAG, "Error sending volume change to system UI.", e); |
| 198 | } |
| 199 | } |
| 200 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 201 | public void onSessionPlaystateChanged(MediaSessionRecord record, int oldState, int newState) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 202 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 203 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 204 | if (user == null || !user.mPriorityStack.contains(record)) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 205 | Log.d(TAG, "Unknown session changed playback state. Ignoring."); |
| 206 | return; |
| 207 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 208 | user.mPriorityStack.onPlaystateChanged(record, oldState, newState); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 212 | public void onSessionPlaybackTypeChanged(MediaSessionRecord record) { |
| 213 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 214 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 215 | if (user == null || !user.mPriorityStack.contains(record)) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 216 | Log.d(TAG, "Unknown session changed playback type. Ignoring."); |
| 217 | return; |
| 218 | } |
| 219 | pushRemoteVolumeUpdateLocked(record.getUserId()); |
| 220 | } |
| 221 | } |
| 222 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 223 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 224 | public void onStartUser(int userId) { |
| 225 | if (DEBUG) Log.d(TAG, "onStartUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 226 | updateUser(); |
| 227 | } |
| 228 | |
| 229 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 230 | public void onSwitchUser(int userId) { |
| 231 | if (DEBUG) Log.d(TAG, "onSwitchUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 232 | updateUser(); |
| 233 | } |
| 234 | |
| 235 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 236 | public void onStopUser(int userId) { |
| 237 | if (DEBUG) Log.d(TAG, "onStopUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 238 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 239 | FullUserRecord user = getFullUserRecordLocked(userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 240 | if (user != null) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 241 | if (user.mFullUserId == userId) { |
| 242 | user.destroySessionsForUserLocked(UserHandle.USER_ALL); |
| 243 | mUserRecords.remove(userId); |
| 244 | } else { |
| 245 | user.destroySessionsForUserLocked(userId); |
| 246 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 247 | } |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 248 | updateUser(); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 253 | public void monitor() { |
| 254 | synchronized (mLock) { |
| 255 | // Check for deadlock |
| 256 | } |
| 257 | } |
| 258 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 259 | protected void enforcePhoneStatePermission(int pid, int uid) { |
| 260 | if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid) |
| 261 | != PackageManager.PERMISSION_GRANTED) { |
| 262 | throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission."); |
| 263 | } |
| 264 | } |
| 265 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 266 | void sessionDied(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 267 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 268 | destroySessionLocked(session); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void destroySession(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 273 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 274 | destroySessionLocked(session); |
| 275 | } |
| 276 | } |
| 277 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 278 | private void updateUser() { |
| 279 | synchronized (mLock) { |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 280 | UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 281 | mFullUserIds.clear(); |
| 282 | List<UserInfo> allUsers = manager.getUsers(); |
| 283 | if (allUsers != null) { |
| 284 | for (UserInfo userInfo : allUsers) { |
| 285 | if (userInfo.isManagedProfile()) { |
| 286 | mFullUserIds.put(userInfo.id, userInfo.profileGroupId); |
| 287 | } else { |
| 288 | mFullUserIds.put(userInfo.id, userInfo.id); |
| 289 | if (mUserRecords.get(userInfo.id) == null) { |
| 290 | mUserRecords.put(userInfo.id, new FullUserRecord(userInfo.id)); |
| 291 | } |
| 292 | } |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 293 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 294 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 295 | // Ensure that the current full user exists. |
| 296 | int currentFullUserId = ActivityManager.getCurrentUser(); |
| 297 | mCurrentFullUserRecord = mUserRecords.get(currentFullUserId); |
| 298 | if (mCurrentFullUserRecord == null) { |
| 299 | Log.w(TAG, "Cannot find FullUserInfo for the current user " + currentFullUserId); |
| 300 | mCurrentFullUserRecord = new FullUserRecord(currentFullUserId); |
| 301 | mUserRecords.put(currentFullUserId, mCurrentFullUserRecord); |
| 302 | } |
| 303 | mFullUserIds.put(currentFullUserId, currentFullUserId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 307 | private void updateActiveSessionListeners() { |
| 308 | synchronized (mLock) { |
| 309 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 310 | SessionsListenerRecord listener = mSessionsListeners.get(i); |
| 311 | try { |
| 312 | enforceMediaPermissions(listener.mComponentName, listener.mPid, listener.mUid, |
| 313 | listener.mUserId); |
| 314 | } catch (SecurityException e) { |
| 315 | Log.i(TAG, "ActiveSessionsListener " + listener.mComponentName |
| 316 | + " is no longer authorized. Disconnecting."); |
| 317 | mSessionsListeners.remove(i); |
| 318 | try { |
| 319 | listener.mListener |
| 320 | .onActiveSessionsChanged(new ArrayList<MediaSession.Token>()); |
| 321 | } catch (Exception e1) { |
| 322 | // ignore |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 329 | /* |
| 330 | * When a session is removed several things need to happen. |
| 331 | * 1. We need to remove it from the relevant user. |
| 332 | * 2. We need to remove it from the priority stack. |
| 333 | * 3. We need to remove it from all sessions. |
| 334 | * 4. If this is the system priority session we need to clear it. |
| 335 | * 5. We need to unlink to death from the cb binder |
| 336 | * 6. We need to tell the session to do any final cleanup (onDestroy) |
| 337 | */ |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 338 | private void destroySessionLocked(MediaSessionRecord session) { |
Insun Kang | 30be970a | 2015-11-26 15:35:44 +0900 | [diff] [blame] | 339 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 340 | Log.d(TAG, "Destroying " + session); |
Insun Kang | 30be970a | 2015-11-26 15:35:44 +0900 | [diff] [blame] | 341 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 342 | int userId = session.getUserId(); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 343 | FullUserRecord user = getFullUserRecordLocked(userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 344 | if (user != null) { |
| 345 | user.removeSessionLocked(session); |
| 346 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 347 | if (mGlobalPrioritySession == session) { |
| 348 | mGlobalPrioritySession = null; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 349 | if (session.isActive() && user != null) { |
| 350 | user.pushAddressedPlayerChangedLocked(); |
| 351 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 352 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 353 | |
| 354 | try { |
| 355 | session.getCallback().asBinder().unlinkToDeath(session, 0); |
| 356 | } catch (Exception e) { |
| 357 | // ignore exceptions while destroying a session. |
| 358 | } |
| 359 | session.onDestroy(); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 360 | mHandler.postSessionsChanged(session.getUserId()); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | private void enforcePackageName(String packageName, int uid) { |
| 364 | if (TextUtils.isEmpty(packageName)) { |
| 365 | throw new IllegalArgumentException("packageName may not be empty"); |
| 366 | } |
| 367 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 368 | final int packageCount = packages.length; |
| 369 | for (int i = 0; i < packageCount; i++) { |
| 370 | if (packageName.equals(packages[i])) { |
| 371 | return; |
| 372 | } |
| 373 | } |
| 374 | throw new IllegalArgumentException("packageName is not owned by the calling process"); |
| 375 | } |
| 376 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 377 | /** |
| 378 | * Checks a caller's authorization to register an IRemoteControlDisplay. |
| 379 | * Authorization is granted if one of the following is true: |
| 380 | * <ul> |
| 381 | * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL |
| 382 | * permission</li> |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 383 | * <li>the caller's listener is one of the enabled notification listeners |
| 384 | * for the caller's user</li> |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 385 | * </ul> |
| 386 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 387 | private void enforceMediaPermissions(ComponentName compName, int pid, int uid, |
| 388 | int resolvedUserId) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 389 | if (isCurrentVolumeController(uid, pid)) return; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 390 | if (getContext() |
| 391 | .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid) |
| 392 | != PackageManager.PERMISSION_GRANTED |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 393 | && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid), |
| 394 | resolvedUserId)) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 395 | throw new SecurityException("Missing permission to control media."); |
| 396 | } |
| 397 | } |
| 398 | |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 399 | private boolean isCurrentVolumeController(int uid, int pid) { |
| 400 | return getContext().checkPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 401 | pid, uid) == PackageManager.PERMISSION_GRANTED; |
John Spurlock | be19ed0 | 2015-02-22 10:57:55 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | private void enforceSystemUiPermission(String action, int pid, int uid) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 405 | if (!isCurrentVolumeController(uid, pid)) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 406 | throw new SecurityException("Only system ui may " + action); |
| 407 | } |
| 408 | } |
| 409 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 410 | /** |
| 411 | * This checks if the component is an enabled notification listener for the |
| 412 | * specified user. Enabled components may only operate on behalf of the user |
| 413 | * they're running as. |
| 414 | * |
| 415 | * @param compName The component that is enabled. |
| 416 | * @param userId The user id of the caller. |
| 417 | * @param forUserId The user id they're making the request on behalf of. |
| 418 | * @return True if the component is enabled, false otherwise |
| 419 | */ |
| 420 | private boolean isEnabledNotificationListener(ComponentName compName, int userId, |
| 421 | int forUserId) { |
| 422 | if (userId != forUserId) { |
| 423 | // You may not access another user's content as an enabled listener. |
| 424 | return false; |
| 425 | } |
RoboErik | 51fa6bc | 2014-06-20 14:59:58 -0700 | [diff] [blame] | 426 | if (DEBUG) { |
| 427 | Log.d(TAG, "Checking if enabled notification listener " + compName); |
| 428 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 429 | if (compName != null) { |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 430 | final String enabledNotifListeners = Settings.Secure.getStringForUser(mContentResolver, |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 431 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 432 | userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 433 | if (enabledNotifListeners != null) { |
| 434 | final String[] components = enabledNotifListeners.split(":"); |
| 435 | for (int i = 0; i < components.length; i++) { |
| 436 | final ComponentName component = |
| 437 | ComponentName.unflattenFromString(components[i]); |
| 438 | if (component != null) { |
| 439 | if (compName.equals(component)) { |
| 440 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 441 | Log.d(TAG, "ok to get sessions. " + component + |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 442 | " is authorized notification listener"); |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 450 | Log.d(TAG, "not ok to get sessions. " + compName + |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 451 | " is not in list of ENABLED_NOTIFICATION_LISTENERS for user " + userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 457 | private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId, |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 458 | String callerPackageName, ISessionCallback cb, String tag) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 459 | synchronized (mLock) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 460 | return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 464 | /* |
| 465 | * When a session is created the following things need to happen. |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 466 | * 1. Its callback binder needs a link to death |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 467 | * 2. It needs to be added to all sessions. |
| 468 | * 3. It needs to be added to the priority stack. |
| 469 | * 4. It needs to be added to the relevant user record. |
| 470 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 471 | private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId, |
| 472 | String callerPackageName, ISessionCallback cb, String tag) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 473 | FullUserRecord user = getFullUserRecordLocked(userId); |
Dongwon Kang | 8cf39c5 | 2016-07-29 13:20:39 -0700 | [diff] [blame] | 474 | if (user == null) { |
| 475 | Log.wtf(TAG, "Request from invalid user: " + userId); |
| 476 | throw new RuntimeException("Session request from invalid user."); |
| 477 | } |
| 478 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 479 | final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId, |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 480 | callerPackageName, cb, tag, this, mHandler.getLooper()); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 481 | try { |
| 482 | cb.asBinder().linkToDeath(session, 0); |
| 483 | } catch (RemoteException e) { |
| 484 | throw new RuntimeException("Media Session owner died prematurely.", e); |
| 485 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 486 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 487 | user.addSessionLocked(session); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 488 | mHandler.postSessionsChanged(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 489 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 490 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 491 | Log.d(TAG, "Created session for " + callerPackageName + " with tag " + tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 492 | } |
| 493 | return session; |
| 494 | } |
| 495 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 496 | private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) { |
| 497 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
RoboErik | a08adb24 | 2014-11-21 18:28:18 -0800 | [diff] [blame] | 498 | if (mSessionsListeners.get(i).mListener.asBinder() == listener.asBinder()) { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 499 | return i; |
| 500 | } |
| 501 | } |
| 502 | return -1; |
| 503 | } |
| 504 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 505 | private void pushSessionsChanged(int userId) { |
| 506 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 507 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 508 | if (user == null) { |
| 509 | Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId); |
| 510 | return; |
| 511 | } |
| 512 | List<MediaSessionRecord> records = user.mPriorityStack.getActiveSessions(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 513 | int size = records.size(); |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 514 | ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 515 | for (int i = 0; i < size; i++) { |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 516 | tokens.add(new MediaSession.Token(records.get(i).getControllerBinder())); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 517 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 518 | pushRemoteVolumeUpdateLocked(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 519 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 520 | SessionsListenerRecord record = mSessionsListeners.get(i); |
| 521 | if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) { |
| 522 | try { |
| 523 | record.mListener.onActiveSessionsChanged(tokens); |
| 524 | } catch (RemoteException e) { |
| 525 | Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing", |
| 526 | e); |
| 527 | mSessionsListeners.remove(i); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 534 | private void pushRemoteVolumeUpdateLocked(int userId) { |
| 535 | if (mRvc != null) { |
| 536 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 537 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 538 | if (user == null) { |
| 539 | Log.w(TAG, "pushRemoteVolumeUpdateLocked failed. No user with id=" + userId); |
| 540 | return; |
| 541 | } |
| 542 | MediaSessionRecord record = user.mPriorityStack.getDefaultRemoteSession(userId); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 543 | mRvc.updateRemoteController(record == null ? null : record.getControllerBinder()); |
| 544 | } catch (RemoteException e) { |
| 545 | Log.wtf(TAG, "Error sending default remote volume to sys ui.", e); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 550 | /** |
| 551 | * Called when the media button receiver for the {@param record} is changed. |
| 552 | * |
| 553 | * @param record the media session whose media button receiver is updated. |
| 554 | */ |
| 555 | public void onMediaButtonReceiverChanged(MediaSessionRecord record) { |
| 556 | synchronized (mLock) { |
| 557 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 558 | MediaSessionRecord mediaButtonSession = |
| 559 | user.mPriorityStack.getMediaButtonSession(); |
| 560 | if (record == mediaButtonSession) { |
| 561 | user.rememberMediaButtonReceiverLocked(mediaButtonSession); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 566 | private String getCallingPackageName(int uid) { |
| 567 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 568 | if (packages != null && packages.length > 0) { |
| 569 | return packages[0]; |
| 570 | } |
| 571 | return ""; |
| 572 | } |
| 573 | |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 574 | private void dispatchVolumeKeyLongPressLocked(KeyEvent keyEvent) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 575 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 576 | mCurrentFullUserRecord.mOnVolumeKeyLongPressListener.onVolumeKeyLongPress(keyEvent); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 577 | } catch (RemoteException e) { |
| 578 | Log.w(TAG, "Failed to send " + keyEvent + " to volume key long-press listener"); |
| 579 | } |
| 580 | } |
| 581 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 582 | private FullUserRecord getFullUserRecordLocked(int userId) { |
| 583 | int fullUserId = mFullUserIds.get(userId, -1); |
| 584 | if (fullUserId < 0) { |
| 585 | return null; |
| 586 | } |
| 587 | return mUserRecords.get(fullUserId); |
| 588 | } |
| 589 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 590 | /** |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 591 | * Information about a full user and its corresponding managed profiles. |
| 592 | * |
| 593 | * <p>Since the full user runs together with its managed profiles, a user wouldn't differentiate |
| 594 | * them when he/she presses a media/volume button. So keeping media sessions for them in one |
| 595 | * place makes more sense and increases the readability.</p> |
| 596 | * <p>The contents of this object is guarded by {@link #mLock}. |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 597 | */ |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 598 | final class FullUserRecord implements MediaSessionStack.OnMediaButtonSessionChangedListener { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 599 | private static final String COMPONENT_NAME_USER_ID_DELIM = ","; |
| 600 | private final int mFullUserId; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 601 | private final MediaSessionStack mPriorityStack; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 602 | private PendingIntent mLastMediaButtonReceiver; |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 603 | private ComponentName mRestoredMediaButtonReceiver; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 604 | private int mRestoredMediaButtonReceiverUserId; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 605 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 606 | private IOnVolumeKeyLongPressListener mOnVolumeKeyLongPressListener; |
| 607 | private int mOnVolumeKeyLongPressListenerUid; |
| 608 | private KeyEvent mInitialDownVolumeKeyEvent; |
| 609 | private int mInitialDownVolumeStream; |
| 610 | private boolean mInitialDownMusicOnly; |
| 611 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 612 | private IOnMediaKeyListener mOnMediaKeyListener; |
| 613 | private int mOnMediaKeyListenerUid; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 614 | private ICallback mCallback; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 615 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 616 | public FullUserRecord(int fullUserId) { |
| 617 | mFullUserId = fullUserId; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 618 | mPriorityStack = new MediaSessionStack(mAudioPlaybackMonitor, this); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 619 | // Restore the remembered media button receiver before the boot. |
| 620 | String mediaButtonReceiver = Settings.Secure.getStringForUser(mContentResolver, |
| 621 | Settings.System.MEDIA_BUTTON_RECEIVER, mFullUserId); |
| 622 | if (mediaButtonReceiver == null) { |
| 623 | return; |
| 624 | } |
| 625 | String[] tokens = mediaButtonReceiver.split(COMPONENT_NAME_USER_ID_DELIM); |
| 626 | if (tokens == null || tokens.length != 2) { |
| 627 | return; |
| 628 | } |
| 629 | mRestoredMediaButtonReceiver = ComponentName.unflattenFromString(tokens[0]); |
| 630 | mRestoredMediaButtonReceiverUserId = Integer.parseInt(tokens[1]); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 633 | public void destroySessionsForUserLocked(int userId) { |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 634 | List<MediaSessionRecord> sessions = mPriorityStack.getPriorityList(false, userId); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 635 | for (MediaSessionRecord session : sessions) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 636 | MediaSessionService.this.destroySessionLocked(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 640 | public void addSessionLocked(MediaSessionRecord session) { |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 641 | mPriorityStack.addSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | public void removeSessionLocked(MediaSessionRecord session) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 645 | mPriorityStack.removeSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | public void dumpLocked(PrintWriter pw, String prefix) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 649 | pw.print(prefix + "Record for full_user=" + mFullUserId); |
| 650 | // Dump managed profile user ids associated with this user. |
| 651 | int size = mFullUserIds.size(); |
| 652 | for (int i = 0; i < size; i++) { |
| 653 | if (mFullUserIds.keyAt(i) != mFullUserIds.valueAt(i) |
| 654 | && mFullUserIds.valueAt(i) == mFullUserId) { |
| 655 | pw.print(", profile_user=" + mFullUserIds.keyAt(i)); |
| 656 | } |
| 657 | } |
| 658 | pw.println(); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 659 | String indent = prefix + " "; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 660 | pw.println(indent + "Volume key long-press listener: " + mOnVolumeKeyLongPressListener); |
| 661 | pw.println(indent + "Volume key long-press listener package: " + |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 662 | getCallingPackageName(mOnVolumeKeyLongPressListenerUid)); |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 663 | pw.println(indent + "Media key listener: " + mOnMediaKeyListener); |
| 664 | pw.println(indent + "Media key listener package: " + |
| 665 | getCallingPackageName(mOnMediaKeyListenerUid)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 666 | pw.println(indent + "Callback: " + mCallback); |
| 667 | pw.println(indent + "Last MediaButtonReceiver: " + mLastMediaButtonReceiver); |
| 668 | pw.println(indent + "Restored MediaButtonReceiver: " + mRestoredMediaButtonReceiver); |
| 669 | mPriorityStack.dump(pw, indent); |
| 670 | } |
| 671 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 672 | @Override |
| 673 | public void onMediaButtonSessionChanged(MediaSessionRecord oldMediaButtonSession, |
| 674 | MediaSessionRecord newMediaButtonSession) { |
| 675 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 98e4aaf | 2017-05-12 17:06:47 +0900 | [diff] [blame^] | 676 | Log.d(TAG, "Media button session is changed to " + newMediaButtonSession); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 677 | } |
| 678 | synchronized (mLock) { |
| 679 | if (oldMediaButtonSession != null) { |
| 680 | mHandler.postSessionsChanged(oldMediaButtonSession.getUserId()); |
| 681 | } |
| 682 | if (newMediaButtonSession != null) { |
| 683 | rememberMediaButtonReceiverLocked(newMediaButtonSession); |
| 684 | mHandler.postSessionsChanged(newMediaButtonSession.getUserId()); |
| 685 | } |
| 686 | pushAddressedPlayerChangedLocked(); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Remember media button receiver and keep it in the persistent storage. |
| 691 | public void rememberMediaButtonReceiverLocked(MediaSessionRecord record) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 692 | PendingIntent receiver = record.getMediaButtonReceiver(); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 693 | mLastMediaButtonReceiver = receiver; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 694 | mRestoredMediaButtonReceiver = null; |
| 695 | String componentName = ""; |
| 696 | if (receiver != null) { |
| 697 | ComponentName component = receiver.getIntent().getComponent(); |
| 698 | if (component != null |
| 699 | && record.getPackageName().equals(component.getPackageName())) { |
| 700 | componentName = component.flattenToString(); |
| 701 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 702 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 703 | Settings.Secure.putStringForUser(mContentResolver, |
| 704 | Settings.System.MEDIA_BUTTON_RECEIVER, |
| 705 | componentName + COMPONENT_NAME_USER_ID_DELIM + record.getUserId(), |
| 706 | mFullUserId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 707 | } |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 708 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 709 | private void pushAddressedPlayerChangedLocked() { |
| 710 | if (mCallback == null) { |
| 711 | return; |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 712 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 713 | try { |
| 714 | MediaSessionRecord mediaButtonSession = getMediaButtonSessionLocked(); |
| 715 | if (mediaButtonSession != null) { |
| 716 | mCallback.onAddressedPlayerChangedToMediaSession( |
| 717 | new MediaSession.Token(mediaButtonSession.getControllerBinder())); |
| 718 | } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) { |
| 719 | mCallback.onAddressedPlayerChangedToMediaButtonReceiver( |
| 720 | mCurrentFullUserRecord.mLastMediaButtonReceiver |
| 721 | .getIntent().getComponent()); |
| 722 | } else if (mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) { |
| 723 | mCallback.onAddressedPlayerChangedToMediaButtonReceiver( |
| 724 | mCurrentFullUserRecord.mRestoredMediaButtonReceiver); |
| 725 | } |
| 726 | } catch (RemoteException e) { |
| 727 | Log.w(TAG, "Failed to pushAddressedPlayerChangedLocked", e); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | private MediaSessionRecord getMediaButtonSessionLocked() { |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 732 | return isGlobalPriorityActiveLocked() |
| 733 | ? mGlobalPrioritySession : mPriorityStack.getMediaButtonSession(); |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 734 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 735 | } |
| 736 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 737 | final class SessionsListenerRecord implements IBinder.DeathRecipient { |
| 738 | private final IActiveSessionsListener mListener; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 739 | private final ComponentName mComponentName; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 740 | private final int mUserId; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 741 | private final int mPid; |
| 742 | private final int mUid; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 743 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 744 | public SessionsListenerRecord(IActiveSessionsListener listener, |
| 745 | ComponentName componentName, |
| 746 | int userId, int pid, int uid) { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 747 | mListener = listener; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 748 | mComponentName = componentName; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 749 | mUserId = userId; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 750 | mPid = pid; |
| 751 | mUid = uid; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public void binderDied() { |
| 756 | synchronized (mLock) { |
| 757 | mSessionsListeners.remove(this); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 762 | final class SettingsObserver extends ContentObserver { |
| 763 | private final Uri mSecureSettingsUri = Settings.Secure.getUriFor( |
| 764 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
| 765 | |
| 766 | private SettingsObserver() { |
| 767 | super(null); |
| 768 | } |
| 769 | |
| 770 | private void observe() { |
| 771 | mContentResolver.registerContentObserver(mSecureSettingsUri, |
| 772 | false, this, UserHandle.USER_ALL); |
| 773 | } |
| 774 | |
| 775 | @Override |
| 776 | public void onChange(boolean selfChange, Uri uri) { |
| 777 | updateActiveSessionListeners(); |
| 778 | } |
| 779 | } |
| 780 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 781 | class SessionManagerImpl extends ISessionManager.Stub { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 782 | private static final String EXTRA_WAKELOCK_ACQUIRED = |
| 783 | "android.media.AudioService.WAKELOCK_ACQUIRED"; |
| 784 | private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number |
| 785 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 786 | private boolean mVoiceButtonDown = false; |
| 787 | private boolean mVoiceButtonHandled = false; |
| 788 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 789 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 790 | public ISession createSession(String packageName, ISessionCallback cb, String tag, |
| 791 | int userId) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 792 | final int pid = Binder.getCallingPid(); |
| 793 | final int uid = Binder.getCallingUid(); |
| 794 | final long token = Binder.clearCallingIdentity(); |
| 795 | try { |
| 796 | enforcePackageName(packageName, uid); |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 797 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 798 | false /* allowAll */, true /* requireFull */, "createSession", packageName); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 799 | if (cb == null) { |
| 800 | throw new IllegalArgumentException("Controller callback cannot be null"); |
| 801 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 802 | return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag) |
| 803 | .getSessionBinder(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 804 | } finally { |
| 805 | Binder.restoreCallingIdentity(token); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 810 | public List<IBinder> getSessions(ComponentName componentName, int userId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 811 | final int pid = Binder.getCallingPid(); |
| 812 | final int uid = Binder.getCallingUid(); |
| 813 | final long token = Binder.clearCallingIdentity(); |
| 814 | |
| 815 | try { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 816 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 817 | ArrayList<IBinder> binders = new ArrayList<IBinder>(); |
| 818 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 819 | if (resolvedUserId == UserHandle.USER_ALL) { |
| 820 | int size = mUserRecords.size(); |
| 821 | for (int i = 0; i < size; i++) { |
| 822 | List<MediaSessionRecord> records = |
| 823 | mUserRecords.valueAt(i).mPriorityStack.getActiveSessions( |
| 824 | resolvedUserId); |
| 825 | for (MediaSessionRecord record : records) { |
| 826 | binders.add(record.getControllerBinder().asBinder()); |
| 827 | } |
| 828 | } |
| 829 | } else { |
| 830 | FullUserRecord user = getFullUserRecordLocked(resolvedUserId); |
| 831 | if (user == null) { |
| 832 | Log.w(TAG, "getSessions failed. Unknown user " + userId); |
| 833 | return binders; |
| 834 | } |
| 835 | List<MediaSessionRecord> records = user.mPriorityStack |
| 836 | .getActiveSessions(resolvedUserId); |
| 837 | for (MediaSessionRecord record : records) { |
| 838 | binders.add(record.getControllerBinder().asBinder()); |
| 839 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | return binders; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 843 | } finally { |
| 844 | Binder.restoreCallingIdentity(token); |
| 845 | } |
| 846 | } |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 847 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 848 | @Override |
| 849 | public void addSessionsListener(IActiveSessionsListener listener, |
| 850 | ComponentName componentName, int userId) throws RemoteException { |
| 851 | final int pid = Binder.getCallingPid(); |
| 852 | final int uid = Binder.getCallingUid(); |
| 853 | final long token = Binder.clearCallingIdentity(); |
| 854 | |
| 855 | try { |
| 856 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
| 857 | synchronized (mLock) { |
| 858 | int index = findIndexOfSessionsListenerLocked(listener); |
| 859 | if (index != -1) { |
| 860 | Log.w(TAG, "ActiveSessionsListener is already added, ignoring"); |
| 861 | return; |
| 862 | } |
| 863 | SessionsListenerRecord record = new SessionsListenerRecord(listener, |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 864 | componentName, resolvedUserId, pid, uid); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 865 | try { |
| 866 | listener.asBinder().linkToDeath(record, 0); |
| 867 | } catch (RemoteException e) { |
| 868 | Log.e(TAG, "ActiveSessionsListener is dead, ignoring it", e); |
| 869 | return; |
| 870 | } |
| 871 | mSessionsListeners.add(record); |
| 872 | } |
| 873 | } finally { |
| 874 | Binder.restoreCallingIdentity(token); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | @Override |
| 879 | public void removeSessionsListener(IActiveSessionsListener listener) |
| 880 | throws RemoteException { |
| 881 | synchronized (mLock) { |
| 882 | int index = findIndexOfSessionsListenerLocked(listener); |
| 883 | if (index != -1) { |
| 884 | SessionsListenerRecord record = mSessionsListeners.remove(index); |
| 885 | try { |
| 886 | record.mListener.asBinder().unlinkToDeath(record, 0); |
| 887 | } catch (Exception e) { |
| 888 | // ignore exceptions, the record is being removed |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 894 | /** |
| 895 | * Handles the dispatching of the media button events to one of the |
| 896 | * registered listeners, or if there was none, broadcast an |
| 897 | * ACTION_MEDIA_BUTTON intent to the rest of the system. |
| 898 | * |
| 899 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 900 | * supported media buttons |
| 901 | * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held |
| 902 | * while this key event is dispatched. |
| 903 | */ |
| 904 | @Override |
| 905 | public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { |
| 906 | if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) { |
| 907 | Log.w(TAG, "Attempted to dispatch null or non-media key event."); |
| 908 | return; |
| 909 | } |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 910 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 911 | final int pid = Binder.getCallingPid(); |
| 912 | final int uid = Binder.getCallingUid(); |
| 913 | final long token = Binder.clearCallingIdentity(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 914 | try { |
Jeff Brown | 221a827 | 2015-03-23 13:53:09 -0700 | [diff] [blame] | 915 | if (DEBUG) { |
| 916 | Log.d(TAG, "dispatchMediaKeyEvent, pid=" + pid + ", uid=" + uid + ", event=" |
| 917 | + keyEvent); |
| 918 | } |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 919 | if (!isUserSetupComplete()) { |
| 920 | // Global media key handling can have the side-effect of starting new |
| 921 | // activities which is undesirable while setup is in progress. |
| 922 | Slog.i(TAG, "Not dispatching media key event because user " |
| 923 | + "setup is in progress."); |
| 924 | return; |
| 925 | } |
| 926 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 927 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 928 | boolean isGlobalPriorityActive = isGlobalPriorityActiveLocked(); |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 929 | if (isGlobalPriorityActive && uid != Process.SYSTEM_UID) { |
| 930 | // Prevent dispatching key event through reflection while the global |
| 931 | // priority session is active. |
| 932 | Slog.i(TAG, "Only the system can dispatch media key event " |
| 933 | + "to the global priority session."); |
| 934 | return; |
| 935 | } |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 936 | if (!isGlobalPriorityActive) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 937 | if (mCurrentFullUserRecord.mOnMediaKeyListener != null) { |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 938 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 939 | Log.d(TAG, "Send " + keyEvent + " to the media key listener"); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 940 | } |
| 941 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 942 | mCurrentFullUserRecord.mOnMediaKeyListener.onMediaKey(keyEvent, |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 943 | new MediaKeyListenerResultReceiver(keyEvent, needWakeLock)); |
| 944 | return; |
| 945 | } catch (RemoteException e) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 946 | Log.w(TAG, "Failed to send " + keyEvent |
| 947 | + " to the media key listener"); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | } |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 951 | if (!isGlobalPriorityActive && isVoiceKey(keyEvent.getKeyCode())) { |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 952 | handleVoiceKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 953 | } else { |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 954 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } finally { |
| 958 | Binder.restoreCallingIdentity(token); |
| 959 | } |
| 960 | } |
| 961 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 962 | @Override |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 963 | public void setCallback(ICallback callback) { |
| 964 | final int pid = Binder.getCallingPid(); |
| 965 | final int uid = Binder.getCallingUid(); |
| 966 | final long token = Binder.clearCallingIdentity(); |
| 967 | try { |
Amith Yamasani | e259ad2 | 2017-04-24 11:30:19 -0700 | [diff] [blame] | 968 | if (!UserHandle.isSameApp(uid, Process.BLUETOOTH_UID)) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 969 | throw new SecurityException("Only Bluetooth service processes can set" |
| 970 | + " Callback"); |
| 971 | } |
| 972 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 973 | int userId = UserHandle.getUserId(uid); |
| 974 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 975 | if (user == null || user.mFullUserId != userId) { |
| 976 | Log.w(TAG, "Only the full user can set the callback" |
| 977 | + ", userId=" + userId); |
| 978 | return; |
| 979 | } |
| 980 | user.mCallback = callback; |
| 981 | Log.d(TAG, "The callback " + user.mCallback |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 982 | + " is set by " + getCallingPackageName(uid)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 983 | if (user.mCallback == null) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 987 | user.mCallback.asBinder().linkToDeath( |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 988 | new IBinder.DeathRecipient() { |
| 989 | @Override |
| 990 | public void binderDied() { |
| 991 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 992 | user.mCallback = null; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | }, 0); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 996 | user.pushAddressedPlayerChangedLocked(); |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 997 | } catch (RemoteException e) { |
| 998 | Log.w(TAG, "Failed to set callback", e); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 999 | user.mCallback = null; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } finally { |
| 1003 | Binder.restoreCallingIdentity(token); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1008 | public void setOnVolumeKeyLongPressListener(IOnVolumeKeyLongPressListener listener) { |
| 1009 | final int pid = Binder.getCallingPid(); |
| 1010 | final int uid = Binder.getCallingUid(); |
| 1011 | final long token = Binder.clearCallingIdentity(); |
| 1012 | try { |
| 1013 | // Enforce SET_VOLUME_KEY_LONG_PRESS_LISTENER permission. |
| 1014 | if (getContext().checkPermission( |
| 1015 | android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER, pid, uid) |
| 1016 | != PackageManager.PERMISSION_GRANTED) { |
| 1017 | throw new SecurityException("Must hold the SET_VOLUME_KEY_LONG_PRESS_LISTENER" + |
| 1018 | " permission."); |
| 1019 | } |
| 1020 | |
| 1021 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1022 | int userId = UserHandle.getUserId(uid); |
| 1023 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 1024 | if (user == null || user.mFullUserId != userId) { |
| 1025 | Log.w(TAG, "Only the full user can set the volume key long-press listener" |
| 1026 | + ", userId=" + userId); |
| 1027 | return; |
| 1028 | } |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1029 | if (user.mOnVolumeKeyLongPressListener != null && |
| 1030 | user.mOnVolumeKeyLongPressListenerUid != uid) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1031 | Log.w(TAG, "The volume key long-press listener cannot be reset" |
| 1032 | + " by another app , mOnVolumeKeyLongPressListener=" |
| 1033 | + user.mOnVolumeKeyLongPressListenerUid |
| 1034 | + ", uid=" + uid); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | |
| 1038 | user.mOnVolumeKeyLongPressListener = listener; |
| 1039 | user.mOnVolumeKeyLongPressListenerUid = uid; |
| 1040 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1041 | Log.d(TAG, "The volume key long-press listener " |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1042 | + listener + " is set by " + getCallingPackageName(uid)); |
| 1043 | |
| 1044 | if (user.mOnVolumeKeyLongPressListener != null) { |
| 1045 | try { |
| 1046 | user.mOnVolumeKeyLongPressListener.asBinder().linkToDeath( |
| 1047 | new IBinder.DeathRecipient() { |
| 1048 | @Override |
| 1049 | public void binderDied() { |
| 1050 | synchronized (mLock) { |
| 1051 | user.mOnVolumeKeyLongPressListener = null; |
| 1052 | } |
| 1053 | } |
| 1054 | }, 0); |
| 1055 | } catch (RemoteException e) { |
| 1056 | Log.w(TAG, "Failed to set death recipient " |
| 1057 | + user.mOnVolumeKeyLongPressListener); |
| 1058 | user.mOnVolumeKeyLongPressListener = null; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } finally { |
| 1063 | Binder.restoreCallingIdentity(token); |
| 1064 | } |
| 1065 | } |
| 1066 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1067 | @Override |
| 1068 | public void setOnMediaKeyListener(IOnMediaKeyListener listener) { |
| 1069 | final int pid = Binder.getCallingPid(); |
| 1070 | final int uid = Binder.getCallingUid(); |
| 1071 | final long token = Binder.clearCallingIdentity(); |
| 1072 | try { |
| 1073 | // Enforce SET_MEDIA_KEY_LISTENER permission. |
| 1074 | if (getContext().checkPermission( |
| 1075 | android.Manifest.permission.SET_MEDIA_KEY_LISTENER, pid, uid) |
| 1076 | != PackageManager.PERMISSION_GRANTED) { |
| 1077 | throw new SecurityException("Must hold the SET_MEDIA_KEY_LISTENER" + |
| 1078 | " permission."); |
| 1079 | } |
| 1080 | |
| 1081 | synchronized (mLock) { |
| 1082 | int userId = UserHandle.getUserId(uid); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1083 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 1084 | if (user == null || user.mFullUserId != userId) { |
| 1085 | Log.w(TAG, "Only the full user can set the media key listener" |
| 1086 | + ", userId=" + userId); |
| 1087 | return; |
| 1088 | } |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1089 | if (user.mOnMediaKeyListener != null && user.mOnMediaKeyListenerUid != uid) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1090 | Log.w(TAG, "The media key listener cannot be reset by another app. " |
| 1091 | + ", mOnMediaKeyListenerUid=" + user.mOnMediaKeyListenerUid |
| 1092 | + ", uid=" + uid); |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | user.mOnMediaKeyListener = listener; |
| 1097 | user.mOnMediaKeyListenerUid = uid; |
| 1098 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1099 | Log.d(TAG, "The media key listener " + user.mOnMediaKeyListener |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1100 | + " is set by " + getCallingPackageName(uid)); |
| 1101 | |
| 1102 | if (user.mOnMediaKeyListener != null) { |
| 1103 | try { |
| 1104 | user.mOnMediaKeyListener.asBinder().linkToDeath( |
| 1105 | new IBinder.DeathRecipient() { |
| 1106 | @Override |
| 1107 | public void binderDied() { |
| 1108 | synchronized (mLock) { |
| 1109 | user.mOnMediaKeyListener = null; |
| 1110 | } |
| 1111 | } |
| 1112 | }, 0); |
| 1113 | } catch (RemoteException e) { |
| 1114 | Log.w(TAG, "Failed to set death recipient " + user.mOnMediaKeyListener); |
| 1115 | user.mOnMediaKeyListener = null; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | } finally { |
| 1120 | Binder.restoreCallingIdentity(token); |
| 1121 | } |
| 1122 | } |
| 1123 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1124 | /** |
| 1125 | * Handles the dispatching of the volume button events to one of the |
| 1126 | * registered listeners. If there's a volume key long-press listener and |
| 1127 | * there's no active global priority session, long-pressess will be sent to the |
| 1128 | * long-press listener instead of adjusting volume. |
| 1129 | * |
| 1130 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 1131 | * {@link KeyEvent#KEYCODE_VOLUME_UP}, |
| 1132 | * {@link KeyEvent#KEYCODE_VOLUME_DOWN}, |
| 1133 | * or {@link KeyEvent#KEYCODE_VOLUME_MUTE}. |
| 1134 | * @param stream stream type to adjust volume. |
| 1135 | * @param musicOnly true if both UI nor haptic feedback aren't needed when adjust volume. |
| 1136 | */ |
| 1137 | @Override |
| 1138 | public void dispatchVolumeKeyEvent(KeyEvent keyEvent, int stream, boolean musicOnly) { |
| 1139 | if (keyEvent == null || |
| 1140 | (keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP |
| 1141 | && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN |
| 1142 | && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_MUTE)) { |
| 1143 | Log.w(TAG, "Attempted to dispatch null or non-volume key event."); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | final int pid = Binder.getCallingPid(); |
| 1148 | final int uid = Binder.getCallingUid(); |
| 1149 | final long token = Binder.clearCallingIdentity(); |
| 1150 | |
Jaewan Kim | b2781e7 | 2017-03-02 09:57:09 +0900 | [diff] [blame] | 1151 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1152 | Log.d(TAG, "dispatchVolumeKeyEvent, pid=" + pid + ", uid=" + uid + ", event=" |
| 1153 | + keyEvent); |
| 1154 | } |
| 1155 | |
| 1156 | try { |
| 1157 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1158 | if (isGlobalPriorityActiveLocked() |
| 1159 | || mCurrentFullUserRecord.mOnVolumeKeyLongPressListener == null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1160 | dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly); |
| 1161 | } else { |
| 1162 | // TODO: Consider the case when both volume up and down keys are pressed |
| 1163 | // at the same time. |
| 1164 | if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) { |
| 1165 | if (keyEvent.getRepeatCount() == 0) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1166 | // Keeps the copy of the KeyEvent because it can be reused. |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1167 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = |
| 1168 | KeyEvent.obtain(keyEvent); |
| 1169 | mCurrentFullUserRecord.mInitialDownVolumeStream = stream; |
| 1170 | mCurrentFullUserRecord.mInitialDownMusicOnly = musicOnly; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1171 | mHandler.sendMessageDelayed( |
| 1172 | mHandler.obtainMessage( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1173 | MessageHandler.MSG_VOLUME_INITIAL_DOWN, |
| 1174 | mCurrentFullUserRecord.mFullUserId, 0), |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1175 | mLongPressTimeout); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1176 | } |
| 1177 | if (keyEvent.getRepeatCount() > 0 || keyEvent.isLongPress()) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1178 | mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1179 | if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1180 | dispatchVolumeKeyLongPressLocked( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1181 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1182 | // Mark that the key is already handled. |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1183 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = null; |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1184 | } |
| 1185 | dispatchVolumeKeyLongPressLocked(keyEvent); |
| 1186 | } |
| 1187 | } else { // if up |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1188 | mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1189 | if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null |
| 1190 | && mCurrentFullUserRecord.mInitialDownVolumeKeyEvent |
| 1191 | .getDownTime() == keyEvent.getDownTime()) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1192 | // Short-press. Should change volume. |
| 1193 | dispatchVolumeKeyEventLocked( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1194 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent, |
| 1195 | mCurrentFullUserRecord.mInitialDownVolumeStream, |
| 1196 | mCurrentFullUserRecord.mInitialDownMusicOnly); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1197 | dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly); |
| 1198 | } else { |
| 1199 | dispatchVolumeKeyLongPressLocked(keyEvent); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } finally { |
| 1205 | Binder.restoreCallingIdentity(token); |
| 1206 | } |
| 1207 | } |
| 1208 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1209 | private void dispatchVolumeKeyEventLocked( |
| 1210 | KeyEvent keyEvent, int stream, boolean musicOnly) { |
| 1211 | boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN; |
| 1212 | boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP; |
| 1213 | int direction = 0; |
| 1214 | boolean isMute = false; |
| 1215 | switch (keyEvent.getKeyCode()) { |
| 1216 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1217 | direction = AudioManager.ADJUST_RAISE; |
| 1218 | break; |
| 1219 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1220 | direction = AudioManager.ADJUST_LOWER; |
| 1221 | break; |
| 1222 | case KeyEvent.KEYCODE_VOLUME_MUTE: |
| 1223 | isMute = true; |
| 1224 | break; |
| 1225 | } |
| 1226 | if (down || up) { |
| 1227 | int flags = AudioManager.FLAG_FROM_KEY; |
| 1228 | if (musicOnly) { |
| 1229 | // This flag is used when the screen is off to only affect active media. |
| 1230 | flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY; |
| 1231 | } else { |
| 1232 | // These flags are consistent with the home screen |
| 1233 | if (up) { |
| 1234 | flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE; |
| 1235 | } else { |
| 1236 | flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE; |
| 1237 | } |
| 1238 | } |
| 1239 | if (direction != 0) { |
| 1240 | // If this is action up we want to send a beep for non-music events |
| 1241 | if (up) { |
| 1242 | direction = 0; |
| 1243 | } |
| 1244 | dispatchAdjustVolumeLocked(stream, direction, flags); |
| 1245 | } else if (isMute) { |
| 1246 | if (down && keyEvent.getRepeatCount() == 0) { |
| 1247 | dispatchAdjustVolumeLocked(stream, AudioManager.ADJUST_TOGGLE_MUTE, flags); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | @Override |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1254 | public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) { |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1255 | final long token = Binder.clearCallingIdentity(); |
| 1256 | try { |
| 1257 | synchronized (mLock) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1258 | dispatchAdjustVolumeLocked(suggestedStream, delta, flags); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } finally { |
| 1261 | Binder.restoreCallingIdentity(token); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | @Override |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 1266 | public void setRemoteVolumeController(IRemoteVolumeController rvc) { |
| 1267 | final int pid = Binder.getCallingPid(); |
| 1268 | final int uid = Binder.getCallingUid(); |
| 1269 | final long token = Binder.clearCallingIdentity(); |
| 1270 | try { |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 1271 | enforceSystemUiPermission("listen for volume changes", pid, uid); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 1272 | mRvc = rvc; |
| 1273 | } finally { |
| 1274 | Binder.restoreCallingIdentity(token); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | @Override |
RoboErik | de9ba39 | 2014-09-26 12:51:01 -0700 | [diff] [blame] | 1279 | public boolean isGlobalPriorityActive() { |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 1280 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1281 | return isGlobalPriorityActiveLocked(); |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 1282 | } |
RoboErik | de9ba39 | 2014-09-26 12:51:01 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1286 | public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1287 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1288 | |
| 1289 | pw.println("MEDIA SESSION SERVICE (dumpsys media_session)"); |
| 1290 | pw.println(); |
| 1291 | |
| 1292 | synchronized (mLock) { |
RoboErik | a08adb24 | 2014-11-21 18:28:18 -0800 | [diff] [blame] | 1293 | pw.println(mSessionsListeners.size() + " sessions listeners."); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1294 | pw.println("Global priority session is " + mGlobalPrioritySession); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 1295 | pw.println("User Records:"); |
Jaewan Kim | e0ca3f3 | 2017-02-16 15:52:39 +0900 | [diff] [blame] | 1296 | int count = mUserRecords.size(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1297 | for (int i = 0; i < count; i++) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1298 | mUserRecords.valueAt(i).dumpLocked(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1299 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1300 | mAudioPlaybackMonitor.dump(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1303 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1304 | private int verifySessionsRequest(ComponentName componentName, int userId, final int pid, |
| 1305 | final int uid) { |
| 1306 | String packageName = null; |
| 1307 | if (componentName != null) { |
| 1308 | // If they gave us a component name verify they own the |
| 1309 | // package |
| 1310 | packageName = componentName.getPackageName(); |
| 1311 | enforcePackageName(packageName, uid); |
| 1312 | } |
| 1313 | // Check that they can make calls on behalf of the user and |
| 1314 | // get the final user id |
| 1315 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 1316 | true /* allowAll */, true /* requireFull */, "getSessions", packageName); |
| 1317 | // Check if they have the permissions or their component is |
| 1318 | // enabled for the user they're calling from. |
| 1319 | enforceMediaPermissions(componentName, pid, uid, resolvedUserId); |
| 1320 | return resolvedUserId; |
| 1321 | } |
| 1322 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1323 | private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1324 | MediaSessionRecord session = isGlobalPriorityActiveLocked() ? mGlobalPrioritySession |
| 1325 | : mCurrentFullUserRecord.mPriorityStack.getDefaultVolumeSession(); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1326 | |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1327 | boolean preferSuggestedStream = false; |
| 1328 | if (isValidLocalStreamType(suggestedStream) |
| 1329 | && AudioSystem.isStreamActive(suggestedStream, 0)) { |
| 1330 | preferSuggestedStream = true; |
| 1331 | } |
Jaewan Kim | b2781e7 | 2017-03-02 09:57:09 +0900 | [diff] [blame] | 1332 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 1333 | Log.d(TAG, "Adjusting " + session + " by " + direction + ". flags=" |
| 1334 | + flags + ", suggestedStream=" + suggestedStream |
| 1335 | + ", preferSuggestedStream=" + preferSuggestedStream); |
| 1336 | } |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1337 | if (session == null || preferSuggestedStream) { |
RoboErik | 94c716e | 2014-09-14 13:54:31 -0700 | [diff] [blame] | 1338 | if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0 |
| 1339 | && !AudioSystem.isStreamActive(AudioManager.STREAM_MUSIC, 0)) { |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1340 | if (DEBUG) { |
| 1341 | Log.d(TAG, "No active session to adjust, skipping media only volume event"); |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1342 | } |
RoboErik | b7c014c | 2014-07-22 15:58:22 -0700 | [diff] [blame] | 1343 | return; |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1344 | } |
Shibin George | 19e8404 | 2016-06-14 20:42:13 +0530 | [diff] [blame] | 1345 | |
| 1346 | // Execute mAudioService.adjustSuggestedStreamVolume() on |
| 1347 | // handler thread of MediaSessionService. |
| 1348 | // This will release the MediaSessionService.mLock sooner and avoid |
| 1349 | // a potential deadlock between MediaSessionService.mLock and |
| 1350 | // ActivityManagerService lock. |
| 1351 | mHandler.post(new Runnable() { |
| 1352 | @Override |
| 1353 | public void run() { |
| 1354 | try { |
| 1355 | String packageName = getContext().getOpPackageName(); |
| 1356 | mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream, |
| 1357 | flags, packageName, TAG); |
| 1358 | } catch (RemoteException e) { |
| 1359 | Log.e(TAG, "Error adjusting default volume.", e); |
| 1360 | } |
| 1361 | } |
| 1362 | }); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1363 | } else { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1364 | session.adjustVolume(direction, flags, getContext().getPackageName(), |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1365 | Process.SYSTEM_UID, true); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1369 | private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) { |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1370 | int action = keyEvent.getAction(); |
| 1371 | boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0; |
| 1372 | if (action == KeyEvent.ACTION_DOWN) { |
| 1373 | if (keyEvent.getRepeatCount() == 0) { |
| 1374 | mVoiceButtonDown = true; |
| 1375 | mVoiceButtonHandled = false; |
| 1376 | } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) { |
| 1377 | mVoiceButtonHandled = true; |
| 1378 | startVoiceInput(needWakeLock); |
| 1379 | } |
| 1380 | } else if (action == KeyEvent.ACTION_UP) { |
| 1381 | if (mVoiceButtonDown) { |
| 1382 | mVoiceButtonDown = false; |
| 1383 | if (!mVoiceButtonHandled && !keyEvent.isCanceled()) { |
| 1384 | // Resend the down then send this event through |
| 1385 | KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1386 | dispatchMediaKeyEventLocked(downEvent, needWakeLock); |
| 1387 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1393 | private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1394 | MediaSessionRecord session = mCurrentFullUserRecord.getMediaButtonSessionLocked(); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1395 | if (session != null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1396 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 1397 | Log.d(TAG, "Sending " + keyEvent + " to " + session); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1398 | } |
| 1399 | if (needWakeLock) { |
| 1400 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 1401 | } |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1402 | // If we don't need a wakelock use -1 as the id so we won't release it later. |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1403 | session.sendMediaButton(keyEvent, |
| 1404 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1405 | mKeyEventReceiver, Process.SYSTEM_UID, |
Donghyun Cho | 1ea5683 | 2016-02-23 16:30:07 +0900 | [diff] [blame] | 1406 | getContext().getPackageName()); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1407 | if (mCurrentFullUserRecord.mCallback != null) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1408 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1409 | mCurrentFullUserRecord.mCallback.onMediaKeyEventDispatchedToMediaSession( |
| 1410 | keyEvent, |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1411 | new MediaSession.Token(session.getControllerBinder())); |
| 1412 | } catch (RemoteException e) { |
| 1413 | Log.w(TAG, "Failed to send callback", e); |
| 1414 | } |
| 1415 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1416 | } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null |
| 1417 | || mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) { |
| 1418 | if (needWakeLock) { |
| 1419 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 1420 | } |
| 1421 | Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); |
| 1422 | mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 1423 | mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); |
| 1424 | try { |
| 1425 | if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) { |
| 1426 | PendingIntent receiver = mCurrentFullUserRecord.mLastMediaButtonReceiver; |
| 1427 | if (DEBUG_KEY_EVENT) { |
| 1428 | Log.d(TAG, "Sending " + keyEvent |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1429 | + " to the last known PendingIntent " + receiver); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1430 | } |
| 1431 | receiver.send(getContext(), |
| 1432 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
| 1433 | mediaButtonIntent, mKeyEventReceiver, mHandler); |
| 1434 | if (mCurrentFullUserRecord.mCallback != null) { |
| 1435 | ComponentName componentName = mCurrentFullUserRecord |
| 1436 | .mLastMediaButtonReceiver.getIntent().getComponent(); |
| 1437 | if (componentName != null) { |
| 1438 | mCurrentFullUserRecord.mCallback |
| 1439 | .onMediaKeyEventDispatchedToMediaButtonReceiver( |
| 1440 | keyEvent, componentName); |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1441 | } |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 1442 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1443 | } else { |
| 1444 | ComponentName receiver = |
| 1445 | mCurrentFullUserRecord.mRestoredMediaButtonReceiver; |
| 1446 | if (DEBUG_KEY_EVENT) { |
| 1447 | Log.d(TAG, "Sending " + keyEvent + " to the restored intent " |
| 1448 | + receiver); |
| 1449 | } |
| 1450 | mediaButtonIntent.setComponent(receiver); |
| 1451 | getContext().sendBroadcastAsUser(mediaButtonIntent, |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1452 | UserHandle.of(mCurrentFullUserRecord |
| 1453 | .mRestoredMediaButtonReceiverUserId)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1454 | if (mCurrentFullUserRecord.mCallback != null) { |
| 1455 | mCurrentFullUserRecord.mCallback |
| 1456 | .onMediaKeyEventDispatchedToMediaButtonReceiver( |
| 1457 | keyEvent, receiver); |
| 1458 | } |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1459 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1460 | } catch (CanceledException e) { |
| 1461 | Log.i(TAG, "Error sending key event to media button receiver " |
| 1462 | + mCurrentFullUserRecord.mLastMediaButtonReceiver, e); |
| 1463 | } catch (RemoteException e) { |
| 1464 | Log.w(TAG, "Failed to send callback", e); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1465 | } |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | private void startVoiceInput(boolean needWakeLock) { |
| 1470 | Intent voiceIntent = null; |
| 1471 | // select which type of search to launch: |
| 1472 | // - screen on and device unlocked: action is ACTION_WEB_SEARCH |
| 1473 | // - device locked or screen off: action is |
| 1474 | // ACTION_VOICE_SEARCH_HANDS_FREE |
| 1475 | // with EXTRA_SECURE set to true if the device is securely locked |
| 1476 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
| 1477 | boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 1478 | if (!isLocked && pm.isScreenOn()) { |
| 1479 | voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); |
| 1480 | Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH"); |
| 1481 | } else { |
| 1482 | voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); |
| 1483 | voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, |
| 1484 | isLocked && mKeyguardManager.isKeyguardSecure()); |
| 1485 | Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE"); |
| 1486 | } |
| 1487 | // start the search activity |
| 1488 | if (needWakeLock) { |
| 1489 | mMediaEventWakeLock.acquire(); |
| 1490 | } |
| 1491 | try { |
| 1492 | if (voiceIntent != null) { |
| 1493 | voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 1494 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1495 | if (DEBUG) Log.d(TAG, "voiceIntent: " + voiceIntent); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1496 | getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT); |
| 1497 | } |
| 1498 | } catch (ActivityNotFoundException e) { |
| 1499 | Log.w(TAG, "No activity for search: " + e); |
| 1500 | } finally { |
| 1501 | if (needWakeLock) { |
| 1502 | mMediaEventWakeLock.release(); |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | private boolean isVoiceKey(int keyCode) { |
| 1508 | return keyCode == KeyEvent.KEYCODE_HEADSETHOOK; |
| 1509 | } |
| 1510 | |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 1511 | private boolean isUserSetupComplete() { |
| 1512 | return Settings.Secure.getIntForUser(getContext().getContentResolver(), |
| 1513 | Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0; |
| 1514 | } |
| 1515 | |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1516 | // we only handle public stream types, which are 0-5 |
| 1517 | private boolean isValidLocalStreamType(int streamType) { |
| 1518 | return streamType >= AudioManager.STREAM_VOICE_CALL |
| 1519 | && streamType <= AudioManager.STREAM_NOTIFICATION; |
| 1520 | } |
| 1521 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1522 | private class MediaKeyListenerResultReceiver extends ResultReceiver implements Runnable { |
| 1523 | private KeyEvent mKeyEvent; |
| 1524 | private boolean mNeedWakeLock; |
| 1525 | private boolean mHandled; |
| 1526 | |
| 1527 | private MediaKeyListenerResultReceiver(KeyEvent keyEvent, boolean needWakeLock) { |
| 1528 | super(mHandler); |
| 1529 | mHandler.postDelayed(this, MEDIA_KEY_LISTENER_TIMEOUT); |
| 1530 | mKeyEvent = keyEvent; |
| 1531 | mNeedWakeLock = needWakeLock; |
| 1532 | } |
| 1533 | |
| 1534 | @Override |
| 1535 | public void run() { |
| 1536 | Log.d(TAG, "The media key listener is timed-out for " + mKeyEvent); |
| 1537 | dispatchMediaKeyEvent(); |
| 1538 | } |
| 1539 | |
| 1540 | @Override |
| 1541 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 1542 | if (resultCode == MediaSessionManager.RESULT_MEDIA_KEY_HANDLED) { |
| 1543 | mHandled = true; |
| 1544 | mHandler.removeCallbacks(this); |
| 1545 | return; |
| 1546 | } |
| 1547 | dispatchMediaKeyEvent(); |
| 1548 | } |
| 1549 | |
| 1550 | private void dispatchMediaKeyEvent() { |
| 1551 | if (mHandled) { |
| 1552 | return; |
| 1553 | } |
| 1554 | mHandled = true; |
| 1555 | mHandler.removeCallbacks(this); |
| 1556 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1557 | if (!isGlobalPriorityActiveLocked() |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1558 | && isVoiceKey(mKeyEvent.getKeyCode())) { |
| 1559 | handleVoiceKeyEventLocked(mKeyEvent, mNeedWakeLock); |
| 1560 | } else { |
| 1561 | dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock); |
| 1562 | } |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1567 | private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler); |
| 1568 | |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1569 | class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable, |
| 1570 | PendingIntent.OnFinished { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1571 | private final Handler mHandler; |
| 1572 | private int mRefCount = 0; |
| 1573 | private int mLastTimeoutId = 0; |
| 1574 | |
| 1575 | public KeyEventWakeLockReceiver(Handler handler) { |
| 1576 | super(handler); |
| 1577 | mHandler = handler; |
| 1578 | } |
| 1579 | |
| 1580 | public void onTimeout() { |
| 1581 | synchronized (mLock) { |
| 1582 | if (mRefCount == 0) { |
| 1583 | // We've already released it, so just return |
| 1584 | return; |
| 1585 | } |
| 1586 | mLastTimeoutId++; |
| 1587 | mRefCount = 0; |
| 1588 | releaseWakeLockLocked(); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | public void aquireWakeLockLocked() { |
| 1593 | if (mRefCount == 0) { |
| 1594 | mMediaEventWakeLock.acquire(); |
| 1595 | } |
| 1596 | mRefCount++; |
| 1597 | mHandler.removeCallbacks(this); |
| 1598 | mHandler.postDelayed(this, WAKELOCK_TIMEOUT); |
| 1599 | |
| 1600 | } |
| 1601 | |
| 1602 | @Override |
| 1603 | public void run() { |
| 1604 | onTimeout(); |
| 1605 | } |
| 1606 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1607 | @Override |
| 1608 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1609 | if (resultCode < mLastTimeoutId) { |
| 1610 | // Ignore results from calls that were before the last |
| 1611 | // timeout, just in case. |
| 1612 | return; |
| 1613 | } else { |
| 1614 | synchronized (mLock) { |
| 1615 | if (mRefCount > 0) { |
| 1616 | mRefCount--; |
| 1617 | if (mRefCount == 0) { |
| 1618 | releaseWakeLockLocked(); |
| 1619 | } |
| 1620 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | } |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1624 | |
| 1625 | private void releaseWakeLockLocked() { |
| 1626 | mMediaEventWakeLock.release(); |
| 1627 | mHandler.removeCallbacks(this); |
| 1628 | } |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1629 | |
| 1630 | @Override |
| 1631 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 1632 | String resultData, Bundle resultExtras) { |
| 1633 | onReceiveResult(resultCode, null); |
| 1634 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1635 | }; |
| 1636 | |
| 1637 | BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { |
| 1638 | @Override |
| 1639 | public void onReceive(Context context, Intent intent) { |
| 1640 | if (intent == null) { |
| 1641 | return; |
| 1642 | } |
| 1643 | Bundle extras = intent.getExtras(); |
| 1644 | if (extras == null) { |
| 1645 | return; |
| 1646 | } |
| 1647 | synchronized (mLock) { |
| 1648 | if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED) |
| 1649 | && mMediaEventWakeLock.isHeld()) { |
| 1650 | mMediaEventWakeLock.release(); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | }; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1655 | } |
| 1656 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1657 | final class MessageHandler extends Handler { |
| 1658 | private static final int MSG_SESSIONS_CHANGED = 1; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1659 | private static final int MSG_VOLUME_INITIAL_DOWN = 2; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1660 | private final SparseArray<Integer> mIntegerCache = new SparseArray<>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1661 | |
| 1662 | @Override |
| 1663 | public void handleMessage(Message msg) { |
| 1664 | switch (msg.what) { |
| 1665 | case MSG_SESSIONS_CHANGED: |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1666 | pushSessionsChanged((int) msg.obj); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1667 | break; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1668 | case MSG_VOLUME_INITIAL_DOWN: |
| 1669 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1670 | FullUserRecord user = mUserRecords.get((int) msg.arg1); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1671 | if (user != null && user.mInitialDownVolumeKeyEvent != null) { |
| 1672 | dispatchVolumeKeyLongPressLocked(user.mInitialDownVolumeKeyEvent); |
| 1673 | // Mark that the key is already handled. |
| 1674 | user.mInitialDownVolumeKeyEvent = null; |
| 1675 | } |
| 1676 | } |
| 1677 | break; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1681 | public void postSessionsChanged(int userId) { |
| 1682 | // Use object instead of the arguments when posting message to remove pending requests. |
| 1683 | Integer userIdInteger = mIntegerCache.get(userId); |
| 1684 | if (userIdInteger == null) { |
| 1685 | userIdInteger = Integer.valueOf(userId); |
| 1686 | mIntegerCache.put(userId, userIdInteger); |
| 1687 | } |
| 1688 | removeMessages(MSG_SESSIONS_CHANGED, userIdInteger); |
| 1689 | obtainMessage(MSG_SESSIONS_CHANGED, userIdInteger).sendToTarget(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1692 | } |