Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 19 | import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR; |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 20 | import static android.media.MediaRouter2Utils.getOriginalId; |
| 21 | import static android.media.MediaRouter2Utils.getProviderId; |
| 22 | |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 23 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 24 | |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 25 | import android.annotation.NonNull; |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 27 | import android.app.ActivityManager; |
| 28 | import android.content.Context; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 30 | import android.media.IMediaRouter2; |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 31 | import android.media.IMediaRouter2Manager; |
Kyunglyul Hyun | caae8dc | 2019-04-29 15:45:23 +0900 | [diff] [blame] | 32 | import android.media.MediaRoute2Info; |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 33 | import android.media.MediaRoute2ProviderInfo; |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 34 | import android.media.MediaRoute2ProviderService; |
| 35 | import android.media.MediaRouter2Manager; |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 36 | import android.media.RouteDiscoveryPreference; |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 37 | import android.media.RoutingSessionInfo; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 38 | import android.os.Binder; |
Kyunglyul Hyun | 7d720f7 | 2020-01-17 11:03:28 +0000 | [diff] [blame] | 39 | import android.os.Bundle; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.IBinder; |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 42 | import android.os.Looper; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 43 | import android.os.RemoteException; |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 44 | import android.os.UserHandle; |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 45 | import android.text.TextUtils; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 46 | import android.util.ArrayMap; |
| 47 | import android.util.Log; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 48 | import android.util.Slog; |
| 49 | import android.util.SparseArray; |
| 50 | |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 51 | import com.android.internal.annotations.GuardedBy; |
Kyunglyul Hyun | 30e2c65 | 2019-07-11 16:25:31 +0900 | [diff] [blame] | 52 | import com.android.internal.util.function.pooled.PooledLambda; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 53 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 54 | import java.io.PrintWriter; |
| 55 | import java.lang.ref.WeakReference; |
| 56 | import java.util.ArrayList; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 57 | import java.util.Collection; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 58 | import java.util.Collections; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 59 | import java.util.List; |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 60 | import java.util.Map; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 61 | import java.util.Objects; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 62 | import java.util.concurrent.CopyOnWriteArrayList; |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 63 | import java.util.concurrent.atomic.AtomicInteger; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 64 | |
| 65 | /** |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 66 | * Implements features related to {@link android.media.MediaRouter2} and |
| 67 | * {@link android.media.MediaRouter2Manager}. |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 68 | */ |
| 69 | class MediaRouter2ServiceImpl { |
Hyundo Moon | 7d3ab33 | 2019-10-16 11:09:56 +0900 | [diff] [blame] | 70 | private static final String TAG = "MR2ServiceImpl"; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 71 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 72 | |
Hyundo Moon | 8ca032d | 2020-03-31 15:45:45 +0900 | [diff] [blame] | 73 | // TODO: (In Android S or later) if we add callback methods for generic failures |
| 74 | // in MediaRouter2, remove this constant and replace the usages with the real request IDs. |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 75 | private static final long DUMMY_REQUEST_ID = -1; |
| 76 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 77 | private final Context mContext; |
| 78 | private final Object mLock = new Object(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 79 | final AtomicInteger mNextRouterOrManagerId = new AtomicInteger(1); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 80 | |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 81 | @GuardedBy("mLock") |
| 82 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<>(); |
| 83 | @GuardedBy("mLock") |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 84 | private final ArrayMap<IBinder, RouterRecord> mAllRouterRecords = new ArrayMap<>(); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 85 | @GuardedBy("mLock") |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 86 | private final ArrayMap<IBinder, ManagerRecord> mAllManagerRecords = new ArrayMap<>(); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 87 | @GuardedBy("mLock") |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 88 | private int mCurrentUserId = -1; |
| 89 | |
| 90 | MediaRouter2ServiceImpl(Context context) { |
| 91 | mContext = context; |
| 92 | } |
| 93 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 94 | //////////////////////////////////////////////////////////////// |
| 95 | //// Calls from MediaRouter2 |
| 96 | //// - Should not have @NonNull/@Nullable on any arguments |
| 97 | //////////////////////////////////////////////////////////////// |
| 98 | |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 99 | @NonNull |
| 100 | public List<MediaRoute2Info> getSystemRoutes() { |
| 101 | final int uid = Binder.getCallingUid(); |
Sungsoo Lim | 277ea28 | 2020-01-14 16:35:05 +0900 | [diff] [blame] | 102 | final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 103 | final boolean hasModifyAudioRoutingPermission = mContext.checkCallingOrSelfPermission( |
| 104 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 105 | == PackageManager.PERMISSION_GRANTED; |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 106 | |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 107 | final long token = Binder.clearCallingIdentity(); |
| 108 | try { |
| 109 | Collection<MediaRoute2Info> systemRoutes; |
| 110 | synchronized (mLock) { |
| 111 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 112 | if (hasModifyAudioRoutingPermission) { |
| 113 | MediaRoute2ProviderInfo providerInfo = |
| 114 | userRecord.mHandler.mSystemProvider.getProviderInfo(); |
| 115 | if (providerInfo != null) { |
| 116 | systemRoutes = providerInfo.getRoutes(); |
| 117 | } else { |
| 118 | systemRoutes = Collections.emptyList(); |
| 119 | } |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 120 | } else { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 121 | systemRoutes = new ArrayList<>(); |
| 122 | systemRoutes.add(userRecord.mHandler.mSystemProvider.getDefaultRoute()); |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 123 | } |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 124 | } |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 125 | return new ArrayList<>(systemRoutes); |
| 126 | } finally { |
| 127 | Binder.restoreCallingIdentity(token); |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 128 | } |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 131 | @NonNull |
| 132 | public RoutingSessionInfo getSystemSessionInfo() { |
| 133 | final int uid = Binder.getCallingUid(); |
| 134 | final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 135 | final boolean hasModifyAudioRoutingPermission = mContext.checkCallingOrSelfPermission( |
| 136 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 137 | == PackageManager.PERMISSION_GRANTED; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 138 | |
| 139 | final long token = Binder.clearCallingIdentity(); |
| 140 | try { |
| 141 | RoutingSessionInfo systemSessionInfo = null; |
| 142 | synchronized (mLock) { |
| 143 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 144 | List<RoutingSessionInfo> sessionInfos; |
| 145 | if (hasModifyAudioRoutingPermission) { |
| 146 | sessionInfos = userRecord.mHandler.mSystemProvider.getSessionInfos(); |
| 147 | if (sessionInfos != null && !sessionInfos.isEmpty()) { |
| 148 | systemSessionInfo = sessionInfos.get(0); |
| 149 | } else { |
| 150 | Slog.w(TAG, "System provider does not have any session info."); |
| 151 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 152 | } else { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 153 | systemSessionInfo = userRecord.mHandler.mSystemProvider.getDefaultSessionInfo(); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | return systemSessionInfo; |
| 157 | } finally { |
| 158 | Binder.restoreCallingIdentity(token); |
| 159 | } |
| 160 | } |
| 161 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 162 | public void registerRouter2(IMediaRouter2 router, String packageName) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 163 | Objects.requireNonNull(router, "router must not be null"); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 164 | if (TextUtils.isEmpty(packageName)) { |
| 165 | throw new IllegalArgumentException("packageName must not be empty"); |
| 166 | } |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 167 | |
| 168 | final int uid = Binder.getCallingUid(); |
| 169 | final int pid = Binder.getCallingPid(); |
Sungsoo Lim | 277ea28 | 2020-01-14 16:35:05 +0900 | [diff] [blame] | 170 | final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier(); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 171 | final boolean hasConfigureWifiDisplayPermission = mContext.checkCallingOrSelfPermission( |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 172 | android.Manifest.permission.CONFIGURE_WIFI_DISPLAY) |
| 173 | == PackageManager.PERMISSION_GRANTED; |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 174 | final boolean hasModifyAudioRoutingPermission = mContext.checkCallingOrSelfPermission( |
| 175 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 176 | == PackageManager.PERMISSION_GRANTED; |
| 177 | |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 178 | final long token = Binder.clearCallingIdentity(); |
| 179 | try { |
| 180 | synchronized (mLock) { |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 181 | registerRouter2Locked(router, uid, pid, packageName, userId, |
| 182 | hasConfigureWifiDisplayPermission, hasModifyAudioRoutingPermission); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 183 | } |
| 184 | } finally { |
| 185 | Binder.restoreCallingIdentity(token); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 186 | } |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 189 | public void unregisterRouter2(IMediaRouter2 router) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 190 | Objects.requireNonNull(router, "router must not be null"); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 191 | |
| 192 | final long token = Binder.clearCallingIdentity(); |
| 193 | try { |
| 194 | synchronized (mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 195 | unregisterRouter2Locked(router, false); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 196 | } |
| 197 | } finally { |
| 198 | Binder.restoreCallingIdentity(token); |
| 199 | } |
| 200 | } |
| 201 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 202 | public void setDiscoveryRequestWithRouter2(IMediaRouter2 router, |
| 203 | RouteDiscoveryPreference preference) { |
| 204 | Objects.requireNonNull(router, "router must not be null"); |
| 205 | Objects.requireNonNull(preference, "preference must not be null"); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 206 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 207 | final long token = Binder.clearCallingIdentity(); |
| 208 | try { |
| 209 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 210 | RouterRecord routerRecord = mAllRouterRecords.get(router.asBinder()); |
| 211 | if (routerRecord == null) { |
| 212 | Slog.w(TAG, "Ignoring updating discoveryRequest of null routerRecord."); |
| 213 | return; |
| 214 | } |
| 215 | setDiscoveryRequestWithRouter2Locked(routerRecord, preference); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 216 | } |
| 217 | } finally { |
| 218 | Binder.restoreCallingIdentity(token); |
| 219 | } |
| 220 | } |
| 221 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 222 | public void setRouteVolumeWithRouter2(IMediaRouter2 router, |
| 223 | MediaRoute2Info route, int volume) { |
| 224 | Objects.requireNonNull(router, "router must not be null"); |
| 225 | Objects.requireNonNull(route, "route must not be null"); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 226 | |
| 227 | final long token = Binder.clearCallingIdentity(); |
| 228 | try { |
| 229 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 230 | setRouteVolumeWithRouter2Locked(router, route, volume); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 231 | } |
| 232 | } finally { |
| 233 | Binder.restoreCallingIdentity(token); |
| 234 | } |
| 235 | } |
| 236 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 237 | public void requestCreateSessionWithRouter2(IMediaRouter2 router, int requestId, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 238 | long managerRequestId, RoutingSessionInfo oldSession, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 239 | MediaRoute2Info route, Bundle sessionHints) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 240 | Objects.requireNonNull(router, "router must not be null"); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 241 | Objects.requireNonNull(oldSession, "oldSession must not be null"); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 242 | Objects.requireNonNull(route, "route must not be null"); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 243 | |
| 244 | final long token = Binder.clearCallingIdentity(); |
| 245 | try { |
| 246 | synchronized (mLock) { |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 247 | requestCreateSessionWithRouter2Locked(requestId, managerRequestId, |
| 248 | router, oldSession, route, sessionHints); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 249 | } |
| 250 | } finally { |
| 251 | Binder.restoreCallingIdentity(token); |
| 252 | } |
| 253 | } |
| 254 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 255 | public void selectRouteWithRouter2(IMediaRouter2 router, String uniqueSessionId, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 256 | MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 257 | Objects.requireNonNull(router, "router must not be null"); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 258 | Objects.requireNonNull(route, "route must not be null"); |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 259 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 260 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 261 | } |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 262 | |
| 263 | final long token = Binder.clearCallingIdentity(); |
| 264 | try { |
| 265 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 266 | selectRouteWithRouter2Locked(router, uniqueSessionId, route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 267 | } |
| 268 | } finally { |
| 269 | Binder.restoreCallingIdentity(token); |
| 270 | } |
| 271 | } |
| 272 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 273 | public void deselectRouteWithRouter2(IMediaRouter2 router, String uniqueSessionId, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 274 | MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 275 | Objects.requireNonNull(router, "router must not be null"); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 276 | Objects.requireNonNull(route, "route must not be null"); |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 277 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 278 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 279 | } |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 280 | |
| 281 | final long token = Binder.clearCallingIdentity(); |
| 282 | try { |
| 283 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 284 | deselectRouteWithRouter2Locked(router, uniqueSessionId, route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 285 | } |
| 286 | } finally { |
| 287 | Binder.restoreCallingIdentity(token); |
| 288 | } |
| 289 | } |
| 290 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 291 | public void transferToRouteWithRouter2(IMediaRouter2 router, String uniqueSessionId, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 292 | MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 293 | Objects.requireNonNull(router, "router must not be null"); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 294 | Objects.requireNonNull(route, "route must not be null"); |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 295 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 296 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 297 | } |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 298 | |
| 299 | final long token = Binder.clearCallingIdentity(); |
| 300 | try { |
| 301 | synchronized (mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 302 | transferToRouteWithRouter2Locked(router, uniqueSessionId, route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 303 | } |
| 304 | } finally { |
| 305 | Binder.restoreCallingIdentity(token); |
| 306 | } |
| 307 | } |
| 308 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 309 | public void setSessionVolumeWithRouter2(IMediaRouter2 router, String uniqueSessionId, |
| 310 | int volume) { |
| 311 | Objects.requireNonNull(router, "router must not be null"); |
| 312 | Objects.requireNonNull(uniqueSessionId, "uniqueSessionId must not be null"); |
| 313 | |
| 314 | final long token = Binder.clearCallingIdentity(); |
| 315 | try { |
| 316 | synchronized (mLock) { |
| 317 | setSessionVolumeWithRouter2Locked(router, uniqueSessionId, volume); |
| 318 | } |
| 319 | } finally { |
| 320 | Binder.restoreCallingIdentity(token); |
| 321 | } |
| 322 | } |
| 323 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 324 | public void releaseSessionWithRouter2(IMediaRouter2 router, String uniqueSessionId) { |
| 325 | Objects.requireNonNull(router, "router must not be null"); |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 326 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 327 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 328 | } |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 329 | |
| 330 | final long token = Binder.clearCallingIdentity(); |
| 331 | try { |
| 332 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 333 | releaseSessionWithRouter2Locked(router, uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 334 | } |
| 335 | } finally { |
| 336 | Binder.restoreCallingIdentity(token); |
| 337 | } |
| 338 | } |
| 339 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 340 | //////////////////////////////////////////////////////////////// |
| 341 | //// Calls from MediaRouter2Manager |
| 342 | //// - Should not have @NonNull/@Nullable on any arguments |
| 343 | //////////////////////////////////////////////////////////////// |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 344 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 345 | @NonNull |
| 346 | public List<RoutingSessionInfo> getActiveSessions(IMediaRouter2Manager manager) { |
| 347 | Objects.requireNonNull(manager, "manager must not be null"); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 348 | final long token = Binder.clearCallingIdentity(); |
| 349 | try { |
| 350 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 351 | return getActiveSessionsLocked(manager); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 352 | } |
| 353 | } finally { |
| 354 | Binder.restoreCallingIdentity(token); |
| 355 | } |
| 356 | } |
| 357 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 358 | public void registerManager(IMediaRouter2Manager manager, String packageName) { |
| 359 | Objects.requireNonNull(manager, "manager must not be null"); |
| 360 | if (TextUtils.isEmpty(packageName)) { |
| 361 | throw new IllegalArgumentException("packageName must not be empty"); |
| 362 | } |
| 363 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 364 | final int uid = Binder.getCallingUid(); |
| 365 | final int pid = Binder.getCallingPid(); |
| 366 | final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier(); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 367 | |
| 368 | final long token = Binder.clearCallingIdentity(); |
| 369 | try { |
| 370 | synchronized (mLock) { |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 371 | registerManagerLocked(manager, uid, pid, packageName, userId); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 372 | } |
| 373 | } finally { |
| 374 | Binder.restoreCallingIdentity(token); |
| 375 | } |
| 376 | } |
| 377 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 378 | public void unregisterManager(IMediaRouter2Manager manager) { |
| 379 | Objects.requireNonNull(manager, "manager must not be null"); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 380 | |
| 381 | final long token = Binder.clearCallingIdentity(); |
| 382 | try { |
| 383 | synchronized (mLock) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 384 | unregisterManagerLocked(manager, false); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 385 | } |
| 386 | } finally { |
| 387 | Binder.restoreCallingIdentity(token); |
| 388 | } |
| 389 | } |
| 390 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 391 | public void setRouteVolumeWithManager(IMediaRouter2Manager manager, int requestId, |
| 392 | MediaRoute2Info route, int volume) { |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 393 | Objects.requireNonNull(manager, "manager must not be null"); |
| 394 | Objects.requireNonNull(route, "route must not be null"); |
| 395 | |
| 396 | final long token = Binder.clearCallingIdentity(); |
| 397 | try { |
| 398 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 399 | setRouteVolumeWithManagerLocked(requestId, manager, route, volume); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 400 | } |
| 401 | } finally { |
| 402 | Binder.restoreCallingIdentity(token); |
| 403 | } |
| 404 | } |
| 405 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 406 | public void requestCreateSessionWithManager(IMediaRouter2Manager manager, int requestId, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 407 | RoutingSessionInfo oldSession, MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 408 | Objects.requireNonNull(manager, "manager must not be null"); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 409 | Objects.requireNonNull(oldSession, "oldSession must not be null"); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 410 | |
| 411 | final long token = Binder.clearCallingIdentity(); |
| 412 | try { |
| 413 | synchronized (mLock) { |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 414 | requestCreateSessionWithManagerLocked(requestId, manager, oldSession, route); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 415 | } |
| 416 | } finally { |
| 417 | Binder.restoreCallingIdentity(token); |
| 418 | } |
| 419 | } |
| 420 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 421 | public void selectRouteWithManager(IMediaRouter2Manager manager, int requestId, |
| 422 | String uniqueSessionId, MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 423 | Objects.requireNonNull(manager, "manager must not be null"); |
| 424 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 425 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 426 | } |
| 427 | Objects.requireNonNull(route, "route must not be null"); |
| 428 | |
| 429 | final long token = Binder.clearCallingIdentity(); |
| 430 | try { |
| 431 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 432 | selectRouteWithManagerLocked(requestId, manager, uniqueSessionId, route); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 433 | } |
| 434 | } finally { |
| 435 | Binder.restoreCallingIdentity(token); |
| 436 | } |
| 437 | } |
| 438 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 439 | public void deselectRouteWithManager(IMediaRouter2Manager manager, int requestId, |
| 440 | String uniqueSessionId, MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 441 | Objects.requireNonNull(manager, "manager must not be null"); |
| 442 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 443 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 444 | } |
| 445 | Objects.requireNonNull(route, "route must not be null"); |
| 446 | |
| 447 | final long token = Binder.clearCallingIdentity(); |
| 448 | try { |
| 449 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 450 | deselectRouteWithManagerLocked(requestId, manager, uniqueSessionId, route); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 451 | } |
| 452 | } finally { |
| 453 | Binder.restoreCallingIdentity(token); |
| 454 | } |
| 455 | } |
| 456 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 457 | public void transferToRouteWithManager(IMediaRouter2Manager manager, int requestId, |
| 458 | String uniqueSessionId, MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 459 | Objects.requireNonNull(manager, "manager must not be null"); |
| 460 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 461 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 462 | } |
| 463 | Objects.requireNonNull(route, "route must not be null"); |
| 464 | |
| 465 | final long token = Binder.clearCallingIdentity(); |
| 466 | try { |
| 467 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 468 | transferToRouteWithManagerLocked(requestId, manager, uniqueSessionId, route); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 469 | } |
| 470 | } finally { |
| 471 | Binder.restoreCallingIdentity(token); |
| 472 | } |
| 473 | } |
| 474 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 475 | public void setSessionVolumeWithManager(IMediaRouter2Manager manager, int requestId, |
| 476 | String uniqueSessionId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 477 | Objects.requireNonNull(manager, "manager must not be null"); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 478 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 479 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
| 480 | } |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 481 | |
| 482 | final long token = Binder.clearCallingIdentity(); |
| 483 | try { |
| 484 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 485 | setSessionVolumeWithManagerLocked(requestId, manager, uniqueSessionId, volume); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 486 | } |
| 487 | } finally { |
| 488 | Binder.restoreCallingIdentity(token); |
| 489 | } |
| 490 | } |
| 491 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 492 | public void releaseSessionWithManager(IMediaRouter2Manager manager, int requestId, |
| 493 | String uniqueSessionId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 494 | Objects.requireNonNull(manager, "manager must not be null"); |
| 495 | if (TextUtils.isEmpty(uniqueSessionId)) { |
| 496 | throw new IllegalArgumentException("uniqueSessionId must not be empty"); |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 497 | } |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 498 | |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 499 | final long token = Binder.clearCallingIdentity(); |
| 500 | try { |
| 501 | synchronized (mLock) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 502 | releaseSessionWithManagerLocked(requestId, manager, uniqueSessionId); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 503 | } |
| 504 | } finally { |
| 505 | Binder.restoreCallingIdentity(token); |
| 506 | } |
| 507 | } |
| 508 | |
Kyunglyul Hyun | ef7e88f | 2020-04-01 22:15:08 +0900 | [diff] [blame] | 509 | //TODO(b/136703681): Review this is handling multi-user properly. |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 510 | void switchUser() { |
| 511 | synchronized (mLock) { |
| 512 | int userId = ActivityManager.getCurrentUser(); |
| 513 | if (mCurrentUserId != userId) { |
| 514 | final int oldUserId = mCurrentUserId; |
| 515 | mCurrentUserId = userId; // do this first |
| 516 | |
| 517 | UserRecord oldUser = mUserRecords.get(oldUserId); |
| 518 | if (oldUser != null) { |
Kyunglyul Hyun | 30e2c65 | 2019-07-11 16:25:31 +0900 | [diff] [blame] | 519 | oldUser.mHandler.sendMessage( |
| 520 | obtainMessage(UserHandler::stop, oldUser.mHandler)); |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 521 | disposeUserIfNeededLocked(oldUser); // since no longer current user |
| 522 | } |
| 523 | |
| 524 | UserRecord newUser = mUserRecords.get(userId); |
| 525 | if (newUser != null) { |
Kyunglyul Hyun | 30e2c65 | 2019-07-11 16:25:31 +0900 | [diff] [blame] | 526 | newUser.mHandler.sendMessage( |
| 527 | obtainMessage(UserHandler::start, newUser.mHandler)); |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 533 | void routerDied(@NonNull RouterRecord routerRecord) { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 534 | synchronized (mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 535 | unregisterRouter2Locked(routerRecord.mRouter, true); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 539 | void managerDied(@NonNull ManagerRecord managerRecord) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 540 | synchronized (mLock) { |
| 541 | unregisterManagerLocked(managerRecord.mManager, true); |
| 542 | } |
| 543 | } |
| 544 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 545 | //////////////////////////////////////////////////////////////// |
| 546 | //// ***Locked methods related to MediaRouter2 |
| 547 | //// - Should have @NonNull/@Nullable on all arguments |
| 548 | //////////////////////////////////////////////////////////////// |
| 549 | |
| 550 | private void registerRouter2Locked(@NonNull IMediaRouter2 router, int uid, int pid, |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 551 | @NonNull String packageName, int userId, boolean hasConfigureWifiDisplayPermission, |
| 552 | boolean hasModifyAudioRoutingPermission) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 553 | final IBinder binder = router.asBinder(); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 554 | if (mAllRouterRecords.get(binder) != null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 555 | Slog.w(TAG, "registerRouter2Locked: Same router already exists. packageName=" |
| 556 | + packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 557 | return; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 558 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 559 | |
| 560 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 561 | RouterRecord routerRecord = new RouterRecord(userRecord, router, uid, pid, packageName, |
| 562 | hasConfigureWifiDisplayPermission, hasModifyAudioRoutingPermission); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 563 | try { |
| 564 | binder.linkToDeath(routerRecord, 0); |
| 565 | } catch (RemoteException ex) { |
| 566 | throw new RuntimeException("MediaRouter2 died prematurely.", ex); |
| 567 | } |
| 568 | |
| 569 | userRecord.mRouterRecords.add(routerRecord); |
| 570 | mAllRouterRecords.put(binder, routerRecord); |
| 571 | |
| 572 | userRecord.mHandler.sendMessage( |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 573 | obtainMessage(UserHandler::notifyRouterRegistered, |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 574 | userRecord.mHandler, routerRecord)); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 575 | } |
| 576 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 577 | private void unregisterRouter2Locked(@NonNull IMediaRouter2 router, boolean died) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 578 | RouterRecord routerRecord = mAllRouterRecords.remove(router.asBinder()); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 579 | if (routerRecord == null) { |
| 580 | Slog.w(TAG, "Ignoring unregistering unknown router2"); |
| 581 | return; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 582 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 583 | |
| 584 | UserRecord userRecord = routerRecord.mUserRecord; |
| 585 | userRecord.mRouterRecords.remove(routerRecord); |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 586 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 587 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManagers, |
| 588 | routerRecord.mUserRecord.mHandler, |
| 589 | routerRecord.mPackageName, /* preferredFeatures=*/ null)); |
Kyunglyul Hyun | ef7e88f | 2020-04-01 22:15:08 +0900 | [diff] [blame] | 590 | userRecord.mHandler.sendMessage( |
| 591 | obtainMessage(UserHandler::updateDiscoveryPreferenceOnHandler, |
| 592 | userRecord.mHandler)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 593 | routerRecord.dispose(); |
| 594 | disposeUserIfNeededLocked(userRecord); // since router removed from user |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 595 | } |
| 596 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 597 | private void setDiscoveryRequestWithRouter2Locked(@NonNull RouterRecord routerRecord, |
| 598 | @NonNull RouteDiscoveryPreference discoveryRequest) { |
| 599 | if (routerRecord.mDiscoveryPreference.equals(discoveryRequest)) { |
| 600 | return; |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 601 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 602 | routerRecord.mDiscoveryPreference = discoveryRequest; |
| 603 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 604 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManagers, |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 605 | routerRecord.mUserRecord.mHandler, |
| 606 | routerRecord.mPackageName, |
| 607 | routerRecord.mDiscoveryPreference.getPreferredFeatures())); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 608 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 609 | obtainMessage(UserHandler::updateDiscoveryPreferenceOnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 610 | routerRecord.mUserRecord.mHandler)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 611 | } |
| 612 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 613 | private void setRouteVolumeWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 614 | @NonNull MediaRoute2Info route, int volume) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 615 | final IBinder binder = router.asBinder(); |
| 616 | RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 617 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 618 | if (routerRecord != null) { |
| 619 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 620 | obtainMessage(UserHandler::setRouteVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 621 | routerRecord.mUserRecord.mHandler, |
| 622 | DUMMY_REQUEST_ID, route, volume)); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 626 | private void requestCreateSessionWithRouter2Locked(int requestId, long managerRequestId, |
| 627 | @NonNull IMediaRouter2 router, @NonNull RoutingSessionInfo oldSession, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 628 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 629 | final IBinder binder = router.asBinder(); |
| 630 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 631 | |
| 632 | if (routerRecord == null) { |
| 633 | return; |
| 634 | } |
| 635 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 636 | if (managerRequestId != MediaRoute2ProviderService.REQUEST_ID_NONE) { |
| 637 | ManagerRecord manager = routerRecord.mUserRecord.mHandler.findManagerWithId( |
| 638 | toRequesterId(managerRequestId)); |
| 639 | if (manager == null || manager.mLastSessionCreationRequest == null) { |
| 640 | Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " |
| 641 | + "Ignoring unknown request."); |
| 642 | routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( |
| 643 | routerRecord, requestId); |
| 644 | return; |
| 645 | } |
| 646 | if (!TextUtils.equals(manager.mLastSessionCreationRequest.mOldSession.getId(), |
| 647 | oldSession.getId())) { |
| 648 | Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " |
| 649 | + "Ignoring unmatched routing session."); |
| 650 | routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( |
| 651 | routerRecord, requestId); |
| 652 | return; |
| 653 | } |
| 654 | if (!TextUtils.equals(manager.mLastSessionCreationRequest.mRoute.getId(), |
| 655 | route.getId())) { |
| 656 | // When media router has no permission |
| 657 | if (!routerRecord.mHasModifyAudioRoutingPermission |
| 658 | && manager.mLastSessionCreationRequest.mRoute.isSystemRoute() |
| 659 | && route.isSystemRoute()) { |
| 660 | route = manager.mLastSessionCreationRequest.mRoute; |
| 661 | } else { |
| 662 | Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " |
| 663 | + "Ignoring unmatched route."); |
| 664 | routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( |
| 665 | routerRecord, requestId); |
| 666 | return; |
| 667 | } |
| 668 | } |
| 669 | manager.mLastSessionCreationRequest = null; |
| 670 | } else { |
| 671 | if (route.isSystemRoute() && !routerRecord.mHasModifyAudioRoutingPermission |
| 672 | && !TextUtils.equals(route.getId(), |
| 673 | routerRecord.mUserRecord.mHandler.mSystemProvider.getDefaultRoute().getId())) { |
| 674 | Slog.w(TAG, "MODIFY_AUDIO_ROUTING permission is required to transfer to" |
| 675 | + route); |
| 676 | routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( |
| 677 | routerRecord, requestId); |
| 678 | return; |
| 679 | } |
Hyundo Moon | fa3d0ef | 2020-04-13 00:27:44 +0900 | [diff] [blame] | 680 | } |
| 681 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 682 | long uniqueRequestId = toUniqueRequestId(routerRecord.mRouterId, requestId); |
| 683 | routerRecord.mUserRecord.mHandler.sendMessage( |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 684 | obtainMessage(UserHandler::requestCreateSessionWithRouter2OnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 685 | routerRecord.mUserRecord.mHandler, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 686 | uniqueRequestId, managerRequestId, routerRecord, oldSession, route, |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 687 | sessionHints)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | private void selectRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 691 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 692 | final IBinder binder = router.asBinder(); |
| 693 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 694 | |
| 695 | if (routerRecord == null) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 700 | obtainMessage(UserHandler::selectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 701 | routerRecord.mUserRecord.mHandler, |
| 702 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | private void deselectRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 706 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 707 | final IBinder binder = router.asBinder(); |
| 708 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 709 | |
| 710 | if (routerRecord == null) { |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 715 | obtainMessage(UserHandler::deselectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 716 | routerRecord.mUserRecord.mHandler, |
| 717 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | private void transferToRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 721 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 722 | final IBinder binder = router.asBinder(); |
| 723 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 724 | |
| 725 | if (routerRecord == null) { |
| 726 | return; |
| 727 | } |
| 728 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 729 | String defaultRouteId = |
| 730 | routerRecord.mUserRecord.mHandler.mSystemProvider.getDefaultRoute().getId(); |
| 731 | if (route.isSystemRoute() && !routerRecord.mHasModifyAudioRoutingPermission |
| 732 | && !TextUtils.equals(route.getId(), defaultRouteId)) { |
| 733 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 734 | obtainMessage(UserHandler::notifySessionCreationFailedToRouter, |
| 735 | routerRecord.mUserRecord.mHandler, |
| 736 | routerRecord, toOriginalRequestId(DUMMY_REQUEST_ID))); |
| 737 | } else { |
| 738 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 739 | obtainMessage(UserHandler::transferToRouteOnHandler, |
| 740 | routerRecord.mUserRecord.mHandler, |
| 741 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
| 742 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | private void setSessionVolumeWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 746 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 747 | final IBinder binder = router.asBinder(); |
| 748 | RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 749 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 750 | if (routerRecord == null) { |
| 751 | return; |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 752 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 753 | |
| 754 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 755 | obtainMessage(UserHandler::setSessionVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 756 | routerRecord.mUserRecord.mHandler, |
| 757 | DUMMY_REQUEST_ID, uniqueSessionId, volume)); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 758 | } |
| 759 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 760 | private void releaseSessionWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 761 | @NonNull String uniqueSessionId) { |
| 762 | final IBinder binder = router.asBinder(); |
| 763 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 764 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 765 | if (routerRecord == null) { |
| 766 | return; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 767 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 768 | |
| 769 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 770 | obtainMessage(UserHandler::releaseSessionOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 771 | routerRecord.mUserRecord.mHandler, |
| 772 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId)); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 773 | } |
| 774 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 775 | //////////////////////////////////////////////////////////// |
| 776 | //// ***Locked methods related to MediaRouter2Manager |
| 777 | //// - Should have @NonNull/@Nullable on all arguments |
| 778 | //////////////////////////////////////////////////////////// |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 779 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 780 | private List<RoutingSessionInfo> getActiveSessionsLocked( |
| 781 | @NonNull IMediaRouter2Manager manager) { |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 782 | final IBinder binder = manager.asBinder(); |
| 783 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 784 | |
| 785 | if (managerRecord == null) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 786 | Slog.w(TAG, "getActiveSessionLocked: Ignoring unknown manager"); |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 787 | return Collections.emptyList(); |
| 788 | } |
| 789 | |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 790 | List<RoutingSessionInfo> sessionInfos = new ArrayList<>(); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 791 | for (MediaRoute2Provider provider : managerRecord.mUserRecord.mHandler.mRouteProviders) { |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 792 | sessionInfos.addAll(provider.getSessionInfos()); |
| 793 | } |
| 794 | return sessionInfos; |
| 795 | } |
| 796 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 797 | private void registerManagerLocked(@NonNull IMediaRouter2Manager manager, |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 798 | int uid, int pid, @NonNull String packageName, int userId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 799 | final IBinder binder = manager.asBinder(); |
| 800 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 801 | |
| 802 | if (managerRecord != null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 803 | Slog.w(TAG, "registerManagerLocked: Same manager already exists. packageName=" |
| 804 | + packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 805 | return; |
| 806 | } |
| 807 | |
| 808 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 809 | managerRecord = new ManagerRecord(userRecord, manager, uid, pid, packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 810 | try { |
| 811 | binder.linkToDeath(managerRecord, 0); |
| 812 | } catch (RemoteException ex) { |
| 813 | throw new RuntimeException("Media router manager died prematurely.", ex); |
| 814 | } |
| 815 | |
| 816 | userRecord.mManagerRecords.add(managerRecord); |
| 817 | mAllManagerRecords.put(binder, managerRecord); |
| 818 | |
| 819 | userRecord.mHandler.sendMessage(obtainMessage(UserHandler::notifyRoutesToManager, |
| 820 | userRecord.mHandler, manager)); |
| 821 | |
| 822 | for (RouterRecord routerRecord : userRecord.mRouterRecords) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 823 | // TODO: UserRecord <-> routerRecord, why do they reference each other? |
| 824 | // How about removing mUserRecord from routerRecord? |
| 825 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | 38d06df | 2020-05-20 14:24:06 +0900 | [diff] [blame] | 826 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManager, |
| 827 | routerRecord.mUserRecord.mHandler, routerRecord, manager)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | private void unregisterManagerLocked(@NonNull IMediaRouter2Manager manager, boolean died) { |
| 832 | ManagerRecord managerRecord = mAllManagerRecords.remove(manager.asBinder()); |
| 833 | if (managerRecord == null) { |
| 834 | return; |
| 835 | } |
| 836 | UserRecord userRecord = managerRecord.mUserRecord; |
| 837 | userRecord.mManagerRecords.remove(managerRecord); |
| 838 | managerRecord.dispose(); |
| 839 | disposeUserIfNeededLocked(userRecord); // since manager removed from user |
| 840 | } |
| 841 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 842 | private void setRouteVolumeWithManagerLocked(int requestId, |
| 843 | @NonNull IMediaRouter2Manager manager, |
| 844 | @NonNull MediaRoute2Info route, int volume) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 845 | final IBinder binder = manager.asBinder(); |
| 846 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 847 | |
| 848 | if (managerRecord == null) { |
| 849 | return; |
| 850 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 851 | |
| 852 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 853 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 854 | obtainMessage(UserHandler::setRouteVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 855 | managerRecord.mUserRecord.mHandler, |
| 856 | uniqueRequestId, route, volume)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 857 | } |
| 858 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 859 | private void requestCreateSessionWithManagerLocked(int requestId, |
| 860 | @NonNull IMediaRouter2Manager manager, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 861 | @NonNull RoutingSessionInfo oldSession, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 862 | ManagerRecord managerRecord = mAllManagerRecords.get(manager.asBinder()); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 863 | if (managerRecord == null) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 867 | String packageName = oldSession.getClientPackageName(); |
| 868 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 869 | RouterRecord routerRecord = managerRecord.mUserRecord.findRouterRecordLocked(packageName); |
| 870 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 871 | Slog.w(TAG, "requestCreateSessionWithManagerLocked: Ignoring session creation for " |
| 872 | + "unknown router."); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 873 | try { |
| 874 | managerRecord.mManager.notifyRequestFailed(requestId, REASON_UNKNOWN_ERROR); |
| 875 | } catch (RemoteException ex) { |
| 876 | Slog.w(TAG, "requestCreateSessionWithManagerLocked: Failed to notify failure. " |
| 877 | + "Manager probably died."); |
| 878 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
| 882 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 883 | if (managerRecord.mLastSessionCreationRequest != null) { |
| 884 | managerRecord.mUserRecord.mHandler.notifyRequestFailedToManager( |
| 885 | managerRecord.mManager, |
| 886 | toOriginalRequestId(managerRecord.mLastSessionCreationRequest |
| 887 | .mManagerRequestId), |
| 888 | REASON_UNKNOWN_ERROR); |
| 889 | managerRecord.mLastSessionCreationRequest = null; |
| 890 | } |
| 891 | managerRecord.mLastSessionCreationRequest = new SessionCreationRequest(routerRecord, |
| 892 | MediaRoute2ProviderService.REQUEST_ID_NONE, uniqueRequestId, |
| 893 | oldSession, route); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 894 | |
| 895 | // Before requesting to the provider, get session hints from the media router. |
| 896 | // As a return, media router will request to create a session. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 897 | routerRecord.mUserRecord.mHandler.sendMessage( |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 898 | obtainMessage(UserHandler::requestRouterCreateSessionOnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 899 | routerRecord.mUserRecord.mHandler, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 900 | uniqueRequestId, routerRecord, managerRecord, oldSession, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 901 | } |
| 902 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 903 | private void selectRouteWithManagerLocked(int requestId, @NonNull IMediaRouter2Manager manager, |
| 904 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 905 | final IBinder binder = manager.asBinder(); |
| 906 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 907 | |
| 908 | if (managerRecord == null) { |
| 909 | return; |
| 910 | } |
| 911 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 912 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 913 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 914 | .findRouterWithSessionLocked(uniqueSessionId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 915 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 916 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 917 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 918 | obtainMessage(UserHandler::selectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 919 | managerRecord.mUserRecord.mHandler, |
| 920 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 921 | } |
| 922 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 923 | private void deselectRouteWithManagerLocked(int requestId, |
| 924 | @NonNull IMediaRouter2Manager manager, |
| 925 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 926 | final IBinder binder = manager.asBinder(); |
| 927 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 928 | |
| 929 | if (managerRecord == null) { |
| 930 | return; |
| 931 | } |
| 932 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 933 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 934 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 935 | .findRouterWithSessionLocked(uniqueSessionId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 936 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 937 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 938 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 939 | obtainMessage(UserHandler::deselectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 940 | managerRecord.mUserRecord.mHandler, |
| 941 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 942 | } |
| 943 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 944 | private void transferToRouteWithManagerLocked(int requestId, |
| 945 | @NonNull IMediaRouter2Manager manager, |
| 946 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 947 | final IBinder binder = manager.asBinder(); |
| 948 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 949 | |
| 950 | if (managerRecord == null) { |
| 951 | return; |
| 952 | } |
| 953 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 954 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 955 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 956 | .findRouterWithSessionLocked(uniqueSessionId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 957 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 958 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 959 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 960 | obtainMessage(UserHandler::transferToRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 961 | managerRecord.mUserRecord.mHandler, |
| 962 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 963 | } |
| 964 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 965 | private void setSessionVolumeWithManagerLocked(int requestId, |
| 966 | @NonNull IMediaRouter2Manager manager, |
| 967 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 968 | final IBinder binder = manager.asBinder(); |
| 969 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 970 | |
| 971 | if (managerRecord == null) { |
| 972 | return; |
| 973 | } |
| 974 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 975 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 976 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 977 | obtainMessage(UserHandler::setSessionVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 978 | managerRecord.mUserRecord.mHandler, |
| 979 | uniqueRequestId, uniqueSessionId, volume)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 980 | } |
| 981 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 982 | private void releaseSessionWithManagerLocked(int requestId, |
| 983 | @NonNull IMediaRouter2Manager manager, |
| 984 | @NonNull String uniqueSessionId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 985 | final IBinder binder = manager.asBinder(); |
| 986 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 987 | |
| 988 | if (managerRecord == null) { |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 993 | .findRouterWithSessionLocked(uniqueSessionId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 994 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 995 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 996 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 997 | obtainMessage(UserHandler::releaseSessionOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 998 | managerRecord.mUserRecord.mHandler, |
| 999 | uniqueRequestId, routerRecord, uniqueSessionId)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | //////////////////////////////////////////////////////////// |
| 1003 | //// ***Locked methods used by both router2 and manager |
| 1004 | //// - Should have @NonNull/@Nullable on all arguments |
| 1005 | //////////////////////////////////////////////////////////// |
| 1006 | |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 1007 | private UserRecord getOrCreateUserRecordLocked(int userId) { |
| 1008 | UserRecord userRecord = mUserRecords.get(userId); |
| 1009 | if (userRecord == null) { |
| 1010 | userRecord = new UserRecord(userId); |
| 1011 | mUserRecords.put(userId, userRecord); |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1012 | userRecord.init(); |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 1013 | if (userId == mCurrentUserId) { |
| 1014 | userRecord.mHandler.sendMessage( |
| 1015 | obtainMessage(UserHandler::start, userRecord.mHandler)); |
| 1016 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1017 | } |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 1018 | return userRecord; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1019 | } |
| 1020 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1021 | private void disposeUserIfNeededLocked(@NonNull UserRecord userRecord) { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1022 | // If there are no records left and the user is no longer current then go ahead |
| 1023 | // and purge the user record and all of its associated state. If the user is current |
| 1024 | // then leave it alone since we might be connected to a route or want to query |
| 1025 | // the same route information again soon. |
| 1026 | if (userRecord.mUserId != mCurrentUserId |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1027 | && userRecord.mRouterRecords.isEmpty() |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1028 | && userRecord.mManagerRecords.isEmpty()) { |
| 1029 | if (DEBUG) { |
| 1030 | Slog.d(TAG, userRecord + ": Disposed"); |
| 1031 | } |
| 1032 | mUserRecords.remove(userRecord.mUserId); |
| 1033 | // Note: User already stopped (by switchUser) so no need to send stop message here. |
| 1034 | } |
| 1035 | } |
| 1036 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1037 | static long toUniqueRequestId(int requesterId, int originalRequestId) { |
| 1038 | return ((long) requesterId << 32) | originalRequestId; |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1039 | } |
| 1040 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1041 | static int toRequesterId(long uniqueRequestId) { |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1042 | return (int) (uniqueRequestId >> 32); |
| 1043 | } |
| 1044 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1045 | static int toOriginalRequestId(long uniqueRequestId) { |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1046 | return (int) uniqueRequestId; |
| 1047 | } |
| 1048 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1049 | final class UserRecord { |
| 1050 | public final int mUserId; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1051 | //TODO: make records private for thread-safety |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1052 | final ArrayList<RouterRecord> mRouterRecords = new ArrayList<>(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1053 | final ArrayList<ManagerRecord> mManagerRecords = new ArrayList<>(); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 1054 | RouteDiscoveryPreference mCompositeDiscoveryPreference = RouteDiscoveryPreference.EMPTY; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1055 | final UserHandler mHandler; |
| 1056 | |
| 1057 | UserRecord(int userId) { |
| 1058 | mUserId = userId; |
| 1059 | mHandler = new UserHandler(MediaRouter2ServiceImpl.this, this); |
| 1060 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1061 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1062 | void init() { |
| 1063 | mHandler.init(); |
| 1064 | } |
| 1065 | |
Hyundo Moon | 0f78fc6 | 2020-06-01 14:50:32 +0900 | [diff] [blame] | 1066 | // TODO: This assumes that only one router exists in a package. |
| 1067 | // Do this in Android S or later. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1068 | RouterRecord findRouterRecordLocked(String packageName) { |
| 1069 | for (RouterRecord routerRecord : mRouterRecords) { |
| 1070 | if (TextUtils.equals(routerRecord.mPackageName, packageName)) { |
| 1071 | return routerRecord; |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 1072 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1073 | } |
| 1074 | return null; |
| 1075 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1076 | } |
| 1077 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1078 | final class RouterRecord implements IBinder.DeathRecipient { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1079 | public final UserRecord mUserRecord; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1080 | public final String mPackageName; |
Hyundo Moon | 4140fee | 2019-11-08 14:47:50 +0900 | [diff] [blame] | 1081 | public final List<Integer> mSelectRouteSequenceNumbers; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1082 | public final IMediaRouter2 mRouter; |
Kyunglyul Hyun | 8657671 | 2019-11-21 18:31:46 +0900 | [diff] [blame] | 1083 | public final int mUid; |
| 1084 | public final int mPid; |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1085 | public final boolean mHasConfigureWifiDisplayPermission; |
| 1086 | public final boolean mHasModifyAudioRoutingPermission; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1087 | public final int mRouterId; |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 1088 | |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1089 | public RouteDiscoveryPreference mDiscoveryPreference; |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1090 | public MediaRoute2Info mSelectedRoute; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1091 | |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1092 | RouterRecord(UserRecord userRecord, IMediaRouter2 router, int uid, int pid, |
| 1093 | String packageName, boolean hasConfigureWifiDisplayPermission, |
| 1094 | boolean hasModifyAudioRoutingPermission) { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1095 | mUserRecord = userRecord; |
Kyunglyul Hyun | 2f43fd6 | 2019-09-16 18:47:52 +0900 | [diff] [blame] | 1096 | mPackageName = packageName; |
Hyundo Moon | 4140fee | 2019-11-08 14:47:50 +0900 | [diff] [blame] | 1097 | mSelectRouteSequenceNumbers = new ArrayList<>(); |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1098 | mDiscoveryPreference = RouteDiscoveryPreference.EMPTY; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1099 | mRouter = router; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1100 | mUid = uid; |
| 1101 | mPid = pid; |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1102 | mHasConfigureWifiDisplayPermission = hasConfigureWifiDisplayPermission; |
| 1103 | mHasModifyAudioRoutingPermission = hasModifyAudioRoutingPermission; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1104 | mRouterId = mNextRouterOrManagerId.getAndIncrement(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | public void dispose() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1108 | mRouter.asBinder().unlinkToDeath(this, 0); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | @Override |
| 1112 | public void binderDied() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1113 | routerDied(this); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1114 | } |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | final class ManagerRecord implements IBinder.DeathRecipient { |
| 1118 | public final UserRecord mUserRecord; |
| 1119 | public final IMediaRouter2Manager mManager; |
| 1120 | public final int mUid; |
| 1121 | public final int mPid; |
| 1122 | public final String mPackageName; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1123 | public final int mManagerId; |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1124 | public SessionCreationRequest mLastSessionCreationRequest; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1125 | |
| 1126 | ManagerRecord(UserRecord userRecord, IMediaRouter2Manager manager, |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1127 | int uid, int pid, String packageName) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1128 | mUserRecord = userRecord; |
| 1129 | mManager = manager; |
| 1130 | mUid = uid; |
| 1131 | mPid = pid; |
| 1132 | mPackageName = packageName; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1133 | mManagerId = mNextRouterOrManagerId.getAndIncrement(); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | public void dispose() { |
| 1137 | mManager.asBinder().unlinkToDeath(this, 0); |
| 1138 | } |
| 1139 | |
| 1140 | @Override |
| 1141 | public void binderDied() { |
| 1142 | managerDied(this); |
| 1143 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1144 | |
| 1145 | public void dump(PrintWriter pw, String prefix) { |
| 1146 | pw.println(prefix + this); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | @Override |
| 1150 | public String toString() { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1151 | return "Manager " + mPackageName + " (pid " + mPid + ")"; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | static final class UserHandler extends Handler implements |
| 1156 | MediaRoute2ProviderWatcher.Callback, |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1157 | MediaRoute2Provider.Callback { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1158 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1159 | private final WeakReference<MediaRouter2ServiceImpl> mServiceRef; |
| 1160 | private final UserRecord mUserRecord; |
| 1161 | private final MediaRoute2ProviderWatcher mWatcher; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1162 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1163 | private final SystemMediaRoute2Provider mSystemProvider; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1164 | private final ArrayList<MediaRoute2Provider> mRouteProviders = |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1165 | new ArrayList<>(); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1166 | |
| 1167 | private final List<MediaRoute2ProviderInfo> mLastProviderInfos = new ArrayList<>(); |
| 1168 | private final CopyOnWriteArrayList<SessionCreationRequest> mSessionCreationRequests = |
| 1169 | new CopyOnWriteArrayList<>(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1170 | private final Map<String, RouterRecord> mSessionToRouterMap = new ArrayMap<>(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1171 | |
| 1172 | private boolean mRunning; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1173 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1174 | // TODO: (In Android S+) Pull out SystemMediaRoute2Provider out of UserHandler. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1175 | UserHandler(@NonNull MediaRouter2ServiceImpl service, @NonNull UserRecord userRecord) { |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 1176 | super(Looper.getMainLooper(), null, true); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1177 | mServiceRef = new WeakReference<>(service); |
| 1178 | mUserRecord = userRecord; |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1179 | mSystemProvider = new SystemMediaRoute2Provider(service.mContext); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1180 | mRouteProviders.add(mSystemProvider); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1181 | mWatcher = new MediaRoute2ProviderWatcher(service.mContext, this, |
| 1182 | this, mUserRecord.mUserId); |
| 1183 | } |
| 1184 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1185 | void init() { |
| 1186 | mSystemProvider.setCallback(this); |
| 1187 | } |
| 1188 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1189 | private void start() { |
| 1190 | if (!mRunning) { |
| 1191 | mRunning = true; |
| 1192 | mWatcher.start(); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | private void stop() { |
| 1197 | if (mRunning) { |
| 1198 | mRunning = false; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1199 | mWatcher.stop(); // also stops all providers |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1204 | public void onAddProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1205 | proxy.setCallback(this); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1206 | mRouteProviders.add(proxy); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1207 | proxy.updateDiscoveryPreference(mUserRecord.mCompositeDiscoveryPreference); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1211 | public void onRemoveProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1212 | mRouteProviders.remove(proxy); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | @Override |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1216 | public void onProviderStateChanged(@NonNull MediaRoute2Provider provider) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1217 | sendMessage(PooledLambda.obtainMessage(UserHandler::onProviderStateChangedOnHandler, |
| 1218 | this, provider)); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1219 | } |
| 1220 | |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 1221 | @Override |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1222 | public void onSessionCreated(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1223 | long uniqueRequestId, @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1224 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionCreatedOnHandler, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1225 | this, provider, uniqueRequestId, sessionInfo)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1226 | } |
| 1227 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 1228 | @Override |
| 1229 | public void onSessionUpdated(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1230 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1231 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionInfoChangedOnHandler, |
| 1232 | this, provider, sessionInfo)); |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1236 | public void onSessionReleased(@NonNull MediaRoute2Provider provider, |
| 1237 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1238 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionReleasedOnHandler, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1239 | this, provider, sessionInfo)); |
| 1240 | } |
| 1241 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1242 | @Override |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1243 | public void onRequestFailed(@NonNull MediaRoute2Provider provider, long uniqueRequestId, |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1244 | int reason) { |
| 1245 | sendMessage(PooledLambda.obtainMessage(UserHandler::onRequestFailedOnHandler, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1246 | this, provider, uniqueRequestId, reason)); |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1247 | } |
| 1248 | |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1249 | @Nullable |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1250 | public RouterRecord findRouterWithSessionLocked(@NonNull String uniqueSessionId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1251 | return mSessionToRouterMap.get(uniqueSessionId); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1254 | @Nullable |
| 1255 | public ManagerRecord findManagerWithId(int managerId) { |
| 1256 | for (ManagerRecord manager : getManagerRecords()) { |
| 1257 | if (manager.mManagerId == managerId) { |
| 1258 | return manager; |
| 1259 | } |
| 1260 | } |
| 1261 | return null; |
| 1262 | } |
| 1263 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1264 | private void onProviderStateChangedOnHandler(@NonNull MediaRoute2Provider provider) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1265 | int providerInfoIndex = getLastProviderInfoIndex(provider.getUniqueId()); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1266 | MediaRoute2ProviderInfo currentInfo = provider.getProviderInfo(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1267 | MediaRoute2ProviderInfo prevInfo = |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1268 | (providerInfoIndex < 0) ? null : mLastProviderInfos.get(providerInfoIndex); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1269 | if (Objects.equals(prevInfo, currentInfo)) return; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1270 | |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1271 | List<MediaRoute2Info> addedRoutes = new ArrayList<>(); |
| 1272 | List<MediaRoute2Info> removedRoutes = new ArrayList<>(); |
| 1273 | List<MediaRoute2Info> changedRoutes = new ArrayList<>(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1274 | if (prevInfo == null) { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1275 | mLastProviderInfos.add(currentInfo); |
| 1276 | addedRoutes.addAll(currentInfo.getRoutes()); |
| 1277 | } else if (currentInfo == null) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1278 | mLastProviderInfos.remove(prevInfo); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1279 | removedRoutes.addAll(prevInfo.getRoutes()); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1280 | } else { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1281 | mLastProviderInfos.set(providerInfoIndex, currentInfo); |
| 1282 | final Collection<MediaRoute2Info> prevRoutes = prevInfo.getRoutes(); |
| 1283 | final Collection<MediaRoute2Info> currentRoutes = currentInfo.getRoutes(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1284 | |
| 1285 | for (MediaRoute2Info route : currentRoutes) { |
| 1286 | if (!route.isValid()) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1287 | Slog.w(TAG, "onProviderStateChangedOnHandler: Ignoring invalid route : " |
| 1288 | + route); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1289 | continue; |
| 1290 | } |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1291 | MediaRoute2Info prevRoute = prevInfo.getRoute(route.getOriginalId()); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1292 | if (prevRoute == null) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1293 | addedRoutes.add(route); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1294 | } else if (!Objects.equals(prevRoute, route)) { |
| 1295 | changedRoutes.add(route); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | for (MediaRoute2Info prevRoute : prevInfo.getRoutes()) { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1300 | if (currentInfo.getRoute(prevRoute.getOriginalId()) == null) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1301 | removedRoutes.add(prevRoute); |
| 1302 | } |
| 1303 | } |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1304 | } |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1305 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1306 | List<IMediaRouter2> routersWithModifyAudioRoutingPermission = getRouters(true); |
| 1307 | List<IMediaRouter2> routersWithoutModifyAudioRoutingPermission = getRouters(false); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1308 | List<IMediaRouter2Manager> managers = getManagers(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1309 | List<MediaRoute2Info> defaultRoute = new ArrayList<>(); |
| 1310 | defaultRoute.add(mSystemProvider.getDefaultRoute()); |
| 1311 | |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1312 | if (addedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1313 | notifyRoutesAddedToRouters(routersWithModifyAudioRoutingPermission, addedRoutes); |
| 1314 | if (!provider.mIsSystemRouteProvider) { |
| 1315 | notifyRoutesAddedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1316 | addedRoutes); |
| 1317 | } else if (prevInfo == null) { |
| 1318 | notifyRoutesAddedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1319 | defaultRoute); |
| 1320 | } // 'else' is handled as changed routes |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1321 | notifyRoutesAddedToManagers(managers, addedRoutes); |
| 1322 | } |
| 1323 | if (removedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1324 | notifyRoutesRemovedToRouters(routersWithModifyAudioRoutingPermission, |
| 1325 | removedRoutes); |
| 1326 | if (!provider.mIsSystemRouteProvider) { |
| 1327 | notifyRoutesRemovedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1328 | removedRoutes); |
| 1329 | } |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1330 | notifyRoutesRemovedToManagers(managers, removedRoutes); |
| 1331 | } |
| 1332 | if (changedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1333 | notifyRoutesChangedToRouters(routersWithModifyAudioRoutingPermission, |
| 1334 | changedRoutes); |
| 1335 | if (!provider.mIsSystemRouteProvider) { |
| 1336 | notifyRoutesChangedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1337 | changedRoutes); |
| 1338 | } else if (prevInfo != null) { |
| 1339 | notifyRoutesChangedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1340 | defaultRoute); |
| 1341 | } // 'else' is handled as added routes |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1342 | notifyRoutesChangedToManagers(managers, changedRoutes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1343 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1344 | } |
| 1345 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1346 | private int getLastProviderInfoIndex(@NonNull String providerId) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1347 | for (int i = 0; i < mLastProviderInfos.size(); i++) { |
| 1348 | MediaRoute2ProviderInfo providerInfo = mLastProviderInfos.get(i); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1349 | if (TextUtils.equals(providerInfo.getUniqueId(), providerId)) { |
| 1350 | return i; |
| 1351 | } |
| 1352 | } |
| 1353 | return -1; |
| 1354 | } |
| 1355 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1356 | private void requestRouterCreateSessionOnHandler(long uniqueRequestId, |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1357 | @NonNull RouterRecord routerRecord, @NonNull ManagerRecord managerRecord, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1358 | @NonNull RoutingSessionInfo oldSession, @NonNull MediaRoute2Info route) { |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1359 | try { |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1360 | if (route.isSystemRoute() && !routerRecord.mHasModifyAudioRoutingPermission) { |
| 1361 | routerRecord.mRouter.requestCreateSessionByManager(uniqueRequestId, |
| 1362 | oldSession, mSystemProvider.getDefaultRoute()); |
| 1363 | } else { |
| 1364 | routerRecord.mRouter.requestCreateSessionByManager(uniqueRequestId, |
| 1365 | oldSession, route); |
| 1366 | } |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1367 | } catch (RemoteException ex) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1368 | Slog.w(TAG, "getSessionHintsForCreatingSessionOnHandler: " |
| 1369 | + "Failed to request. Router probably died.", ex); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1370 | notifyRequestFailedToManager(managerRecord.mManager, |
| 1371 | toOriginalRequestId(uniqueRequestId), REASON_UNKNOWN_ERROR); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | private void requestCreateSessionWithRouter2OnHandler(long uniqueRequestId, |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1376 | long managerRequestId, @NonNull RouterRecord routerRecord, |
| 1377 | @NonNull RoutingSessionInfo oldSession, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1378 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1379 | |
| 1380 | final MediaRoute2Provider provider = findProvider(route.getProviderId()); |
| 1381 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1382 | Slog.w(TAG, "requestCreateSessionWithRouter2OnHandler: Ignoring session " |
| 1383 | + "creation request since no provider found for given route=" + route); |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1384 | notifySessionCreationFailedToRouter(routerRecord, |
| 1385 | toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1386 | return; |
| 1387 | } |
| 1388 | |
Hyundo Moon | dc79a7a | 2020-01-16 15:18:33 +0900 | [diff] [blame] | 1389 | SessionCreationRequest request = |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1390 | new SessionCreationRequest(routerRecord, uniqueRequestId, |
| 1391 | managerRequestId, oldSession, route); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1392 | mSessionCreationRequests.add(request); |
| 1393 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1394 | provider.requestCreateSession(uniqueRequestId, routerRecord.mPackageName, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1395 | route.getOriginalId(), sessionHints); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1396 | } |
| 1397 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1398 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1399 | private void selectRouteOnHandler(long uniqueRequestId, @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1400 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1401 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1402 | "selecting")) { |
| 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | final String providerId = route.getProviderId(); |
| 1407 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1408 | if (provider == null) { |
| 1409 | return; |
| 1410 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1411 | provider.selectRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
| 1412 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1413 | } |
| 1414 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1415 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1416 | private void deselectRouteOnHandler(long uniqueRequestId, |
| 1417 | @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1418 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1419 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1420 | "deselecting")) { |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | final String providerId = route.getProviderId(); |
| 1425 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1426 | if (provider == null) { |
| 1427 | return; |
| 1428 | } |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1429 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1430 | provider.deselectRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1431 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1432 | } |
| 1433 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1434 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1435 | private void transferToRouteOnHandler(long uniqueRequestId, |
| 1436 | @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1437 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1438 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1439 | "transferring to")) { |
| 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | final String providerId = route.getProviderId(); |
| 1444 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1445 | if (provider == null) { |
| 1446 | return; |
| 1447 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1448 | provider.transferToRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1449 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1450 | } |
| 1451 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1452 | // routerRecord is null if and only if the session is created without the request, which |
| 1453 | // includes the system's session and RCN cases. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1454 | private boolean checkArgumentsForSessionControl(@Nullable RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1455 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route, |
| 1456 | @NonNull String description) { |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1457 | final String providerId = route.getProviderId(); |
| 1458 | final MediaRoute2Provider provider = findProvider(providerId); |
| 1459 | if (provider == null) { |
| 1460 | Slog.w(TAG, "Ignoring " + description + " route since no provider found for " |
| 1461 | + "given route=" + route); |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1465 | // Bypass checking router if it's the system session (routerRecord should be null) |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 1466 | if (TextUtils.equals(getProviderId(uniqueSessionId), mSystemProvider.getUniqueId())) { |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1470 | RouterRecord matchingRecord = mSessionToRouterMap.get(uniqueSessionId); |
| 1471 | if (matchingRecord != routerRecord) { |
| 1472 | Slog.w(TAG, "Ignoring " + description + " route from non-matching router. " |
| 1473 | + "packageName=" + routerRecord.mPackageName + " route=" + route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1474 | return false; |
| 1475 | } |
| 1476 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1477 | final String sessionId = getOriginalId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1478 | if (sessionId == null) { |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1479 | Slog.w(TAG, "Failed to get original session id from unique session id. " |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1480 | + "uniqueSessionId=" + uniqueSessionId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1487 | private void setRouteVolumeOnHandler(long uniqueRequestId, @NonNull MediaRoute2Info route, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1488 | int volume) { |
| 1489 | final MediaRoute2Provider provider = findProvider(route.getProviderId()); |
| 1490 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1491 | Slog.w(TAG, "setRouteVolumeOnHandler: Couldn't find provider for route=" + route); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1492 | return; |
| 1493 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1494 | provider.setRouteVolume(uniqueRequestId, route.getOriginalId(), volume); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1495 | } |
| 1496 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1497 | private void setSessionVolumeOnHandler(long uniqueRequestId, |
| 1498 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1499 | final MediaRoute2Provider provider = findProvider(getProviderId(uniqueSessionId)); |
| 1500 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1501 | Slog.w(TAG, "setSessionVolumeOnHandler: Couldn't find provider for session id=" |
| 1502 | + uniqueSessionId); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1503 | return; |
| 1504 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1505 | provider.setSessionVolume(uniqueRequestId, getOriginalId(uniqueSessionId), volume); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | private void releaseSessionOnHandler(long uniqueRequestId, |
Hyundo Moon | 1f06668 | 2020-04-13 04:04:15 +0900 | [diff] [blame] | 1509 | @Nullable RouterRecord routerRecord, @NonNull String uniqueSessionId) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1510 | final RouterRecord matchingRecord = mSessionToRouterMap.get(uniqueSessionId); |
| 1511 | if (matchingRecord != routerRecord) { |
Hyundo Moon | 1f06668 | 2020-04-13 04:04:15 +0900 | [diff] [blame] | 1512 | Slog.w(TAG, "Ignoring releasing session from non-matching router. packageName=" |
| 1513 | + (routerRecord == null ? null : routerRecord.mPackageName) |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1514 | + " uniqueSessionId=" + uniqueSessionId); |
| 1515 | return; |
| 1516 | } |
| 1517 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1518 | final String providerId = getProviderId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1519 | if (providerId == null) { |
| 1520 | Slog.w(TAG, "Ignoring releasing session with invalid unique session ID. " |
| 1521 | + "uniqueSessionId=" + uniqueSessionId); |
| 1522 | return; |
| 1523 | } |
| 1524 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1525 | final String sessionId = getOriginalId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1526 | if (sessionId == null) { |
| 1527 | Slog.w(TAG, "Ignoring releasing session with invalid unique session ID. " |
| 1528 | + "uniqueSessionId=" + uniqueSessionId + " providerId=" + providerId); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | final MediaRoute2Provider provider = findProvider(providerId); |
| 1533 | if (provider == null) { |
| 1534 | Slog.w(TAG, "Ignoring releasing session since no provider found for given " |
| 1535 | + "providerId=" + providerId); |
| 1536 | return; |
| 1537 | } |
| 1538 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1539 | provider.releaseSession(uniqueRequestId, sessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | private void onSessionCreatedOnHandler(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1543 | long uniqueRequestId, @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1544 | SessionCreationRequest matchingRequest = null; |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 1545 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1546 | for (SessionCreationRequest request : mSessionCreationRequests) { |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1547 | if (request.mUniqueRequestId == uniqueRequestId |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1548 | && TextUtils.equals( |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1549 | request.mRoute.getProviderId(), provider.getUniqueId())) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1550 | matchingRequest = request; |
| 1551 | break; |
| 1552 | } |
| 1553 | } |
| 1554 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1555 | long managerRequestId = (matchingRequest == null) |
| 1556 | ? MediaRoute2ProviderService.REQUEST_ID_NONE |
| 1557 | : matchingRequest.mManagerRequestId; |
| 1558 | // Managers should know created session even if it's not requested. |
| 1559 | notifySessionCreatedToManagers(managerRequestId, sessionInfo); |
| 1560 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1561 | if (matchingRequest == null) { |
| 1562 | Slog.w(TAG, "Ignoring session creation result for unknown request. " |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1563 | + "uniqueRequestId=" + uniqueRequestId + ", sessionInfo=" + sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 1567 | mSessionCreationRequests.remove(matchingRequest); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1568 | // Not to show old session |
| 1569 | MediaRoute2Provider oldProvider = |
| 1570 | findProvider(matchingRequest.mOldSession.getProviderId()); |
| 1571 | if (oldProvider != null) { |
| 1572 | oldProvider.prepareReleaseSession(matchingRequest.mOldSession.getId()); |
| 1573 | } else { |
| 1574 | Slog.w(TAG, "onSessionCreatedOnHandler: Can't find provider for an old session. " |
| 1575 | + "session=" + matchingRequest.mOldSession); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | String originalRouteId = matchingRequest.mRoute.getId(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1579 | RouterRecord routerRecord = matchingRequest.mRouterRecord; |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1580 | |
Hyundo Moon | dc79a7a | 2020-01-16 15:18:33 +0900 | [diff] [blame] | 1581 | if (!sessionInfo.getSelectedRoutes().contains(originalRouteId)) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1582 | Slog.w(TAG, "Created session doesn't match the original request." |
| 1583 | + " originalRouteId=" + originalRouteId |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1584 | + ", uniqueRequestId=" + uniqueRequestId + ", sessionInfo=" + sessionInfo); |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1585 | notifySessionCreationFailedToRouter(matchingRequest.mRouterRecord, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1586 | toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | // Succeeded |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 1591 | if (sessionInfo.isSystemSession() |
| 1592 | && !matchingRequest.mRouterRecord.mHasModifyAudioRoutingPermission) { |
| 1593 | notifySessionCreatedToRouter(matchingRequest.mRouterRecord, |
| 1594 | toOriginalRequestId(uniqueRequestId), |
| 1595 | mSystemProvider.getDefaultSessionInfo()); |
| 1596 | } else { |
| 1597 | notifySessionCreatedToRouter(matchingRequest.mRouterRecord, |
| 1598 | toOriginalRequestId(uniqueRequestId), sessionInfo); |
| 1599 | } |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1600 | mSessionToRouterMap.put(sessionInfo.getId(), routerRecord); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1601 | } |
| 1602 | |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1603 | private void onSessionInfoChangedOnHandler(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1604 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1605 | List<IMediaRouter2Manager> managers = getManagers(); |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1606 | notifySessionUpdatedToManagers(managers, sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1607 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1608 | // For system provider, notify all routers. |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1609 | if (provider == mSystemProvider) { |
| 1610 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1611 | if (service == null) { |
| 1612 | return; |
| 1613 | } |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1614 | notifySessionInfoChangedToRouters(getRouters(true), sessionInfo); |
| 1615 | notifySessionInfoChangedToRouters(getRouters(false), |
| 1616 | mSystemProvider.getDefaultSessionInfo()); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1617 | return; |
| 1618 | } |
| 1619 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1620 | RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId()); |
| 1621 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1622 | Slog.w(TAG, "onSessionInfoChangedOnHandler: No matching router found for session=" |
| 1623 | + sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1624 | return; |
| 1625 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1626 | notifySessionInfoChangedToRouter(routerRecord, sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1627 | } |
| 1628 | |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1629 | private void onSessionReleasedOnHandler(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1630 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1631 | List<IMediaRouter2Manager> managers = getManagers(); |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1632 | notifySessionReleasedToManagers(managers, sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1633 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1634 | RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId()); |
| 1635 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1636 | Slog.w(TAG, "onSessionReleasedOnHandler: No matching router found for session=" |
| 1637 | + sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1638 | return; |
| 1639 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1640 | notifySessionReleasedToRouter(routerRecord, sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1641 | } |
| 1642 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1643 | private void onRequestFailedOnHandler(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1644 | long uniqueRequestId, int reason) { |
| 1645 | if (handleSessionCreationRequestFailed(provider, uniqueRequestId, reason)) { |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1646 | return; |
| 1647 | } |
| 1648 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1649 | final int requesterId = toRequesterId(uniqueRequestId); |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1650 | ManagerRecord manager = findManagerWithId(requesterId); |
| 1651 | if (manager != null) { |
| 1652 | notifyRequestFailedToManager( |
| 1653 | manager.mManager, toOriginalRequestId(uniqueRequestId), reason); |
| 1654 | return; |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1655 | } |
| 1656 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1657 | // Currently, only the manager can get notified of failures. |
| 1658 | // TODO: Notify router too when the related callback is introduced. |
| 1659 | } |
| 1660 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1661 | private boolean handleSessionCreationRequestFailed(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1662 | long uniqueRequestId, int reason) { |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1663 | // Check whether the failure is about creating a session |
| 1664 | SessionCreationRequest matchingRequest = null; |
| 1665 | for (SessionCreationRequest request : mSessionCreationRequests) { |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1666 | if (request.mUniqueRequestId == uniqueRequestId && TextUtils.equals( |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1667 | request.mRoute.getProviderId(), provider.getUniqueId())) { |
| 1668 | matchingRequest = request; |
| 1669 | break; |
| 1670 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1671 | } |
| 1672 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1673 | if (matchingRequest == null) { |
| 1674 | // The failure is not about creating a session. |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
| 1678 | mSessionCreationRequests.remove(matchingRequest); |
| 1679 | |
| 1680 | // Notify the requester about the failure. |
| 1681 | // The call should be made by either MediaRouter2 or MediaRouter2Manager. |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1682 | if (matchingRequest.mManagerRequestId == MediaRouter2Manager.REQUEST_ID_NONE) { |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1683 | notifySessionCreationFailedToRouter( |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1684 | matchingRequest.mRouterRecord, toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1685 | } else { |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1686 | final int requesterId = toRequesterId(matchingRequest.mManagerRequestId); |
| 1687 | ManagerRecord manager = findManagerWithId(requesterId); |
| 1688 | if (manager != null) { |
| 1689 | notifyRequestFailedToManager(manager.mManager, |
| 1690 | toOriginalRequestId(matchingRequest.mManagerRequestId), reason); |
| 1691 | } |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1692 | } |
| 1693 | return true; |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | private void notifySessionCreatedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1697 | int requestId, @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1698 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1699 | routerRecord.mRouter.notifySessionCreated(requestId, sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1700 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1701 | Slog.w(TAG, "Failed to notify router of the session creation." |
| 1702 | + " Router probably died.", ex); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1706 | private void notifySessionCreationFailedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1707 | int requestId) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1708 | try { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1709 | routerRecord.mRouter.notifySessionCreated(requestId, |
| 1710 | /* sessionInfo= */ null); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1711 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1712 | Slog.w(TAG, "Failed to notify router of the session creation failure." |
| 1713 | + " Router probably died.", ex); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1717 | private void notifySessionInfoChangedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1718 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1719 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1720 | routerRecord.mRouter.notifySessionInfoChanged(sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1721 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1722 | Slog.w(TAG, "Failed to notify router of the session info change." |
| 1723 | + " Router probably died.", ex); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1727 | private void notifySessionReleasedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1728 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1729 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1730 | routerRecord.mRouter.notifySessionReleased(sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1731 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1732 | Slog.w(TAG, "Failed to notify router of the session release." |
| 1733 | + " Router probably died.", ex); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1737 | private List<IMediaRouter2> getAllRouters() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1738 | final List<IMediaRouter2> routers = new ArrayList<>(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1739 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1740 | if (service == null) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1741 | return routers; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1742 | } |
| 1743 | synchronized (service.mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1744 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 1745 | routers.add(routerRecord.mRouter); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1746 | } |
| 1747 | } |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1748 | return routers; |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1749 | } |
| 1750 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1751 | private List<IMediaRouter2> getRouters(boolean hasModifyAudioRoutingPermission) { |
| 1752 | final List<IMediaRouter2> routers = new ArrayList<>(); |
| 1753 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1754 | if (service == null) { |
| 1755 | return routers; |
| 1756 | } |
| 1757 | synchronized (service.mLock) { |
| 1758 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 1759 | if (hasModifyAudioRoutingPermission |
| 1760 | == routerRecord.mHasModifyAudioRoutingPermission) { |
| 1761 | routers.add(routerRecord.mRouter); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | return routers; |
| 1766 | } |
| 1767 | |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1768 | private List<IMediaRouter2Manager> getManagers() { |
| 1769 | final List<IMediaRouter2Manager> managers = new ArrayList<>(); |
| 1770 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1771 | if (service == null) { |
| 1772 | return managers; |
| 1773 | } |
| 1774 | synchronized (service.mLock) { |
| 1775 | for (ManagerRecord managerRecord : mUserRecord.mManagerRecords) { |
| 1776 | managers.add(managerRecord.mManager); |
| 1777 | } |
| 1778 | } |
| 1779 | return managers; |
| 1780 | } |
| 1781 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1782 | private List<ManagerRecord> getManagerRecords() { |
| 1783 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1784 | if (service == null) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1785 | return Collections.emptyList(); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1786 | } |
| 1787 | synchronized (service.mLock) { |
| 1788 | return new ArrayList<>(mUserRecord.mManagerRecords); |
| 1789 | } |
| 1790 | } |
| 1791 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1792 | private void notifyRouterRegistered(@NonNull RouterRecord routerRecord) { |
| 1793 | List<MediaRoute2Info> currentRoutes = new ArrayList<>(); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1794 | |
| 1795 | MediaRoute2ProviderInfo systemProviderInfo = null; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1796 | for (MediaRoute2ProviderInfo providerInfo : mLastProviderInfos) { |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1797 | // TODO: Create MediaRoute2ProviderInfo#isSystemProvider() |
| 1798 | if (TextUtils.equals(providerInfo.getUniqueId(), mSystemProvider.getUniqueId())) { |
| 1799 | // Adding routes from system provider will be handled below, so skip it here. |
| 1800 | systemProviderInfo = providerInfo; |
| 1801 | continue; |
| 1802 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1803 | currentRoutes.addAll(providerInfo.getRoutes()); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1804 | } |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1805 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1806 | RoutingSessionInfo currentSystemSessionInfo; |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1807 | if (routerRecord.mHasModifyAudioRoutingPermission) { |
| 1808 | if (systemProviderInfo != null) { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1809 | currentRoutes.addAll(systemProviderInfo.getRoutes()); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1810 | } else { |
| 1811 | // This shouldn't happen. |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1812 | Slog.wtf(TAG, "System route provider not found."); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1813 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1814 | currentSystemSessionInfo = mSystemProvider.getSessionInfos().get(0); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1815 | } else { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1816 | currentRoutes.add(mSystemProvider.getDefaultRoute()); |
| 1817 | currentSystemSessionInfo = mSystemProvider.getDefaultSessionInfo(); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1818 | } |
| 1819 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1820 | if (currentRoutes.size() == 0) { |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1823 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1824 | try { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1825 | routerRecord.mRouter.notifyRouterRegistered( |
| 1826 | currentRoutes, currentSystemSessionInfo); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1827 | } catch (RemoteException ex) { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1828 | Slog.w(TAG, "Failed to notify router registered. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1832 | private void notifyRoutesAddedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1833 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1834 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1835 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1836 | router.notifyRoutesAdded(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1837 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1838 | Slog.w(TAG, "Failed to notify routes added. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1843 | private void notifyRoutesRemovedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1844 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1845 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1846 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1847 | router.notifyRoutesRemoved(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1848 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1849 | Slog.w(TAG, "Failed to notify routes removed. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1854 | private void notifyRoutesChangedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1855 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1856 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1857 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1858 | router.notifyRoutesChanged(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1859 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1860 | Slog.w(TAG, "Failed to notify routes changed. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1861 | } |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1865 | private void notifySessionInfoChangedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1866 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1867 | for (IMediaRouter2 router : routers) { |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1868 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1869 | router.notifySessionInfoChanged(sessionInfo); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1870 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1871 | Slog.w(TAG, "Failed to notify session info changed. Router probably died.", ex); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1876 | private void notifyRoutesToManager(@NonNull IMediaRouter2Manager manager) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1877 | List<MediaRoute2Info> routes = new ArrayList<>(); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1878 | for (MediaRoute2ProviderInfo providerInfo : mLastProviderInfos) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1879 | routes.addAll(providerInfo.getRoutes()); |
| 1880 | } |
| 1881 | if (routes.size() == 0) { |
| 1882 | return; |
| 1883 | } |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1884 | try { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1885 | manager.notifyRoutesAdded(routes); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1886 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1887 | Slog.w(TAG, "Failed to notify all routes. Manager probably died.", ex); |
| 1888 | } |
| 1889 | } |
| 1890 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1891 | private void notifyRoutesAddedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1892 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1893 | for (IMediaRouter2Manager manager : managers) { |
| 1894 | try { |
| 1895 | manager.notifyRoutesAdded(routes); |
| 1896 | } catch (RemoteException ex) { |
| 1897 | Slog.w(TAG, "Failed to notify routes added. Manager probably died.", ex); |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1902 | private void notifyRoutesRemovedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1903 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1904 | for (IMediaRouter2Manager manager : managers) { |
| 1905 | try { |
| 1906 | manager.notifyRoutesRemoved(routes); |
| 1907 | } catch (RemoteException ex) { |
| 1908 | Slog.w(TAG, "Failed to notify routes removed. Manager probably died.", ex); |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1913 | private void notifyRoutesChangedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1914 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1915 | for (IMediaRouter2Manager manager : managers) { |
| 1916 | try { |
| 1917 | manager.notifyRoutesChanged(routes); |
| 1918 | } catch (RemoteException ex) { |
| 1919 | Slog.w(TAG, "Failed to notify routes changed. Manager probably died.", ex); |
| 1920 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1924 | private void notifySessionCreatedToManagers(long managerRequestId, |
| 1925 | @NonNull RoutingSessionInfo session) { |
| 1926 | int requesterId = toRequesterId(managerRequestId); |
| 1927 | int originalRequestId = toOriginalRequestId(managerRequestId); |
| 1928 | |
| 1929 | for (ManagerRecord manager : getManagerRecords()) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1930 | try { |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1931 | manager.mManager.notifySessionCreated( |
| 1932 | ((manager.mManagerId == requesterId) ? originalRequestId : |
| 1933 | MediaRouter2Manager.REQUEST_ID_NONE), session); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1934 | } catch (RemoteException ex) { |
| 1935 | Slog.w(TAG, "notifySessionCreatedToManagers: " |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 1936 | + "Failed to notify. Manager probably died.", ex); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1941 | private void notifySessionUpdatedToManagers( |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1942 | @NonNull List<IMediaRouter2Manager> managers, |
| 1943 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1944 | for (IMediaRouter2Manager manager : managers) { |
| 1945 | try { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1946 | manager.notifySessionUpdated(sessionInfo); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1947 | } catch (RemoteException ex) { |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1948 | Slog.w(TAG, "notifySessionUpdatedToManagers: " |
| 1949 | + "Failed to notify. Manager probably died.", ex); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | private void notifySessionReleasedToManagers( |
| 1955 | @NonNull List<IMediaRouter2Manager> managers, |
| 1956 | @NonNull RoutingSessionInfo sessionInfo) { |
| 1957 | for (IMediaRouter2Manager manager : managers) { |
| 1958 | try { |
| 1959 | manager.notifySessionReleased(sessionInfo); |
| 1960 | } catch (RemoteException ex) { |
| 1961 | Slog.w(TAG, "notifySessionReleasedToManagers: " |
| 1962 | + "Failed to notify. Manager probably died.", ex); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
Hyundo Moon | 38d06df | 2020-05-20 14:24:06 +0900 | [diff] [blame] | 1967 | private void notifyPreferredFeaturesChangedToManager(@NonNull RouterRecord routerRecord, |
| 1968 | @NonNull IMediaRouter2Manager manager) { |
| 1969 | try { |
| 1970 | manager.notifyPreferredFeaturesChanged(routerRecord.mPackageName, |
| 1971 | routerRecord.mDiscoveryPreference.getPreferredFeatures()); |
| 1972 | } catch (RemoteException ex) { |
| 1973 | Slog.w(TAG, "Failed to notify preferred features changed." |
| 1974 | + " Manager probably died.", ex); |
| 1975 | } |
| 1976 | } |
| 1977 | |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 1978 | private void notifyPreferredFeaturesChangedToManagers(@NonNull String routerPackageName, |
| 1979 | @Nullable List<String> preferredFeatures) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1980 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1981 | if (service == null) { |
| 1982 | return; |
| 1983 | } |
| 1984 | List<IMediaRouter2Manager> managers = new ArrayList<>(); |
| 1985 | synchronized (service.mLock) { |
Hyundo Moon | 7d3ab33 | 2019-10-16 11:09:56 +0900 | [diff] [blame] | 1986 | for (ManagerRecord managerRecord : mUserRecord.mManagerRecords) { |
| 1987 | managers.add(managerRecord.mManager); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1988 | } |
| 1989 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1990 | for (IMediaRouter2Manager manager : managers) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1991 | try { |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 1992 | manager.notifyPreferredFeaturesChanged(routerPackageName, preferredFeatures); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1993 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1994 | Slog.w(TAG, "Failed to notify preferred features changed." |
| 1995 | + " Manager probably died.", ex); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1996 | } |
| 1997 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1998 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1999 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 2000 | private void notifyRequestFailedToManager(@NonNull IMediaRouter2Manager manager, |
| 2001 | int requestId, int reason) { |
| 2002 | try { |
| 2003 | manager.notifyRequestFailed(requestId, reason); |
| 2004 | } catch (RemoteException ex) { |
| 2005 | Slog.w(TAG, "Failed to notify manager of the request failure." |
| 2006 | + " Manager probably died.", ex); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | private void updateDiscoveryPreferenceOnHandler() { |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2011 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 2012 | if (service == null) { |
| 2013 | return; |
| 2014 | } |
| 2015 | List<RouteDiscoveryPreference> discoveryPreferences = new ArrayList<>(); |
| 2016 | synchronized (service.mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 2017 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 2018 | discoveryPreferences.add(routerRecord.mDiscoveryPreference); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2019 | } |
| 2020 | mUserRecord.mCompositeDiscoveryPreference = |
| 2021 | new RouteDiscoveryPreference.Builder(discoveryPreferences) |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 2022 | .build(); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2023 | } |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 2024 | for (MediaRoute2Provider provider : mRouteProviders) { |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2025 | provider.updateDiscoveryPreference(mUserRecord.mCompositeDiscoveryPreference); |
| 2026 | } |
| 2027 | } |
| 2028 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 2029 | private MediaRoute2Provider findProvider(@Nullable String providerId) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 2030 | for (MediaRoute2Provider provider : mRouteProviders) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 2031 | if (TextUtils.equals(provider.getUniqueId(), providerId)) { |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 2032 | return provider; |
| 2033 | } |
| 2034 | } |
| 2035 | return null; |
| 2036 | } |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2037 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 2038 | } |
| 2039 | static final class SessionCreationRequest { |
| 2040 | public final RouterRecord mRouterRecord; |
| 2041 | public final long mUniqueRequestId; |
| 2042 | public final long mManagerRequestId; |
| 2043 | public final RoutingSessionInfo mOldSession; |
| 2044 | public final MediaRoute2Info mRoute; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2045 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 2046 | SessionCreationRequest(@NonNull RouterRecord routerRecord, long uniqueRequestId, |
| 2047 | long managerRequestId, @NonNull RoutingSessionInfo oldSession, |
| 2048 | @NonNull MediaRoute2Info route) { |
| 2049 | mRouterRecord = routerRecord; |
| 2050 | mUniqueRequestId = uniqueRequestId; |
| 2051 | mManagerRequestId = managerRequestId; |
| 2052 | mOldSession = oldSession; |
| 2053 | mRoute = route; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2054 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 2055 | } |
| 2056 | } |