From e84ffd94e2ee2e327595eaf12ddd1ef63dbc0eb6 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Wed, 16 May 2018 12:59:43 -0700 Subject: [PATCH] Don't crash apps unfairly under FAS When an app calls startForegroundService() from a bg state (e.g. from a broadcast receiver) but the user has placed it under FAS, it's incorrect to crash the app: it hasn't actually violated the fg/bg restrictions contract. Fail quietly instead, just like the legacy-app case when O+ restrictions are imposed on it. Change-Id: I3e8ed2ea6bdbf4167249132ddf9b7c549b9a8062 Fixes: 79235311 Test: ApiDemos [before and after changing target sdk to O+] Test: atest android.app.cts.ServiceTest --- .../java/com/android/server/am/ActiveServices.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index b32ece710a666..bdcb9cb873972 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -476,12 +476,23 @@ public final class ActiveServices { Slog.w(TAG, "Background start not allowed: service " + service + " to " + r.name.flattenToShortString() + " from pid=" + callingPid + " uid=" + callingUid - + " pkg=" + callingPackage); + + " pkg=" + callingPackage + " startFg?=" + fgRequired); if (allowed == ActivityManager.APP_START_MODE_DELAYED || forceSilentAbort) { // In this case we are silently disabling the app, to disrupt as // little as possible existing apps. return null; } + if (forcedStandby) { + // This is an O+ app, but we might be here because the user has placed + // it under strict background restrictions. Don't punish the app if it's + // trying to do the right thing but we're denying it for that reason. + if (fgRequired) { + if (DEBUG_BACKGROUND_CHECK) { + Slog.v(TAG, "Silently dropping foreground service launch due to FAS"); + } + return null; + } + } // This app knows it is in the new model where this operation is not // allowed, so tell it what has happened. UidRecord uidRec = mAm.mActiveUids.get(r.appInfo.uid);