Merge "Add a config to control if show the prompt on abusive apps" into tm-dev

This commit is contained in:
Jing Ji
2022-04-15 16:01:01 +00:00
committed by Android (Google) Code Review
4 changed files with 71 additions and 2 deletions

View File

@@ -5682,8 +5682,8 @@
restricted level.
-->
<array name="config_bg_current_drain_threshold_to_bg_restricted">
<item>10.0</item> <!-- regular device -->
<item>20.0</item> <!-- low ram device -->
<item>4.0</item> <!-- regular device -->
<item>8.0</item> <!-- low ram device -->
</array>
<!-- The background current drain monitoring window size. -->
@@ -5744,6 +5744,12 @@
-->
<bool name="config_bg_prompt_fgs_with_noti_to_bg_restricted">false</bool>
<!-- The behavior when the system detects it's abusive, should the system prompt the user
to put it into the bg restricted level.
True - we'll show the prompt to user, False - we'll not show it.
-->
<bool name="config_bg_prompt_abusive_apps_to_bg_restricted">false</bool>
<!-- The types of state where we'll exempt its battery usage during that state.
The state here must be one or a combination of STATE_TYPE_* in BaseAppStateTracker.
-->

View File

@@ -4765,6 +4765,7 @@
<java-symbol type="integer" name="config_bg_current_drain_media_playback_min_duration" />
<java-symbol type="integer" name="config_bg_current_drain_location_min_duration" />
<java-symbol type="bool" name="config_bg_prompt_fgs_with_noti_to_bg_restricted" />
<java-symbol type="bool" name="config_bg_prompt_abusive_apps_to_bg_restricted" />
<java-symbol type="integer" name="config_bg_current_drain_exempted_types" />
<java-symbol type="bool" name="config_bg_current_drain_high_threshold_by_bg_location" />
<java-symbol type="drawable" name="ic_swap_horiz" />

View File

