From 9659c230910b1f7914e0df1c63dcc0980d658bee Mon Sep 17 00:00:00 2001 From: Hyundo Moon Date: Thu, 1 Jul 2021 09:56:55 +0900 Subject: [PATCH] Send features first and then routes when registering MR2Manager When a new MediaRouter2Manager is registered, system service first sends the routes, and then sends the preferred features of all media apps. This order was not a problem for MediaRouter2Manager, but it is a problem when creating system MediaRouter2. Here's why: As a background, system MR2 is for one media app, while MR2Manager is for all media apps. MR2 creates MR2Manager internally to communicate with the system media router service, and it 'filters' the information that are useful for the target app. When system MR2 is created, the system service sends the routes and preferred features of all media apps to the internal MR2Manager. When the routes arrive, system MR2 checks whether the routes match the media app's features. If there is no matching routes, the onRoutesAdded() callback is not called. However, since the features come AFTER the routes, the feature is yet empty at the point when routes arrive. This makes the filtered routes also as empty, in turn the onRoutesAdded() callback is never called for the initial routes. Bug: 192454011 Test: atest com.android.mediaroutertest.MediaRouter2ManagerTest atest android.media.cts.SystemMediaRouter2Test Change-Id: I2f6101ff86ec6501e8364c75a41b58f2a04ee212 --- .../android/server/media/MediaRouter2ServiceImpl.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index 168ca551317ab..7d08ad0a47d11 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -903,9 +903,9 @@ class MediaRouter2ServiceImpl { userRecord.mManagerRecords.add(managerRecord); mAllManagerRecords.put(binder, managerRecord); - userRecord.mHandler.sendMessage(obtainMessage(UserHandler::notifyRoutesToManager, - userRecord.mHandler, manager)); - + // Note: Features should be sent first before the routes. If not, the + // RouteCallback#onRoutesAdded() for system MR2 will never be called with initial routes + // due to the lack of features. for (RouterRecord routerRecord : userRecord.mRouterRecords) { // TODO: UserRecord <-> routerRecord, why do they reference each other? // How about removing mUserRecord from routerRecord? @@ -913,6 +913,9 @@ class MediaRouter2ServiceImpl { obtainMessage(UserHandler::notifyPreferredFeaturesChangedToManager, routerRecord.mUserRecord.mHandler, routerRecord, manager)); } + + userRecord.mHandler.sendMessage(obtainMessage(UserHandler::notifyRoutesToManager, + userRecord.mHandler, manager)); } private void unregisterManagerLocked(@NonNull IMediaRouter2Manager manager, boolean died) {