RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.media; |
| 18 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 19 | import android.Manifest; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 22 | import android.app.KeyguardManager; |
| 23 | import android.content.ActivityNotFoundException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 27 | import android.content.Context; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 30 | import android.media.AudioManager; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 31 | import android.media.IAudioService; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 32 | import android.media.IRemoteVolumeController; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 33 | import android.media.session.IActiveSessionsListener; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 34 | import android.media.session.ISession; |
| 35 | import android.media.session.ISessionCallback; |
| 36 | import android.media.session.ISessionManager; |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 37 | import android.media.session.MediaSession; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 38 | import android.os.Binder; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 39 | import android.os.Bundle; |
RoboErik | 8ae0f34 | 2014-02-24 18:02:08 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 41 | import android.os.IBinder; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 42 | import android.os.Message; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 43 | import android.os.PowerManager; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 44 | import android.os.RemoteException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 45 | import android.os.ResultReceiver; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 46 | import android.os.ServiceManager; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
| 48 | import android.provider.Settings; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 49 | import android.speech.RecognizerIntent; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
| 51 | import android.util.Log; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 52 | import android.util.SparseArray; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 53 | import android.view.KeyEvent; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 54 | |
| 55 | import com.android.server.SystemService; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 56 | import com.android.server.Watchdog; |
| 57 | import com.android.server.Watchdog.Monitor; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 58 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 59 | import java.io.FileDescriptor; |
| 60 | import java.io.PrintWriter; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 61 | import java.util.ArrayList; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 62 | import java.util.List; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * System implementation of MediaSessionManager |
| 66 | */ |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 67 | public class MediaSessionService extends SystemService implements Monitor { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 68 | private static final String TAG = "MediaSessionService"; |
| 69 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 70 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 71 | private static final int WAKELOCK_TIMEOUT = 5000; |
| 72 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 73 | private final SessionManagerImpl mSessionManagerImpl; |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 74 | private final MediaSessionStack mPriorityStack; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 75 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 76 | private final ArrayList<MediaSessionRecord> mAllSessions = new ArrayList<MediaSessionRecord>(); |
| 77 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<UserRecord>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 78 | private final ArrayList<SessionsListenerRecord> mSessionsListeners |
| 79 | = new ArrayList<SessionsListenerRecord>(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 80 | private final Object mLock = new Object(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 81 | private final MessageHandler mHandler = new MessageHandler(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 82 | private final PowerManager.WakeLock mMediaEventWakeLock; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 83 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 84 | private KeyguardManager mKeyguardManager; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 85 | private IAudioService mAudioService; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 86 | private ContentResolver mContentResolver; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 87 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 88 | private MediaSessionRecord mPrioritySession; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 89 | private int mCurrentUserId = -1; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 90 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 91 | // Used to notify system UI when remote volume was changed. TODO find a |
| 92 | // better way to handle this. |
| 93 | private IRemoteVolumeController mRvc; |
| 94 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 95 | public MediaSessionService(Context context) { |
| 96 | super(context); |
| 97 | mSessionManagerImpl = new SessionManagerImpl(); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 98 | mPriorityStack = new MediaSessionStack(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 99 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 100 | mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent"); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onStart() { |
| 105 | publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 106 | Watchdog.getInstance().addMonitor(this); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 107 | updateUser(); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 108 | mKeyguardManager = |
| 109 | (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 110 | mAudioService = getAudioService(); |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 111 | mContentResolver = getContext().getContentResolver(); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private IAudioService getAudioService() { |
| 115 | IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); |
| 116 | return IAudioService.Stub.asInterface(b); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 117 | } |
| 118 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 119 | public void updateSession(MediaSessionRecord record) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 120 | synchronized (mLock) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 121 | if (!mAllSessions.contains(record)) { |
| 122 | Log.d(TAG, "Unknown session updated. Ignoring."); |
| 123 | return; |
| 124 | } |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 125 | mPriorityStack.onSessionStateChange(record); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 126 | if (record.isSystemPriority()) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 127 | if (record.isActive()) { |
| 128 | if (mPrioritySession != null) { |
| 129 | Log.w(TAG, "Replacing existing priority session with a new session"); |
| 130 | } |
| 131 | mPrioritySession = record; |
| 132 | } else { |
| 133 | if (mPrioritySession == record) { |
| 134 | mPrioritySession = null; |
| 135 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 136 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 139 | mHandler.post(MessageHandler.MSG_SESSIONS_CHANGED, record.getUserId(), 0); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 140 | } |
| 141 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 142 | public void onSessionPlaystateChange(MediaSessionRecord record, int oldState, int newState) { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 143 | boolean updateSessions = false; |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 144 | synchronized (mLock) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 145 | if (!mAllSessions.contains(record)) { |
| 146 | Log.d(TAG, "Unknown session changed playback state. Ignoring."); |
| 147 | return; |
| 148 | } |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 149 | updateSessions = mPriorityStack.onPlaystateChange(record, oldState, newState); |
| 150 | } |
| 151 | if (updateSessions) { |
| 152 | mHandler.post(MessageHandler.MSG_SESSIONS_CHANGED, record.getUserId(), 0); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 156 | public void onSessionPlaybackTypeChanged(MediaSessionRecord record) { |
| 157 | synchronized (mLock) { |
| 158 | if (!mAllSessions.contains(record)) { |
| 159 | Log.d(TAG, "Unknown session changed playback type. Ignoring."); |
| 160 | return; |
| 161 | } |
| 162 | pushRemoteVolumeUpdateLocked(record.getUserId()); |
| 163 | } |
| 164 | } |
| 165 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 166 | @Override |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 167 | public void onStartUser(int userHandle) { |
| 168 | updateUser(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void onSwitchUser(int userHandle) { |
| 173 | updateUser(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onStopUser(int userHandle) { |
| 178 | synchronized (mLock) { |
| 179 | UserRecord user = mUserRecords.get(userHandle); |
| 180 | if (user != null) { |
| 181 | destroyUserLocked(user); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 187 | public void monitor() { |
| 188 | synchronized (mLock) { |
| 189 | // Check for deadlock |
| 190 | } |
| 191 | } |
| 192 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 193 | protected void enforcePhoneStatePermission(int pid, int uid) { |
| 194 | if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid) |
| 195 | != PackageManager.PERMISSION_GRANTED) { |
| 196 | throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission."); |
| 197 | } |
| 198 | } |
| 199 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 200 | void sessionDied(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 201 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 202 | destroySessionLocked(session); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void destroySession(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 207 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 208 | destroySessionLocked(session); |
| 209 | } |
| 210 | } |
| 211 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 212 | private void updateUser() { |
| 213 | synchronized (mLock) { |
| 214 | int userId = ActivityManager.getCurrentUser(); |
| 215 | if (mCurrentUserId != userId) { |
| 216 | final int oldUserId = mCurrentUserId; |
| 217 | mCurrentUserId = userId; // do this first |
| 218 | |
| 219 | UserRecord oldUser = mUserRecords.get(oldUserId); |
| 220 | if (oldUser != null) { |
| 221 | oldUser.stopLocked(); |
| 222 | } |
| 223 | |
| 224 | UserRecord newUser = getOrCreateUser(userId); |
| 225 | newUser.startLocked(); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Stop the user and unbind from everything. |
| 232 | * |
| 233 | * @param user The user to dispose of |
| 234 | */ |
| 235 | private void destroyUserLocked(UserRecord user) { |
| 236 | user.stopLocked(); |
| 237 | user.destroyLocked(); |
| 238 | mUserRecords.remove(user.mUserId); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * When a session is removed several things need to happen. |
| 243 | * 1. We need to remove it from the relevant user. |
| 244 | * 2. We need to remove it from the priority stack. |
| 245 | * 3. We need to remove it from all sessions. |
| 246 | * 4. If this is the system priority session we need to clear it. |
| 247 | * 5. We need to unlink to death from the cb binder |
| 248 | * 6. We need to tell the session to do any final cleanup (onDestroy) |
| 249 | */ |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 250 | private void destroySessionLocked(MediaSessionRecord session) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 251 | int userId = session.getUserId(); |
| 252 | UserRecord user = mUserRecords.get(userId); |
| 253 | if (user != null) { |
| 254 | user.removeSessionLocked(session); |
| 255 | } |
| 256 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 257 | mPriorityStack.removeSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 258 | mAllSessions.remove(session); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 259 | if (session == mPrioritySession) { |
| 260 | mPrioritySession = null; |
| 261 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 262 | |
| 263 | try { |
| 264 | session.getCallback().asBinder().unlinkToDeath(session, 0); |
| 265 | } catch (Exception e) { |
| 266 | // ignore exceptions while destroying a session. |
| 267 | } |
| 268 | session.onDestroy(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 269 | |
| 270 | mHandler.post(MessageHandler.MSG_SESSIONS_CHANGED, session.getUserId(), 0); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | private void enforcePackageName(String packageName, int uid) { |
| 274 | if (TextUtils.isEmpty(packageName)) { |
| 275 | throw new IllegalArgumentException("packageName may not be empty"); |
| 276 | } |
| 277 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 278 | final int packageCount = packages.length; |
| 279 | for (int i = 0; i < packageCount; i++) { |
| 280 | if (packageName.equals(packages[i])) { |
| 281 | return; |
| 282 | } |
| 283 | } |
| 284 | throw new IllegalArgumentException("packageName is not owned by the calling process"); |
| 285 | } |
| 286 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Checks a caller's authorization to register an IRemoteControlDisplay. |
| 289 | * Authorization is granted if one of the following is true: |
| 290 | * <ul> |
| 291 | * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL |
| 292 | * permission</li> |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 293 | * <li>the caller's listener is one of the enabled notification listeners |
| 294 | * for the caller's user</li> |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 295 | * </ul> |
| 296 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 297 | private void enforceMediaPermissions(ComponentName compName, int pid, int uid, |
| 298 | int resolvedUserId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 299 | if (getContext() |
| 300 | .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid) |
| 301 | != PackageManager.PERMISSION_GRANTED |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 302 | && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid), |
| 303 | resolvedUserId)) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 304 | throw new SecurityException("Missing permission to control media."); |
| 305 | } |
| 306 | } |
| 307 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 308 | private void enforceStatusBarPermission(String action, int pid, int uid) { |
| 309 | if (getContext().checkPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 310 | pid, uid) != PackageManager.PERMISSION_GRANTED) { |
| 311 | throw new SecurityException("Only system ui may " + action); |
| 312 | } |
| 313 | } |
| 314 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 315 | /** |
| 316 | * This checks if the component is an enabled notification listener for the |
| 317 | * specified user. Enabled components may only operate on behalf of the user |
| 318 | * they're running as. |
| 319 | * |
| 320 | * @param compName The component that is enabled. |
| 321 | * @param userId The user id of the caller. |
| 322 | * @param forUserId The user id they're making the request on behalf of. |
| 323 | * @return True if the component is enabled, false otherwise |
| 324 | */ |
| 325 | private boolean isEnabledNotificationListener(ComponentName compName, int userId, |
| 326 | int forUserId) { |
| 327 | if (userId != forUserId) { |
| 328 | // You may not access another user's content as an enabled listener. |
| 329 | return false; |
| 330 | } |
RoboErik | 51fa6bc | 2014-06-20 14:59:58 -0700 | [diff] [blame] | 331 | if (DEBUG) { |
| 332 | Log.d(TAG, "Checking if enabled notification listener " + compName); |
| 333 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 334 | if (compName != null) { |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 335 | final String enabledNotifListeners = Settings.Secure.getStringForUser(mContentResolver, |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 336 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 337 | userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 338 | if (enabledNotifListeners != null) { |
| 339 | final String[] components = enabledNotifListeners.split(":"); |
| 340 | for (int i = 0; i < components.length; i++) { |
| 341 | final ComponentName component = |
| 342 | ComponentName.unflattenFromString(components[i]); |
| 343 | if (component != null) { |
| 344 | if (compName.equals(component)) { |
| 345 | if (DEBUG) { |
| 346 | Log.d(TAG, "ok to get sessions: " + component + |
| 347 | " is authorized notification listener"); |
| 348 | } |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | if (DEBUG) { |
| 355 | Log.d(TAG, "not ok to get sessions, " + compName + |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 356 | " is not in list of ENABLED_NOTIFICATION_LISTENERS for user " + userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 362 | private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId, |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 363 | String callerPackageName, ISessionCallback cb, String tag) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 364 | synchronized (mLock) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 365 | return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 369 | /* |
| 370 | * When a session is created the following things need to happen. |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 371 | * 1. Its callback binder needs a link to death |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 372 | * 2. It needs to be added to all sessions. |
| 373 | * 3. It needs to be added to the priority stack. |
| 374 | * 4. It needs to be added to the relevant user record. |
| 375 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 376 | private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId, |
| 377 | String callerPackageName, ISessionCallback cb, String tag) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 378 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 379 | final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId, |
| 380 | callerPackageName, cb, tag, this, mHandler); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 381 | try { |
| 382 | cb.asBinder().linkToDeath(session, 0); |
| 383 | } catch (RemoteException e) { |
| 384 | throw new RuntimeException("Media Session owner died prematurely.", e); |
| 385 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 386 | |
| 387 | mAllSessions.add(session); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 388 | mPriorityStack.addSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 389 | |
| 390 | UserRecord user = getOrCreateUser(userId); |
| 391 | user.addSessionLocked(session); |
| 392 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 393 | mHandler.post(MessageHandler.MSG_SESSIONS_CHANGED, userId, 0); |
| 394 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 395 | if (DEBUG) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 396 | Log.d(TAG, "Created session for package " + callerPackageName + " with tag " + tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 397 | } |
| 398 | return session; |
| 399 | } |
| 400 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 401 | private UserRecord getOrCreateUser(int userId) { |
| 402 | UserRecord user = mUserRecords.get(userId); |
| 403 | if (user == null) { |
| 404 | user = new UserRecord(getContext(), userId); |
| 405 | mUserRecords.put(userId, user); |
| 406 | } |
| 407 | return user; |
| 408 | } |
| 409 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 410 | private int findIndexOfSessionForIdLocked(String sessionId) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 411 | for (int i = mAllSessions.size() - 1; i >= 0; i--) { |
| 412 | MediaSessionRecord session = mAllSessions.get(i); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 413 | if (TextUtils.equals(session.getSessionInfo().getId(), sessionId)) { |
| 414 | return i; |
| 415 | } |
| 416 | } |
| 417 | return -1; |
| 418 | } |
| 419 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 420 | private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) { |
| 421 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 422 | if (mSessionsListeners.get(i).mListener == listener) { |
| 423 | return i; |
| 424 | } |
| 425 | } |
| 426 | return -1; |
| 427 | } |
| 428 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 429 | private boolean isSessionDiscoverable(MediaSessionRecord record) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 430 | // TODO probably want to check more than if it's active. |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 431 | return record.isActive(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 432 | } |
| 433 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 434 | private void pushSessionsChanged(int userId) { |
| 435 | synchronized (mLock) { |
| 436 | List<MediaSessionRecord> records = mPriorityStack.getActiveSessions(userId); |
| 437 | int size = records.size(); |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 438 | if (size > 0) { |
| 439 | persistMediaButtonReceiverLocked(records.get(0)); |
| 440 | } |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 441 | ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 442 | for (int i = 0; i < size; i++) { |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 443 | tokens.add(new MediaSession.Token(records.get(i).getControllerBinder())); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 444 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 445 | pushRemoteVolumeUpdateLocked(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 446 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 447 | SessionsListenerRecord record = mSessionsListeners.get(i); |
| 448 | if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) { |
| 449 | try { |
| 450 | record.mListener.onActiveSessionsChanged(tokens); |
| 451 | } catch (RemoteException e) { |
| 452 | Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing", |
| 453 | e); |
| 454 | mSessionsListeners.remove(i); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 461 | private void pushRemoteVolumeUpdateLocked(int userId) { |
| 462 | if (mRvc != null) { |
| 463 | try { |
| 464 | MediaSessionRecord record = mPriorityStack.getDefaultRemoteSession(userId); |
| 465 | mRvc.updateRemoteController(record == null ? null : record.getControllerBinder()); |
| 466 | } catch (RemoteException e) { |
| 467 | Log.wtf(TAG, "Error sending default remote volume to sys ui.", e); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 472 | private void persistMediaButtonReceiverLocked(MediaSessionRecord record) { |
| 473 | ComponentName receiver = record.getMediaButtonReceiver(); |
| 474 | if (receiver != null) { |
| 475 | Settings.System.putStringForUser(mContentResolver, |
| 476 | Settings.System.MEDIA_BUTTON_RECEIVER, |
| 477 | receiver == null ? "" : receiver.flattenToString(), |
| 478 | UserHandle.USER_CURRENT); |
| 479 | } |
| 480 | } |
| 481 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Information about a particular user. The contents of this object is |
| 484 | * guarded by mLock. |
| 485 | */ |
| 486 | final class UserRecord { |
| 487 | private final int mUserId; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 488 | private final ArrayList<MediaSessionRecord> mSessions = new ArrayList<MediaSessionRecord>(); |
| 489 | |
| 490 | public UserRecord(Context context, int userId) { |
| 491 | mUserId = userId; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | public void startLocked() { |
Jeff Brown | 01a500e | 2014-07-10 22:50:50 -0700 | [diff] [blame] | 495 | // nothing for now |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | public void stopLocked() { |
Jeff Brown | 01a500e | 2014-07-10 22:50:50 -0700 | [diff] [blame] | 499 | // nothing for now |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | public void destroyLocked() { |
| 503 | for (int i = mSessions.size() - 1; i >= 0; i--) { |
| 504 | MediaSessionRecord session = mSessions.get(i); |
| 505 | MediaSessionService.this.destroySessionLocked(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 509 | public ArrayList<MediaSessionRecord> getSessionsLocked() { |
| 510 | return mSessions; |
| 511 | } |
| 512 | |
| 513 | public void addSessionLocked(MediaSessionRecord session) { |
| 514 | mSessions.add(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | public void removeSessionLocked(MediaSessionRecord session) { |
| 518 | mSessions.remove(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | public void dumpLocked(PrintWriter pw, String prefix) { |
| 522 | pw.println(prefix + "Record for user " + mUserId); |
| 523 | String indent = prefix + " "; |
Jeff Brown | 01a500e | 2014-07-10 22:50:50 -0700 | [diff] [blame] | 524 | int size = mSessions.size(); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 525 | pw.println(indent + size + " Sessions:"); |
| 526 | for (int i = 0; i < size; i++) { |
| 527 | // Just print the session info, the full session dump will |
| 528 | // already be in the list of all sessions. |
| 529 | pw.println(indent + mSessions.get(i).getSessionInfo()); |
| 530 | } |
| 531 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 532 | } |
| 533 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 534 | final class SessionsListenerRecord implements IBinder.DeathRecipient { |
| 535 | private final IActiveSessionsListener mListener; |
| 536 | private final int mUserId; |
| 537 | |
| 538 | public SessionsListenerRecord(IActiveSessionsListener listener, int userId) { |
| 539 | mListener = listener; |
| 540 | mUserId = userId; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void binderDied() { |
| 545 | synchronized (mLock) { |
| 546 | mSessionsListeners.remove(this); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 551 | class SessionManagerImpl extends ISessionManager.Stub { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 552 | private static final String EXTRA_WAKELOCK_ACQUIRED = |
| 553 | "android.media.AudioService.WAKELOCK_ACQUIRED"; |
| 554 | private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number |
| 555 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 556 | private boolean mVoiceButtonDown = false; |
| 557 | private boolean mVoiceButtonHandled = false; |
| 558 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 559 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 560 | public ISession createSession(String packageName, ISessionCallback cb, String tag, |
| 561 | int userId) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 562 | final int pid = Binder.getCallingPid(); |
| 563 | final int uid = Binder.getCallingUid(); |
| 564 | final long token = Binder.clearCallingIdentity(); |
| 565 | try { |
| 566 | enforcePackageName(packageName, uid); |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 567 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 568 | false /* allowAll */, true /* requireFull */, "createSession", packageName); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 569 | if (cb == null) { |
| 570 | throw new IllegalArgumentException("Controller callback cannot be null"); |
| 571 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 572 | return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag) |
| 573 | .getSessionBinder(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 574 | } finally { |
| 575 | Binder.restoreCallingIdentity(token); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 580 | public List<IBinder> getSessions(ComponentName componentName, int userId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 581 | final int pid = Binder.getCallingPid(); |
| 582 | final int uid = Binder.getCallingUid(); |
| 583 | final long token = Binder.clearCallingIdentity(); |
| 584 | |
| 585 | try { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 586 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 587 | ArrayList<IBinder> binders = new ArrayList<IBinder>(); |
| 588 | synchronized (mLock) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 589 | ArrayList<MediaSessionRecord> records = mPriorityStack |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 590 | .getActiveSessions(resolvedUserId); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 591 | int size = records.size(); |
| 592 | for (int i = 0; i < size; i++) { |
| 593 | binders.add(records.get(i).getControllerBinder().asBinder()); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | return binders; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 597 | } finally { |
| 598 | Binder.restoreCallingIdentity(token); |
| 599 | } |
| 600 | } |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 601 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 602 | @Override |
| 603 | public void addSessionsListener(IActiveSessionsListener listener, |
| 604 | ComponentName componentName, int userId) throws RemoteException { |
| 605 | final int pid = Binder.getCallingPid(); |
| 606 | final int uid = Binder.getCallingUid(); |
| 607 | final long token = Binder.clearCallingIdentity(); |
| 608 | |
| 609 | try { |
| 610 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
| 611 | synchronized (mLock) { |
| 612 | int index = findIndexOfSessionsListenerLocked(listener); |
| 613 | if (index != -1) { |
| 614 | Log.w(TAG, "ActiveSessionsListener is already added, ignoring"); |
| 615 | return; |
| 616 | } |
| 617 | SessionsListenerRecord record = new SessionsListenerRecord(listener, |
| 618 | resolvedUserId); |
| 619 | try { |
| 620 | listener.asBinder().linkToDeath(record, 0); |
| 621 | } catch (RemoteException e) { |
| 622 | Log.e(TAG, "ActiveSessionsListener is dead, ignoring it", e); |
| 623 | return; |
| 624 | } |
| 625 | mSessionsListeners.add(record); |
| 626 | } |
| 627 | } finally { |
| 628 | Binder.restoreCallingIdentity(token); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public void removeSessionsListener(IActiveSessionsListener listener) |
| 634 | throws RemoteException { |
| 635 | synchronized (mLock) { |
| 636 | int index = findIndexOfSessionsListenerLocked(listener); |
| 637 | if (index != -1) { |
| 638 | SessionsListenerRecord record = mSessionsListeners.remove(index); |
| 639 | try { |
| 640 | record.mListener.asBinder().unlinkToDeath(record, 0); |
| 641 | } catch (Exception e) { |
| 642 | // ignore exceptions, the record is being removed |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 648 | /** |
| 649 | * Handles the dispatching of the media button events to one of the |
| 650 | * registered listeners, or if there was none, broadcast an |
| 651 | * ACTION_MEDIA_BUTTON intent to the rest of the system. |
| 652 | * |
| 653 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 654 | * supported media buttons |
| 655 | * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held |
| 656 | * while this key event is dispatched. |
| 657 | */ |
| 658 | @Override |
| 659 | public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { |
| 660 | if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) { |
| 661 | Log.w(TAG, "Attempted to dispatch null or non-media key event."); |
| 662 | return; |
| 663 | } |
| 664 | final int pid = Binder.getCallingPid(); |
| 665 | final int uid = Binder.getCallingUid(); |
| 666 | final long token = Binder.clearCallingIdentity(); |
| 667 | |
| 668 | try { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 669 | synchronized (mLock) { |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 670 | MediaSessionRecord session = mPriorityStack |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 671 | .getDefaultMediaButtonSession(mCurrentUserId); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 672 | if (isVoiceKey(keyEvent.getKeyCode())) { |
| 673 | handleVoiceKeyEventLocked(keyEvent, needWakeLock, session); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 674 | } else { |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 675 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } finally { |
| 679 | Binder.restoreCallingIdentity(token); |
| 680 | } |
| 681 | } |
| 682 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 683 | @Override |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 684 | public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 685 | throws RemoteException { |
| 686 | final int pid = Binder.getCallingPid(); |
| 687 | final int uid = Binder.getCallingUid(); |
| 688 | final long token = Binder.clearCallingIdentity(); |
| 689 | try { |
| 690 | synchronized (mLock) { |
| 691 | MediaSessionRecord session = mPriorityStack |
| 692 | .getDefaultVolumeSession(mCurrentUserId); |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 693 | dispatchAdjustVolumeLocked(suggestedStream, delta, flags, session); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 694 | } |
| 695 | } finally { |
| 696 | Binder.restoreCallingIdentity(token); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | @Override |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 701 | public void setRemoteVolumeController(IRemoteVolumeController rvc) { |
| 702 | final int pid = Binder.getCallingPid(); |
| 703 | final int uid = Binder.getCallingUid(); |
| 704 | final long token = Binder.clearCallingIdentity(); |
| 705 | try { |
| 706 | enforceStatusBarPermission("listen for volume changes", pid, uid); |
| 707 | mRvc = rvc; |
| 708 | } finally { |
| 709 | Binder.restoreCallingIdentity(token); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 714 | public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) { |
| 715 | if (getContext().checkCallingOrSelfPermission(Manifest.permission.DUMP) |
| 716 | != PackageManager.PERMISSION_GRANTED) { |
| 717 | pw.println("Permission Denial: can't dump MediaSessionService from from pid=" |
| 718 | + Binder.getCallingPid() |
| 719 | + ", uid=" + Binder.getCallingUid()); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | pw.println("MEDIA SESSION SERVICE (dumpsys media_session)"); |
| 724 | pw.println(); |
| 725 | |
| 726 | synchronized (mLock) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 727 | pw.println("Session for calls:" + mPrioritySession); |
| 728 | if (mPrioritySession != null) { |
| 729 | mPrioritySession.dump(pw, ""); |
| 730 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 731 | int count = mAllSessions.size(); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 732 | pw.println(count + " Sessions:"); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 733 | for (int i = 0; i < count; i++) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 734 | mAllSessions.get(i).dump(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 735 | pw.println(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 736 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 737 | mPriorityStack.dump(pw, ""); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 738 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 739 | pw.println("User Records:"); |
| 740 | count = mUserRecords.size(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 741 | for (int i = 0; i < count; i++) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 742 | UserRecord user = mUserRecords.get(i); |
| 743 | user.dumpLocked(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 747 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 748 | private int verifySessionsRequest(ComponentName componentName, int userId, final int pid, |
| 749 | final int uid) { |
| 750 | String packageName = null; |
| 751 | if (componentName != null) { |
| 752 | // If they gave us a component name verify they own the |
| 753 | // package |
| 754 | packageName = componentName.getPackageName(); |
| 755 | enforcePackageName(packageName, uid); |
| 756 | } |
| 757 | // Check that they can make calls on behalf of the user and |
| 758 | // get the final user id |
| 759 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 760 | true /* allowAll */, true /* requireFull */, "getSessions", packageName); |
| 761 | // Check if they have the permissions or their component is |
| 762 | // enabled for the user they're calling from. |
| 763 | enforceMediaPermissions(componentName, pid, uid, resolvedUserId); |
| 764 | return resolvedUserId; |
| 765 | } |
| 766 | |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 767 | private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags, |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 768 | MediaSessionRecord session) { |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 769 | if (DEBUG) { |
| 770 | String sessionInfo = session == null ? null : session.getSessionInfo().toString(); |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 771 | Log.d(TAG, "Adjusting session " + sessionInfo + " by " + direction + ". flags=" + flags |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 772 | + ", suggestedStream=" + suggestedStream); |
| 773 | |
| 774 | } |
| 775 | if (session == null) { |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 776 | if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0) { |
| 777 | if (DEBUG) { |
| 778 | Log.d(TAG, "No active session to adjust, skipping media only volume event"); |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 779 | } |
RoboErik | b7c014c | 2014-07-22 15:58:22 -0700 | [diff] [blame^] | 780 | return; |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 781 | } |
RoboErik | 0791e17 | 2014-06-08 10:52:32 -0700 | [diff] [blame] | 782 | try { |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 783 | mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream, flags, |
| 784 | getContext().getOpPackageName()); |
RoboErik | 0791e17 | 2014-06-08 10:52:32 -0700 | [diff] [blame] | 785 | } catch (RemoteException e) { |
| 786 | Log.e(TAG, "Error adjusting default volume.", e); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 787 | } |
| 788 | } else { |
RoboErik | 1ff5b16 | 2014-07-15 17:23:18 -0700 | [diff] [blame] | 789 | session.adjustVolume(direction, flags); |
RoboErik | 851d2d5 | 2014-06-27 18:02:40 -0700 | [diff] [blame] | 790 | if (session.getPlaybackType() == MediaSession.PLAYBACK_TYPE_REMOTE |
| 791 | && mRvc != null) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 792 | try { |
| 793 | mRvc.remoteVolumeChanged(session.getControllerBinder(), flags); |
| 794 | } catch (Exception e) { |
| 795 | Log.wtf(TAG, "Error sending volume change to system UI.", e); |
| 796 | } |
| 797 | } |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 801 | private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock, |
| 802 | MediaSessionRecord session) { |
| 803 | if (session != null && session.hasFlag(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY)) { |
| 804 | // If the phone app has priority just give it the event |
| 805 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); |
| 806 | return; |
| 807 | } |
| 808 | int action = keyEvent.getAction(); |
| 809 | boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0; |
| 810 | if (action == KeyEvent.ACTION_DOWN) { |
| 811 | if (keyEvent.getRepeatCount() == 0) { |
| 812 | mVoiceButtonDown = true; |
| 813 | mVoiceButtonHandled = false; |
| 814 | } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) { |
| 815 | mVoiceButtonHandled = true; |
| 816 | startVoiceInput(needWakeLock); |
| 817 | } |
| 818 | } else if (action == KeyEvent.ACTION_UP) { |
| 819 | if (mVoiceButtonDown) { |
| 820 | mVoiceButtonDown = false; |
| 821 | if (!mVoiceButtonHandled && !keyEvent.isCanceled()) { |
| 822 | // Resend the down then send this event through |
| 823 | KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN); |
| 824 | dispatchMediaKeyEventLocked(downEvent, needWakeLock, session); |
| 825 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock, |
| 832 | MediaSessionRecord session) { |
| 833 | if (session != null) { |
| 834 | if (DEBUG) { |
| 835 | Log.d(TAG, "Sending media key to " + session.getSessionInfo()); |
| 836 | } |
| 837 | if (needWakeLock) { |
| 838 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 839 | } |
| 840 | // If we don't need a wakelock use -1 as the id so we |
| 841 | // won't release it later |
| 842 | session.sendMediaButton(keyEvent, |
| 843 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
| 844 | mKeyEventReceiver); |
| 845 | } else { |
| 846 | if (needWakeLock) { |
| 847 | mMediaEventWakeLock.acquire(); |
| 848 | } |
| 849 | if (DEBUG) { |
| 850 | Log.d(TAG, "Sending media key ordered broadcast"); |
| 851 | } |
| 852 | // Fallback to legacy behavior |
| 853 | Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); |
| 854 | keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); |
| 855 | if (needWakeLock) { |
| 856 | keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, |
| 857 | WAKELOCK_RELEASE_ON_FINISHED); |
| 858 | } |
| 859 | getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, |
| 860 | null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | private void startVoiceInput(boolean needWakeLock) { |
| 865 | Intent voiceIntent = null; |
| 866 | // select which type of search to launch: |
| 867 | // - screen on and device unlocked: action is ACTION_WEB_SEARCH |
| 868 | // - device locked or screen off: action is |
| 869 | // ACTION_VOICE_SEARCH_HANDS_FREE |
| 870 | // with EXTRA_SECURE set to true if the device is securely locked |
| 871 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
| 872 | boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 873 | if (!isLocked && pm.isScreenOn()) { |
| 874 | voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); |
| 875 | Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH"); |
| 876 | } else { |
| 877 | voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); |
| 878 | voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, |
| 879 | isLocked && mKeyguardManager.isKeyguardSecure()); |
| 880 | Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE"); |
| 881 | } |
| 882 | // start the search activity |
| 883 | if (needWakeLock) { |
| 884 | mMediaEventWakeLock.acquire(); |
| 885 | } |
| 886 | try { |
| 887 | if (voiceIntent != null) { |
| 888 | voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 889 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 890 | getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT); |
| 891 | } |
| 892 | } catch (ActivityNotFoundException e) { |
| 893 | Log.w(TAG, "No activity for search: " + e); |
| 894 | } finally { |
| 895 | if (needWakeLock) { |
| 896 | mMediaEventWakeLock.release(); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | private boolean isVoiceKey(int keyCode) { |
| 902 | return keyCode == KeyEvent.KEYCODE_HEADSETHOOK; |
| 903 | } |
| 904 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 905 | private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler); |
| 906 | |
| 907 | class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable { |
| 908 | private final Handler mHandler; |
| 909 | private int mRefCount = 0; |
| 910 | private int mLastTimeoutId = 0; |
| 911 | |
| 912 | public KeyEventWakeLockReceiver(Handler handler) { |
| 913 | super(handler); |
| 914 | mHandler = handler; |
| 915 | } |
| 916 | |
| 917 | public void onTimeout() { |
| 918 | synchronized (mLock) { |
| 919 | if (mRefCount == 0) { |
| 920 | // We've already released it, so just return |
| 921 | return; |
| 922 | } |
| 923 | mLastTimeoutId++; |
| 924 | mRefCount = 0; |
| 925 | releaseWakeLockLocked(); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | public void aquireWakeLockLocked() { |
| 930 | if (mRefCount == 0) { |
| 931 | mMediaEventWakeLock.acquire(); |
| 932 | } |
| 933 | mRefCount++; |
| 934 | mHandler.removeCallbacks(this); |
| 935 | mHandler.postDelayed(this, WAKELOCK_TIMEOUT); |
| 936 | |
| 937 | } |
| 938 | |
| 939 | @Override |
| 940 | public void run() { |
| 941 | onTimeout(); |
| 942 | } |
| 943 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 944 | @Override |
| 945 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 946 | if (resultCode < mLastTimeoutId) { |
| 947 | // Ignore results from calls that were before the last |
| 948 | // timeout, just in case. |
| 949 | return; |
| 950 | } else { |
| 951 | synchronized (mLock) { |
| 952 | if (mRefCount > 0) { |
| 953 | mRefCount--; |
| 954 | if (mRefCount == 0) { |
| 955 | releaseWakeLockLocked(); |
| 956 | } |
| 957 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 961 | |
| 962 | private void releaseWakeLockLocked() { |
| 963 | mMediaEventWakeLock.release(); |
| 964 | mHandler.removeCallbacks(this); |
| 965 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 966 | }; |
| 967 | |
| 968 | BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { |
| 969 | @Override |
| 970 | public void onReceive(Context context, Intent intent) { |
| 971 | if (intent == null) { |
| 972 | return; |
| 973 | } |
| 974 | Bundle extras = intent.getExtras(); |
| 975 | if (extras == null) { |
| 976 | return; |
| 977 | } |
| 978 | synchronized (mLock) { |
| 979 | if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED) |
| 980 | && mMediaEventWakeLock.isHeld()) { |
| 981 | mMediaEventWakeLock.release(); |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | }; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 986 | } |
| 987 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 988 | final class MessageHandler extends Handler { |
| 989 | private static final int MSG_SESSIONS_CHANGED = 1; |
| 990 | |
| 991 | @Override |
| 992 | public void handleMessage(Message msg) { |
| 993 | switch (msg.what) { |
| 994 | case MSG_SESSIONS_CHANGED: |
| 995 | pushSessionsChanged(msg.arg1); |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | public void post(int what, int arg1, int arg2) { |
| 1001 | obtainMessage(what, arg1, arg2).sendToTarget(); |
| 1002 | } |
| 1003 | } |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1004 | } |