blob: aa652445c45a1591ab5822d03b7dfbe2dfe438fe [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
RoboErike7880d82014-04-30 12:48:25 -070019import android.app.ActivityManager;
Julia Reynoldsb852e562017-06-06 16:14:18 -040020import android.app.INotificationManager;
RoboErik9a9d0b52014-05-20 14:53:39 -070021import android.app.KeyguardManager;
RoboErikb214efb2014-07-24 13:20:30 -070022import android.app.PendingIntent;
23import android.app.PendingIntent.CanceledException;
RoboErik9a9d0b52014-05-20 14:53:39 -070024import android.content.ActivityNotFoundException;
RoboErik8a2cfc32014-05-16 11:19:38 -070025import android.content.BroadcastReceiver;
RoboErike7880d82014-04-30 12:48:25 -070026import android.content.ComponentName;
RoboErik6f0e4dd2014-06-17 16:56:27 -070027import android.content.ContentResolver;
RoboErik01fe6612014-02-13 14:19:04 -080028import android.content.Context;
RoboErik8a2cfc32014-05-16 11:19:38 -070029import android.content.Intent;
RoboErika278ea72014-04-24 14:49:01 -070030import android.content.pm.PackageManager;
Jaewan Kima7dce192017-02-16 17:10:54 +090031import android.content.pm.UserInfo;
RoboErik7aef77b2014-08-08 15:56:54 -070032import android.database.ContentObserver;
RoboErik3c45c292014-07-08 16:47:31 -070033import android.media.AudioManager;
John Spurlockeb69e242015-02-17 17:15:04 -050034import android.media.AudioManagerInternal;
RoboErik94c716e2014-09-14 13:54:31 -070035import android.media.AudioSystem;
RoboErikb69ffd42014-05-30 14:57:59 -070036import android.media.IAudioService;
RoboErik19c95182014-06-23 15:38:48 -070037import android.media.IRemoteVolumeController;
RoboErik2e7a9162014-06-04 16:53:45 -070038import android.media.session.IActiveSessionsListener;
Jaewan Kimbd16f452017-02-03 16:21:38 +090039import android.media.session.ICallback;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080040import android.media.session.IOnMediaKeyListener;
Jaewan Kim50269362016-12-23 11:22:02 +090041import android.media.session.IOnVolumeKeyLongPressListener;
RoboErik07c70772014-03-20 13:33:52 -070042import android.media.session.ISession;
43import android.media.session.ISessionCallback;
44import android.media.session.ISessionManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070045import android.media.session.MediaSession;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080046import android.media.session.MediaSessionManager;
RoboErik7aef77b2014-08-08 15:56:54 -070047import android.net.Uri;
RoboErik01fe6612014-02-13 14:19:04 -080048import android.os.Binder;
RoboErik8a2cfc32014-05-16 11:19:38 -070049import android.os.Bundle;
RoboErik8ae0f342014-02-24 18:02:08 -080050import android.os.Handler;
RoboErike7880d82014-04-30 12:48:25 -070051import android.os.IBinder;
RoboErik2e7a9162014-06-04 16:53:45 -070052import android.os.Message;
RoboErik8a2cfc32014-05-16 11:19:38 -070053import android.os.PowerManager;
Jaewan Kim8f729082016-06-21 12:36:26 +090054import android.os.Process;
RoboErik01fe6612014-02-13 14:19:04 -080055import android.os.RemoteException;
RoboErik8a2cfc32014-05-16 11:19:38 -070056import android.os.ResultReceiver;
RoboErikb69ffd42014-05-30 14:57:59 -070057import android.os.ServiceManager;
RoboErike7880d82014-04-30 12:48:25 -070058import android.os.UserHandle;
Jaewan Kim8f729082016-06-21 12:36:26 +090059import android.os.UserManager;
RoboErike7880d82014-04-30 12:48:25 -070060import android.provider.Settings;
RoboErik9a9d0b52014-05-20 14:53:39 -070061import android.speech.RecognizerIntent;
RoboErik01fe6612014-02-13 14:19:04 -080062import android.text.TextUtils;
63import android.util.Log;
Jeff Brown38d3feb2015-03-19 18:26:30 -070064import android.util.Slog;
RoboErik4646d282014-05-13 10:13:04 -070065import android.util.SparseArray;
Jaewan Kima7dce192017-02-16 17:10:54 +090066import android.util.SparseIntArray;
RoboErik8a2cfc32014-05-16 11:19:38 -070067import android.view.KeyEvent;
Jaewan Kimd61a87b2017-02-17 23:14:10 +090068import android.view.ViewConfiguration;
RoboErik01fe6612014-02-13 14:19:04 -080069
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060070import com.android.internal.util.DumpUtils;
John Spurlockeb69e242015-02-17 17:15:04 -050071import com.android.server.LocalServices;
RoboErik01fe6612014-02-13 14:19:04 -080072import com.android.server.SystemService;
RoboErika278ea72014-04-24 14:49:01 -070073import com.android.server.Watchdog;
74import com.android.server.Watchdog.Monitor;
RoboErik01fe6612014-02-13 14:19:04 -080075
RoboErika278ea72014-04-24 14:49:01 -070076import java.io.FileDescriptor;
77import java.io.PrintWriter;
RoboErik01fe6612014-02-13 14:19:04 -080078import java.util.ArrayList;
RoboErike7880d82014-04-30 12:48:25 -070079import java.util.List;
RoboErik01fe6612014-02-13 14:19:04 -080080
81/**
82 * System implementation of MediaSessionManager
83 */
RoboErika278ea72014-04-24 14:49:01 -070084public class MediaSessionService extends SystemService implements Monitor {
RoboErik01fe6612014-02-13 14:19:04 -080085 private static final String TAG = "MediaSessionService";
Jaewan Kim92dea332017-02-02 11:52:08 +090086 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jaewan Kim50269362016-12-23 11:22:02 +090087 // Leave log for key event always.
88 private static final boolean DEBUG_KEY_EVENT = true;
RoboErik01fe6612014-02-13 14:19:04 -080089
RoboErik418c10c2014-05-19 09:25:25 -070090 private static final int WAKELOCK_TIMEOUT = 5000;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -080091 private static final int MEDIA_KEY_LISTENER_TIMEOUT = 1000;
RoboErik418c10c2014-05-19 09:25:25 -070092
RoboErik01fe6612014-02-13 14:19:04 -080093 private final SessionManagerImpl mSessionManagerImpl;
94
Jaewan Kima7dce192017-02-16 17:10:54 +090095 // Keeps the full user id for each user.
96 private final SparseIntArray mFullUserIds = new SparseIntArray();
97 private final SparseArray<FullUserRecord> mUserRecords = new SparseArray<FullUserRecord>();
RoboErik2e7a9162014-06-04 16:53:45 -070098 private final ArrayList<SessionsListenerRecord> mSessionsListeners
99 = new ArrayList<SessionsListenerRecord>();
RoboErik01fe6612014-02-13 14:19:04 -0800100 private final Object mLock = new Object();
RoboErik2e7a9162014-06-04 16:53:45 -0700101 private final MessageHandler mHandler = new MessageHandler();
RoboErik8a2cfc32014-05-16 11:19:38 -0700102 private final PowerManager.WakeLock mMediaEventWakeLock;
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900103 private final int mLongPressTimeout;
RoboErik01fe6612014-02-13 14:19:04 -0800104
RoboErik9a9d0b52014-05-20 14:53:39 -0700105 private KeyguardManager mKeyguardManager;
RoboErikb69ffd42014-05-30 14:57:59 -0700106 private IAudioService mAudioService;
John Spurlockeb69e242015-02-17 17:15:04 -0500107 private AudioManagerInternal mAudioManagerInternal;
RoboErik6f0e4dd2014-06-17 16:56:27 -0700108 private ContentResolver mContentResolver;
RoboErik7aef77b2014-08-08 15:56:54 -0700109 private SettingsObserver mSettingsObserver;
Julia Reynoldsb852e562017-06-06 16:14:18 -0400110 private INotificationManager mNotificationManager;
Jaewan Kimfdb612e2017-07-01 09:23:41 +0900111 private boolean mHasFeatureLeanback;
RoboErik9a9d0b52014-05-20 14:53:39 -0700112
Jaewan Kima7dce192017-02-16 17:10:54 +0900113 // The FullUserRecord of the current users. (i.e. The foreground user that isn't a profile)
114 // It's always not null after the MediaSessionService is started.
115 private FullUserRecord mCurrentFullUserRecord;
116 private MediaSessionRecord mGlobalPrioritySession;
Jaewan Kim92dea332017-02-02 11:52:08 +0900117 private AudioPlaybackMonitor mAudioPlaybackMonitor;
RoboErike7880d82014-04-30 12:48:25 -0700118
RoboErik19c95182014-06-23 15:38:48 -0700119 // Used to notify system UI when remote volume was changed. TODO find a
120 // better way to handle this.
121 private IRemoteVolumeController mRvc;
122
RoboErik01fe6612014-02-13 14:19:04 -0800123 public MediaSessionService(Context context) {
124 super(context);
125 mSessionManagerImpl = new SessionManagerImpl();
RoboErik8a2cfc32014-05-16 11:19:38 -0700126 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
127 mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent");
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900128 mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
Julia Reynoldsb852e562017-06-06 16:14:18 -0400129 mNotificationManager = INotificationManager.Stub.asInterface(
130 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
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();
Sungsoob3658562017-05-22 17:10:44 +0900140 mAudioPlaybackMonitor = AudioPlaybackMonitor.getInstance(getContext(), mAudioService);
141 mAudioPlaybackMonitor.registerOnAudioPlaybackStartedListener(
Jaewan Kim92dea332017-02-02 11:52:08 +0900142 new AudioPlaybackMonitor.OnAudioPlaybackStartedListener() {
Sungsoob3658562017-05-22 17:10:44 +0900143 @Override
144 public void onAudioPlaybackStarted(int uid) {
145 synchronized (mLock) {
146 FullUserRecord user =
147 getFullUserRecordLocked(UserHandle.getUserId(uid));
148 if (user != null) {
149 user.mPriorityStack.updateMediaButtonSessionIfNeeded();
Jaewan Kim92dea332017-02-02 11:52:08 +0900150 }
Sungsoob3658562017-05-22 17:10:44 +0900151 }
152 }
153 });
John Spurlockeb69e242015-02-17 17:15:04 -0500154 mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class);
RoboErik6f0e4dd2014-06-17 16:56:27 -0700155 mContentResolver = getContext().getContentResolver();
RoboErik7aef77b2014-08-08 15:56:54 -0700156 mSettingsObserver = new SettingsObserver();
157 mSettingsObserver.observe();
Jaewan Kimfdb612e2017-07-01 09:23:41 +0900158 mHasFeatureLeanback = getContext().getPackageManager().hasSystemFeature(
159 PackageManager.FEATURE_LEANBACK);
RoboErikc8f92d12015-01-05 16:48:07 -0800160
161 updateUser();
RoboErikb69ffd42014-05-30 14:57:59 -0700162 }
163
164 private IAudioService getAudioService() {
165 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
166 return IAudioService.Stub.asInterface(b);
RoboErik07c70772014-03-20 13:33:52 -0700167 }
168
Jaewan Kima7dce192017-02-16 17:10:54 +0900169 private boolean isGlobalPriorityActiveLocked() {
170 return mGlobalPrioritySession != null && mGlobalPrioritySession.isActive();
171 }
172
RoboErika8f95142014-05-05 14:23:49 -0700173 public void updateSession(MediaSessionRecord record) {
RoboErike7880d82014-04-30 12:48:25 -0700174 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900175 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
Jaewan Kim101b4d52017-05-18 13:23:11 +0900176 if (user == null) {
177 Log.w(TAG, "Unknown session updated. Ignoring.");
RoboErik4646d282014-05-13 10:13:04 -0700178 return;
179 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900180 if ((record.getFlags() & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) {
Jaewan Kim101b4d52017-05-18 13:23:11 +0900181 if (DEBUG_KEY_EVENT) {
182 Log.d(TAG, "Global priority session is updated, active=" + record.isActive());
183 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900184 user.pushAddressedPlayerChangedLocked();
Jaewan Kim101b4d52017-05-18 13:23:11 +0900185 } else {
186 if (!user.mPriorityStack.contains(record)) {
187 Log.w(TAG, "Unknown session updated. Ignoring.");
188 return;
189 }
190 user.mPriorityStack.onSessionStateChange(record);
Jaewan Kima7dce192017-02-16 17:10:54 +0900191 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900192 mHandler.postSessionsChanged(record.getUserId());
RoboErike7880d82014-04-30 12:48:25 -0700193 }
194 }
195
Jaewan Kimfa85b602017-10-10 16:49:58 +0900196 public void setGlobalPrioritySession(MediaSessionRecord record) {
197 synchronized (mLock) {
198 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
199 if (mGlobalPrioritySession != record) {
200 Log.d(TAG, "Global priority session is changed from " + mGlobalPrioritySession
201 + " to " + record);
202 mGlobalPrioritySession = record;
203 if (user != null && user.mPriorityStack.contains(record)) {
204 // Handle the global priority session separately.
205 // Otherwise, it can be the media button session regardless of the active state
206 // because it or other system components might have been the lastly played media
207 // app.
208 user.mPriorityStack.removeSession(record);
209 }
210 }
211 }
212 }
213
Jaewan Kim101b4d52017-05-18 13:23:11 +0900214 private List<MediaSessionRecord> getActiveSessionsLocked(int userId) {
Jaewan Kimda74a152017-10-03 23:58:11 +0900215 List<MediaSessionRecord> records = new ArrayList<>();
Jaewan Kim101b4d52017-05-18 13:23:11 +0900216 if (userId == UserHandle.USER_ALL) {
Jaewan Kim101b4d52017-05-18 13:23:11 +0900217 int size = mUserRecords.size();
218 for (int i = 0; i < size; i++) {
219 records.addAll(mUserRecords.valueAt(i).mPriorityStack.getActiveSessions(userId));
220 }
221 } else {
222 FullUserRecord user = getFullUserRecordLocked(userId);
223 if (user == null) {
224 Log.w(TAG, "getSessions failed. Unknown user " + userId);
Jaewan Kimda74a152017-10-03 23:58:11 +0900225 return records;
Jaewan Kim101b4d52017-05-18 13:23:11 +0900226 }
Jaewan Kimda74a152017-10-03 23:58:11 +0900227 records.addAll(user.mPriorityStack.getActiveSessions(userId));
Jaewan Kim101b4d52017-05-18 13:23:11 +0900228 }
229
230 // Return global priority session at the first whenever it's asked.
231 if (isGlobalPriorityActiveLocked()
232 && (userId == UserHandle.USER_ALL
233 || userId == mGlobalPrioritySession.getUserId())) {
234 records.add(0, mGlobalPrioritySession);
235 }
236 return records;
237 }
238
RoboErik9c5b7cb2015-01-15 15:09:09 -0800239 /**
Hyundo Moona055f132017-01-13 15:31:06 +0900240 * Tells the system UI that volume has changed on an active remote session.
RoboErik9c5b7cb2015-01-15 15:09:09 -0800241 */
242 public void notifyRemoteVolumeChanged(int flags, MediaSessionRecord session) {
Hyundo Moona055f132017-01-13 15:31:06 +0900243 if (mRvc == null || !session.isActive()) {
RoboErik9c5b7cb2015-01-15 15:09:09 -0800244 return;
245 }
246 try {
247 mRvc.remoteVolumeChanged(session.getControllerBinder(), flags);
248 } catch (Exception e) {
249 Log.wtf(TAG, "Error sending volume change to system UI.", e);
250 }
251 }
252
Jaewan Kim92dea332017-02-02 11:52:08 +0900253 public void onSessionPlaystateChanged(MediaSessionRecord record, int oldState, int newState) {
RoboErika8f95142014-05-05 14:23:49 -0700254 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900255 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
256 if (user == null || !user.mPriorityStack.contains(record)) {
RoboErik4646d282014-05-13 10:13:04 -0700257 Log.d(TAG, "Unknown session changed playback state. Ignoring.");
258 return;
259 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900260 user.mPriorityStack.onPlaystateChanged(record, oldState, newState);
RoboErika8f95142014-05-05 14:23:49 -0700261 }
262 }
263
RoboErik19c95182014-06-23 15:38:48 -0700264 public void onSessionPlaybackTypeChanged(MediaSessionRecord record) {
265 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900266 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
267 if (user == null || !user.mPriorityStack.contains(record)) {
RoboErik19c95182014-06-23 15:38:48 -0700268 Log.d(TAG, "Unknown session changed playback type. Ignoring.");
269 return;
270 }
271 pushRemoteVolumeUpdateLocked(record.getUserId());
272 }
273 }
274
RoboErika278ea72014-04-24 14:49:01 -0700275 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900276 public void onStartUser(int userId) {
277 if (DEBUG) Log.d(TAG, "onStartUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700278 updateUser();
279 }
280
281 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900282 public void onSwitchUser(int userId) {
283 if (DEBUG) Log.d(TAG, "onSwitchUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700284 updateUser();
285 }
286
287 @Override
Jaewan Kim8f729082016-06-21 12:36:26 +0900288 public void onStopUser(int userId) {
289 if (DEBUG) Log.d(TAG, "onStopUser: " + userId);
RoboErik4646d282014-05-13 10:13:04 -0700290 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900291 FullUserRecord user = getFullUserRecordLocked(userId);
RoboErik4646d282014-05-13 10:13:04 -0700292 if (user != null) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900293 if (user.mFullUserId == userId) {
294 user.destroySessionsForUserLocked(UserHandle.USER_ALL);
295 mUserRecords.remove(userId);
296 } else {
297 user.destroySessionsForUserLocked(userId);
298 }
RoboErik4646d282014-05-13 10:13:04 -0700299 }
Jaewan Kim8f729082016-06-21 12:36:26 +0900300 updateUser();
RoboErik4646d282014-05-13 10:13:04 -0700301 }
302 }
303
304 @Override
RoboErika278ea72014-04-24 14:49:01 -0700305 public void monitor() {
306 synchronized (mLock) {
307 // Check for deadlock
308 }
309 }
310
RoboErik4646d282014-05-13 10:13:04 -0700311 protected void enforcePhoneStatePermission(int pid, int uid) {
312 if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid)
313 != PackageManager.PERMISSION_GRANTED) {
314 throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission.");
315 }
316 }
317
RoboErik01fe6612014-02-13 14:19:04 -0800318 void sessionDied(MediaSessionRecord session) {
RoboErika278ea72014-04-24 14:49:01 -0700319 synchronized (mLock) {
RoboErik01fe6612014-02-13 14:19:04 -0800320 destroySessionLocked(session);
321 }
322 }
323
324 void destroySession(MediaSessionRecord session) {
RoboErika278ea72014-04-24 14:49:01 -0700325 synchronized (mLock) {
RoboErik01fe6612014-02-13 14:19:04 -0800326 destroySessionLocked(session);
327 }
328 }
329
RoboErik4646d282014-05-13 10:13:04 -0700330 private void updateUser() {
331 synchronized (mLock) {
Jaewan Kim8f729082016-06-21 12:36:26 +0900332 UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
Jaewan Kima7dce192017-02-16 17:10:54 +0900333 mFullUserIds.clear();
334 List<UserInfo> allUsers = manager.getUsers();
335 if (allUsers != null) {
336 for (UserInfo userInfo : allUsers) {
337 if (userInfo.isManagedProfile()) {
338 mFullUserIds.put(userInfo.id, userInfo.profileGroupId);
339 } else {
340 mFullUserIds.put(userInfo.id, userInfo.id);
341 if (mUserRecords.get(userInfo.id) == null) {
342 mUserRecords.put(userInfo.id, new FullUserRecord(userInfo.id));
343 }
344 }
Jaewan Kim8f729082016-06-21 12:36:26 +0900345 }
RoboErik4646d282014-05-13 10:13:04 -0700346 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900347 // Ensure that the current full user exists.
348 int currentFullUserId = ActivityManager.getCurrentUser();
349 mCurrentFullUserRecord = mUserRecords.get(currentFullUserId);
350 if (mCurrentFullUserRecord == null) {
351 Log.w(TAG, "Cannot find FullUserInfo for the current user " + currentFullUserId);
352 mCurrentFullUserRecord = new FullUserRecord(currentFullUserId);
353 mUserRecords.put(currentFullUserId, mCurrentFullUserRecord);
354 }
355 mFullUserIds.put(currentFullUserId, currentFullUserId);
RoboErik4646d282014-05-13 10:13:04 -0700356 }
357 }
358
RoboErik7aef77b2014-08-08 15:56:54 -0700359 private void updateActiveSessionListeners() {
360 synchronized (mLock) {
361 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
362 SessionsListenerRecord listener = mSessionsListeners.get(i);
363 try {
364 enforceMediaPermissions(listener.mComponentName, listener.mPid, listener.mUid,
365 listener.mUserId);
366 } catch (SecurityException e) {
367 Log.i(TAG, "ActiveSessionsListener " + listener.mComponentName
368 + " is no longer authorized. Disconnecting.");
369 mSessionsListeners.remove(i);
370 try {
371 listener.mListener
372 .onActiveSessionsChanged(new ArrayList<MediaSession.Token>());
373 } catch (Exception e1) {
374 // ignore
375 }
376 }
377 }
378 }
379 }
380
RoboErik4646d282014-05-13 10:13:04 -0700381 /*
382 * When a session is removed several things need to happen.
383 * 1. We need to remove it from the relevant user.
384 * 2. We need to remove it from the priority stack.
385 * 3. We need to remove it from all sessions.
386 * 4. If this is the system priority session we need to clear it.
387 * 5. We need to unlink to death from the cb binder
388 * 6. We need to tell the session to do any final cleanup (onDestroy)
389 */
RoboErik01fe6612014-02-13 14:19:04 -0800390 private void destroySessionLocked(MediaSessionRecord session) {
Insun Kang30be970a2015-11-26 15:35:44 +0900391 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900392 Log.d(TAG, "Destroying " + session);
Insun Kang30be970a2015-11-26 15:35:44 +0900393 }
Jaewan Kim101b4d52017-05-18 13:23:11 +0900394 FullUserRecord user = getFullUserRecordLocked(session.getUserId());
Jaewan Kima7dce192017-02-16 17:10:54 +0900395 if (mGlobalPrioritySession == session) {
396 mGlobalPrioritySession = null;
Jaewan Kim92dea332017-02-02 11:52:08 +0900397 if (session.isActive() && user != null) {
398 user.pushAddressedPlayerChangedLocked();
399 }
Jaewan Kim101b4d52017-05-18 13:23:11 +0900400 } else {
401 if (user != null) {
402 user.mPriorityStack.removeSession(session);
403 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900404 }
RoboErik4646d282014-05-13 10:13:04 -0700405
406 try {
407 session.getCallback().asBinder().unlinkToDeath(session, 0);
408 } catch (Exception e) {
409 // ignore exceptions while destroying a session.
410 }
411 session.onDestroy();
Jaewan Kim92dea332017-02-02 11:52:08 +0900412 mHandler.postSessionsChanged(session.getUserId());
RoboErik01fe6612014-02-13 14:19:04 -0800413 }
414
415 private void enforcePackageName(String packageName, int uid) {
416 if (TextUtils.isEmpty(packageName)) {
417 throw new IllegalArgumentException("packageName may not be empty");
418 }
419 String[] packages = getContext().getPackageManager().getPackagesForUid(uid);
420 final int packageCount = packages.length;
421 for (int i = 0; i < packageCount; i++) {
422 if (packageName.equals(packages[i])) {
423 return;
424 }
425 }
426 throw new IllegalArgumentException("packageName is not owned by the calling process");
427 }
428
RoboErike7880d82014-04-30 12:48:25 -0700429 /**
430 * Checks a caller's authorization to register an IRemoteControlDisplay.
431 * Authorization is granted if one of the following is true:
432 * <ul>
433 * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL
434 * permission</li>
RoboErika5b02322014-05-07 17:05:49 -0700435 * <li>the caller's listener is one of the enabled notification listeners
436 * for the caller's user</li>
RoboErike7880d82014-04-30 12:48:25 -0700437 * </ul>
438 */
RoboErika5b02322014-05-07 17:05:49 -0700439 private void enforceMediaPermissions(ComponentName compName, int pid, int uid,
440 int resolvedUserId) {
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500441 if (isCurrentVolumeController(uid, pid)) return;
RoboErike7880d82014-04-30 12:48:25 -0700442 if (getContext()
443 .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid)
444 != PackageManager.PERMISSION_GRANTED
RoboErika5b02322014-05-07 17:05:49 -0700445 && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid),
446 resolvedUserId)) {
RoboErike7880d82014-04-30 12:48:25 -0700447 throw new SecurityException("Missing permission to control media.");
448 }
449 }
450
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500451 private boolean isCurrentVolumeController(int uid, int pid) {
452 return getContext().checkPermission(android.Manifest.permission.STATUS_BAR_SERVICE,
453 pid, uid) == PackageManager.PERMISSION_GRANTED;
John Spurlockbe19ed02015-02-22 10:57:55 -0500454 }
455
456 private void enforceSystemUiPermission(String action, int pid, int uid) {
Julia Reynoldsbb983d202017-01-06 09:54:20 -0500457 if (!isCurrentVolumeController(uid, pid)) {
RoboErik19c95182014-06-23 15:38:48 -0700458 throw new SecurityException("Only system ui may " + action);
459 }
460 }
461
RoboErika5b02322014-05-07 17:05:49 -0700462 /**
463 * This checks if the component is an enabled notification listener for the
464 * specified user. Enabled components may only operate on behalf of the user
465 * they're running as.
466 *
467 * @param compName The component that is enabled.
468 * @param userId The user id of the caller.
469 * @param forUserId The user id they're making the request on behalf of.
470 * @return True if the component is enabled, false otherwise
471 */
472 private boolean isEnabledNotificationListener(ComponentName compName, int userId,
473 int forUserId) {
474 if (userId != forUserId) {
475 // You may not access another user's content as an enabled listener.
476 return false;
477 }
RoboErik51fa6bc2014-06-20 14:59:58 -0700478 if (DEBUG) {
479 Log.d(TAG, "Checking if enabled notification listener " + compName);
480 }
RoboErike7880d82014-04-30 12:48:25 -0700481 if (compName != null) {
Julia Reynoldsb852e562017-06-06 16:14:18 -0400482 try {
483 return mNotificationManager.isNotificationListenerAccessGrantedForUser(
484 compName, userId);
485 } catch(RemoteException e) {
486 Log.w(TAG, "Dead NotificationManager in isEnabledNotificationListener", e);
RoboErike7880d82014-04-30 12:48:25 -0700487 }
488 }
489 return false;
490 }
491
RoboErika5b02322014-05-07 17:05:49 -0700492 private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId,
RoboErik4646d282014-05-13 10:13:04 -0700493 String callerPackageName, ISessionCallback cb, String tag) throws RemoteException {
RoboErik01fe6612014-02-13 14:19:04 -0800494 synchronized (mLock) {
RoboErika5b02322014-05-07 17:05:49 -0700495 return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag);
RoboErik01fe6612014-02-13 14:19:04 -0800496 }
497 }
498
RoboErik4646d282014-05-13 10:13:04 -0700499 /*
500 * When a session is created the following things need to happen.
RoboErik8a2cfc32014-05-16 11:19:38 -0700501 * 1. Its callback binder needs a link to death
RoboErik4646d282014-05-13 10:13:04 -0700502 * 2. It needs to be added to all sessions.
503 * 3. It needs to be added to the priority stack.
504 * 4. It needs to be added to the relevant user record.
505 */
RoboErika5b02322014-05-07 17:05:49 -0700506 private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId,
507 String callerPackageName, ISessionCallback cb, String tag) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900508 FullUserRecord user = getFullUserRecordLocked(userId);
Dongwon Kang8cf39c52016-07-29 13:20:39 -0700509 if (user == null) {
510 Log.wtf(TAG, "Request from invalid user: " + userId);
511 throw new RuntimeException("Session request from invalid user.");
512 }
513
RoboErika5b02322014-05-07 17:05:49 -0700514 final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId,
Jaewan Kim92dea332017-02-02 11:52:08 +0900515 callerPackageName, cb, tag, this, mHandler.getLooper());
RoboErik01fe6612014-02-13 14:19:04 -0800516 try {
517 cb.asBinder().linkToDeath(session, 0);
518 } catch (RemoteException e) {
519 throw new RuntimeException("Media Session owner died prematurely.", e);
520 }
RoboErik4646d282014-05-13 10:13:04 -0700521
Jaewan Kim101b4d52017-05-18 13:23:11 +0900522 user.mPriorityStack.addSession(session);
Jaewan Kim92dea332017-02-02 11:52:08 +0900523 mHandler.postSessionsChanged(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700524
RoboErik01fe6612014-02-13 14:19:04 -0800525 if (DEBUG) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +0900526 Log.d(TAG, "Created session for " + callerPackageName + " with tag " + tag);
RoboErik01fe6612014-02-13 14:19:04 -0800527 }
528 return session;
529 }
530
RoboErik2e7a9162014-06-04 16:53:45 -0700531 private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) {
532 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
RoboErika08adb242014-11-21 18:28:18 -0800533 if (mSessionsListeners.get(i).mListener.asBinder() == listener.asBinder()) {
RoboErik2e7a9162014-06-04 16:53:45 -0700534 return i;
535 }
536 }
537 return -1;
538 }
539
RoboErik2e7a9162014-06-04 16:53:45 -0700540 private void pushSessionsChanged(int userId) {
541 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900542 FullUserRecord user = getFullUserRecordLocked(userId);
543 if (user == null) {
544 Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId);
545 return;
546 }
Jaewan Kim101b4d52017-05-18 13:23:11 +0900547 List<MediaSessionRecord> records = getActiveSessionsLocked(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700548 int size = records.size();
Jeff Browndba34ba2014-06-24 20:46:03 -0700549 ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>();
RoboErik2e7a9162014-06-04 16:53:45 -0700550 for (int i = 0; i < size; i++) {
Jeff Browndba34ba2014-06-24 20:46:03 -0700551 tokens.add(new MediaSession.Token(records.get(i).getControllerBinder()));
RoboErik2e7a9162014-06-04 16:53:45 -0700552 }
RoboErik19c95182014-06-23 15:38:48 -0700553 pushRemoteVolumeUpdateLocked(userId);
RoboErik2e7a9162014-06-04 16:53:45 -0700554 for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
555 SessionsListenerRecord record = mSessionsListeners.get(i);
556 if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) {
557 try {
558 record.mListener.onActiveSessionsChanged(tokens);
559 } catch (RemoteException e) {
560 Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing",
561 e);
562 mSessionsListeners.remove(i);
563 }
564 }
565 }
566 }
567 }
568
RoboErik19c95182014-06-23 15:38:48 -0700569 private void pushRemoteVolumeUpdateLocked(int userId) {
570 if (mRvc != null) {
571 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900572 FullUserRecord user = getFullUserRecordLocked(userId);
573 if (user == null) {
574 Log.w(TAG, "pushRemoteVolumeUpdateLocked failed. No user with id=" + userId);
575 return;
576 }
577 MediaSessionRecord record = user.mPriorityStack.getDefaultRemoteSession(userId);
RoboErik19c95182014-06-23 15:38:48 -0700578 mRvc.updateRemoteController(record == null ? null : record.getControllerBinder());
579 } catch (RemoteException e) {
580 Log.wtf(TAG, "Error sending default remote volume to sys ui.", e);
581 }
582 }
583 }
584
Jaewan Kim92dea332017-02-02 11:52:08 +0900585 /**
586 * Called when the media button receiver for the {@param record} is changed.
587 *
588 * @param record the media session whose media button receiver is updated.
589 */
590 public void onMediaButtonReceiverChanged(MediaSessionRecord record) {
591 synchronized (mLock) {
592 FullUserRecord user = getFullUserRecordLocked(record.getUserId());
593 MediaSessionRecord mediaButtonSession =
594 user.mPriorityStack.getMediaButtonSession();
595 if (record == mediaButtonSession) {
596 user.rememberMediaButtonReceiverLocked(mediaButtonSession);
597 }
598 }
599 }
600
Jaewan Kim50269362016-12-23 11:22:02 +0900601 private String getCallingPackageName(int uid) {
602 String[] packages = getContext().getPackageManager().getPackagesForUid(uid);
603 if (packages != null && packages.length > 0) {
604 return packages[0];
605 }
606 return "";
607 }
608
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900609 private void dispatchVolumeKeyLongPressLocked(KeyEvent keyEvent) {
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900610 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900611 mCurrentFullUserRecord.mOnVolumeKeyLongPressListener.onVolumeKeyLongPress(keyEvent);
Jaewan Kimd61a87b2017-02-17 23:14:10 +0900612 } catch (RemoteException e) {
613 Log.w(TAG, "Failed to send " + keyEvent + " to volume key long-press listener");
614 }
615 }
616
Jaewan Kima7dce192017-02-16 17:10:54 +0900617 private FullUserRecord getFullUserRecordLocked(int userId) {
618 int fullUserId = mFullUserIds.get(userId, -1);
619 if (fullUserId < 0) {
620 return null;
621 }
622 return mUserRecords.get(fullUserId);
623 }
624
RoboErik4646d282014-05-13 10:13:04 -0700625 /**
Jaewan Kima7dce192017-02-16 17:10:54 +0900626 * Information about a full user and its corresponding managed profiles.
627 *
628 * <p>Since the full user runs together with its managed profiles, a user wouldn't differentiate
629 * them when he/she presses a media/volume button. So keeping media sessions for them in one
630 * place makes more sense and increases the readability.</p>
631 * <p>The contents of this object is guarded by {@link #mLock}.
RoboErik4646d282014-05-13 10:13:04 -0700632 */
Jaewan Kim92dea332017-02-02 11:52:08 +0900633 final class FullUserRecord implements MediaSessionStack.OnMediaButtonSessionChangedListener {
Jaewan Kima7dce192017-02-16 17:10:54 +0900634 private static final String COMPONENT_NAME_USER_ID_DELIM = ",";
635 private final int mFullUserId;
Jaewan Kim92dea332017-02-02 11:52:08 +0900636 private final MediaSessionStack mPriorityStack;
RoboErikb214efb2014-07-24 13:20:30 -0700637 private PendingIntent mLastMediaButtonReceiver;
RoboErikc8f92d12015-01-05 16:48:07 -0800638 private ComponentName mRestoredMediaButtonReceiver;
Jaewan Kima7dce192017-02-16 17:10:54 +0900639 private int mRestoredMediaButtonReceiverUserId;
RoboErik4646d282014-05-13 10:13:04 -0700640
Jaewan Kim50269362016-12-23 11:22:02 +0900641 private IOnVolumeKeyLongPressListener mOnVolumeKeyLongPressListener;
642 private int mOnVolumeKeyLongPressListenerUid;
643 private KeyEvent mInitialDownVolumeKeyEvent;
644 private int mInitialDownVolumeStream;
645 private boolean mInitialDownMusicOnly;
646
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800647 private IOnMediaKeyListener mOnMediaKeyListener;
648 private int mOnMediaKeyListenerUid;
Jaewan Kima7dce192017-02-16 17:10:54 +0900649 private ICallback mCallback;
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800650
Jaewan Kima7dce192017-02-16 17:10:54 +0900651 public FullUserRecord(int fullUserId) {
652 mFullUserId = fullUserId;
Jaewan Kim92dea332017-02-02 11:52:08 +0900653 mPriorityStack = new MediaSessionStack(mAudioPlaybackMonitor, this);
Jaewan Kima7dce192017-02-16 17:10:54 +0900654 // Restore the remembered media button receiver before the boot.
655 String mediaButtonReceiver = Settings.Secure.getStringForUser(mContentResolver,
656 Settings.System.MEDIA_BUTTON_RECEIVER, mFullUserId);
657 if (mediaButtonReceiver == null) {
658 return;
659 }
660 String[] tokens = mediaButtonReceiver.split(COMPONENT_NAME_USER_ID_DELIM);
661 if (tokens == null || tokens.length != 2) {
662 return;
663 }
664 mRestoredMediaButtonReceiver = ComponentName.unflattenFromString(tokens[0]);
665 mRestoredMediaButtonReceiverUserId = Integer.parseInt(tokens[1]);
RoboErik4646d282014-05-13 10:13:04 -0700666 }
667
Jaewan Kima7dce192017-02-16 17:10:54 +0900668 public void destroySessionsForUserLocked(int userId) {
Jaewan Kim92dea332017-02-02 11:52:08 +0900669 List<MediaSessionRecord> sessions = mPriorityStack.getPriorityList(false, userId);
Jaewan Kima7dce192017-02-16 17:10:54 +0900670 for (MediaSessionRecord session : sessions) {
RoboErik4646d282014-05-13 10:13:04 -0700671 MediaSessionService.this.destroySessionLocked(session);
RoboErik4646d282014-05-13 10:13:04 -0700672 }
673 }
674
RoboErik4646d282014-05-13 10:13:04 -0700675 public void dumpLocked(PrintWriter pw, String prefix) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900676 pw.print(prefix + "Record for full_user=" + mFullUserId);
677 // Dump managed profile user ids associated with this user.
678 int size = mFullUserIds.size();
679 for (int i = 0; i < size; i++) {
680 if (mFullUserIds.keyAt(i) != mFullUserIds.valueAt(i)
681 && mFullUserIds.valueAt(i) == mFullUserId) {
682 pw.print(", profile_user=" + mFullUserIds.keyAt(i));
683 }
684 }
685 pw.println();
RoboErik4646d282014-05-13 10:13:04 -0700686 String indent = prefix + " ";
Jaewan Kima7dce192017-02-16 17:10:54 +0900687 pw.println(indent + "Volume key long-press listener: " + mOnVolumeKeyLongPressListener);
688 pw.println(indent + "Volume key long-press listener package: " +
Jaewan Kim50269362016-12-23 11:22:02 +0900689 getCallingPackageName(mOnVolumeKeyLongPressListenerUid));
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800690 pw.println(indent + "Media key listener: " + mOnMediaKeyListener);
691 pw.println(indent + "Media key listener package: " +
692 getCallingPackageName(mOnMediaKeyListenerUid));
Jaewan Kima7dce192017-02-16 17:10:54 +0900693 pw.println(indent + "Callback: " + mCallback);
694 pw.println(indent + "Last MediaButtonReceiver: " + mLastMediaButtonReceiver);
695 pw.println(indent + "Restored MediaButtonReceiver: " + mRestoredMediaButtonReceiver);
696 mPriorityStack.dump(pw, indent);
697 }
698
Jaewan Kim92dea332017-02-02 11:52:08 +0900699 @Override
700 public void onMediaButtonSessionChanged(MediaSessionRecord oldMediaButtonSession,
701 MediaSessionRecord newMediaButtonSession) {
702 if (DEBUG_KEY_EVENT) {
Jaewan Kim98e4aaf2017-05-12 17:06:47 +0900703 Log.d(TAG, "Media button session is changed to " + newMediaButtonSession);
Jaewan Kim92dea332017-02-02 11:52:08 +0900704 }
705 synchronized (mLock) {
706 if (oldMediaButtonSession != null) {
707 mHandler.postSessionsChanged(oldMediaButtonSession.getUserId());
708 }
709 if (newMediaButtonSession != null) {
710 rememberMediaButtonReceiverLocked(newMediaButtonSession);
711 mHandler.postSessionsChanged(newMediaButtonSession.getUserId());
712 }
713 pushAddressedPlayerChangedLocked();
714 }
715 }
716
717 // Remember media button receiver and keep it in the persistent storage.
718 public void rememberMediaButtonReceiverLocked(MediaSessionRecord record) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900719 PendingIntent receiver = record.getMediaButtonReceiver();
Jaewan Kima7dce192017-02-16 17:10:54 +0900720 mLastMediaButtonReceiver = receiver;
Jaewan Kim92dea332017-02-02 11:52:08 +0900721 mRestoredMediaButtonReceiver = null;
722 String componentName = "";
723 if (receiver != null) {
724 ComponentName component = receiver.getIntent().getComponent();
725 if (component != null
726 && record.getPackageName().equals(component.getPackageName())) {
727 componentName = component.flattenToString();
728 }
RoboErik4646d282014-05-13 10:13:04 -0700729 }
Jaewan Kim92dea332017-02-02 11:52:08 +0900730 Settings.Secure.putStringForUser(mContentResolver,
731 Settings.System.MEDIA_BUTTON_RECEIVER,
732 componentName + COMPONENT_NAME_USER_ID_DELIM + record.getUserId(),
733 mFullUserId);
RoboErik4646d282014-05-13 10:13:04 -0700734 }
RoboErikc8f92d12015-01-05 16:48:07 -0800735
Jaewan Kima7dce192017-02-16 17:10:54 +0900736 private void pushAddressedPlayerChangedLocked() {
737 if (mCallback == null) {
738 return;
RoboErikc8f92d12015-01-05 16:48:07 -0800739 }
Jaewan Kima7dce192017-02-16 17:10:54 +0900740 try {
741 MediaSessionRecord mediaButtonSession = getMediaButtonSessionLocked();
742 if (mediaButtonSession != null) {
743 mCallback.onAddressedPlayerChangedToMediaSession(
744 new MediaSession.Token(mediaButtonSession.getControllerBinder()));
745 } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) {
746 mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
747 mCurrentFullUserRecord.mLastMediaButtonReceiver
748 .getIntent().getComponent());
749 } else if (mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) {
750 mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
751 mCurrentFullUserRecord.mRestoredMediaButtonReceiver);
752 }
753 } catch (RemoteException e) {
754 Log.w(TAG, "Failed to pushAddressedPlayerChangedLocked", e);
755 }
756 }
757
758 private MediaSessionRecord getMediaButtonSessionLocked() {
Jaewan Kim92dea332017-02-02 11:52:08 +0900759 return isGlobalPriorityActiveLocked()
760 ? mGlobalPrioritySession : mPriorityStack.getMediaButtonSession();
RoboErikc8f92d12015-01-05 16:48:07 -0800761 }
RoboErik4646d282014-05-13 10:13:04 -0700762 }
763
RoboErik2e7a9162014-06-04 16:53:45 -0700764 final class SessionsListenerRecord implements IBinder.DeathRecipient {
765 private final IActiveSessionsListener mListener;
RoboErik7aef77b2014-08-08 15:56:54 -0700766 private final ComponentName mComponentName;
RoboErik2e7a9162014-06-04 16:53:45 -0700767 private final int mUserId;
RoboErik7aef77b2014-08-08 15:56:54 -0700768 private final int mPid;
769 private final int mUid;
RoboErik2e7a9162014-06-04 16:53:45 -0700770
RoboErik7aef77b2014-08-08 15:56:54 -0700771 public SessionsListenerRecord(IActiveSessionsListener listener,
772 ComponentName componentName,
773 int userId, int pid, int uid) {
RoboErik2e7a9162014-06-04 16:53:45 -0700774 mListener = listener;
RoboErik7aef77b2014-08-08 15:56:54 -0700775 mComponentName = componentName;
RoboErik2e7a9162014-06-04 16:53:45 -0700776 mUserId = userId;
RoboErik7aef77b2014-08-08 15:56:54 -0700777 mPid = pid;
778 mUid = uid;
RoboErik2e7a9162014-06-04 16:53:45 -0700779 }
780
781 @Override
782 public void binderDied() {
783 synchronized (mLock) {
784 mSessionsListeners.remove(this);
785 }
786 }
787 }
788
RoboErik7aef77b2014-08-08 15:56:54 -0700789 final class SettingsObserver extends ContentObserver {
790 private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(
791 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
792
793 private SettingsObserver() {
794 super(null);
795 }
796
797 private void observe() {
798 mContentResolver.registerContentObserver(mSecureSettingsUri,
799 false, this, UserHandle.USER_ALL);
800 }
801
802 @Override
803 public void onChange(boolean selfChange, Uri uri) {
804 updateActiveSessionListeners();
805 }
806 }
807
RoboErik07c70772014-03-20 13:33:52 -0700808 class SessionManagerImpl extends ISessionManager.Stub {
RoboErik8a2cfc32014-05-16 11:19:38 -0700809 private static final String EXTRA_WAKELOCK_ACQUIRED =
810 "android.media.AudioService.WAKELOCK_ACQUIRED";
811 private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number
812
RoboErik9a9d0b52014-05-20 14:53:39 -0700813 private boolean mVoiceButtonDown = false;
814 private boolean mVoiceButtonHandled = false;
815
RoboErik07c70772014-03-20 13:33:52 -0700816 @Override
RoboErika5b02322014-05-07 17:05:49 -0700817 public ISession createSession(String packageName, ISessionCallback cb, String tag,
818 int userId) throws RemoteException {
RoboErik01fe6612014-02-13 14:19:04 -0800819 final int pid = Binder.getCallingPid();
820 final int uid = Binder.getCallingUid();
821 final long token = Binder.clearCallingIdentity();
822 try {
823 enforcePackageName(packageName, uid);
RoboErika5b02322014-05-07 17:05:49 -0700824 int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
825 false /* allowAll */, true /* requireFull */, "createSession", packageName);
RoboErik01fe6612014-02-13 14:19:04 -0800826 if (cb == null) {
827 throw new IllegalArgumentException("Controller callback cannot be null");
828 }
RoboErika5b02322014-05-07 17:05:49 -0700829 return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag)
830 .getSessionBinder();
RoboErike7880d82014-04-30 12:48:25 -0700831 } finally {
832 Binder.restoreCallingIdentity(token);
833 }
834 }
835
836 @Override
RoboErika5b02322014-05-07 17:05:49 -0700837 public List<IBinder> getSessions(ComponentName componentName, int userId) {
RoboErike7880d82014-04-30 12:48:25 -0700838 final int pid = Binder.getCallingPid();
839 final int uid = Binder.getCallingUid();
840 final long token = Binder.clearCallingIdentity();
841
842 try {
RoboErik2e7a9162014-06-04 16:53:45 -0700843 int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid);
RoboErike7880d82014-04-30 12:48:25 -0700844 ArrayList<IBinder> binders = new ArrayList<IBinder>();
845 synchronized (mLock) {
Jaewan Kim101b4d52017-05-18 13:23:11 +0900846 List<MediaSessionRecord> records = getActiveSessionsLocked(resolvedUserId);
847 for (MediaSessionRecord record : records) {
848 binders.add(record.getControllerBinder().asBinder());
RoboErike7880d82014-04-30 12:48:25 -0700849 }
850 }
851 return binders;
RoboErik01fe6612014-02-13 14:19:04 -0800852 } finally {
853 Binder.restoreCallingIdentity(token);
854 }
855 }
RoboErika278ea72014-04-24 14:49:01 -0700856
RoboErik2e7a9162014-06-04 16:53:45 -0700857 @Override
858 public void addSessionsListener(IActiveSessionsListener listener,
859 ComponentName componentName, int userId) throws RemoteException {
860 final int pid = Binder.getCallingPid();
861 final int uid = Binder.getCallingUid();
862 final long token = Binder.clearCallingIdentity();
863
864 try {
865 int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid);
866 synchronized (mLock) {
867 int index = findIndexOfSessionsListenerLocked(listener);
868 if (index != -1) {
869 Log.w(TAG, "ActiveSessionsListener is already added, ignoring");
870 return;
871 }
872 SessionsListenerRecord record = new SessionsListenerRecord(listener,
RoboErik7aef77b2014-08-08 15:56:54 -0700873 componentName, resolvedUserId, pid, uid);
RoboErik2e7a9162014-06-04 16:53:45 -0700874 try {
875 listener.asBinder().linkToDeath(record, 0);
876 } catch (RemoteException e) {
877 Log.e(TAG, "ActiveSessionsListener is dead, ignoring it", e);
878 return;
879 }
880 mSessionsListeners.add(record);
881 }
882 } finally {
883 Binder.restoreCallingIdentity(token);
884 }
885 }
886
887 @Override
888 public void removeSessionsListener(IActiveSessionsListener listener)
889 throws RemoteException {
890 synchronized (mLock) {
891 int index = findIndexOfSessionsListenerLocked(listener);
892 if (index != -1) {
893 SessionsListenerRecord record = mSessionsListeners.remove(index);
894 try {
895 record.mListener.asBinder().unlinkToDeath(record, 0);
896 } catch (Exception e) {
897 // ignore exceptions, the record is being removed
898 }
899 }
900 }
901 }
902
RoboErik8a2cfc32014-05-16 11:19:38 -0700903 /**
904 * Handles the dispatching of the media button events to one of the
905 * registered listeners, or if there was none, broadcast an
906 * ACTION_MEDIA_BUTTON intent to the rest of the system.
907 *
908 * @param keyEvent a non-null KeyEvent whose key code is one of the
909 * supported media buttons
910 * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held
911 * while this key event is dispatched.
912 */
913 @Override
914 public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
915 if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) {
916 Log.w(TAG, "Attempted to dispatch null or non-media key event.");
917 return;
918 }
Jeff Brown38d3feb2015-03-19 18:26:30 -0700919
RoboErik8a2cfc32014-05-16 11:19:38 -0700920 final int pid = Binder.getCallingPid();
921 final int uid = Binder.getCallingUid();
922 final long token = Binder.clearCallingIdentity();
RoboErik8a2cfc32014-05-16 11:19:38 -0700923 try {
Jeff Brown221a8272015-03-23 13:53:09 -0700924 if (DEBUG) {
925 Log.d(TAG, "dispatchMediaKeyEvent, pid=" + pid + ", uid=" + uid + ", event="
926 + keyEvent);
927 }
Jeff Brown38d3feb2015-03-19 18:26:30 -0700928 if (!isUserSetupComplete()) {
929 // Global media key handling can have the side-effect of starting new
930 // activities which is undesirable while setup is in progress.
931 Slog.i(TAG, "Not dispatching media key event because user "
932 + "setup is in progress.");
933 return;
934 }
935
RoboErik8a2cfc32014-05-16 11:19:38 -0700936 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900937 boolean isGlobalPriorityActive = isGlobalPriorityActiveLocked();
Jaewan Kim51255012017-02-24 16:19:14 +0900938 if (isGlobalPriorityActive && uid != Process.SYSTEM_UID) {
939 // Prevent dispatching key event through reflection while the global
940 // priority session is active.
941 Slog.i(TAG, "Only the system can dispatch media key event "
942 + "to the global priority session.");
943 return;
944 }
Jaewan Kim98003d32017-02-24 18:33:04 +0900945 if (!isGlobalPriorityActive) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900946 if (mCurrentFullUserRecord.mOnMediaKeyListener != null) {
Jaewan Kim98003d32017-02-24 18:33:04 +0900947 if (DEBUG_KEY_EVENT) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900948 Log.d(TAG, "Send " + keyEvent + " to the media key listener");
Jaewan Kim98003d32017-02-24 18:33:04 +0900949 }
950 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900951 mCurrentFullUserRecord.mOnMediaKeyListener.onMediaKey(keyEvent,
Jaewan Kim98003d32017-02-24 18:33:04 +0900952 new MediaKeyListenerResultReceiver(keyEvent, needWakeLock));
953 return;
954 } catch (RemoteException e) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900955 Log.w(TAG, "Failed to send " + keyEvent
956 + " to the media key listener");
Jaewan Kim98003d32017-02-24 18:33:04 +0900957 }
958 }
959 }
Jaewan Kim51255012017-02-24 16:19:14 +0900960 if (!isGlobalPriorityActive && isVoiceKey(keyEvent.getKeyCode())) {
Jaewan Kim6e2b01c2017-01-19 16:33:14 -0800961 handleVoiceKeyEventLocked(keyEvent, needWakeLock);
RoboErik8a2cfc32014-05-16 11:19:38 -0700962 } else {
Jaewan Kim98003d32017-02-24 18:33:04 +0900963 dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
RoboErik8a2cfc32014-05-16 11:19:38 -0700964 }
965 }
966 } finally {
967 Binder.restoreCallingIdentity(token);
968 }
969 }
970
RoboErika278ea72014-04-24 14:49:01 -0700971 @Override
Jaewan Kimbd16f452017-02-03 16:21:38 +0900972 public void setCallback(ICallback callback) {
973 final int pid = Binder.getCallingPid();
974 final int uid = Binder.getCallingUid();
975 final long token = Binder.clearCallingIdentity();
976 try {
Amith Yamasanie259ad22017-04-24 11:30:19 -0700977 if (!UserHandle.isSameApp(uid, Process.BLUETOOTH_UID)) {
Jaewan Kimbd16f452017-02-03 16:21:38 +0900978 throw new SecurityException("Only Bluetooth service processes can set"
979 + " Callback");
980 }
981 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +0900982 int userId = UserHandle.getUserId(uid);
983 FullUserRecord user = getFullUserRecordLocked(userId);
984 if (user == null || user.mFullUserId != userId) {
985 Log.w(TAG, "Only the full user can set the callback"
986 + ", userId=" + userId);
987 return;
988 }
989 user.mCallback = callback;
990 Log.d(TAG, "The callback " + user.mCallback
Jaewan Kimbd16f452017-02-03 16:21:38 +0900991 + " is set by " + getCallingPackageName(uid));
Jaewan Kima7dce192017-02-16 17:10:54 +0900992 if (user.mCallback == null) {
Jaewan Kimbd16f452017-02-03 16:21:38 +0900993 return;
994 }
995 try {
Jaewan Kima7dce192017-02-16 17:10:54 +0900996 user.mCallback.asBinder().linkToDeath(
Jaewan Kimbd16f452017-02-03 16:21:38 +0900997 new IBinder.DeathRecipient() {
998 @Override
999 public void binderDied() {
1000 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001001 user.mCallback = null;
Jaewan Kimbd16f452017-02-03 16:21:38 +09001002 }
1003 }
1004 }, 0);
Jaewan Kima7dce192017-02-16 17:10:54 +09001005 user.pushAddressedPlayerChangedLocked();
Jaewan Kimbd16f452017-02-03 16:21:38 +09001006 } catch (RemoteException e) {
1007 Log.w(TAG, "Failed to set callback", e);
Jaewan Kima7dce192017-02-16 17:10:54 +09001008 user.mCallback = null;
Jaewan Kimbd16f452017-02-03 16:21:38 +09001009 }
1010 }
1011 } finally {
1012 Binder.restoreCallingIdentity(token);
1013 }
1014 }
1015
1016 @Override
Jaewan Kim50269362016-12-23 11:22:02 +09001017 public void setOnVolumeKeyLongPressListener(IOnVolumeKeyLongPressListener listener) {
1018 final int pid = Binder.getCallingPid();
1019 final int uid = Binder.getCallingUid();
1020 final long token = Binder.clearCallingIdentity();
1021 try {
1022 // Enforce SET_VOLUME_KEY_LONG_PRESS_LISTENER permission.
1023 if (getContext().checkPermission(
1024 android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER, pid, uid)
1025 != PackageManager.PERMISSION_GRANTED) {
1026 throw new SecurityException("Must hold the SET_VOLUME_KEY_LONG_PRESS_LISTENER" +
1027 " permission.");
1028 }
1029
1030 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001031 int userId = UserHandle.getUserId(uid);
1032 FullUserRecord user = getFullUserRecordLocked(userId);
1033 if (user == null || user.mFullUserId != userId) {
1034 Log.w(TAG, "Only the full user can set the volume key long-press listener"
1035 + ", userId=" + userId);
1036 return;
1037 }
Jaewan Kim50269362016-12-23 11:22:02 +09001038 if (user.mOnVolumeKeyLongPressListener != null &&
1039 user.mOnVolumeKeyLongPressListenerUid != uid) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001040 Log.w(TAG, "The volume key long-press listener cannot be reset"
1041 + " by another app , mOnVolumeKeyLongPressListener="
1042 + user.mOnVolumeKeyLongPressListenerUid
1043 + ", uid=" + uid);
Jaewan Kim50269362016-12-23 11:22:02 +09001044 return;
1045 }
1046
1047 user.mOnVolumeKeyLongPressListener = listener;
1048 user.mOnVolumeKeyLongPressListenerUid = uid;
1049
Jaewan Kima7dce192017-02-16 17:10:54 +09001050 Log.d(TAG, "The volume key long-press listener "
Jaewan Kim50269362016-12-23 11:22:02 +09001051 + listener + " is set by " + getCallingPackageName(uid));
1052
1053 if (user.mOnVolumeKeyLongPressListener != null) {
1054 try {
1055 user.mOnVolumeKeyLongPressListener.asBinder().linkToDeath(
1056 new IBinder.DeathRecipient() {
1057 @Override
1058 public void binderDied() {
1059 synchronized (mLock) {
1060 user.mOnVolumeKeyLongPressListener = null;
1061 }
1062 }
1063 }, 0);
1064 } catch (RemoteException e) {
1065 Log.w(TAG, "Failed to set death recipient "
1066 + user.mOnVolumeKeyLongPressListener);
1067 user.mOnVolumeKeyLongPressListener = null;
1068 }
1069 }
1070 }
1071 } finally {
1072 Binder.restoreCallingIdentity(token);
1073 }
1074 }
1075
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001076 @Override
1077 public void setOnMediaKeyListener(IOnMediaKeyListener listener) {
1078 final int pid = Binder.getCallingPid();
1079 final int uid = Binder.getCallingUid();
1080 final long token = Binder.clearCallingIdentity();
1081 try {
1082 // Enforce SET_MEDIA_KEY_LISTENER permission.
1083 if (getContext().checkPermission(
1084 android.Manifest.permission.SET_MEDIA_KEY_LISTENER, pid, uid)
1085 != PackageManager.PERMISSION_GRANTED) {
1086 throw new SecurityException("Must hold the SET_MEDIA_KEY_LISTENER" +
1087 " permission.");
1088 }
1089
1090 synchronized (mLock) {
1091 int userId = UserHandle.getUserId(uid);
Jaewan Kima7dce192017-02-16 17:10:54 +09001092 FullUserRecord user = getFullUserRecordLocked(userId);
1093 if (user == null || user.mFullUserId != userId) {
1094 Log.w(TAG, "Only the full user can set the media key listener"
1095 + ", userId=" + userId);
1096 return;
1097 }
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001098 if (user.mOnMediaKeyListener != null && user.mOnMediaKeyListenerUid != uid) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001099 Log.w(TAG, "The media key listener cannot be reset by another app. "
1100 + ", mOnMediaKeyListenerUid=" + user.mOnMediaKeyListenerUid
1101 + ", uid=" + uid);
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001102 return;
1103 }
1104
1105 user.mOnMediaKeyListener = listener;
1106 user.mOnMediaKeyListenerUid = uid;
1107
Jaewan Kima7dce192017-02-16 17:10:54 +09001108 Log.d(TAG, "The media key listener " + user.mOnMediaKeyListener
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001109 + " is set by " + getCallingPackageName(uid));
1110
1111 if (user.mOnMediaKeyListener != null) {
1112 try {
1113 user.mOnMediaKeyListener.asBinder().linkToDeath(
1114 new IBinder.DeathRecipient() {
1115 @Override
1116 public void binderDied() {
1117 synchronized (mLock) {
1118 user.mOnMediaKeyListener = null;
1119 }
1120 }
1121 }, 0);
1122 } catch (RemoteException e) {
1123 Log.w(TAG, "Failed to set death recipient " + user.mOnMediaKeyListener);
1124 user.mOnMediaKeyListener = null;
1125 }
1126 }
1127 }
1128 } finally {
1129 Binder.restoreCallingIdentity(token);
1130 }
1131 }
1132
Jaewan Kim50269362016-12-23 11:22:02 +09001133 /**
1134 * Handles the dispatching of the volume button events to one of the
1135 * registered listeners. If there's a volume key long-press listener and
1136 * there's no active global priority session, long-pressess will be sent to the
1137 * long-press listener instead of adjusting volume.
1138 *
1139 * @param keyEvent a non-null KeyEvent whose key code is one of the
1140 * {@link KeyEvent#KEYCODE_VOLUME_UP},
1141 * {@link KeyEvent#KEYCODE_VOLUME_DOWN},
1142 * or {@link KeyEvent#KEYCODE_VOLUME_MUTE}.
1143 * @param stream stream type to adjust volume.
1144 * @param musicOnly true if both UI nor haptic feedback aren't needed when adjust volume.
1145 */
1146 @Override
1147 public void dispatchVolumeKeyEvent(KeyEvent keyEvent, int stream, boolean musicOnly) {
1148 if (keyEvent == null ||
1149 (keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP
1150 && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN
1151 && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_MUTE)) {
1152 Log.w(TAG, "Attempted to dispatch null or non-volume key event.");
1153 return;
1154 }
1155
1156 final int pid = Binder.getCallingPid();
1157 final int uid = Binder.getCallingUid();
1158 final long token = Binder.clearCallingIdentity();
1159
Jaewan Kimb2781e72017-03-02 09:57:09 +09001160 if (DEBUG_KEY_EVENT) {
Jaewan Kim50269362016-12-23 11:22:02 +09001161 Log.d(TAG, "dispatchVolumeKeyEvent, pid=" + pid + ", uid=" + uid + ", event="
1162 + keyEvent);
1163 }
1164
1165 try {
1166 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001167 if (isGlobalPriorityActiveLocked()
1168 || mCurrentFullUserRecord.mOnVolumeKeyLongPressListener == null) {
Jaewan Kim50269362016-12-23 11:22:02 +09001169 dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly);
1170 } else {
1171 // TODO: Consider the case when both volume up and down keys are pressed
1172 // at the same time.
1173 if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
1174 if (keyEvent.getRepeatCount() == 0) {
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001175 // Keeps the copy of the KeyEvent because it can be reused.
Jaewan Kima7dce192017-02-16 17:10:54 +09001176 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent =
1177 KeyEvent.obtain(keyEvent);
1178 mCurrentFullUserRecord.mInitialDownVolumeStream = stream;
1179 mCurrentFullUserRecord.mInitialDownMusicOnly = musicOnly;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001180 mHandler.sendMessageDelayed(
1181 mHandler.obtainMessage(
Jaewan Kima7dce192017-02-16 17:10:54 +09001182 MessageHandler.MSG_VOLUME_INITIAL_DOWN,
1183 mCurrentFullUserRecord.mFullUserId, 0),
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001184 mLongPressTimeout);
Jaewan Kim50269362016-12-23 11:22:02 +09001185 }
1186 if (keyEvent.getRepeatCount() > 0 || keyEvent.isLongPress()) {
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) {
Jaewan Kim50269362016-12-23 11:22:02 +09001189 dispatchVolumeKeyLongPressLocked(
Jaewan Kima7dce192017-02-16 17:10:54 +09001190 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent);
Jaewan Kim50269362016-12-23 11:22:02 +09001191 // Mark that the key is already handled.
Jaewan Kima7dce192017-02-16 17:10:54 +09001192 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = null;
Jaewan Kim50269362016-12-23 11:22:02 +09001193 }
1194 dispatchVolumeKeyLongPressLocked(keyEvent);
1195 }
1196 } else { // if up
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001197 mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN);
Jaewan Kima7dce192017-02-16 17:10:54 +09001198 if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null
1199 && mCurrentFullUserRecord.mInitialDownVolumeKeyEvent
1200 .getDownTime() == keyEvent.getDownTime()) {
Jaewan Kim50269362016-12-23 11:22:02 +09001201 // Short-press. Should change volume.
1202 dispatchVolumeKeyEventLocked(
Jaewan Kima7dce192017-02-16 17:10:54 +09001203 mCurrentFullUserRecord.mInitialDownVolumeKeyEvent,
1204 mCurrentFullUserRecord.mInitialDownVolumeStream,
1205 mCurrentFullUserRecord.mInitialDownMusicOnly);
Jaewan Kim50269362016-12-23 11:22:02 +09001206 dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly);
1207 } else {
1208 dispatchVolumeKeyLongPressLocked(keyEvent);
1209 }
1210 }
1211 }
1212 }
1213 } finally {
1214 Binder.restoreCallingIdentity(token);
1215 }
1216 }
1217
Jaewan Kim50269362016-12-23 11:22:02 +09001218 private void dispatchVolumeKeyEventLocked(
1219 KeyEvent keyEvent, int stream, boolean musicOnly) {
1220 boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
1221 boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
1222 int direction = 0;
1223 boolean isMute = false;
1224 switch (keyEvent.getKeyCode()) {
1225 case KeyEvent.KEYCODE_VOLUME_UP:
1226 direction = AudioManager.ADJUST_RAISE;
1227 break;
1228 case KeyEvent.KEYCODE_VOLUME_DOWN:
1229 direction = AudioManager.ADJUST_LOWER;
1230 break;
1231 case KeyEvent.KEYCODE_VOLUME_MUTE:
1232 isMute = true;
1233 break;
1234 }
1235 if (down || up) {
1236 int flags = AudioManager.FLAG_FROM_KEY;
1237 if (musicOnly) {
1238 // This flag is used when the screen is off to only affect active media.
1239 flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
1240 } else {
1241 // These flags are consistent with the home screen
1242 if (up) {
1243 flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
1244 } else {
1245 flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
1246 }
1247 }
1248 if (direction != 0) {
1249 // If this is action up we want to send a beep for non-music events
1250 if (up) {
1251 direction = 0;
1252 }
1253 dispatchAdjustVolumeLocked(stream, direction, flags);
1254 } else if (isMute) {
1255 if (down && keyEvent.getRepeatCount() == 0) {
1256 dispatchAdjustVolumeLocked(stream, AudioManager.ADJUST_TOGGLE_MUTE, flags);
1257 }
1258 }
1259 }
1260 }
1261
1262 @Override
RoboErik7c82ced2014-12-04 17:39:08 -08001263 public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) {
RoboErikb69ffd42014-05-30 14:57:59 -07001264 final long token = Binder.clearCallingIdentity();
1265 try {
1266 synchronized (mLock) {
Jaewan Kim50269362016-12-23 11:22:02 +09001267 dispatchAdjustVolumeLocked(suggestedStream, delta, flags);
RoboErikb69ffd42014-05-30 14:57:59 -07001268 }
1269 } finally {
1270 Binder.restoreCallingIdentity(token);
1271 }
1272 }
1273
1274 @Override
RoboErik19c95182014-06-23 15:38:48 -07001275 public void setRemoteVolumeController(IRemoteVolumeController rvc) {
1276 final int pid = Binder.getCallingPid();
1277 final int uid = Binder.getCallingUid();
1278 final long token = Binder.clearCallingIdentity();
1279 try {
John Spurlockeb69e242015-02-17 17:15:04 -05001280 enforceSystemUiPermission("listen for volume changes", pid, uid);
RoboErik19c95182014-06-23 15:38:48 -07001281 mRvc = rvc;
1282 } finally {
1283 Binder.restoreCallingIdentity(token);
1284 }
1285 }
1286
1287 @Override
RoboErikde9ba392014-09-26 12:51:01 -07001288 public boolean isGlobalPriorityActive() {
Jaewan Kim51255012017-02-24 16:19:14 +09001289 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001290 return isGlobalPriorityActiveLocked();
Jaewan Kim51255012017-02-24 16:19:14 +09001291 }
RoboErikde9ba392014-09-26 12:51:01 -07001292 }
1293
1294 @Override
RoboErika278ea72014-04-24 14:49:01 -07001295 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001296 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
RoboErika278ea72014-04-24 14:49:01 -07001297
1298 pw.println("MEDIA SESSION SERVICE (dumpsys media_session)");
1299 pw.println();
1300
1301 synchronized (mLock) {
RoboErika08adb242014-11-21 18:28:18 -08001302 pw.println(mSessionsListeners.size() + " sessions listeners.");
Jaewan Kima7dce192017-02-16 17:10:54 +09001303 pw.println("Global priority session is " + mGlobalPrioritySession);
Jaewan Kim101b4d52017-05-18 13:23:11 +09001304 if (mGlobalPrioritySession != null) {
1305 mGlobalPrioritySession.dump(pw, " ");
1306 }
RoboErik4646d282014-05-13 10:13:04 -07001307 pw.println("User Records:");
Jaewan Kime0ca3f32017-02-16 15:52:39 +09001308 int count = mUserRecords.size();
RoboErika278ea72014-04-24 14:49:01 -07001309 for (int i = 0; i < count; i++) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001310 mUserRecords.valueAt(i).dumpLocked(pw, "");
RoboErika278ea72014-04-24 14:49:01 -07001311 }
Jaewan Kim92dea332017-02-02 11:52:08 +09001312 mAudioPlaybackMonitor.dump(pw, "");
RoboErika278ea72014-04-24 14:49:01 -07001313 }
1314 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001315
RoboErik2e7a9162014-06-04 16:53:45 -07001316 private int verifySessionsRequest(ComponentName componentName, int userId, final int pid,
1317 final int uid) {
1318 String packageName = null;
1319 if (componentName != null) {
1320 // If they gave us a component name verify they own the
1321 // package
1322 packageName = componentName.getPackageName();
1323 enforcePackageName(packageName, uid);
1324 }
1325 // Check that they can make calls on behalf of the user and
1326 // get the final user id
1327 int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
1328 true /* allowAll */, true /* requireFull */, "getSessions", packageName);
1329 // Check if they have the permissions or their component is
1330 // enabled for the user they're calling from.
1331 enforceMediaPermissions(componentName, pid, uid, resolvedUserId);
1332 return resolvedUserId;
1333 }
1334
Jaewan Kim50269362016-12-23 11:22:02 +09001335 private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001336 MediaSessionRecord session = isGlobalPriorityActiveLocked() ? mGlobalPrioritySession
1337 : mCurrentFullUserRecord.mPriorityStack.getDefaultVolumeSession();
Jaewan Kim50269362016-12-23 11:22:02 +09001338
RoboErik9c785402014-11-11 16:52:26 -08001339 boolean preferSuggestedStream = false;
1340 if (isValidLocalStreamType(suggestedStream)
1341 && AudioSystem.isStreamActive(suggestedStream, 0)) {
1342 preferSuggestedStream = true;
1343 }
Jaewan Kimb2781e72017-03-02 09:57:09 +09001344 if (DEBUG_KEY_EVENT) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +09001345 Log.d(TAG, "Adjusting " + session + " by " + direction + ". flags="
1346 + flags + ", suggestedStream=" + suggestedStream
1347 + ", preferSuggestedStream=" + preferSuggestedStream);
1348 }
RoboErik9c785402014-11-11 16:52:26 -08001349 if (session == null || preferSuggestedStream) {
RoboErik94c716e2014-09-14 13:54:31 -07001350 if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0
1351 && !AudioSystem.isStreamActive(AudioManager.STREAM_MUSIC, 0)) {
RoboErik3c45c292014-07-08 16:47:31 -07001352 if (DEBUG) {
1353 Log.d(TAG, "No active session to adjust, skipping media only volume event");
RoboErik3c45c292014-07-08 16:47:31 -07001354 }
RoboErikb7c014c2014-07-22 15:58:22 -07001355 return;
RoboErik3c45c292014-07-08 16:47:31 -07001356 }
Shibin George19e84042016-06-14 20:42:13 +05301357
1358 // Execute mAudioService.adjustSuggestedStreamVolume() on
1359 // handler thread of MediaSessionService.
1360 // This will release the MediaSessionService.mLock sooner and avoid
1361 // a potential deadlock between MediaSessionService.mLock and
1362 // ActivityManagerService lock.
1363 mHandler.post(new Runnable() {
1364 @Override
1365 public void run() {
1366 try {
1367 String packageName = getContext().getOpPackageName();
1368 mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream,
1369 flags, packageName, TAG);
1370 } catch (RemoteException e) {
1371 Log.e(TAG, "Error adjusting default volume.", e);
Hyundo Moon739d6c22017-09-18 17:01:48 +09001372 } catch (IllegalArgumentException e) {
1373 Log.e(TAG, "Cannot adjust volume: direction=" + direction
1374 + ", suggestedStream=" + suggestedStream + ", flags=" + flags,
1375 e);
Shibin George19e84042016-06-14 20:42:13 +05301376 }
1377 }
1378 });
RoboErikb69ffd42014-05-30 14:57:59 -07001379 } else {
RoboErik0dac35a2014-08-12 15:48:49 -07001380 session.adjustVolume(direction, flags, getContext().getPackageName(),
Jaewan Kim8f729082016-06-21 12:36:26 +09001381 Process.SYSTEM_UID, true);
RoboErikb69ffd42014-05-30 14:57:59 -07001382 }
1383 }
1384
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001385 private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) {
RoboErik9a9d0b52014-05-20 14:53:39 -07001386 int action = keyEvent.getAction();
1387 boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0;
1388 if (action == KeyEvent.ACTION_DOWN) {
1389 if (keyEvent.getRepeatCount() == 0) {
1390 mVoiceButtonDown = true;
1391 mVoiceButtonHandled = false;
1392 } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) {
1393 mVoiceButtonHandled = true;
1394 startVoiceInput(needWakeLock);
1395 }
1396 } else if (action == KeyEvent.ACTION_UP) {
1397 if (mVoiceButtonDown) {
1398 mVoiceButtonDown = false;
1399 if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
1400 // Resend the down then send this event through
1401 KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN);
Jaewan Kim98003d32017-02-24 18:33:04 +09001402 dispatchMediaKeyEventLocked(downEvent, needWakeLock);
1403 dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
RoboErik9a9d0b52014-05-20 14:53:39 -07001404 }
1405 }
1406 }
1407 }
1408
Jaewan Kim98003d32017-02-24 18:33:04 +09001409 private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001410 MediaSessionRecord session = mCurrentFullUserRecord.getMediaButtonSessionLocked();
RoboErik9a9d0b52014-05-20 14:53:39 -07001411 if (session != null) {
Jaewan Kim50269362016-12-23 11:22:02 +09001412 if (DEBUG_KEY_EVENT) {
Jaewan Kim5e1476e2016-07-19 22:25:39 +09001413 Log.d(TAG, "Sending " + keyEvent + " to " + session);
RoboErik9a9d0b52014-05-20 14:53:39 -07001414 }
1415 if (needWakeLock) {
1416 mKeyEventReceiver.aquireWakeLockLocked();
1417 }
Jaewan Kim50269362016-12-23 11:22:02 +09001418 // 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 -07001419 session.sendMediaButton(keyEvent,
1420 needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
Jaewan Kim8f729082016-06-21 12:36:26 +09001421 mKeyEventReceiver, Process.SYSTEM_UID,
Donghyun Cho1ea56832016-02-23 16:30:07 +09001422 getContext().getPackageName());
Jaewan Kima7dce192017-02-16 17:10:54 +09001423 if (mCurrentFullUserRecord.mCallback != null) {
Jaewan Kimbd16f452017-02-03 16:21:38 +09001424 try {
Jaewan Kima7dce192017-02-16 17:10:54 +09001425 mCurrentFullUserRecord.mCallback.onMediaKeyEventDispatchedToMediaSession(
1426 keyEvent,
Jaewan Kimbd16f452017-02-03 16:21:38 +09001427 new MediaSession.Token(session.getControllerBinder()));
1428 } catch (RemoteException e) {
1429 Log.w(TAG, "Failed to send callback", e);
1430 }
1431 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001432 } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null
1433 || mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) {
1434 if (needWakeLock) {
1435 mKeyEventReceiver.aquireWakeLockLocked();
1436 }
1437 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
1438 mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1439 mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
1440 try {
1441 if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) {
1442 PendingIntent receiver = mCurrentFullUserRecord.mLastMediaButtonReceiver;
1443 if (DEBUG_KEY_EVENT) {
1444 Log.d(TAG, "Sending " + keyEvent
Jaewan Kim92dea332017-02-02 11:52:08 +09001445 + " to the last known PendingIntent " + receiver);
Jaewan Kima7dce192017-02-16 17:10:54 +09001446 }
1447 receiver.send(getContext(),
1448 needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
1449 mediaButtonIntent, mKeyEventReceiver, mHandler);
1450 if (mCurrentFullUserRecord.mCallback != null) {
1451 ComponentName componentName = mCurrentFullUserRecord
1452 .mLastMediaButtonReceiver.getIntent().getComponent();
1453 if (componentName != null) {
1454 mCurrentFullUserRecord.mCallback
1455 .onMediaKeyEventDispatchedToMediaButtonReceiver(
1456 keyEvent, componentName);
Jaewan Kimbd16f452017-02-03 16:21:38 +09001457 }
RoboErikc8f92d12015-01-05 16:48:07 -08001458 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001459 } else {
1460 ComponentName receiver =
1461 mCurrentFullUserRecord.mRestoredMediaButtonReceiver;
1462 if (DEBUG_KEY_EVENT) {
1463 Log.d(TAG, "Sending " + keyEvent + " to the restored intent "
1464 + receiver);
1465 }
1466 mediaButtonIntent.setComponent(receiver);
1467 getContext().sendBroadcastAsUser(mediaButtonIntent,
Jaewan Kim92dea332017-02-02 11:52:08 +09001468 UserHandle.of(mCurrentFullUserRecord
1469 .mRestoredMediaButtonReceiverUserId));
Jaewan Kima7dce192017-02-16 17:10:54 +09001470 if (mCurrentFullUserRecord.mCallback != null) {
1471 mCurrentFullUserRecord.mCallback
1472 .onMediaKeyEventDispatchedToMediaButtonReceiver(
1473 keyEvent, receiver);
1474 }
RoboErikb214efb2014-07-24 13:20:30 -07001475 }
Jaewan Kima7dce192017-02-16 17:10:54 +09001476 } catch (CanceledException e) {
1477 Log.i(TAG, "Error sending key event to media button receiver "
1478 + mCurrentFullUserRecord.mLastMediaButtonReceiver, e);
1479 } catch (RemoteException e) {
1480 Log.w(TAG, "Failed to send callback", e);
RoboErik9a9d0b52014-05-20 14:53:39 -07001481 }
RoboErik9a9d0b52014-05-20 14:53:39 -07001482 }
1483 }
1484
1485 private void startVoiceInput(boolean needWakeLock) {
1486 Intent voiceIntent = null;
1487 // select which type of search to launch:
1488 // - screen on and device unlocked: action is ACTION_WEB_SEARCH
1489 // - device locked or screen off: action is
1490 // ACTION_VOICE_SEARCH_HANDS_FREE
1491 // with EXTRA_SECURE set to true if the device is securely locked
1492 PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
1493 boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1494 if (!isLocked && pm.isScreenOn()) {
1495 voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH);
1496 Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH");
1497 } else {
1498 voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
1499 voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE,
1500 isLocked && mKeyguardManager.isKeyguardSecure());
1501 Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE");
1502 }
1503 // start the search activity
1504 if (needWakeLock) {
1505 mMediaEventWakeLock.acquire();
1506 }
1507 try {
1508 if (voiceIntent != null) {
1509 voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1510 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Jaewan Kim8f729082016-06-21 12:36:26 +09001511 if (DEBUG) Log.d(TAG, "voiceIntent: " + voiceIntent);
RoboErik9a9d0b52014-05-20 14:53:39 -07001512 getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT);
1513 }
1514 } catch (ActivityNotFoundException e) {
1515 Log.w(TAG, "No activity for search: " + e);
1516 } finally {
1517 if (needWakeLock) {
1518 mMediaEventWakeLock.release();
1519 }
1520 }
1521 }
1522
1523 private boolean isVoiceKey(int keyCode) {
Jaewan Kimba18d8e2017-05-12 17:37:57 +09001524 return keyCode == KeyEvent.KEYCODE_HEADSETHOOK
Jaewan Kimfdb612e2017-07-01 09:23:41 +09001525 || (!mHasFeatureLeanback && keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
RoboErik9a9d0b52014-05-20 14:53:39 -07001526 }
1527
Jeff Brown38d3feb2015-03-19 18:26:30 -07001528 private boolean isUserSetupComplete() {
1529 return Settings.Secure.getIntForUser(getContext().getContentResolver(),
1530 Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
1531 }
1532
RoboErik9c785402014-11-11 16:52:26 -08001533 // we only handle public stream types, which are 0-5
1534 private boolean isValidLocalStreamType(int streamType) {
1535 return streamType >= AudioManager.STREAM_VOICE_CALL
1536 && streamType <= AudioManager.STREAM_NOTIFICATION;
1537 }
1538
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001539 private class MediaKeyListenerResultReceiver extends ResultReceiver implements Runnable {
1540 private KeyEvent mKeyEvent;
1541 private boolean mNeedWakeLock;
1542 private boolean mHandled;
1543
1544 private MediaKeyListenerResultReceiver(KeyEvent keyEvent, boolean needWakeLock) {
1545 super(mHandler);
1546 mHandler.postDelayed(this, MEDIA_KEY_LISTENER_TIMEOUT);
1547 mKeyEvent = keyEvent;
1548 mNeedWakeLock = needWakeLock;
1549 }
1550
1551 @Override
1552 public void run() {
1553 Log.d(TAG, "The media key listener is timed-out for " + mKeyEvent);
1554 dispatchMediaKeyEvent();
1555 }
1556
1557 @Override
1558 protected void onReceiveResult(int resultCode, Bundle resultData) {
1559 if (resultCode == MediaSessionManager.RESULT_MEDIA_KEY_HANDLED) {
1560 mHandled = true;
1561 mHandler.removeCallbacks(this);
1562 return;
1563 }
1564 dispatchMediaKeyEvent();
1565 }
1566
1567 private void dispatchMediaKeyEvent() {
1568 if (mHandled) {
1569 return;
1570 }
1571 mHandled = true;
1572 mHandler.removeCallbacks(this);
1573 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001574 if (!isGlobalPriorityActiveLocked()
Jaewan Kim98003d32017-02-24 18:33:04 +09001575 && isVoiceKey(mKeyEvent.getKeyCode())) {
1576 handleVoiceKeyEventLocked(mKeyEvent, mNeedWakeLock);
1577 } else {
1578 dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock);
1579 }
Jaewan Kim6e2b01c2017-01-19 16:33:14 -08001580 }
1581 }
1582 }
1583
RoboErik418c10c2014-05-19 09:25:25 -07001584 private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler);
1585
RoboErikb214efb2014-07-24 13:20:30 -07001586 class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable,
1587 PendingIntent.OnFinished {
RoboErik418c10c2014-05-19 09:25:25 -07001588 private final Handler mHandler;
1589 private int mRefCount = 0;
1590 private int mLastTimeoutId = 0;
1591
1592 public KeyEventWakeLockReceiver(Handler handler) {
1593 super(handler);
1594 mHandler = handler;
1595 }
1596
1597 public void onTimeout() {
1598 synchronized (mLock) {
1599 if (mRefCount == 0) {
1600 // We've already released it, so just return
1601 return;
1602 }
1603 mLastTimeoutId++;
1604 mRefCount = 0;
1605 releaseWakeLockLocked();
1606 }
1607 }
1608
1609 public void aquireWakeLockLocked() {
1610 if (mRefCount == 0) {
1611 mMediaEventWakeLock.acquire();
1612 }
1613 mRefCount++;
1614 mHandler.removeCallbacks(this);
1615 mHandler.postDelayed(this, WAKELOCK_TIMEOUT);
1616
1617 }
1618
1619 @Override
1620 public void run() {
1621 onTimeout();
1622 }
1623
RoboErik8a2cfc32014-05-16 11:19:38 -07001624 @Override
1625 protected void onReceiveResult(int resultCode, Bundle resultData) {
RoboErik418c10c2014-05-19 09:25:25 -07001626 if (resultCode < mLastTimeoutId) {
1627 // Ignore results from calls that were before the last
1628 // timeout, just in case.
1629 return;
1630 } else {
1631 synchronized (mLock) {
1632 if (mRefCount > 0) {
1633 mRefCount--;
1634 if (mRefCount == 0) {
1635 releaseWakeLockLocked();
1636 }
1637 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001638 }
1639 }
1640 }
RoboErik418c10c2014-05-19 09:25:25 -07001641
1642 private void releaseWakeLockLocked() {
1643 mMediaEventWakeLock.release();
1644 mHandler.removeCallbacks(this);
1645 }
RoboErikb214efb2014-07-24 13:20:30 -07001646
1647 @Override
1648 public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode,
1649 String resultData, Bundle resultExtras) {
1650 onReceiveResult(resultCode, null);
1651 }
RoboErik8a2cfc32014-05-16 11:19:38 -07001652 };
1653
1654 BroadcastReceiver mKeyEventDone = new BroadcastReceiver() {
1655 @Override
1656 public void onReceive(Context context, Intent intent) {
1657 if (intent == null) {
1658 return;
1659 }
1660 Bundle extras = intent.getExtras();
1661 if (extras == null) {
1662 return;
1663 }
1664 synchronized (mLock) {
1665 if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED)
1666 && mMediaEventWakeLock.isHeld()) {
1667 mMediaEventWakeLock.release();
1668 }
1669 }
1670 }
1671 };
RoboErik01fe6612014-02-13 14:19:04 -08001672 }
1673
RoboErik2e7a9162014-06-04 16:53:45 -07001674 final class MessageHandler extends Handler {
1675 private static final int MSG_SESSIONS_CHANGED = 1;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001676 private static final int MSG_VOLUME_INITIAL_DOWN = 2;
Jaewan Kim92dea332017-02-02 11:52:08 +09001677 private final SparseArray<Integer> mIntegerCache = new SparseArray<>();
RoboErik2e7a9162014-06-04 16:53:45 -07001678
1679 @Override
1680 public void handleMessage(Message msg) {
1681 switch (msg.what) {
1682 case MSG_SESSIONS_CHANGED:
Jaewan Kim92dea332017-02-02 11:52:08 +09001683 pushSessionsChanged((int) msg.obj);
RoboErik2e7a9162014-06-04 16:53:45 -07001684 break;
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001685 case MSG_VOLUME_INITIAL_DOWN:
1686 synchronized (mLock) {
Jaewan Kima7dce192017-02-16 17:10:54 +09001687 FullUserRecord user = mUserRecords.get((int) msg.arg1);
Jaewan Kimd61a87b2017-02-17 23:14:10 +09001688 if (user != null && user.mInitialDownVolumeKeyEvent != null) {
1689 dispatchVolumeKeyLongPressLocked(user.mInitialDownVolumeKeyEvent);
1690 // Mark that the key is already handled.
1691 user.mInitialDownVolumeKeyEvent = null;
1692 }
1693 }
1694 break;
RoboErik2e7a9162014-06-04 16:53:45 -07001695 }
1696 }
1697
Jaewan Kim92dea332017-02-02 11:52:08 +09001698 public void postSessionsChanged(int userId) {
1699 // Use object instead of the arguments when posting message to remove pending requests.
1700 Integer userIdInteger = mIntegerCache.get(userId);
1701 if (userIdInteger == null) {
1702 userIdInteger = Integer.valueOf(userId);
1703 mIntegerCache.put(userId, userIdInteger);
1704 }
1705 removeMessages(MSG_SESSIONS_CHANGED, userIdInteger);
1706 obtainMessage(MSG_SESSIONS_CHANGED, userIdInteger).sendToTarget();
RoboErik2e7a9162014-06-04 16:53:45 -07001707 }
1708 }
RoboErik01fe6612014-02-13 14:19:04 -08001709}