From f65253c3b4dd6bcd5f7310a71dc14bd07506f0de Mon Sep 17 00:00:00 2001 From: Jing Ji Date: Tue, 1 Mar 2022 11:23:46 -0800 Subject: [PATCH] Extend the min interval of alerting long-running FGS per package By default, we won't post the notification for alerting the long-running FGS for the same package until 30 days later. Bug: 203105544 Test: FrameworksMockingServicesTests:BackgroundRestrictionTest Change-Id: I028d384469c156cdc893ee8db4551173aa0d156a --- .../server/am/AppRestrictionController.java | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/am/AppRestrictionController.java b/services/core/java/com/android/server/am/AppRestrictionController.java index d07590fc8db5d..6e6cb879ae4f4 100644 --- a/services/core/java/com/android/server/am/AppRestrictionController.java +++ b/services/core/java/com/android/server/am/AppRestrictionController.java @@ -77,6 +77,7 @@ import static com.android.internal.notification.SystemNotificationChannels.ABUSI import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; import static com.android.server.am.AppFGSTracker.foregroundServiceTypeToIndex; +import static com.android.server.am.BaseAppStateTracker.ONE_DAY; import android.annotation.ElapsedRealtimeLong; import android.annotation.IntDef; @@ -647,6 +648,13 @@ public final class AppRestrictionController { static final String KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "abusive_notification_minimal_interval"; + /** + * The minimal interval in ms before posting a notification again on long running FGS + * from a certain package. + */ + static final String KEY_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL = + DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "long_fgs_notification_minimal_interval"; + /** * The behavior for an app with a FGS and its notification is still showing, when the system * detects it's abusive and should be put into bg restricted level. {@code true} - we'll @@ -661,8 +669,20 @@ public final class AppRestrictionController { static final String KEY_BG_RESTRICTION_EXEMPTED_PACKAGES = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "restriction_exempted_packages"; + /** + * Default value to {@link #mBgAutoRestrictedBucket}. + */ static final boolean DEFAULT_BG_AUTO_RESTRICTED_BUCKET_ON_BG_RESTRICTION = false; - static final long DEFAULT_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL_MS = 24 * 60 * 60 * 1000; + + /** + * Default value to {@link #mBgAbusiveNotificationMinIntervalMs}. + */ + static final long DEFAULT_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL_MS = ONE_DAY; + + /** + * Default value to {@link #mBgAbusiveNotificationMinIntervalMs}. + */ + static final long DEFAULT_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL_MS = 30 * ONE_DAY; /** * Default value to {@link #mBgPromptFgsWithNotiToBgRestricted}. @@ -673,7 +693,9 @@ public final class AppRestrictionController { volatile boolean mRestrictedBucketEnabled; - volatile long mBgNotificationMinIntervalMs; + volatile long mBgAbusiveNotificationMinIntervalMs; + + volatile long mBgLongFgsNotificationMinIntervalMs; /** * @see #KEY_BG_RESTRICTION_EXEMPTED_PACKAGES. @@ -706,6 +728,9 @@ public final class AppRestrictionController { case KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL: updateBgAbusiveNotificationMinimalInterval(); break; + case KEY_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL: + updateBgLongFgsNotificationMinimalInterval(); + break; case KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_TO_BG_RESTRICTED: updateBgPromptFgsWithNotiToBgRestricted(); break; @@ -743,6 +768,7 @@ public final class AppRestrictionController { void updateDeviceConfig() { updateBgAutoRestrictedBucketChanged(); updateBgAbusiveNotificationMinimalInterval(); + updateBgLongFgsNotificationMinimalInterval(); updateBgPromptFgsWithNotiToBgRestricted(); updateBgRestrictionExemptedPackages(); } @@ -759,12 +785,19 @@ public final class AppRestrictionController { } private void updateBgAbusiveNotificationMinimalInterval() { - mBgNotificationMinIntervalMs = DeviceConfig.getLong( + mBgAbusiveNotificationMinIntervalMs = DeviceConfig.getLong( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL, DEFAULT_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL_MS); } + private void updateBgLongFgsNotificationMinimalInterval() { + mBgLongFgsNotificationMinIntervalMs = DeviceConfig.getLong( + DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, + KEY_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL, + DEFAULT_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL_MS); + } + private void updateBgPromptFgsWithNotiToBgRestricted() { mBgPromptFgsWithNotiToBgRestricted = DeviceConfig.getBoolean( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, @@ -801,7 +834,11 @@ public final class AppRestrictionController { pw.print(prefix); pw.print(KEY_BG_ABUSIVE_NOTIFICATION_MINIMAL_INTERVAL); pw.print('='); - pw.println(mBgNotificationMinIntervalMs); + pw.println(mBgAbusiveNotificationMinIntervalMs); + pw.print(prefix); + pw.print(KEY_BG_LONG_FGS_NOTIFICATION_MINIMAL_INTERVAL); + pw.print('='); + pw.println(mBgLongFgsNotificationMinIntervalMs); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_TO_BG_RESTRICTED); pw.print('='); @@ -1650,6 +1687,17 @@ public final class AppRestrictionController { pendingIntent, packageName, uid, null); } + long getNotificationMinInterval(@NotificationType int notificationType) { + switch (notificationType) { + case NOTIFICATION_TYPE_ABUSIVE_CURRENT_DRAIN: + return mBgController.mConstantsObserver.mBgAbusiveNotificationMinIntervalMs; + case NOTIFICATION_TYPE_LONG_RUNNING_FGS: + return mBgController.mConstantsObserver.mBgLongFgsNotificationMinIntervalMs; + default: + return 0L; + } + } + int getNotificationIdIfNecessary(@NotificationType int notificationType, String packageName, int uid) { synchronized (mSettingsLock) { @@ -1663,7 +1711,7 @@ public final class AppRestrictionController { final long lastNotificationShownTimeElapsed = settings.getLastNotificationTime(notificationType); if (lastNotificationShownTimeElapsed != 0 && (lastNotificationShownTimeElapsed - + mBgController.mConstantsObserver.mBgNotificationMinIntervalMs > now)) { + + getNotificationMinInterval(notificationType) > now)) { if (DEBUG_BG_RESTRICTION_CONTROLLER) { Slog.i(TAG, "Not showing notification as last notification was shown " + TimeUtils.formatDuration(now - lastNotificationShownTimeElapsed)