@@ -1094,6 +1094,14 @@ public final class AppRestrictionController {
static final String KEY_BG_RESTRICTION_EXEMPTED_PACKAGES =
DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "restriction_exempted_packages";
/**
* Whether or not to show the notification for abusive apps, i.e. when the system
* detects it's draining significant amount of battery in the background.
* {@code true} - we'll show the prompt to user, {@code false} - we'll not show it.
*/
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 #mBgAutoRestrictedBucket}.
*/
@@ -1119,6 +1127,11 @@ public final class AppRestrictionController {
*/
final boolean mDefaultBgPromptFgsWithNotiToBgRestricted;
/**
* Default value to {@link #mBgPromptAbusiveAppsToBgRestricted}.
*/
final boolean mDefaultBgPromptAbusiveAppToBgRestricted;
volatile boolean mBgAutoRestrictedBucket;
volatile boolean mRestrictedBucketEnabled;
@@ -1144,10 +1157,17 @@ public final class AppRestrictionController {
*/
volatile boolean mBgPromptFgsWithNotiOnLongRunning;
/**
* @see #KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED.
*/
volatile boolean mBgPromptAbusiveAppsToBgRestricted;
ConstantsObserver(Handler handler, Context context) {
super(handler);
mDefaultBgPromptFgsWithNotiToBgRestricted = context.getResources().getBoolean(
com.android.internal.R.bool.config_bg_prompt_fgs_with_noti_to_bg_restricted);
mDefaultBgPromptAbusiveAppToBgRestricted = context.getResources().getBoolean(
com.android.internal.R.bool.config_bg_prompt_abusive_apps_to_bg_restricted);
}
@Override
@@ -1172,6 +1192,9 @@ public final class AppRestrictionController {
case KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING:
updateBgPromptFgsWithNotiOnLongRunning();
break;
case KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED:
updateBgPromptAbusiveAppToBgRestricted();
break;
case KEY_BG_RESTRICTION_EXEMPTED_PACKAGES:
updateBgRestrictionExemptedPackages();
break;
@@ -1209,6 +1232,7 @@ public final class AppRestrictionController {
updateBgLongFgsNotificationMinimalInterval();
updateBgPromptFgsWithNotiToBgRestricted();
updateBgPromptFgsWithNotiOnLongRunning();
updateBgPromptAbusiveAppToBgRestricted();
updateBgRestrictionExemptedPackages();
}
@@ -1251,6 +1275,13 @@ public final class AppRestrictionController {
DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING);
}
private void updateBgPromptAbusiveAppToBgRestricted() {
mBgPromptAbusiveAppsToBgRestricted = DeviceConfig.getBoolean(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED,
mDefaultBgPromptAbusiveAppToBgRestricted);
}
private void updateBgRestrictionExemptedPackages() {
final String settings = DeviceConfig.getString(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
@@ -1290,6 +1321,10 @@ public final class AppRestrictionController {
pw.print('=');
pw.println(mBgPromptFgsWithNotiToBgRestricted);
pw.print(prefix);
pw.print(KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED);
pw.print('=');
pw.println(mBgPromptAbusiveAppsToBgRestricted);
pw.print(prefix);
pw.print(KEY_BG_RESTRICTION_EXEMPTED_PACKAGES);
pw.print('=');
pw.println(mBgRestrictionExemptedPackages.toString());
@@ -2296,6 +2331,13 @@ public final class AppRestrictionController {
}
void postRequestBgRestrictedIfNecessary(String packageName, int uid) {
if (!mBgController.mConstantsObserver.mBgPromptAbusiveAppsToBgRestricted) {
if (DEBUG_BG_RESTRICTION_CONTROLLER) {
Slog.i(TAG, "Not requesting bg-restriction due to config");
}
return;
}
final Intent intent = new Intent(Settings.ACTION_VIEW_ADVANCED_POWER_USAGE_DETAIL);
intent.setData(Uri.fromParts(PACKAGE_SCHEME, packageName, null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

View File

@@ -585,6 +585,7 @@ public final class BackgroundRestrictionTest {
DeviceConfigSession<Float> bgCurrentDrainRestrictedBucketThreshold = null;
DeviceConfigSession<Float> bgCurrentDrainBgRestrictedThreshold = null;
DeviceConfigSession<Boolean> bgPromptFgsWithNotiToBgRestricted = null;
DeviceConfigSession<Boolean> bgPromptAbusiveAppToBgRestricted = null;
DeviceConfigSession<Long> bgNotificationMinInterval = null;
DeviceConfigSession<Integer> bgBatteryExemptionTypes = null;
DeviceConfigSession<Boolean> bgCurrentDrainDecoupleThresholds = null;
@@ -642,6 +643,14 @@ public final class BackgroundRestrictionTest {
R.bool.config_bg_prompt_fgs_with_noti_to_bg_restricted));
bgPromptFgsWithNotiToBgRestricted.set(true);
bgPromptAbusiveAppToBgRestricted = new DeviceConfigSession<>(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
ConstantsObserver.KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED,
DeviceConfig::getBoolean,
mContext.getResources().getBoolean(
R.bool.config_bg_prompt_abusive_apps_to_bg_restricted));
bgPromptAbusiveAppToBgRestricted.set(true);
bgNotificationMinInterval = new DeviceConfigSession<>(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
ConstantsObserver.KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL,
@@ -1055,6 +1064,7 @@ public final class BackgroundRestrictionTest {
closeIfNotNull(bgCurrentDrainRestrictedBucketThreshold);
closeIfNotNull(bgCurrentDrainBgRestrictedThreshold);
closeIfNotNull(bgPromptFgsWithNotiToBgRestricted);
closeIfNotNull(bgPromptAbusiveAppToBgRestricted);
closeIfNotNull(bgNotificationMinInterval);
closeIfNotNull(bgBatteryExemptionTypes);
closeIfNotNull(bgCurrentDrainDecoupleThresholds);
@@ -1613,6 +1623,7 @@ public final class BackgroundRestrictionTest {
DeviceConfigSession<String> bgPermissionsInMonitor = null;
DeviceConfigSession<Boolean> bgCurrentDrainHighThresholdByBgLocation = null;
DeviceConfigSession<Boolean> bgCurrentDrainDecoupleThresholds = null;
DeviceConfigSession<Boolean> bgPromptAbusiveAppToBgRestricted = null;
mBgRestrictionController.addAppBackgroundRestrictionListener(listener);
@@ -1751,6 +1762,14 @@ public final class BackgroundRestrictionTest {
AppBatteryPolicy.DEFAULT_BG_CURRENT_DRAIN_DECOUPLE_THRESHOLD);
bgCurrentDrainDecoupleThresholds.set(true);
bgPromptAbusiveAppToBgRestricted = new DeviceConfigSession<>(
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
ConstantsObserver.KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED,
DeviceConfig::getBoolean,
mContext.getResources().getBoolean(
R.bool.config_bg_prompt_abusive_apps_to_bg_restricted));
bgPromptAbusiveAppToBgRestricted.set(true);
mCurrentTimeMillis = 10_000L;
doReturn(mCurrentTimeMillis - windowMs).when(stats).getStatsStartTimestamp();
doReturn(mCurrentTimeMillis).when(stats).getStatsEndTimestamp();
@@ -2168,6 +2187,7 @@ public final class BackgroundRestrictionTest {
closeIfNotNull(bgBatteryExemptionTypes);
closeIfNotNull(bgPermissionMonitorEnabled);
closeIfNotNull(bgPermissionsInMonitor);
closeIfNotNull(bgPromptAbusiveAppToBgRestricted);
closeIfNotNull(bgCurrentDrainHighThresholdByBgLocation);
closeIfNotNull(bgCurrentDrainDecoupleThresholds);
}