blob: 4bf9d8f5ff53887d7ead3369cb72dbde246a8c4c [file] [log] [blame]
RoboErik01fe6612014-02-13 14:19:04 -08001/*
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
17package com.android.server.media;
18
RoboErika278ea72014-04-24 14:49:01 -070019import android.Manifest;
Jaewan Kim50269362016-12-23 11:22:02 +090020import android.annotation.NonNull;
RoboErik8a2cfc32014-05-16 11:19:38 -070021import android.app.Activity;
RoboErike7880d82014-04-30 12:48:25 -070022import android.app.ActivityManager;
RoboErik9a9d0b52014-05-20 14:53:39 -070023import android.app.KeyguardManager;
RoboErikb214efb2014-07-24 13:20:30 -070024import android.app.PendingIntent;
25import android.app.PendingIntent.CanceledException;
RoboErik9a9d0b52014-05-20 14:53:39 -070026import android.content.ActivityNotFoundException;
RoboErik8a2cfc32014-05-16 11:19:38 -070027import android.content.BroadcastReceiver;
RoboErike7880d82014-04-30 12:48:25 -070028import android.content.ComponentName;
RoboErik6f0e4dd2014-06-17 16:56:27 -070029import android.content.ContentResolver;
RoboErik01fe6612014-02-13 14:19:04 -080030import android.content.Context;
RoboErik8a2cfc32014-05-16 11:19:38 -070031import android.content.Intent;
RoboErika278ea72014-04-24 14:49:01 -070032import android.content.pm.PackageManager;
Jaewan Kima7dce192017-02-16 17:10:54 +090033import android.content.pm.UserInfo;
RoboErik7aef77b2014-08-08 15:56:54 -070034import android.database.ContentObserver;
RoboErik3c45c292014-07-08 16:47:31 -070035import android.media.AudioManager;
John Spurlockeb69e242015-02-17 17:15:04 -050036import android.media.AudioManagerInternal;
RoboErik94c716e2014-09-14 13:54:31 -070037import android.media.AudioSystem;
RoboErikb69ffd42014-05-30 14:57:59 -070038import android.media.IAudioService;
RoboErik19c95182014-06-23 15:38:48 -070039import android.media.IRemoteVolumeController;
RoboErik2e7a9162014-06-04 16:53:45 -070040import android.media.session.IActiveSessionsListener;
Jaewan Kimbd16f452017-02-03 16:21:38 +090041import android.media.session.ICallback;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080042import android.media.session.IOnMediaKeyListener;
Jaewan Kim50269362016-12-23 11:22:02 +090043import android.media.session.IOnVolumeKeyLongPressListener;
RoboErik07c70772014-03-20 13:33:52 -070044import android.media.session.ISession;
45import android.media.session.ISessionCallback;
46import android.media.session.ISessionManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070047import android.media.session.MediaSession;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080048import android.media.session.MediaSessionManager;
RoboErik7aef77b2014-08-08 15:56:54 -070049import android.net.Uri;
RoboErik01fe6612014-02-13 14:19:04 -080050import android.os.Binder;
RoboErik8a2cfc32014-05-16 11:19:38 -070051import android.os.Bundle;
RoboErik8ae0f342014-02-24 18:02:08 -080052import android.os.Handler;
RoboErike7880d82014-04-30 12:48:25 -070053import android.os.IBinder;
RoboErik2e7a9162014-06-04 16:53:45 -070054import android.os.Message;
RoboErik8a2cfc32014-05-16 11:19:38 -070055import android.os.PowerManager;
Jaewan Kim8f729082016-06-21 12:36:26 +090056import android.os.Process;
RoboErik01fe6612014-02-13 14:19:04 -080057import android.os.RemoteException;
RoboErik8a2cfc32014-05-16 11:19:38 -070058import android.os.ResultReceiver;
RoboErikb69ffd42014-05-30 14:57:59 -070059import android.os.ServiceManager;
Jaewan Kim92dea332017-02-02 11:52:08 +090060import android.os.SystemProperties;
RoboErike7880d82014-04-30 12:48:25 -070061import android.os.UserHandle;
Jaewan Kim8f729082016-06-21 12:36:26 +090062import android.os.UserManager;
RoboErike7880d82014-04-30 12:48:25 -070063import android.provider.Settings;
RoboErik9a9d0b52014-05-20 14:53:39 -070064import android.speech.RecognizerIntent;
RoboErik01fe6612014-02-13 14:19:04 -080065import android.text.TextUtils;
Jaewan Kim92dea332017-02-02 11:52:08 +090066import android.util.IntArray;
RoboErik01fe6612014-02-13 14:19:04 -080067import android.util.Log;
Jeff Brown38d3feb2015-03-19 18:26:30 -070068import android.util.Slog;
RoboErik4646d282014-05-13 10:13:04 -070069import android.util.SparseArray;
Jaewan Kima7dce192017-02-16 17:10:54 +090070import android.util.SparseIntArray;
RoboErik8a2cfc32014-05-16 11:19:38 -070071import android.view.KeyEvent;
Jaewan Kimd61a87b2017-02-17 23:14:10 +090072import android.view.ViewConfiguration;
RoboErik01fe6612014-02-13 14:19:04 -080073
John Spurlockeb69e242015-02-17 17:15:04 -050074import com.android.server.LocalServices;
RoboErik01fe6612014-02-13 14:19:04 -080075import com.android.server.SystemService;
RoboErika278ea72014-04-24 14:49:01 -070076import com.android.server.Watchdog;
77import com.android.server.Watchdog.Monitor;
RoboErik01fe6612014-02-13 14:19:04 -080078
RoboErika278ea72014-04-24 14:49:01 -070079import java.io.FileDescriptor;
80import java.io.PrintWriter;
RoboErik01fe6612014-02-13 14:19:04 -080081import java.util.ArrayList;
Jaewan Kim8f729082016-06-21 12:36:26 +090082import java.util.Arrays;
RoboErike7880d82014-04-30 12:48:25 -070083import java.util.List;
RoboErik01fe6612014-02-13 14:19:04 -080084
85/**
86 * System implementation of MediaSessionManager
87 */
RoboErika278ea72014-04-24 14:49:01 -070088public class MediaSessionService extends SystemService implements Monitor {
RoboErik01fe6612014-02-13 14:19:04 -080089 private static final String TAG = "MediaSessionService";
Jaewan Kim92dea332017-02-02 11:52:08 +090090 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jaewan Kim50269362016-12-23 11:22:02 +090091 // Leave log for key event always.
92 private static final boolean DEBUG_KEY_EVENT = true;
RoboErik01fe6612014-02-13 14:19:04 -080093
RoboErik418c10c2014-05-19 09:25:25 -070094 private static final int WAKELOCK_TIMEOUT = 5000;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080095 private static final int MEDIA_KEY_LISTENER_TIMEOUT = 1000;
RoboErik418c10c2014-05-19 09:25:25 -070096
RoboErik01fe6612014-02-13 14:19:04 -080097 private final SessionManagerImpl mSessionManagerImpl;
98
Jaewan Kima7dce192017-02-16 17:10:54 +090099 // Keeps the full user id for each user.
100 private final SparseIntArray mFullUserIds = new SparseIntArray();
101 private final SparseArray<FullUserRecord> mUserRecords = new SparseArray<FullUserRecord>();
RoboErik2e7a9162014-06-04 16:53:45 -0700102 private final ArrayList<SessionsListenerRecord> mSessionsListeners
103 = new ArrayList<SessionsListenerRecord>();
RoboErik01fe6612014-02-13 14:19:04 -0800104 private final Object mLock = new Object();
RoboErik2e7a9162014-06-04 16:53:45 -0700105 private final MessageHandler mHandler = new MessageHandler();
RoboErik8a2cfc32014-05-16 11:19:38 -0700106 private final PowerManager.WakeLock mMediaEventWakeLock;
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900107 private final int mLongPressTimeout;
RoboErik01fe6612014-02-13 14:19:04 -0800108
RoboErik9a9d0b52014-05-20 14:53:39 -0700109 private KeyguardManager mKeyguardManager;
RoboErikb69ffd42014-05-30 14:57:59 -0700110 private IAudioService mAudioService;
John Spurlockeb69e242015-02-17 17:15:04 -0500111 private AudioManagerInternal mAudioManagerInternal;
RoboErik6f0e4dd2014-06-17 16:56:27 -0700112 private ContentResolver mContentResolver;
RoboErik7aef77b2014-08-08 15:56:54 -0700113 private SettingsObserver mSettingsObserver;
RoboErik9a9d0b52014-05-20 14:53:39 -0700114
Jaewan Kima7dce192017-02-16 17:10:54 +0900115 // The FullUserRecord of the current users. (i.e. The foreground user that isn't a profile)
116 // It's always not null after the MediaSessionService is started.
117 private FullUserRecord mCurrentFullUserRecord;
118 private MediaSessionRecord mGlobalPrioritySession;
Jaewan Kim92dea332017-02-02 11:52:08 +0900119 private AudioPlaybackMonitor mAudioPlaybackMonitor;
RoboErike7880d82014-04-30 12:48:25 -0700120
RoboErik19c95182014-06-23 15:38:48 -0700121 // Used to notify system UI when remote volume was changed. TODO find a
122 // better way to handle this.
123 private IRemoteVolumeController mRvc;
124
RoboErik01fe6612014-02-13 14:19:04 -0800125 public MediaSessionService(Context context) {
126 super(context);
127 mSessionManagerImpl = new SessionManagerImpl();
RoboErik8a2cfc32014-05-16 11:19:38 -0700128 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
129 mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent");
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900130 mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
RoboErik01fe6612014-02-13 14:19:04 -0800131 }
132
133 @Override
134 public void onStart() {
135 publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl);
RoboErika278ea72014-04-24 14:49:01 -0700136 Watchdog.getInstance().addMonitor(this);
RoboErik9a9d0b52014-05-20 14:53:39 -0700137 mKeyguardManager =
138 (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
RoboErikb69ffd42014-05-30 14:57:59 -0700139 mAudioService = getAudioService();
Jaewan Kim92dea332017-02-02 11:52:08 +0900140 mAudioPlaybackMonitor = new AudioPlaybackMonitor(getContext(), mAudioService,
141 new AudioPlaybackMonitor.OnAudioPlaybackStartedListener() {
142 @Override
143 public void onAudioPlaybackStarted(int uid) {
144 synchronized (mLock) {
145 FullUserRecord user =
146 getFullUserRecordLocked(UserHandle.getUserId(uid));
147 if (user != null) {
148 user.mPriorityStack.updateMediaButtonSessionIfNeeded();
149 }
150 }
151 }
152 });
John Spurlockeb69e242015-02-17 17:15:04 -0500153 mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class);
RoboErik6f0e4dd2014-06-17 16:56:27 -0700154 mContentResolver = getContext().getContentResolver();
RoboErik7aef77b2014-08-08 15:56:54 -0700155 mSettingsObserver = new SettingsObserver();
156 mSettingsObserver.observe();
RoboErikc8f92d12015-01-05 16:48:07 -0800157
158 updateUser();
RoboErikb69ffd42014-05-30 14:57:59 -0700159 }
160
161 private IAudioService getAudioService() {
162 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
163 return IAudioService.Stub.asInterface(b);
RoboErik07c70772014-03-20 13:33:52 -0700164 }
165
Jaewan Kima7dce192017-02-16 17:10:54 +0900166 private boolean isGlobalPriorityActiveLocked() {
167 return mGlobalPrioritySession != null && mGlobalPrioritySession.isActive();
168 }
169
RoboErika8f95142014-05-05 14:23:49 -0700170 public void updateSession(MediaSessionRecord record) {
RoboErike7880d82014-04-30 12:48:25 -0700171 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900172 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
173 if (user == null || !user.mPriorityStack.contains(record)) {
RoboErik4646d282014-05-13 10:13:04 -0700174 Log.d(TAG, "Unknown session updated. Ignoring.");
175 return;
176 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900177 user.mPriorityStack.onSessionStateChange(record);
178 if ((record.getFlags() & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) {
179 mGlobalPrioritySession = record;
Jaewan Kim92dea332017-02-02 11:52:08 +0900180 user.pushAddressedPlayerChangedLocked();
Jaewan Kima7dce192017-02-16 17:10:54 +0900181 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900182 mHandler.postSessionsChanged(record.getUserId());
RoboErike7880d82014-04-30 12:48:25 -0700183 }
184 }
185
RoboErik9c5b7cb2015-01-15 15:09:09 -0800186 /**
Hyundo Moona055f132017-01-13 15:31:06 +0900187 * Tells the system UI that volume has changed on an active remote session.
RoboErik9c5b7cb2015-01-15 15:09:09 -0800188 */
189 public void notifyRemoteVolumeChanged(int flags, MediaSessionRecord session) {
Hyundo Moona055f132017-01-13 15:31:06 +0900190 if (mRvc == null || !session.isActive()) {
RoboErik9c5b7cb2015-01-15 15:09:09 -0800191 return;
192 }
193 try {
194 mRvc.remoteVolumeChanged(session.getControllerBinder(), flags);
195 } catch (Exception e) {
196 Log.wtf(TAG, "Error sending volume change to system UI.", e);
197 }
198 }
199
Jaewan Kim92dea332017-02-02 11:52:08 +0900200 public void onSessionPlaystateChanged(MediaSessionRecord record, int oldState, int newState) {
RoboErika8f95142014-05-05 14:23:49 -0700201 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900202 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
203 if (user == null || !user.mPriorityStack.contains(record)) {
RoboErik4646d282014-05-13 10:13:04 -0700204 Log.d(TAG, "Unknown session changed playback state. Ignoring.");
205 return;
206 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900207 user.mPriorityStack.onPlaystateChanged(record, oldState, newState);
RoboErika8f95142014-05-05 14:23:49 -0700208 }
209 }
210
RoboErik19c95182014-06-23 15:38:48 -0700211 public void onSessionPlaybackTypeChanged(MediaSessionRecord record) {
212 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900213 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
214 if (user == null || !user.mPriorityStack.contains(record)) {
RoboErik19c95182014-06-23 15:38:48 -0700215 Log.d(TAG, "Unknown session changed playback type. Ignoring.");
216 return;
217 }
218 pushRemoteVolumeUpdateLocked(record.getUserId());
219 }
220 }
221
RoboErika278ea72014-04-24 14:49:01 -0700222 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900223 public void onStartUser(int userId) {
224 if (DEBUG) Log.d(TAG, "onStartUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700225 updateUser();
226 }
227
228 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900229 public void onSwitchUser(int userId) {
230 if (DEBUG) Log.d(TAG, "onSwitchUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700231 updateUser();
232 }
233
234 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900235 public void onStopUser(int userId) {
236 if (DEBUG) Log.d(TAG, "onStopUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700237 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900238 FullUserRecord user = getFullUserRecordLocked(userId);
RoboErik4646d282014-05-13 10:13:04 -0700239 if (user != null) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900240 if (user.mFullUserId == userId) {
241 user.destroySessionsForUserLocked(UserHandle.USER_ALL);
242 mUserRecords.remove(userId);
243 } else {
244 user.destroySessionsForUserLocked(userId);
245 }
RoboErik4646d282014-05-13 10:13:04 -0700246 }
Jaewan Kim8f729082016-06-21 12:36:26 +0900247 updateUser();
RoboErik4646d282014-05-13 10:13:04 -0700248 }
249 }
250
251 @Override
RoboErika278ea72014-04-24 14:49:01 -0700252 public void monitor() {
253 synchronized (mLock) {
254 // Check for deadlock
255 }
256 }
257
RoboErik4646d282014-05-13 10:13:04 -0700258 protected void enforcePhoneStatePermission(int pid, int uid) {
259 if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid)
260 != PackageManager.PERMISSION_GRANTED) {
261 throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission.");
262 }
263 }
264
RoboErik01fe6612014-02-13 14:19:04 -0800265 void sessionDied(MediaSessionRecord session) {
RoboErika278ea72014-04-24 14:49:01 -0700266 synchronized (mLock) {
RoboErik01fe6612014-02-13 14:19:04 -0800267 destroySessionLocked(session);
268 }
269 }
270
271 void destroySession(MediaSessionRecord session) {
RoboErika278ea72014-04-24 14:49:01 -0700272 synchronized (mLock) {
RoboErik01fe6612014-02-13 14:19:04 -0800273 destroySessionLocked(session);
274 }
275 }
276
RoboErik4646d282014-05-13 10:13:04 -0700277 private void updateUser() {
278 synchronized (mLock) {
Jaewan Kim8f729082016-06-21 12:36:26 +0900279 UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
Jaewan Kima7dce192017-02-16 17:10:54 +0900280 mFullUserIds.clear();
281 List<UserInfo> allUsers = manager.getUsers();
282 if (allUsers != null) {
283 for (UserInfo userInfo : allUsers) {
284 if (userInfo.isManagedProfile()) {
285 mFullUserIds.put(userInfo.id, userInfo.profileGroupId);
286 } else {
287 mFullUserIds.put(userInfo.id, userInfo.id);
288 if (mUserRecords.get(userInfo.id) == null) {
289 mUserRecords.put(userInfo.id, new FullUserRecord(userInfo.id));
290 }
291 }
Jaewan Kim8f729082016-06-21 12:36:26 +0900292 }
RoboErik4646d282014-05-13 10:13:04 -0700293 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900294 // Ensure that the current full user exists.
295 int currentFullUserId = ActivityManager.getCurrentUser();
296 mCurrentFullUserRecord = mUserRecords.get(currentFullUserId);
297 if (mCurrentFullUserRecord == null) {
298 Log.w(TAG, "Cannot find FullUserInfo for the current user " + currentFullUserId);
299 mCurrentFullUserRecord = new FullUserRecord(currentFullUserId);
300 mUserRecords.put(currentFullUserId, mCurrentFullUserRecord);
301 }
302 mFullUserIds.put(currentFullUserId, currentFullUserId);
RoboErik4646d282014-05-13 10:13:04 -0700303 }
304 }
305
RoboErik7aef77b2014-08-08 15:56:54 -0700306 private void updateActiveSessionListeners() {
307 synchronized (mLock) {
308 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
309 SessionsListenerRecord listener = mSessionsListeners.get(i);
310 try {
311 enforceMediaPermissions(listener.mComponentName, listener.mPid, listener.mUid,
312 listener.mUserId);
313 } catch (SecurityException e) {
314 Log.i(TAG, "ActiveSessionsListener " + listener.mComponentName
315 + " is no longer authorized. Disconnecting.");
316 mSessionsListeners.remove(i);
317 try {
318 listener.mListener
319 .onActiveSessionsChanged(new ArrayList<MediaSession.Token>());
320 } catch (Exception e1) {
321 // ignore
322 }
323 }
324 }
325 }
326 }
327
RoboErik4646d282014-05-13 10:13:04 -0700328 /*
329 * When a session is removed several things need to happen.
330 * 1. We need to remove it from the relevant user.
331 * 2. We need to remove it from the priority stack.
332 * 3. We need to remove it from all sessions.
333 * 4. If this is the system priority session we need to clear it.
334 * 5. We need to unlink to death from the cb binder
335 * 6. We need to tell the session to do any final cleanup (onDestroy)
336 */
RoboErik01fe6612014-02-13 14:19:04 -0800337 private void destroySessionLocked(MediaSessionRecord session) {
Insun Kang30be970a2015-11-26 15:35:44 +0900338 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900339 Log.d(TAG, "Destroying " + session);
Insun Kang30be970a2015-11-26 15:35:44 +0900340 }
RoboErik4646d282014-05-13 10:13:04 -0700341 int userId = session.getUserId();
Jaewan Kima7dce192017-02-16 17:10:54 +0900342 FullUserRecord user = getFullUserRecordLocked(userId);
RoboErik4646d282014-05-13 10:13:04 -0700343 if (user != null) {
344 user.removeSessionLocked(session);
345 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900346 if (mGlobalPrioritySession == session) {
347 mGlobalPrioritySession = null;
Jaewan Kim92dea332017-02-02 11:52:08 +0900348 if (session.isActive() && user != null) {
349 user.pushAddressedPlayerChangedLocked();
350 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900351 }
RoboErik4646d282014-05-13 10:13:04 -0700352
353 try {
354 session.getCallback().asBinder().unlinkToDeath(session, 0);
355 } catch (Exception e) {
356 // ignore exceptions while destroying a session.
357 }
358 session.onDestroy();
Jaewan Kim92dea332017-02-02 11:52:08 +0900359 mHandler.postSessionsChanged(session.getUserId());
RoboErik01fe6612014-02-13 14:19:04 -0800360 }
361
362 private void enforcePackageName(String packageName, int uid) {
363 if (TextUtils.isEmpty(packageName)) {
364 throw new IllegalArgumentException("packageName may not be empty");
365 }
366 String[] packages = getContext().getPackageManager().getPackagesForUid(uid);
367 final int packageCount = packages.length;
368 for (int i = 0; i < packageCount; i++) {
369 if (packageName.equals(packages[i])) {
370 return;
371 }
372 }
373 throw new IllegalArgumentException("packageName is not owned by the calling process");
374 }
375
RoboErike7880d82014-04-30 12:48:25 -0700376 /**
377 * Checks a caller's authorization to register an IRemoteControlDisplay.
378 * Authorization is granted if one of the following is true:
379 * <ul>
380 * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL
381 * permission</li>
RoboErika5b02322014-05-07 17:05:49 -0700382 * <li>the caller's listener is one of the enabled notification listeners
383 * for the caller's user</li>
RoboErike7880d82014-04-30 12:48:25 -0700384 * </ul>
385 */
RoboErika5b02322014-05-07 17:05:49 -0700386 private void enforceMediaPermissions(ComponentName compName, int pid, int uid,
387 int resolvedUserId) {
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500388 if (isCurrentVolumeController(uid, pid)) return;
RoboErike7880d82014-04-30 12:48:25 -0700389 if (getContext()
390 .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid)
391 != PackageManager.PERMISSION_GRANTED
RoboErika5b02322014-05-07 17:05:49 -0700392 && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid),
393 resolvedUserId)) {
RoboErike7880d82014-04-30 12:48:25 -0700394 throw new SecurityException("Missing permission to control media.");
395 }
396 }
397
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500398 private boolean isCurrentVolumeController(int uid, int pid) {
399 return getContext().checkPermission(android.Manifest.permission.STATUS_BAR_SERVICE,
400 pid, uid) == PackageManager.PERMISSION_GRANTED;
John Spurlockbe19ed02015-02-22 10:57:55 -0500401 }
402
403 private void enforceSystemUiPermission(String action, int pid, int uid) {
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500404 if (!isCurrentVolumeController(uid, pid)) {
RoboErik19c95182014-06-23 15:38:48 -0700405 throw new SecurityException("Only system ui may " + action);
406 }
407 }
408
RoboErika5b02322014-05-07 17:05:49 -0700409 /**
410 * This checks if the component is an enabled notification listener for the
411 * specified user. Enabled components may only operate on behalf of the user
412 * they're running as.
413 *
414 * @param compName The component that is enabled.
415 * @param userId The user id of the caller.
416 * @param forUserId The user id they're making the request on behalf of.
417 * @return True if the component is enabled, false otherwise
418 */
419 private boolean isEnabledNotificationListener(ComponentName compName, int userId,
420 int forUserId) {
421 if (userId != forUserId) {
422 // You may not access another user's content as an enabled listener.
423 return false;
424 }
RoboErik51fa6bc2014-06-20 14:59:58 -0700425 if (DEBUG) {
426 Log.d(TAG, "Checking if enabled notification listener " + compName);
427 }
RoboErike7880d82014-04-30 12:48:25 -0700428 if (compName != null) {
RoboErik6f0e4dd2014-06-17 16:56:27 -0700429 final String enabledNotifListeners = Settings.Secure.getStringForUser(mContentResolver,
RoboErike7880d82014-04-30 12:48:25 -0700430 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS,
RoboErika5b02322014-05-07 17:05:49 -0700431 userId);
RoboErike7880d82014-04-30 12:48:25 -0700432 if (enabledNotifListeners != null) {
433 final String[] components = enabledNotifListeners.split(":");
434 for (int i = 0; i < components.length; i++) {
435 final ComponentName component =
436 ComponentName.unflattenFromString(components[i]);
437 if (component != null) {
438 if (compName.equals(component)) {
439 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900440 Log.d(TAG, "ok to get sessions. " + component +
RoboErike7880d82014-04-30 12:48:25 -0700441 " is authorized notification listener");
442 }
443 return true;
444 }
445 }
446 }
447 }
448 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900449 Log.d(TAG, "not ok to get sessions. " + compName +
RoboErika5b02322014-05-07 17:05:49 -0700450 " is not in list of ENABLED_NOTIFICATION_LISTENERS for user " + userId);
RoboErike7880d82014-04-30 12:48:25 -0700451 }
452 }
453 return false;
454 }
455
RoboErika5b02322014-05-07 17:05:49 -0700456 private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId,
RoboErik4646d282014-05-13 10:13:04 -0700457 String callerPackageName, ISessionCallback cb, String tag) throws RemoteException {
RoboErik01fe6612014-02-13 14:19:04 -0800458 synchronized (mLock) {
RoboErika5b02322014-05-07 17:05:49 -0700459 return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag);
RoboErik01fe6612014-02-13 14:19:04 -0800460 }
461 }
462
RoboErik4646d282014-05-13 10:13:04 -0700463 /*
464 * When a session is created the following things need to happen.
RoboErik8a2cfc32014-05-16 11:19:38 -0700465 * 1. Its callback binder needs a link to death
RoboErik4646d282014-05-13 10:13:04 -0700466 * 2. It needs to be added to all sessions.
467 * 3. It needs to be added to the priority stack.
468 * 4. It needs to be added to the relevant user record.
469 */
RoboErika5b02322014-05-07 17:05:49 -0700470 private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId,
471 String callerPackageName, ISessionCallback cb, String tag) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900472 FullUserRecord user = getFullUserRecordLocked(userId);
Dongwon Kang8cf39c52016-07-29 13:20:39 -0700473 if (user == null) {
474 Log.wtf(TAG, "Request from invalid user: " + userId);
475 throw new RuntimeException("Session request from invalid user.");
476 }
477
RoboErika5b02322014-05-07 17:05:49 -0700478 final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId,
Jaewan Kim92dea332017-02-02 11:52:08 +0900479 callerPackageName, cb, tag, this, mHandler.getLooper());
RoboErik01fe6612014-02-13 14:19:04 -0800480 try {
481 cb.asBinder().linkToDeath(session, 0);
482 } catch (RemoteException e) {
483 throw new RuntimeException("Media Session owner died prematurely.", e);
484 }
RoboErik4646d282014-05-13 10:13:04 -0700485
RoboErik4646d282014-05-13 10:13:04 -0700486 user.addSessionLocked(session);
Jaewan Kim92dea332017-02-02 11:52:08 +0900487 mHandler.postSessionsChanged(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700488
RoboErik01fe6612014-02-13 14:19:04 -0800489 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900490 Log.d(TAG, "Created session for " + callerPackageName + " with tag " + tag);
RoboErik01fe6612014-02-13 14:19:04 -0800491 }
492 return session;
493 }
494
RoboErik2e7a9162014-06-04 16:53:45 -0700495 private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) {
496 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
RoboErika08adb242014-11-21 18:28:18 -0800497 if (mSessionsListeners.get(i).mListener.asBinder() == listener.asBinder()) {
RoboErik2e7a9162014-06-04 16:53:45 -0700498 return i;
499 }
500 }
501 return -1;
502 }
503
RoboErik2e7a9162014-06-04 16:53:45 -0700504 private void pushSessionsChanged(int userId) {
505 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900506 FullUserRecord user = getFullUserRecordLocked(userId);
507 if (user == null) {
508 Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId);
509 return;
510 }
511 List<MediaSessionRecord> records = user.mPriorityStack.getActiveSessions(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700512 int size = records.size();
Jeff Browndba34ba2014-06-24 20:46:03 -0700513 ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>();
RoboErik2e7a9162014-06-04 16:53:45 -0700514 for (int i = 0; i < size; i++) {
Jeff Browndba34ba2014-06-24 20:46:03 -0700515 tokens.add(new MediaSession.Token(records.get(i).getControllerBinder()));
RoboErik2e7a9162014-06-04 16:53:45 -0700516 }
RoboErik19c95182014-06-23 15:38:48 -0700517 pushRemoteVolumeUpdateLocked(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700518 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
519 SessionsListenerRecord record = mSessionsListeners.get(i);
520 if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) {
521 try {
522 record.mListener.onActiveSessionsChanged(tokens);
523 } catch (RemoteException e) {
524 Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing",
525 e);
526 mSessionsListeners.remove(i);
527 }
528 }
529 }
530 }
531 }
532
RoboErik19c95182014-06-23 15:38:48 -0700533 private void pushRemoteVolumeUpdateLocked(int userId) {
534 if (mRvc != null) {
535 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900536 FullUserRecord user = getFullUserRecordLocked(userId);
537 if (user == null) {
538 Log.w(TAG, "pushRemoteVolumeUpdateLocked failed. No user with id=" + userId);
539 return;
540 }
541 MediaSessionRecord record = user.mPriorityStack.getDefaultRemoteSession(userId);
RoboErik19c95182014-06-23 15:38:48 -0700542 mRvc.updateRemoteController(record == null ? null : record.getControllerBinder());
543 } catch (RemoteException e) {
544 Log.wtf(TAG, "Error sending default remote volume to sys ui.", e);
545 }
546 }
547 }
548
Jaewan Kim92dea332017-02-02 11:52:08 +0900549 /**
550 * Called when the media button receiver for the {@param record} is changed.
551 *
552 * @param record the media session whose media button receiver is updated.
553 */
554 public void onMediaButtonReceiverChanged(MediaSessionRecord record) {
555 synchronized (mLock) {
556 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
557 MediaSessionRecord mediaButtonSession =
558 user.mPriorityStack.getMediaButtonSession();
559 if (record == mediaButtonSession) {
560 user.rememberMediaButtonReceiverLocked(mediaButtonSession);
561 }
562 }
563 }
564
Jaewan Kim50269362016-12-23 11:22:02 +0900565 private String getCallingPackageName(int uid) {
566 String[] packages = getContext().getPackageManager().getPackagesForUid(uid);
567 if (packages != null && packages.length > 0) {
568 return packages[0];
569 }
570 return "";
571 }
572
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900573 private void dispatchVolumeKeyLongPressLocked(KeyEvent keyEvent) {
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900574 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900575 mCurrentFullUserRecord.mOnVolumeKeyLongPressListener.onVolumeKeyLongPress(keyEvent);
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900576 } catch (RemoteException e) {
577 Log.w(TAG, "Failed to send " + keyEvent + " to volume key long-press listener");
578 }
579 }
580
Jaewan Kima7dce192017-02-16 17:10:54 +0900581 private FullUserRecord getFullUserRecordLocked(int userId) {
582 int fullUserId = mFullUserIds.get(userId, -1);
583 if (fullUserId < 0) {
584 return null;
585 }
586 return mUserRecords.get(fullUserId);
587 }
588
RoboErik4646d282014-05-13 10:13:04 -0700589 /**
Jaewan Kima7dce192017-02-16 17:10:54 +0900590 * Information about a full user and its corresponding managed profiles.
591 *
592 * <p>Since the full user runs together with its managed profiles, a user wouldn't differentiate
593 * them when he/she presses a media/volume button. So keeping media sessions for them in one
594 * place makes more sense and increases the readability.</p>
595 * <p>The contents of this object is guarded by {@link #mLock}.
RoboErik4646d282014-05-13 10:13:04 -0700596 */
Jaewan Kim92dea332017-02-02 11:52:08 +0900597 final class FullUserRecord implements MediaSessionStack.OnMediaButtonSessionChangedListener {
Jaewan Kima7dce192017-02-16 17:10:54 +0900598 private static final String COMPONENT_NAME_USER_ID_DELIM = ",";
599 private final int mFullUserId;
Jaewan Kim92dea332017-02-02 11:52:08 +0900600 private final MediaSessionStack mPriorityStack;
RoboErikb214efb2014-07-24 13:20:30 -0700601 private PendingIntent mLastMediaButtonReceiver;
RoboErikc8f92d12015-01-05 16:48:07 -0800602 private ComponentName mRestoredMediaButtonReceiver;
Jaewan Kima7dce192017-02-16 17:10:54 +0900603 private int mRestoredMediaButtonReceiverUserId;
RoboErik4646d282014-05-13 10:13:04 -0700604
Jaewan Kim50269362016-12-23 11:22:02 +0900605 private IOnVolumeKeyLongPressListener mOnVolumeKeyLongPressListener;
606 private int mOnVolumeKeyLongPressListenerUid;
607 private KeyEvent mInitialDownVolumeKeyEvent;
608 private int mInitialDownVolumeStream;
609 private boolean mInitialDownMusicOnly;
610
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800611 private IOnMediaKeyListener mOnMediaKeyListener;
612 private int mOnMediaKeyListenerUid;
Jaewan Kima7dce192017-02-16 17:10:54 +0900613 private ICallback mCallback;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800614
Jaewan Kima7dce192017-02-16 17:10:54 +0900615 public FullUserRecord(int fullUserId) {
616 mFullUserId = fullUserId;
Jaewan Kim92dea332017-02-02 11:52:08 +0900617 mPriorityStack = new MediaSessionStack(mAudioPlaybackMonitor, this);
Jaewan Kima7dce192017-02-16 17:10:54 +0900618 // Restore the remembered media button receiver before the boot.
619 String mediaButtonReceiver = Settings.Secure.getStringForUser(mContentResolver,
620 Settings.System.MEDIA_BUTTON_RECEIVER, mFullUserId);
621 if (mediaButtonReceiver == null) {
622 return;
623 }
624 String[] tokens = mediaButtonReceiver.split(COMPONENT_NAME_USER_ID_DELIM);
625 if (tokens == null || tokens.length != 2) {
626 return;
627 }
628 mRestoredMediaButtonReceiver = ComponentName.unflattenFromString(tokens[0]);
629 mRestoredMediaButtonReceiverUserId = Integer.parseInt(tokens[1]);
RoboErik4646d282014-05-13 10:13:04 -0700630 }
631
Jaewan Kima7dce192017-02-16 17:10:54 +0900632 public void destroySessionsForUserLocked(int userId) {
Jaewan Kim92dea332017-02-02 11:52:08 +0900633 List<MediaSessionRecord> sessions = mPriorityStack.getPriorityList(false, userId);
Jaewan Kima7dce192017-02-16 17:10:54 +0900634 for (MediaSessionRecord session : sessions) {
RoboErik4646d282014-05-13 10:13:04 -0700635 MediaSessionService.this.destroySessionLocked(session);
RoboErik4646d282014-05-13 10:13:04 -0700636 }
637 }
638
RoboErik4646d282014-05-13 10:13:04 -0700639 public void addSessionLocked(MediaSessionRecord session) {
Jaewan Kim92dea332017-02-02 11:52:08 +0900640 mPriorityStack.addSession(session);
RoboErik4646d282014-05-13 10:13:04 -0700641 }
642
643 public void removeSessionLocked(MediaSessionRecord session) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900644 mPriorityStack.removeSession(session);
RoboErik4646d282014-05-13 10:13:04 -0700645 }
646
647 public void dumpLocked(PrintWriter pw, String prefix) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900648 pw.print(prefix + "Record for full_user=" + mFullUserId);
649 // Dump managed profile user ids associated with this user.
650 int size = mFullUserIds.size();
651 for (int i = 0; i < size; i++) {
652 if (mFullUserIds.keyAt(i) != mFullUserIds.valueAt(i)
653 && mFullUserIds.valueAt(i) == mFullUserId) {
654 pw.print(", profile_user=" + mFullUserIds.keyAt(i));
655 }
656 }
657 pw.println();
RoboErik4646d282014-05-13 10:13:04 -0700658 String indent = prefix + " ";
Jaewan Kima7dce192017-02-16 17:10:54 +0900659 pw.println(indent + "Volume key long-press listener: " + mOnVolumeKeyLongPressListener);
660 pw.println(indent + "Volume key long-press listener package: " +
Jaewan Kim50269362016-12-23 11:22:02 +0900661 getCallingPackageName(mOnVolumeKeyLongPressListenerUid));
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800662 pw.println(indent + "Media key listener: " + mOnMediaKeyListener);
663 pw.println(indent + "Media key listener package: " +
664 getCallingPackageName(mOnMediaKeyListenerUid));
Jaewan Kima7dce192017-02-16 17:10:54 +0900665 pw.println(indent + "Callback: " + mCallback);
666 pw.println(indent + "Last MediaButtonReceiver: " + mLastMediaButtonReceiver);
667 pw.println(indent + "Restored MediaButtonReceiver: " + mRestoredMediaButtonReceiver);
668 mPriorityStack.dump(pw, indent);
669 }
670
Jaewan Kim92dea332017-02-02 11:52:08 +0900671 @Override
672 public void onMediaButtonSessionChanged(MediaSessionRecord oldMediaButtonSession,
673 MediaSessionRecord newMediaButtonSession) {
674 if (DEBUG_KEY_EVENT) {
675 Log.d(TAG, "Media button session will be changed to " + newMediaButtonSession);
676 }
677 synchronized (mLock) {
678 if (oldMediaButtonSession != null) {
679 mHandler.postSessionsChanged(oldMediaButtonSession.getUserId());
680 }
681 if (newMediaButtonSession != null) {
682 rememberMediaButtonReceiverLocked(newMediaButtonSession);
683 mHandler.postSessionsChanged(newMediaButtonSession.getUserId());
684 }
685 pushAddressedPlayerChangedLocked();
686 }
687 }
688
689 // Remember media button receiver and keep it in the persistent storage.
690 public void rememberMediaButtonReceiverLocked(MediaSessionRecord record) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900691 PendingIntent receiver = record.getMediaButtonReceiver();
Jaewan Kima7dce192017-02-16 17:10:54 +0900692 mLastMediaButtonReceiver = receiver;
Jaewan Kim92dea332017-02-02 11:52:08 +0900693 mRestoredMediaButtonReceiver = null;
694 String componentName = "";
695 if (receiver != null) {
696 ComponentName component = receiver.getIntent().getComponent();
697 if (component != null
698 && record.getPackageName().equals(component.getPackageName())) {
699 componentName = component.flattenToString();
700 }
RoboErik4646d282014-05-13 10:13:04 -0700701 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900702 Settings.Secure.putStringForUser(mContentResolver,
703 Settings.System.MEDIA_BUTTON_RECEIVER,
704 componentName + COMPONENT_NAME_USER_ID_DELIM + record.getUserId(),
705 mFullUserId);
RoboErik4646d282014-05-13 10:13:04 -0700706 }
RoboErikc8f92d12015-01-05 16:48:07 -0800707
Jaewan Kima7dce192017-02-16 17:10:54 +0900708 private void pushAddressedPlayerChangedLocked() {
709 if (mCallback == null) {
710 return;
RoboErikc8f92d12015-01-05 16:48:07 -0800711 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900712 try {
713 MediaSessionRecord mediaButtonSession = getMediaButtonSessionLocked();
714 if (mediaButtonSession != null) {
715 mCallback.onAddressedPlayerChangedToMediaSession(
716 new MediaSession.Token(mediaButtonSession.getControllerBinder()));
717 } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) {
718 mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
719 mCurrentFullUserRecord.mLastMediaButtonReceiver
720 .getIntent().getComponent());
721 } else if (mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) {
722 mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
723 mCurrentFullUserRecord.mRestoredMediaButtonReceiver);
724 }
725 } catch (RemoteException e) {
726 Log.w(TAG, "Failed to pushAddressedPlayerChangedLocked", e);
727 }
728 }
729
730 private MediaSessionRecord getMediaButtonSessionLocked() {
Jaewan Kim92dea332017-02-02 11:52:08 +0900731 return isGlobalPriorityActiveLocked()
732 ? mGlobalPrioritySession : mPriorityStack.getMediaButtonSession();
RoboErikc8f92d12015-01-05 16:48:07 -0800733 }
RoboErik4646d282014-05-13 10:13:04 -0700734 }
735
RoboErik2e7a9162014-06-04 16:53:45 -0700736 final class SessionsListenerRecord implements IBinder.DeathRecipient {
737 private final IActiveSessionsListener mListener;
RoboErik7aef77b2014-08-08 15:56:54 -0700738 private final ComponentName mComponentName;
RoboErik2e7a9162014-06-04 16:53:45 -0700739 private final int mUserId;
RoboErik7aef77b2014-08-08 15:56:54 -0700740 private final int mPid;
741 private final int mUid;
RoboErik2e7a9162014-06-04 16:53:45 -0700742
RoboErik7aef77b2014-08-08 15:56:54 -0700743 public SessionsListenerRecord(IActiveSessionsListener listener,
744 ComponentName componentName,
745 int userId, int pid, int uid) {
RoboErik2e7a9162014-06-04 16:53:45 -0700746 mListener = listener;
RoboErik7aef77b2014-08-08 15:56:54 -0700747 mComponentName = componentName;
RoboErik2e7a9162014-06-04 16:53:45 -0700748 mUserId = userId;
RoboErik7aef77b2014-08-08 15:56:54 -0700749 mPid = pid;
750 mUid = uid;
RoboErik2e7a9162014-06-04 16:53:45 -0700751 }
752
753 @Override
754 public void binderDied() {
755 synchronized (mLock) {
756 mSessionsListeners.remove(this);
757 }
758 }
759 }
760
RoboErik7aef77b2014-08-08 15:56:54 -0700761 final class SettingsObserver extends ContentObserver {
762 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(
763 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
764
765 private SettingsObserver() {
766 super(null);
767 }
768
769 private void observe() {
770 mContentResolver.registerContentObserver(mSecureSettingsUri,
771 false, this, UserHandle.USER_ALL);
772 }
773
774 @Override
775 public void onChange(boolean selfChange, Uri uri) {
776 updateActiveSessionListeners();
777 }
778 }
779
RoboErik07c70772014-03-20 13:33:52 -0700780 class SessionManagerImpl extends ISessionManager.Stub {
RoboErik8a2cfc32014-05-16 11:19:38 -0700781 private static final String EXTRA_WAKELOCK_ACQUIRED =
782 "android.media.AudioService.WAKELOCK_ACQUIRED";
783 private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number
784
RoboErik9a9d0b52014-05-20 14:53:39 -0700785 private boolean mVoiceButtonDown = false;
786 private boolean mVoiceButtonHandled = false;
787
RoboErik07c70772014-03-20 13:33:52 -0700788 @Override
RoboErika5b02322014-05-07 17:05:49 -0700789 public ISession createSession(String packageName, ISessionCallback cb, String tag,
790 int userId) throws RemoteException {
RoboErik01fe6612014-02-13 14:19:04 -0800791 final int pid = Binder.getCallingPid();
792 final int uid = Binder.getCallingUid();
793 final long token = Binder.clearCallingIdentity();
794 try {
795 enforcePackageName(packageName, uid);
RoboErika5b02322014-05-07 17:05:49 -0700796 int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
797 false /* allowAll */, true /* requireFull */, "createSession", packageName);
RoboErik01fe6612014-02-13 14:19:04 -0800798 if (cb == null) {
799 throw new IllegalArgumentException("Controller callback cannot be null");
800 }
RoboErika5b02322014-05-07 17:05:49 -0700801 return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag)
802 .getSessionBinder();
RoboErike7880d82014-04-30 12:48:25 -0700803 } finally {
804 Binder.restoreCallingIdentity(token);
805 }
806 }
807
808 @Override
RoboErika5b02322014-05-07 17:05:49 -0700809 public List<IBinder> getSessions(ComponentName componentName, int userId) {
RoboErike7880d82014-04-30 12:48:25 -0700810 final int pid = Binder.getCallingPid();
811 final int uid = Binder.getCallingUid();
812 final long token = Binder.clearCallingIdentity();
813
814 try {
RoboErik2e7a9162014-06-04 16:53:45 -0700815 int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid);
RoboErike7880d82014-04-30 12:48:25 -0700816 ArrayList<IBinder> binders = new ArrayList<IBinder>();
817 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900818 if (resolvedUserId == UserHandle.USER_ALL) {
819 int size = mUserRecords.size();
820 for (int i = 0; i < size; i++) {
821 List<MediaSessionRecord> records =
822 mUserRecords.valueAt(i).mPriorityStack.getActiveSessions(
823 resolvedUserId);
824 for (MediaSessionRecord record : records) {
825 binders.add(record.getControllerBinder().asBinder());
826 }
827 }
828 } else {
829 FullUserRecord user = getFullUserRecordLocked(resolvedUserId);
830 if (user == null) {
831 Log.w(TAG, "getSessions failed. Unknown user " + userId);
832 return binders;
833 }
834 List<MediaSessionRecord> records = user.mPriorityStack
835 .getActiveSessions(resolvedUserId);
836 for (MediaSessionRecord record : records) {
837 binders.add(record.getControllerBinder().asBinder());
838 }
RoboErike7880d82014-04-30 12:48:25 -0700839 }
840 }
841 return binders;
RoboErik01fe6612014-02-13 14:19:04 -0800842 } finally {
843 Binder.restoreCallingIdentity(token);
844 }
845 }
RoboErika278ea72014-04-24 14:49:01 -0700846
RoboErik2e7a9162014-06-04 16:53:45 -0700847 @Override
848 public void addSessionsListener(IActiveSessionsListener listener,
849 ComponentName componentName, int userId) throws RemoteException {
850 final int pid = Binder.getCallingPid();
851 final int uid = Binder.getCallingUid();
852 final long token = Binder.clearCallingIdentity();
853
854 try {
855 int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid);
856 synchronized (mLock) {
857 int index = findIndexOfSessionsListenerLocked(listener);
858 if (index != -1) {
859 Log.w(TAG, "ActiveSessionsListener is already added, ignoring");
860 return;
861 }
862 SessionsListenerRecord record = new SessionsListenerRecord(listener,
RoboErik7aef77b2014-08-08 15:56:54 -0700863 componentName, resolvedUserId, pid, uid);
RoboErik2e7a9162014-06-04 16:53:45 -0700864 try {
865 listener.asBinder().linkToDeath(record, 0);
866 } catch (RemoteException e) {
867 Log.e(TAG, "ActiveSessionsListener is dead, ignoring it", e);
868 return;
869 }
870 mSessionsListeners.add(record);
871 }
872 } finally {
873 Binder.restoreCallingIdentity(token);
874 }
875 }
876
877 @Override
878 public void removeSessionsListener(IActiveSessionsListener listener)
879 throws RemoteException {
880 synchronized (mLock) {
881 int index = findIndexOfSessionsListenerLocked(listener);
882 if (index != -1) {
883 SessionsListenerRecord record = mSessionsListeners.remove(index);
884 try {
885 record.mListener.asBinder().unlinkToDeath(record, 0);
886 } catch (Exception e) {
887 // ignore exceptions, the record is being removed
888 }
889 }
890 }
891 }
892
RoboErik8a2cfc32014-05-16 11:19:38 -0700893 /**
894 * Handles the dispatching of the media button events to one of the
895 * registered listeners, or if there was none, broadcast an
896 * ACTION_MEDIA_BUTTON intent to the rest of the system.
897 *
898 * @param keyEvent a non-null KeyEvent whose key code is one of the
899 * supported media buttons
900 * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held
901 * while this key event is dispatched.
902 */
903 @Override
904 public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
905 if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) {
906 Log.w(TAG, "Attempted to dispatch null or non-media key event.");
907 return;
908 }
Jeff Brown38d3feb2015-03-19 18:26:30 -0700909
RoboErik8a2cfc32014-05-16 11:19:38 -0700910 final int pid = Binder.getCallingPid();
911 final int uid = Binder.getCallingUid();
912 final long token = Binder.clearCallingIdentity();
RoboErik8a2cfc32014-05-16 11:19:38 -0700913 try {
Jeff Brown221a8272015-03-23 13:53:09 -0700914 if (DEBUG) {
915 Log.d(TAG, "dispatchMediaKeyEvent, pid=" + pid + ", uid=" + uid + ", event="
916 + keyEvent);
917 }
Jeff Brown38d3feb2015-03-19 18:26:30 -0700918 if (!isUserSetupComplete()) {
919 // Global media key handling can have the side-effect of starting new
920 // activities which is undesirable while setup is in progress.
921 Slog.i(TAG, "Not dispatching media key event because user "
922 + "setup is in progress.");
923 return;
924 }
925
RoboErik8a2cfc32014-05-16 11:19:38 -0700926 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900927 boolean isGlobalPriorityActive = isGlobalPriorityActiveLocked();
Jaewan Kim51255012017-02-24 16:19:14 +0900928 if (isGlobalPriorityActive && uid != Process.SYSTEM_UID) {
929 // Prevent dispatching key event through reflection while the global
930 // priority session is active.
931 Slog.i(TAG, "Only the system can dispatch media key event "
932 + "to the global priority session.");
933 return;
934 }
Jaewan Kim98003d32017-02-24 18:33:04 +0900935 if (!isGlobalPriorityActive) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900936 if (mCurrentFullUserRecord.mOnMediaKeyListener != null) {
Jaewan Kim98003d32017-02-24 18:33:04 +0900937 if (DEBUG_KEY_EVENT) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900938 Log.d(TAG, "Send " + keyEvent + " to the media key listener");
Jaewan Kim98003d32017-02-24 18:33:04 +0900939 }
940 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900941 mCurrentFullUserRecord.mOnMediaKeyListener.onMediaKey(keyEvent,
Jaewan Kim98003d32017-02-24 18:33:04 +0900942 new MediaKeyListenerResultReceiver(keyEvent, needWakeLock));
943 return;
944 } catch (RemoteException e) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900945 Log.w(TAG, "Failed to send " + keyEvent
946 + " to the media key listener");
Jaewan Kim98003d32017-02-24 18:33:04 +0900947 }
948 }
949 }
Jaewan Kim51255012017-02-24 16:19:14 +0900950 if (!isGlobalPriorityActive && isVoiceKey(keyEvent.getKeyCode())) {
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800951 handleVoiceKeyEventLocked(keyEvent, needWakeLock);
RoboErik8a2cfc32014-05-16 11:19:38 -0700952 } else {
Jaewan Kim98003d32017-02-24 18:33:04 +0900953 dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
RoboErik8a2cfc32014-05-16 11:19:38 -0700954 }
955 }
956 } finally {
957 Binder.restoreCallingIdentity(token);
958 }
959 }
960
RoboErika278ea72014-04-24 14:49:01 -0700961 @Override
Jaewan Kimbd16f452017-02-03 16:21:38 +0900962 public void setCallback(ICallback callback) {
963 final int pid = Binder.getCallingPid();
964 final int uid = Binder.getCallingUid();
965 final long token = Binder.clearCallingIdentity();
966 try {
967 if (uid != Process.BLUETOOTH_UID) {
968 throw new SecurityException("Only Bluetooth service processes can set"
969 + " Callback");
970 }
971 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900972 int userId = UserHandle.getUserId(uid);
973 FullUserRecord user = getFullUserRecordLocked(userId);
974 if (user == null || user.mFullUserId != userId) {
975 Log.w(TAG, "Only the full user can set the callback"
976 + ", userId=" + userId);
977 return;
978 }
979 user.mCallback = callback;
980 Log.d(TAG, "The callback " + user.mCallback
Jaewan Kimbd16f452017-02-03 16:21:38 +0900981 + " is set by " + getCallingPackageName(uid));
Jaewan Kima7dce192017-02-16 17:10:54 +0900982 if (user.mCallback == null) {
Jaewan Kimbd16f452017-02-03 16:21:38 +0900983 return;
984 }
985 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900986 user.mCallback.asBinder().linkToDeath(
Jaewan Kimbd16f452017-02-03 16:21:38 +0900987 new IBinder.DeathRecipient() {
988 @Override
989 public void binderDied() {
990 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900991 user.mCallback = null;
Jaewan Kimbd16f452017-02-03 16:21:38 +0900992 }
993 }
994 }, 0);
Jaewan Kima7dce192017-02-16 17:10:54 +0900995 user.pushAddressedPlayerChangedLocked();
Jaewan Kimbd16f452017-02-03 16:21:38 +0900996 } catch (RemoteException e) {
997 Log.w(TAG, "Failed to set callback", e);
Jaewan Kima7dce192017-02-16 17:10:54 +0900998 user.mCallback = null;
Jaewan Kimbd16f452017-02-03 16:21:38 +0900999 }
1000 }
1001 } finally {
1002 Binder.restoreCallingIdentity(token);
1003 }
1004 }
1005
1006 @Override
Jaewan Kim50269362016-12-23 11:22:02 +09001007 public void setOnVolumeKeyLongPressListener(IOnVolumeKeyLongPressListener listener) {
1008 final int pid = Binder.getCallingPid();
1009 final int uid = Binder.getCallingUid();
1010 final long token = Binder.clearCallingIdentity();
1011 try {
1012 // Enforce SET_VOLUME_KEY_LONG_PRESS_LISTENER permission.
1013 if (getContext().checkPermission(
1014 android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER, pid, uid)
1015 != PackageManager.PERMISSION_GRANTED) {
1016 throw new SecurityException("Must hold the SET_VOLUME_KEY_LONG_PRESS_LISTENER" +
1017 " permission.");
1018 }
1019
1020 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001021 int userId = UserHandle.getUserId(uid);
1022 FullUserRecord user = getFullUserRecordLocked(userId);
1023 if (user == null || user.mFullUserId != userId) {
1024 Log.w(TAG, "Only the full user can set the volume key long-press listener"
1025 + ", userId=" + userId);
1026 return;
1027 }
Jaewan Kim50269362016-12-23 11:22:02 +09001028 if (user.mOnVolumeKeyLongPressListener != null &&
1029 user.mOnVolumeKeyLongPressListenerUid != uid) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001030 Log.w(TAG, "The volume key long-press listener cannot be reset"
1031 + " by another app , mOnVolumeKeyLongPressListener="
1032 + user.mOnVolumeKeyLongPressListenerUid
1033 + ", uid=" + uid);
Jaewan Kim50269362016-12-23 11:22:02 +09001034 return;
1035 }
1036
1037 user.mOnVolumeKeyLongPressListener = listener;
1038 user.mOnVolumeKeyLongPressListenerUid = uid;
1039
Jaewan Kima7dce192017-02-16 17:10:54 +09001040 Log.d(TAG, "The volume key long-press listener "
Jaewan Kim50269362016-12-23 11:22:02 +09001041 + listener + " is set by " + getCallingPackageName(uid));
1042
1043 if (user.mOnVolumeKeyLongPressListener != null) {
1044 try {
1045 user.mOnVolumeKeyLongPressListener.asBinder().linkToDeath(
1046 new IBinder.DeathRecipient() {
1047 @Override
1048 public void binderDied() {
1049 synchronized (mLock) {
1050 user.mOnVolumeKeyLongPressListener = null;
1051 }
1052 }
1053 }, 0);
1054 } catch (RemoteException e) {
1055 Log.w(TAG, "Failed to set death recipient "
1056 + user.mOnVolumeKeyLongPressListener);
1057 user.mOnVolumeKeyLongPressListener = null;
1058 }
1059 }
1060 }
1061 } finally {
1062 Binder.restoreCallingIdentity(token);
1063 }
1064 }
1065
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001066 @Override
1067 public void setOnMediaKeyListener(IOnMediaKeyListener listener) {
1068 final int pid = Binder.getCallingPid();
1069 final int uid = Binder.getCallingUid();
1070 final long token = Binder.clearCallingIdentity();
1071 try {
1072 // Enforce SET_MEDIA_KEY_LISTENER permission.
1073 if (getContext().checkPermission(
1074 android.Manifest.permission.SET_MEDIA_KEY_LISTENER, pid, uid)
1075 != PackageManager.PERMISSION_GRANTED) {
1076 throw new SecurityException("Must hold the SET_MEDIA_KEY_LISTENER" +
1077 " permission.");
1078 }
1079
1080 synchronized (mLock) {
1081 int userId = UserHandle.getUserId(uid);
Jaewan Kima7dce192017-02-16 17:10:54 +09001082 FullUserRecord user = getFullUserRecordLocked(userId);
1083 if (user == null || user.mFullUserId != userId) {
1084 Log.w(TAG, "Only the full user can set the media key listener"
1085 + ", userId=" + userId);
1086 return;
1087 }
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001088 if (user.mOnMediaKeyListener != null && user.mOnMediaKeyListenerUid != uid) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001089 Log.w(TAG, "The media key listener cannot be reset by another app. "
1090 + ", mOnMediaKeyListenerUid=" + user.mOnMediaKeyListenerUid
1091 + ", uid=" + uid);
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001092 return;
1093 }
1094
1095 user.mOnMediaKeyListener = listener;
1096 user.mOnMediaKeyListenerUid = uid;
1097
Jaewan Kima7dce192017-02-16 17:10:54 +09001098 Log.d(TAG, "The media key listener " + user.mOnMediaKeyListener
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001099 + " is set by " + getCallingPackageName(uid));
1100
1101 if (user.mOnMediaKeyListener != null) {
1102 try {
1103 user.mOnMediaKeyListener.asBinder().linkToDeath(
1104 new IBinder.DeathRecipient() {
1105 @Override
1106 public void binderDied() {
1107 synchronized (mLock) {
1108 user.mOnMediaKeyListener = null;
1109 }
1110 }
1111 }, 0);
1112 } catch (RemoteException e) {
1113 Log.w(TAG, "Failed to set death recipient " + user.mOnMediaKeyListener);
1114 user.mOnMediaKeyListener = null;
1115 }
1116 }
1117 }
1118 } finally {
1119 Binder.restoreCallingIdentity(token);
1120 }
1121 }
1122
Jaewan Kim50269362016-12-23 11:22:02 +09001123 /**
1124 * Handles the dispatching of the volume button events to one of the
1125 * registered listeners. If there's a volume key long-press listener and
1126 * there's no active global priority session, long-pressess will be sent to the
1127 * long-press listener instead of adjusting volume.
1128 *
1129 * @param keyEvent a non-null KeyEvent whose key code is one of the
1130 * {@link KeyEvent#KEYCODE_VOLUME_UP},
1131 * {@link KeyEvent#KEYCODE_VOLUME_DOWN},
1132 * or {@link KeyEvent#KEYCODE_VOLUME_MUTE}.
1133 * @param stream stream type to adjust volume.
1134 * @param musicOnly true if both UI nor haptic feedback aren't needed when adjust volume.
1135 */
1136 @Override
1137 public void dispatchVolumeKeyEvent(KeyEvent keyEvent, int stream, boolean musicOnly) {
1138 if (keyEvent == null ||
1139 (keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP
1140 && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN
1141 && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_MUTE)) {
1142 Log.w(TAG, "Attempted to dispatch null or non-volume key event.");
1143 return;
1144 }
1145
1146 final int pid = Binder.getCallingPid();
1147 final int uid = Binder.getCallingUid();
1148 final long token = Binder.clearCallingIdentity();
1149
Jaewan Kimb2781e72017-03-02 09:57:09 +09001150 if (DEBUG_KEY_EVENT) {
Jaewan Kim50269362016-12-23 11:22:02 +09001151 Log.d(TAG, "dispatchVolumeKeyEvent, pid=" + pid + ", uid=" + uid + ", event="
1152 + keyEvent);
1153 }
1154
1155 try {
1156 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001157 if (isGlobalPriorityActiveLocked()
1158 || mCurrentFullUserRecord.mOnVolumeKeyLongPressListener == null) {
Jaewan Kim50269362016-12-23 11:22:02 +09001159 dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly);
1160 } else {
1161 // TODO: Consider the case when both volume up and down keys are pressed
1162 // at the same time.
1163 if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
1164 if (keyEvent.getRepeatCount() == 0) {
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001165 // Keeps the copy of the KeyEvent because it can be reused.
Jaewan Kima7dce192017-02-16 17:10:54 +09001166 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent =
1167 KeyEvent.obtain(keyEvent);
1168 mCurrentFullUserRecord.mInitialDownVolumeStream = stream;
1169 mCurrentFullUserRecord.mInitialDownMusicOnly = musicOnly;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001170 mHandler.sendMessageDelayed(
1171 mHandler.obtainMessage(
Jaewan Kima7dce192017-02-16 17:10:54 +09001172 MessageHandler.MSG_VOLUME_INITIAL_DOWN,
1173 mCurrentFullUserRecord.mFullUserId, 0),
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001174 mLongPressTimeout);
Jaewan Kim50269362016-12-23 11:22:02 +09001175 }
1176 if (keyEvent.getRepeatCount() > 0 || keyEvent.isLongPress()) {
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001177 mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN);
Jaewan Kima7dce192017-02-16 17:10:54 +09001178 if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null) {
Jaewan Kim50269362016-12-23 11:22:02 +09001179 dispatchVolumeKeyLongPressLocked(
Jaewan Kima7dce192017-02-16 17:10:54 +09001180 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent);
Jaewan Kim50269362016-12-23 11:22:02 +09001181 // Mark that the key is already handled.
Jaewan Kima7dce192017-02-16 17:10:54 +09001182 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = null;
Jaewan Kim50269362016-12-23 11:22:02 +09001183 }
1184 dispatchVolumeKeyLongPressLocked(keyEvent);
1185 }
1186 } else { // if up
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001187 mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN);
Jaewan Kima7dce192017-02-16 17:10:54 +09001188 if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null
1189 && mCurrentFullUserRecord.mInitialDownVolumeKeyEvent
1190 .getDownTime() == keyEvent.getDownTime()) {
Jaewan Kim50269362016-12-23 11:22:02 +09001191 // Short-press. Should change volume.
1192 dispatchVolumeKeyEventLocked(
Jaewan Kima7dce192017-02-16 17:10:54 +09001193 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent,
1194 mCurrentFullUserRecord.mInitialDownVolumeStream,
1195 mCurrentFullUserRecord.mInitialDownMusicOnly);
Jaewan Kim50269362016-12-23 11:22:02 +09001196 dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly);
1197 } else {
1198 dispatchVolumeKeyLongPressLocked(keyEvent);
1199 }
1200 }
1201 }
1202 }
1203 } finally {
1204 Binder.restoreCallingIdentity(token);
1205 }
1206 }
1207
Jaewan Kim50269362016-12-23 11:22:02 +09001208 private void dispatchVolumeKeyEventLocked(
1209 KeyEvent keyEvent, int stream, boolean musicOnly) {
1210 boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
1211 boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
1212 int direction = 0;
1213 boolean isMute = false;
1214 switch (keyEvent.getKeyCode()) {
1215 case KeyEvent.KEYCODE_VOLUME_UP:
1216 direction = AudioManager.ADJUST_RAISE;
1217 break;
1218 case KeyEvent.KEYCODE_VOLUME_DOWN:
1219 direction = AudioManager.ADJUST_LOWER;
1220 break;
1221 case KeyEvent.KEYCODE_VOLUME_MUTE:
1222 isMute = true;
1223 break;
1224 }
1225 if (down || up) {
1226 int flags = AudioManager.FLAG_FROM_KEY;
1227 if (musicOnly) {
1228 // This flag is used when the screen is off to only affect active media.
1229 flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
1230 } else {
1231 // These flags are consistent with the home screen
1232 if (up) {
1233 flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
1234 } else {
1235 flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
1236 }
1237 }
1238 if (direction != 0) {
1239 // If this is action up we want to send a beep for non-music events
1240 if (up) {
1241 direction = 0;
1242 }
1243 dispatchAdjustVolumeLocked(stream, direction, flags);
1244 } else if (isMute) {
1245 if (down && keyEvent.getRepeatCount() == 0) {
1246 dispatchAdjustVolumeLocked(stream, AudioManager.ADJUST_TOGGLE_MUTE, flags);
1247 }
1248 }
1249 }
1250 }
1251
1252 @Override
RoboErik7c82ced2014-12-04 17:39:08 -08001253 public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) {
RoboErikb69ffd42014-05-30 14:57:59 -07001254 final long token = Binder.clearCallingIdentity();
1255 try {
1256 synchronized (mLock) {
Jaewan Kim50269362016-12-23 11:22:02 +09001257 dispatchAdjustVolumeLocked(suggestedStream, delta, flags);
RoboErikb69ffd42014-05-30 14:57:59 -07001258 }
1259 } finally {
1260 Binder.restoreCallingIdentity(token);
1261 }
1262 }
1263
1264 @Override
RoboErik19c95182014-06-23 15:38:48 -07001265 public void setRemoteVolumeController(IRemoteVolumeController rvc) {
1266 final int pid = Binder.getCallingPid();
1267 final int uid = Binder.getCallingUid();
1268 final long token = Binder.clearCallingIdentity();
1269 try {
John Spurlockeb69e242015-02-17 17:15:04 -05001270 enforceSystemUiPermission("listen for volume changes", pid, uid);
RoboErik19c95182014-06-23 15:38:48 -07001271 mRvc = rvc;
1272 } finally {
1273 Binder.restoreCallingIdentity(token);
1274 }
1275 }
1276
1277 @Override
RoboErikde9ba392014-09-26 12:51:01 -07001278 public boolean isGlobalPriorityActive() {
Jaewan Kim51255012017-02-24 16:19:14 +09001279 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001280 return isGlobalPriorityActiveLocked();
Jaewan Kim51255012017-02-24 16:19:14 +09001281 }
RoboErikde9ba392014-09-26 12:51:01 -07001282 }
1283
1284 @Override
RoboErika278ea72014-04-24 14:49:01 -07001285 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1286 if (getContext().checkCallingOrSelfPermission(Manifest.permission.DUMP)
1287 != PackageManager.PERMISSION_GRANTED) {
1288 pw.println("Permission Denial: can't dump MediaSessionService from from pid="
1289 + Binder.getCallingPid()
1290 + ", uid=" + Binder.getCallingUid());
1291 return;
1292 }
1293
1294 pw.println("MEDIA SESSION SERVICE (dumpsys media_session)");
1295 pw.println();
1296
1297 synchronized (mLock) {
RoboErika08adb242014-11-21 18:28:18 -08001298 pw.println(mSessionsListeners.size() + " sessions listeners.");
Jaewan Kima7dce192017-02-16 17:10:54 +09001299 pw.println("Global priority session is " + mGlobalPrioritySession);
RoboErik4646d282014-05-13 10:13:04 -07001300 pw.println("User Records:");
Jaewan Kime0ca3f32017-02-16 15:52:39 +09001301 int count = mUserRecords.size();
RoboErika278ea72014-04-24 14:49:01 -07001302 for (int i = 0; i < count; i++) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001303 mUserRecords.valueAt(i).dumpLocked(pw, "");
RoboErika278ea72014-04-24 14:49:01 -07001304 }
Jaewan Kim92dea332017-02-02 11:52:08 +09001305 mAudioPlaybackMonitor.dump(pw, "");
RoboErika278ea72014-04-24 14:49:01 -07001306 }
1307 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001308
RoboErik2e7a9162014-06-04 16:53:45 -07001309 private int verifySessionsRequest(ComponentName componentName, int userId, final int pid,
1310 final int uid) {
1311 String packageName = null;
1312 if (componentName != null) {
1313 // If they gave us a component name verify they own the
1314 // package
1315 packageName = componentName.getPackageName();
1316 enforcePackageName(packageName, uid);
1317 }
1318 // Check that they can make calls on behalf of the user and
1319 // get the final user id
1320 int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
1321 true /* allowAll */, true /* requireFull */, "getSessions", packageName);
1322 // Check if they have the permissions or their component is
1323 // enabled for the user they're calling from.
1324 enforceMediaPermissions(componentName, pid, uid, resolvedUserId);
1325 return resolvedUserId;
1326 }
1327
Jaewan Kim50269362016-12-23 11:22:02 +09001328 private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001329 MediaSessionRecord session = isGlobalPriorityActiveLocked() ? mGlobalPrioritySession
1330 : mCurrentFullUserRecord.mPriorityStack.getDefaultVolumeSession();
Jaewan Kim50269362016-12-23 11:22:02 +09001331
RoboErik9c785402014-11-11 16:52:26 -08001332 boolean preferSuggestedStream = false;
1333 if (isValidLocalStreamType(suggestedStream)
1334 && AudioSystem.isStreamActive(suggestedStream, 0)) {
1335 preferSuggestedStream = true;
1336 }
Jaewan Kimb2781e72017-03-02 09:57:09 +09001337 if (DEBUG_KEY_EVENT) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +09001338 Log.d(TAG, "Adjusting " + session + " by " + direction + ". flags="
1339 + flags + ", suggestedStream=" + suggestedStream
1340 + ", preferSuggestedStream=" + preferSuggestedStream);
1341 }
RoboErik9c785402014-11-11 16:52:26 -08001342 if (session == null || preferSuggestedStream) {
RoboErik94c716e2014-09-14 13:54:31 -07001343 if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0
1344 && !AudioSystem.isStreamActive(AudioManager.STREAM_MUSIC, 0)) {
RoboErik3c45c292014-07-08 16:47:31 -07001345 if (DEBUG) {
1346 Log.d(TAG, "No active session to adjust, skipping media only volume event");
RoboErik3c45c292014-07-08 16:47:31 -07001347 }
RoboErikb7c014c2014-07-22 15:58:22 -07001348 return;
RoboErik3c45c292014-07-08 16:47:31 -07001349 }
Shibin George19e84042016-06-14 20:42:13 +05301350
1351 // Execute mAudioService.adjustSuggestedStreamVolume() on
1352 // handler thread of MediaSessionService.
1353 // This will release the MediaSessionService.mLock sooner and avoid
1354 // a potential deadlock between MediaSessionService.mLock and
1355 // ActivityManagerService lock.
1356 mHandler.post(new Runnable() {
1357 @Override
1358 public void run() {
1359 try {
1360 String packageName = getContext().getOpPackageName();
1361 mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream,
1362 flags, packageName, TAG);
1363 } catch (RemoteException e) {
1364 Log.e(TAG, "Error adjusting default volume.", e);
1365 }
1366 }
1367 });
RoboErikb69ffd42014-05-30 14:57:59 -07001368 } else {
RoboErik0dac35a2014-08-12 15:48:49 -07001369 session.adjustVolume(direction, flags, getContext().getPackageName(),
Jaewan Kim8f729082016-06-21 12:36:26 +09001370 Process.SYSTEM_UID, true);
RoboErikb69ffd42014-05-30 14:57:59 -07001371 }
1372 }
1373
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001374 private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) {
RoboErik9a9d0b52014-05-20 14:53:39 -07001375 int action = keyEvent.getAction();
1376 boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0;
1377 if (action == KeyEvent.ACTION_DOWN) {
1378 if (keyEvent.getRepeatCount() == 0) {
1379 mVoiceButtonDown = true;
1380 mVoiceButtonHandled = false;
1381 } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) {
1382 mVoiceButtonHandled = true;
1383 startVoiceInput(needWakeLock);
1384 }
1385 } else if (action == KeyEvent.ACTION_UP) {
1386 if (mVoiceButtonDown) {
1387 mVoiceButtonDown = false;
1388 if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
1389 // Resend the down then send this event through
1390 KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN);
Jaewan Kim98003d32017-02-24 18:33:04 +09001391 dispatchMediaKeyEventLocked(downEvent, needWakeLock);
1392 dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
RoboErik9a9d0b52014-05-20 14:53:39 -07001393 }
1394 }
1395 }
1396 }
1397
Jaewan Kim98003d32017-02-24 18:33:04 +09001398 private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001399 MediaSessionRecord session = mCurrentFullUserRecord.getMediaButtonSessionLocked();
RoboErik9a9d0b52014-05-20 14:53:39 -07001400 if (session != null) {
Jaewan Kim50269362016-12-23 11:22:02 +09001401 if (DEBUG_KEY_EVENT) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +09001402 Log.d(TAG, "Sending " + keyEvent + " to " + session);
RoboErik9a9d0b52014-05-20 14:53:39 -07001403 }
1404 if (needWakeLock) {
1405 mKeyEventReceiver.aquireWakeLockLocked();
1406 }
Jaewan Kim50269362016-12-23 11:22:02 +09001407 // If we don't need a wakelock use -1 as the id so we won't release it later.
RoboErik9a9d0b52014-05-20 14:53:39 -07001408 session.sendMediaButton(keyEvent,
1409 needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
Jaewan Kim8f729082016-06-21 12:36:26 +09001410 mKeyEventReceiver, Process.SYSTEM_UID,
Donghyun Cho1ea56832016-02-23 16:30:07 +09001411 getContext().getPackageName());
Jaewan Kima7dce192017-02-16 17:10:54 +09001412 if (mCurrentFullUserRecord.mCallback != null) {
Jaewan Kimbd16f452017-02-03 16:21:38 +09001413 try {
Jaewan Kima7dce192017-02-16 17:10:54 +09001414 mCurrentFullUserRecord.mCallback.onMediaKeyEventDispatchedToMediaSession(
1415 keyEvent,
Jaewan Kimbd16f452017-02-03 16:21:38 +09001416 new MediaSession.Token(session.getControllerBinder()));
1417 } catch (RemoteException e) {
1418 Log.w(TAG, "Failed to send callback", e);
1419 }
1420 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001421 } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null
1422 || mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) {
1423 if (needWakeLock) {
1424 mKeyEventReceiver.aquireWakeLockLocked();
1425 }
1426 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
1427 mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1428 mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
1429 try {
1430 if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) {
1431 PendingIntent receiver = mCurrentFullUserRecord.mLastMediaButtonReceiver;
1432 if (DEBUG_KEY_EVENT) {
1433 Log.d(TAG, "Sending " + keyEvent
Jaewan Kim92dea332017-02-02 11:52:08 +09001434 + " to the last known PendingIntent " + receiver);
Jaewan Kima7dce192017-02-16 17:10:54 +09001435 }
1436 receiver.send(getContext(),
1437 needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
1438 mediaButtonIntent, mKeyEventReceiver, mHandler);
1439 if (mCurrentFullUserRecord.mCallback != null) {
1440 ComponentName componentName = mCurrentFullUserRecord
1441 .mLastMediaButtonReceiver.getIntent().getComponent();
1442 if (componentName != null) {
1443 mCurrentFullUserRecord.mCallback
1444 .onMediaKeyEventDispatchedToMediaButtonReceiver(
1445 keyEvent, componentName);
Jaewan Kimbd16f452017-02-03 16:21:38 +09001446 }
RoboErikc8f92d12015-01-05 16:48:07 -08001447 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001448 } else {
1449 ComponentName receiver =
1450 mCurrentFullUserRecord.mRestoredMediaButtonReceiver;
1451 if (DEBUG_KEY_EVENT) {
1452 Log.d(TAG, "Sending " + keyEvent + " to the restored intent "
1453 + receiver);
1454 }
1455 mediaButtonIntent.setComponent(receiver);
1456 getContext().sendBroadcastAsUser(mediaButtonIntent,
Jaewan Kim92dea332017-02-02 11:52:08 +09001457 UserHandle.of(mCurrentFullUserRecord
1458 .mRestoredMediaButtonReceiverUserId));
Jaewan Kima7dce192017-02-16 17:10:54 +09001459 if (mCurrentFullUserRecord.mCallback != null) {
1460 mCurrentFullUserRecord.mCallback
1461 .onMediaKeyEventDispatchedToMediaButtonReceiver(
1462 keyEvent, receiver);
1463 }
RoboErikb214efb2014-07-24 13:20:30 -07001464 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001465 } catch (CanceledException e) {
1466 Log.i(TAG, "Error sending key event to media button receiver "
1467 + mCurrentFullUserRecord.mLastMediaButtonReceiver, e);
1468 } catch (RemoteException e) {
1469 Log.w(TAG, "Failed to send callback", e);
RoboErik9a9d0b52014-05-20 14:53:39 -07001470 }
RoboErik9a9d0b52014-05-20 14:53:39 -07001471 }
1472 }
1473
1474 private void startVoiceInput(boolean needWakeLock) {
1475 Intent voiceIntent = null;
1476 // select which type of search to launch:
1477 // - screen on and device unlocked: action is ACTION_WEB_SEARCH
1478 // - device locked or screen off: action is
1479 // ACTION_VOICE_SEARCH_HANDS_FREE
1480 // with EXTRA_SECURE set to true if the device is securely locked
1481 PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
1482 boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1483 if (!isLocked && pm.isScreenOn()) {
1484 voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH);
1485 Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH");
1486 } else {
1487 voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
1488 voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE,
1489 isLocked && mKeyguardManager.isKeyguardSecure());
1490 Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE");
1491 }
1492 // start the search activity
1493 if (needWakeLock) {
1494 mMediaEventWakeLock.acquire();
1495 }
1496 try {
1497 if (voiceIntent != null) {
1498 voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1499 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Jaewan Kim8f729082016-06-21 12:36:26 +09001500 if (DEBUG) Log.d(TAG, "voiceIntent: " + voiceIntent);
RoboErik9a9d0b52014-05-20 14:53:39 -07001501 getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT);
1502 }
1503 } catch (ActivityNotFoundException e) {
1504 Log.w(TAG, "No activity for search: " + e);
1505 } finally {
1506 if (needWakeLock) {
1507 mMediaEventWakeLock.release();
1508 }
1509 }
1510 }
1511
1512 private boolean isVoiceKey(int keyCode) {
1513 return keyCode == KeyEvent.KEYCODE_HEADSETHOOK;
1514 }
1515
Jeff Brown38d3feb2015-03-19 18:26:30 -07001516 private boolean isUserSetupComplete() {
1517 return Settings.Secure.getIntForUser(getContext().getContentResolver(),
1518 Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
1519 }
1520
RoboErik9c785402014-11-11 16:52:26 -08001521 // we only handle public stream types, which are 0-5
1522 private boolean isValidLocalStreamType(int streamType) {
1523 return streamType >= AudioManager.STREAM_VOICE_CALL
1524 && streamType <= AudioManager.STREAM_NOTIFICATION;
1525 }
1526
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001527 private class MediaKeyListenerResultReceiver extends ResultReceiver implements Runnable {
1528 private KeyEvent mKeyEvent;
1529 private boolean mNeedWakeLock;
1530 private boolean mHandled;
1531
1532 private MediaKeyListenerResultReceiver(KeyEvent keyEvent, boolean needWakeLock) {
1533 super(mHandler);
1534 mHandler.postDelayed(this, MEDIA_KEY_LISTENER_TIMEOUT);
1535 mKeyEvent = keyEvent;
1536 mNeedWakeLock = needWakeLock;
1537 }
1538
1539 @Override
1540 public void run() {
1541 Log.d(TAG, "The media key listener is timed-out for " + mKeyEvent);
1542 dispatchMediaKeyEvent();
1543 }
1544
1545 @Override
1546 protected void onReceiveResult(int resultCode, Bundle resultData) {
1547 if (resultCode == MediaSessionManager.RESULT_MEDIA_KEY_HANDLED) {
1548 mHandled = true;
1549 mHandler.removeCallbacks(this);
1550 return;
1551 }
1552 dispatchMediaKeyEvent();
1553 }
1554
1555 private void dispatchMediaKeyEvent() {
1556 if (mHandled) {
1557 return;
1558 }
1559 mHandled = true;
1560 mHandler.removeCallbacks(this);
1561 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001562 if (!isGlobalPriorityActiveLocked()
Jaewan Kim98003d32017-02-24 18:33:04 +09001563 && isVoiceKey(mKeyEvent.getKeyCode())) {
1564 handleVoiceKeyEventLocked(mKeyEvent, mNeedWakeLock);
1565 } else {
1566 dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock);
1567 }
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001568 }
1569 }
1570 }
1571
RoboErik418c10c2014-05-19 09:25:25 -07001572 private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler);
1573
RoboErikb214efb2014-07-24 13:20:30 -07001574 class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable,
1575 PendingIntent.OnFinished {
RoboErik418c10c2014-05-19 09:25:25 -07001576 private final Handler mHandler;
1577 private int mRefCount = 0;
1578 private int mLastTimeoutId = 0;
1579
1580 public KeyEventWakeLockReceiver(Handler handler) {
1581 super(handler);
1582 mHandler = handler;
1583 }
1584
1585 public void onTimeout() {
1586 synchronized (mLock) {
1587 if (mRefCount == 0) {
1588 // We've already released it, so just return
1589 return;
1590 }
1591 mLastTimeoutId++;
1592 mRefCount = 0;
1593 releaseWakeLockLocked();
1594 }
1595 }
1596
1597 public void aquireWakeLockLocked() {
1598 if (mRefCount == 0) {
1599 mMediaEventWakeLock.acquire();
1600 }
1601 mRefCount++;
1602 mHandler.removeCallbacks(this);
1603 mHandler.postDelayed(this, WAKELOCK_TIMEOUT);
1604
1605 }
1606
1607 @Override
1608 public void run() {
1609 onTimeout();
1610 }
1611
RoboErik8a2cfc32014-05-16 11:19:38 -07001612 @Override
1613 protected void onReceiveResult(int resultCode, Bundle resultData) {
RoboErik418c10c2014-05-19 09:25:25 -07001614 if (resultCode < mLastTimeoutId) {
1615 // Ignore results from calls that were before the last
1616 // timeout, just in case.
1617 return;
1618 } else {
1619 synchronized (mLock) {
1620 if (mRefCount > 0) {
1621 mRefCount--;
1622 if (mRefCount == 0) {
1623 releaseWakeLockLocked();
1624 }
1625 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001626 }
1627 }
1628 }
RoboErik418c10c2014-05-19 09:25:25 -07001629
1630 private void releaseWakeLockLocked() {
1631 mMediaEventWakeLock.release();
1632 mHandler.removeCallbacks(this);
1633 }
RoboErikb214efb2014-07-24 13:20:30 -07001634
1635 @Override
1636 public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode,
1637 String resultData, Bundle resultExtras) {
1638 onReceiveResult(resultCode, null);
1639 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001640 };
1641
1642 BroadcastReceiver mKeyEventDone = new BroadcastReceiver() {
1643 @Override
1644 public void onReceive(Context context, Intent intent) {
1645 if (intent == null) {
1646 return;
1647 }
1648 Bundle extras = intent.getExtras();
1649 if (extras == null) {
1650 return;
1651 }
1652 synchronized (mLock) {
1653 if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED)
1654 && mMediaEventWakeLock.isHeld()) {
1655 mMediaEventWakeLock.release();
1656 }
1657 }
1658 }
1659 };
RoboErik01fe6612014-02-13 14:19:04 -08001660 }
1661
RoboErik2e7a9162014-06-04 16:53:45 -07001662 final class MessageHandler extends Handler {
1663 private static final int MSG_SESSIONS_CHANGED = 1;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001664 private static final int MSG_VOLUME_INITIAL_DOWN = 2;
Jaewan Kim92dea332017-02-02 11:52:08 +09001665 private final SparseArray<Integer> mIntegerCache = new SparseArray<>();
RoboErik2e7a9162014-06-04 16:53:45 -07001666
1667 @Override
1668 public void handleMessage(Message msg) {
1669 switch (msg.what) {
1670 case MSG_SESSIONS_CHANGED:
Jaewan Kim92dea332017-02-02 11:52:08 +09001671 pushSessionsChanged((int) msg.obj);
RoboErik2e7a9162014-06-04 16:53:45 -07001672 break;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001673 case MSG_VOLUME_INITIAL_DOWN:
1674 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001675 FullUserRecord user = mUserRecords.get((int) msg.arg1);
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001676 if (user != null && user.mInitialDownVolumeKeyEvent != null) {
1677 dispatchVolumeKeyLongPressLocked(user.mInitialDownVolumeKeyEvent);
1678 // Mark that the key is already handled.
1679 user.mInitialDownVolumeKeyEvent = null;
1680 }
1681 }
1682 break;
RoboErik2e7a9162014-06-04 16:53:45 -07001683 }
1684 }
1685
Jaewan Kim92dea332017-02-02 11:52:08 +09001686 public void postSessionsChanged(int userId) {
1687 // Use object instead of the arguments when posting message to remove pending requests.
1688 Integer userIdInteger = mIntegerCache.get(userId);
1689 if (userIdInteger == null) {
1690 userIdInteger = Integer.valueOf(userId);
1691 mIntegerCache.put(userId, userIdInteger);
1692 }
1693 removeMessages(MSG_SESSIONS_CHANGED, userIdInteger);
1694 obtainMessage(MSG_SESSIONS_CHANGED, userIdInteger).sendToTarget();
RoboErik2e7a9162014-06-04 16:53:45 -07001695 }
1696 }
RoboErik01fe6612014-02-13 14:19:04 -08001697}