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