Loading services/core/java/com/android/server/notification/ZenModeHelper.java +14 −2 Original line number Diff line number Diff line Loading @@ -1105,9 +1105,12 @@ public class ZenModeHelper { * {@link ZenRule#equals} or {@link AutomaticZenRule#equals}, for various reasons: * <ul> * <li>some metadata-related fields are not considered * <li>some fields (like {@code condition} are always reset, and ignored for this result * <li>some fields (like {@code condition} are normally reset, and ignored for this result * <li>an app may provide changes that are not actually applied, as described above * </ul> * * <p>The rule's {@link ZenRule#condition} is cleared (meaning that an active rule will be * deactivated) unless the update has origin == {@link ZenModeConfig#UPDATE_ORIGIN_USER}. */ private boolean populateZenRule(String pkg, AutomaticZenRule azr, ZenRule rule, @ConfigChangeOrigin int origin, boolean isNew) { Loading @@ -1122,11 +1125,20 @@ public class ZenModeHelper { modified = true; } rule.condition = null; if (!Objects.equals(rule.conditionId, azr.getConditionId())) { rule.conditionId = azr.getConditionId(); modified = true; } boolean shouldPreserveCondition = Flags.modesApi() && Flags.modesUi() && !isNew && origin == UPDATE_ORIGIN_USER && rule.enabled == azr.isEnabled() && rule.conditionId != null && rule.condition != null && rule.conditionId.equals(rule.condition.id); if (!shouldPreserveCondition) { // Do not update 'modified'. If only this changes we treat it as a no-op updateAZR. rule.condition = null; } if (rule.enabled != azr.isEnabled()) { rule.enabled = azr.isEnabled(); rule.snoozing = false; Loading services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +50 −0 Original line number Diff line number Diff line Loading @@ -4955,6 +4955,56 @@ public class ZenModeHelperTest extends UiServiceTestCase { CONDITION_TRUE); } @Test @EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI}) public void updateAutomaticZenRule_ruleChangedByUser_doesNotDeactivateRule() { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", CONDITION_ID) .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, UPDATE_ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateWithDiff = new AutomaticZenRule.Builder(rule) .setTriggerDescription("Whenever") .build(); mZenModeHelper.updateAutomaticZenRule(ruleId, updateWithDiff, UPDATE_ORIGIN_USER, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isEqualTo( CONDITION_TRUE); } @Test @EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI}) public void updateAutomaticZenRule_ruleDisabledByUser_doesNotReactivateOnReenable() { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", CONDITION_ID) .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, UPDATE_ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); mZenModeHelper.updateAutomaticZenRule(ruleId, new AutomaticZenRule.Builder(rule).setEnabled(false).build(), UPDATE_ORIGIN_USER, "disable", CUSTOM_PKG_UID); mZenModeHelper.updateAutomaticZenRule(ruleId, new AutomaticZenRule.Builder(rule).setEnabled(true).build(), UPDATE_ORIGIN_USER, "enable", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isNull(); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void removeAutomaticZenRule_propagatesOriginToEffectsApplier() { Loading Loading
services/core/java/com/android/server/notification/ZenModeHelper.java +14 −2 Original line number Diff line number Diff line Loading @@ -1105,9 +1105,12 @@ public class ZenModeHelper { * {@link ZenRule#equals} or {@link AutomaticZenRule#equals}, for various reasons: * <ul> * <li>some metadata-related fields are not considered * <li>some fields (like {@code condition} are always reset, and ignored for this result * <li>some fields (like {@code condition} are normally reset, and ignored for this result * <li>an app may provide changes that are not actually applied, as described above * </ul> * * <p>The rule's {@link ZenRule#condition} is cleared (meaning that an active rule will be * deactivated) unless the update has origin == {@link ZenModeConfig#UPDATE_ORIGIN_USER}. */ private boolean populateZenRule(String pkg, AutomaticZenRule azr, ZenRule rule, @ConfigChangeOrigin int origin, boolean isNew) { Loading @@ -1122,11 +1125,20 @@ public class ZenModeHelper { modified = true; } rule.condition = null; if (!Objects.equals(rule.conditionId, azr.getConditionId())) { rule.conditionId = azr.getConditionId(); modified = true; } boolean shouldPreserveCondition = Flags.modesApi() && Flags.modesUi() && !isNew && origin == UPDATE_ORIGIN_USER && rule.enabled == azr.isEnabled() && rule.conditionId != null && rule.condition != null && rule.conditionId.equals(rule.condition.id); if (!shouldPreserveCondition) { // Do not update 'modified'. If only this changes we treat it as a no-op updateAZR. rule.condition = null; } if (rule.enabled != azr.isEnabled()) { rule.enabled = azr.isEnabled(); rule.snoozing = false; Loading
services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +50 −0 Original line number Diff line number Diff line Loading @@ -4955,6 +4955,56 @@ public class ZenModeHelperTest extends UiServiceTestCase { CONDITION_TRUE); } @Test @EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI}) public void updateAutomaticZenRule_ruleChangedByUser_doesNotDeactivateRule() { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", CONDITION_ID) .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, UPDATE_ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateWithDiff = new AutomaticZenRule.Builder(rule) .setTriggerDescription("Whenever") .build(); mZenModeHelper.updateAutomaticZenRule(ruleId, updateWithDiff, UPDATE_ORIGIN_USER, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isEqualTo( CONDITION_TRUE); } @Test @EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI}) public void updateAutomaticZenRule_ruleDisabledByUser_doesNotReactivateOnReenable() { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", CONDITION_ID) .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID); mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, UPDATE_ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); mZenModeHelper.updateAutomaticZenRule(ruleId, new AutomaticZenRule.Builder(rule).setEnabled(false).build(), UPDATE_ORIGIN_USER, "disable", CUSTOM_PKG_UID); mZenModeHelper.updateAutomaticZenRule(ruleId, new AutomaticZenRule.Builder(rule).setEnabled(true).build(), UPDATE_ORIGIN_USER, "enable", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isNull(); } @Test @EnableFlags(Flags.FLAG_MODES_API) public void removeAutomaticZenRule_propagatesOriginToEffectsApplier() { Loading