From 42d9c6d3149b60a2a86acbda570ea43b9b835494 Mon Sep 17 00:00:00 2001 From: Yuri Lin Date: Mon, 28 Aug 2023 17:25:08 -0400 Subject: Apply default policy for enabled rules without custom policy When an automatic zen rule doesn't have a custom policy set, we were not applying the default rule until the end to fill in any settings that were unset by any other rule's specific policy. This means that when an enabled rule used the default policy, it would not apply even if the merged consolidated policy would otherwise have kept the default policy's setting due to it being stricter. Fixes: 297415089 Test: manual, ZenModeHelperTest Change-Id: I641fdb53f023c0b37b5871550e59962717ef529c --- .../android/server/notification/ZenModeHelper.java | 5 +- .../server/notification/ZenModeHelperTest.java | 137 +++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java index 1f5bd3e0cc60..030c5507e8be 100644 --- a/services/core/java/com/android/server/notification/ZenModeHelper.java +++ b/services/core/java/com/android/server/notification/ZenModeHelper.java @@ -1104,8 +1104,11 @@ public class ZenModeHelper { .allowAlarms(true) .allowMedia(true) .build()); - } else { + } else if (rule.zenPolicy != null) { policy.apply(rule.zenPolicy); + } else { + // active rule with no specified policy inherits the default settings + policy.apply(mConfig.toZenPolicy()); } } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java index 3ee75de23fdb..89d44b380911 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java @@ -2439,6 +2439,143 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertEquals(12345, mZenModeEventLogger.getPackageUid(4)); } + @Test + public void testUpdateConsolidatedPolicy_defaultRulesOnly() { + setupZenConfig(); + + // When there's one automatic rule active and it doesn't specify a policy, test that the + // resulting consolidated policy is one that matches the default rule settings. + AutomaticZenRule zenRule = new AutomaticZenRule("name", + null, + new ComponentName(CUSTOM_PKG_NAME, "ScheduleConditionProvider"), + ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), + null, + NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); + String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, "test", + Process.SYSTEM_UID, true); + + // enable the rule + mZenModeHelper.setAutomaticZenRuleState(id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), + Process.SYSTEM_UID, true); + + // inspect the consolidated policy. Based on setupZenConfig() values. + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowAlarms()); + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowMedia()); + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowSystem()); + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowReminders()); + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowCalls()); + assertEquals(PRIORITY_SENDERS_STARRED, mZenModeHelper.mConsolidatedPolicy.allowCallsFrom()); + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowMessages()); + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowConversations()); + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowRepeatCallers()); + assertFalse(mZenModeHelper.mConsolidatedPolicy.showBadges()); + } + + @Test + public void testUpdateConsolidatedPolicy_customPolicyOnly() { + setupZenConfig(); + + // when there's only one automatic rule active and it has a custom policy, make sure that's + // what the consolidated policy reflects whether or not it's stricter than what the default + // would specify. + ZenPolicy customPolicy = new ZenPolicy.Builder() + .allowAlarms(true) // more lenient than default + .allowMedia(true) // more lenient than default + .allowRepeatCallers(false) // more restrictive than default + .allowCalls(ZenPolicy.PEOPLE_TYPE_NONE) // more restrictive than default + .showBadges(true) // more lenient + .showPeeking(false) // more restrictive + .build(); + + AutomaticZenRule zenRule = new AutomaticZenRule("name", + null, + new ComponentName(CUSTOM_PKG_NAME, "ScheduleConditionProvider"), + ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), + customPolicy, + NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); + String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, "test", + Process.SYSTEM_UID, true); + + // enable the rule; this will update the consolidated policy + mZenModeHelper.setAutomaticZenRuleState(id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), + Process.SYSTEM_UID, true); + + // since this is the only active rule, the consolidated policy should match the custom + // policy for every field specified, and take default values for unspecified things + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowAlarms()); // custom + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowMedia()); // custom + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowSystem()); // default + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowReminders()); // default + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowCalls()); // custom + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowMessages()); // default + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowConversations()); // default + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowRepeatCallers()); // custom + assertTrue(mZenModeHelper.mConsolidatedPolicy.showBadges()); // custom + assertFalse(mZenModeHelper.mConsolidatedPolicy.showPeeking()); // custom + } + + @Test + public void testUpdateConsolidatedPolicy_defaultAndCustomActive() { + setupZenConfig(); + + // when there are two rules active, one inheriting the default policy and one setting its + // own custom policy, they should be merged to form the most restrictive combination. + + // rule 1: no custom policy + AutomaticZenRule zenRule = new AutomaticZenRule("name", + null, + new ComponentName(CUSTOM_PKG_NAME, "ScheduleConditionProvider"), + ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), + null, + NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); + String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, "test", + Process.SYSTEM_UID, true); + + // enable rule 1 + mZenModeHelper.setAutomaticZenRuleState(id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), + Process.SYSTEM_UID, true); + + // custom policy for rule 2 + ZenPolicy customPolicy = new ZenPolicy.Builder() + .allowAlarms(true) // more lenient than default + .allowMedia(true) // more lenient than default + .allowRepeatCallers(false) // more restrictive than default + .allowCalls(ZenPolicy.PEOPLE_TYPE_NONE) // more restrictive than default + .showBadges(true) // more lenient + .showPeeking(false) // more restrictive + .build(); + + AutomaticZenRule zenRule2 = new AutomaticZenRule("name2", + null, + new ComponentName(CUSTOM_PKG_NAME, "ScheduleConditionProvider"), + ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), + customPolicy, + NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); + String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, + "test", Process.SYSTEM_UID, true); + + // enable rule 2; this will update the consolidated policy + mZenModeHelper.setAutomaticZenRuleState(id2, + new Condition(zenRule2.getConditionId(), "", STATE_TRUE), + Process.SYSTEM_UID, true); + + // now both rules should be on, and the consolidated policy should reflect the most + // restrictive option of each of the two + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowAlarms()); // default stricter + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowMedia()); // default stricter + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowSystem()); // default, unset in custom + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowReminders()); // default + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowCalls()); // custom stricter + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowMessages()); // default, unset in custom + assertTrue(mZenModeHelper.mConsolidatedPolicy.allowConversations()); // default + assertFalse(mZenModeHelper.mConsolidatedPolicy.allowRepeatCallers()); // custom stricter + assertFalse(mZenModeHelper.mConsolidatedPolicy.showBadges()); // default stricter + assertFalse(mZenModeHelper.mConsolidatedPolicy.showPeeking()); // custom stricter + } + private void setupZenConfig() { mZenModeHelper.mZenMode = Global.ZEN_MODE_OFF; mZenModeHelper.mConfig.allowAlarms = false; -- cgit v1.2.3-59-g8ed1b