From 9b1b98e9dd25cbcfa31c007d70de8b1a51f7e4a4 Mon Sep 17 00:00:00 2001 From: Alex Dadukin Date: Thu, 8 Feb 2024 09:54:50 +0000 Subject: Add package name to LocalRouter#getSystemSessionInfo Test: atest MediaRouter2Test SystemMediaRouter2Test Bug: b/324359100 Change-Id: I47522397fb1885d94b833892de344f26fe7181ba --- media/java/android/media/MediaRouter2.java | 50 ++++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java index 425db06ce55f..f09ceb8eb510 100644 --- a/media/java/android/media/MediaRouter2.java +++ b/media/java/android/media/MediaRouter2.java @@ -868,7 +868,8 @@ public final class MediaRouter2 { updateRoutesOnHandler(currentRoutes); RoutingSessionInfo oldInfo = mSystemController.getRoutingSessionInfo(); - mSystemController.setRoutingSessionInfo(currentSystemSessionInfo); + mSystemController.setRoutingSessionInfo(ensureClientPackageNameForSystemSession( + currentSystemSessionInfo, mContext.getPackageName())); if (!oldInfo.equals(currentSystemSessionInfo)) { notifyControllerUpdated(mSystemController); } @@ -1291,6 +1292,25 @@ public final class MediaRouter2 { } } + /** + * Sets the routing session's {@linkplain RoutingSessionInfo#getClientPackageName() client + * package name} to {@code packageName} if empty and returns the session. + * + *

This method must only be used for {@linkplain RoutingSessionInfo#isSystemSession() + * system routing sessions}. + */ + private static RoutingSessionInfo ensureClientPackageNameForSystemSession( + @NonNull RoutingSessionInfo sessionInfo, @NonNull String packageName) { + if (!sessionInfo.isSystemSession() + || !TextUtils.isEmpty(sessionInfo.getClientPackageName())) { + return sessionInfo; + } + + return new RoutingSessionInfo.Builder(sessionInfo) + .setClientPackageName(packageName) + .build(); + } + /** Callback for receiving events about media route discovery. */ public abstract static class RouteCallback { /** @@ -2658,25 +2678,6 @@ public final class MediaRouter2 { } } - /** - * Sets the routing session's {@linkplain RoutingSessionInfo#getClientPackageName() client - * package name} to {@link #mClientPackageName} if empty and returns the session. - * - *

This method must only be used for {@linkplain RoutingSessionInfo#isSystemSession() - * system routing sessions}. - */ - private RoutingSessionInfo ensureClientPackageNameForSystemSession( - RoutingSessionInfo sessionInfo) { - if (!sessionInfo.isSystemSession() - || !TextUtils.isEmpty(sessionInfo.getClientPackageName())) { - return sessionInfo; - } - - return new RoutingSessionInfo.Builder(sessionInfo) - .setClientPackageName(mClientPackageName) - .build(); - } - /** * Requests the release of a {@linkplain RoutingSessionInfo routing session}. Calls {@link * #onSessionReleasedOnHandler(RoutingSessionInfo)} on success. @@ -2765,7 +2766,7 @@ public final class MediaRouter2 { RoutingController oldController; if (oldSession.isSystemSession()) { mSystemController.setRoutingSessionInfo( - ensureClientPackageNameForSystemSession(oldSession)); + ensureClientPackageNameForSystemSession(oldSession, mClientPackageName)); oldController = mSystemController; } else { oldController = new RoutingController(oldSession); @@ -2774,7 +2775,7 @@ public final class MediaRouter2 { RoutingController newController; if (newSession.isSystemSession()) { mSystemController.setRoutingSessionInfo( - ensureClientPackageNameForSystemSession(newSession)); + ensureClientPackageNameForSystemSession(newSession, mClientPackageName)); newController = mSystemController; } else { newController = new RoutingController(newSession); @@ -2801,7 +2802,7 @@ public final class MediaRouter2 { RoutingController controller; if (session.isSystemSession()) { mSystemController.setRoutingSessionInfo( - ensureClientPackageNameForSystemSession(session)); + ensureClientPackageNameForSystemSession(session, mClientPackageName)); controller = mSystemController; } else { controller = new RoutingController(session); @@ -3059,7 +3060,8 @@ public final class MediaRouter2 { public RoutingSessionInfo getSystemSessionInfo() { RoutingSessionInfo currentSystemSessionInfo = null; try { - currentSystemSessionInfo = mMediaRouterService.getSystemSessionInfo(); + currentSystemSessionInfo = ensureClientPackageNameForSystemSession( + mMediaRouterService.getSystemSessionInfo(), mContext.getPackageName()); } catch (RemoteException ex) { ex.rethrowFromSystemServer(); } -- cgit v1.2.3-59-g8ed1b