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