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