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 | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 20 | import android.app.INotificationManager; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 21 | import android.app.KeyguardManager; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
| 23 | import android.app.PendingIntent.CanceledException; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 24 | import android.content.ActivityNotFoundException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 28 | import android.content.Context; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 31 | import android.content.pm.UserInfo; |
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; |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 34 | import android.media.AudioPlaybackConfiguration; |
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; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 39 | import android.media.session.ICallback; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 40 | import android.media.session.IOnMediaKeyListener; |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 41 | import android.media.session.IOnVolumeKeyLongPressListener; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 42 | import android.media.session.ISession; |
| 43 | import android.media.session.ISessionCallback; |
| 44 | import android.media.session.ISessionManager; |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 45 | import android.media.session.MediaSession; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 46 | import android.media.session.MediaSessionManager; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 47 | import android.net.Uri; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 48 | import android.os.Binder; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 49 | import android.os.Bundle; |
RoboErik | 8ae0f34 | 2014-02-24 18:02:08 -0800 | [diff] [blame] | 50 | import android.os.Handler; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 51 | import android.os.IBinder; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 52 | import android.os.Message; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 53 | import android.os.PowerManager; |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 54 | import android.os.Process; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 55 | import android.os.RemoteException; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 56 | import android.os.ResultReceiver; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 57 | import android.os.ServiceManager; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 58 | import android.os.UserHandle; |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 59 | import android.os.UserManager; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 60 | import android.provider.Settings; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 61 | import android.speech.RecognizerIntent; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 62 | import android.text.TextUtils; |
| 63 | import android.util.Log; |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 64 | import android.util.Slog; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 65 | import android.util.SparseArray; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 66 | import android.util.SparseIntArray; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 67 | import android.view.KeyEvent; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 68 | import android.view.ViewConfiguration; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 69 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 70 | import com.android.internal.util.DumpUtils; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 71 | import com.android.server.SystemService; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 72 | import com.android.server.Watchdog; |
| 73 | import com.android.server.Watchdog.Monitor; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 74 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 75 | import java.io.FileDescriptor; |
| 76 | import java.io.PrintWriter; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 77 | import java.util.ArrayList; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 78 | import java.util.List; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * System implementation of MediaSessionManager |
| 82 | */ |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 83 | public class MediaSessionService extends SystemService implements Monitor { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 84 | private static final String TAG = "MediaSessionService"; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 85 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 86 | // Leave log for key event always. |
| 87 | private static final boolean DEBUG_KEY_EVENT = true; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 88 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 89 | private static final int WAKELOCK_TIMEOUT = 5000; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 90 | private static final int MEDIA_KEY_LISTENER_TIMEOUT = 1000; |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 91 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 92 | private final SessionManagerImpl mSessionManagerImpl; |
| 93 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 94 | // Keeps the full user id for each user. |
| 95 | private final SparseIntArray mFullUserIds = new SparseIntArray(); |
| 96 | private final SparseArray<FullUserRecord> mUserRecords = new SparseArray<FullUserRecord>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 97 | private final ArrayList<SessionsListenerRecord> mSessionsListeners |
| 98 | = new ArrayList<SessionsListenerRecord>(); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 99 | private final Object mLock = new Object(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 100 | private final MessageHandler mHandler = new MessageHandler(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 101 | private final PowerManager.WakeLock mMediaEventWakeLock; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 102 | private final int mLongPressTimeout; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 103 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 104 | private KeyguardManager mKeyguardManager; |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 105 | private IAudioService mAudioService; |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 106 | private ContentResolver mContentResolver; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 107 | private SettingsObserver mSettingsObserver; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 108 | private INotificationManager mNotificationManager; |
Jaewan Kim | fdb612e | 2017-07-01 09:23:41 +0900 | [diff] [blame] | 109 | private boolean mHasFeatureLeanback; |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 110 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 111 | // The FullUserRecord of the current users. (i.e. The foreground user that isn't a profile) |
| 112 | // It's always not null after the MediaSessionService is started. |
| 113 | private FullUserRecord mCurrentFullUserRecord; |
| 114 | private MediaSessionRecord mGlobalPrioritySession; |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 115 | private AudioPlayerStateMonitor mAudioPlayerStateMonitor; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 116 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 117 | // Used to notify system UI when remote volume was changed. TODO find a |
| 118 | // better way to handle this. |
| 119 | private IRemoteVolumeController mRvc; |
| 120 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 121 | public MediaSessionService(Context context) { |
| 122 | super(context); |
| 123 | mSessionManagerImpl = new SessionManagerImpl(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 124 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 125 | mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent"); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 126 | mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 127 | mNotificationManager = INotificationManager.Stub.asInterface( |
| 128 | ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onStart() { |
| 133 | publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 134 | Watchdog.getInstance().addMonitor(this); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 135 | mKeyguardManager = |
| 136 | (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 137 | mAudioService = getAudioService(); |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 138 | mAudioPlayerStateMonitor = AudioPlayerStateMonitor.getInstance(); |
| 139 | mAudioPlayerStateMonitor.registerListener( |
Sungsoo Lim | 2afdbc4 | 2017-11-01 13:45:59 +0900 | [diff] [blame^] | 140 | (config, isRemoved) -> { |
| 141 | if (isRemoved || !config.isActive() || config.getPlayerType() |
| 142 | == AudioPlaybackConfiguration.PLAYER_TYPE_JAM_SOUNDPOOL) { |
| 143 | return; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 144 | } |
Sungsoo Lim | 2afdbc4 | 2017-11-01 13:45:59 +0900 | [diff] [blame^] | 145 | synchronized (mLock) { |
| 146 | FullUserRecord user = getFullUserRecordLocked( |
| 147 | UserHandle.getUserId(config.getClientUid())); |
| 148 | if (user != null) { |
| 149 | user.mPriorityStack.updateMediaButtonSessionIfNeeded(); |
| 150 | } |
| 151 | } |
| 152 | }, null /* handler */); |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 153 | mAudioPlayerStateMonitor.registerSelfIntoAudioServiceIfNeeded(mAudioService); |
RoboErik | 6f0e4dd | 2014-06-17 16:56:27 -0700 | [diff] [blame] | 154 | mContentResolver = getContext().getContentResolver(); |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 155 | mSettingsObserver = new SettingsObserver(); |
| 156 | mSettingsObserver.observe(); |
Jaewan Kim | fdb612e | 2017-07-01 09:23:41 +0900 | [diff] [blame] | 157 | mHasFeatureLeanback = getContext().getPackageManager().hasSystemFeature( |
| 158 | PackageManager.FEATURE_LEANBACK); |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 159 | |
| 160 | updateUser(); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private IAudioService getAudioService() { |
| 164 | IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); |
| 165 | return IAudioService.Stub.asInterface(b); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 168 | private boolean isGlobalPriorityActiveLocked() { |
| 169 | return mGlobalPrioritySession != null && mGlobalPrioritySession.isActive(); |
| 170 | } |
| 171 | |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 172 | public void updateSession(MediaSessionRecord record) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 173 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 174 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 175 | if (user == null) { |
| 176 | Log.w(TAG, "Unknown session updated. Ignoring."); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 179 | if ((record.getFlags() & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) { |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 180 | if (DEBUG_KEY_EVENT) { |
| 181 | Log.d(TAG, "Global priority session is updated, active=" + record.isActive()); |
| 182 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 183 | user.pushAddressedPlayerChangedLocked(); |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 184 | } else { |
| 185 | if (!user.mPriorityStack.contains(record)) { |
| 186 | Log.w(TAG, "Unknown session updated. Ignoring."); |
| 187 | return; |
| 188 | } |
| 189 | user.mPriorityStack.onSessionStateChange(record); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 190 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 191 | mHandler.postSessionsChanged(record.getUserId()); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Jaewan Kim | fa85b60 | 2017-10-10 16:49:58 +0900 | [diff] [blame] | 195 | public void setGlobalPrioritySession(MediaSessionRecord record) { |
| 196 | synchronized (mLock) { |
| 197 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 198 | if (mGlobalPrioritySession != record) { |
| 199 | Log.d(TAG, "Global priority session is changed from " + mGlobalPrioritySession |
| 200 | + " to " + record); |
| 201 | mGlobalPrioritySession = record; |
| 202 | if (user != null && user.mPriorityStack.contains(record)) { |
| 203 | // Handle the global priority session separately. |
| 204 | // Otherwise, it can be the media button session regardless of the active state |
| 205 | // because it or other system components might have been the lastly played media |
| 206 | // app. |
| 207 | user.mPriorityStack.removeSession(record); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 213 | private List<MediaSessionRecord> getActiveSessionsLocked(int userId) { |
Jaewan Kim | da74a15 | 2017-10-03 23:58:11 +0900 | [diff] [blame] | 214 | List<MediaSessionRecord> records = new ArrayList<>(); |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 215 | if (userId == UserHandle.USER_ALL) { |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 216 | int size = mUserRecords.size(); |
| 217 | for (int i = 0; i < size; i++) { |
| 218 | records.addAll(mUserRecords.valueAt(i).mPriorityStack.getActiveSessions(userId)); |
| 219 | } |
| 220 | } else { |
| 221 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 222 | if (user == null) { |
| 223 | Log.w(TAG, "getSessions failed. Unknown user " + userId); |
Jaewan Kim | da74a15 | 2017-10-03 23:58:11 +0900 | [diff] [blame] | 224 | return records; |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 225 | } |
Jaewan Kim | da74a15 | 2017-10-03 23:58:11 +0900 | [diff] [blame] | 226 | records.addAll(user.mPriorityStack.getActiveSessions(userId)); |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Return global priority session at the first whenever it's asked. |
| 230 | if (isGlobalPriorityActiveLocked() |
| 231 | && (userId == UserHandle.USER_ALL |
| 232 | || userId == mGlobalPrioritySession.getUserId())) { |
| 233 | records.add(0, mGlobalPrioritySession); |
| 234 | } |
| 235 | return records; |
| 236 | } |
| 237 | |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 238 | /** |
Hyundo Moon | a055f13 | 2017-01-13 15:31:06 +0900 | [diff] [blame] | 239 | * Tells the system UI that volume has changed on an active remote session. |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 240 | */ |
| 241 | public void notifyRemoteVolumeChanged(int flags, MediaSessionRecord session) { |
Hyundo Moon | a055f13 | 2017-01-13 15:31:06 +0900 | [diff] [blame] | 242 | if (mRvc == null || !session.isActive()) { |
RoboErik | 9c5b7cb | 2015-01-15 15:09:09 -0800 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | try { |
| 246 | mRvc.remoteVolumeChanged(session.getControllerBinder(), flags); |
| 247 | } catch (Exception e) { |
| 248 | Log.wtf(TAG, "Error sending volume change to system UI.", e); |
| 249 | } |
| 250 | } |
| 251 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 252 | public void onSessionPlaystateChanged(MediaSessionRecord record, int oldState, int newState) { |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 253 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 254 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 255 | if (user == null || !user.mPriorityStack.contains(record)) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 256 | Log.d(TAG, "Unknown session changed playback state. Ignoring."); |
| 257 | return; |
| 258 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 259 | user.mPriorityStack.onPlaystateChanged(record, oldState, newState); |
RoboErik | a8f9514 | 2014-05-05 14:23:49 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 263 | public void onSessionPlaybackTypeChanged(MediaSessionRecord record) { |
| 264 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 265 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 266 | if (user == null || !user.mPriorityStack.contains(record)) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 267 | Log.d(TAG, "Unknown session changed playback type. Ignoring."); |
| 268 | return; |
| 269 | } |
| 270 | pushRemoteVolumeUpdateLocked(record.getUserId()); |
| 271 | } |
| 272 | } |
| 273 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 274 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 275 | public void onStartUser(int userId) { |
| 276 | if (DEBUG) Log.d(TAG, "onStartUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 277 | updateUser(); |
| 278 | } |
| 279 | |
| 280 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 281 | public void onSwitchUser(int userId) { |
| 282 | if (DEBUG) Log.d(TAG, "onSwitchUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 283 | updateUser(); |
| 284 | } |
| 285 | |
| 286 | @Override |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 287 | public void onStopUser(int userId) { |
| 288 | if (DEBUG) Log.d(TAG, "onStopUser: " + userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 289 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 290 | FullUserRecord user = getFullUserRecordLocked(userId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 291 | if (user != null) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 292 | if (user.mFullUserId == userId) { |
| 293 | user.destroySessionsForUserLocked(UserHandle.USER_ALL); |
| 294 | mUserRecords.remove(userId); |
| 295 | } else { |
| 296 | user.destroySessionsForUserLocked(userId); |
| 297 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 298 | } |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 299 | updateUser(); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 304 | public void monitor() { |
| 305 | synchronized (mLock) { |
| 306 | // Check for deadlock |
| 307 | } |
| 308 | } |
| 309 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 310 | protected void enforcePhoneStatePermission(int pid, int uid) { |
| 311 | if (getContext().checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE, pid, uid) |
| 312 | != PackageManager.PERMISSION_GRANTED) { |
| 313 | throw new SecurityException("Must hold the MODIFY_PHONE_STATE permission."); |
| 314 | } |
| 315 | } |
| 316 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 317 | void sessionDied(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 318 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 319 | destroySessionLocked(session); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void destroySession(MediaSessionRecord session) { |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 324 | synchronized (mLock) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 325 | destroySessionLocked(session); |
| 326 | } |
| 327 | } |
| 328 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 329 | private void updateUser() { |
| 330 | synchronized (mLock) { |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 331 | UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 332 | mFullUserIds.clear(); |
| 333 | List<UserInfo> allUsers = manager.getUsers(); |
| 334 | if (allUsers != null) { |
| 335 | for (UserInfo userInfo : allUsers) { |
| 336 | if (userInfo.isManagedProfile()) { |
| 337 | mFullUserIds.put(userInfo.id, userInfo.profileGroupId); |
| 338 | } else { |
| 339 | mFullUserIds.put(userInfo.id, userInfo.id); |
| 340 | if (mUserRecords.get(userInfo.id) == null) { |
| 341 | mUserRecords.put(userInfo.id, new FullUserRecord(userInfo.id)); |
| 342 | } |
| 343 | } |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 344 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 345 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 346 | // Ensure that the current full user exists. |
| 347 | int currentFullUserId = ActivityManager.getCurrentUser(); |
| 348 | mCurrentFullUserRecord = mUserRecords.get(currentFullUserId); |
| 349 | if (mCurrentFullUserRecord == null) { |
| 350 | Log.w(TAG, "Cannot find FullUserInfo for the current user " + currentFullUserId); |
| 351 | mCurrentFullUserRecord = new FullUserRecord(currentFullUserId); |
| 352 | mUserRecords.put(currentFullUserId, mCurrentFullUserRecord); |
| 353 | } |
| 354 | mFullUserIds.put(currentFullUserId, currentFullUserId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 358 | private void updateActiveSessionListeners() { |
| 359 | synchronized (mLock) { |
| 360 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 361 | SessionsListenerRecord listener = mSessionsListeners.get(i); |
| 362 | try { |
| 363 | enforceMediaPermissions(listener.mComponentName, listener.mPid, listener.mUid, |
| 364 | listener.mUserId); |
| 365 | } catch (SecurityException e) { |
| 366 | Log.i(TAG, "ActiveSessionsListener " + listener.mComponentName |
| 367 | + " is no longer authorized. Disconnecting."); |
| 368 | mSessionsListeners.remove(i); |
| 369 | try { |
| 370 | listener.mListener |
| 371 | .onActiveSessionsChanged(new ArrayList<MediaSession.Token>()); |
| 372 | } catch (Exception e1) { |
| 373 | // ignore |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 380 | /* |
| 381 | * When a session is removed several things need to happen. |
| 382 | * 1. We need to remove it from the relevant user. |
| 383 | * 2. We need to remove it from the priority stack. |
| 384 | * 3. We need to remove it from all sessions. |
| 385 | * 4. If this is the system priority session we need to clear it. |
| 386 | * 5. We need to unlink to death from the cb binder |
| 387 | * 6. We need to tell the session to do any final cleanup (onDestroy) |
| 388 | */ |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 389 | private void destroySessionLocked(MediaSessionRecord session) { |
Insun Kang | 30be970a | 2015-11-26 15:35:44 +0900 | [diff] [blame] | 390 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 391 | Log.d(TAG, "Destroying " + session); |
Insun Kang | 30be970a | 2015-11-26 15:35:44 +0900 | [diff] [blame] | 392 | } |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 393 | FullUserRecord user = getFullUserRecordLocked(session.getUserId()); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 394 | if (mGlobalPrioritySession == session) { |
| 395 | mGlobalPrioritySession = null; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 396 | if (session.isActive() && user != null) { |
| 397 | user.pushAddressedPlayerChangedLocked(); |
| 398 | } |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 399 | } else { |
| 400 | if (user != null) { |
| 401 | user.mPriorityStack.removeSession(session); |
| 402 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 403 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 404 | |
| 405 | try { |
| 406 | session.getCallback().asBinder().unlinkToDeath(session, 0); |
| 407 | } catch (Exception e) { |
| 408 | // ignore exceptions while destroying a session. |
| 409 | } |
| 410 | session.onDestroy(); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 411 | mHandler.postSessionsChanged(session.getUserId()); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | private void enforcePackageName(String packageName, int uid) { |
| 415 | if (TextUtils.isEmpty(packageName)) { |
| 416 | throw new IllegalArgumentException("packageName may not be empty"); |
| 417 | } |
| 418 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 419 | final int packageCount = packages.length; |
| 420 | for (int i = 0; i < packageCount; i++) { |
| 421 | if (packageName.equals(packages[i])) { |
| 422 | return; |
| 423 | } |
| 424 | } |
| 425 | throw new IllegalArgumentException("packageName is not owned by the calling process"); |
| 426 | } |
| 427 | |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 428 | /** |
| 429 | * Checks a caller's authorization to register an IRemoteControlDisplay. |
| 430 | * Authorization is granted if one of the following is true: |
| 431 | * <ul> |
| 432 | * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL |
| 433 | * permission</li> |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 434 | * <li>the caller's listener is one of the enabled notification listeners |
| 435 | * for the caller's user</li> |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 436 | * </ul> |
| 437 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 438 | private void enforceMediaPermissions(ComponentName compName, int pid, int uid, |
| 439 | int resolvedUserId) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 440 | if (isCurrentVolumeController(uid, pid)) return; |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 441 | if (getContext() |
| 442 | .checkPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL, pid, uid) |
| 443 | != PackageManager.PERMISSION_GRANTED |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 444 | && !isEnabledNotificationListener(compName, UserHandle.getUserId(uid), |
| 445 | resolvedUserId)) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 446 | throw new SecurityException("Missing permission to control media."); |
| 447 | } |
| 448 | } |
| 449 | |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 450 | private boolean isCurrentVolumeController(int uid, int pid) { |
| 451 | return getContext().checkPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 452 | pid, uid) == PackageManager.PERMISSION_GRANTED; |
John Spurlock | be19ed0 | 2015-02-22 10:57:55 -0500 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | private void enforceSystemUiPermission(String action, int pid, int uid) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 456 | if (!isCurrentVolumeController(uid, pid)) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 457 | throw new SecurityException("Only system ui may " + action); |
| 458 | } |
| 459 | } |
| 460 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 461 | /** |
| 462 | * This checks if the component is an enabled notification listener for the |
| 463 | * specified user. Enabled components may only operate on behalf of the user |
| 464 | * they're running as. |
| 465 | * |
| 466 | * @param compName The component that is enabled. |
| 467 | * @param userId The user id of the caller. |
| 468 | * @param forUserId The user id they're making the request on behalf of. |
| 469 | * @return True if the component is enabled, false otherwise |
| 470 | */ |
| 471 | private boolean isEnabledNotificationListener(ComponentName compName, int userId, |
| 472 | int forUserId) { |
| 473 | if (userId != forUserId) { |
| 474 | // You may not access another user's content as an enabled listener. |
| 475 | return false; |
| 476 | } |
RoboErik | 51fa6bc | 2014-06-20 14:59:58 -0700 | [diff] [blame] | 477 | if (DEBUG) { |
| 478 | Log.d(TAG, "Checking if enabled notification listener " + compName); |
| 479 | } |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 480 | if (compName != null) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 481 | try { |
| 482 | return mNotificationManager.isNotificationListenerAccessGrantedForUser( |
| 483 | compName, userId); |
| 484 | } catch(RemoteException e) { |
| 485 | Log.w(TAG, "Dead NotificationManager in isEnabledNotificationListener", e); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | return false; |
| 489 | } |
| 490 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 491 | private MediaSessionRecord createSessionInternal(int callerPid, int callerUid, int userId, |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 492 | String callerPackageName, ISessionCallback cb, String tag) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 493 | synchronized (mLock) { |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 494 | return createSessionLocked(callerPid, callerUid, userId, callerPackageName, cb, tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 498 | /* |
| 499 | * When a session is created the following things need to happen. |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 500 | * 1. Its callback binder needs a link to death |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 501 | * 2. It needs to be added to all sessions. |
| 502 | * 3. It needs to be added to the priority stack. |
| 503 | * 4. It needs to be added to the relevant user record. |
| 504 | */ |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 505 | private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId, |
| 506 | String callerPackageName, ISessionCallback cb, String tag) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 507 | FullUserRecord user = getFullUserRecordLocked(userId); |
Dongwon Kang | 8cf39c5 | 2016-07-29 13:20:39 -0700 | [diff] [blame] | 508 | if (user == null) { |
| 509 | Log.wtf(TAG, "Request from invalid user: " + userId); |
| 510 | throw new RuntimeException("Session request from invalid user."); |
| 511 | } |
| 512 | |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 513 | final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId, |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 514 | callerPackageName, cb, tag, this, mHandler.getLooper()); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 515 | try { |
| 516 | cb.asBinder().linkToDeath(session, 0); |
| 517 | } catch (RemoteException e) { |
| 518 | throw new RuntimeException("Media Session owner died prematurely.", e); |
| 519 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 520 | |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 521 | user.mPriorityStack.addSession(session); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 522 | mHandler.postSessionsChanged(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 523 | |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 524 | if (DEBUG) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 525 | Log.d(TAG, "Created session for " + callerPackageName + " with tag " + tag); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 526 | } |
| 527 | return session; |
| 528 | } |
| 529 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 530 | private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) { |
| 531 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
RoboErik | a08adb24 | 2014-11-21 18:28:18 -0800 | [diff] [blame] | 532 | if (mSessionsListeners.get(i).mListener.asBinder() == listener.asBinder()) { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 533 | return i; |
| 534 | } |
| 535 | } |
| 536 | return -1; |
| 537 | } |
| 538 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 539 | private void pushSessionsChanged(int userId) { |
| 540 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 541 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 542 | if (user == null) { |
| 543 | Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId); |
| 544 | return; |
| 545 | } |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 546 | List<MediaSessionRecord> records = getActiveSessionsLocked(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 547 | int size = records.size(); |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 548 | ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 549 | for (int i = 0; i < size; i++) { |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 550 | tokens.add(new MediaSession.Token(records.get(i).getControllerBinder())); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 551 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 552 | pushRemoteVolumeUpdateLocked(userId); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 553 | for (int i = mSessionsListeners.size() - 1; i >= 0; i--) { |
| 554 | SessionsListenerRecord record = mSessionsListeners.get(i); |
| 555 | if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) { |
| 556 | try { |
| 557 | record.mListener.onActiveSessionsChanged(tokens); |
| 558 | } catch (RemoteException e) { |
| 559 | Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing", |
| 560 | e); |
| 561 | mSessionsListeners.remove(i); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 568 | private void pushRemoteVolumeUpdateLocked(int userId) { |
| 569 | if (mRvc != null) { |
| 570 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 571 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 572 | if (user == null) { |
| 573 | Log.w(TAG, "pushRemoteVolumeUpdateLocked failed. No user with id=" + userId); |
| 574 | return; |
| 575 | } |
| 576 | MediaSessionRecord record = user.mPriorityStack.getDefaultRemoteSession(userId); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 577 | mRvc.updateRemoteController(record == null ? null : record.getControllerBinder()); |
| 578 | } catch (RemoteException e) { |
| 579 | Log.wtf(TAG, "Error sending default remote volume to sys ui.", e); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 584 | /** |
| 585 | * Called when the media button receiver for the {@param record} is changed. |
| 586 | * |
| 587 | * @param record the media session whose media button receiver is updated. |
| 588 | */ |
| 589 | public void onMediaButtonReceiverChanged(MediaSessionRecord record) { |
| 590 | synchronized (mLock) { |
| 591 | FullUserRecord user = getFullUserRecordLocked(record.getUserId()); |
| 592 | MediaSessionRecord mediaButtonSession = |
| 593 | user.mPriorityStack.getMediaButtonSession(); |
| 594 | if (record == mediaButtonSession) { |
| 595 | user.rememberMediaButtonReceiverLocked(mediaButtonSession); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 600 | private String getCallingPackageName(int uid) { |
| 601 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 602 | if (packages != null && packages.length > 0) { |
| 603 | return packages[0]; |
| 604 | } |
| 605 | return ""; |
| 606 | } |
| 607 | |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 608 | private void dispatchVolumeKeyLongPressLocked(KeyEvent keyEvent) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 609 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 610 | mCurrentFullUserRecord.mOnVolumeKeyLongPressListener.onVolumeKeyLongPress(keyEvent); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 611 | } catch (RemoteException e) { |
| 612 | Log.w(TAG, "Failed to send " + keyEvent + " to volume key long-press listener"); |
| 613 | } |
| 614 | } |
| 615 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 616 | private FullUserRecord getFullUserRecordLocked(int userId) { |
| 617 | int fullUserId = mFullUserIds.get(userId, -1); |
| 618 | if (fullUserId < 0) { |
| 619 | return null; |
| 620 | } |
| 621 | return mUserRecords.get(fullUserId); |
| 622 | } |
| 623 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 624 | /** |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 625 | * Information about a full user and its corresponding managed profiles. |
| 626 | * |
| 627 | * <p>Since the full user runs together with its managed profiles, a user wouldn't differentiate |
| 628 | * them when he/she presses a media/volume button. So keeping media sessions for them in one |
| 629 | * place makes more sense and increases the readability.</p> |
| 630 | * <p>The contents of this object is guarded by {@link #mLock}. |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 631 | */ |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 632 | final class FullUserRecord implements MediaSessionStack.OnMediaButtonSessionChangedListener { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 633 | private static final String COMPONENT_NAME_USER_ID_DELIM = ","; |
| 634 | private final int mFullUserId; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 635 | private final MediaSessionStack mPriorityStack; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 636 | private PendingIntent mLastMediaButtonReceiver; |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 637 | private ComponentName mRestoredMediaButtonReceiver; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 638 | private int mRestoredMediaButtonReceiverUserId; |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 639 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 640 | private IOnVolumeKeyLongPressListener mOnVolumeKeyLongPressListener; |
| 641 | private int mOnVolumeKeyLongPressListenerUid; |
| 642 | private KeyEvent mInitialDownVolumeKeyEvent; |
| 643 | private int mInitialDownVolumeStream; |
| 644 | private boolean mInitialDownMusicOnly; |
| 645 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 646 | private IOnMediaKeyListener mOnMediaKeyListener; |
| 647 | private int mOnMediaKeyListenerUid; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 648 | private ICallback mCallback; |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 649 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 650 | public FullUserRecord(int fullUserId) { |
| 651 | mFullUserId = fullUserId; |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 652 | mPriorityStack = new MediaSessionStack(mAudioPlayerStateMonitor, this); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 653 | // Restore the remembered media button receiver before the boot. |
| 654 | String mediaButtonReceiver = Settings.Secure.getStringForUser(mContentResolver, |
| 655 | Settings.System.MEDIA_BUTTON_RECEIVER, mFullUserId); |
| 656 | if (mediaButtonReceiver == null) { |
| 657 | return; |
| 658 | } |
| 659 | String[] tokens = mediaButtonReceiver.split(COMPONENT_NAME_USER_ID_DELIM); |
| 660 | if (tokens == null || tokens.length != 2) { |
| 661 | return; |
| 662 | } |
| 663 | mRestoredMediaButtonReceiver = ComponentName.unflattenFromString(tokens[0]); |
| 664 | mRestoredMediaButtonReceiverUserId = Integer.parseInt(tokens[1]); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 667 | public void destroySessionsForUserLocked(int userId) { |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 668 | List<MediaSessionRecord> sessions = mPriorityStack.getPriorityList(false, userId); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 669 | for (MediaSessionRecord session : sessions) { |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 670 | MediaSessionService.this.destroySessionLocked(session); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 674 | public void dumpLocked(PrintWriter pw, String prefix) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 675 | pw.print(prefix + "Record for full_user=" + mFullUserId); |
| 676 | // Dump managed profile user ids associated with this user. |
| 677 | int size = mFullUserIds.size(); |
| 678 | for (int i = 0; i < size; i++) { |
| 679 | if (mFullUserIds.keyAt(i) != mFullUserIds.valueAt(i) |
| 680 | && mFullUserIds.valueAt(i) == mFullUserId) { |
| 681 | pw.print(", profile_user=" + mFullUserIds.keyAt(i)); |
| 682 | } |
| 683 | } |
| 684 | pw.println(); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 685 | String indent = prefix + " "; |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 686 | pw.println(indent + "Volume key long-press listener: " + mOnVolumeKeyLongPressListener); |
| 687 | pw.println(indent + "Volume key long-press listener package: " + |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 688 | getCallingPackageName(mOnVolumeKeyLongPressListenerUid)); |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 689 | pw.println(indent + "Media key listener: " + mOnMediaKeyListener); |
| 690 | pw.println(indent + "Media key listener package: " + |
| 691 | getCallingPackageName(mOnMediaKeyListenerUid)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 692 | pw.println(indent + "Callback: " + mCallback); |
| 693 | pw.println(indent + "Last MediaButtonReceiver: " + mLastMediaButtonReceiver); |
| 694 | pw.println(indent + "Restored MediaButtonReceiver: " + mRestoredMediaButtonReceiver); |
| 695 | mPriorityStack.dump(pw, indent); |
| 696 | } |
| 697 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 698 | @Override |
| 699 | public void onMediaButtonSessionChanged(MediaSessionRecord oldMediaButtonSession, |
| 700 | MediaSessionRecord newMediaButtonSession) { |
| 701 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 98e4aaf | 2017-05-12 17:06:47 +0900 | [diff] [blame] | 702 | Log.d(TAG, "Media button session is changed to " + newMediaButtonSession); |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 703 | } |
| 704 | synchronized (mLock) { |
| 705 | if (oldMediaButtonSession != null) { |
| 706 | mHandler.postSessionsChanged(oldMediaButtonSession.getUserId()); |
| 707 | } |
| 708 | if (newMediaButtonSession != null) { |
| 709 | rememberMediaButtonReceiverLocked(newMediaButtonSession); |
| 710 | mHandler.postSessionsChanged(newMediaButtonSession.getUserId()); |
| 711 | } |
| 712 | pushAddressedPlayerChangedLocked(); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // Remember media button receiver and keep it in the persistent storage. |
| 717 | public void rememberMediaButtonReceiverLocked(MediaSessionRecord record) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 718 | PendingIntent receiver = record.getMediaButtonReceiver(); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 719 | mLastMediaButtonReceiver = receiver; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 720 | mRestoredMediaButtonReceiver = null; |
| 721 | String componentName = ""; |
| 722 | if (receiver != null) { |
| 723 | ComponentName component = receiver.getIntent().getComponent(); |
| 724 | if (component != null |
| 725 | && record.getPackageName().equals(component.getPackageName())) { |
| 726 | componentName = component.flattenToString(); |
| 727 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 728 | } |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 729 | Settings.Secure.putStringForUser(mContentResolver, |
| 730 | Settings.System.MEDIA_BUTTON_RECEIVER, |
| 731 | componentName + COMPONENT_NAME_USER_ID_DELIM + record.getUserId(), |
| 732 | mFullUserId); |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 733 | } |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 734 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 735 | private void pushAddressedPlayerChangedLocked() { |
| 736 | if (mCallback == null) { |
| 737 | return; |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 738 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 739 | try { |
| 740 | MediaSessionRecord mediaButtonSession = getMediaButtonSessionLocked(); |
| 741 | if (mediaButtonSession != null) { |
| 742 | mCallback.onAddressedPlayerChangedToMediaSession( |
| 743 | new MediaSession.Token(mediaButtonSession.getControllerBinder())); |
| 744 | } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) { |
| 745 | mCallback.onAddressedPlayerChangedToMediaButtonReceiver( |
| 746 | mCurrentFullUserRecord.mLastMediaButtonReceiver |
| 747 | .getIntent().getComponent()); |
| 748 | } else if (mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) { |
| 749 | mCallback.onAddressedPlayerChangedToMediaButtonReceiver( |
| 750 | mCurrentFullUserRecord.mRestoredMediaButtonReceiver); |
| 751 | } |
| 752 | } catch (RemoteException e) { |
| 753 | Log.w(TAG, "Failed to pushAddressedPlayerChangedLocked", e); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | private MediaSessionRecord getMediaButtonSessionLocked() { |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 758 | return isGlobalPriorityActiveLocked() |
| 759 | ? mGlobalPrioritySession : mPriorityStack.getMediaButtonSession(); |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 760 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 761 | } |
| 762 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 763 | final class SessionsListenerRecord implements IBinder.DeathRecipient { |
| 764 | private final IActiveSessionsListener mListener; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 765 | private final ComponentName mComponentName; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 766 | private final int mUserId; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 767 | private final int mPid; |
| 768 | private final int mUid; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 769 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 770 | public SessionsListenerRecord(IActiveSessionsListener listener, |
| 771 | ComponentName componentName, |
| 772 | int userId, int pid, int uid) { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 773 | mListener = listener; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 774 | mComponentName = componentName; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 775 | mUserId = userId; |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 776 | mPid = pid; |
| 777 | mUid = uid; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public void binderDied() { |
| 782 | synchronized (mLock) { |
| 783 | mSessionsListeners.remove(this); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 788 | final class SettingsObserver extends ContentObserver { |
| 789 | private final Uri mSecureSettingsUri = Settings.Secure.getUriFor( |
| 790 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
| 791 | |
| 792 | private SettingsObserver() { |
| 793 | super(null); |
| 794 | } |
| 795 | |
| 796 | private void observe() { |
| 797 | mContentResolver.registerContentObserver(mSecureSettingsUri, |
| 798 | false, this, UserHandle.USER_ALL); |
| 799 | } |
| 800 | |
| 801 | @Override |
| 802 | public void onChange(boolean selfChange, Uri uri) { |
| 803 | updateActiveSessionListeners(); |
| 804 | } |
| 805 | } |
| 806 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 807 | class SessionManagerImpl extends ISessionManager.Stub { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 808 | private static final String EXTRA_WAKELOCK_ACQUIRED = |
| 809 | "android.media.AudioService.WAKELOCK_ACQUIRED"; |
| 810 | private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number |
| 811 | |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 812 | private boolean mVoiceButtonDown = false; |
| 813 | private boolean mVoiceButtonHandled = false; |
| 814 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 815 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 816 | public ISession createSession(String packageName, ISessionCallback cb, String tag, |
| 817 | int userId) throws RemoteException { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 818 | final int pid = Binder.getCallingPid(); |
| 819 | final int uid = Binder.getCallingUid(); |
| 820 | final long token = Binder.clearCallingIdentity(); |
| 821 | try { |
| 822 | enforcePackageName(packageName, uid); |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 823 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 824 | false /* allowAll */, true /* requireFull */, "createSession", packageName); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 825 | if (cb == null) { |
| 826 | throw new IllegalArgumentException("Controller callback cannot be null"); |
| 827 | } |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 828 | return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag) |
| 829 | .getSessionBinder(); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 830 | } finally { |
| 831 | Binder.restoreCallingIdentity(token); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | @Override |
RoboErik | a5b0232 | 2014-05-07 17:05:49 -0700 | [diff] [blame] | 836 | public List<IBinder> getSessions(ComponentName componentName, int userId) { |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 837 | final int pid = Binder.getCallingPid(); |
| 838 | final int uid = Binder.getCallingUid(); |
| 839 | final long token = Binder.clearCallingIdentity(); |
| 840 | |
| 841 | try { |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 842 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 843 | ArrayList<IBinder> binders = new ArrayList<IBinder>(); |
| 844 | synchronized (mLock) { |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 845 | List<MediaSessionRecord> records = getActiveSessionsLocked(resolvedUserId); |
| 846 | for (MediaSessionRecord record : records) { |
| 847 | binders.add(record.getControllerBinder().asBinder()); |
RoboErik | e7880d8 | 2014-04-30 12:48:25 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | return binders; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 851 | } finally { |
| 852 | Binder.restoreCallingIdentity(token); |
| 853 | } |
| 854 | } |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 855 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 856 | @Override |
| 857 | public void addSessionsListener(IActiveSessionsListener listener, |
| 858 | ComponentName componentName, int userId) throws RemoteException { |
| 859 | final int pid = Binder.getCallingPid(); |
| 860 | final int uid = Binder.getCallingUid(); |
| 861 | final long token = Binder.clearCallingIdentity(); |
| 862 | |
| 863 | try { |
| 864 | int resolvedUserId = verifySessionsRequest(componentName, userId, pid, uid); |
| 865 | synchronized (mLock) { |
| 866 | int index = findIndexOfSessionsListenerLocked(listener); |
| 867 | if (index != -1) { |
| 868 | Log.w(TAG, "ActiveSessionsListener is already added, ignoring"); |
| 869 | return; |
| 870 | } |
| 871 | SessionsListenerRecord record = new SessionsListenerRecord(listener, |
RoboErik | 7aef77b | 2014-08-08 15:56:54 -0700 | [diff] [blame] | 872 | componentName, resolvedUserId, pid, uid); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 873 | try { |
| 874 | listener.asBinder().linkToDeath(record, 0); |
| 875 | } catch (RemoteException e) { |
| 876 | Log.e(TAG, "ActiveSessionsListener is dead, ignoring it", e); |
| 877 | return; |
| 878 | } |
| 879 | mSessionsListeners.add(record); |
| 880 | } |
| 881 | } finally { |
| 882 | Binder.restoreCallingIdentity(token); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | @Override |
| 887 | public void removeSessionsListener(IActiveSessionsListener listener) |
| 888 | throws RemoteException { |
| 889 | synchronized (mLock) { |
| 890 | int index = findIndexOfSessionsListenerLocked(listener); |
| 891 | if (index != -1) { |
| 892 | SessionsListenerRecord record = mSessionsListeners.remove(index); |
| 893 | try { |
| 894 | record.mListener.asBinder().unlinkToDeath(record, 0); |
| 895 | } catch (Exception e) { |
| 896 | // ignore exceptions, the record is being removed |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 902 | /** |
| 903 | * Handles the dispatching of the media button events to one of the |
| 904 | * registered listeners, or if there was none, broadcast an |
| 905 | * ACTION_MEDIA_BUTTON intent to the rest of the system. |
| 906 | * |
| 907 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 908 | * supported media buttons |
| 909 | * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held |
| 910 | * while this key event is dispatched. |
| 911 | */ |
| 912 | @Override |
| 913 | public void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { |
| 914 | if (keyEvent == null || !KeyEvent.isMediaKey(keyEvent.getKeyCode())) { |
| 915 | Log.w(TAG, "Attempted to dispatch null or non-media key event."); |
| 916 | return; |
| 917 | } |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 918 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 919 | final int pid = Binder.getCallingPid(); |
| 920 | final int uid = Binder.getCallingUid(); |
| 921 | final long token = Binder.clearCallingIdentity(); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 922 | try { |
Jeff Brown | 221a827 | 2015-03-23 13:53:09 -0700 | [diff] [blame] | 923 | if (DEBUG) { |
| 924 | Log.d(TAG, "dispatchMediaKeyEvent, pid=" + pid + ", uid=" + uid + ", event=" |
| 925 | + keyEvent); |
| 926 | } |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 927 | if (!isUserSetupComplete()) { |
| 928 | // Global media key handling can have the side-effect of starting new |
| 929 | // activities which is undesirable while setup is in progress. |
| 930 | Slog.i(TAG, "Not dispatching media key event because user " |
| 931 | + "setup is in progress."); |
| 932 | return; |
| 933 | } |
| 934 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 935 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 936 | boolean isGlobalPriorityActive = isGlobalPriorityActiveLocked(); |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 937 | if (isGlobalPriorityActive && uid != Process.SYSTEM_UID) { |
| 938 | // Prevent dispatching key event through reflection while the global |
| 939 | // priority session is active. |
| 940 | Slog.i(TAG, "Only the system can dispatch media key event " |
| 941 | + "to the global priority session."); |
| 942 | return; |
| 943 | } |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 944 | if (!isGlobalPriorityActive) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 945 | if (mCurrentFullUserRecord.mOnMediaKeyListener != null) { |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 946 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 947 | Log.d(TAG, "Send " + keyEvent + " to the media key listener"); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 948 | } |
| 949 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 950 | mCurrentFullUserRecord.mOnMediaKeyListener.onMediaKey(keyEvent, |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 951 | new MediaKeyListenerResultReceiver(keyEvent, needWakeLock)); |
| 952 | return; |
| 953 | } catch (RemoteException e) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 954 | Log.w(TAG, "Failed to send " + keyEvent |
| 955 | + " to the media key listener"); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 959 | if (!isGlobalPriorityActive && isVoiceKey(keyEvent.getKeyCode())) { |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 960 | handleVoiceKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 961 | } else { |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 962 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | } finally { |
| 966 | Binder.restoreCallingIdentity(token); |
| 967 | } |
| 968 | } |
| 969 | |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 970 | @Override |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 971 | public void setCallback(ICallback callback) { |
| 972 | final int pid = Binder.getCallingPid(); |
| 973 | final int uid = Binder.getCallingUid(); |
| 974 | final long token = Binder.clearCallingIdentity(); |
| 975 | try { |
Amith Yamasani | e259ad2 | 2017-04-24 11:30:19 -0700 | [diff] [blame] | 976 | if (!UserHandle.isSameApp(uid, Process.BLUETOOTH_UID)) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 977 | throw new SecurityException("Only Bluetooth service processes can set" |
| 978 | + " Callback"); |
| 979 | } |
| 980 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 981 | int userId = UserHandle.getUserId(uid); |
| 982 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 983 | if (user == null || user.mFullUserId != userId) { |
| 984 | Log.w(TAG, "Only the full user can set the callback" |
| 985 | + ", userId=" + userId); |
| 986 | return; |
| 987 | } |
| 988 | user.mCallback = callback; |
| 989 | Log.d(TAG, "The callback " + user.mCallback |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 990 | + " is set by " + getCallingPackageName(uid)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 991 | if (user.mCallback == null) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 992 | return; |
| 993 | } |
| 994 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 995 | user.mCallback.asBinder().linkToDeath( |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 996 | new IBinder.DeathRecipient() { |
| 997 | @Override |
| 998 | public void binderDied() { |
| 999 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1000 | user.mCallback = null; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | }, 0); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1004 | user.pushAddressedPlayerChangedLocked(); |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1005 | } catch (RemoteException e) { |
| 1006 | Log.w(TAG, "Failed to set callback", e); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1007 | user.mCallback = null; |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } finally { |
| 1011 | Binder.restoreCallingIdentity(token); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | @Override |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1016 | public void setOnVolumeKeyLongPressListener(IOnVolumeKeyLongPressListener listener) { |
| 1017 | final int pid = Binder.getCallingPid(); |
| 1018 | final int uid = Binder.getCallingUid(); |
| 1019 | final long token = Binder.clearCallingIdentity(); |
| 1020 | try { |
| 1021 | // Enforce SET_VOLUME_KEY_LONG_PRESS_LISTENER permission. |
| 1022 | if (getContext().checkPermission( |
| 1023 | android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER, pid, uid) |
| 1024 | != PackageManager.PERMISSION_GRANTED) { |
| 1025 | throw new SecurityException("Must hold the SET_VOLUME_KEY_LONG_PRESS_LISTENER" + |
| 1026 | " permission."); |
| 1027 | } |
| 1028 | |
| 1029 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1030 | int userId = UserHandle.getUserId(uid); |
| 1031 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 1032 | if (user == null || user.mFullUserId != userId) { |
| 1033 | Log.w(TAG, "Only the full user can set the volume key long-press listener" |
| 1034 | + ", userId=" + userId); |
| 1035 | return; |
| 1036 | } |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1037 | if (user.mOnVolumeKeyLongPressListener != null && |
| 1038 | user.mOnVolumeKeyLongPressListenerUid != uid) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1039 | Log.w(TAG, "The volume key long-press listener cannot be reset" |
| 1040 | + " by another app , mOnVolumeKeyLongPressListener=" |
| 1041 | + user.mOnVolumeKeyLongPressListenerUid |
| 1042 | + ", uid=" + uid); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | user.mOnVolumeKeyLongPressListener = listener; |
| 1047 | user.mOnVolumeKeyLongPressListenerUid = uid; |
| 1048 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1049 | Log.d(TAG, "The volume key long-press listener " |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1050 | + listener + " is set by " + getCallingPackageName(uid)); |
| 1051 | |
| 1052 | if (user.mOnVolumeKeyLongPressListener != null) { |
| 1053 | try { |
| 1054 | user.mOnVolumeKeyLongPressListener.asBinder().linkToDeath( |
| 1055 | new IBinder.DeathRecipient() { |
| 1056 | @Override |
| 1057 | public void binderDied() { |
| 1058 | synchronized (mLock) { |
| 1059 | user.mOnVolumeKeyLongPressListener = null; |
| 1060 | } |
| 1061 | } |
| 1062 | }, 0); |
| 1063 | } catch (RemoteException e) { |
| 1064 | Log.w(TAG, "Failed to set death recipient " |
| 1065 | + user.mOnVolumeKeyLongPressListener); |
| 1066 | user.mOnVolumeKeyLongPressListener = null; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | } finally { |
| 1071 | Binder.restoreCallingIdentity(token); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1075 | @Override |
| 1076 | public void setOnMediaKeyListener(IOnMediaKeyListener listener) { |
| 1077 | final int pid = Binder.getCallingPid(); |
| 1078 | final int uid = Binder.getCallingUid(); |
| 1079 | final long token = Binder.clearCallingIdentity(); |
| 1080 | try { |
| 1081 | // Enforce SET_MEDIA_KEY_LISTENER permission. |
| 1082 | if (getContext().checkPermission( |
| 1083 | android.Manifest.permission.SET_MEDIA_KEY_LISTENER, pid, uid) |
| 1084 | != PackageManager.PERMISSION_GRANTED) { |
| 1085 | throw new SecurityException("Must hold the SET_MEDIA_KEY_LISTENER" + |
| 1086 | " permission."); |
| 1087 | } |
| 1088 | |
| 1089 | synchronized (mLock) { |
| 1090 | int userId = UserHandle.getUserId(uid); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1091 | FullUserRecord user = getFullUserRecordLocked(userId); |
| 1092 | if (user == null || user.mFullUserId != userId) { |
| 1093 | Log.w(TAG, "Only the full user can set the media key listener" |
| 1094 | + ", userId=" + userId); |
| 1095 | return; |
| 1096 | } |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1097 | if (user.mOnMediaKeyListener != null && user.mOnMediaKeyListenerUid != uid) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1098 | Log.w(TAG, "The media key listener cannot be reset by another app. " |
| 1099 | + ", mOnMediaKeyListenerUid=" + user.mOnMediaKeyListenerUid |
| 1100 | + ", uid=" + uid); |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | user.mOnMediaKeyListener = listener; |
| 1105 | user.mOnMediaKeyListenerUid = uid; |
| 1106 | |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1107 | Log.d(TAG, "The media key listener " + user.mOnMediaKeyListener |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1108 | + " is set by " + getCallingPackageName(uid)); |
| 1109 | |
| 1110 | if (user.mOnMediaKeyListener != null) { |
| 1111 | try { |
| 1112 | user.mOnMediaKeyListener.asBinder().linkToDeath( |
| 1113 | new IBinder.DeathRecipient() { |
| 1114 | @Override |
| 1115 | public void binderDied() { |
| 1116 | synchronized (mLock) { |
| 1117 | user.mOnMediaKeyListener = null; |
| 1118 | } |
| 1119 | } |
| 1120 | }, 0); |
| 1121 | } catch (RemoteException e) { |
| 1122 | Log.w(TAG, "Failed to set death recipient " + user.mOnMediaKeyListener); |
| 1123 | user.mOnMediaKeyListener = null; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } finally { |
| 1128 | Binder.restoreCallingIdentity(token); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1132 | /** |
| 1133 | * Handles the dispatching of the volume button events to one of the |
| 1134 | * registered listeners. If there's a volume key long-press listener and |
| 1135 | * there's no active global priority session, long-pressess will be sent to the |
| 1136 | * long-press listener instead of adjusting volume. |
| 1137 | * |
| 1138 | * @param keyEvent a non-null KeyEvent whose key code is one of the |
| 1139 | * {@link KeyEvent#KEYCODE_VOLUME_UP}, |
| 1140 | * {@link KeyEvent#KEYCODE_VOLUME_DOWN}, |
| 1141 | * or {@link KeyEvent#KEYCODE_VOLUME_MUTE}. |
| 1142 | * @param stream stream type to adjust volume. |
| 1143 | * @param musicOnly true if both UI nor haptic feedback aren't needed when adjust volume. |
| 1144 | */ |
| 1145 | @Override |
| 1146 | public void dispatchVolumeKeyEvent(KeyEvent keyEvent, int stream, boolean musicOnly) { |
| 1147 | if (keyEvent == null || |
| 1148 | (keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP |
| 1149 | && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN |
| 1150 | && keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_MUTE)) { |
| 1151 | Log.w(TAG, "Attempted to dispatch null or non-volume key event."); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | final int pid = Binder.getCallingPid(); |
| 1156 | final int uid = Binder.getCallingUid(); |
| 1157 | final long token = Binder.clearCallingIdentity(); |
| 1158 | |
Jaewan Kim | b2781e7 | 2017-03-02 09:57:09 +0900 | [diff] [blame] | 1159 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1160 | Log.d(TAG, "dispatchVolumeKeyEvent, pid=" + pid + ", uid=" + uid + ", event=" |
| 1161 | + keyEvent); |
| 1162 | } |
| 1163 | |
| 1164 | try { |
| 1165 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1166 | if (isGlobalPriorityActiveLocked() |
| 1167 | || mCurrentFullUserRecord.mOnVolumeKeyLongPressListener == null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1168 | dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly); |
| 1169 | } else { |
| 1170 | // TODO: Consider the case when both volume up and down keys are pressed |
| 1171 | // at the same time. |
| 1172 | if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) { |
| 1173 | if (keyEvent.getRepeatCount() == 0) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1174 | // Keeps the copy of the KeyEvent because it can be reused. |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1175 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = |
| 1176 | KeyEvent.obtain(keyEvent); |
| 1177 | mCurrentFullUserRecord.mInitialDownVolumeStream = stream; |
| 1178 | mCurrentFullUserRecord.mInitialDownMusicOnly = musicOnly; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1179 | mHandler.sendMessageDelayed( |
| 1180 | mHandler.obtainMessage( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1181 | MessageHandler.MSG_VOLUME_INITIAL_DOWN, |
| 1182 | mCurrentFullUserRecord.mFullUserId, 0), |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1183 | mLongPressTimeout); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1184 | } |
| 1185 | if (keyEvent.getRepeatCount() > 0 || keyEvent.isLongPress()) { |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1186 | mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1187 | if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1188 | dispatchVolumeKeyLongPressLocked( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1189 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1190 | // Mark that the key is already handled. |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1191 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = null; |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1192 | } |
| 1193 | dispatchVolumeKeyLongPressLocked(keyEvent); |
| 1194 | } |
| 1195 | } else { // if up |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1196 | mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1197 | if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null |
| 1198 | && mCurrentFullUserRecord.mInitialDownVolumeKeyEvent |
| 1199 | .getDownTime() == keyEvent.getDownTime()) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1200 | // Short-press. Should change volume. |
| 1201 | dispatchVolumeKeyEventLocked( |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1202 | mCurrentFullUserRecord.mInitialDownVolumeKeyEvent, |
| 1203 | mCurrentFullUserRecord.mInitialDownVolumeStream, |
| 1204 | mCurrentFullUserRecord.mInitialDownMusicOnly); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1205 | dispatchVolumeKeyEventLocked(keyEvent, stream, musicOnly); |
| 1206 | } else { |
| 1207 | dispatchVolumeKeyLongPressLocked(keyEvent); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | } finally { |
| 1213 | Binder.restoreCallingIdentity(token); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1217 | private void dispatchVolumeKeyEventLocked( |
| 1218 | KeyEvent keyEvent, int stream, boolean musicOnly) { |
| 1219 | boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN; |
| 1220 | boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP; |
| 1221 | int direction = 0; |
| 1222 | boolean isMute = false; |
| 1223 | switch (keyEvent.getKeyCode()) { |
| 1224 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1225 | direction = AudioManager.ADJUST_RAISE; |
| 1226 | break; |
| 1227 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1228 | direction = AudioManager.ADJUST_LOWER; |
| 1229 | break; |
| 1230 | case KeyEvent.KEYCODE_VOLUME_MUTE: |
| 1231 | isMute = true; |
| 1232 | break; |
| 1233 | } |
| 1234 | if (down || up) { |
| 1235 | int flags = AudioManager.FLAG_FROM_KEY; |
| 1236 | if (musicOnly) { |
| 1237 | // This flag is used when the screen is off to only affect active media. |
| 1238 | flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY; |
| 1239 | } else { |
| 1240 | // These flags are consistent with the home screen |
| 1241 | if (up) { |
| 1242 | flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE; |
| 1243 | } else { |
| 1244 | flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE; |
| 1245 | } |
| 1246 | } |
| 1247 | if (direction != 0) { |
| 1248 | // If this is action up we want to send a beep for non-music events |
| 1249 | if (up) { |
| 1250 | direction = 0; |
| 1251 | } |
| 1252 | dispatchAdjustVolumeLocked(stream, direction, flags); |
| 1253 | } else if (isMute) { |
| 1254 | if (down && keyEvent.getRepeatCount() == 0) { |
| 1255 | dispatchAdjustVolumeLocked(stream, AudioManager.ADJUST_TOGGLE_MUTE, flags); |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | @Override |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1262 | public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) { |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1263 | final long token = Binder.clearCallingIdentity(); |
| 1264 | try { |
| 1265 | synchronized (mLock) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1266 | dispatchAdjustVolumeLocked(suggestedStream, delta, flags); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } finally { |
| 1269 | Binder.restoreCallingIdentity(token); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | @Override |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 1274 | public void setRemoteVolumeController(IRemoteVolumeController rvc) { |
| 1275 | final int pid = Binder.getCallingPid(); |
| 1276 | final int uid = Binder.getCallingUid(); |
| 1277 | final long token = Binder.clearCallingIdentity(); |
| 1278 | try { |
John Spurlock | eb69e24 | 2015-02-17 17:15:04 -0500 | [diff] [blame] | 1279 | enforceSystemUiPermission("listen for volume changes", pid, uid); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 1280 | mRvc = rvc; |
| 1281 | } finally { |
| 1282 | Binder.restoreCallingIdentity(token); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | @Override |
RoboErik | de9ba39 | 2014-09-26 12:51:01 -0700 | [diff] [blame] | 1287 | public boolean isGlobalPriorityActive() { |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 1288 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1289 | return isGlobalPriorityActiveLocked(); |
Jaewan Kim | 5125501 | 2017-02-24 16:19:14 +0900 | [diff] [blame] | 1290 | } |
RoboErik | de9ba39 | 2014-09-26 12:51:01 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | @Override |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1294 | public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1295 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return; |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1296 | |
| 1297 | pw.println("MEDIA SESSION SERVICE (dumpsys media_session)"); |
| 1298 | pw.println(); |
| 1299 | |
| 1300 | synchronized (mLock) { |
RoboErik | a08adb24 | 2014-11-21 18:28:18 -0800 | [diff] [blame] | 1301 | pw.println(mSessionsListeners.size() + " sessions listeners."); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1302 | pw.println("Global priority session is " + mGlobalPrioritySession); |
Jaewan Kim | 101b4d5 | 2017-05-18 13:23:11 +0900 | [diff] [blame] | 1303 | if (mGlobalPrioritySession != null) { |
| 1304 | mGlobalPrioritySession.dump(pw, " "); |
| 1305 | } |
RoboErik | 4646d28 | 2014-05-13 10:13:04 -0700 | [diff] [blame] | 1306 | pw.println("User Records:"); |
Jaewan Kim | e0ca3f3 | 2017-02-16 15:52:39 +0900 | [diff] [blame] | 1307 | int count = mUserRecords.size(); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1308 | for (int i = 0; i < count; i++) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1309 | mUserRecords.valueAt(i).dumpLocked(pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1310 | } |
Sungsoo Lim | 875e697 | 2017-11-03 02:22:35 +0000 | [diff] [blame] | 1311 | mAudioPlayerStateMonitor.dump(getContext(), pw, ""); |
RoboErik | a278ea7 | 2014-04-24 14:49:01 -0700 | [diff] [blame] | 1312 | } |
| 1313 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1314 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1315 | private int verifySessionsRequest(ComponentName componentName, int userId, final int pid, |
| 1316 | final int uid) { |
| 1317 | String packageName = null; |
| 1318 | if (componentName != null) { |
| 1319 | // If they gave us a component name verify they own the |
| 1320 | // package |
| 1321 | packageName = componentName.getPackageName(); |
| 1322 | enforcePackageName(packageName, uid); |
| 1323 | } |
| 1324 | // Check that they can make calls on behalf of the user and |
| 1325 | // get the final user id |
| 1326 | int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId, |
| 1327 | true /* allowAll */, true /* requireFull */, "getSessions", packageName); |
| 1328 | // Check if they have the permissions or their component is |
| 1329 | // enabled for the user they're calling from. |
| 1330 | enforceMediaPermissions(componentName, pid, uid, resolvedUserId); |
| 1331 | return resolvedUserId; |
| 1332 | } |
| 1333 | |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1334 | private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1335 | MediaSessionRecord session = isGlobalPriorityActiveLocked() ? mGlobalPrioritySession |
| 1336 | : mCurrentFullUserRecord.mPriorityStack.getDefaultVolumeSession(); |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1337 | |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1338 | boolean preferSuggestedStream = false; |
| 1339 | if (isValidLocalStreamType(suggestedStream) |
| 1340 | && AudioSystem.isStreamActive(suggestedStream, 0)) { |
| 1341 | preferSuggestedStream = true; |
| 1342 | } |
Jaewan Kim | b2781e7 | 2017-03-02 09:57:09 +0900 | [diff] [blame] | 1343 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 1344 | Log.d(TAG, "Adjusting " + session + " by " + direction + ". flags=" |
| 1345 | + flags + ", suggestedStream=" + suggestedStream |
| 1346 | + ", preferSuggestedStream=" + preferSuggestedStream); |
| 1347 | } |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1348 | if (session == null || preferSuggestedStream) { |
RoboErik | 94c716e | 2014-09-14 13:54:31 -0700 | [diff] [blame] | 1349 | if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0 |
| 1350 | && !AudioSystem.isStreamActive(AudioManager.STREAM_MUSIC, 0)) { |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1351 | if (DEBUG) { |
| 1352 | Log.d(TAG, "No active session to adjust, skipping media only volume event"); |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1353 | } |
RoboErik | b7c014c | 2014-07-22 15:58:22 -0700 | [diff] [blame] | 1354 | return; |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 1355 | } |
Shibin George | 19e8404 | 2016-06-14 20:42:13 +0530 | [diff] [blame] | 1356 | |
| 1357 | // Execute mAudioService.adjustSuggestedStreamVolume() on |
| 1358 | // handler thread of MediaSessionService. |
| 1359 | // This will release the MediaSessionService.mLock sooner and avoid |
| 1360 | // a potential deadlock between MediaSessionService.mLock and |
| 1361 | // ActivityManagerService lock. |
| 1362 | mHandler.post(new Runnable() { |
| 1363 | @Override |
| 1364 | public void run() { |
| 1365 | try { |
| 1366 | String packageName = getContext().getOpPackageName(); |
| 1367 | mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream, |
| 1368 | flags, packageName, TAG); |
| 1369 | } catch (RemoteException e) { |
| 1370 | Log.e(TAG, "Error adjusting default volume.", e); |
Hyundo Moon | 739d6c2 | 2017-09-18 17:01:48 +0900 | [diff] [blame] | 1371 | } catch (IllegalArgumentException e) { |
| 1372 | Log.e(TAG, "Cannot adjust volume: direction=" + direction |
| 1373 | + ", suggestedStream=" + suggestedStream + ", flags=" + flags, |
| 1374 | e); |
Shibin George | 19e8404 | 2016-06-14 20:42:13 +0530 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | }); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1378 | } else { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1379 | session.adjustVolume(direction, flags, getContext().getPackageName(), |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1380 | Process.SYSTEM_UID, true); |
RoboErik | b69ffd4 | 2014-05-30 14:57:59 -0700 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1384 | private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) { |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1385 | int action = keyEvent.getAction(); |
| 1386 | boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0; |
| 1387 | if (action == KeyEvent.ACTION_DOWN) { |
| 1388 | if (keyEvent.getRepeatCount() == 0) { |
| 1389 | mVoiceButtonDown = true; |
| 1390 | mVoiceButtonHandled = false; |
| 1391 | } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) { |
| 1392 | mVoiceButtonHandled = true; |
| 1393 | startVoiceInput(needWakeLock); |
| 1394 | } |
| 1395 | } else if (action == KeyEvent.ACTION_UP) { |
| 1396 | if (mVoiceButtonDown) { |
| 1397 | mVoiceButtonDown = false; |
| 1398 | if (!mVoiceButtonHandled && !keyEvent.isCanceled()) { |
| 1399 | // Resend the down then send this event through |
| 1400 | KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN); |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1401 | dispatchMediaKeyEventLocked(downEvent, needWakeLock); |
| 1402 | dispatchMediaKeyEventLocked(keyEvent, needWakeLock); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1408 | private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1409 | MediaSessionRecord session = mCurrentFullUserRecord.getMediaButtonSessionLocked(); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1410 | if (session != null) { |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1411 | if (DEBUG_KEY_EVENT) { |
Jaewan Kim | 5e1476e | 2016-07-19 22:25:39 +0900 | [diff] [blame] | 1412 | Log.d(TAG, "Sending " + keyEvent + " to " + session); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1413 | } |
| 1414 | if (needWakeLock) { |
| 1415 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 1416 | } |
Jaewan Kim | 5026936 | 2016-12-23 11:22:02 +0900 | [diff] [blame] | 1417 | // 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] | 1418 | session.sendMediaButton(keyEvent, |
| 1419 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1420 | mKeyEventReceiver, Process.SYSTEM_UID, |
Donghyun Cho | 1ea5683 | 2016-02-23 16:30:07 +0900 | [diff] [blame] | 1421 | getContext().getPackageName()); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1422 | if (mCurrentFullUserRecord.mCallback != null) { |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1423 | try { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1424 | mCurrentFullUserRecord.mCallback.onMediaKeyEventDispatchedToMediaSession( |
| 1425 | keyEvent, |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1426 | new MediaSession.Token(session.getControllerBinder())); |
| 1427 | } catch (RemoteException e) { |
| 1428 | Log.w(TAG, "Failed to send callback", e); |
| 1429 | } |
| 1430 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1431 | } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null |
| 1432 | || mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) { |
| 1433 | if (needWakeLock) { |
| 1434 | mKeyEventReceiver.aquireWakeLockLocked(); |
| 1435 | } |
| 1436 | Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); |
| 1437 | mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 1438 | mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); |
| 1439 | try { |
| 1440 | if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) { |
| 1441 | PendingIntent receiver = mCurrentFullUserRecord.mLastMediaButtonReceiver; |
| 1442 | if (DEBUG_KEY_EVENT) { |
| 1443 | Log.d(TAG, "Sending " + keyEvent |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1444 | + " to the last known PendingIntent " + receiver); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1445 | } |
| 1446 | receiver.send(getContext(), |
| 1447 | needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, |
| 1448 | mediaButtonIntent, mKeyEventReceiver, mHandler); |
| 1449 | if (mCurrentFullUserRecord.mCallback != null) { |
| 1450 | ComponentName componentName = mCurrentFullUserRecord |
| 1451 | .mLastMediaButtonReceiver.getIntent().getComponent(); |
| 1452 | if (componentName != null) { |
| 1453 | mCurrentFullUserRecord.mCallback |
| 1454 | .onMediaKeyEventDispatchedToMediaButtonReceiver( |
| 1455 | keyEvent, componentName); |
Jaewan Kim | bd16f45 | 2017-02-03 16:21:38 +0900 | [diff] [blame] | 1456 | } |
RoboErik | c8f92d1 | 2015-01-05 16:48:07 -0800 | [diff] [blame] | 1457 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1458 | } else { |
| 1459 | ComponentName receiver = |
| 1460 | mCurrentFullUserRecord.mRestoredMediaButtonReceiver; |
| 1461 | if (DEBUG_KEY_EVENT) { |
| 1462 | Log.d(TAG, "Sending " + keyEvent + " to the restored intent " |
| 1463 | + receiver); |
| 1464 | } |
| 1465 | mediaButtonIntent.setComponent(receiver); |
| 1466 | getContext().sendBroadcastAsUser(mediaButtonIntent, |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1467 | UserHandle.of(mCurrentFullUserRecord |
| 1468 | .mRestoredMediaButtonReceiverUserId)); |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1469 | if (mCurrentFullUserRecord.mCallback != null) { |
| 1470 | mCurrentFullUserRecord.mCallback |
| 1471 | .onMediaKeyEventDispatchedToMediaButtonReceiver( |
| 1472 | keyEvent, receiver); |
| 1473 | } |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1474 | } |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1475 | } catch (CanceledException e) { |
| 1476 | Log.i(TAG, "Error sending key event to media button receiver " |
| 1477 | + mCurrentFullUserRecord.mLastMediaButtonReceiver, e); |
| 1478 | } catch (RemoteException e) { |
| 1479 | Log.w(TAG, "Failed to send callback", e); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1480 | } |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | private void startVoiceInput(boolean needWakeLock) { |
| 1485 | Intent voiceIntent = null; |
| 1486 | // select which type of search to launch: |
| 1487 | // - screen on and device unlocked: action is ACTION_WEB_SEARCH |
| 1488 | // - device locked or screen off: action is |
| 1489 | // ACTION_VOICE_SEARCH_HANDS_FREE |
| 1490 | // with EXTRA_SECURE set to true if the device is securely locked |
| 1491 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
| 1492 | boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 1493 | if (!isLocked && pm.isScreenOn()) { |
| 1494 | voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); |
| 1495 | Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH"); |
| 1496 | } else { |
| 1497 | voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); |
| 1498 | voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, |
| 1499 | isLocked && mKeyguardManager.isKeyguardSecure()); |
| 1500 | Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE"); |
| 1501 | } |
| 1502 | // start the search activity |
| 1503 | if (needWakeLock) { |
| 1504 | mMediaEventWakeLock.acquire(); |
| 1505 | } |
| 1506 | try { |
| 1507 | if (voiceIntent != null) { |
| 1508 | voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 1509 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Jaewan Kim | 8f72908 | 2016-06-21 12:36:26 +0900 | [diff] [blame] | 1510 | if (DEBUG) Log.d(TAG, "voiceIntent: " + voiceIntent); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1511 | getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT); |
| 1512 | } |
| 1513 | } catch (ActivityNotFoundException e) { |
| 1514 | Log.w(TAG, "No activity for search: " + e); |
| 1515 | } finally { |
| 1516 | if (needWakeLock) { |
| 1517 | mMediaEventWakeLock.release(); |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | private boolean isVoiceKey(int keyCode) { |
Jaewan Kim | ba18d8e | 2017-05-12 17:37:57 +0900 | [diff] [blame] | 1523 | return keyCode == KeyEvent.KEYCODE_HEADSETHOOK |
Jaewan Kim | fdb612e | 2017-07-01 09:23:41 +0900 | [diff] [blame] | 1524 | || (!mHasFeatureLeanback && keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE); |
RoboErik | 9a9d0b5 | 2014-05-20 14:53:39 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Jeff Brown | 38d3feb | 2015-03-19 18:26:30 -0700 | [diff] [blame] | 1527 | private boolean isUserSetupComplete() { |
| 1528 | return Settings.Secure.getIntForUser(getContext().getContentResolver(), |
| 1529 | Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0; |
| 1530 | } |
| 1531 | |
RoboErik | 9c78540 | 2014-11-11 16:52:26 -0800 | [diff] [blame] | 1532 | // we only handle public stream types, which are 0-5 |
| 1533 | private boolean isValidLocalStreamType(int streamType) { |
| 1534 | return streamType >= AudioManager.STREAM_VOICE_CALL |
| 1535 | && streamType <= AudioManager.STREAM_NOTIFICATION; |
| 1536 | } |
| 1537 | |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1538 | private class MediaKeyListenerResultReceiver extends ResultReceiver implements Runnable { |
| 1539 | private KeyEvent mKeyEvent; |
| 1540 | private boolean mNeedWakeLock; |
| 1541 | private boolean mHandled; |
| 1542 | |
| 1543 | private MediaKeyListenerResultReceiver(KeyEvent keyEvent, boolean needWakeLock) { |
| 1544 | super(mHandler); |
| 1545 | mHandler.postDelayed(this, MEDIA_KEY_LISTENER_TIMEOUT); |
| 1546 | mKeyEvent = keyEvent; |
| 1547 | mNeedWakeLock = needWakeLock; |
| 1548 | } |
| 1549 | |
| 1550 | @Override |
| 1551 | public void run() { |
| 1552 | Log.d(TAG, "The media key listener is timed-out for " + mKeyEvent); |
| 1553 | dispatchMediaKeyEvent(); |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 1558 | if (resultCode == MediaSessionManager.RESULT_MEDIA_KEY_HANDLED) { |
| 1559 | mHandled = true; |
| 1560 | mHandler.removeCallbacks(this); |
| 1561 | return; |
| 1562 | } |
| 1563 | dispatchMediaKeyEvent(); |
| 1564 | } |
| 1565 | |
| 1566 | private void dispatchMediaKeyEvent() { |
| 1567 | if (mHandled) { |
| 1568 | return; |
| 1569 | } |
| 1570 | mHandled = true; |
| 1571 | mHandler.removeCallbacks(this); |
| 1572 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1573 | if (!isGlobalPriorityActiveLocked() |
Jaewan Kim | 98003d3 | 2017-02-24 18:33:04 +0900 | [diff] [blame] | 1574 | && isVoiceKey(mKeyEvent.getKeyCode())) { |
| 1575 | handleVoiceKeyEventLocked(mKeyEvent, mNeedWakeLock); |
| 1576 | } else { |
| 1577 | dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock); |
| 1578 | } |
Jaewan Kim | 6e2b01c | 2017-01-19 16:33:14 -0800 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1583 | private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler); |
| 1584 | |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1585 | class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable, |
| 1586 | PendingIntent.OnFinished { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1587 | private final Handler mHandler; |
| 1588 | private int mRefCount = 0; |
| 1589 | private int mLastTimeoutId = 0; |
| 1590 | |
| 1591 | public KeyEventWakeLockReceiver(Handler handler) { |
| 1592 | super(handler); |
| 1593 | mHandler = handler; |
| 1594 | } |
| 1595 | |
| 1596 | public void onTimeout() { |
| 1597 | synchronized (mLock) { |
| 1598 | if (mRefCount == 0) { |
| 1599 | // We've already released it, so just return |
| 1600 | return; |
| 1601 | } |
| 1602 | mLastTimeoutId++; |
| 1603 | mRefCount = 0; |
| 1604 | releaseWakeLockLocked(); |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | public void aquireWakeLockLocked() { |
| 1609 | if (mRefCount == 0) { |
| 1610 | mMediaEventWakeLock.acquire(); |
| 1611 | } |
| 1612 | mRefCount++; |
| 1613 | mHandler.removeCallbacks(this); |
| 1614 | mHandler.postDelayed(this, WAKELOCK_TIMEOUT); |
| 1615 | |
| 1616 | } |
| 1617 | |
| 1618 | @Override |
| 1619 | public void run() { |
| 1620 | onTimeout(); |
| 1621 | } |
| 1622 | |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1623 | @Override |
| 1624 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1625 | if (resultCode < mLastTimeoutId) { |
| 1626 | // Ignore results from calls that were before the last |
| 1627 | // timeout, just in case. |
| 1628 | return; |
| 1629 | } else { |
| 1630 | synchronized (mLock) { |
| 1631 | if (mRefCount > 0) { |
| 1632 | mRefCount--; |
| 1633 | if (mRefCount == 0) { |
| 1634 | releaseWakeLockLocked(); |
| 1635 | } |
| 1636 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | } |
RoboErik | 418c10c | 2014-05-19 09:25:25 -0700 | [diff] [blame] | 1640 | |
| 1641 | private void releaseWakeLockLocked() { |
| 1642 | mMediaEventWakeLock.release(); |
| 1643 | mHandler.removeCallbacks(this); |
| 1644 | } |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 1645 | |
| 1646 | @Override |
| 1647 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 1648 | String resultData, Bundle resultExtras) { |
| 1649 | onReceiveResult(resultCode, null); |
| 1650 | } |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 1651 | }; |
| 1652 | |
| 1653 | BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { |
| 1654 | @Override |
| 1655 | public void onReceive(Context context, Intent intent) { |
| 1656 | if (intent == null) { |
| 1657 | return; |
| 1658 | } |
| 1659 | Bundle extras = intent.getExtras(); |
| 1660 | if (extras == null) { |
| 1661 | return; |
| 1662 | } |
| 1663 | synchronized (mLock) { |
| 1664 | if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED) |
| 1665 | && mMediaEventWakeLock.isHeld()) { |
| 1666 | mMediaEventWakeLock.release(); |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | }; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1673 | final class MessageHandler extends Handler { |
| 1674 | private static final int MSG_SESSIONS_CHANGED = 1; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1675 | private static final int MSG_VOLUME_INITIAL_DOWN = 2; |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1676 | private final SparseArray<Integer> mIntegerCache = new SparseArray<>(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1677 | |
| 1678 | @Override |
| 1679 | public void handleMessage(Message msg) { |
| 1680 | switch (msg.what) { |
| 1681 | case MSG_SESSIONS_CHANGED: |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1682 | pushSessionsChanged((int) msg.obj); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1683 | break; |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1684 | case MSG_VOLUME_INITIAL_DOWN: |
| 1685 | synchronized (mLock) { |
Jaewan Kim | a7dce19 | 2017-02-16 17:10:54 +0900 | [diff] [blame] | 1686 | FullUserRecord user = mUserRecords.get((int) msg.arg1); |
Jaewan Kim | d61a87b | 2017-02-17 23:14:10 +0900 | [diff] [blame] | 1687 | if (user != null && user.mInitialDownVolumeKeyEvent != null) { |
| 1688 | dispatchVolumeKeyLongPressLocked(user.mInitialDownVolumeKeyEvent); |
| 1689 | // Mark that the key is already handled. |
| 1690 | user.mInitialDownVolumeKeyEvent = null; |
| 1691 | } |
| 1692 | } |
| 1693 | break; |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 1697 | public void postSessionsChanged(int userId) { |
| 1698 | // Use object instead of the arguments when posting message to remove pending requests. |
| 1699 | Integer userIdInteger = mIntegerCache.get(userId); |
| 1700 | if (userIdInteger == null) { |
| 1701 | userIdInteger = Integer.valueOf(userId); |
| 1702 | mIntegerCache.put(userId, userIdInteger); |
| 1703 | } |
| 1704 | removeMessages(MSG_SESSIONS_CHANGED, userIdInteger); |
| 1705 | obtainMessage(MSG_SESSIONS_CHANGED, userIdInteger).sendToTarget(); |
RoboErik | 2e7a916 | 2014-06-04 16:53:45 -0700 | [diff] [blame] | 1706 | } |
| 1707 | } |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 1708 | } |