Loading services/core/java/com/android/server/notification/ZenModeHelper.java +19 −1 Original line number Diff line number Diff line Loading @@ -326,7 +326,8 @@ public class ZenModeHelper { int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner()) + getCurrentInstanceCount(automaticZenRule.getConfigurationActivity()) + 1; if (newRuleInstanceCount > RULE_LIMIT_PER_PACKAGE int newPackageRuleCount = getPackageRuleCount(pkg) + 1; if (newPackageRuleCount > RULE_LIMIT_PER_PACKAGE || (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) { throw new IllegalArgumentException("Rule instance limit exceeded"); } Loading Loading @@ -507,6 +508,23 @@ public class ZenModeHelper { return count; } // Equivalent method to getCurrentInstanceCount, but for all rules associated with a specific // package rather than a condition provider service or activity. private int getPackageRuleCount(String pkg) { if (pkg == null) { return 0; } int count = 0; synchronized (mConfig) { for (ZenRule rule : mConfig.automaticRules.values()) { if (pkg.equals(rule.getPkg())) { count++; } } } return count; } public boolean canManageAutomaticZenRule(ZenRule rule) { final int callingUid = Binder.getCallingUid(); if (callingUid == 0 || callingUid == Process.SYSTEM_UID) { Loading services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +33 −2 Original line number Diff line number Diff line Loading @@ -1618,7 +1618,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test"); // We need the package name to be something that's not "android" so there aren't any // existing rules under that package. String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); assertNotNull(id); } try { Loading @@ -1628,12 +1630,41 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test"); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay } } @Test public void testAddAutomaticZenRule_beyondSystemLimit_differentComponents() { // Make sure the system limit is enforced per-package even with different component provider // names. for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) { ScheduleInfo si = new ScheduleInfo(); si.startHour = i; AutomaticZenRule zenRule = new AutomaticZenRule("name" + i, null, new ComponentName("android", "ScheduleConditionProvider" + i), ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); assertNotNull(id); } try { AutomaticZenRule zenRule = new AutomaticZenRule("name", null, new ComponentName("android", "ScheduleConditionProviderFinal"), ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay } } @Test Loading Loading
services/core/java/com/android/server/notification/ZenModeHelper.java +19 −1 Original line number Diff line number Diff line Loading @@ -326,7 +326,8 @@ public class ZenModeHelper { int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner()) + getCurrentInstanceCount(automaticZenRule.getConfigurationActivity()) + 1; if (newRuleInstanceCount > RULE_LIMIT_PER_PACKAGE int newPackageRuleCount = getPackageRuleCount(pkg) + 1; if (newPackageRuleCount > RULE_LIMIT_PER_PACKAGE || (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) { throw new IllegalArgumentException("Rule instance limit exceeded"); } Loading Loading @@ -507,6 +508,23 @@ public class ZenModeHelper { return count; } // Equivalent method to getCurrentInstanceCount, but for all rules associated with a specific // package rather than a condition provider service or activity. private int getPackageRuleCount(String pkg) { if (pkg == null) { return 0; } int count = 0; synchronized (mConfig) { for (ZenRule rule : mConfig.automaticRules.values()) { if (pkg.equals(rule.getPkg())) { count++; } } } return count; } public boolean canManageAutomaticZenRule(ZenRule rule) { final int callingUid = Binder.getCallingUid(); if (callingUid == 0 || callingUid == Process.SYSTEM_UID) { Loading
services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +33 −2 Original line number Diff line number Diff line Loading @@ -1618,7 +1618,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test"); // We need the package name to be something that's not "android" so there aren't any // existing rules under that package. String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); assertNotNull(id); } try { Loading @@ -1628,12 +1630,41 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test"); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay } } @Test public void testAddAutomaticZenRule_beyondSystemLimit_differentComponents() { // Make sure the system limit is enforced per-package even with different component provider // names. for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) { ScheduleInfo si = new ScheduleInfo(); si.startHour = i; AutomaticZenRule zenRule = new AutomaticZenRule("name" + i, null, new ComponentName("android", "ScheduleConditionProvider" + i), ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); assertNotNull(id); } try { AutomaticZenRule zenRule = new AutomaticZenRule("name", null, new ComponentName("android", "ScheduleConditionProviderFinal"), ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test"); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay } } @Test Loading