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