Services exit fg when bg-restricted app leaves top
Apps under bg restrictions can still start foreground services while they are the top/frontmost UI. However, this was not being reexamined when the app was no longer the top UI. This is now fixed: when an app under bg restrictions exits the top state, any foreground services it is hosting are demoted out of the foreground state. At this point the service lifecycle is just like any other ordinary service that an app has left running after it is no longer the active UI: the service is stopped by the OS after the standard grace period. Bug: 139436349 Test: repro app supplied with the bug Test: foreground use of GPM under bg restrictions Test: atest CtsAppTestCases:android.app.cts.ActivityManagerProcessStateTest\#testBgRestrictedForegroundService Change-Id: I6d0e954c961f7c547c1f5d98e3926586f3071bca
This commit is contained in:
@@ -192,6 +192,12 @@ public final class ActiveServices {
|
|||||||
@Override
|
@Override
|
||||||
public void stopForegroundServicesForUidPackage(final int uid, final String packageName) {
|
public void stopForegroundServicesForUidPackage(final int uid, final String packageName) {
|
||||||
synchronized (mAm) {
|
synchronized (mAm) {
|
||||||
|
stopAllForegroundServicesLocked(uid, packageName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopAllForegroundServicesLocked(final int uid, final String packageName) {
|
||||||
final ServiceMap smap = getServiceMapLocked(UserHandle.getUserId(uid));
|
final ServiceMap smap = getServiceMapLocked(UserHandle.getUserId(uid));
|
||||||
final int N = smap.mServicesByInstanceName.size();
|
final int N = smap.mServicesByInstanceName.size();
|
||||||
final ArrayList<ServiceRecord> toStop = new ArrayList<>(N);
|
final ArrayList<ServiceRecord> toStop = new ArrayList<>(N);
|
||||||
@@ -209,7 +215,7 @@ public final class ActiveServices {
|
|||||||
final int numToStop = toStop.size();
|
final int numToStop = toStop.size();
|
||||||
if (numToStop > 0 && DEBUG_FOREGROUND_SERVICE) {
|
if (numToStop > 0 && DEBUG_FOREGROUND_SERVICE) {
|
||||||
Slog.i(TAG, "Package " + packageName + "/" + uid
|
Slog.i(TAG, "Package " + packageName + "/" + uid
|
||||||
+ " entering FAS with foreground services");
|
+ " in FAS with foreground services");
|
||||||
}
|
}
|
||||||
for (int i = 0; i < numToStop; i++) {
|
for (int i = 0; i < numToStop; i++) {
|
||||||
final ServiceRecord r = toStop.get(i);
|
final ServiceRecord r = toStop.get(i);
|
||||||
@@ -219,8 +225,6 @@ public final class ActiveServices {
|
|||||||
setServiceForegroundInnerLocked(r, 0, null, 0, 0);
|
setServiceForegroundInnerLocked(r, 0, null, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Information about an app that is currently running one or more foreground services.
|
* Information about an app that is currently running one or more foreground services.
|
||||||
@@ -1019,12 +1023,23 @@ public final class ActiveServices {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!aa.mAppOnTop) {
|
if (!aa.mAppOnTop) {
|
||||||
|
// Transitioning a fg-service host app out of top: if it's bg restricted,
|
||||||
|
// it loses the fg service state now.
|
||||||
|
if (!appRestrictedAnyInBackground(aa.mUid, aa.mPackageName)) {
|
||||||
if (active == null) {
|
if (active == null) {
|
||||||
active = new ArrayList<>();
|
active = new ArrayList<>();
|
||||||
}
|
}
|
||||||
if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Adding active: pkg="
|
if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Adding active: pkg="
|
||||||
+ aa.mPackageName + ", uid=" + aa.mUid);
|
+ aa.mPackageName + ", uid=" + aa.mUid);
|
||||||
active.add(aa);
|
active.add(aa);
|
||||||
|
} else {
|
||||||
|
if (DEBUG_FOREGROUND_SERVICE) {
|
||||||
|
Slog.d(TAG, "bg-restricted app "
|
||||||
|
+ aa.mPackageName + "/" + aa.mUid
|
||||||
|
+ " exiting top; demoting fg services ");
|
||||||
|
}
|
||||||
|
stopAllForegroundServicesLocked(aa.mUid, aa.mPackageName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
smap.removeMessages(ServiceMap.MSG_UPDATE_FOREGROUND_APPS);
|
smap.removeMessages(ServiceMap.MSG_UPDATE_FOREGROUND_APPS);
|
||||||
|
|||||||
Reference in New Issue
Block a user