From 505460a2a0bb206bbaf592d931805101125f625e Mon Sep 17 00:00:00 2001 From: Iván Budnik Date: Thu, 28 Dec 2023 22:09:03 +0000 Subject: Preload system routes on ProxyMediaRouter2 instances Preloading system routes avoids an initial incongruent state where MediaRouter2 does not store any routes, which happens between instantiation and receiving the first batch of route updates from system server. Regular MediaRouter2 instances already preload system routes. Test: atest CtsMediaBetterTogetherTestCases Bug: 316584471 Change-Id: I62fd163108737a40bb2f82050e46d4c2c4c3f142 --- media/java/android/media/MediaRouter2.java | 36 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java index 5e235515c852..89792c7638cb 100644 --- a/media/java/android/media/MediaRouter2.java +++ b/media/java/android/media/MediaRouter2.java @@ -18,8 +18,8 @@ package android.media; import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; import static com.android.media.flags.Flags.FLAG_ENABLE_BUILT_IN_SPEAKER_ROUTE_SUITABILITY_STATUSES; -import static com.android.media.flags.Flags.FLAG_ENABLE_RLP_CALLBACKS_IN_MEDIA_ROUTER2; import static com.android.media.flags.Flags.FLAG_ENABLE_CROSS_USER_ROUTING_IN_MEDIA_ROUTER2; +import static com.android.media.flags.Flags.FLAG_ENABLE_RLP_CALLBACKS_IN_MEDIA_ROUTER2; import android.Manifest; import android.annotation.CallbackExecutor; @@ -344,25 +344,13 @@ public final class MediaRouter2 { mImpl = new LocalMediaRouter2Impl(mContext.getPackageName()); mHandler = new Handler(Looper.getMainLooper()); - List currentSystemRoutes = null; - try { - currentSystemRoutes = mMediaRouterService.getSystemRoutes(); - } catch (RemoteException ex) { - ex.rethrowFromSystemServer(); - } - - if (currentSystemRoutes == null || currentSystemRoutes.isEmpty()) { - throw new RuntimeException("Null or empty currentSystemRoutes. Something is wrong."); - } + loadSystemRoutes(); RoutingSessionInfo currentSystemSessionInfo = mImpl.getSystemSessionInfo(); if (currentSystemSessionInfo == null) { throw new RuntimeException("Null currentSystemSessionInfo. Something is wrong."); } - for (MediaRoute2Info route : currentSystemRoutes) { - mRoutes.put(route.getId(), route); - } mSystemController = new SystemRoutingController(currentSystemSessionInfo); } @@ -374,6 +362,8 @@ public final class MediaRouter2 { IMediaRouterService.Stub.asInterface( ServiceManager.getService(Context.MEDIA_ROUTER_SERVICE)); + loadSystemRoutes(); + mSystemController = new SystemRoutingController( ProxyMediaRouter2Impl.getSystemSessionInfoImpl( @@ -381,6 +371,24 @@ public final class MediaRouter2 { mImpl = new ProxyMediaRouter2Impl(context, clientPackageName, user); } + @GuardedBy("mLock") + private void loadSystemRoutes() { + List currentSystemRoutes = null; + try { + currentSystemRoutes = mMediaRouterService.getSystemRoutes(); + } catch (RemoteException ex) { + ex.rethrowFromSystemServer(); + } + + if (currentSystemRoutes == null || currentSystemRoutes.isEmpty()) { + throw new RuntimeException("Null or empty currentSystemRoutes. Something is wrong."); + } + + for (MediaRoute2Info route : currentSystemRoutes) { + mRoutes.put(route.getId(), route); + } + } + /** * Gets the client package name of the app which this media router controls. * -- cgit v1.2.3-59-g8ed1b