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

Commit 947f40c7 authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Handle duplicate Uris in DND rules

If a user creates 2 DND rules of the same type with the same criteria,
DND would either not start properly or end properly when that criteria
started/ended.

This makes sure all rules with matching criteria have their Conditions
updated.

Test: atest
Fixes: 141343447
Change-Id: Id8c56433ec3f238d3bfc08deb70212c8a7f27843
parent b5671dc8
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -417,7 +417,9 @@ public class ZenModeHelper {
            if (mConfig == null) return;

            newConfig = mConfig.copy();
            setAutomaticZenRuleStateLocked(newConfig, newConfig.automaticRules.get(id), condition);
            ArrayList<ZenRule> rules = new ArrayList<>();
            rules.add(newConfig.automaticRules.get(id));
            setAutomaticZenRuleStateLocked(newConfig, rules, condition);
        }
    }

@@ -428,31 +430,34 @@ public class ZenModeHelper {
            newConfig = mConfig.copy();

            setAutomaticZenRuleStateLocked(newConfig,
                    findMatchingRule(newConfig, ruleDefinition, condition),
                    findMatchingRules(newConfig, ruleDefinition, condition),
                    condition);
        }
    }

    private void setAutomaticZenRuleStateLocked(ZenModeConfig config, ZenRule rule,
    private void setAutomaticZenRuleStateLocked(ZenModeConfig config, List<ZenRule> rules,
            Condition condition) {
        if (rule == null) return;
        if (rules == null || rules.isEmpty()) return;

        for (ZenRule rule : rules) {
            rule.condition = condition;
            updateSnoozing(rule);
            setConfigLocked(config, rule.component, "conditionChanged");
        }
    }

    private ZenRule findMatchingRule(ZenModeConfig config, Uri id, Condition condition) {
    private List<ZenRule> findMatchingRules(ZenModeConfig config, Uri id, Condition condition) {
        List<ZenRule> matchingRules= new ArrayList<>();
        if (ruleMatches(id, condition, config.manualRule)) {
            return config.manualRule;
            matchingRules.add(config.manualRule);
        } else {
            for (ZenRule automaticRule : config.automaticRules.values()) {
                if (ruleMatches(id, condition, automaticRule)) {
                    return automaticRule;
                    matchingRules.add(automaticRule);
                }
            }
        }
        return null;
        return matchingRules;
    }

    private boolean ruleMatches(Uri id, Condition condition, ZenRule rule) {
+44 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import static com.android.os.AtomsProto.DNDModeProto.ZEN_MODE_FIELD_NUMBER;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

@@ -1556,6 +1557,49 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        assertEquals(zenRule.getName(), ruleInConfig.name);
    }

    @Test
    public void testRulesWithSameUri() {
        Uri sharedUri = ZenModeConfig.toScheduleConditionId(new ScheduleInfo());
        AutomaticZenRule zenRule = new AutomaticZenRule("name",
                new ComponentName("android", "ScheduleConditionProvider"),
                sharedUri,
                NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
        String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
        AutomaticZenRule zenRule2 = new AutomaticZenRule("name2",
                new ComponentName("android", "ScheduleConditionProvider"),
                sharedUri,
                NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
        String id2 = mZenModeHelperSpy.addAutomaticZenRule(zenRule2, "test");

        Condition condition = new Condition(sharedUri, "", Condition.STATE_TRUE);
        mZenModeHelperSpy.setAutomaticZenRuleState(sharedUri, condition);

        for (ZenModeConfig.ZenRule rule : mZenModeHelperSpy.mConfig.automaticRules.values()) {
            if (rule.id.equals(id)) {
                assertNotNull(rule.condition);
                assertTrue(rule.condition.state == Condition.STATE_TRUE);
            }
            if (rule.id.equals(id2)) {
                assertNotNull(rule.condition);
                assertTrue(rule.condition.state == Condition.STATE_TRUE);
            }
        }

        condition = new Condition(sharedUri, "", Condition.STATE_FALSE);
        mZenModeHelperSpy.setAutomaticZenRuleState(sharedUri, condition);

        for (ZenModeConfig.ZenRule rule : mZenModeHelperSpy.mConfig.automaticRules.values()) {
            if (rule.id.equals(id)) {
                assertNotNull(rule.condition);
                assertTrue(rule.condition.state == Condition.STATE_FALSE);
            }
            if (rule.id.equals(id2)) {
                assertNotNull(rule.condition);
                assertTrue(rule.condition.state == Condition.STATE_FALSE);
            }
        }
    }

    private void setupZenConfig() {
        mZenModeHelperSpy.mZenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
        mZenModeHelperSpy.mConfig.allowAlarms = false;