From 8a865d9bae57171e6f758bd3595391305c324055 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Tue, 11 Aug 2020 20:56:24 -0700 Subject: [PATCH] Apply restriction at startForegroundService() call. Apply FGS background startForeground() restriction at Context.startForegroundService() call (the startServiceLocked() method with fgRequired flag), this is earlier than later apply restriction at Service.startForeground() call so the service will not start at all if Context.startForegroundService() and mAllowStartForeground is false. Bug: 157473819 Test: atest atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java Change-Id: Id5c69d697e845376f49b5071e09fdb971d0a4255 --- .../com/android/server/am/ActiveServices.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index b83aa4fb4d86b..23e9d1b552d58 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -494,6 +494,13 @@ public final class ActiveServices { ServiceRecord r = res.record; + if (allowBackgroundActivityStarts) { + r.allowBgActivityStartsOnServiceStart(); + } + + setFgsRestrictionLocked(callingPackage, callingPid, callingUid, service, r, + allowBackgroundActivityStarts); + if (!mAm.mUserController.exists(r.userId)) { Slog.w(TAG, "Trying to start service with non-existent user! " + r.userId); return null; @@ -516,6 +523,21 @@ public final class ActiveServices { forcedStandby = true; } + if (fgRequired) { + if (!r.mAllowStartForeground) { + if (!r.mLoggedInfoAllowStartForeground) { + Slog.wtf(TAG, "Background started FGS " + r.mInfoAllowStartForeground); + r.mLoggedInfoAllowStartForeground = true; + } + if (mAm.mConstants.mFlagFgsStartRestrictionEnabled) { + Slog.w(TAG, "startForegroundService() not allowed due to " + + " mAllowStartForeground false: service " + + r.shortInstanceName); + forcedStandby = true; + } + } + } + // If this is a direct-to-foreground start, make sure it is allowed as per the app op. boolean forceSilentAbort = false; if (fgRequired) { @@ -688,14 +710,7 @@ public final class ActiveServices { "Not potential delay (user " + r.userId + " not started): " + r); } } - - if (allowBackgroundActivityStarts) { - r.allowBgActivityStartsOnServiceStart(); - } ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting); - - setFgsRestrictionLocked(callingPackage, callingPid, - callingUid, service, r, allowBackgroundActivityStarts); return cmp; } @@ -1422,8 +1437,8 @@ public final class ActiveServices { if (mAm.mConstants.mFlagFgsStartRestrictionEnabled) { Slog.w(TAG, "Service.startForeground() not allowed due to " - + " mAllowStartForeground false: service " - + r.shortInstanceName); + + "mAllowStartForeground false: service " + + r.shortInstanceName); updateServiceForegroundLocked(r.app, true); ignoreForeground = true; }