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