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