Merge "Revert "Kill process if it's cached & idle and in forced-app-standby mode"" into sc-dev am: 160fb766f1

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15131779

Change-Id: Ied32e6dc6a5074f28d1dc035214b681c58eccb60
This commit is contained in:
Jing Ji
2021-06-29 19:24:25 +00:00
committed by Automerger Merge Worker
10 changed files with 25 additions and 385 deletions

View File

@@ -25,29 +25,19 @@ public interface AppStateTracker {
String TAG = "AppStateTracker";
/**
* Register a {@link ForcedAppStandbyListener} to listen for forced-app-standby changes that
* should affect services etc.
* Register a {@link ServiceStateListener} to listen for forced-app-standby changes that should
* affect services.
*/
void addForcedAppStandbyListener(@NonNull ForcedAppStandbyListener listener);
void addServiceStateListener(@NonNull ServiceStateListener listener);
/**
* @return {code true} if the given UID/package has been in forced app standby mode.
* A listener to listen to forced-app-standby changes that should affect services.
*/
boolean isAppInForcedAppStandby(int uid, @NonNull String packageName);
/**
* A listener to listen to forced-app-standby changes that should affect services etc.
*/
interface ForcedAppStandbyListener {
interface ServiceStateListener {
/**
* Called when an app goes in/out of forced app standby.
* Called when an app goes into forced app standby and its foreground
* services need to be removed from that state.
*/
void updateForceAppStandbyForUidPackage(int uid, String packageName, boolean standby);
/**
* Called when all apps' forced-app-standby states need to be re-evaluated, due to
* enable/disable certain feature flags.
*/
void updateForcedAppStandbyForAllApps();
void stopForegroundServicesForUidPackage(int uid, String packageName);
}
}

View File

@@ -60,10 +60,8 @@ import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* Class to keep track of the information related to "force app standby", which includes:
@@ -162,46 +160,16 @@ public class AppStateTrackerImpl implements AppStateTracker {
@GuardedBy("mLock")
boolean mForcedAppStandbyEnabled;
/**
* A lock-free set of (uid, packageName) pairs in forced app standby mode.
*
* <p>
* It's bascially shadowing the {@link #mRunAnyRestrictedPackages} together with
* the {@link #mForcedAppStandbyEnabled} and the {@link #mForceAllAppsStandby} - mutations on
* them would result in copy-on-write.
*
* Note: when {@link #mForcedAppStandbyEnabled} is {@code false}, it'll be set to an empty set.
* when {@link #mForceAllAppsStandby} is {@code true}, it'll be set to null;
* </p>
*/
volatile Set<Pair<Integer, String>> mForcedAppStandbyUidPackages = Collections.emptySet();
@Override
public void addForcedAppStandbyListener(@NonNull ForcedAppStandbyListener listener) {
public void addServiceStateListener(@NonNull ServiceStateListener listener) {
addListener(new Listener() {
@Override
public void updateForceAppStandbyForUidPackage(int uid, String packageName,
boolean standby) {
listener.updateForceAppStandbyForUidPackage(uid, packageName, standby);
}
@Override
public void updateForcedAppStandbyForAllApps() {
listener.updateForcedAppStandbyForAllApps();
public void stopForegroundServicesForUidPackage(int uid, String packageName) {
listener.stopForegroundServicesForUidPackage(uid, packageName);
}
});
}
@Override
public boolean isAppInForcedAppStandby(int uid, @NonNull String packageName) {
final Set<Pair<Integer, String>> fasUidPkgs = mForcedAppStandbyUidPackages;
if (fasUidPkgs == null) {
// Meaning the mForceAllAppsStandby is true.
return true;
}
return fasUidPkgs.contains(Pair.create(uid, packageName));
}
interface Stats {
int UID_FG_STATE_CHANGED = 0;
int UID_ACTIVE_STATE_CHANGED = 1;
@@ -265,7 +233,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
return;
}
mForcedAppStandbyEnabled = enabled;
updateForcedAppStandbyUidPackagesLocked();
if (DEBUG) {
Slog.d(TAG, "Forced app standby feature flag changed: "
+ mForcedAppStandbyEnabled);
@@ -310,11 +277,7 @@ public class AppStateTrackerImpl implements AppStateTracker {
if (!sender.isRunAnyInBackgroundAppOpsAllowed(uid, packageName)) {
Slog.v(TAG, "Package " + packageName + "/" + uid
+ " toggled into fg service restriction");
updateForceAppStandbyForUidPackage(uid, packageName, true);
} else {
Slog.v(TAG, "Package " + packageName + "/" + uid
+ " toggled out of fg service restriction");
updateForceAppStandbyForUidPackage(uid, packageName, false);
stopForegroundServicesForUidPackage(uid, packageName);
}
}
@@ -379,7 +342,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
private void onForceAllAppsStandbyChanged(AppStateTrackerImpl sender) {
updateAllJobs();
updateAllAlarms();
updateForcedAppStandbyForAllApps();
}
/**
@@ -404,17 +366,10 @@ public class AppStateTrackerImpl implements AppStateTracker {
}
/**
* Called when an app goes in/out of forced app standby.
* Called when an app goes into forced app standby and its foreground
* services need to be removed from that state.
*/
public void updateForceAppStandbyForUidPackage(int uid, String packageName,
boolean standby) {
}
/**
* Called when all apps' forced-app-standby states need to be re-evaluated due to changes of
* feature flags such as {@link #mForcedAppStandbyEnabled} or {@link #mForceAllAppsStandby}.
*/
public void updateForcedAppStandbyForAllApps() {
public void stopForegroundServicesForUidPackage(int uid, String packageName) {
}
/**
@@ -483,12 +438,9 @@ public class AppStateTrackerImpl implements AppStateTracker {
final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
// No need to notify for state change as all the alarms and jobs should be
// removed too.
synchronized (mLock) {
mExemptedBucketPackages.remove(userId, pkgName);
mRunAnyRestrictedPackages.remove(Pair.create(uid, pkgName));
updateForcedAppStandbyUidPackagesLocked();
mActiveUids.delete(uid);
}
mExemptedBucketPackages.remove(userId, pkgName);
mRunAnyRestrictedPackages.remove(Pair.create(uid, pkgName));
mActiveUids.delete(uid);
}
break;
}
@@ -628,29 +580,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
}
}
}
updateForcedAppStandbyUidPackagesLocked();
}
/**
* Update the {@link #mForcedAppStandbyUidPackages} upon mutations on
* {@link #mRunAnyRestrictedPackages}, {@link #mForcedAppStandbyEnabled} or
* {@link #mForceAllAppsStandby}.
*/
@GuardedBy("mLock")
private void updateForcedAppStandbyUidPackagesLocked() {
if (!mForcedAppStandbyEnabled) {
mForcedAppStandbyUidPackages = Collections.emptySet();
return;
}
if (mForceAllAppsStandby) {
mForcedAppStandbyUidPackages = null;
return;
}
Set<Pair<Integer, String>> fasUidPkgs = new ArraySet<>();
for (int i = 0, size = mRunAnyRestrictedPackages.size(); i < size; i++) {
fasUidPkgs.add(mRunAnyRestrictedPackages.valueAt(i));
}
mForcedAppStandbyUidPackages = Collections.unmodifiableSet(fasUidPkgs);
}
private void updateForceAllAppStandbyState() {
@@ -672,7 +601,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
return;
}
mForceAllAppsStandby = enable;
updateForcedAppStandbyUidPackagesLocked();
mHandler.notifyForceAllAppsStandbyChanged();
}
@@ -717,7 +645,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
} else {
mRunAnyRestrictedPackages.removeAt(index);
}
updateForcedAppStandbyUidPackagesLocked();
return true;
}
@@ -969,7 +896,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
if (unblockAlarms) {
l.unblockAllUnrestrictedAlarms();
}
l.updateForcedAppStandbyForAllApps();
}
mStatLogger.logDurationStat(
Stats.FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED, start);
@@ -1040,7 +966,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
mRunAnyRestrictedPackages.removeAt(i);
}
}
updateForcedAppStandbyUidPackagesLocked();
cleanUpArrayForUser(mActiveUids, removedUserId);
mExemptedBucketPackages.remove(removedUserId);
}

