diff --git a/services/core/java/com/android/server/am/AppBatteryTracker.java b/services/core/java/com/android/server/am/AppBatteryTracker.java index 6fa13eb0058b1..9ce2751432e59 100644 --- a/services/core/java/com/android/server/am/AppBatteryTracker.java +++ b/services/core/java/com/android/server/am/AppBatteryTracker.java @@ -1673,6 +1673,7 @@ final class AppBatteryTracker extends BaseAppStateTracker if (pair != null) { final long[] ts = pair.first; final int restrictedLevel = ts[TIME_STAMP_INDEX_RESTRICTED_BUCKET] > 0 + && mTracker.mAppRestrictionController.isAutoRestrictAbusiveAppEnabled() ? RESTRICTION_LEVEL_RESTRICTED_BUCKET : RESTRICTION_LEVEL_ADAPTIVE_BUCKET; if (maxLevel > RESTRICTION_LEVEL_BACKGROUND_RESTRICTED) { diff --git a/services/core/java/com/android/server/am/AppRestrictionController.java b/services/core/java/com/android/server/am/AppRestrictionController.java index da083aff62ff4..7b763b7d5f9fc 100644 --- a/services/core/java/com/android/server/am/AppRestrictionController.java +++ b/services/core/java/com/android/server/am/AppRestrictionController.java @@ -1062,6 +1062,13 @@ public final class AppRestrictionController { static final String KEY_BG_AUTO_RESTRICTED_BUCKET_ON_BG_RESTRICTION = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "auto_restricted_bucket_on_bg_restricted"; + /** + * Whether or not to move the app to restricted standby level automatically + * when system detects it's abusive. + */ + static final String KEY_BG_AUTO_RESTRICT_ABUSIVE_APPS = + DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "auto_restrict_abusive_apps"; + /** * The minimal interval in ms before posting a notification again on abusive behaviors * of a certain package. @@ -1106,6 +1113,11 @@ public final class AppRestrictionController { static final String KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "prompt_abusive_apps_to_bg_restricted"; + /** + * Default value to {@link #mBgAutoRestrictAbusiveApps}. + */ + static final boolean DEFAULT_BG_AUTO_RESTRICT_ABUSIVE_APPS = true; + /** * Default value to {@link #mBgAutoRestrictedBucket}. */ @@ -1138,6 +1150,8 @@ public final class AppRestrictionController { volatile boolean mBgAutoRestrictedBucket; + volatile boolean mBgAutoRestrictAbusiveApps; + volatile boolean mRestrictedBucketEnabled; volatile long mBgAbusiveNotificationMinIntervalMs; @@ -1184,6 +1198,9 @@ public final class AppRestrictionController { case KEY_BG_AUTO_RESTRICTED_BUCKET_ON_BG_RESTRICTION: updateBgAutoRestrictedBucketChanged(); break; + case KEY_BG_AUTO_RESTRICT_ABUSIVE_APPS: + updateBgAutoRestrictAbusiveApps(); + break; case KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL: updateBgAbusiveNotificationMinimalInterval(); break; @@ -1232,6 +1249,7 @@ public final class AppRestrictionController { void updateDeviceConfig() { updateBgAutoRestrictedBucketChanged(); + updateBgAutoRestrictAbusiveApps(); updateBgAbusiveNotificationMinimalInterval(); updateBgLongFgsNotificationMinimalInterval(); updateBgPromptFgsWithNotiToBgRestricted(); @@ -1251,6 +1269,13 @@ public final class AppRestrictionController { } } + private void updateBgAutoRestrictAbusiveApps() { + mBgAutoRestrictAbusiveApps = DeviceConfig.getBoolean( + DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, + KEY_BG_AUTO_RESTRICT_ABUSIVE_APPS, + DEFAULT_BG_AUTO_RESTRICT_ABUSIVE_APPS); + } + private void updateBgAbusiveNotificationMinimalInterval() { mBgAbusiveNotificationMinIntervalMs = DeviceConfig.getLong( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, @@ -1313,6 +1338,10 @@ public final class AppRestrictionController { pw.print('='); pw.println(mBgAutoRestrictedBucket); pw.print(prefix); + pw.print(KEY_BG_AUTO_RESTRICT_ABUSIVE_APPS); + pw.print('='); + pw.println(mBgAutoRestrictAbusiveApps); + pw.print(prefix); pw.print(KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL); pw.print('='); pw.println(mBgAbusiveNotificationMinIntervalMs); @@ -1771,6 +1800,14 @@ public final class AppRestrictionController { return mRestrictionSettings.getRestrictionLevel(packageName, userId); } + /** + * @return Whether or not to move the app to restricted level automatically + * when system detects it's abusive. + */ + boolean isAutoRestrictAbusiveAppEnabled() { + return mConstantsObserver.mBgAutoRestrictAbusiveApps; + } + /** * @return The total foreground service durations for the given package/uid with given * foreground service type, or the total durations regardless the type if the given type is 0. diff --git a/services/core/java/com/android/server/am/BaseAppStateTimeSlotEventsTracker.java b/services/core/java/com/android/server/am/BaseAppStateTimeSlotEventsTracker.java index b8aee13ccdd70..a12d7deeeb052 100644 --- a/services/core/java/com/android/server/am/BaseAppStateTimeSlotEventsTracker.java +++ b/services/core/java/com/android/server/am/BaseAppStateTimeSlotEventsTracker.java @@ -301,6 +301,7 @@ abstract class BaseAppStateTimeSlotEventsTracker @RestrictionLevel int maxLevel) { synchronized (mLock) { final int level = mExcessiveEventPkgs.get(packageName, uid) == null + || !mTracker.mAppRestrictionController.isAutoRestrictAbusiveAppEnabled() ? RESTRICTION_LEVEL_ADAPTIVE_BUCKET : RESTRICTION_LEVEL_RESTRICTED_BUCKET; if (maxLevel > RESTRICTION_LEVEL_RESTRICTED_BUCKET) {