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