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

Commit 1cca7e88 authored by Matías Hernández's avatar Matías Hernández
Browse files

Always make copies of objects from mDefaultConfig

ZenRule and ZenPolicy are not immutable, so taking objects from mDefaultConfig and putting them in mConfig runs the risk of inadvertently modifying mDefaultConfig.

Note that, for API calls from apps, it was most likely safe because copies are creating when unparceling (but it's still risky since ZenModeHelper updates ZenRule objects).

Fixes: 368580925
Test: atest ZenModeHelperTest
Flag: EXEMPT trivial bugfix
Change-Id: If694b615ff911a3f393a5b8d6ae74b224cf5bc2a
parent 5b2f1b8b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1358,7 +1358,8 @@ public class ZenModeHelper {
            if (isNew) {
                // Newly created rule with no provided policy; fill in with the default.
                zenRule.zenPolicy =
                        Flags.modesUi() ? mDefaultConfig.getZenPolicy() : mConfig.getZenPolicy();
                        (Flags.modesUi() ? mDefaultConfig.getZenPolicy() : mConfig.getZenPolicy())
                                .copy();
                return true;
            }
            // Otherwise, a null policy means no policy changes, so we can stop here.
@@ -1773,7 +1774,7 @@ public class ZenModeHelper {
                // 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);
                    config.automaticRules.put(rule.id, rule.copy());
                }
                reason += ", reset to default rules";
            }
+16 −0
Original line number Diff line number Diff line
@@ -6992,6 +6992,22 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        assertThat(zenRule.condition).isNotNull();
    }

    @Test
    @EnableFlags(FLAG_MODES_API)
    public void addAutomaticZenRule_withoutPolicy_getsItsOwnInstanceOfDefaultPolicy() {
        // Add a rule without policy -> uses default config
        AutomaticZenRule azr = new AutomaticZenRule.Builder("Rule", Uri.parse("cond"))
                .setPackage(mPkg)
                .build();
        String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, azr, ORIGIN_APP, "adding",
                CUSTOM_PKG_UID);

        ZenRule zenRule = checkNotNull(mZenModeHelper.mConfig.automaticRules.get(ruleId));

        assertThat(zenRule.zenPolicy).isEqualTo(mZenModeHelper.getDefaultZenPolicy());
        assertThat(zenRule.zenPolicy).isNotSameInstanceAs(mZenModeHelper.getDefaultZenPolicy());
    }

    private static void addZenRule(ZenModeConfig config, String id, String ownerPkg, int zenMode,
            @Nullable ZenPolicy zenPolicy) {
        ZenRule rule = new ZenRule();