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 | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 24 | import android.content.Context; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 27 | import android.media.routeprovider.RouteRequest; |
| 28 | import android.media.session.ISession; |
| 29 | import android.media.session.ISessionCallback; |
| 30 | import android.media.session.ISessionManager; |
| 31 | import android.media.session.RouteInfo; |
| 32 | import android.media.session.RouteOptions; |
RoboErik | 42ea7ee | 2014-05-16 16:27:35 -0700 | [diff] [blame] | 33 | import android.media.session.MediaSession; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 34 | import android.os.Binder; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
RoboErik | 8ae0f34 | 2014-02-24 18:02:08 -0800 | [diff] [blame] | 36 | import android.os.Handler; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 37 | import android.os.IBinder; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 38 | import android.os.PowerManager; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 39 | import android.os.RemoteException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 40 | import android.os.ResultReceiver; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
| 42 | import android.provider.Settings; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 43 | import android.text.TextUtils; |
| 44 | import android.util.Log; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 45 | import android.util.SparseArray; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 46 | import android.view.KeyEvent; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 47 | |
| 48 | import com.android.server.SystemService; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 49 | import com.android.server.Watchdog; |
| 50 | import com.android.server.Watchdog.Monitor; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 51 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 52 | import java.io.FileDescriptor; |
| 53 | import java.io.PrintWriter; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 54 | import java.util.ArrayList; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 55 | import java.util.List; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * System implementation of MediaSessionManager |
| 59 | */ |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 60 | public class MediaSessionService extends SystemService implements Monitor { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 61 | private static final String TAG = "MediaSessionService"; |
| 62 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 63 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 64 | private static final int WAKELOCK_TIMEOUT = 5000; |
| 65 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 66 | private final SessionManagerImpl mSessionManagerImpl; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 67 | // private final MediaRouteProviderWatcher mRouteProviderWatcher; |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 68 | private final MediaSessionStack mPriorityStack; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 69 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 70 | private final ArrayList<MediaSessionRecord> mAllSessions = new ArrayList<MediaSessionRecord>(); |
| 71 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<UserRecord>(); |
| 72 | // private final ArrayList<MediaRouteProviderProxy> mProviders |
| 73 | // = new ArrayList<MediaRouteProviderProxy>(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 74 | private final Object mLock = new Object(); |
RoboErik | 8ae0f34 | 2014-02-24 18:02:08 -0800 | [diff] [blame] | 75 | private final Handler mHandler = new Handler(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 76 | private final PowerManager.WakeLock mMediaEventWakeLock; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 77 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 78 | private MediaSessionRecord mPrioritySession; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 79 | private int mCurrentUserId = -1; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 80 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 81 | // Used to keep track of the current request to show routes for a specific |
| 82 | // session so we drop late callbacks properly. |
| 83 | private int mShowRoutesRequestId = 0; |
| 84 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 85 | // TODO refactor to have per user state for providers. See |
| 86 | // MediaRouterService for an example |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 87 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 88 | public MediaSessionService(Context context) { |
| 89 | super(context); |
| 90 | mSessionManagerImpl = new SessionManagerImpl(); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 91 | mPriorityStack = new MediaSessionStack(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 92 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 93 | mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent"); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onStart() { |
| 98 | publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 99 | Watchdog.getInstance().addMonitor(this); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 100 | updateUser(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Should trigger showing the Media route picker dialog. Right now it just |
| 105 | * kicks off a query to all the providers to get routes. |
| 106 | * |
| 107 | * @param record The session to show the picker for. |
| 108 | */ |
| 109 | public void showRoutePickerForSession(MediaSessionRecord record) { |
| 110 | // TODO for now just toggle the route to test (we will only have one |
| 111 | // match for now) |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 112 | synchronized (mLock) { |
| 113 | if (!mAllSessions.contains(record)) { |
| 114 | Log.d(TAG, "Unknown session tried to show route picker. Ignoring."); |
| 115 | return; |
| 116 | } |
| 117 | RouteInfo current = record.getRoute(); |
| 118 | UserRecord user = mUserRecords.get(record.getUserId()); |
| 119 | if (current != null) { |
| 120 | // For now send null to mean the local route |
| 121 | MediaRouteProviderProxy proxy = user.getProviderLocked(current.getProvider()); |
| 122 | if (proxy != null) { |
| 123 | proxy.removeSession(record); |
| 124 | } |
| 125 | record.selectRoute(null); |
| 126 | return; |
| 127 | } |
| 128 | ArrayList<MediaRouteProviderProxy> providers = user.getProvidersLocked(); |
| 129 | mShowRoutesRequestId++; |
| 130 | for (int i = providers.size() - 1; i >= 0; i--) { |
| 131 | MediaRouteProviderProxy provider = providers.get(i); |
| 132 | provider.getRoutes(record, mShowRoutesRequestId); |
| 133 | } |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Connect a session to the given route. |
| 139 | * |
| 140 | * @param session The session to connect. |
| 141 | * @param route The route to connect to. |
| 142 | * @param options The options to use for the connection. |
| 143 | */ |
| 144 | public void connectToRoute(MediaSessionRecord session, RouteInfo route, |
| 145 | RouteOptions options) { |
| 146 | synchronized (mLock) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 147 | if (!mAllSessions.contains(session)) { |
| 148 | Log.d(TAG, "Unknown session attempting to connect to route. Ignoring"); |
| 149 | return; |
| 150 | } |
| 151 | UserRecord user = mUserRecords.get(session.getUserId()); |
| 152 | if (user == null) { |
| 153 | Log.wtf(TAG, "connectToRoute: User " + session.getUserId() + " does not exist."); |
| 154 | return; |
| 155 | } |
| 156 | MediaRouteProviderProxy proxy = user.getProviderLocked(route.getProvider()); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 157 | if (proxy == null) { |
| 158 | Log.w(TAG, "Provider for route " + route.getName() + " does not exist."); |
| 159 | return; |
| 160 | } |
| 161 | RouteRequest request = new RouteRequest(session.getSessionInfo(), options, true); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 162 | proxy.connectToRoute(session, route, request); |
| 163 | } |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 164 | } |
| 165 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 166 | public void updateSession(MediaSessionRecord record) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 167 | synchronized (mLock) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 168 | if (!mAllSessions.contains(record)) { |
| 169 | Log.d(TAG, "Unknown session updated. Ignoring."); |
| 170 | return; |
| 171 | } |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 172 | mPriorityStack.onSessionStateChange(record); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 173 | if (record.isSystemPriority()) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 174 | if (record.isActive()) { |
| 175 | if (mPrioritySession != null) { |
| 176 | Log.w(TAG, "Replacing existing priority session with a new session"); |
| 177 | } |
| 178 | mPrioritySession = record; |
| 179 | } else { |
| 180 | if (mPrioritySession == record) { |
| 181 | mPrioritySession = null; |
| 182 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 183 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 188 | public void onSessionPlaystateChange(MediaSessionRecord record, int oldState, int newState) { |
| 189 | synchronized (mLock) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 190 | if (!mAllSessions.contains(record)) { |
| 191 | Log.d(TAG, "Unknown session changed playback state. Ignoring."); |
| 192 | return; |
| 193 | } |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 194 | mPriorityStack.onPlaystateChange(record, oldState, newState); |
| 195 | } |
| 196 | } |
| 197 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 198 | @Override |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 199 | public void onStartUser(int userHandle) { |
| 200 | updateUser(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onSwitchUser(int userHandle) { |
| 205 | updateUser(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void onStopUser(int userHandle) { |
| 210 | synchronized (mLock) { |
| 211 | UserRecord user = mUserRecords.get(userHandle); |
| 212 | if (user != null) { |
| 213 | destroyUserLocked(user); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 219 | public void monitor() { |
| 220 | synchronized (mLock) { |
| 221 | // Check for deadlock |
| 222 | } |
| 223 | } |
| 224 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 225 | protected void enforcePhoneStatePermission(int pid, int uid) { |
| 226 | if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid) |
| 227 | != PackageManager.PERMISSION_GRANTED) { |
| 228 | throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission."); |
| 229 | } |
| 230 | } |
| 231 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 232 | void sessionDied(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 233 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 234 | destroySessionLocked(session); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void destroySession(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 239 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 240 | destroySessionLocked(session); |
| 241 | } |
| 242 | } |
| 243 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 244 | private void updateUser() { |
| 245 | synchronized (mLock) { |
| 246 | int userId = ActivityManager.getCurrentUser(); |
| 247 | if (mCurrentUserId != userId) { |
| 248 | final int oldUserId = mCurrentUserId; |
| 249 | mCurrentUserId = userId; // do this first |
| 250 | |
| 251 | UserRecord oldUser = mUserRecords.get(oldUserId); |
| 252 | if (oldUser != null) { |
| 253 | oldUser.stopLocked(); |
| 254 | } |
| 255 | |
| 256 | UserRecord newUser = getOrCreateUser(userId); |
| 257 | newUser.startLocked(); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Stop the user and unbind from everything. |
| 264 | * |
| 265 | * @param user The user to dispose of |
| 266 | */ |
| 267 | private void destroyUserLocked(UserRecord user) { |
| 268 | user.stopLocked(); |
| 269 | user.destroyLocked(); |
| 270 | mUserRecords.remove(user.mUserId); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * When a session is removed several things need to happen. |
| 275 | * 1. We need to remove it from the relevant user. |
| 276 | * 2. We need to remove it from the priority stack. |
| 277 | * 3. We need to remove it from all sessions. |
| 278 | * 4. If this is the system priority session we need to clear it. |
| 279 | * 5. We need to unlink to death from the cb binder |
| 280 | * 6. We need to tell the session to do any final cleanup (onDestroy) |
| 281 | */ |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 282 | private void destroySessionLocked(MediaSessionRecord session) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 283 | int userId = session.getUserId(); |
| 284 | UserRecord user = mUserRecords.get(userId); |
| 285 | if (user != null) { |
| 286 | user.removeSessionLocked(session); |
| 287 | } |
| 288 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 289 | mPriorityStack.removeSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 290 | mAllSessions.remove(session); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 291 | if (session == mPrioritySession) { |
| 292 | mPrioritySession = null; |
| 293 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 294 | |
| 295 | try { |
| 296 | session.getCallback().asBinder().unlinkToDeath(session, 0); |
| 297 | } catch (Exception e) { |
| 298 | // ignore exceptions while destroying a session. |
| 299 | } |
| 300 | session.onDestroy(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | private void enforcePackageName(String packageName, int uid) { |
| 304 | if (TextUtils.isEmpty(packageName)) { |
| 305 | throw new IllegalArgumentException("packageName may not be empty"); |
| 306 | } |
| 307 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 308 | final int packageCount = packages.length; |
| 309 | for (int i = 0; i < packageCount; i++) { |
| 310 | if (packageName.equals(packages[i])) { |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | throw new IllegalArgumentException("packageName is not owned by the calling process"); |
| 315 | } |
| 316 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Checks a caller's authorization to register an IRemoteControlDisplay. |
| 319 | * Authorization is granted if one of the following is true: |
| 320 | * <ul> |
| 321 | * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL |
| 322 | * permission</li> |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 323 | * <li>the caller's listener is one of the enabled notification listeners |
| 324 | * for the caller's user</li> |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 325 | * </ul> |
| 326 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 327 | private void enforceMediaPermissions(ComponentName compName, int pid, int uid, |
| 328 | int resolvedUserId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 329 | if (getContext() |
| 330 | .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid) |
| 331 | != PackageManager.PERMISSION_GRANTED |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 332 | && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid), |
| 333 | resolvedUserId)) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 334 | throw new SecurityException("Missing permission to control media."); |
| 335 | } |
| 336 | } |
| 337 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 338 | /** |
| 339 | * This checks if the component is an enabled notification listener for the |
| 340 | * specified user. Enabled components may only operate on behalf of the user |
| 341 | * they're running as. |
| 342 | * |
| 343 | * @param compName The component that is enabled. |
| 344 | * @param userId The user id of the caller. |
| 345 | * @param forUserId The user id they're making the request on behalf of. |
| 346 | * @return True if the component is enabled, false otherwise |
| 347 | */ |
| 348 | private boolean isEnabledNotificationListener(ComponentName compName, int userId, |
| 349 | int forUserId) { |
| 350 | if (userId != forUserId) { |
| 351 | // You may not access another user's content as an enabled listener. |
| 352 | return false; |
| 353 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 354 | if (compName != null) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 355 | final String enabledNotifListeners = Settings.Secure.getStringForUser( |
| 356 | getContext().getContentResolver(), |
| 357 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 358 | userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 359 | if (enabledNotifListeners != null) { |
| 360 | final String[] components = enabledNotifListeners.split(":"); |
| 361 | for (int i = 0; i < components.length; i++) { |
| 362 | final ComponentName component = |
| 363 | ComponentName.unflattenFromString(components[i]); |
| 364 | if (component != null) { |
| 365 | if (compName.equals(component)) { |
| 366 | if (DEBUG) { |
| 367 | Log.d(TAG, "ok to get sessions: " + component + |
| 368 | " is authorized notification listener"); |
| 369 | } |
| 370 | return true; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | if (DEBUG) { |
| 376 | Log.d(TAG, "not ok to get sessions, " + compName + |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 377 | " is not in list of ENABLED_NOTIFICATION_LISTENERS for user " + userId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 383 | private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId, |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 384 | String callerPackageName, ISessionCallback cb, String tag) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 385 | synchronized (mLock) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 386 | return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 390 | /* |
| 391 | * When a session is created the following things need to happen. |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 392 | * 1. Its callback binder needs a link to death |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 393 | * 2. It needs to be added to all sessions. |
| 394 | * 3. It needs to be added to the priority stack. |
| 395 | * 4. It needs to be added to the relevant user record. |
| 396 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 397 | private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId, |
| 398 | String callerPackageName, ISessionCallback cb, String tag) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 399 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 400 | final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId, |
| 401 | callerPackageName, cb, tag, this, mHandler); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 402 | try { |
| 403 | cb.asBinder().linkToDeath(session, 0); |
| 404 | } catch (RemoteException e) { |
| 405 | throw new RuntimeException("Media Session owner died prematurely.", e); |
| 406 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 407 | |
| 408 | mAllSessions.add(session); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 409 | mPriorityStack.addSession(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 410 | |
| 411 | UserRecord user = getOrCreateUser(userId); |
| 412 | user.addSessionLocked(session); |
| 413 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 414 | if (DEBUG) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 415 | Log.d(TAG, "Created session for package " + callerPackageName + " with tag " + tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 416 | } |
| 417 | return session; |
| 418 | } |
| 419 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 420 | private UserRecord getOrCreateUser(int userId) { |
| 421 | UserRecord user = mUserRecords.get(userId); |
| 422 | if (user == null) { |
| 423 | user = new UserRecord(getContext(), userId); |
| 424 | mUserRecords.put(userId, user); |
| 425 | } |
| 426 | return user; |
| 427 | } |
| 428 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 429 | private int findIndexOfSessionForIdLocked(String sessionId) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 430 | for (int i = mAllSessions.size() - 1; i >= 0; i--) { |
| 431 | MediaSessionRecord session = mAllSessions.get(i); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 432 | if (TextUtils.equals(session.getSessionInfo().getId(), sessionId)) { |
| 433 | return i; |
| 434 | } |
| 435 | } |
| 436 | return -1; |
| 437 | } |
| 438 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 439 | private boolean isSessionDiscoverable(MediaSessionRecord record) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 440 | // TODO probably want to check more than if it's active. |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 441 | return record.isActive(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 442 | } |
| 443 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 444 | private MediaRouteProviderProxy.RoutesListener mRoutesCallback |
| 445 | = new MediaRouteProviderProxy.RoutesListener() { |
| 446 | @Override |
| 447 | public void onRoutesUpdated(String sessionId, ArrayList<RouteInfo> routes, |
| 448 | int reqId) { |
| 449 | // TODO for now select the first route to test, eventually add the |
| 450 | // new routes to the dialog if it is still open |
| 451 | synchronized (mLock) { |
| 452 | int index = findIndexOfSessionForIdLocked(sessionId); |
| 453 | if (index != -1 && routes != null && routes.size() > 0) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 454 | MediaSessionRecord record = mAllSessions.get(index); |
| 455 | RouteInfo route = routes.get(0); |
| 456 | record.selectRoute(route); |
| 457 | UserRecord user = mUserRecords.get(record.getUserId()); |
| 458 | MediaRouteProviderProxy provider = user.getProviderLocked(route.getProvider()); |
| 459 | provider.addSession(record); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public void onRouteConnected(String sessionId, RouteInfo route, |
| 466 | RouteRequest options, RouteConnectionRecord connection) { |
| 467 | synchronized (mLock) { |
| 468 | int index = findIndexOfSessionForIdLocked(sessionId); |
| 469 | if (index != -1) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 470 | MediaSessionRecord session = mAllSessions.get(index); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 471 | session.setRouteConnected(route, options.getConnectionOptions(), connection); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | }; |
| 476 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 477 | /** |
| 478 | * Information about a particular user. The contents of this object is |
| 479 | * guarded by mLock. |
| 480 | */ |
| 481 | final class UserRecord { |
| 482 | private final int mUserId; |
| 483 | private final MediaRouteProviderWatcher mRouteProviderWatcher; |
| 484 | private final ArrayList<MediaRouteProviderProxy> mProviders |
| 485 | = new ArrayList<MediaRouteProviderProxy>(); |
| 486 | private final ArrayList<MediaSessionRecord> mSessions = new ArrayList<MediaSessionRecord>(); |
| 487 | |
| 488 | public UserRecord(Context context, int userId) { |
| 489 | mUserId = userId; |
| 490 | mRouteProviderWatcher = new MediaRouteProviderWatcher(context, |
| 491 | mProviderWatcherCallback, mHandler, userId); |
| 492 | } |
| 493 | |
| 494 | public void startLocked() { |
| 495 | mRouteProviderWatcher.start(); |
| 496 | } |
| 497 | |
| 498 | public void stopLocked() { |
| 499 | mRouteProviderWatcher.stop(); |
| 500 | updateInterestLocked(); |
| 501 | } |
| 502 | |
| 503 | public void destroyLocked() { |
| 504 | for (int i = mSessions.size() - 1; i >= 0; i--) { |
| 505 | MediaSessionRecord session = mSessions.get(i); |
| 506 | MediaSessionService.this.destroySessionLocked(session); |
| 507 | if (session.isConnected()) { |
RoboErik | 42ea7ee | 2014-05-16 16:27:35 -0700 | [diff] [blame] | 508 | session.disconnect(MediaSession.DISCONNECT_REASON_USER_STOPPING); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | public ArrayList<MediaRouteProviderProxy> getProvidersLocked() { |
| 514 | return mProviders; |
| 515 | } |
| 516 | |
| 517 | public ArrayList<MediaSessionRecord> getSessionsLocked() { |
| 518 | return mSessions; |
| 519 | } |
| 520 | |
| 521 | public void addSessionLocked(MediaSessionRecord session) { |
| 522 | mSessions.add(session); |
| 523 | updateInterestLocked(); |
| 524 | } |
| 525 | |
| 526 | public void removeSessionLocked(MediaSessionRecord session) { |
| 527 | mSessions.remove(session); |
| 528 | RouteInfo route = session.getRoute(); |
| 529 | if (route != null) { |
| 530 | MediaRouteProviderProxy provider = getProviderLocked(route.getProvider()); |
| 531 | if (provider != null) { |
| 532 | provider.removeSession(session); |
| 533 | } |
| 534 | } |
| 535 | updateInterestLocked(); |
| 536 | } |
| 537 | |
| 538 | public void dumpLocked(PrintWriter pw, String prefix) { |
| 539 | pw.println(prefix + "Record for user " + mUserId); |
| 540 | String indent = prefix + " "; |
| 541 | int size = mProviders.size(); |
| 542 | pw.println(indent + size + " Providers:"); |
| 543 | for (int i = 0; i < size; i++) { |
| 544 | mProviders.get(i).dump(pw, indent); |
| 545 | } |
| 546 | pw.println(); |
| 547 | size = mSessions.size(); |
| 548 | pw.println(indent + size + " Sessions:"); |
| 549 | for (int i = 0; i < size; i++) { |
| 550 | // Just print the session info, the full session dump will |
| 551 | // already be in the list of all sessions. |
| 552 | pw.println(indent + mSessions.get(i).getSessionInfo()); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | public void updateInterestLocked() { |
| 557 | // TODO go through the sessions and build up the set of interfaces |
| 558 | // we're interested in. Update the provider watcher. |
| 559 | // For now, just express interest in all providers for the current |
| 560 | // user |
| 561 | boolean interested = mUserId == mCurrentUserId; |
| 562 | for (int i = mProviders.size() - 1; i >= 0; i--) { |
| 563 | mProviders.get(i).setInterested(interested); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | private MediaRouteProviderProxy getProviderLocked(String providerId) { |
| 568 | for (int i = mProviders.size() - 1; i >= 0; i--) { |
| 569 | MediaRouteProviderProxy provider = mProviders.get(i); |
| 570 | if (TextUtils.equals(providerId, provider.getId())) { |
| 571 | return provider; |
| 572 | } |
| 573 | } |
| 574 | return null; |
| 575 | } |
| 576 | |
| 577 | private MediaRouteProviderWatcher.Callback mProviderWatcherCallback |
| 578 | = new MediaRouteProviderWatcher.Callback() { |
| 579 | @Override |
| 580 | public void removeProvider(MediaRouteProviderProxy provider) { |
| 581 | synchronized (mLock) { |
| 582 | mProviders.remove(provider); |
| 583 | provider.setRoutesListener(null); |
| 584 | provider.setInterested(false); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public void addProvider(MediaRouteProviderProxy provider) { |
| 590 | synchronized (mLock) { |
| 591 | mProviders.add(provider); |
| 592 | provider.setRoutesListener(mRoutesCallback); |
| 593 | provider.setInterested(true); |
| 594 | } |
| 595 | } |
| 596 | }; |
| 597 | } |
| 598 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 599 | class SessionManagerImpl extends ISessionManager.Stub { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 600 | private static final String EXTRA_WAKELOCK_ACQUIRED = |
| 601 | "android.media.AudioService.WAKELOCK_ACQUIRED"; |
| 602 | private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number |
| 603 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 604 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 605 | public ISession createSession(String packageName, ISessionCallback cb, String tag, |
| 606 | int userId) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 607 | final int pid = Binder.getCallingPid(); |
| 608 | final int uid = Binder.getCallingUid(); |
| 609 | final long token = Binder.clearCallingIdentity(); |
| 610 | try { |
| 611 | enforcePackageName(packageName, uid); |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 612 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 613 | false /* allowAll */, true /* requireFull */, "createSession", packageName); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 614 | if (cb == null) { |
| 615 | throw new IllegalArgumentException("Controller callback cannot be null"); |
| 616 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 617 | return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag) |
| 618 | .getSessionBinder(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 619 | } finally { |
| 620 | Binder.restoreCallingIdentity(token); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 625 | public List<IBinder> getSessions(ComponentName componentName, int userId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 626 | final int pid = Binder.getCallingPid(); |
| 627 | final int uid = Binder.getCallingUid(); |
| 628 | final long token = Binder.clearCallingIdentity(); |
| 629 | |
| 630 | try { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 631 | String packageName = null; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 632 | if (componentName != null) { |
| 633 | // If they gave us a component name verify they own the |
| 634 | // package |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 635 | packageName = componentName.getPackageName(); |
| 636 | enforcePackageName(packageName, uid); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 637 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 638 | // Check that they can make calls on behalf of the user and |
| 639 | // get the final user id |
| 640 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 641 | true /* allowAll */, true /* requireFull */, "getSessions", packageName); |
| 642 | // Check if they have the permissions or their component is |
| 643 | // enabled for the user they're calling from. |
| 644 | enforceMediaPermissions(componentName, pid, uid, resolvedUserId); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 645 | ArrayList<IBinder> binders = new ArrayList<IBinder>(); |
| 646 | synchronized (mLock) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 647 | ArrayList<MediaSessionRecord> records = mPriorityStack |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 648 | .getActiveSessions(resolvedUserId); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 649 | int size = records.size(); |
| 650 | for (int i = 0; i < size; i++) { |
| 651 | binders.add(records.get(i).getControllerBinder().asBinder()); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | return binders; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 655 | } finally { |
| 656 | Binder.restoreCallingIdentity(token); |
| 657 | } |
| 658 | } |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 659 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 660 | /** |
| 661 | * Handles the dispatching of the media button events to one of the |
| 662 | * registered listeners, or if there was none, broadcast an |
| 663 | * ACTION_MEDIA_BUTTON intent to the rest of the system. |
| 664 | * |
| 665 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 666 | * supported media buttons |
| 667 | * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held |
| 668 | * while this key event is dispatched. |
| 669 | */ |
| 670 | @Override |
| 671 | public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { |
| 672 | if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) { |
| 673 | Log.w(TAG, "Attempted to dispatch null or non-media key event."); |
| 674 | return; |
| 675 | } |
| 676 | final int pid = Binder.getCallingPid(); |
| 677 | final int uid = Binder.getCallingUid(); |
| 678 | final long token = Binder.clearCallingIdentity(); |
| 679 | |
| 680 | try { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 681 | synchronized (mLock) { |
| 682 | MediaSessionRecord mbSession = mPriorityStack |
| 683 | .getDefaultMediaButtonSession(mCurrentUserId); |
| 684 | if (mbSession != null) { |
| 685 | if (DEBUG) { |
| 686 | Log.d(TAG, "Sending media key to " + mbSession.getSessionInfo()); |
| 687 | } |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 688 | if (needWakeLock) { |
| 689 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 690 | } |
| 691 | // If we don't need a wakelock use -1 as the id so we |
| 692 | // won't release it later |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 693 | mbSession.sendMediaButton(keyEvent, |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 694 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
| 695 | mKeyEventReceiver); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 696 | } else { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 697 | if (needWakeLock) { |
| 698 | mMediaEventWakeLock.acquire(); |
| 699 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 700 | if (DEBUG) { |
| 701 | Log.d(TAG, "Sending media key ordered broadcast"); |
| 702 | } |
| 703 | // Fallback to legacy behavior |
| 704 | Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); |
| 705 | keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); |
| 706 | if (needWakeLock) { |
| 707 | keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, |
| 708 | WAKELOCK_RELEASE_ON_FINISHED); |
| 709 | } |
| 710 | getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, |
| 711 | null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null); |
| 712 | } |
| 713 | } |
| 714 | } finally { |
| 715 | Binder.restoreCallingIdentity(token); |
| 716 | } |
| 717 | } |
| 718 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 719 | @Override |
| 720 | public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) { |
| 721 | if (getContext().checkCallingOrSelfPermission(Manifest.permission.DUMP) |
| 722 | != PackageManager.PERMISSION_GRANTED) { |
| 723 | pw.println("Permission Denial: can't dump MediaSessionService from from pid=" |
| 724 | + Binder.getCallingPid() |
| 725 | + ", uid=" + Binder.getCallingUid()); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | pw.println("MEDIA SESSION SERVICE (dumpsys media_session)"); |
| 730 | pw.println(); |
| 731 | |
| 732 | synchronized (mLock) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 733 | pw.println("Session for calls:" + mPrioritySession); |
| 734 | if (mPrioritySession != null) { |
| 735 | mPrioritySession.dump(pw, ""); |
| 736 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 737 | int count = mAllSessions.size(); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 738 | pw.println(count + " Sessions:"); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 739 | for (int i = 0; i < count; i++) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 740 | mAllSessions.get(i).dump(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 741 | pw.println(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 742 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 743 | mPriorityStack.dump(pw, ""); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 744 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 745 | pw.println("User Records:"); |
| 746 | count = mUserRecords.size(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 747 | for (int i = 0; i < count; i++) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 748 | UserRecord user = mUserRecords.get(i); |
| 749 | user.dumpLocked(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 753 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 754 | private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler); |
| 755 | |
| 756 | class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable { |
| 757 | private final Handler mHandler; |
| 758 | private int mRefCount = 0; |
| 759 | private int mLastTimeoutId = 0; |
| 760 | |
| 761 | public KeyEventWakeLockReceiver(Handler handler) { |
| 762 | super(handler); |
| 763 | mHandler = handler; |
| 764 | } |
| 765 | |
| 766 | public void onTimeout() { |
| 767 | synchronized (mLock) { |
| 768 | if (mRefCount == 0) { |
| 769 | // We've already released it, so just return |
| 770 | return; |
| 771 | } |
| 772 | mLastTimeoutId++; |
| 773 | mRefCount = 0; |
| 774 | releaseWakeLockLocked(); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | public void aquireWakeLockLocked() { |
| 779 | if (mRefCount == 0) { |
| 780 | mMediaEventWakeLock.acquire(); |
| 781 | } |
| 782 | mRefCount++; |
| 783 | mHandler.removeCallbacks(this); |
| 784 | mHandler.postDelayed(this, WAKELOCK_TIMEOUT); |
| 785 | |
| 786 | } |
| 787 | |
| 788 | @Override |
| 789 | public void run() { |
| 790 | onTimeout(); |
| 791 | } |
| 792 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 793 | @Override |
| 794 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 795 | if (resultCode < mLastTimeoutId) { |
| 796 | // Ignore results from calls that were before the last |
| 797 | // timeout, just in case. |
| 798 | return; |
| 799 | } else { |
| 800 | synchronized (mLock) { |
| 801 | if (mRefCount > 0) { |
| 802 | mRefCount--; |
| 803 | if (mRefCount == 0) { |
| 804 | releaseWakeLockLocked(); |
| 805 | } |
| 806 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | } |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame^] | 810 | |
| 811 | private void releaseWakeLockLocked() { |
| 812 | mMediaEventWakeLock.release(); |
| 813 | mHandler.removeCallbacks(this); |
| 814 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { |
| 818 | @Override |
| 819 | public void onReceive(Context context, Intent intent) { |
| 820 | if (intent == null) { |
| 821 | return; |
| 822 | } |
| 823 | Bundle extras = intent.getExtras(); |
| 824 | if (extras == null) { |
| 825 | return; |
| 826 | } |
| 827 | synchronized (mLock) { |
| 828 | if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED) |
| 829 | && mMediaEventWakeLock.isHeld()) { |
| 830 | mMediaEventWakeLock.release(); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | }; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | } |