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