Loading services/core/java/com/android/server/am/AppRestrictionController.java +43 −0 Original line number Diff line number Diff line Loading @@ -1114,6 +1114,14 @@ public final class AppRestrictionController { static final String KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "prompt_fgs_with_noti_on_long_running"; /** * The behavior for an app with a FGS, when the system detects it's running for * a very long time, should we prompt the user. * {@code true} - we'll show the prompt to user, {@code false} - we'll not show it. */ static final String KEY_BG_PROMPT_FGS_ON_LONG_RUNNING = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "prompt_fgs_on_long_running"; /** * The list of packages to be exempted from all these background restrictions. */ Loading Loading @@ -1153,6 +1161,11 @@ public final class AppRestrictionController { */ static final boolean DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING = false; /** * Default value to {@link #mBgPromptFgsOnLongRunning}. */ static final boolean DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING = true; /** * Default value to {@link #mBgPromptFgsWithNotiToBgRestricted}. */ Loading Loading @@ -1190,6 +1203,11 @@ public final class AppRestrictionController { */ volatile boolean mBgPromptFgsWithNotiOnLongRunning; /** * @see #KEY_BG_PROMPT_FGS_ON_LONG_RUNNING. */ volatile boolean mBgPromptFgsOnLongRunning; /** * @see #KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED. */ Loading Loading @@ -1228,6 +1246,9 @@ public final class AppRestrictionController { case KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING: updateBgPromptFgsWithNotiOnLongRunning(); break; case KEY_BG_PROMPT_FGS_ON_LONG_RUNNING: updateBgPromptFgsOnLongRunning(); break; case KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED: updateBgPromptAbusiveAppToBgRestricted(); break; Loading Loading @@ -1269,6 +1290,7 @@ public final class AppRestrictionController { updateBgLongFgsNotificationMinimalInterval(); updateBgPromptFgsWithNotiToBgRestricted(); updateBgPromptFgsWithNotiOnLongRunning(); updateBgPromptFgsOnLongRunning(); updateBgPromptAbusiveAppToBgRestricted(); updateBgRestrictionExemptedPackages(); } Loading Loading @@ -1319,6 +1341,13 @@ public final class AppRestrictionController { DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); } private void updateBgPromptFgsOnLongRunning() { mBgPromptFgsOnLongRunning = DeviceConfig.getBoolean( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); } private void updateBgPromptAbusiveAppToBgRestricted() { mBgPromptAbusiveAppsToBgRestricted = DeviceConfig.getBoolean( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, Loading Loading @@ -1365,6 +1394,14 @@ public final class AppRestrictionController { pw.print('='); pw.println(mBgLongFgsNotificationMinIntervalMs); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_ON_LONG_RUNNING); pw.print('='); pw.println(mBgPromptFgsOnLongRunning); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); pw.print('='); pw.println(mBgPromptFgsWithNotiOnLongRunning); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_TO_BG_RESTRICTED); pw.print('='); pw.println(mBgPromptFgsWithNotiToBgRestricted); Loading Loading @@ -2500,6 +2537,12 @@ public final class AppRestrictionController { ActivityManager.isLowRamDeviceStatic(), mBgController.getRestrictionLevel(uid)); PendingIntent pendingIntent; if (!mBgController.mConstantsObserver.mBgPromptFgsOnLongRunning) { if (DEBUG_BG_RESTRICTION_CONTROLLER) { Slog.i(TAG, "Long-running FGS prompt is disabled."); } return; } if (!mBgController.mConstantsObserver.mBgPromptFgsWithNotiOnLongRunning && mBgController.hasForegroundServiceNotifications(packageName, uid)) { if (DEBUG_BG_RESTRICTION_CONTROLLER) { Loading services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java +35 −0 Original line number Diff line number Diff line Loading @@ -1141,6 +1141,7 @@ public final class BackgroundRestrictionTest { DeviceConfigSession<Long> longRunningFGSWindow = null; DeviceConfigSession<Long> longRunningFGSThreshold = null; DeviceConfigSession<Boolean> longRunningFGSWithNotification = null; DeviceConfigSession<Boolean> longRunningFGS = null; try { longRunningFGSMonitor = new DeviceConfigSession<>( Loading Loading @@ -1171,6 +1172,13 @@ public final class BackgroundRestrictionTest { ConstantsObserver.DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); longRunningFGSWithNotification.set(true); longRunningFGS = new DeviceConfigSession<>( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, ConstantsObserver.KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DeviceConfig::getBoolean, ConstantsObserver.DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); longRunningFGS.set(true); // Basic case mAppFGSTracker.onForegroundServiceStateChanged(testPkgName1, testUid1, testPid1, true); Loading Loading @@ -1214,6 +1222,23 @@ public final class BackgroundRestrictionTest { testPid2, false); checkNotificationGone(testPkgName2, timeout(windowMs), notificationId); // Turn OFF the notification. longRunningFGS.set(false); clearInvocations(mInjector.getNotificationManager()); mBgRestrictionController.resetRestrictionSettings(); // Start the FGS again. mAppFGSTracker.onForegroundServiceStateChanged(testPkgName2, testUid2, testPid2, true); // Verify we do NOT have the notification. checkNotificationShown( new String[] {testPkgName2}, timeout(windowMs * 2).times(0), false); // Stop this FGS mAppFGSTracker.onForegroundServiceStateChanged(testPkgName2, testUid2, testPid2, false); // Turn it back ON. longRunningFGS.set(true); // Start over with concurrent cases. clearInvocations(mInjector.getNotificationManager()); mBgRestrictionController.resetRestrictionSettings(); Loading Loading @@ -1306,6 +1331,7 @@ public final class BackgroundRestrictionTest { closeIfNotNull(longRunningFGSWindow); closeIfNotNull(longRunningFGSThreshold); closeIfNotNull(longRunningFGSWithNotification); closeIfNotNull(longRunningFGS); } } Loading @@ -1332,6 +1358,7 @@ public final class BackgroundRestrictionTest { DeviceConfigSession<Long> mediaPlaybackFGSThreshold = null; DeviceConfigSession<Long> locationFGSThreshold = null; DeviceConfigSession<Boolean> longRunningFGSWithNotification = null; DeviceConfigSession<Boolean> longRunningFGS = null; doReturn(testPkgName1).when(mInjector).getPackageName(testPid1); doReturn(testPkgName2).when(mInjector).getPackageName(testPid2); Loading Loading @@ -1379,6 +1406,13 @@ public final class BackgroundRestrictionTest { ConstantsObserver.DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); longRunningFGSWithNotification.set(true); longRunningFGS = new DeviceConfigSession<>( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, ConstantsObserver.KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DeviceConfig::getBoolean, ConstantsObserver.DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); longRunningFGS.set(true); // Long-running FGS with type "location", but ran for a very short time. runTestLongFGSExemptionOnce(testPkgName1, testUid1, testPid1, FOREGROUND_SERVICE_TYPE_LOCATION, 0, null, OP_NONE, null, null, Loading Loading @@ -1487,6 +1521,7 @@ public final class BackgroundRestrictionTest { closeIfNotNull(mediaPlaybackFGSThreshold); closeIfNotNull(locationFGSThreshold); closeIfNotNull(longRunningFGSWithNotification); closeIfNotNull(longRunningFGS); } } Loading Loading
services/core/java/com/android/server/am/AppRestrictionController.java +43 −0 Original line number Diff line number Diff line Loading @@ -1114,6 +1114,14 @@ public final class AppRestrictionController { static final String KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "prompt_fgs_with_noti_on_long_running"; /** * The behavior for an app with a FGS, when the system detects it's running for * a very long time, should we prompt the user. * {@code true} - we'll show the prompt to user, {@code false} - we'll not show it. */ static final String KEY_BG_PROMPT_FGS_ON_LONG_RUNNING = DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "prompt_fgs_on_long_running"; /** * The list of packages to be exempted from all these background restrictions. */ Loading Loading @@ -1153,6 +1161,11 @@ public final class AppRestrictionController { */ static final boolean DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING = false; /** * Default value to {@link #mBgPromptFgsOnLongRunning}. */ static final boolean DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING = true; /** * Default value to {@link #mBgPromptFgsWithNotiToBgRestricted}. */ Loading Loading @@ -1190,6 +1203,11 @@ public final class AppRestrictionController { */ volatile boolean mBgPromptFgsWithNotiOnLongRunning; /** * @see #KEY_BG_PROMPT_FGS_ON_LONG_RUNNING. */ volatile boolean mBgPromptFgsOnLongRunning; /** * @see #KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED. */ Loading Loading @@ -1228,6 +1246,9 @@ public final class AppRestrictionController { case KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING: updateBgPromptFgsWithNotiOnLongRunning(); break; case KEY_BG_PROMPT_FGS_ON_LONG_RUNNING: updateBgPromptFgsOnLongRunning(); break; case KEY_BG_PROMPT_ABUSIVE_APPS_TO_BG_RESTRICTED: updateBgPromptAbusiveAppToBgRestricted(); break; Loading Loading @@ -1269,6 +1290,7 @@ public final class AppRestrictionController { updateBgLongFgsNotificationMinimalInterval(); updateBgPromptFgsWithNotiToBgRestricted(); updateBgPromptFgsWithNotiOnLongRunning(); updateBgPromptFgsOnLongRunning(); updateBgPromptAbusiveAppToBgRestricted(); updateBgRestrictionExemptedPackages(); } Loading Loading @@ -1319,6 +1341,13 @@ public final class AppRestrictionController { DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); } private void updateBgPromptFgsOnLongRunning() { mBgPromptFgsOnLongRunning = DeviceConfig.getBoolean( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); } private void updateBgPromptAbusiveAppToBgRestricted() { mBgPromptAbusiveAppsToBgRestricted = DeviceConfig.getBoolean( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, Loading Loading @@ -1365,6 +1394,14 @@ public final class AppRestrictionController { pw.print('='); pw.println(mBgLongFgsNotificationMinIntervalMs); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_ON_LONG_RUNNING); pw.print('='); pw.println(mBgPromptFgsOnLongRunning); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); pw.print('='); pw.println(mBgPromptFgsWithNotiOnLongRunning); pw.print(prefix); pw.print(KEY_BG_PROMPT_FGS_WITH_NOTIFICATION_TO_BG_RESTRICTED); pw.print('='); pw.println(mBgPromptFgsWithNotiToBgRestricted); Loading Loading @@ -2500,6 +2537,12 @@ public final class AppRestrictionController { ActivityManager.isLowRamDeviceStatic(), mBgController.getRestrictionLevel(uid)); PendingIntent pendingIntent; if (!mBgController.mConstantsObserver.mBgPromptFgsOnLongRunning) { if (DEBUG_BG_RESTRICTION_CONTROLLER) { Slog.i(TAG, "Long-running FGS prompt is disabled."); } return; } if (!mBgController.mConstantsObserver.mBgPromptFgsWithNotiOnLongRunning && mBgController.hasForegroundServiceNotifications(packageName, uid)) { if (DEBUG_BG_RESTRICTION_CONTROLLER) { Loading
services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java +35 −0 Original line number Diff line number Diff line Loading @@ -1141,6 +1141,7 @@ public final class BackgroundRestrictionTest { DeviceConfigSession<Long> longRunningFGSWindow = null; DeviceConfigSession<Long> longRunningFGSThreshold = null; DeviceConfigSession<Boolean> longRunningFGSWithNotification = null; DeviceConfigSession<Boolean> longRunningFGS = null; try { longRunningFGSMonitor = new DeviceConfigSession<>( Loading Loading @@ -1171,6 +1172,13 @@ public final class BackgroundRestrictionTest { ConstantsObserver.DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); longRunningFGSWithNotification.set(true); longRunningFGS = new DeviceConfigSession<>( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, ConstantsObserver.KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DeviceConfig::getBoolean, ConstantsObserver.DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); longRunningFGS.set(true); // Basic case mAppFGSTracker.onForegroundServiceStateChanged(testPkgName1, testUid1, testPid1, true); Loading Loading @@ -1214,6 +1222,23 @@ public final class BackgroundRestrictionTest { testPid2, false); checkNotificationGone(testPkgName2, timeout(windowMs), notificationId); // Turn OFF the notification. longRunningFGS.set(false); clearInvocations(mInjector.getNotificationManager()); mBgRestrictionController.resetRestrictionSettings(); // Start the FGS again. mAppFGSTracker.onForegroundServiceStateChanged(testPkgName2, testUid2, testPid2, true); // Verify we do NOT have the notification. checkNotificationShown( new String[] {testPkgName2}, timeout(windowMs * 2).times(0), false); // Stop this FGS mAppFGSTracker.onForegroundServiceStateChanged(testPkgName2, testUid2, testPid2, false); // Turn it back ON. longRunningFGS.set(true); // Start over with concurrent cases. clearInvocations(mInjector.getNotificationManager()); mBgRestrictionController.resetRestrictionSettings(); Loading Loading @@ -1306,6 +1331,7 @@ public final class BackgroundRestrictionTest { closeIfNotNull(longRunningFGSWindow); closeIfNotNull(longRunningFGSThreshold); closeIfNotNull(longRunningFGSWithNotification); closeIfNotNull(longRunningFGS); } } Loading @@ -1332,6 +1358,7 @@ public final class BackgroundRestrictionTest { DeviceConfigSession<Long> mediaPlaybackFGSThreshold = null; DeviceConfigSession<Long> locationFGSThreshold = null; DeviceConfigSession<Boolean> longRunningFGSWithNotification = null; DeviceConfigSession<Boolean> longRunningFGS = null; doReturn(testPkgName1).when(mInjector).getPackageName(testPid1); doReturn(testPkgName2).when(mInjector).getPackageName(testPid2); Loading Loading @@ -1379,6 +1406,13 @@ public final class BackgroundRestrictionTest { ConstantsObserver.DEFAULT_BG_PROMPT_FGS_WITH_NOTIFICATION_ON_LONG_RUNNING); longRunningFGSWithNotification.set(true); longRunningFGS = new DeviceConfigSession<>( DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, ConstantsObserver.KEY_BG_PROMPT_FGS_ON_LONG_RUNNING, DeviceConfig::getBoolean, ConstantsObserver.DEFAULT_BG_PROMPT_FGS_ON_LONG_RUNNING); longRunningFGS.set(true); // Long-running FGS with type "location", but ran for a very short time. runTestLongFGSExemptionOnce(testPkgName1, testUid1, testPid1, FOREGROUND_SERVICE_TYPE_LOCATION, 0, null, OP_NONE, null, null, Loading Loading @@ -1487,6 +1521,7 @@ public final class BackgroundRestrictionTest { closeIfNotNull(mediaPlaybackFGSThreshold); closeIfNotNull(locationFGSThreshold); closeIfNotNull(longRunningFGSWithNotification); closeIfNotNull(longRunningFGS); } } Loading