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

Commit 1006ce12 authored by Matías Hernández's avatar Matías Hernández
Browse files

Delete the built-in "Sleeping" rule when a TYPE_BEDTIME rule is added

Fixes: 315454303
Test: atest ZenModeHelperTest
Change-Id: Id7d61959edaf23c9fb4d49bddd0fb36d6bd431c0
parent b5c9babe
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -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 {
@@ -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;
@@ -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);
+65 −2
Original line number Diff line number Diff line
@@ -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;
@@ -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);