Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 735fdefe authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Also delete Sleeping when an existing rule is updated to TYPE_BEDTIME" into main

parents 41698197 0c766cd9
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -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;
@@ -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(
@@ -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);
        }
+27 −0
Original line number Diff line number Diff line
@@ -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() {