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

Commit 0324a22a authored by Yuri Lin's avatar Yuri Lin
Browse files

Fill in policies for default zen rules.

With modes_api, all zen rules must have fully-filled-in policies. However, the default XML config file doesn't include the default policy (which would be the same as the overall config) for the default system rules. This change fills those policies in so they are fully specified when ZenModeHelper is created.

Fixes: 322689667
Bug: 312739308
Test: repro manually on freshly wiped device before fix; confirmed it works correctly and does not crash after fix.
Test: ZenModeHelperTest
Change-Id: Ic156d997aa4ac44374726ef5ce283a41f75139ea
parent 37d7dedf
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -210,6 +210,9 @@ public class ZenModeHelper {

        mDefaultConfig = readDefaultConfig(mContext.getResources());
        updateDefaultAutomaticRuleNames();
        if (Flags.modesApi()) {
            updateDefaultAutomaticRulePolicies();
        }
        mConfig = mDefaultConfig.copy();
        synchronized (mConfigsArrayLock) {
            mConfigs.put(UserHandle.USER_SYSTEM, mConfig);
@@ -1966,6 +1969,21 @@ public class ZenModeHelper {
        }
    }

    // Updates the policies in the default automatic rules (provided via default XML config) to
    // be fully filled in default values.
    private void updateDefaultAutomaticRulePolicies() {
        if (!Flags.modesApi()) {
            // Should be checked before calling, but just in case.
            return;
        }
        ZenPolicy defaultPolicy = mDefaultConfig.toZenPolicy();
        for (ZenRule rule : mDefaultConfig.automaticRules.values()) {
            if (ZenModeConfig.DEFAULT_RULE_IDS.contains(rule.id) && rule.zenPolicy == null) {
                rule.zenPolicy = defaultPolicy.copy();
            }
        }
    }

    @VisibleForTesting
    protected void applyRestrictions() {
        final boolean zenOn = mZenMode != Global.ZEN_MODE_OFF;
+19 −0
Original line number Diff line number Diff line
@@ -2122,6 +2122,25 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        assertFalse(Objects.equals(defaultRuleName, ruleAfterUpdating.name)); // update name
    }

    @Test
    @EnableFlags(Flags.FLAG_MODES_API)
    public void testDefaultRulesFromConfig_modesApi_getPolicies() {
        // After mZenModeHelper was created, set some things in the policy so it's changed from
        // default.
        setupZenConfig();

        // Find default rules; check they have non-null policies; check that they match the default
        // and not whatever has been set up in setupZenConfig.
        ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelper.mConfig.automaticRules;
        for (String defaultId : ZenModeConfig.DEFAULT_RULE_IDS) {
            assertThat(rules).containsKey(defaultId);
            ZenRule rule = rules.get(defaultId);
            assertThat(rule.zenPolicy).isNotNull();

            assertThat(rule.zenPolicy).isEqualTo(mZenModeHelper.getDefaultZenPolicy());
        }
    }

    @Test
    public void testAddAutomaticZenRule_beyondSystemLimit() {
        for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) {