From 7423573ccd49f8bb45317889e5d837bd3dc855ee Mon Sep 17 00:00:00 2001 From: Santiago Seifert Date: Tue, 1 Oct 2024 12:30:30 +0000 Subject: Create ManagerRecord#notifyRoutesUpdated This change is part of a series of non-functional refactors to encapsulate binder operations in the record that corresponds to the target binder object. The objective is to improve the logging associated with binder failures by adding information that's available in the record. For example: Package names and ids. Bug: 360129098 Test: atest CtsMediaBetterTogetherTestCases MediaRouter2HostSideTest Flag: EXEMPT refactor Change-Id: Iba0897590d0129ffb0eaf8e152713d582be1fd1f --- .../server/media/MediaRouter2ServiceImpl.java | 38 ++++++++++------------ 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index e1b8e9f559ed..958d35e78ea2 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -1653,9 +1653,11 @@ class MediaRouter2ServiceImpl { manager)); } + List routes = + userRecord.mHandler.mLastNotifiedRoutesToPrivilegedRouters.values().stream() + .toList(); userRecord.mHandler.sendMessage( - obtainMessage( - UserHandler::notifyInitialRoutesToManager, userRecord.mHandler, manager)); + obtainMessage(ManagerRecord::notifyRoutesUpdated, managerRecord, routes)); } @GuardedBy("mLock") @@ -2433,6 +2435,19 @@ class MediaRouter2ServiceImpl { } } + /** + * Notifies the corresponding manager of the availability of the given routes. + * + * @param routes The routes available to the manager that corresponds to this record. + */ + public void notifyRoutesUpdated(List routes) { + try { + mManager.notifyRoutesUpdated(routes); + } catch (RemoteException ex) { + Slog.w(TAG, "Failed to notify routes. Manager probably died.", ex); + } + } + private void updateScanningState(@ScanningState int scanningState) { if (mScanningState == scanningState) { return; @@ -3269,25 +3284,6 @@ class MediaRouter2ServiceImpl { } } - /** - * Notifies {@code manager} with all known routes. This only happens once after {@code - * manager} is registered through {@link #registerManager(IMediaRouter2Manager, String) - * registerManager()}. - * - * @param manager {@link IMediaRouter2Manager} to be notified. - */ - private void notifyInitialRoutesToManager(@NonNull IMediaRouter2Manager manager) { - if (mLastNotifiedRoutesToPrivilegedRouters.isEmpty()) { - return; - } - try { - manager.notifyRoutesUpdated( - new ArrayList<>(mLastNotifiedRoutesToPrivilegedRouters.values())); - } catch (RemoteException ex) { - Slog.w(TAG, "Failed to notify all routes. Manager probably died.", ex); - } - } - private void notifyRoutesUpdatedToManagers( @NonNull List managers, @NonNull List routes) { -- cgit v1.2.3-59-g8ed1b From 6dd198b56ef598cacc9de72a4ce4b5b83a147a34 Mon Sep 17 00:00:00 2001 From: Santiago Seifert Date: Tue, 1 Oct 2024 18:36:12 +0000 Subject: Remove unnecessary notifyRoutesUpdatedToManagers It's mostly a duplicate of RouterRecord#notifyRoutesUpdated. Bug: 360129098 Test: atest CtsMediaBetterTogetherTestCases MediaRouter2HostSideTest Flag: EXEMPT refactor Change-Id: I6923845677176f3f3bd86905778834d565d25f55 --- .../server/media/MediaRouter2ServiceImpl.java | 24 +++++++--------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index 958d35e78ea2..a09702a19647 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -2776,18 +2776,20 @@ class MediaRouter2ServiceImpl { getRouterRecords(/* hasSystemRoutingPermission= */ true); List routerRecordsWithoutSystemRoutingPermission = getRouterRecords(/* hasSystemRoutingPermission= */ false); - List managers = getManagers(); + List managers = getManagerRecords(); // Managers receive all provider updates with all routes. - notifyRoutesUpdatedToManagers( - managers, new ArrayList<>(mLastNotifiedRoutesToPrivilegedRouters.values())); + List routesForPrivilegedRouters = + mLastNotifiedRoutesToPrivilegedRouters.values().stream().toList(); + for (ManagerRecord manager : managers) { + manager.notifyRoutesUpdated(routesForPrivilegedRouters); + } // Routers with system routing access (either via {@link MODIFY_AUDIO_ROUTING} or // {@link BLUETOOTH_CONNECT} + {@link BLUETOOTH_SCAN}) receive all provider updates // with all routes. notifyRoutesUpdatedToRouterRecords( - routerRecordsWithSystemRoutingPermission, - new ArrayList<>(mLastNotifiedRoutesToPrivilegedRouters.values())); + routerRecordsWithSystemRoutingPermission, routesForPrivilegedRouters); if (!isSystemProvider) { // Regular routers receive updates from all non-system providers with all non-system @@ -3284,18 +3286,6 @@ class MediaRouter2ServiceImpl { } } - private void notifyRoutesUpdatedToManagers( - @NonNull List managers, - @NonNull List routes) { - for (IMediaRouter2Manager manager : managers) { - try { - manager.notifyRoutesUpdated(routes); - } catch (RemoteException ex) { - Slog.w(TAG, "Failed to notify routes changed. Manager probably died.", ex); - } - } - } - private void notifySessionCreatedToManagers(long managerRequestId, @NonNull RoutingSessionInfo session) { int requesterId = toRequesterId(managerRequestId); -- cgit v1.2.3-59-g8ed1b From 945a81b5c0d22a3d698af5a5e6fcec8716fde8dd Mon Sep 17 00:00:00 2001 From: Santiago Seifert Date: Tue, 1 Oct 2024 18:44:27 +0000 Subject: Create ManagerRecord#notifySessionReleased This change is part of a series of non-functional refactors to encapsulate binder operations in the record that corresponds to the target binder object. The objective is to improve the logging associated with binder failures by adding information that's available in the record. For example: Package names and ids. Bug: 360129098 Test: atest CtsMediaBetterTogetherTestCases MediaRouter2HostSideTest Flag: EXEMPT refactor Change-Id: I00352a3974b6f60cbf0e75b6196375a53a7c2ed9 --- .../server/media/MediaRouter2ServiceImpl.java | 35 ++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index a09702a19647..3c8d07307b83 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -2448,6 +2448,22 @@ class MediaRouter2ServiceImpl { } } + /** + * Notifies the corresponding manager that the given session has been released. + * + * @param sessionInfo The released session info. + */ + public void notifySessionReleased(RoutingSessionInfo sessionInfo) { + try { + mManager.notifySessionReleased(sessionInfo); + } catch (RemoteException ex) { + Slog.w( + TAG, + "notifySessionReleasedToManagers: Failed to notify. Manager probably died.", + ex); + } + } + private void updateScanningState(@ScanningState int scanningState) { if (mScanningState == scanningState) { return; @@ -3110,8 +3126,10 @@ class MediaRouter2ServiceImpl { private void onSessionReleasedOnHandler(@NonNull MediaRoute2Provider provider, @NonNull RoutingSessionInfo sessionInfo) { - List managers = getManagers(); - notifySessionReleasedToManagers(managers, sessionInfo); + List managers = getManagerRecords(); + for (ManagerRecord manager : managers) { + manager.notifySessionReleased(sessionInfo); + } RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId()); if (routerRecord == null) { @@ -3316,19 +3334,6 @@ class MediaRouter2ServiceImpl { } } - private void notifySessionReleasedToManagers( - @NonNull List managers, - @NonNull RoutingSessionInfo sessionInfo) { - for (IMediaRouter2Manager manager : managers) { - try { - manager.notifySessionReleased(sessionInfo); - } catch (RemoteException ex) { - Slog.w(TAG, "notifySessionReleasedToManagers: " - + "Failed to notify. Manager probably died.", ex); - } - } - } - private void notifyDiscoveryPreferenceChangedToManager(@NonNull RouterRecord routerRecord, @NonNull IMediaRouter2Manager manager) { try { -- cgit v1.2.3-59-g8ed1b From 33b673c15a5370660f5a33ad92be8c5597de5c04 Mon Sep 17 00:00:00 2001 From: Santiago Seifert Date: Tue, 1 Oct 2024 19:04:26 +0000 Subject: Create ManagerRecord#notifySessionUpdated This change is part of a series of non-functional refactors to encapsulate binder operations in the record that corresponds to the target binder object. The objective is to improve the logging associated with binder failures by adding information that's available in the record. For example: Package names and ids. Bug: 360129098 Test: atest CtsMediaBetterTogetherTestCases MediaRouter2HostSideTest Flag: EXEMPT refactor Change-Id: I9a91fa8bafaceed50ededf9f81b6c1db22412e5e --- .../server/media/MediaRouter2ServiceImpl.java | 49 +++++++++------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index 3c8d07307b83..8b06dadbaeeb 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -2448,6 +2448,22 @@ class MediaRouter2ServiceImpl { } } + /** + * Notifies the corresponding manager of an update in the given session. + * + * @param sessionInfo The updated session info. + */ + public void notifySessionUpdated(RoutingSessionInfo sessionInfo) { + try { + mManager.notifySessionUpdated(sessionInfo); + } catch (RemoteException ex) { + Slog.w( + TAG, + "notifySessionUpdatedToManagers: Failed to notify. Manager probably died.", + ex); + } + } + /** * Notifies the corresponding manager that the given session has been released. * @@ -3101,8 +3117,10 @@ class MediaRouter2ServiceImpl { private void onSessionInfoChangedOnHandler(@NonNull MediaRoute2Provider provider, @NonNull RoutingSessionInfo sessionInfo) { - List managers = getManagers(); - notifySessionUpdatedToManagers(managers, sessionInfo); + List managers = getManagerRecords(); + for (ManagerRecord manager : managers) { + manager.notifySessionUpdated(sessionInfo); + } // For system provider, notify all routers. if (provider == mSystemProvider) { @@ -3204,20 +3222,6 @@ class MediaRouter2ServiceImpl { return true; } - private List getManagers() { - final List managers = new ArrayList<>(); - MediaRouter2ServiceImpl service = mServiceRef.get(); - if (service == null) { - return managers; - } - synchronized (service.mLock) { - for (ManagerRecord managerRecord : mUserRecord.mManagerRecords) { - managers.add(managerRecord.mManager); - } - } - return managers; - } - private List getRouterRecords() { MediaRouter2ServiceImpl service = mServiceRef.get(); if (service == null) { @@ -3321,19 +3325,6 @@ class MediaRouter2ServiceImpl { } } - private void notifySessionUpdatedToManagers( - @NonNull List managers, - @NonNull RoutingSessionInfo sessionInfo) { - for (IMediaRouter2Manager manager : managers) { - try { - manager.notifySessionUpdated(sessionInfo); - } catch (RemoteException ex) { - Slog.w(TAG, "notifySessionUpdatedToManagers: " - + "Failed to notify. Manager probably died.", ex); - } - } - } - private void notifyDiscoveryPreferenceChangedToManager(@NonNull RouterRecord routerRecord, @NonNull IMediaRouter2Manager manager) { try { -- cgit v1.2.3-59-g8ed1b