Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dd1ce567 authored by Santiago Seifert's avatar Santiago Seifert Committed by Android (Google) Code Review
Browse files

Merge changes I9a91fa8b,I00352a39,I69238456,Iba089759 into main

* changes:
  Create ManagerRecord#notifySessionUpdated
  Create ManagerRecord#notifySessionReleased
  Remove unnecessary notifyRoutesUpdatedToManagers
  Create ManagerRecord#notifyRoutesUpdated
parents 32cf19bb 33b673c1
Loading
Loading
Loading
Loading
+64 −82
Original line number Diff line number Diff line
@@ -1653,9 +1653,11 @@ class MediaRouter2ServiceImpl {
                            manager));
        }

        List<MediaRoute2Info> 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,51 @@ 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<MediaRoute2Info> routes) {
            try {
                mManager.notifyRoutesUpdated(routes);
            } catch (RemoteException ex) {
                Slog.w(TAG, "Failed to notify routes. Manager probably died.", ex);
            }
        }

        /**
         * 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.
         *
         * @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;
@@ -2761,18 +2808,20 @@ class MediaRouter2ServiceImpl {
                    getRouterRecords(/* hasSystemRoutingPermission= */ true);
            List<RouterRecord> routerRecordsWithoutSystemRoutingPermission =
                    getRouterRecords(/* hasSystemRoutingPermission= */ false);
            List<IMediaRouter2Manager> managers = getManagers();
            List<ManagerRecord> managers = getManagerRecords();

            // Managers receive all provider updates with all routes.
            notifyRoutesUpdatedToManagers(
                    managers, new ArrayList<>(mLastNotifiedRoutesToPrivilegedRouters.values()));
            List<MediaRoute2Info> 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
@@ -3068,8 +3117,10 @@ class MediaRouter2ServiceImpl {

        private void onSessionInfoChangedOnHandler(@NonNull MediaRoute2Provider provider,
                @NonNull RoutingSessionInfo sessionInfo) {
            List<IMediaRouter2Manager> managers = getManagers();
            notifySessionUpdatedToManagers(managers, sessionInfo);
            List<ManagerRecord> managers = getManagerRecords();
            for (ManagerRecord manager : managers) {
                manager.notifySessionUpdated(sessionInfo);
            }

            // For system provider, notify all routers.
            if (provider == mSystemProvider) {
@@ -3093,8 +3144,10 @@ class MediaRouter2ServiceImpl {

        private void onSessionReleasedOnHandler(@NonNull MediaRoute2Provider provider,
                @NonNull RoutingSessionInfo sessionInfo) {
            List<IMediaRouter2Manager> managers = getManagers();
            notifySessionReleasedToManagers(managers, sessionInfo);
            List<ManagerRecord> managers = getManagerRecords();
            for (ManagerRecord manager : managers) {
                manager.notifySessionReleased(sessionInfo);
            }

            RouterRecord routerRecord = mSessionToRouterMap.get(sessionInfo.getId());
            if (routerRecord == null) {
@@ -3169,20 +3222,6 @@ class MediaRouter2ServiceImpl {
            return true;
        }

        private List<IMediaRouter2Manager> getManagers() {
            final List<IMediaRouter2Manager> 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<RouterRecord> getRouterRecords() {
            MediaRouter2ServiceImpl service = mServiceRef.get();
            if (service == null) {
@@ -3269,37 +3308,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<IMediaRouter2Manager> managers,
                @NonNull List<MediaRoute2Info> 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);
@@ -3317,32 +3325,6 @@ class MediaRouter2ServiceImpl {
            }
        }

        private void notifySessionUpdatedToManagers(
                @NonNull List<IMediaRouter2Manager> 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 notifySessionReleasedToManagers(
                @NonNull List<IMediaRouter2Manager> 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 {