Loading services/core/java/com/android/server/notification/ZenModeHelper.java +20 −5 Original line number Diff line number Diff line Loading @@ -485,7 +485,7 @@ public class ZenModeHelper { populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true); rule = maybeRestoreRemovedRule(newConfig, pkg, rule, automaticZenRule, origin); newConfig.automaticRules.put(rule.id, rule); maybeReplaceDefaultRule(newConfig, automaticZenRule); maybeReplaceDefaultRule(newConfig, null, automaticZenRule); if (setConfigLocked(newConfig, origin, reason, rule.component, true, callingUid)) { return rule.id; Loading Loading @@ -535,13 +535,24 @@ public class ZenModeHelper { return ruleToRestore; } private static void maybeReplaceDefaultRule(ZenModeConfig config, AutomaticZenRule addedRule) { /** * Possibly delete built-in rules if a more suitable rule is added or updated. * * <p>Today, this is done in one case: delete a disabled "Sleeping" rule if a Bedtime Mode is * added (or an existing mode is turned into {@link AutomaticZenRule#TYPE_BEDTIME}, when * upgrading). Because only the {@code config_systemWellbeing} package is allowed to use rules * of this type, this will not trigger wantonly. * * @param oldRule If non-null, {@code rule} is updating {@code oldRule}. Otherwise, * {@code rule} is being added. */ private static void maybeReplaceDefaultRule(ZenModeConfig config, @Nullable ZenRule oldRule, AutomaticZenRule rule) { 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. if (rule.getType() == AutomaticZenRule.TYPE_BEDTIME && (oldRule == null || oldRule.type != rule.getType())) { // 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( Loading Loading @@ -589,6 +600,10 @@ public class ZenModeHelper { // condition) when no changes happen. return true; } if (Flags.modesUi()) { maybeReplaceDefaultRule(newConfig, oldRule, automaticZenRule); } return setConfigLocked(newConfig, origin, reason, newRule.component, true, callingUid); } Loading services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +27 −0 Original line number Diff line number Diff line Loading @@ -3031,6 +3031,33 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI}) public void updateAutomaticZenRule_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 futureBedtime = new AutomaticZenRule.Builder("Bedtime (?)", CONDITION_ID) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(mPkg, futureBedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()) .containsExactly(sleepingRule.id, bedtimeRuleId); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime (!)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); mZenModeHelper.updateAutomaticZenRule(bedtimeRuleId, bedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @Test @EnableFlags(FLAG_MODES_API) public void testSetManualZenMode() { Loading Loading
services/core/java/com/android/server/notification/ZenModeHelper.java +20 −5 Original line number Diff line number Diff line Loading @@ -485,7 +485,7 @@ public class ZenModeHelper { populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true); rule = maybeRestoreRemovedRule(newConfig, pkg, rule, automaticZenRule, origin); newConfig.automaticRules.put(rule.id, rule); maybeReplaceDefaultRule(newConfig, automaticZenRule); maybeReplaceDefaultRule(newConfig, null, automaticZenRule); if (setConfigLocked(newConfig, origin, reason, rule.component, true, callingUid)) { return rule.id; Loading Loading @@ -535,13 +535,24 @@ public class ZenModeHelper { return ruleToRestore; } private static void maybeReplaceDefaultRule(ZenModeConfig config, AutomaticZenRule addedRule) { /** * Possibly delete built-in rules if a more suitable rule is added or updated. * * <p>Today, this is done in one case: delete a disabled "Sleeping" rule if a Bedtime Mode is * added (or an existing mode is turned into {@link AutomaticZenRule#TYPE_BEDTIME}, when * upgrading). Because only the {@code config_systemWellbeing} package is allowed to use rules * of this type, this will not trigger wantonly. * * @param oldRule If non-null, {@code rule} is updating {@code oldRule}. Otherwise, * {@code rule} is being added. */ private static void maybeReplaceDefaultRule(ZenModeConfig config, @Nullable ZenRule oldRule, AutomaticZenRule rule) { 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. if (rule.getType() == AutomaticZenRule.TYPE_BEDTIME && (oldRule == null || oldRule.type != rule.getType())) { // 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( Loading Loading @@ -589,6 +600,10 @@ public class ZenModeHelper { // condition) when no changes happen. return true; } if (Flags.modesUi()) { maybeReplaceDefaultRule(newConfig, oldRule, automaticZenRule); } return setConfigLocked(newConfig, origin, reason, newRule.component, true, callingUid); } Loading
services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +27 −0 Original line number Diff line number Diff line Loading @@ -3031,6 +3031,33 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); } @Test @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI}) public void updateAutomaticZenRule_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 futureBedtime = new AutomaticZenRule.Builder("Bedtime (?)", CONDITION_ID) .build(); String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(mPkg, futureBedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()) .containsExactly(sleepingRule.id, bedtimeRuleId); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime (!)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); mZenModeHelper.updateAutomaticZenRule(bedtimeRuleId, bedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @Test @EnableFlags(FLAG_MODES_API) public void testSetManualZenMode() { Loading