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) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 570 | Slog.w(TAG, "registerRouter2Locked: Same router already exists. packageName=" |
| 571 | + packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 572 | return; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 573 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 574 | |
| 575 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 576 | RouterRecord routerRecord = new RouterRecord(userRecord, router, uid, pid, packageName, |
| 577 | hasConfigureWifiDisplayPermission, hasModifyAudioRoutingPermission); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 578 | try { |
| 579 | binder.linkToDeath(routerRecord, 0); |
| 580 | } catch (RemoteException ex) { |
| 581 | throw new RuntimeException("MediaRouter2 died prematurely.", ex); |
| 582 | } |
| 583 | |
| 584 | userRecord.mRouterRecords.add(routerRecord); |
| 585 | mAllRouterRecords.put(binder, routerRecord); |
| 586 | |
| 587 | userRecord.mHandler.sendMessage( |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 588 | obtainMessage(UserHandler::notifyRouterRegistered, |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 589 | userRecord.mHandler, routerRecord)); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 590 | } |
| 591 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 592 | private void unregisterRouter2Locked(@NonNull IMediaRouter2 router, boolean died) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 593 | RouterRecord routerRecord = mAllRouterRecords.remove(router.asBinder()); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 594 | if (routerRecord == null) { |
| 595 | Slog.w(TAG, "Ignoring unregistering unknown router2"); |
| 596 | return; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 597 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 598 | |
| 599 | UserRecord userRecord = routerRecord.mUserRecord; |
| 600 | userRecord.mRouterRecords.remove(routerRecord); |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 601 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 602 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManagers, |
| 603 | routerRecord.mUserRecord.mHandler, |
| 604 | routerRecord.mPackageName, /* preferredFeatures=*/ null)); |
Kyunglyul Hyun | ef7e88f | 2020-04-01 22:15:08 +0900 | [diff] [blame] | 605 | userRecord.mHandler.sendMessage( |
| 606 | obtainMessage(UserHandler::updateDiscoveryPreferenceOnHandler, |
| 607 | userRecord.mHandler)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 608 | routerRecord.dispose(); |
| 609 | disposeUserIfNeededLocked(userRecord); // since router removed from user |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 610 | } |
| 611 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 612 | private void setDiscoveryRequestWithRouter2Locked(@NonNull RouterRecord routerRecord, |
| 613 | @NonNull RouteDiscoveryPreference discoveryRequest) { |
| 614 | if (routerRecord.mDiscoveryPreference.equals(discoveryRequest)) { |
| 615 | return; |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 616 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 617 | routerRecord.mDiscoveryPreference = discoveryRequest; |
| 618 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 619 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManagers, |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 620 | routerRecord.mUserRecord.mHandler, |
| 621 | routerRecord.mPackageName, |
| 622 | routerRecord.mDiscoveryPreference.getPreferredFeatures())); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 623 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 624 | obtainMessage(UserHandler::updateDiscoveryPreferenceOnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 625 | routerRecord.mUserRecord.mHandler)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 626 | } |
| 627 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 628 | private void setRouteVolumeWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 629 | @NonNull MediaRoute2Info route, int volume) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 630 | final IBinder binder = router.asBinder(); |
| 631 | RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 632 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 633 | if (routerRecord != null) { |
| 634 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 635 | obtainMessage(UserHandler::setRouteVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 636 | routerRecord.mUserRecord.mHandler, |
| 637 | DUMMY_REQUEST_ID, route, volume)); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 641 | private void requestCreateSessionWithRouter2Locked(int requestId, @NonNull IMediaRouter2 router, |
| 642 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 643 | final IBinder binder = router.asBinder(); |
| 644 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 645 | |
| 646 | if (routerRecord == null) { |
| 647 | return; |
| 648 | } |
| 649 | |
Hyundo Moon | fa3d0ef | 2020-04-13 00:27:44 +0900 | [diff] [blame] | 650 | if (route.isSystemRoute() && !routerRecord.mHasModifyAudioRoutingPermission |
| 651 | && !TextUtils.equals(route.getId(), |
| 652 | routerRecord.mUserRecord.mHandler.mSystemProvider.getDefaultRoute().getId())) { |
| 653 | Slog.w(TAG, "MODIFY_AUDIO_ROUTING permission is required to transfer to" |
| 654 | + route); |
| 655 | routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( |
| 656 | routerRecord, requestId); |
| 657 | return; |
| 658 | } |
| 659 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 660 | long uniqueRequestId = toUniqueRequestId(routerRecord.mRouterId, requestId); |
| 661 | routerRecord.mUserRecord.mHandler.sendMessage( |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 662 | obtainMessage(UserHandler::requestCreateSessionWithRouter2OnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 663 | routerRecord.mUserRecord.mHandler, |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 664 | uniqueRequestId, routerRecord, route, |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 665 | sessionHints)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 666 | } |
| 667 | |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 668 | private void notifySessionHintsForCreatingSessionLocked(long uniqueRequestId, |
| 669 | @NonNull IMediaRouter2 router, |
| 670 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
| 671 | final IBinder binder = router.asBinder(); |
| 672 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 673 | |
| 674 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 675 | Slog.w(TAG, "notifySessionHintsForCreatingSessionLocked: Ignoring unknown router."); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | |
| 679 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 680 | obtainMessage(UserHandler::requestCreateSessionWithManagerOnHandler, |
| 681 | routerRecord.mUserRecord.mHandler, |
| 682 | uniqueRequestId, routerRecord, route, sessionHints)); |
| 683 | } |
| 684 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 685 | private void selectRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 686 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 687 | final IBinder binder = router.asBinder(); |
| 688 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 689 | |
| 690 | if (routerRecord == null) { |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 695 | obtainMessage(UserHandler::selectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 696 | routerRecord.mUserRecord.mHandler, |
| 697 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | private void deselectRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 701 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 702 | final IBinder binder = router.asBinder(); |
| 703 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 704 | |
| 705 | if (routerRecord == null) { |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 710 | obtainMessage(UserHandler::deselectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 711 | routerRecord.mUserRecord.mHandler, |
| 712 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | private void transferToRouteWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 716 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
| 717 | final IBinder binder = router.asBinder(); |
| 718 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
| 719 | |
| 720 | if (routerRecord == null) { |
| 721 | return; |
| 722 | } |
| 723 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 724 | String defaultRouteId = |
| 725 | routerRecord.mUserRecord.mHandler.mSystemProvider.getDefaultRoute().getId(); |
| 726 | if (route.isSystemRoute() && !routerRecord.mHasModifyAudioRoutingPermission |
| 727 | && !TextUtils.equals(route.getId(), defaultRouteId)) { |
| 728 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 729 | obtainMessage(UserHandler::notifySessionCreationFailedToRouter, |
| 730 | routerRecord.mUserRecord.mHandler, |
| 731 | routerRecord, toOriginalRequestId(DUMMY_REQUEST_ID))); |
| 732 | } else { |
| 733 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 734 | obtainMessage(UserHandler::transferToRouteOnHandler, |
| 735 | routerRecord.mUserRecord.mHandler, |
| 736 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId, route)); |
| 737 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | private void setSessionVolumeWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 741 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 742 | final IBinder binder = router.asBinder(); |
| 743 | RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 744 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 745 | if (routerRecord == null) { |
| 746 | return; |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 747 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 748 | |
| 749 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 750 | obtainMessage(UserHandler::setSessionVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 751 | routerRecord.mUserRecord.mHandler, |
| 752 | DUMMY_REQUEST_ID, uniqueSessionId, volume)); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 753 | } |
| 754 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 755 | private void releaseSessionWithRouter2Locked(@NonNull IMediaRouter2 router, |
| 756 | @NonNull String uniqueSessionId) { |
| 757 | final IBinder binder = router.asBinder(); |
| 758 | final RouterRecord routerRecord = mAllRouterRecords.get(binder); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 759 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 760 | if (routerRecord == null) { |
| 761 | return; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 762 | } |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 763 | |
| 764 | routerRecord.mUserRecord.mHandler.sendMessage( |
| 765 | obtainMessage(UserHandler::releaseSessionOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 766 | routerRecord.mUserRecord.mHandler, |
| 767 | DUMMY_REQUEST_ID, routerRecord, uniqueSessionId)); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 768 | } |
| 769 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 770 | //////////////////////////////////////////////////////////// |
| 771 | //// ***Locked methods related to MediaRouter2Manager |
| 772 | //// - Should have @NonNull/@Nullable on all arguments |
| 773 | //////////////////////////////////////////////////////////// |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 774 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 775 | private List<RoutingSessionInfo> getActiveSessionsLocked( |
| 776 | @NonNull IMediaRouter2Manager manager) { |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 777 | final IBinder binder = manager.asBinder(); |
| 778 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 779 | |
| 780 | if (managerRecord == null) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 781 | Slog.w(TAG, "getActiveSessionLocked: Ignoring unknown manager"); |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 782 | return Collections.emptyList(); |
| 783 | } |
| 784 | |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 785 | List<RoutingSessionInfo> sessionInfos = new ArrayList<>(); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 786 | for (MediaRoute2Provider provider : managerRecord.mUserRecord.mHandler.mRouteProviders) { |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 787 | sessionInfos.addAll(provider.getSessionInfos()); |
| 788 | } |
| 789 | return sessionInfos; |
| 790 | } |
| 791 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 792 | private void registerManagerLocked(@NonNull IMediaRouter2Manager manager, |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 793 | int uid, int pid, @NonNull String packageName, int userId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 794 | final IBinder binder = manager.asBinder(); |
| 795 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 796 | |
| 797 | if (managerRecord != null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 798 | Slog.w(TAG, "registerManagerLocked: Same manager already exists. packageName=" |
| 799 | + packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
| 803 | UserRecord userRecord = getOrCreateUserRecordLocked(userId); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 804 | managerRecord = new ManagerRecord(userRecord, manager, uid, pid, packageName); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 805 | try { |
| 806 | binder.linkToDeath(managerRecord, 0); |
| 807 | } catch (RemoteException ex) { |
| 808 | throw new RuntimeException("Media router manager died prematurely.", ex); |
| 809 | } |
| 810 | |
| 811 | userRecord.mManagerRecords.add(managerRecord); |
| 812 | mAllManagerRecords.put(binder, managerRecord); |
| 813 | |
| 814 | userRecord.mHandler.sendMessage(obtainMessage(UserHandler::notifyRoutesToManager, |
| 815 | userRecord.mHandler, manager)); |
| 816 | |
| 817 | for (RouterRecord routerRecord : userRecord.mRouterRecords) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 818 | // TODO: UserRecord <-> routerRecord, why do they reference each other? |
| 819 | // How about removing mUserRecord from routerRecord? |
| 820 | routerRecord.mUserRecord.mHandler.sendMessage( |
Hyundo Moon | 38d06df | 2020-05-20 14:24:06 +0900 | [diff] [blame] | 821 | obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManager, |
| 822 | routerRecord.mUserRecord.mHandler, routerRecord, manager)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | private void unregisterManagerLocked(@NonNull IMediaRouter2Manager manager, boolean died) { |
| 827 | ManagerRecord managerRecord = mAllManagerRecords.remove(manager.asBinder()); |
| 828 | if (managerRecord == null) { |
| 829 | return; |
| 830 | } |
| 831 | UserRecord userRecord = managerRecord.mUserRecord; |
| 832 | userRecord.mManagerRecords.remove(managerRecord); |
| 833 | managerRecord.dispose(); |
| 834 | disposeUserIfNeededLocked(userRecord); // since manager removed from user |
| 835 | } |
| 836 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 837 | private void setRouteVolumeWithManagerLocked(int requestId, |
| 838 | @NonNull IMediaRouter2Manager manager, |
| 839 | @NonNull MediaRoute2Info route, int volume) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 840 | final IBinder binder = manager.asBinder(); |
| 841 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 842 | |
| 843 | if (managerRecord == null) { |
| 844 | return; |
| 845 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 846 | |
| 847 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 848 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 849 | obtainMessage(UserHandler::setRouteVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 850 | managerRecord.mUserRecord.mHandler, |
| 851 | uniqueRequestId, route, volume)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 852 | } |
| 853 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 854 | private void requestCreateSessionWithManagerLocked(int requestId, |
| 855 | @NonNull IMediaRouter2Manager manager, |
| 856 | @NonNull String packageName, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 857 | ManagerRecord managerRecord = mAllManagerRecords.get(manager.asBinder()); |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 858 | if (managerRecord == null) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 859 | return; |
| 860 | } |
| 861 | |
| 862 | RouterRecord routerRecord = managerRecord.mUserRecord.findRouterRecordLocked(packageName); |
| 863 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 864 | Slog.w(TAG, "requestCreateSessionWithManagerLocked: Ignoring session creation for " |
| 865 | + "unknown router."); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 866 | return; |
| 867 | } |
| 868 | |
| 869 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 870 | |
| 871 | // Before requesting to the provider, get session hints from the media router. |
| 872 | // As a return, media router will request to create a session. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 873 | routerRecord.mUserRecord.mHandler.sendMessage( |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 874 | obtainMessage(UserHandler::getSessionHintsForCreatingSessionOnHandler, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 875 | routerRecord.mUserRecord.mHandler, |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 876 | uniqueRequestId, routerRecord, managerRecord, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 877 | } |
| 878 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 879 | private void selectRouteWithManagerLocked(int requestId, @NonNull IMediaRouter2Manager manager, |
| 880 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 881 | final IBinder binder = manager.asBinder(); |
| 882 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 883 | |
| 884 | if (managerRecord == null) { |
| 885 | return; |
| 886 | } |
| 887 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 888 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 889 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
| 890 | .findRouterforSessionLocked(uniqueSessionId); |
| 891 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 892 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 893 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 894 | obtainMessage(UserHandler::selectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 895 | managerRecord.mUserRecord.mHandler, |
| 896 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 897 | } |
| 898 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 899 | private void deselectRouteWithManagerLocked(int requestId, |
| 900 | @NonNull IMediaRouter2Manager manager, |
| 901 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 902 | final IBinder binder = manager.asBinder(); |
| 903 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 904 | |
| 905 | if (managerRecord == null) { |
| 906 | return; |
| 907 | } |
| 908 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 909 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 910 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
| 911 | .findRouterforSessionLocked(uniqueSessionId); |
| 912 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 913 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 914 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 915 | obtainMessage(UserHandler::deselectRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 916 | managerRecord.mUserRecord.mHandler, |
| 917 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 918 | } |
| 919 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 920 | private void transferToRouteWithManagerLocked(int requestId, |
| 921 | @NonNull IMediaRouter2Manager manager, |
| 922 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 923 | final IBinder binder = manager.asBinder(); |
| 924 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 925 | |
| 926 | if (managerRecord == null) { |
| 927 | return; |
| 928 | } |
| 929 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 930 | // Can be null if the session is system's or RCN. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 931 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
| 932 | .findRouterforSessionLocked(uniqueSessionId); |
| 933 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 934 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 935 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 936 | obtainMessage(UserHandler::transferToRouteOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 937 | managerRecord.mUserRecord.mHandler, |
| 938 | uniqueRequestId, routerRecord, uniqueSessionId, route)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 939 | } |
| 940 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 941 | private void setSessionVolumeWithManagerLocked(int requestId, |
| 942 | @NonNull IMediaRouter2Manager manager, |
| 943 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 944 | final IBinder binder = manager.asBinder(); |
| 945 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 946 | |
| 947 | if (managerRecord == null) { |
| 948 | return; |
| 949 | } |
| 950 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 951 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 952 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 953 | obtainMessage(UserHandler::setSessionVolumeOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 954 | managerRecord.mUserRecord.mHandler, |
| 955 | uniqueRequestId, uniqueSessionId, volume)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 956 | } |
| 957 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 958 | private void releaseSessionWithManagerLocked(int requestId, |
| 959 | @NonNull IMediaRouter2Manager manager, |
| 960 | @NonNull String uniqueSessionId) { |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 961 | final IBinder binder = manager.asBinder(); |
| 962 | ManagerRecord managerRecord = mAllManagerRecords.get(binder); |
| 963 | |
| 964 | if (managerRecord == null) { |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | RouterRecord routerRecord = managerRecord.mUserRecord.mHandler |
| 969 | .findRouterforSessionLocked(uniqueSessionId); |
| 970 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 971 | long uniqueRequestId = toUniqueRequestId(managerRecord.mManagerId, requestId); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 972 | managerRecord.mUserRecord.mHandler.sendMessage( |
| 973 | obtainMessage(UserHandler::releaseSessionOnHandler, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 974 | managerRecord.mUserRecord.mHandler, |
| 975 | uniqueRequestId, routerRecord, uniqueSessionId)); |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | //////////////////////////////////////////////////////////// |
| 979 | //// ***Locked methods used by both router2 and manager |
| 980 | //// - Should have @NonNull/@Nullable on all arguments |
| 981 | //////////////////////////////////////////////////////////// |
| 982 | |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 983 | private UserRecord getOrCreateUserRecordLocked(int userId) { |
| 984 | UserRecord userRecord = mUserRecords.get(userId); |
| 985 | if (userRecord == null) { |
| 986 | userRecord = new UserRecord(userId); |
| 987 | mUserRecords.put(userId, userRecord); |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 988 | userRecord.init(); |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 989 | if (userId == mCurrentUserId) { |
| 990 | userRecord.mHandler.sendMessage( |
| 991 | obtainMessage(UserHandler::start, userRecord.mHandler)); |
| 992 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 993 | } |
Sungsoo Lim | dfafaf7 | 2020-01-07 13:04:05 +0900 | [diff] [blame] | 994 | return userRecord; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 995 | } |
| 996 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 997 | private void disposeUserIfNeededLocked(@NonNull UserRecord userRecord) { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 998 | // If there are no records left and the user is no longer current then go ahead |
| 999 | // and purge the user record and all of its associated state. If the user is current |
| 1000 | // then leave it alone since we might be connected to a route or want to query |
| 1001 | // the same route information again soon. |
| 1002 | if (userRecord.mUserId != mCurrentUserId |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1003 | && userRecord.mRouterRecords.isEmpty() |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1004 | && userRecord.mManagerRecords.isEmpty()) { |
| 1005 | if (DEBUG) { |
| 1006 | Slog.d(TAG, userRecord + ": Disposed"); |
| 1007 | } |
| 1008 | mUserRecords.remove(userRecord.mUserId); |
| 1009 | // Note: User already stopped (by switchUser) so no need to send stop message here. |
| 1010 | } |
| 1011 | } |
| 1012 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1013 | static long toUniqueRequestId(int requesterId, int originalRequestId) { |
| 1014 | return ((long) requesterId << 32) | originalRequestId; |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1015 | } |
| 1016 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1017 | static int toRequesterId(long uniqueRequestId) { |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1018 | return (int) (uniqueRequestId >> 32); |
| 1019 | } |
| 1020 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1021 | static int toOriginalRequestId(long uniqueRequestId) { |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 1022 | return (int) uniqueRequestId; |
| 1023 | } |
| 1024 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1025 | final class UserRecord { |
| 1026 | public final int mUserId; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1027 | //TODO: make records private for thread-safety |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1028 | final ArrayList<RouterRecord> mRouterRecords = new ArrayList<>(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1029 | final ArrayList<ManagerRecord> mManagerRecords = new ArrayList<>(); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 1030 | RouteDiscoveryPreference mCompositeDiscoveryPreference = RouteDiscoveryPreference.EMPTY; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1031 | final UserHandler mHandler; |
| 1032 | |
| 1033 | UserRecord(int userId) { |
| 1034 | mUserId = userId; |
| 1035 | mHandler = new UserHandler(MediaRouter2ServiceImpl.this, this); |
| 1036 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1037 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1038 | void init() { |
| 1039 | mHandler.init(); |
| 1040 | } |
| 1041 | |
Hyundo Moon | 0f78fc6 | 2020-06-01 14:50:32 +0900 | [diff] [blame] | 1042 | // TODO: This assumes that only one router exists in a package. |
| 1043 | // Do this in Android S or later. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1044 | RouterRecord findRouterRecordLocked(String packageName) { |
| 1045 | for (RouterRecord routerRecord : mRouterRecords) { |
| 1046 | if (TextUtils.equals(routerRecord.mPackageName, packageName)) { |
| 1047 | return routerRecord; |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 1048 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1049 | } |
| 1050 | return null; |
| 1051 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1052 | } |
| 1053 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1054 | final class RouterRecord implements IBinder.DeathRecipient { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1055 | public final UserRecord mUserRecord; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1056 | public final String mPackageName; |
Hyundo Moon | 4140fee | 2019-11-08 14:47:50 +0900 | [diff] [blame] | 1057 | public final List<Integer> mSelectRouteSequenceNumbers; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1058 | public final IMediaRouter2 mRouter; |
Kyunglyul Hyun | 8657671 | 2019-11-21 18:31:46 +0900 | [diff] [blame] | 1059 | public final int mUid; |
| 1060 | public final int mPid; |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1061 | public final boolean mHasConfigureWifiDisplayPermission; |
| 1062 | public final boolean mHasModifyAudioRoutingPermission; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1063 | public final int mRouterId; |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 1064 | |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1065 | public RouteDiscoveryPreference mDiscoveryPreference; |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1066 | public MediaRoute2Info mSelectedRoute; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1067 | |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1068 | RouterRecord(UserRecord userRecord, IMediaRouter2 router, int uid, int pid, |
| 1069 | String packageName, boolean hasConfigureWifiDisplayPermission, |
| 1070 | boolean hasModifyAudioRoutingPermission) { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1071 | mUserRecord = userRecord; |
Kyunglyul Hyun | 2f43fd6 | 2019-09-16 18:47:52 +0900 | [diff] [blame] | 1072 | mPackageName = packageName; |
Hyundo Moon | 4140fee | 2019-11-08 14:47:50 +0900 | [diff] [blame] | 1073 | mSelectRouteSequenceNumbers = new ArrayList<>(); |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1074 | mDiscoveryPreference = RouteDiscoveryPreference.EMPTY; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1075 | mRouter = router; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1076 | mUid = uid; |
| 1077 | mPid = pid; |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1078 | mHasConfigureWifiDisplayPermission = hasConfigureWifiDisplayPermission; |
| 1079 | mHasModifyAudioRoutingPermission = hasModifyAudioRoutingPermission; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1080 | mRouterId = mNextRouterOrManagerId.getAndIncrement(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | public void dispose() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1084 | mRouter.asBinder().unlinkToDeath(this, 0); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void binderDied() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1089 | routerDied(this); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1090 | } |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | final class ManagerRecord implements IBinder.DeathRecipient { |
| 1094 | public final UserRecord mUserRecord; |
| 1095 | public final IMediaRouter2Manager mManager; |
| 1096 | public final int mUid; |
| 1097 | public final int mPid; |
| 1098 | public final String mPackageName; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1099 | public final int mManagerId; |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1100 | |
| 1101 | ManagerRecord(UserRecord userRecord, IMediaRouter2Manager manager, |
Sungsoo Lim | 7065f1b | 2020-03-24 18:52:42 +0900 | [diff] [blame] | 1102 | int uid, int pid, String packageName) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1103 | mUserRecord = userRecord; |
| 1104 | mManager = manager; |
| 1105 | mUid = uid; |
| 1106 | mPid = pid; |
| 1107 | mPackageName = packageName; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1108 | mManagerId = mNextRouterOrManagerId.getAndIncrement(); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | public void dispose() { |
| 1112 | mManager.asBinder().unlinkToDeath(this, 0); |
| 1113 | } |
| 1114 | |
| 1115 | @Override |
| 1116 | public void binderDied() { |
| 1117 | managerDied(this); |
| 1118 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1119 | |
| 1120 | public void dump(PrintWriter pw, String prefix) { |
| 1121 | pw.println(prefix + this); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | @Override |
| 1125 | public String toString() { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1126 | return "Manager " + mPackageName + " (pid " + mPid + ")"; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static final class UserHandler extends Handler implements |
| 1131 | MediaRoute2ProviderWatcher.Callback, |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1132 | MediaRoute2Provider.Callback { |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1133 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1134 | private final WeakReference<MediaRouter2ServiceImpl> mServiceRef; |
| 1135 | private final UserRecord mUserRecord; |
| 1136 | private final MediaRoute2ProviderWatcher mWatcher; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1137 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1138 | private final SystemMediaRoute2Provider mSystemProvider; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1139 | private final ArrayList<MediaRoute2Provider> mRouteProviders = |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1140 | new ArrayList<>(); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1141 | |
| 1142 | private final List<MediaRoute2ProviderInfo> mLastProviderInfos = new ArrayList<>(); |
| 1143 | private final CopyOnWriteArrayList<SessionCreationRequest> mSessionCreationRequests = |
| 1144 | new CopyOnWriteArrayList<>(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1145 | private final Map<String, RouterRecord> mSessionToRouterMap = new ArrayMap<>(); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1146 | |
| 1147 | private boolean mRunning; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1148 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1149 | // TODO: (In Android S+) Pull out SystemMediaRoute2Provider out of UserHandler. |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1150 | UserHandler(@NonNull MediaRouter2ServiceImpl service, @NonNull UserRecord userRecord) { |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 1151 | super(Looper.getMainLooper(), null, true); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1152 | mServiceRef = new WeakReference<>(service); |
| 1153 | mUserRecord = userRecord; |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1154 | mSystemProvider = new SystemMediaRoute2Provider(service.mContext); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1155 | mRouteProviders.add(mSystemProvider); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1156 | mWatcher = new MediaRoute2ProviderWatcher(service.mContext, this, |
| 1157 | this, mUserRecord.mUserId); |
| 1158 | } |
| 1159 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 1160 | void init() { |
| 1161 | mSystemProvider.setCallback(this); |
| 1162 | } |
| 1163 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1164 | private void start() { |
| 1165 | if (!mRunning) { |
| 1166 | mRunning = true; |
| 1167 | mWatcher.start(); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | private void stop() { |
| 1172 | if (mRunning) { |
| 1173 | mRunning = false; |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1174 | mWatcher.stop(); // also stops all providers |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1179 | public void onAddProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1180 | proxy.setCallback(this); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1181 | mRouteProviders.add(proxy); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1182 | proxy.updateDiscoveryPreference(mUserRecord.mCompositeDiscoveryPreference); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1186 | public void onRemoveProviderService(@NonNull MediaRoute2ProviderServiceProxy proxy) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1187 | mRouteProviders.remove(proxy); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | @Override |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1191 | public void onProviderStateChanged(@NonNull MediaRoute2Provider provider) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1192 | sendMessage(PooledLambda.obtainMessage(UserHandler::onProviderStateChangedOnHandler, |
| 1193 | this, provider)); |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1194 | } |
| 1195 | |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 1196 | @Override |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1197 | public void onSessionCreated(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1198 | long uniqueRequestId, @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1199 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionCreatedOnHandler, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1200 | this, provider, uniqueRequestId, sessionInfo)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1201 | } |
| 1202 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 1203 | @Override |
| 1204 | public void onSessionUpdated(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1205 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1206 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionInfoChangedOnHandler, |
| 1207 | this, provider, sessionInfo)); |
| 1208 | } |
| 1209 | |
| 1210 | @Override |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1211 | public void onSessionReleased(@NonNull MediaRoute2Provider provider, |
| 1212 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1213 | sendMessage(PooledLambda.obtainMessage(UserHandler::onSessionReleasedOnHandler, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1214 | this, provider, sessionInfo)); |
| 1215 | } |
| 1216 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1217 | @Override |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1218 | public void onRequestFailed(@NonNull MediaRoute2Provider provider, long uniqueRequestId, |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1219 | int reason) { |
| 1220 | sendMessage(PooledLambda.obtainMessage(UserHandler::onRequestFailedOnHandler, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1221 | this, provider, uniqueRequestId, reason)); |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1222 | } |
| 1223 | |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1224 | @Nullable |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1225 | public RouterRecord findRouterforSessionLocked(@NonNull String uniqueSessionId) { |
| 1226 | return mSessionToRouterMap.get(uniqueSessionId); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1229 | private void onProviderStateChangedOnHandler(@NonNull MediaRoute2Provider provider) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1230 | int providerInfoIndex = getLastProviderInfoIndex(provider.getUniqueId()); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1231 | MediaRoute2ProviderInfo currentInfo = provider.getProviderInfo(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1232 | MediaRoute2ProviderInfo prevInfo = |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1233 | (providerInfoIndex < 0) ? null : mLastProviderInfos.get(providerInfoIndex); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1234 | if (Objects.equals(prevInfo, currentInfo)) return; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1235 | |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1236 | List<MediaRoute2Info> addedRoutes = new ArrayList<>(); |
| 1237 | List<MediaRoute2Info> removedRoutes = new ArrayList<>(); |
| 1238 | List<MediaRoute2Info> changedRoutes = new ArrayList<>(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1239 | if (prevInfo == null) { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1240 | mLastProviderInfos.add(currentInfo); |
| 1241 | addedRoutes.addAll(currentInfo.getRoutes()); |
| 1242 | } else if (currentInfo == null) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1243 | mLastProviderInfos.remove(prevInfo); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1244 | removedRoutes.addAll(prevInfo.getRoutes()); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1245 | } else { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1246 | mLastProviderInfos.set(providerInfoIndex, currentInfo); |
| 1247 | final Collection<MediaRoute2Info> prevRoutes = prevInfo.getRoutes(); |
| 1248 | final Collection<MediaRoute2Info> currentRoutes = currentInfo.getRoutes(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1249 | |
| 1250 | for (MediaRoute2Info route : currentRoutes) { |
| 1251 | if (!route.isValid()) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1252 | Slog.w(TAG, "onProviderStateChangedOnHandler: Ignoring invalid route : " |
| 1253 | + route); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1254 | continue; |
| 1255 | } |
Kyunglyul Hyun | 616096e | 2020-01-09 16:15:03 +0900 | [diff] [blame] | 1256 | MediaRoute2Info prevRoute = prevInfo.getRoute(route.getOriginalId()); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1257 | if (prevRoute == null) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1258 | addedRoutes.add(route); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1259 | } else if (!Objects.equals(prevRoute, route)) { |
| 1260 | changedRoutes.add(route); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | for (MediaRoute2Info prevRoute : prevInfo.getRoutes()) { |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1265 | if (currentInfo.getRoute(prevRoute.getOriginalId()) == null) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1266 | removedRoutes.add(prevRoute); |
| 1267 | } |
| 1268 | } |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1269 | } |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1270 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1271 | List<IMediaRouter2> routersWithModifyAudioRoutingPermission = getRouters(true); |
| 1272 | List<IMediaRouter2> routersWithoutModifyAudioRoutingPermission = getRouters(false); |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1273 | List<IMediaRouter2Manager> managers = getManagers(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1274 | List<MediaRoute2Info> defaultRoute = new ArrayList<>(); |
| 1275 | defaultRoute.add(mSystemProvider.getDefaultRoute()); |
| 1276 | |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1277 | if (addedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1278 | notifyRoutesAddedToRouters(routersWithModifyAudioRoutingPermission, addedRoutes); |
| 1279 | if (!provider.mIsSystemRouteProvider) { |
| 1280 | notifyRoutesAddedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1281 | addedRoutes); |
| 1282 | } else if (prevInfo == null) { |
| 1283 | notifyRoutesAddedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1284 | defaultRoute); |
| 1285 | } // 'else' is handled as changed routes |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1286 | notifyRoutesAddedToManagers(managers, addedRoutes); |
| 1287 | } |
| 1288 | if (removedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1289 | notifyRoutesRemovedToRouters(routersWithModifyAudioRoutingPermission, |
| 1290 | removedRoutes); |
| 1291 | if (!provider.mIsSystemRouteProvider) { |
| 1292 | notifyRoutesRemovedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1293 | removedRoutes); |
| 1294 | } |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1295 | notifyRoutesRemovedToManagers(managers, removedRoutes); |
| 1296 | } |
| 1297 | if (changedRoutes.size() > 0) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1298 | notifyRoutesChangedToRouters(routersWithModifyAudioRoutingPermission, |
| 1299 | changedRoutes); |
| 1300 | if (!provider.mIsSystemRouteProvider) { |
| 1301 | notifyRoutesChangedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1302 | changedRoutes); |
| 1303 | } else if (prevInfo != null) { |
| 1304 | notifyRoutesChangedToRouters(routersWithoutModifyAudioRoutingPermission, |
| 1305 | defaultRoute); |
| 1306 | } // 'else' is handled as added routes |
Sungsoo Lim | 82268bd | 2020-04-02 16:00:22 +0900 | [diff] [blame] | 1307 | notifyRoutesChangedToManagers(managers, changedRoutes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1308 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1309 | } |
| 1310 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 1311 | private int getLastProviderInfoIndex(@NonNull String providerId) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1312 | for (int i = 0; i < mLastProviderInfos.size(); i++) { |
| 1313 | MediaRoute2ProviderInfo providerInfo = mLastProviderInfos.get(i); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1314 | if (TextUtils.equals(providerInfo.getUniqueId(), providerId)) { |
| 1315 | return i; |
| 1316 | } |
| 1317 | } |
| 1318 | return -1; |
| 1319 | } |
| 1320 | |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1321 | private void getSessionHintsForCreatingSessionOnHandler(long uniqueRequestId, |
| 1322 | @NonNull RouterRecord routerRecord, @NonNull ManagerRecord managerRecord, |
| 1323 | @NonNull MediaRoute2Info route) { |
| 1324 | SessionCreationRequest request = |
| 1325 | new SessionCreationRequest(routerRecord, uniqueRequestId, route, managerRecord); |
| 1326 | mSessionCreationRequests.add(request); |
| 1327 | |
| 1328 | try { |
| 1329 | routerRecord.mRouter.getSessionHintsForCreatingSession(uniqueRequestId, route); |
| 1330 | } catch (RemoteException ex) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1331 | Slog.w(TAG, "getSessionHintsForCreatingSessionOnHandler: " |
| 1332 | + "Failed to request. Router probably died.", ex); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1333 | mSessionCreationRequests.remove(request); |
| 1334 | notifyRequestFailedToManager(managerRecord.mManager, |
| 1335 | toOriginalRequestId(uniqueRequestId), REASON_UNKNOWN_ERROR); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | private void requestCreateSessionWithRouter2OnHandler(long uniqueRequestId, |
| 1340 | @NonNull RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1341 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1342 | |
| 1343 | final MediaRoute2Provider provider = findProvider(route.getProviderId()); |
| 1344 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1345 | Slog.w(TAG, "requestCreateSessionWithRouter2OnHandler: Ignoring session " |
| 1346 | + "creation request since no provider found for given route=" + route); |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1347 | notifySessionCreationFailedToRouter(routerRecord, |
| 1348 | toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1349 | return; |
| 1350 | } |
| 1351 | |
Hyundo Moon | dc79a7a | 2020-01-16 15:18:33 +0900 | [diff] [blame] | 1352 | SessionCreationRequest request = |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1353 | new SessionCreationRequest(routerRecord, uniqueRequestId, route, null); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1354 | mSessionCreationRequests.add(request); |
| 1355 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1356 | provider.requestCreateSession(uniqueRequestId, routerRecord.mPackageName, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1357 | route.getOriginalId(), sessionHints); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1358 | } |
| 1359 | |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1360 | private void requestCreateSessionWithManagerOnHandler(long uniqueRequestId, |
| 1361 | @NonNull RouterRecord routerRecord, |
| 1362 | @NonNull MediaRoute2Info route, @Nullable Bundle sessionHints) { |
| 1363 | SessionCreationRequest matchingRequest = null; |
| 1364 | for (SessionCreationRequest request : mSessionCreationRequests) { |
| 1365 | if (request.mUniqueRequestId == uniqueRequestId) { |
| 1366 | matchingRequest = request; |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | if (matchingRequest == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1371 | Slog.w(TAG, "requestCreateSessionWithManagerOnHandler: " |
| 1372 | + "Ignoring an unknown session creation request."); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | if (!TextUtils.equals(matchingRequest.mRoute.getId(), route.getId())) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1377 | Slog.w(TAG, "requestCreateSessionWithManagerOnHandler: " |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1378 | + "The given route is different from the requested route."); |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | final MediaRoute2Provider provider = findProvider(route.getProviderId()); |
| 1383 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1384 | Slog.w(TAG, "requestCreateSessionWithManagerOnHandler: Ignoring session " |
| 1385 | + "creation request since no provider found for given route=" + route); |
Kyunglyul Hyun | f0eb51b | 2020-04-14 19:56:16 +0900 | [diff] [blame] | 1386 | mSessionCreationRequests.remove(matchingRequest); |
| 1387 | notifyRequestFailedToManager(matchingRequest.mRequestedManagerRecord.mManager, |
| 1388 | toOriginalRequestId(uniqueRequestId), REASON_ROUTE_NOT_AVAILABLE); |
| 1389 | return; |
| 1390 | } |
| 1391 | |
| 1392 | provider.requestCreateSession(uniqueRequestId, routerRecord.mPackageName, |
| 1393 | route.getOriginalId(), sessionHints); |
| 1394 | } |
| 1395 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1396 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1397 | private void selectRouteOnHandler(long uniqueRequestId, @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1398 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1399 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1400 | "selecting")) { |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | final String providerId = route.getProviderId(); |
| 1405 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1406 | if (provider == null) { |
| 1407 | return; |
| 1408 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1409 | provider.selectRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
| 1410 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1411 | } |
| 1412 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1413 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1414 | private void deselectRouteOnHandler(long uniqueRequestId, |
| 1415 | @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1416 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1417 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1418 | "deselecting")) { |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | final String providerId = route.getProviderId(); |
| 1423 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1424 | if (provider == null) { |
| 1425 | return; |
| 1426 | } |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1427 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1428 | provider.deselectRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1429 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1430 | } |
| 1431 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1432 | // routerRecord can be null if the session is system's or RCN. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1433 | private void transferToRouteOnHandler(long uniqueRequestId, |
| 1434 | @Nullable RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1435 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1436 | if (!checkArgumentsForSessionControl(routerRecord, uniqueSessionId, route, |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1437 | "transferring to")) { |
| 1438 | return; |
| 1439 | } |
| 1440 | |
| 1441 | final String providerId = route.getProviderId(); |
| 1442 | final MediaRoute2Provider provider = findProvider(providerId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1443 | if (provider == null) { |
| 1444 | return; |
| 1445 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1446 | provider.transferToRoute(uniqueRequestId, getOriginalId(uniqueSessionId), |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1447 | route.getOriginalId()); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1448 | } |
| 1449 | |
Kyunglyul Hyun | a69280b | 2020-04-03 15:15:09 +0900 | [diff] [blame] | 1450 | // routerRecord is null if and only if the session is created without the request, which |
| 1451 | // includes the system's session and RCN cases. |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1452 | private boolean checkArgumentsForSessionControl(@Nullable RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1453 | @NonNull String uniqueSessionId, @NonNull MediaRoute2Info route, |
| 1454 | @NonNull String description) { |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1455 | final String providerId = route.getProviderId(); |
| 1456 | final MediaRoute2Provider provider = findProvider(providerId); |
| 1457 | if (provider == null) { |
| 1458 | Slog.w(TAG, "Ignoring " + description + " route since no provider found for " |
| 1459 | + "given route=" + route); |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1463 | // 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] | 1464 | if (TextUtils.equals(getProviderId(uniqueSessionId), mSystemProvider.getUniqueId())) { |
| 1465 | return true; |
| 1466 | } |
| 1467 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1468 | RouterRecord matchingRecord = mSessionToRouterMap.get(uniqueSessionId); |
| 1469 | if (matchingRecord != routerRecord) { |
| 1470 | Slog.w(TAG, "Ignoring " + description + " route from non-matching router. " |
| 1471 | + "packageName=" + routerRecord.mPackageName + " route=" + route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
| 1474 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1475 | final String sessionId = getOriginalId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1476 | if (sessionId == null) { |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1477 | 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] | 1478 | + "uniqueSessionId=" + uniqueSessionId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | return true; |
| 1483 | } |
| 1484 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1485 | private void setRouteVolumeOnHandler(long uniqueRequestId, @NonNull MediaRoute2Info route, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1486 | int volume) { |
| 1487 | final MediaRoute2Provider provider = findProvider(route.getProviderId()); |
| 1488 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1489 | Slog.w(TAG, "setRouteVolumeOnHandler: Couldn't find provider for route=" + route); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1490 | return; |
| 1491 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1492 | provider.setRouteVolume(uniqueRequestId, route.getOriginalId(), volume); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1493 | } |
| 1494 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1495 | private void setSessionVolumeOnHandler(long uniqueRequestId, |
| 1496 | @NonNull String uniqueSessionId, int volume) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1497 | final MediaRoute2Provider provider = findProvider(getProviderId(uniqueSessionId)); |
| 1498 | if (provider == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1499 | Slog.w(TAG, "setSessionVolumeOnHandler: Couldn't find provider for session id=" |
| 1500 | + uniqueSessionId); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1501 | return; |
| 1502 | } |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1503 | provider.setSessionVolume(uniqueRequestId, getOriginalId(uniqueSessionId), volume); |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | private void releaseSessionOnHandler(long uniqueRequestId, |
Hyundo Moon | 1f06668 | 2020-04-13 04:04:15 +0900 | [diff] [blame] | 1507 | @Nullable RouterRecord routerRecord, @NonNull String uniqueSessionId) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1508 | final RouterRecord matchingRecord = mSessionToRouterMap.get(uniqueSessionId); |
| 1509 | if (matchingRecord != routerRecord) { |
Hyundo Moon | 1f06668 | 2020-04-13 04:04:15 +0900 | [diff] [blame] | 1510 | Slog.w(TAG, "Ignoring releasing session from non-matching router. packageName=" |
| 1511 | + (routerRecord == null ? null : routerRecord.mPackageName) |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1512 | + " uniqueSessionId=" + uniqueSessionId); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1516 | final String providerId = getProviderId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1517 | if (providerId == null) { |
| 1518 | Slog.w(TAG, "Ignoring releasing session with invalid unique session ID. " |
| 1519 | + "uniqueSessionId=" + uniqueSessionId); |
| 1520 | return; |
| 1521 | } |
| 1522 | |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 1523 | final String sessionId = getOriginalId(uniqueSessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1524 | if (sessionId == null) { |
| 1525 | Slog.w(TAG, "Ignoring releasing session with invalid unique session ID. " |
| 1526 | + "uniqueSessionId=" + uniqueSessionId + " providerId=" + providerId); |
| 1527 | return; |
| 1528 | } |
| 1529 | |
| 1530 | final MediaRoute2Provider provider = findProvider(providerId); |
| 1531 | if (provider == null) { |
| 1532 | Slog.w(TAG, "Ignoring releasing session since no provider found for given " |
| 1533 | + "providerId=" + providerId); |
| 1534 | return; |
| 1535 | } |
| 1536 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1537 | provider.releaseSession(uniqueRequestId, sessionId); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | private void onSessionCreatedOnHandler(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1541 | long uniqueRequestId, @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1542 | notifySessionCreatedToManagers(getManagers(), |
| 1543 | toOriginalRequestId(uniqueRequestId), sessionInfo); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1544 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1545 | if (uniqueRequestId == REQUEST_ID_NONE) { |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 1546 | // The session is created without any matching request. |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1550 | SessionCreationRequest matchingRequest = null; |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 1551 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1552 | for (SessionCreationRequest request : mSessionCreationRequests) { |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1553 | if (request.mUniqueRequestId == uniqueRequestId |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1554 | && TextUtils.equals( |
| 1555 | request.mRoute.getProviderId(), provider.getUniqueId())) { |
| 1556 | matchingRequest = request; |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | if (matchingRequest == null) { |
| 1562 | Slog.w(TAG, "Ignoring session creation result for unknown request. " |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1563 | + "uniqueRequestId=" + uniqueRequestId + ", sessionInfo=" + sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 1567 | mSessionCreationRequests.remove(matchingRequest); |
| 1568 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1569 | if (sessionInfo == null) { |
| 1570 | // Failed |
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 | String originalRouteId = matchingRequest.mRoute.getId(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1577 | RouterRecord routerRecord = matchingRequest.mRouterRecord; |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1578 | |
Hyundo Moon | dc79a7a | 2020-01-16 15:18:33 +0900 | [diff] [blame] | 1579 | if (!sessionInfo.getSelectedRoutes().contains(originalRouteId)) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1580 | Slog.w(TAG, "Created session doesn't match the original request." |
| 1581 | + " originalRouteId=" + originalRouteId |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1582 | + ", uniqueRequestId=" + uniqueRequestId + ", sessionInfo=" + sessionInfo); |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1583 | notifySessionCreationFailedToRouter(matchingRequest.mRouterRecord, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1584 | toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | // Succeeded |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 1589 | if (sessionInfo.isSystemSession() |
| 1590 | && !matchingRequest.mRouterRecord.mHasModifyAudioRoutingPermission) { |
| 1591 | notifySessionCreatedToRouter(matchingRequest.mRouterRecord, |
| 1592 | toOriginalRequestId(uniqueRequestId), |
| 1593 | mSystemProvider.getDefaultSessionInfo()); |
| 1594 | } else { |
| 1595 | notifySessionCreatedToRouter(matchingRequest.mRouterRecord, |
| 1596 | toOriginalRequestId(uniqueRequestId), sessionInfo); |
| 1597 | } |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1598 | mSessionToRouterMap.put(sessionInfo.getId(), routerRecord); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1599 | } |
| 1600 | |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1601 | private void onSessionInfoChangedOnHandler(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1602 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1603 | List<IMediaRouter2Manager> managers = getManagers(); |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1604 | notifySessionUpdatedToManagers(managers, sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1605 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1606 | // For system provider, notify all routers. |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1607 | if (provider == mSystemProvider) { |
| 1608 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1609 | if (service == null) { |
| 1610 | return; |
| 1611 | } |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1612 | notifySessionInfoChangedToRouters(getRouters(true), sessionInfo); |
| 1613 | notifySessionInfoChangedToRouters(getRouters(false), |
| 1614 | mSystemProvider.getDefaultSessionInfo()); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1615 | return; |
| 1616 | } |
| 1617 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1618 | RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId()); |
| 1619 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1620 | Slog.w(TAG, "onSessionInfoChangedOnHandler: No matching router found for session=" |
| 1621 | + sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1622 | return; |
| 1623 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1624 | notifySessionInfoChangedToRouter(routerRecord, sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1625 | } |
| 1626 | |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1627 | private void onSessionReleasedOnHandler(@NonNull MediaRoute2Provider provider, |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 1628 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1629 | List<IMediaRouter2Manager> managers = getManagers(); |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1630 | notifySessionReleasedToManagers(managers, sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1631 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1632 | RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId()); |
| 1633 | if (routerRecord == null) { |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1634 | Slog.w(TAG, "onSessionReleasedOnHandler: No matching router found for session=" |
| 1635 | + sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1636 | return; |
| 1637 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1638 | notifySessionReleasedToRouter(routerRecord, sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1639 | } |
| 1640 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1641 | private void onRequestFailedOnHandler(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1642 | long uniqueRequestId, int reason) { |
| 1643 | if (handleSessionCreationRequestFailed(provider, uniqueRequestId, reason)) { |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1644 | return; |
| 1645 | } |
| 1646 | |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1647 | final int requesterId = toRequesterId(uniqueRequestId); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1648 | for (ManagerRecord manager : getManagerRecords()) { |
| 1649 | if (manager.mManagerId == requesterId) { |
| 1650 | notifyRequestFailedToManager( |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1651 | manager.mManager, toOriginalRequestId(uniqueRequestId), reason); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1652 | return; |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1656 | // Currently, only the manager can get notified of failures. |
| 1657 | // TODO: Notify router too when the related callback is introduced. |
| 1658 | } |
| 1659 | |
Hyundo Moon | 0f78fc6 | 2020-06-01 14:50:32 +0900 | [diff] [blame] | 1660 | // TODO(b/157873556): Find a way to prevent providers from notifying error on random reqID. |
| 1661 | // Possible solutions can be: |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1662 | // 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] | 1663 | // 2) Throw exception on providers when they try to notify error on |
| 1664 | // random uniqueRequestId. |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1665 | private boolean handleSessionCreationRequestFailed(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1666 | long uniqueRequestId, int reason) { |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1667 | // Check whether the failure is about creating a session |
| 1668 | SessionCreationRequest matchingRequest = null; |
| 1669 | for (SessionCreationRequest request : mSessionCreationRequests) { |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1670 | if (request.mUniqueRequestId == uniqueRequestId && TextUtils.equals( |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1671 | request.mRoute.getProviderId(), provider.getUniqueId())) { |
| 1672 | matchingRequest = request; |
| 1673 | break; |
| 1674 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1675 | } |
| 1676 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1677 | if (matchingRequest == null) { |
| 1678 | // The failure is not about creating a session. |
| 1679 | return false; |
| 1680 | } |
| 1681 | |
| 1682 | mSessionCreationRequests.remove(matchingRequest); |
| 1683 | |
| 1684 | // Notify the requester about the failure. |
| 1685 | // The call should be made by either MediaRouter2 or MediaRouter2Manager. |
| 1686 | if (matchingRequest.mRequestedManagerRecord == null) { |
| 1687 | notifySessionCreationFailedToRouter( |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1688 | matchingRequest.mRouterRecord, toOriginalRequestId(uniqueRequestId)); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1689 | } else { |
| 1690 | notifyRequestFailedToManager(matchingRequest.mRequestedManagerRecord.mManager, |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 1691 | toOriginalRequestId(uniqueRequestId), reason); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1692 | } |
| 1693 | return true; |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | private void notifySessionCreatedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1697 | int requestId, @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1698 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1699 | routerRecord.mRouter.notifySessionCreated(requestId, sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1700 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1701 | Slog.w(TAG, "Failed to notify router of the session creation." |
| 1702 | + " Router probably died.", ex); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1706 | private void notifySessionCreationFailedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1707 | int requestId) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1708 | try { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1709 | routerRecord.mRouter.notifySessionCreated(requestId, |
| 1710 | /* sessionInfo= */ null); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1711 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1712 | Slog.w(TAG, "Failed to notify router of the session creation failure." |
| 1713 | + " Router probably died.", ex); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1717 | private void notifySessionInfoChangedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1718 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1719 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1720 | routerRecord.mRouter.notifySessionInfoChanged(sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1721 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1722 | Slog.w(TAG, "Failed to notify router of the session info change." |
| 1723 | + " Router probably died.", ex); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1727 | private void notifySessionReleasedToRouter(@NonNull RouterRecord routerRecord, |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1728 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1729 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1730 | routerRecord.mRouter.notifySessionReleased(sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1731 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1732 | Slog.w(TAG, "Failed to notify router of the session release." |
| 1733 | + " Router probably died.", ex); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1737 | private List<IMediaRouter2> getAllRouters() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1738 | final List<IMediaRouter2> routers = new ArrayList<>(); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1739 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1740 | if (service == null) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1741 | return routers; |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1742 | } |
| 1743 | synchronized (service.mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1744 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 1745 | routers.add(routerRecord.mRouter); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1746 | } |
| 1747 | } |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1748 | return routers; |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1749 | } |
| 1750 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 1751 | private List<IMediaRouter2> getRouters(boolean hasModifyAudioRoutingPermission) { |
| 1752 | final List<IMediaRouter2> routers = new ArrayList<>(); |
| 1753 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1754 | if (service == null) { |
| 1755 | return routers; |
| 1756 | } |
| 1757 | synchronized (service.mLock) { |
| 1758 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 1759 | if (hasModifyAudioRoutingPermission |
| 1760 | == routerRecord.mHasModifyAudioRoutingPermission) { |
| 1761 | routers.add(routerRecord.mRouter); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | return routers; |
| 1766 | } |
| 1767 | |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1768 | private List<IMediaRouter2Manager> getManagers() { |
| 1769 | final List<IMediaRouter2Manager> managers = new ArrayList<>(); |
| 1770 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1771 | if (service == null) { |
| 1772 | return managers; |
| 1773 | } |
| 1774 | synchronized (service.mLock) { |
| 1775 | for (ManagerRecord managerRecord : mUserRecord.mManagerRecords) { |
| 1776 | managers.add(managerRecord.mManager); |
| 1777 | } |
| 1778 | } |
| 1779 | return managers; |
| 1780 | } |
| 1781 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1782 | private List<ManagerRecord> getManagerRecords() { |
| 1783 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1784 | if (service == null) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 1785 | return Collections.emptyList(); |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 1786 | } |
| 1787 | synchronized (service.mLock) { |
| 1788 | return new ArrayList<>(mUserRecord.mManagerRecords); |
| 1789 | } |
| 1790 | } |
| 1791 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1792 | private void notifyRouterRegistered(@NonNull RouterRecord routerRecord) { |
| 1793 | List<MediaRoute2Info> currentRoutes = new ArrayList<>(); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1794 | |
| 1795 | MediaRoute2ProviderInfo systemProviderInfo = null; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1796 | for (MediaRoute2ProviderInfo providerInfo : mLastProviderInfos) { |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1797 | // TODO: Create MediaRoute2ProviderInfo#isSystemProvider() |
| 1798 | if (TextUtils.equals(providerInfo.getUniqueId(), mSystemProvider.getUniqueId())) { |
| 1799 | // Adding routes from system provider will be handled below, so skip it here. |
| 1800 | systemProviderInfo = providerInfo; |
| 1801 | continue; |
| 1802 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1803 | currentRoutes.addAll(providerInfo.getRoutes()); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1804 | } |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1805 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1806 | RoutingSessionInfo currentSystemSessionInfo; |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1807 | if (routerRecord.mHasModifyAudioRoutingPermission) { |
| 1808 | if (systemProviderInfo != null) { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1809 | currentRoutes.addAll(systemProviderInfo.getRoutes()); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1810 | } else { |
| 1811 | // This shouldn't happen. |
Hyundo Moon | 369be95 | 2020-06-11 17:37:01 +0900 | [diff] [blame] | 1812 | Slog.wtf(TAG, "System route provider not found."); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1813 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1814 | currentSystemSessionInfo = mSystemProvider.getSessionInfos().get(0); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1815 | } else { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1816 | currentRoutes.add(mSystemProvider.getDefaultRoute()); |
| 1817 | currentSystemSessionInfo = mSystemProvider.getDefaultSessionInfo(); |
Hyundo Moon | fa248a7 | 2020-05-12 20:05:27 +0900 | [diff] [blame] | 1818 | } |
| 1819 | |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1820 | if (currentRoutes.size() == 0) { |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1823 | |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1824 | try { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1825 | routerRecord.mRouter.notifyRouterRegistered( |
| 1826 | currentRoutes, currentSystemSessionInfo); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1827 | } catch (RemoteException ex) { |
Hyundo Moon | bc63327 | 2020-06-04 18:12:29 +0900 | [diff] [blame] | 1828 | Slog.w(TAG, "Failed to notify router registered. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1832 | private void notifyRoutesAddedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1833 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1834 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1835 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1836 | router.notifyRoutesAdded(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1837 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1838 | Slog.w(TAG, "Failed to notify routes added. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1843 | private void notifyRoutesRemovedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1844 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1845 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1846 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1847 | router.notifyRoutesRemoved(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1848 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1849 | Slog.w(TAG, "Failed to notify routes removed. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1854 | private void notifyRoutesChangedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1855 | @NonNull List<MediaRoute2Info> routes) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1856 | for (IMediaRouter2 router : routers) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1857 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1858 | router.notifyRoutesChanged(routes); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1859 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1860 | Slog.w(TAG, "Failed to notify routes changed. Router probably died.", ex); |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 1861 | } |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1865 | private void notifySessionInfoChangedToRouters(@NonNull List<IMediaRouter2> routers, |
| 1866 | @NonNull RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1867 | for (IMediaRouter2 router : routers) { |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1868 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1869 | router.notifySessionInfoChanged(sessionInfo); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1870 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1871 | Slog.w(TAG, "Failed to notify session info changed. Router probably died.", ex); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1876 | private void notifyRoutesToManager(@NonNull IMediaRouter2Manager manager) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1877 | List<MediaRoute2Info> routes = new ArrayList<>(); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 1878 | for (MediaRoute2ProviderInfo providerInfo : mLastProviderInfos) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1879 | routes.addAll(providerInfo.getRoutes()); |
| 1880 | } |
| 1881 | if (routes.size() == 0) { |
| 1882 | return; |
| 1883 | } |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1884 | try { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1885 | manager.notifyRoutesAdded(routes); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 1886 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1887 | Slog.w(TAG, "Failed to notify all routes. Manager probably died.", ex); |
| 1888 | } |
| 1889 | } |
| 1890 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1891 | private void notifyRoutesAddedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1892 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1893 | for (IMediaRouter2Manager manager : managers) { |
| 1894 | try { |
| 1895 | manager.notifyRoutesAdded(routes); |
| 1896 | } catch (RemoteException ex) { |
| 1897 | Slog.w(TAG, "Failed to notify routes added. Manager probably died.", ex); |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1902 | private void notifyRoutesRemovedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1903 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1904 | for (IMediaRouter2Manager manager : managers) { |
| 1905 | try { |
| 1906 | manager.notifyRoutesRemoved(routes); |
| 1907 | } catch (RemoteException ex) { |
| 1908 | Slog.w(TAG, "Failed to notify routes removed. Manager probably died.", ex); |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1913 | private void notifyRoutesChangedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
| 1914 | @NonNull List<MediaRoute2Info> routes) { |
Kyunglyul Hyun | 4ed6875 | 2019-11-04 16:05:34 +0900 | [diff] [blame] | 1915 | for (IMediaRouter2Manager manager : managers) { |
| 1916 | try { |
| 1917 | manager.notifyRoutesChanged(routes); |
| 1918 | } catch (RemoteException ex) { |
| 1919 | Slog.w(TAG, "Failed to notify routes changed. Manager probably died.", ex); |
| 1920 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 1924 | private void notifySessionCreatedToManagers(@NonNull List<IMediaRouter2Manager> managers, |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1925 | int requestId, @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1926 | for (IMediaRouter2Manager manager : managers) { |
| 1927 | try { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1928 | manager.notifySessionCreated(requestId, sessionInfo); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1929 | } catch (RemoteException ex) { |
| 1930 | Slog.w(TAG, "notifySessionCreatedToManagers: " |
| 1931 | + "failed to notify. Manager probably died.", ex); |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1936 | private void notifySessionUpdatedToManagers( |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1937 | @NonNull List<IMediaRouter2Manager> managers, |
| 1938 | @NonNull RoutingSessionInfo sessionInfo) { |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1939 | for (IMediaRouter2Manager manager : managers) { |
| 1940 | try { |
Kyunglyul Hyun | f064dcae | 2020-03-06 22:59:14 +0900 | [diff] [blame] | 1941 | manager.notifySessionUpdated(sessionInfo); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1942 | } catch (RemoteException ex) { |
Kyunglyul Hyun | fe7b5b1 | 2020-06-04 20:17:59 +0900 | [diff] [blame] | 1943 | Slog.w(TAG, "notifySessionUpdatedToManagers: " |
| 1944 | + "Failed to notify. Manager probably died.", ex); |
| 1945 | } |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | private void notifySessionReleasedToManagers( |
| 1950 | @NonNull List<IMediaRouter2Manager> managers, |
| 1951 | @NonNull RoutingSessionInfo sessionInfo) { |
| 1952 | for (IMediaRouter2Manager manager : managers) { |
| 1953 | try { |
| 1954 | manager.notifySessionReleased(sessionInfo); |
| 1955 | } catch (RemoteException ex) { |
| 1956 | Slog.w(TAG, "notifySessionReleasedToManagers: " |
| 1957 | + "Failed to notify. Manager probably died.", ex); |
Kyunglyul Hyun | c658383 | 2020-01-17 11:11:46 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
Hyundo Moon | 38d06df | 2020-05-20 14:24:06 +0900 | [diff] [blame] | 1962 | private void notifyPreferredFeaturesChangedToManager(@NonNull RouterRecord routerRecord, |
| 1963 | @NonNull IMediaRouter2Manager manager) { |
| 1964 | try { |
| 1965 | manager.notifyPreferredFeaturesChanged(routerRecord.mPackageName, |
| 1966 | routerRecord.mDiscoveryPreference.getPreferredFeatures()); |
| 1967 | } catch (RemoteException ex) { |
| 1968 | Slog.w(TAG, "Failed to notify preferred features changed." |
| 1969 | + " Manager probably died.", ex); |
| 1970 | } |
| 1971 | } |
| 1972 | |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 1973 | private void notifyPreferredFeaturesChangedToManagers(@NonNull String routerPackageName, |
| 1974 | @Nullable List<String> preferredFeatures) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1975 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 1976 | if (service == null) { |
| 1977 | return; |
| 1978 | } |
| 1979 | List<IMediaRouter2Manager> managers = new ArrayList<>(); |
| 1980 | synchronized (service.mLock) { |
Hyundo Moon | 7d3ab33 | 2019-10-16 11:09:56 +0900 | [diff] [blame] | 1981 | for (ManagerRecord managerRecord : mUserRecord.mManagerRecords) { |
| 1982 | managers.add(managerRecord.mManager); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1983 | } |
| 1984 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1985 | for (IMediaRouter2Manager manager : managers) { |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1986 | try { |
Hyundo Moon | ac9df10 | 2020-06-12 19:36:10 +0900 | [diff] [blame] | 1987 | manager.notifyPreferredFeaturesChanged(routerPackageName, preferredFeatures); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1988 | } catch (RemoteException ex) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 1989 | Slog.w(TAG, "Failed to notify preferred features changed." |
| 1990 | + " Manager probably died.", ex); |
Kyunglyul Hyun | dafac54 | 2019-04-29 18:07:21 +0900 | [diff] [blame] | 1991 | } |
| 1992 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 1993 | } |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 1994 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 1995 | private void notifyRequestFailedToManager(@NonNull IMediaRouter2Manager manager, |
| 1996 | int requestId, int reason) { |
| 1997 | try { |
| 1998 | manager.notifyRequestFailed(requestId, reason); |
| 1999 | } catch (RemoteException ex) { |
| 2000 | Slog.w(TAG, "Failed to notify manager of the request failure." |
| 2001 | + " Manager probably died.", ex); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | private void updateDiscoveryPreferenceOnHandler() { |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2006 | MediaRouter2ServiceImpl service = mServiceRef.get(); |
| 2007 | if (service == null) { |
| 2008 | return; |
| 2009 | } |
| 2010 | List<RouteDiscoveryPreference> discoveryPreferences = new ArrayList<>(); |
| 2011 | synchronized (service.mLock) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 2012 | for (RouterRecord routerRecord : mUserRecord.mRouterRecords) { |
| 2013 | discoveryPreferences.add(routerRecord.mDiscoveryPreference); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2014 | } |
| 2015 | mUserRecord.mCompositeDiscoveryPreference = |
| 2016 | new RouteDiscoveryPreference.Builder(discoveryPreferences) |
| 2017 | .build(); |
| 2018 | } |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 2019 | for (MediaRoute2Provider provider : mRouteProviders) { |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 2020 | provider.updateDiscoveryPreference(mUserRecord.mCompositeDiscoveryPreference); |
| 2021 | } |
| 2022 | } |
| 2023 | |
Hyundo Moon | e962ed4 | 2020-02-16 19:47:41 +0900 | [diff] [blame] | 2024 | private MediaRoute2Provider findProvider(@Nullable String providerId) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 2025 | for (MediaRoute2Provider provider : mRouteProviders) { |
Kyunglyul Hyun | 7af7301 | 2019-10-11 20:01:30 +0900 | [diff] [blame] | 2026 | if (TextUtils.equals(provider.getUniqueId(), providerId)) { |
Kyunglyul Hyun | 13bc105 | 2019-05-31 17:00:36 +0900 | [diff] [blame] | 2027 | return provider; |
| 2028 | } |
| 2029 | } |
| 2030 | return null; |
| 2031 | } |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2032 | |
| 2033 | final class SessionCreationRequest { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 2034 | public final RouterRecord mRouterRecord; |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 2035 | public final long mUniqueRequestId; |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 2036 | public final MediaRoute2Info mRoute; |
| 2037 | public final ManagerRecord mRequestedManagerRecord; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2038 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 2039 | // requestedManagerRecord is not null only when the request is made by manager. |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 2040 | SessionCreationRequest(@NonNull RouterRecord routerRecord, long uniqueRequestId, |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 2041 | @NonNull MediaRoute2Info route, |
| 2042 | @Nullable ManagerRecord requestedManagerRecord) { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 2043 | mRouterRecord = routerRecord; |
Kyunglyul Hyun | 3b27d9d | 2020-03-31 19:56:10 +0900 | [diff] [blame] | 2044 | mUniqueRequestId = uniqueRequestId; |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 2045 | mRoute = route; |
| 2046 | mRequestedManagerRecord = requestedManagerRecord; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 2047 | } |
| 2048 | } |
Kyunglyul Hyun | 5aa6082 | 2019-04-18 16:57:53 +0900 | [diff] [blame] | 2049 | } |
| 2050 | } |