Loading services/core/java/com/android/server/notification/ZenModeHelper.java +25 −0 Original line number Diff line number Diff line Loading @@ -453,6 +453,8 @@ public class ZenModeHelper { ZenRule rule = new ZenRule(); populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true); newConfig.automaticRules.put(rule.id, rule); maybeReplaceDefaultRule(newConfig, automaticZenRule); if (setConfigLocked(newConfig, origin, reason, rule.component, true, callingUid)) { return rule.id; } else { Loading @@ -461,6 +463,25 @@ public class ZenModeHelper { } } private static void maybeReplaceDefaultRule(ZenModeConfig config, AutomaticZenRule addedRule) { if (!Flags.modesApi()) { return; } if (addedRule.getType() == AutomaticZenRule.TYPE_BEDTIME) { // Delete a built-in disabled "Sleeping" rule when a BEDTIME rule is added; it may have // smarter triggers and it will prevent confusion about which one to use. // Note: we must not verify canManageAutomaticZenRule here, since most likely they // won't have the same owner (sleeping - system; bedtime - DWB). ZenRule sleepingRule = config.automaticRules.get( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); if (sleepingRule != null && !sleepingRule.enabled && sleepingRule.canBeUpdatedByApp() /* meaning it's not user-customized */) { config.automaticRules.remove(ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); } } } public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZenRule, @ConfigChangeOrigin int origin, String reason, int callingUid) { ZenModeConfig newConfig; Loading Loading @@ -1377,6 +1398,10 @@ public class ZenModeHelper { // reset zen automatic rules to default on restore or upgrade if: // - doesn't already have default rules and // - all previous automatic rules were disabled // // Note: we don't need to check to avoid restoring the Sleeping rule if there is a // TYPE_BEDTIME rule because the config is from an old version and thus by // definition cannot have a rule with TYPE_BEDTIME (or any other type). config.automaticRules = new ArrayMap<>(); for (ZenRule rule : mDefaultConfig.automaticRules.values()) { config.automaticRules.put(rule.id, rule); Loading services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +65 −2 Original line number Diff line number Diff line Loading @@ -185,8 +185,9 @@ import java.util.concurrent.TimeUnit; @TestableLooper.RunWithLooper public class ZenModeHelperTest extends UiServiceTestCase { private static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE"; private static final String SCHEDULE_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE"; private static final String EVENTS_DEFAULT_RULE_ID = ZenModeConfig.EVENTS_DEFAULT_RULE_ID; private static final String SCHEDULE_DEFAULT_RULE_ID = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID; private static final String CUSTOM_PKG_NAME = "not.android"; private static final String CUSTOM_APP_LABEL = "This is not Android"; private static final int CUSTOM_PKG_UID = 1; Loading Loading @@ -2329,6 +2330,68 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(savedEffects).isEqualTo(updateFromUser); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_replacesDisabledSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = false; sleepingRule.userModifiedFields = 0; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_keepsEnabledSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = true; sleepingRule.userModifiedFields = 0; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_keepsCustomizedSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = false; sleepingRule.userModifiedFields = AutomaticZenRule.FIELD_INTERRUPTION_FILTER; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test public void testSetManualZenMode() { mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API); Loading Loading
services/core/java/com/android/server/notification/ZenModeHelper.java +25 −0 Original line number Diff line number Diff line Loading @@ -453,6 +453,8 @@ public class ZenModeHelper { ZenRule rule = new ZenRule(); populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true); newConfig.automaticRules.put(rule.id, rule); maybeReplaceDefaultRule(newConfig, automaticZenRule); if (setConfigLocked(newConfig, origin, reason, rule.component, true, callingUid)) { return rule.id; } else { Loading @@ -461,6 +463,25 @@ public class ZenModeHelper { } } private static void maybeReplaceDefaultRule(ZenModeConfig config, AutomaticZenRule addedRule) { if (!Flags.modesApi()) { return; } if (addedRule.getType() == AutomaticZenRule.TYPE_BEDTIME) { // Delete a built-in disabled "Sleeping" rule when a BEDTIME rule is added; it may have // smarter triggers and it will prevent confusion about which one to use. // Note: we must not verify canManageAutomaticZenRule here, since most likely they // won't have the same owner (sleeping - system; bedtime - DWB). ZenRule sleepingRule = config.automaticRules.get( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); if (sleepingRule != null && !sleepingRule.enabled && sleepingRule.canBeUpdatedByApp() /* meaning it's not user-customized */) { config.automaticRules.remove(ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); } } } public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZenRule, @ConfigChangeOrigin int origin, String reason, int callingUid) { ZenModeConfig newConfig; Loading Loading @@ -1377,6 +1398,10 @@ public class ZenModeHelper { // reset zen automatic rules to default on restore or upgrade if: // - doesn't already have default rules and // - all previous automatic rules were disabled // // Note: we don't need to check to avoid restoring the Sleeping rule if there is a // TYPE_BEDTIME rule because the config is from an old version and thus by // definition cannot have a rule with TYPE_BEDTIME (or any other type). config.automaticRules = new ArrayMap<>(); for (ZenRule rule : mDefaultConfig.automaticRules.values()) { config.automaticRules.put(rule.id, rule); Loading
services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +65 −2 Original line number Diff line number Diff line Loading @@ -185,8 +185,9 @@ import java.util.concurrent.TimeUnit; @TestableLooper.RunWithLooper public class ZenModeHelperTest extends UiServiceTestCase { private static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE"; private static final String SCHEDULE_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE"; private static final String EVENTS_DEFAULT_RULE_ID = ZenModeConfig.EVENTS_DEFAULT_RULE_ID; private static final String SCHEDULE_DEFAULT_RULE_ID = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID; private static final String CUSTOM_PKG_NAME = "not.android"; private static final String CUSTOM_APP_LABEL = "This is not Android"; private static final int CUSTOM_PKG_UID = 1; Loading Loading @@ -2329,6 +2330,68 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(savedEffects).isEqualTo(updateFromUser); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_replacesDisabledSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = false; sleepingRule.userModifiedFields = 0; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_keepsEnabledSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = true; sleepingRule.userModifiedFields = 0; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void addAutomaticZenRule_withTypeBedtime_keepsCustomizedSleeping() { ZenRule sleepingRule = createCustomAutomaticRule(ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID); sleepingRule.enabled = false; sleepingRule.userModifiedFields = AutomaticZenRule.FIELD_INTERRUPTION_FILTER; sleepingRule.name = "ZZZZZZZ..."; mZenModeHelper.mConfig.automaticRules.clear(); mZenModeHelper.mConfig.automaticRules.put(sleepingRule.id, sleepingRule); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test public void testSetManualZenMode() { mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API); Loading