Skip to content

Commit 5fbd220

Browse files
Yuri Linofficialputuid
authored andcommitted
Enforce zen rule limit on a package level.
This means that a single package with multiple different condition providers or configuration activities will correctly have all of their zen rules associated with the same package rather than each owner/activity having their rules counted separately. Bug: 235823407 Test: ZenModeHelperTest Change-Id: I35daf9a24f546ae25a78a2d841be39072cdc5641 Merged-In: I35daf9a24f546ae25a78a2d841be39072cdc5641 (cherry picked from commit f4e6939) (cherry picked from commit 468dd54) Merged-In: I35daf9a24f546ae25a78a2d841be39072cdc5641
1 parent a4db731 commit 5fbd220

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

services/core/java/com/android/server/notification/ZenModeHelper.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ public String addAutomaticZenRule(String pkg, AutomaticZenRule automaticZenRule,
321321
int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner())
322322
+ getCurrentInstanceCount(automaticZenRule.getConfigurationActivity())
323323
+ 1;
324-
if (newRuleInstanceCount > RULE_LIMIT_PER_PACKAGE
324+
int newPackageRuleCount = getPackageRuleCount(pkg) + 1;
325+
if (newPackageRuleCount > RULE_LIMIT_PER_PACKAGE
325326
|| (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) {
326327
throw new IllegalArgumentException("Rule instance limit exceeded");
327328
}
@@ -495,6 +496,23 @@ public int getCurrentInstanceCount(ComponentName cn) {
495496
return count;
496497
}
497498

499+
// Equivalent method to getCurrentInstanceCount, but for all rules associated with a specific
500+
// package rather than a condition provider service or activity.
501+
private int getPackageRuleCount(String pkg) {
502+
if (pkg == null) {
503+
return 0;
504+
}
505+
int count = 0;
506+
synchronized (mConfig) {
507+
for (ZenRule rule : mConfig.automaticRules.values()) {
508+
if (pkg.equals(rule.pkg)) {
509+
count++;
510+
}
511+
}
512+
}
513+
return count;
514+
}
515+
498516
public boolean canManageAutomaticZenRule(ZenRule rule) {
499517
final int callingUid = Binder.getCallingUid();
500518
if (callingUid == 0 || callingUid == Process.SYSTEM_UID) {

services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,9 @@ public void testAddAutomaticZenRule_beyondSystemLimit() {
15861586
ZenModeConfig.toScheduleConditionId(si),
15871587
new ZenPolicy.Builder().build(),
15881588
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1589-
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
1589+
// We need the package name to be something that's not "android" so there aren't any
1590+
// existing rules under that package.
1591+
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
15901592
assertNotNull(id);
15911593
}
15921594
try {
@@ -1596,7 +1598,37 @@ public void testAddAutomaticZenRule_beyondSystemLimit() {
15961598
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
15971599
new ZenPolicy.Builder().build(),
15981600
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1599-
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
1601+
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
1602+
fail("allowed too many rules to be created");
1603+
} catch (IllegalArgumentException e) {
1604+
// yay
1605+
}
1606+
}
1607+
1608+
@Test
1609+
public void testAddAutomaticZenRule_beyondSystemLimit_differentComponents() {
1610+
// Make sure the system limit is enforced per-package even with different component provider
1611+
// names.
1612+
for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) {
1613+
ScheduleInfo si = new ScheduleInfo();
1614+
si.startHour = i;
1615+
AutomaticZenRule zenRule = new AutomaticZenRule("name" + i,
1616+
null,
1617+
new ComponentName("android", "ScheduleConditionProvider" + i),
1618+
ZenModeConfig.toScheduleConditionId(si),
1619+
new ZenPolicy.Builder().build(),
1620+
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1621+
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
1622+
assertNotNull(id);
1623+
}
1624+
try {
1625+
AutomaticZenRule zenRule = new AutomaticZenRule("name",
1626+
null,
1627+
new ComponentName("android", "ScheduleConditionProviderFinal"),
1628+
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
1629+
new ZenPolicy.Builder().build(),
1630+
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1631+
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
16001632
fail("allowed too many rules to be created");
16011633
} catch (IllegalArgumentException e) {
16021634
// yay

0 commit comments

Comments
 (0)