From 3c1082a3506db9e746bf301e483201dea8e26f2f Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Fri, 17 Sep 2021 11:37:24 -0700 Subject: [PATCH] Fix timing issue on the second or later Service.startForeground(). Remove the 10 seconds latency check on the second or later Service.startForeground() before it can call setFgsRestrictionLocked(). Bug: 200297438 Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java Change-Id: Ifc56e8d318beca5e3b3f9401ffa005bac864a19d --- .../java/com/android/server/am/ActiveServices.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 2c0514d7d0aaf..730907c7d049e 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -1875,7 +1875,7 @@ public final class ActiveServices { if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) { resetFgsRestrictionLocked(r); setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(), - r.appInfo.uid, r.intent.getIntent(), r, r.userId,false); + r.appInfo.uid, r.intent.getIntent(), r, r.userId, false); final String temp = "startForegroundDelayMs:" + delayMs; if (r.mInfoAllowStartForeground != null) { r.mInfoAllowStartForeground += "; " + temp; @@ -1888,12 +1888,8 @@ public final class ActiveServices { } else if (r.mStartForegroundCount >= 1) { // The second or later time startForeground() is called after service is // started. Check for app state again. - final long delayMs = SystemClock.elapsedRealtime() - - r.mLastSetFgsRestrictionTime; - if (delayMs > mAm.mConstants.mFgsStartForegroundTimeoutMs) { - setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(), - r.appInfo.uid, r.intent.getIntent(), r, r.userId,false); - } + setFgsRestrictionLocked(r.serviceInfo.packageName, r.app.getPid(), + r.appInfo.uid, r.intent.getIntent(), r, r.userId, false); } // If the foreground service is not started from TOP process, do not allow it to // have while-in-use location/camera/microphone access.