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