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