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

Commit c939f2cb authored by Pradeep Sawlani's avatar Pradeep Sawlani
Browse files

ActivityManagerService: Add method to set media fgs inactive.

Changes adds a new method in activity manager service to set
foreground service of type media playback inactive. This method
is expected to be called from MediaSessionService when media session
is "user-disengaged" for certain amount of time (>10mins).
This method will move foreground service to background and allowing
application to be subject to other system policies to save resources.

Flag: com.android.media.flags.enable_notifying_activity_manager_with_media_session_status_change
BUG: 281762171
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerNotifyMediaFGSTypeTest.java

Change-Id: If4e60dabeec6cc595703c0d07369d7526fe8d862
parent 7c2a45be
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -1149,6 +1149,20 @@ public abstract class ActivityManagerInternal {
     */
    public abstract void stopForegroundServiceDelegate(@NonNull ServiceConnection connection);

    /**
     * Notifies that a media foreground service associated with a media session has
     * transitioned to a "user-disengaged" state.
     * Upon receiving this notification, service may be removed from the foreground state. It
     * should only be called by {@link com.android.server.media.MediaSessionService}
     *
     * @param packageName The package name of the app running the media foreground service.
     * @param userId The user ID associated with the foreground service.
     * @param notificationId The ID of the media notification associated with the foreground
     *                      service.
     */
    public abstract void notifyInactiveMediaForegroundService(@NonNull String packageName,
            @UserIdInt int userId, int notificationId);

    /**
     * Same as {@link android.app.IActivityManager#startProfile(int userId)}, but it would succeed
     * even if the profile is disabled - it should only be called by
+41 −0
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ import static com.android.internal.util.FrameworkStatsLog.SERVICE_REQUEST_EVENT_
import static com.android.internal.util.FrameworkStatsLog.SERVICE_REQUEST_EVENT_REPORTED__PROC_START_TYPE__PROCESS_START_TYPE_WARM;
import static com.android.internal.util.FrameworkStatsLog.SERVICE_REQUEST_EVENT_REPORTED__REQUEST_TYPE__BIND;
import static com.android.internal.util.FrameworkStatsLog.SERVICE_REQUEST_EVENT_REPORTED__REQUEST_TYPE__START;
import static com.android.media.flags.Flags.enableNotifyingActivityManagerWithMediaSessionStatusChange;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BACKGROUND_CHECK;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOREGROUND_SERVICE;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_MU;
@@ -9319,6 +9320,46 @@ public final class ActiveServices {
        }
    }

    /**
     * Handles notifications from MediaSessionService about inactive media foreground services.
     * This method evaluates the provided information and determines whether to stop the
     * corresponding foreground service.
     *
     * @param packageName The package name of the app running the foreground service.
     * @param userId The user ID associated with the foreground service.
     * @param notificationId The ID of the media notification associated with the foreground
     *                      service.
     */
    void notifyInactiveMediaForegroundServiceLocked(@NonNull String packageName,
            @UserIdInt int userId, int notificationId) {
        if (!enableNotifyingActivityManagerWithMediaSessionStatusChange()) {
            return;
        }

        final ServiceMap smap = mServiceMap.get(userId);
        if (smap == null) {
            return;
        }
        final int serviceSize = smap.mServicesByInstanceName.size();
        for (int i = 0; i < serviceSize; i++) {
            final ServiceRecord sr = smap.mServicesByInstanceName.valueAt(i);
            if (sr.appInfo.packageName.equals(packageName) && sr.isForeground) {
                if (sr.foregroundServiceType
                        == ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
                        && sr.foregroundId == notificationId) {
                    if (DEBUG_FOREGROUND_SERVICE) {
                        Slog.d(TAG, "Forcing media foreground service to background for package "
                                + packageName);
                    }
                    setServiceForegroundInnerLocked(sr, /* id */ 0,
                            /* notification */ null, /* flags */ 0,
                            /* foregroundServiceType */ 0, /* callingUidStart */ 0);
                }
            }
        }
    }


    private static void getClientPackages(ServiceRecord sr, ArraySet<String> output) {
        var connections = sr.getConnections();
        for (int conni = connections.size() - 1; conni >= 0; conni--) {
+9 −0
Original line number Diff line number Diff line
@@ -17910,6 +17910,15 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
        }
        @Override
        public void notifyInactiveMediaForegroundService(@NonNull String packageName,
                @UserIdInt int userId, int notificationId) {
            synchronized (ActivityManagerService.this) {
                mServices.notifyInactiveMediaForegroundServiceLocked(packageName, userId,
                        notificationId);
            }
        }
        @Override
        public ArraySet<String> getClientPackages(String servicePackageName) {
            synchronized (ActivityManagerService.this) {