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

Commit 06da1daa authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Log route additions and removals to dumpsys

Test: atest mediaroutertest
Test: `adb shell dumpsys media_router`
Bug: 257039897
Change-Id: Idf5d0a545311b83675369e4b7e410ab2f61326a0
parent 16eaa6fe
Loading
Loading
Loading
Loading
+39 −2
Original line number Diff line number Diff line
@@ -1685,6 +1685,9 @@ class MediaRouter2ServiceImpl {
                    indexOfRouteProviderInfoByUniqueId(provider.getUniqueId(), mLastProviderInfos);
            MediaRoute2ProviderInfo oldInfo =
                    providerInfoIndex == -1 ? null : mLastProviderInfos.get(providerInfoIndex);
            MediaRouter2ServiceImpl mediaRouter2Service = mServiceRef.get();
            EventLogger eventLogger =
                    mediaRouter2Service != null ? mediaRouter2Service.mEventLogger : null;
            if (oldInfo == newInfo) {
                // Nothing to do.
                return;
@@ -1710,6 +1713,7 @@ class MediaRouter2ServiceImpl {
            }

            // Add new routes to the maps.
            ArrayList<MediaRoute2Info> addedRoutes = new ArrayList<>();
            boolean hasAddedOrModifiedRoutes = false;
            for (MediaRoute2Info newRouteInfo : newRoutes) {
                if (!newRouteInfo.isValid()) {
@@ -1724,11 +1728,14 @@ class MediaRouter2ServiceImpl {
                MediaRoute2Info oldRouteInfo =
                        mLastNotifiedRoutesToPrivilegedRouters.put(
                                newRouteInfo.getId(), newRouteInfo);
                hasAddedOrModifiedRoutes |=
                        oldRouteInfo == null || !oldRouteInfo.equals(newRouteInfo);
                hasAddedOrModifiedRoutes |= !newRouteInfo.equals(oldRouteInfo);
                if (oldRouteInfo == null) {
                    addedRoutes.add(newRouteInfo);
                }
            }

            // Remove stale routes from the maps.
            ArrayList<MediaRoute2Info> removedRoutes = new ArrayList<>();
            Collection<MediaRoute2Info> oldRoutes =
                    oldInfo == null ? Collections.emptyList() : oldInfo.getRoutes();
            boolean hasRemovedRoutes = false;
@@ -1738,6 +1745,26 @@ class MediaRouter2ServiceImpl {
                    hasRemovedRoutes = true;
                    mLastNotifiedRoutesToPrivilegedRouters.remove(oldRouteId);
                    mLastNotifiedRoutesToNonPrivilegedRouters.remove(oldRouteId);
                    removedRoutes.add(oldRoute);
                }
            }

            if (eventLogger != null) {
                if (!addedRoutes.isEmpty()) {
                    // If routes were added, newInfo cannot be null.
                    eventLogger.enqueue(
                            toLoggingEvent(
                                    /* source= */ "addProviderRoutes",
                                    newInfo.getUniqueId(),
                                    addedRoutes));
                }
                if (!removedRoutes.isEmpty()) {
                    // If routes were removed, oldInfo cannot be null.
                    eventLogger.enqueue(
                            toLoggingEvent(
                                    /* source= */ "removeProviderRoutes",
                                    oldInfo.getUniqueId(),
                                    removedRoutes));
                }
            }

@@ -1748,6 +1775,16 @@ class MediaRouter2ServiceImpl {
                    mSystemProvider.getDefaultRoute());
        }

        private static EventLogger.Event toLoggingEvent(
                String source, String providerId, ArrayList<MediaRoute2Info> routes) {
            String routesString =
                    routes.stream()
                            .map(it -> String.format("%s | %s", it.getOriginalId(), it.getName()))
                            .collect(Collectors.joining(/* delimiter= */ ", "));
            return EventLogger.StringEvent.from(
                    source, "provider: %s, routes: [%s]", providerId, routesString);
        }

        /**
         * Dispatches the latest route updates in {@link #mLastNotifiedRoutesToPrivilegedRouters}
         * and {@link #mLastNotifiedRoutesToNonPrivilegedRouters} to registered {@link