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

Commit 2018e68d authored by Hui Yu's avatar Hui Yu
Browse files

Only throw IllegalStateException when the service is BG-FGS-launch

restricted.

Bug: 177941003
Test: atest android.app.cts.ActivityManagerProcessStateTest#testForegroundServiceAppOp
Change-Id: I16124dc04e14f772d392c9eb361c644de8d72afd
parent bc503754
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -1692,7 +1692,7 @@ public final class ActiveServices {
            }

            try {
                String ignoreForeground = null;
                boolean ignoreForeground = false;
                final int mode = mAm.getAppOpsManager().checkOpNoThrow(
                        AppOpsManager.OP_START_FOREGROUND, r.appInfo.uid, r.packageName);
                switch (mode) {
@@ -1702,9 +1702,9 @@ public final class ActiveServices {
                        break;
                    case AppOpsManager.MODE_IGNORED:
                        // Whoops, silently ignore this.
                        ignoreForeground = "Service.startForeground() not allowed due to app op: "
                                + "service " + r.shortInstanceName;
                        Slog.w(TAG, ignoreForeground);
                        Slog.w(TAG, "Service.startForeground() not allowed due to app op: service "
                                + r.shortInstanceName);
                        ignoreForeground = true;
                        break;
                    default:
                        throw new SecurityException("Foreground not allowed as per app op");
@@ -1712,18 +1712,19 @@ public final class ActiveServices {

                // Apps that are TOP or effectively similar may call startForeground() on
                // their services even if they are restricted from doing that while in bg.
                if (ignoreForeground == null
                if (!ignoreForeground
                        && !appIsTopLocked(r.appInfo.uid)
                        && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
                    ignoreForeground = "Service.startForeground() not allowed due to bg restriction"
                            + ":service " + r.shortInstanceName;
                    Slog.w(TAG, ignoreForeground);
                    Slog.w(TAG,
                            "Service.startForeground() not allowed due to bg restriction: service "
                                    + r.shortInstanceName);
                    // Back off of any foreground expectations around this service, since we've
                    // just turned down its fg request.
                    updateServiceForegroundLocked(r.app, false);
                    ignoreForeground = true;
                }

                if (ignoreForeground == null) {
                if (!ignoreForeground) {
                    if (isFgsBgStart(r.mAllowStartForeground)) {
                        if (!r.mLoggedInfoAllowStartForeground) {
                            Slog.wtf(TAG, "Background started FGS "
@@ -1732,12 +1733,17 @@ public final class ActiveServices {
                        }
                        if (r.mAllowStartForeground == FGS_FEATURE_DENIED
                                && isBgFgsRestrictionEnabled(r)) {
                            ignoreForeground = "Service.startForeground() not allowed due to "
                            final String msg = "Service.startForeground() not allowed due to "
                                    + "mAllowStartForeground false: service "
                                    + r.shortInstanceName;
                            Slog.w(TAG, ignoreForeground);
                            Slog.w(TAG, msg);
                            showFgsBgRestrictedNotificationLocked(r);
                            updateServiceForegroundLocked(r.app, true);
                            ignoreForeground = true;
                            if (CompatChanges.isChangeEnabled(FGS_START_EXCEPTION_CHANGE_ID,
                                    r.appInfo.uid)) {
                                throw new IllegalStateException(msg);
                            }
                        }
                    }
                }
@@ -1746,7 +1752,7 @@ public final class ActiveServices {
                // services, so now that we've enforced the startForegroundService() contract
                // we only do the machinery of making the service foreground when the app
                // is not restricted.
                if (ignoreForeground == null) {
                if (!ignoreForeground) {
                    if (r.foregroundId != id) {
                        cancelForegroundNotificationLocked(r);
                        r.foregroundId = id;
@@ -1808,10 +1814,6 @@ public final class ActiveServices {
                    if (DEBUG_FOREGROUND_SERVICE) {
                        Slog.d(TAG, "Suppressing startForeground() for FAS " + r);
                    }
                    if (CompatChanges.isChangeEnabled(FGS_START_EXCEPTION_CHANGE_ID, r.appInfo.uid)
                            && isBgFgsRestrictionEnabled(r)) {
                        throw new IllegalStateException(ignoreForeground);
                    }
                }
            } finally {
                if (stopProcStatsOp) {