Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [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 | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 24 | import android.media.IMediaRoute2ProviderService; |
| 25 | import android.media.IMediaRoute2ProviderServiceCallback; |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 26 | import android.media.MediaRoute2ProviderInfo; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 27 | import android.media.MediaRoute2ProviderService; |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 28 | import android.media.RouteDiscoveryPreference; |
Hyundo Moon | f829e6f | 2020-01-11 19:31:35 +0900 | [diff] [blame] | 29 | import android.media.RoutingSessionInfo; |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 30 | import android.os.Bundle; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.IBinder.DeathRecipient; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 34 | import android.os.Looper; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.UserHandle; |
| 37 | import android.util.Log; |
| 38 | import android.util.Slog; |
| 39 | |
| 40 | import java.io.PrintWriter; |
| 41 | import java.lang.ref.WeakReference; |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 42 | import java.util.Objects; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 45 | * Maintains a connection to a particular {@link MediaRoute2ProviderService}. |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 46 | */ |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 47 | final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider |
| 48 | implements ServiceConnection { |
| 49 | private static final String TAG = "MR2ProviderSvcProxy"; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 50 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 51 | |
| 52 | private final Context mContext; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 53 | private final int mUserId; |
| 54 | private final Handler mHandler; |
| 55 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 56 | // Connection state |
| 57 | private boolean mRunning; |
| 58 | private boolean mBound; |
| 59 | private Connection mActiveConnection; |
| 60 | private boolean mConnectionReady; |
| 61 | |
Kyunglyul Hyun | f2b19c8b | 2020-05-18 18:29:11 +0900 | [diff] [blame] | 62 | private RouteDiscoveryPreference mLastDiscoveryPreference = null; |
Kyunglyul Hyun | bab0cda | 2020-03-10 20:59:49 +0900 | [diff] [blame] | 63 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 64 | MediaRoute2ProviderServiceProxy(@NonNull Context context, @NonNull ComponentName componentName, |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 65 | int userId) { |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 66 | super(componentName); |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 67 | mContext = Objects.requireNonNull(context, "Context must not be null."); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 68 | mUserId = userId; |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 69 | mHandler = new Handler(Looper.myLooper()); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public void dump(PrintWriter pw, String prefix) { |
| 73 | pw.println(prefix + "Proxy"); |
| 74 | pw.println(prefix + " mUserId=" + mUserId); |
| 75 | pw.println(prefix + " mRunning=" + mRunning); |
| 76 | pw.println(prefix + " mBound=" + mBound); |
| 77 | pw.println(prefix + " mActiveConnection=" + mActiveConnection); |
| 78 | pw.println(prefix + " mConnectionReady=" + mConnectionReady); |
| 79 | } |
| 80 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 81 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 82 | public void requestCreateSession(long requestId, String packageName, String routeId, |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 83 | Bundle sessionHints) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 84 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 85 | mActiveConnection.requestCreateSession(requestId, packageName, routeId, sessionHints); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 86 | updateBinding(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 91 | public void releaseSession(long requestId, String sessionId) { |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 92 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 93 | mActiveConnection.releaseSession(requestId, sessionId); |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 94 | updateBinding(); |
| 95 | } |
| 96 | } |
| 97 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 98 | @Override |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 99 | public void updateDiscoveryPreference(RouteDiscoveryPreference discoveryPreference) { |
Kyunglyul Hyun | f2b19c8b | 2020-05-18 18:29:11 +0900 | [diff] [blame] | 100 | mLastDiscoveryPreference = discoveryPreference; |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 101 | if (mConnectionReady) { |
| 102 | mActiveConnection.updateDiscoveryPreference(discoveryPreference); |
| 103 | updateBinding(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 108 | public void selectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 109 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 110 | mActiveConnection.selectRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 115 | public void deselectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 116 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 117 | mActiveConnection.deselectRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 122 | public void transferToRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 123 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 124 | mActiveConnection.transferToRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 128 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 129 | public void setRouteVolume(long requestId, String routeId, int volume) { |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 130 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 131 | mActiveConnection.setRouteVolume(requestId, routeId, volume); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 132 | updateBinding(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 137 | public void setSessionVolume(long requestId, String sessionId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 138 | if (mConnectionReady) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 139 | mActiveConnection.setSessionVolume(requestId, sessionId, volume); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 140 | updateBinding(); |
| 141 | } |
| 142 | } |
| 143 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 144 | public boolean hasComponentName(String packageName, String className) { |
| 145 | return mComponentName.getPackageName().equals(packageName) |
| 146 | && mComponentName.getClassName().equals(className); |
| 147 | } |
| 148 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 149 | public void start() { |
| 150 | if (!mRunning) { |
| 151 | if (DEBUG) { |
| 152 | Slog.d(TAG, this + ": Starting"); |
| 153 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 154 | mRunning = true; |
| 155 | updateBinding(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public void stop() { |
| 160 | if (mRunning) { |
| 161 | if (DEBUG) { |
| 162 | Slog.d(TAG, this + ": Stopping"); |
| 163 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 164 | mRunning = false; |
| 165 | updateBinding(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public void rebindIfDisconnected() { |
Kyunglyul Hyun | 89e2ee9 | 2020-04-10 20:17:04 +0900 | [diff] [blame] | 170 | //TODO: When we are connecting to the service, calling this will unbind and bind again. |
| 171 | // We'd better not unbind if we are connecting. |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 172 | if (mActiveConnection == null && shouldBind()) { |
| 173 | unbind(); |
| 174 | bind(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void updateBinding() { |
| 179 | if (shouldBind()) { |
| 180 | bind(); |
| 181 | } else { |
| 182 | unbind(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private boolean shouldBind() { |
Kyunglyul Hyun | 6c5d7dc | 2019-04-24 15:57:07 +0900 | [diff] [blame] | 187 | //TODO: Binding could be delayed until it's necessary. |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 188 | if (mRunning) { |
| 189 | return true; |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | private void bind() { |
| 195 | if (!mBound) { |
| 196 | if (DEBUG) { |
| 197 | Slog.d(TAG, this + ": Binding"); |
| 198 | } |
| 199 | |
| 200 | Intent service = new Intent(MediaRoute2ProviderService.SERVICE_INTERFACE); |
| 201 | service.setComponent(mComponentName); |
| 202 | try { |
| 203 | mBound = mContext.bindServiceAsUser(service, this, |
| 204 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 205 | new UserHandle(mUserId)); |
| 206 | if (!mBound && DEBUG) { |
| 207 | Slog.d(TAG, this + ": Bind failed"); |
| 208 | } |
| 209 | } catch (SecurityException ex) { |
| 210 | if (DEBUG) { |
| 211 | Slog.d(TAG, this + ": Bind failed", ex); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private void unbind() { |
| 218 | if (mBound) { |
| 219 | if (DEBUG) { |
| 220 | Slog.d(TAG, this + ": Unbinding"); |
| 221 | } |
| 222 | |
| 223 | mBound = false; |
| 224 | disconnect(); |
| 225 | mContext.unbindService(this); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 231 | if (DEBUG) { |
| 232 | Slog.d(TAG, this + ": Connected"); |
| 233 | } |
| 234 | |
| 235 | if (mBound) { |
| 236 | disconnect(); |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 237 | IMediaRoute2ProviderService serviceBinder = |
| 238 | IMediaRoute2ProviderService.Stub.asInterface(service); |
| 239 | if (serviceBinder != null) { |
| 240 | Connection connection = new Connection(serviceBinder); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 241 | if (connection.register()) { |
| 242 | mActiveConnection = connection; |
| 243 | } else { |
| 244 | if (DEBUG) { |
| 245 | Slog.d(TAG, this + ": Registration failed"); |
| 246 | } |
| 247 | } |
| 248 | } else { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 249 | Slog.e(TAG, this + ": Service returned invalid binder"); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void onServiceDisconnected(ComponentName name) { |
| 256 | if (DEBUG) { |
| 257 | Slog.d(TAG, this + ": Service disconnected"); |
| 258 | } |
| 259 | disconnect(); |
| 260 | } |
| 261 | |
Hyundo Moon | 7294e5f | 2020-01-29 13:26:14 +0900 | [diff] [blame] | 262 | @Override |
| 263 | public void onBindingDied(ComponentName name) { |
| 264 | if (DEBUG) { |
| 265 | Slog.d(TAG, this + ": Service binding died"); |
| 266 | } |
Hyundo Moon | 7294e5f | 2020-01-29 13:26:14 +0900 | [diff] [blame] | 267 | if (shouldBind()) { |
| 268 | unbind(); |
| 269 | bind(); |
| 270 | } |
| 271 | } |
| 272 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 273 | private void onConnectionReady(Connection connection) { |
| 274 | if (mActiveConnection == connection) { |
| 275 | mConnectionReady = true; |
Kyunglyul Hyun | f2b19c8b | 2020-05-18 18:29:11 +0900 | [diff] [blame] | 276 | if (mLastDiscoveryPreference != null) { |
| 277 | updateDiscoveryPreference(mLastDiscoveryPreference); |
Kyunglyul Hyun | bab0cda | 2020-03-10 20:59:49 +0900 | [diff] [blame] | 278 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private void onConnectionDied(Connection connection) { |
| 283 | if (mActiveConnection == connection) { |
| 284 | if (DEBUG) { |
| 285 | Slog.d(TAG, this + ": Service connection died"); |
| 286 | } |
| 287 | disconnect(); |
| 288 | } |
| 289 | } |
| 290 | |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 291 | private void onProviderStateUpdated(Connection connection, |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 292 | MediaRoute2ProviderInfo providerInfo) { |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 293 | if (mActiveConnection != connection) { |
| 294 | return; |
| 295 | } |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 296 | if (DEBUG) { |
| 297 | Slog.d(TAG, this + ": State changed "); |
| 298 | } |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 299 | setAndNotifyProviderState(providerInfo); |
Kyunglyul Hyun | 23b3aaa | 2019-08-06 11:17:16 +0900 | [diff] [blame] | 300 | } |
| 301 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 302 | private void onSessionCreated(Connection connection, long requestId, |
| 303 | RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 304 | if (mActiveConnection != connection) { |
| 305 | return; |
| 306 | } |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 307 | |
| 308 | if (sessionInfo == null) { |
| 309 | Slog.w(TAG, "onSessionCreated: Ignoring null sessionInfo sent from " + mComponentName); |
| 310 | return; |
Kyunglyul Hyun | 138246e | 2020-01-02 18:19:52 +0900 | [diff] [blame] | 311 | } |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 312 | |
Kyunglyul Hyun | e2dbbf7 | 2020-04-29 22:40:59 +0900 | [diff] [blame] | 313 | sessionInfo = updateSessionInfo(sessionInfo); |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 314 | |
| 315 | boolean duplicateSessionAlreadyExists = false; |
| 316 | synchronized (mLock) { |
| 317 | for (int i = 0; i < mSessionInfos.size(); i++) { |
| 318 | if (mSessionInfos.get(i).getId().equals(sessionInfo.getId())) { |
| 319 | duplicateSessionAlreadyExists = true; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | mSessionInfos.add(sessionInfo); |
| 324 | } |
| 325 | |
| 326 | if (duplicateSessionAlreadyExists) { |
| 327 | Slog.w(TAG, "onSessionCreated: Duplicate session already exists. Ignoring."); |
| 328 | return; |
| 329 | } |
| 330 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 331 | mCallback.onSessionCreated(this, requestId, sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 334 | private void onSessionUpdated(Connection connection, RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 335 | if (mActiveConnection != connection) { |
| 336 | return; |
| 337 | } |
| 338 | if (sessionInfo == null) { |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 339 | Slog.w(TAG, "onSessionUpdated: Ignoring null sessionInfo sent from " |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 340 | + mComponentName); |
| 341 | return; |
| 342 | } |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 343 | |
Kyunglyul Hyun | e2dbbf7 | 2020-04-29 22:40:59 +0900 | [diff] [blame] | 344 | sessionInfo = updateSessionInfo(sessionInfo); |
Hyundo Moon | b26c4b2 | 2020-01-08 19:44:43 +0900 | [diff] [blame] | 345 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 346 | boolean found = false; |
| 347 | synchronized (mLock) { |
| 348 | for (int i = 0; i < mSessionInfos.size(); i++) { |
| 349 | if (mSessionInfos.get(i).getId().equals(sessionInfo.getId())) { |
| 350 | mSessionInfos.set(i, sessionInfo); |
| 351 | found = true; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (!found) { |
| 358 | Slog.w(TAG, "onSessionUpdated: Matching session info not found"); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | mCallback.onSessionUpdated(this, sessionInfo); |
| 363 | } |
| 364 | |
| 365 | private void onSessionReleased(Connection connection, RoutingSessionInfo sessionInfo) { |
| 366 | if (mActiveConnection != connection) { |
| 367 | return; |
| 368 | } |
| 369 | if (sessionInfo == null) { |
| 370 | Slog.w(TAG, "onSessionReleased: Ignoring null sessionInfo sent from " + mComponentName); |
| 371 | return; |
| 372 | } |
| 373 | |
Kyunglyul Hyun | e2dbbf7 | 2020-04-29 22:40:59 +0900 | [diff] [blame] | 374 | sessionInfo = updateSessionInfo(sessionInfo); |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 375 | |
| 376 | boolean found = false; |
| 377 | synchronized (mLock) { |
| 378 | for (int i = 0; i < mSessionInfos.size(); i++) { |
| 379 | if (mSessionInfos.get(i).getId().equals(sessionInfo.getId())) { |
| 380 | mSessionInfos.remove(i); |
| 381 | found = true; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!found) { |
| 388 | Slog.w(TAG, "onSessionReleased: Matching session info not found"); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | mCallback.onSessionReleased(this, sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Kyunglyul Hyun | e2dbbf7 | 2020-04-29 22:40:59 +0900 | [diff] [blame] | 395 | private RoutingSessionInfo updateSessionInfo(RoutingSessionInfo sessionInfo) { |
| 396 | return new RoutingSessionInfo.Builder(sessionInfo) |
| 397 | .setOwnerPackageName(mComponentName.getPackageName()) |
| 398 | .setProviderId(getUniqueId()) |
| 399 | .build(); |
| 400 | } |
| 401 | |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 402 | private void onRequestFailed(Connection connection, long requestId, int reason) { |
| 403 | if (mActiveConnection != connection) { |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (requestId == MediaRoute2ProviderService.REQUEST_ID_NONE) { |
| 408 | Slog.w(TAG, "onRequestFailed: Ignoring requestId REQUEST_ID_NONE"); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | mCallback.onRequestFailed(this, requestId, reason); |
| 413 | } |
| 414 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 415 | private void disconnect() { |
| 416 | if (mActiveConnection != null) { |
| 417 | mConnectionReady = false; |
| 418 | mActiveConnection.dispose(); |
| 419 | mActiveConnection = null; |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 420 | setAndNotifyProviderState(null); |
Kyunglyul Hyun | 47d289e | 2020-02-07 19:24:08 +0900 | [diff] [blame] | 421 | synchronized (mLock) { |
| 422 | for (RoutingSessionInfo sessionInfo : mSessionInfos) { |
| 423 | mCallback.onSessionReleased(this, sessionInfo); |
| 424 | } |
| 425 | mSessionInfos.clear(); |
| 426 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public String toString() { |
| 432 | return "Service connection " + mComponentName.flattenToShortString(); |
| 433 | } |
| 434 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 435 | private final class Connection implements DeathRecipient { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 436 | private final IMediaRoute2ProviderService mService; |
| 437 | private final ServiceCallbackStub mCallbackStub; |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 438 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 439 | Connection(IMediaRoute2ProviderService serviceBinder) { |
| 440 | mService = serviceBinder; |
| 441 | mCallbackStub = new ServiceCallbackStub(this); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | public boolean register() { |
| 445 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 446 | mService.asBinder().linkToDeath(this, 0); |
| 447 | mService.setCallback(mCallbackStub); |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 448 | mHandler.post(() -> onConnectionReady(Connection.this)); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 449 | return true; |
| 450 | } catch (RemoteException ex) { |
| 451 | binderDied(); |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | public void dispose() { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 457 | mService.asBinder().unlinkToDeath(this, 0); |
| 458 | mCallbackStub.dispose(); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 461 | public void requestCreateSession(long requestId, String packageName, String routeId, |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 462 | Bundle sessionHints) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 463 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 464 | mService.requestCreateSession(requestId, packageName, routeId, sessionHints); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 465 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 466 | Slog.e(TAG, "requestCreateSession: Failed to deliver request."); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 470 | public void releaseSession(long requestId, String sessionId) { |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 471 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 472 | mService.releaseSession(requestId, sessionId); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 473 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 474 | Slog.e(TAG, "releaseSession: Failed to deliver request."); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 478 | public void updateDiscoveryPreference(RouteDiscoveryPreference discoveryPreference) { |
| 479 | try { |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 480 | mService.updateDiscoveryPreference(discoveryPreference); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 481 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 482 | Slog.e(TAG, "updateDiscoveryPreference: Failed to deliver request."); |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 486 | public void selectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | caae8dc | 2019-04-29 15:45:23 +0900 | [diff] [blame] | 487 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 488 | mService.selectRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 489 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 490 | Slog.e(TAG, "selectRoute: Failed to deliver request.", ex); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 494 | public void deselectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 495 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 496 | mService.deselectRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 497 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 498 | Slog.e(TAG, "deselectRoute: Failed to deliver request.", ex); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 502 | public void transferToRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 503 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 504 | mService.transferToRoute(requestId, sessionId, routeId); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 505 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 506 | Slog.e(TAG, "transferToRoute: Failed to deliver request.", ex); |
Kyunglyul Hyun | a0d4741 | 2019-07-01 11:14:24 +0900 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 510 | public void setRouteVolume(long requestId, String routeId, int volume) { |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 511 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 512 | mService.setRouteVolume(requestId, routeId, volume); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 513 | } catch (RemoteException ex) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 514 | Slog.e(TAG, "setRouteVolume: Failed to deliver request.", ex); |
| 515 | } |
| 516 | } |
| 517 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 518 | public void setSessionVolume(long requestId, String sessionId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 519 | try { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 520 | mService.setSessionVolume(requestId, sessionId, volume); |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 521 | } catch (RemoteException ex) { |
| 522 | Slog.e(TAG, "setSessionVolume: Failed to deliver request.", ex); |
Kyunglyul Hyun | 5a8dedc | 2019-10-10 14:09:44 +0900 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 526 | @Override |
| 527 | public void binderDied() { |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 528 | mHandler.post(() -> onConnectionDied(Connection.this)); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 531 | void postProviderStateUpdated(MediaRoute2ProviderInfo providerInfo) { |
| 532 | mHandler.post(() -> onProviderStateUpdated(Connection.this, providerInfo)); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 533 | } |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 534 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 535 | void postSessionCreated(long requestId, RoutingSessionInfo sessionInfo) { |
| 536 | mHandler.post(() -> onSessionCreated(Connection.this, requestId, sessionInfo)); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 537 | } |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 538 | |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 539 | void postSessionUpdated(RoutingSessionInfo sessionInfo) { |
| 540 | mHandler.post(() -> onSessionUpdated(Connection.this, sessionInfo)); |
| 541 | } |
| 542 | |
| 543 | void postSessionReleased(RoutingSessionInfo sessionInfo) { |
| 544 | mHandler.post(() -> onSessionReleased(Connection.this, sessionInfo)); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 545 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 546 | |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 547 | void postRequestFailed(long requestId, int reason) { |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 548 | mHandler.post(() -> onRequestFailed(Connection.this, requestId, reason)); |
| 549 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 552 | private static final class ServiceCallbackStub extends |
| 553 | IMediaRoute2ProviderServiceCallback.Stub { |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 554 | private final WeakReference<Connection> mConnectionRef; |
| 555 | |
Hyundo Moon | 5633749 | 2020-02-16 16:19:54 +0900 | [diff] [blame] | 556 | ServiceCallbackStub(Connection connection) { |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 557 | mConnectionRef = new WeakReference<>(connection); |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | public void dispose() { |
| 561 | mConnectionRef.clear(); |
| 562 | } |
| 563 | |
| 564 | @Override |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 565 | public void updateState(MediaRoute2ProviderInfo providerInfo) { |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 566 | Connection connection = mConnectionRef.get(); |
| 567 | if (connection != null) { |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 568 | connection.postProviderStateUpdated(providerInfo); |
Kyunglyul Hyun | 3aedf02 | 2019-04-15 16:38:19 +0900 | [diff] [blame] | 569 | } |
| 570 | } |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 571 | |
| 572 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 573 | public void notifySessionCreated(long requestId, RoutingSessionInfo sessionInfo) { |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 574 | Connection connection = mConnectionRef.get(); |
| 575 | if (connection != null) { |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 576 | connection.postSessionCreated(requestId, sessionInfo); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 577 | } |
| 578 | } |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 579 | |
| 580 | @Override |
Hyundo Moon | 6e30f1b | 2020-01-13 19:50:27 +0900 | [diff] [blame] | 581 | public void notifySessionUpdated(RoutingSessionInfo sessionInfo) { |
| 582 | Connection connection = mConnectionRef.get(); |
| 583 | if (connection != null) { |
| 584 | connection.postSessionUpdated(sessionInfo); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public void notifySessionReleased(RoutingSessionInfo sessionInfo) { |
| 590 | Connection connection = mConnectionRef.get(); |
| 591 | if (connection != null) { |
| 592 | connection.postSessionReleased(sessionInfo); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 593 | } |
| 594 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 595 | |
| 596 | @Override |
| 597 | public void notifyRequestFailed(long requestId, int reason) { |
| 598 | Connection connection = mConnectionRef.get(); |
| 599 | if (connection != null) { |
Hyundo Moon | 7c2059c | 2020-02-27 19:01:52 +0900 | [diff] [blame] | 600 | connection.postRequestFailed(requestId, reason); |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 601 | } |
| 602 | } |
Kyunglyul Hyun | d51666d | 2019-04-11 04:08:40 +0000 | [diff] [blame] | 603 | } |
| 604 | } |