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