OSDN Git Service

Correct bg service start policy re bg-restricted state
authorChristopher Tate <ctate@google.com>
Fri, 30 Mar 2018 21:08:59 +0000 (14:08 -0700)
committerChristopher Tate <ctate@google.com>
Wed, 11 Apr 2018 01:10:01 +0000 (18:10 -0700)
Apps under strict bg restrictions shouldn't have that policy
applied when they're showing foreground UI.

Bug: 77345409
Test: ApiDemos
Test: maps navigation
Change-Id: I16b72c26d3e29b547442c1501a7e1761b182a25f

services/core/java/com/android/server/am/ActiveServices.java

index 067566d..b1cb2fe 100644 (file)
@@ -415,12 +415,23 @@ public final class ActiveServices {
             return null;
         }
 
-        // If the app has strict background restrictions, we treat any service
-        // start analogously to the legacy-app forced-restrictions case.
+        // If we're starting indirectly (e.g. from PendingIntent), figure out whether
+        // we're launching into an app in a background state.
+        final int uidState = mAm.getUidStateLocked(r.appInfo.uid);
+        if (DEBUG_SERVICE) {
+            Slog.v(TAG_SERVICE, "Uid state " + uidState + " indirect starting " + r.shortName);
+        }
+        final boolean bgLaunch = (uidState >
+                ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND);
+
+        // If the app has strict background restrictions, we treat any bg service
+        // start analogously to the legacy-app forced-restrictions case, regardless
+        // of its target SDK version.
         boolean forcedStandby = false;
-        if (appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
+        if (bgLaunch && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
             if (DEBUG_FOREGROUND_SERVICE) {
-                Slog.d(TAG, "Forcing bg-only service start only for " + r.shortName);
+                Slog.d(TAG, "Forcing bg-only service start only for " + r.shortName
+                        + " : bgLaunch=" + bgLaunch + " callerFg=" + callerFg);
             }
             forcedStandby = true;
         }