View File

@@ -328,23 +328,11 @@ public final class ActiveServices {
* Watch for apps being put into forced app standby, so we can step their fg
* services down.
*/
class ForcedStandbyListener implements AppStateTracker.ForcedAppStandbyListener {
class ForcedStandbyListener implements AppStateTracker.ServiceStateListener {
@Override
public void updateForceAppStandbyForUidPackage(int uid, String packageName,
boolean standby) {
public void stopForegroundServicesForUidPackage(final int uid, final String packageName) {
synchronized (mAm) {
if (standby) {
stopAllForegroundServicesLocked(uid, packageName);
}
mAm.mProcessList.updateForceAppStandbyForUidPackageLocked(
uid, packageName, standby);
}
}
@Override
public void updateForcedAppStandbyForAllApps() {
synchronized (mAm) {
mAm.mProcessList.updateForcedAppStandbyForAllAppsLocked();
stopAllForegroundServicesLocked(uid, packageName);
}
}
}
@@ -530,7 +518,7 @@ public final class ActiveServices {
void systemServicesReady() {
AppStateTracker ast = LocalServices.getService(AppStateTracker.class);
ast.addForcedAppStandbyListener(new ForcedStandbyListener());
ast.addServiceStateListener(new ForcedStandbyListener());
mAppWidgetManagerInternal = LocalServices.getService(AppWidgetManagerInternal.class);
setAllowListWhileInUsePermissionInFgs();
}

View File

@@ -52,8 +52,7 @@ final class ActivityManagerConstants extends ContentObserver {
private static final String TAG = "ActivityManagerConstants";
// Key names stored in the settings value.
static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";
private static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";
private static final String KEY_FGSERVICE_MIN_SHOWN_TIME
= "fgservice_min_shown_time";
private static final String KEY_FGSERVICE_MIN_REPORT_TIME
@@ -109,10 +108,10 @@ final class ActivityManagerConstants extends ContentObserver {
static final String KEY_FG_TO_BG_FGS_GRACE_DURATION = "fg_to_bg_fgs_grace_duration";
static final String KEY_FGS_START_FOREGROUND_TIMEOUT = "fgs_start_foreground_timeout";
static final String KEY_FGS_ATOM_SAMPLE_RATE = "fgs_atom_sample_rate";
static final String KEY_KILL_FAS_CACHED_IDLE = "kill_fas_cached_idle";
static final String KEY_FGS_ALLOW_OPT_OUT = "fgs_allow_opt_out";
private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
private static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60*1000;
private static final long DEFAULT_FGSERVICE_MIN_SHOWN_TIME = 2*1000;
private static final long DEFAULT_FGSERVICE_MIN_REPORT_TIME = 3*1000;
private static final long DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME = 1*1000;
@@ -153,10 +152,6 @@ final class ActivityManagerConstants extends ContentObserver {
private static final long DEFAULT_FG_TO_BG_FGS_GRACE_DURATION = 5 * 1000;
private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
private static final float DEFAULT_FGS_ATOM_SAMPLE_RATE = 1; // 100 %
static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60 * 1000;
static final boolean DEFAULT_KILL_FAS_CACHED_IDLE = true;
/**
* Same as {@link TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED}
*/
@@ -500,12 +495,6 @@ final class ActivityManagerConstants extends ContentObserver {
*/
volatile float mFgsAtomSampleRate = DEFAULT_FGS_ATOM_SAMPLE_RATE;
/**
* Whether or not to kill apps in force-app-standby state and it's cached, its UID state is
* idle.
*/
volatile boolean mKillForceAppStandByAndCachedIdle = DEFAULT_KILL_FAS_CACHED_IDLE;
/**
* Whether to allow "opt-out" from the foreground service restrictions.
* (https://developer.android.com/about/versions/12/foreground-services)
@@ -720,9 +709,6 @@ final class ActivityManagerConstants extends ContentObserver {
case KEY_FGS_ATOM_SAMPLE_RATE:
updateFgsAtomSamplePercent();
break;
case KEY_KILL_FAS_CACHED_IDLE:
updateKillFasCachedIdle();
break;
case KEY_FGS_ALLOW_OPT_OUT:
updateFgsAllowOptOut();
break;
@@ -1065,13 +1051,6 @@ final class ActivityManagerConstants extends ContentObserver {
DEFAULT_FGS_ATOM_SAMPLE_RATE);
}
private void updateKillFasCachedIdle() {
mKillForceAppStandByAndCachedIdle = DeviceConfig.getBoolean(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
KEY_KILL_FAS_CACHED_IDLE,
DEFAULT_KILL_FAS_CACHED_IDLE);
}
private void updateFgsAllowOptOut() {
mFgsAllowOptOut = DeviceConfig.getBoolean(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,

View File

@@ -14382,10 +14382,6 @@ public class ActivityManagerService extends IActivityManager.Stub
final int capability = uidRec != null ? uidRec.getSetCapability() : 0;
final boolean ephemeral = uidRec != null ? uidRec.isEphemeral() : isEphemeralLocked(uid);
if (uidRec != null && uidRec.isIdle() && (change & UidRecord.CHANGE_IDLE) != 0) {
mProcessList.killAppIfForceStandbyAndCachedIdleLocked(uidRec);
}
if (uidRec != null && !uidRec.isIdle() && (change & UidRecord.CHANGE_GONE) != 0) {
// If this uid is going away, and we haven't yet reported it is gone,
// then do so now.

View File

@@ -2835,8 +2835,6 @@ public class OomAdjuster {
+ " target=" + state.getAdjTarget() + " capability=" + item.capability);
}
mProcessList.killAppIfForceStandbyAndCachedIdleLocked(app);
return success;
}

View File

@@ -125,7 +125,6 @@ import com.android.internal.os.Zygote;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.MemInfoReader;
import com.android.server.AppStateTracker;
import com.android.server.LocalServices;
import com.android.server.ServiceThread;
import com.android.server.SystemConfig;
@@ -2370,12 +2369,6 @@ public final class ProcessList {
allowlistedAppDataInfoMap = null;
}
AppStateTracker ast = LocalServices.getService(AppStateTracker.class);
if (ast != null) {
app.mState.setForcedAppStandby(ast.isAppInForcedAppStandby(
app.info.uid, app.info.packageName));
}
final Process.ProcessStartResult startResult;
boolean regularZygote = false;
if (hostingRecord.usesWebviewZygote()) {
@@ -5007,55 +5000,6 @@ public final class ProcessList {
return true;
}
@GuardedBy("mService")
void updateForceAppStandbyForUidPackageLocked(int uid, String packageName, boolean standby) {
final UidRecord uidRec = getUidRecordLOSP(uid);
if (uidRec != null) {
uidRec.forEachProcess(app -> {
if (TextUtils.equals(app.info.packageName, packageName)) {
app.mState.setForcedAppStandby(standby);
killAppIfForceStandbyAndCachedIdleLocked(app);
}
});
}
}
@GuardedBy("mService")
void updateForcedAppStandbyForAllAppsLocked() {
if (!mService.mConstants.mKillForceAppStandByAndCachedIdle) {
return;
}
final AppStateTracker ast = LocalServices.getService(AppStateTracker.class);
for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
final ProcessRecord app = mLruProcesses.get(i);
final boolean standby = ast.isAppInForcedAppStandby(
app.info.uid, app.info.packageName);
app.mState.setForcedAppStandby(standby);
if (standby) {
killAppIfForceStandbyAndCachedIdleLocked(app);
}
}
}
@GuardedBy("mService")
void killAppIfForceStandbyAndCachedIdleLocked(ProcessRecord app) {
final UidRecord uidRec = app.getUidRecord();
if (mService.mConstants.mKillForceAppStandByAndCachedIdle
&& uidRec != null && uidRec.isIdle()
&& !app.mState.shouldNotKillOnForcedAppStandbyAndIdle()
&& app.isCached() && app.mState.isForcedAppStandby()) {
app.killLocked("cached idle & forced-app-standby",
ApplicationExitInfo.REASON_OTHER,
ApplicationExitInfo.SUBREASON_CACHED_IDLE_FORCED_APP_STANDBY,
true);
}
}
@GuardedBy("mService")
void killAppIfForceStandbyAndCachedIdleLocked(UidRecord uidRec) {
uidRec.forEachProcess(app -> killAppIfForceStandbyAndCachedIdleLocked(app));
}
/**
* Called by ActivityManagerService when a process died.
*/

View File

@@ -301,12 +301,6 @@ final class ProcessStateRecord {
@GuardedBy("mService")
private int mAllowStartFgsState = PROCESS_STATE_NONEXISTENT;
/**
* Whether or not this process has been in forced-app-standby state.
*/
@GuardedBy("mService")
private boolean mForcedAppStandby;
/**
* Debugging: primary thing impacting oom_adj.
*/
@@ -1125,16 +1119,6 @@ final class ProcessStateRecord {
return mAllowStartFgsState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
}
@GuardedBy("mService")
void setForcedAppStandby(boolean standby) {
mForcedAppStandby = standby;
}
@GuardedBy("mService")
boolean isForcedAppStandby() {
return mForcedAppStandby;
}
@GuardedBy("mService")
void updateLastInvisibleTime(boolean hasVisibleActivities) {
if (hasVisibleActivities) {
@@ -1203,8 +1187,7 @@ final class ProcessStateRecord {
pw.print(" pendingUiClean="); pw.println(mApp.mProfile.hasPendingUiClean());
}
pw.print(prefix); pw.print("cached="); pw.print(mCached);
pw.print(" empty="); pw.print(mEmpty);
pw.print(" forcedAppStandby="); pw.println(mForcedAppStandby);
pw.print(" empty="); pw.println(mEmpty);
if (mServiceB) {
pw.print(prefix); pw.print("serviceb="); pw.print(mServiceB);
pw.print(" serviceHighRam="); pw.println(mServiceHighRam);

View File

@@ -1003,8 +1003,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean());
verify(l, times(1)).updateForceAppStandbyForUidPackage(eq(UID_10_2), eq(PACKAGE_2),
eq(true));
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1019,8 +1017,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean());
verify(l, times(1)).updateForceAppStandbyForUidPackage(eq(UID_10_2), eq(PACKAGE_2),
eq(false));
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1034,7 +1030,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1052,8 +1047,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean());
verify(l, times(1)).updateForceAppStandbyForUidPackage(eq(UID_10_2), eq(PACKAGE_2),
eq(true));
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1070,7 +1063,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1089,7 +1081,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1104,7 +1095,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1121,7 +1111,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1137,7 +1126,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1154,7 +1142,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1171,7 +1158,6 @@ public class AppStateTrackerTest {
verify(l, times(2)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1186,7 +1172,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1203,7 +1188,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1219,7 +1203,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(anyInt());
@@ -1242,7 +1225,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1258,7 +1240,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1274,7 +1255,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1290,7 +1270,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1307,7 +1286,6 @@ public class AppStateTrackerTest {
verify(l, times(1)).updateAllJobs();
verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(1)).updateAllAlarms();
verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1));
@@ -1323,7 +1301,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1339,7 +1316,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1355,7 +1331,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));
@@ -1371,7 +1346,6 @@ public class AppStateTrackerTest {
verify(l, times(0)).updateAllJobs();
verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean());
verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateForceAppStandbyForUidPackage(anyInt(), anyString(), anyBoolean());
verify(l, times(0)).updateAllAlarms();
verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1));

View File

@@ -52,7 +52,6 @@ import android.server.wm.settings.SettingsSession;
import android.support.test.uiautomator.UiDevice;
import android.test.suitebuilder.annotation.LargeTest;
import android.text.TextUtils;
import android.util.KeyValueListParser;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
@@ -421,142 +420,6 @@ public class ActivityManagerTest {
return false;
}
@LargeTest
@Test
public void testKillAppIfFasCachedIdle() throws Exception {
final long shortTimeoutMs = 5_000;
final long backgroundSettleMs = 10_000;
final PackageManager pm = mContext.getPackageManager();
final int uid = pm.getPackageUid(TEST_APP1, 0);
final MyUidImportanceListener uidListener1 = new MyUidImportanceListener(uid);
final MyUidImportanceListener uidListener2 = new MyUidImportanceListener(uid);
SettingsSession<String> amConstantsSettings = null;
DeviceConfigSession<Boolean> killForceAppStandByAndCachedIdle = null;
final ActivityManager am = mContext.getSystemService(ActivityManager.class);
final CountDownLatch[] latchHolder = new CountDownLatch[1];
final H handler = new H(Looper.getMainLooper(), latchHolder);
final Messenger messenger = new Messenger(handler);
try {
am.addOnUidImportanceListener(uidListener1,
RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE);
am.addOnUidImportanceListener(uidListener2, RunningAppProcessInfo.IMPORTANCE_GONE);
toggleScreenOn(true);
killForceAppStandByAndCachedIdle = new DeviceConfigSession<>(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
ActivityManagerConstants.KEY_KILL_FAS_CACHED_IDLE,
DeviceConfig::getBoolean,
ActivityManagerConstants.DEFAULT_KILL_FAS_CACHED_IDLE);
killForceAppStandByAndCachedIdle.set(true);
amConstantsSettings = new SettingsSession<>(
Settings.Global.getUriFor(Settings.Global.ACTIVITY_MANAGER_CONSTANTS),
Settings.Global::getString, Settings.Global::putString);
final KeyValueListParser parser = new KeyValueListParser(',');
long currentBackgroundSettleMs =
ActivityManagerConstants.DEFAULT_BACKGROUND_SETTLE_TIME;
try {
parser.setString(amConstantsSettings.get());
currentBackgroundSettleMs = parser.getLong(
ActivityManagerConstants.KEY_BACKGROUND_SETTLE_TIME,
ActivityManagerConstants.DEFAULT_BACKGROUND_SETTLE_TIME);
} catch (IllegalArgumentException e) {
}
// Drain queue to make sure the existing UID_IDLE_MSG has been processed.
Thread.sleep(currentBackgroundSettleMs);
amConstantsSettings.set(
ActivityManagerConstants.KEY_BACKGROUND_SETTLE_TIME + "=" + backgroundSettleMs);
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND allow");
final Intent intent = new Intent(ACTION_FGS_STATS_TEST);
final ComponentName cn = ComponentName.unflattenFromString(
TEST_APP1 + "/" + TEST_FGS_CLASS);
final Bundle bundle = new Bundle();
intent.setComponent(cn);
bundle.putBinder(EXTRA_MESSENGER, messenger.getBinder());
intent.putExtras(bundle);
// Start the FGS.
latchHolder[0] = new CountDownLatch(1);
mContext.startForegroundService(intent);
assertTrue("Timed out to start fg service", uidListener1.waitFor(
RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE, shortTimeoutMs));
assertTrue("Timed out to get the remote messenger", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
assertFalse("FGS shouldn't be killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Stop the FGS, it shouldn't be killed because it's not in FAS state.
latchHolder[0] = new CountDownLatch(1);
handler.sendRemoteMessage(H.MSG_STOP_FOREGROUND, 0, 0, null);
assertTrue("Timed out to wait for stop fg", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
assertFalse("FGS shouldn't be killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Set the FAS state.
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND deny");
// Now it should've been killed.
assertTrue("Should have been killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Start the FGS.
// Temporarily allow RUN_ANY_IN_BACKGROUND to start FGS.
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND allow");
latchHolder[0] = new CountDownLatch(1);
mContext.startForegroundService(intent);
assertTrue("Timed out to start fg service", uidListener1.waitFor(
RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE, shortTimeoutMs));
assertTrue("Timed out to get the remote messenger", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND deny");
// It shouldn't be killed since it's not cached.
assertFalse("FGS shouldn't be killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Stop the FGS, it should get killed because it's cached & uid idle & in FAS state.
latchHolder[0] = new CountDownLatch(1);
handler.sendRemoteMessage(H.MSG_STOP_FOREGROUND, 0, 0, null);
assertTrue("Timed out to wait for stop fg", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
assertTrue("Should have been killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Disable this FAS cached idle kill feature.
killForceAppStandByAndCachedIdle.set(false);
// Start the FGS.
// Temporarily allow RUN_ANY_IN_BACKGROUND to start FGS.
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND allow");
latchHolder[0] = new CountDownLatch(1);
mContext.startForegroundService(intent);
assertTrue("Timed out to start fg service", uidListener1.waitFor(
RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE, shortTimeoutMs));
assertTrue("Timed out to get the remote messenger", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND deny");
assertFalse("FGS shouldn't be killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
// Stop the FGS, it shouldn't be killed because the feature has been turned off.
latchHolder[0] = new CountDownLatch(1);
handler.sendRemoteMessage(H.MSG_STOP_FOREGROUND, 0, 0, null);
assertTrue("Timed out to wait for stop fg", latchHolder[0].await(
shortTimeoutMs, TimeUnit.MILLISECONDS));
assertFalse("FGS shouldn't be killed", uidListener2.waitFor(
RunningAppProcessInfo.IMPORTANCE_GONE, backgroundSettleMs + shortTimeoutMs));
} finally {
runShellCommand("cmd appops set " + TEST_APP1 + " RUN_ANY_IN_BACKGROUND default");
if (amConstantsSettings != null) {
amConstantsSettings.close();
}
if (killForceAppStandByAndCachedIdle != null) {
killForceAppStandByAndCachedIdle.close();
}
am.removeOnUidImportanceListener(uidListener1);
am.removeOnUidImportanceListener(uidListener2);
}
}
@Ignore("Need to disable calling uid check in ActivityManagerService.killPids before this test")
@Test
public void testKillPids() throws Exception {