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

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

Add systemRequestedFgToBg flag to ServiceRecord for system-initated transistions.



This commit introduces a boolean to track if last service transistion to
background was done by system. This is useful in cases where service
state is managed by system.

Example:
when a media session transistions from playing  to stop/pause,
system moves app hosted media service to background. Similarly when
media session transistion back to playing, system moves app hosted media
service to foreground. If app explicitly requests service to be in
background via #StopForeground() call, system should respect that.

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

Change-Id: Ib10a2700b3d083144804db12ef929f9d1869cebe
Signed-off-by: default avatarPradeep Sawlani <sawlani@google.com>
parent 80f15bd5
Loading
Loading
Loading
Loading
+25 −15
Original line number Diff line number Diff line
@@ -618,7 +618,7 @@ public final class ActiveServices {
                Slog.i(TAG, "  Stopping fg for service " + r);
            }
            setServiceForegroundInnerLocked(r, 0, null, 0, 0,
                    0);
                    0,  /* systemRequestedTransition= */ true);
        }
    }

@@ -1839,7 +1839,7 @@ public final class ActiveServices {
            ServiceRecord r = findServiceLocked(className, token, userId);
            if (r != null) {
                setServiceForegroundInnerLocked(r, id, notification, flags, foregroundServiceType,
                        callingUid);
                        callingUid, /* systemRequestedTransition= */ false);
            }
        } finally {
            mAm.mInjector.restoreCallingIdentity(origId);
@@ -2155,7 +2155,7 @@ public final class ActiveServices {
    @GuardedBy("mAm")
    private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
            Notification notification, int flags, int foregroundServiceType,
            int callingUidIfStart) {
            int callingUidIfStart, boolean systemRequestedTransition) {
        if (id != 0) {
            if (notification == null) {
                throw new IllegalArgumentException("null notification");
@@ -2800,6 +2800,7 @@ public final class ActiveServices {
                // earlier.
                r.foregroundServiceType = 0;
                r.mFgsNotificationWasDeferred = false;
                r.systemRequestedFgToBg = systemRequestedTransition;
                signalForegroundServiceObserversLocked(r);
                resetFgsRestrictionLocked(r);
                mAm.updateForegroundServiceUsageStats(r.name, r.userId, false);
@@ -9339,14 +9340,22 @@ public final class ActiveServices {
                if (sr.foregroundServiceType
                        == ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE
                        && sr.foregroundId == notificationId) {
                    if (DEBUG_FOREGROUND_SERVICE) {
                        Slog.d(TAG, "Moving media service to foreground for package "
                    // check if service is explicitly requested by app to not be in foreground.
                    if (sr.systemRequestedFgToBg) {
                        Slog.d(TAG,
                                "System initiated service transition to foreground "
                                        + "for package "
                                        + packageName);
                    }
                        setServiceForegroundInnerLocked(sr, sr.foregroundId,
                                sr.foregroundNoti, /* flags */ 0,
                                ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
                             /* callingUidStart */ 0);
                                /* callingUidStart */ 0, /* systemRequestedTransition */ true);
                    } else {
                        Slog.d(TAG,
                                "Ignoring system initiated foreground service transition for "
                                        + "package"
                                        + packageName);
                    }
                }
            }
        }
@@ -9379,13 +9388,14 @@ public final class ActiveServices {
                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 "
                    Slog.d(TAG,
                            "System initiated transition of foreground service(type:media) to bg "
                                    + "for package"
                                    + packageName);
                    }
                    setServiceForegroundInnerLocked(sr, /* id */ 0,
                            /* notification */ null, /* flags */ 0,
                            /* foregroundServiceType */ 0, /* callingUidStart */ 0);
                            /* foregroundServiceType */ 0, /* callingUidStart */ 0,
                            /* systemRequestedTransition */ true);
                }
            }
        }
+1 −0
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
    boolean fgWaiting;      // is a timeout for going foreground already scheduled?
    boolean isNotAppComponentUsage; // is service binding not considered component/package usage?
    boolean isForeground;   // is service currently in foreground mode?
    boolean systemRequestedFgToBg; //  system requested service to transition to background.
    boolean inSharedIsolatedProcess; // is the service in a shared isolated process
    int foregroundId;       // Notification ID of last foreground req.
    Notification foregroundNoti; // Notification record of foreground state.