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

Commit 6189d556 authored by Hui Yu's avatar Hui Yu Committed by Automerger Merge Worker
Browse files

Merge "Allow background-restricted app to start FGS from alarm-clock." into...

Merge "Allow background-restricted app to start FGS from alarm-clock." into udc-dev am: b4e7a92b am: fef663ac

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22797869



Change-Id: I9635f6d3f4955ccd8d2c055a4a2dc119636fd341
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 4e8ded91 fef663ac
Loading
Loading
Loading
Loading
+29 −6
Original line number Original line Diff line number Diff line
@@ -52,6 +52,7 @@ import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVI
import static android.os.PowerExemptionManager.REASON_ACTIVE_DEVICE_ADMIN;
import static android.os.PowerExemptionManager.REASON_ACTIVE_DEVICE_ADMIN;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_STARTER;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_STARTER;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD;
import static android.os.PowerExemptionManager.REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD;
import static android.os.PowerExemptionManager.REASON_ALARM_MANAGER_ALARM_CLOCK;
import static android.os.PowerExemptionManager.REASON_ALLOWLISTED_PACKAGE;
import static android.os.PowerExemptionManager.REASON_ALLOWLISTED_PACKAGE;
import static android.os.PowerExemptionManager.REASON_BACKGROUND_ACTIVITY_PERMISSION;
import static android.os.PowerExemptionManager.REASON_BACKGROUND_ACTIVITY_PERMISSION;
import static android.os.PowerExemptionManager.REASON_BACKGROUND_FGS_PERMISSION;
import static android.os.PowerExemptionManager.REASON_BACKGROUND_FGS_PERMISSION;
@@ -458,11 +459,12 @@ public final class ActiveServices {
        public void updateBackgroundRestrictedForUidPackage(int uid, String packageName,
        public void updateBackgroundRestrictedForUidPackage(int uid, String packageName,
                boolean restricted) {
                boolean restricted) {
            synchronized (mAm) {
            synchronized (mAm) {
                if (!isForegroundServiceAllowedInBackgroundRestricted(uid, packageName)) {
                    stopAllForegroundServicesLocked(uid, packageName);
                }
                mAm.mProcessList.updateBackgroundRestrictedForUidPackageLocked(
                mAm.mProcessList.updateBackgroundRestrictedForUidPackageLocked(
                        uid, packageName, restricted);
                        uid, packageName, restricted);
                if (!isForegroundServiceAllowedInBackgroundRestricted(uid, packageName)
                        && !isTempAllowedByAlarmClock(uid)) {
                    stopAllForegroundServicesLocked(uid, packageName);
                }
            }
            }
        }
        }
    }
    }
@@ -475,7 +477,11 @@ public final class ActiveServices {
            final ServiceRecord r = smap.mServicesByInstanceName.valueAt(i);
            final ServiceRecord r = smap.mServicesByInstanceName.valueAt(i);
            if (uid == r.serviceInfo.applicationInfo.uid
            if (uid == r.serviceInfo.applicationInfo.uid
                    || packageName.equals(r.serviceInfo.packageName)) {
                    || packageName.equals(r.serviceInfo.packageName)) {
                if (r.isForeground) {
                // If the FGS is started by temp allowlist of alarm-clock
                // (REASON_ALARM_MANAGER_ALARM_CLOCK), allow it to continue and do not stop it,
                // even the app is background-restricted.
                if (r.isForeground
                        && r.mAllowStartForegroundAtEntering != REASON_ALARM_MANAGER_ALARM_CLOCK) {
                    toStop.add(r);
                    toStop.add(r);
                }
                }
            }
            }
@@ -872,7 +878,9 @@ public final class ActiveServices {
        // start analogously to the legacy-app forced-restrictions case, regardless
        // start analogously to the legacy-app forced-restrictions case, regardless
        // of its target SDK version.
        // of its target SDK version.
        boolean forcedStandby = false;
        boolean forcedStandby = false;
        if (bgLaunch && appRestrictedAnyInBackground(appUid, appPackageName)) {
        if (bgLaunch
                && appRestrictedAnyInBackground(appUid, appPackageName)
                && !isTempAllowedByAlarmClock(appUid)) {
            if (DEBUG_FOREGROUND_SERVICE) {
            if (DEBUG_FOREGROUND_SERVICE) {
                Slog.d(TAG, "Forcing bg-only service start only for " + r.shortInstanceName
                Slog.d(TAG, "Forcing bg-only service start only for " + r.shortInstanceName
                        + " : bgLaunch=" + bgLaunch + " callerFg=" + callerFg);
                        + " : bgLaunch=" + bgLaunch + " callerFg=" + callerFg);
@@ -1931,6 +1939,20 @@ public final class ActiveServices {
                && isForegroundServiceAllowedInBackgroundRestricted(app);
                && isForegroundServiceAllowedInBackgroundRestricted(app);
    }
    }


    /*
     * If the FGS start is temp allowlisted by alarm-clock(REASON_ALARM_MANAGER_ALARM_CLOCK), it is
     * allowed even the app is background-restricted.
     */
    private boolean isTempAllowedByAlarmClock(int uid) {
        final ActivityManagerService.FgsTempAllowListItem item =
                mAm.isAllowlistedForFgsStartLOSP(uid);
        if (item != null) {
            return item.mReasonCode == REASON_ALARM_MANAGER_ALARM_CLOCK;
        } else {
            return false;
        }
    }

    void logFgsApiBeginLocked(int uid, int pid, int apiType) {
    void logFgsApiBeginLocked(int uid, int pid, int apiType) {
        synchronized (mFGSLogger) {
        synchronized (mFGSLogger) {
            mFGSLogger.logForegroundServiceApiEventBegin(uid, pid, apiType, "");
            mFGSLogger.logForegroundServiceApiEventBegin(uid, pid, apiType, "");
@@ -2064,7 +2086,8 @@ public final class ActiveServices {
                // Apps that are TOP or effectively similar may call startForeground() on
                // Apps that are TOP or effectively similar may call startForeground() on
                // their services even if they are restricted from doing that while in bg.
                // their services even if they are restricted from doing that while in bg.
                if (!ignoreForeground
                if (!ignoreForeground
                        && !isForegroundServiceAllowedInBackgroundRestricted(r.app)) {
                        && !isForegroundServiceAllowedInBackgroundRestricted(r.app)
                        && !isTempAllowedByAlarmClock(r.app.uid)) {
                    Slog.w(TAG,
                    Slog.w(TAG,
                            "Service.startForeground() not allowed due to bg restriction: service "
                            "Service.startForeground() not allowed due to bg restriction: service "
                                    + r.shortInstanceName);
                                    + r.shortInstanceName);