Add package name to LocalRouter#getSystemSessionInfo

Test: atest MediaRouter2Test SystemMediaRouter2Test
Bug: b/324359100
Change-Id: I47522397fb1885d94b833892de344f26fe7181ba
diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java
index 425db06..f09ceb8 100644
--- a/media/java/android/media/MediaRouter2.java
+++ b/media/java/android/media/MediaRouter2.java
@@ -868,7 +868,8 @@
         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 @@
         }
     }
 
+    /**
+     * Sets the routing session's {@linkplain RoutingSessionInfo#getClientPackageName() client
+     * package name} to {@code packageName} if empty and returns the session.
+     *
+     * <p>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 {
         /**
@@ -2659,25 +2679,6 @@
         }
 
         /**
-         * Sets the routing session's {@linkplain RoutingSessionInfo#getClientPackageName() client
-         * package name} to {@link #mClientPackageName} if empty and returns the session.
-         *
-         * <p>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 @@
             RoutingController oldController;
             if (oldSession.isSystemSession()) {
                 mSystemController.setRoutingSessionInfo(
-                        ensureClientPackageNameForSystemSession(oldSession));
+                        ensureClientPackageNameForSystemSession(oldSession, mClientPackageName));
                 oldController = mSystemController;
             } else {
                 oldController = new RoutingController(oldSession);
@@ -2774,7 +2775,7 @@
             RoutingController newController;
             if (newSession.isSystemSession()) {
                 mSystemController.setRoutingSessionInfo(
-                        ensureClientPackageNameForSystemSession(newSession));
+                        ensureClientPackageNameForSystemSession(newSession, mClientPackageName));
                 newController = mSystemController;
             } else {
                 newController = new RoutingController(newSession);
@@ -2801,7 +2802,7 @@
             RoutingController controller;
             if (session.isSystemSession()) {
                 mSystemController.setRoutingSessionInfo(
-                        ensureClientPackageNameForSystemSession(session));
+                        ensureClientPackageNameForSystemSession(session, mClientPackageName));
                 controller = mSystemController;
             } else {
                 controller = new RoutingController(session);
@@ -3059,7 +3060,8 @@
         public RoutingSessionInfo getSystemSessionInfo() {
             RoutingSessionInfo currentSystemSessionInfo = null;
             try {
-                currentSystemSessionInfo = mMediaRouterService.getSystemSessionInfo();
+                currentSystemSessionInfo = ensureClientPackageNameForSystemSession(
+                        mMediaRouterService.getSystemSessionInfo(), mContext.getPackageName());
             } catch (RemoteException ex) {
                 ex.rethrowFromSystemServer();
             }