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

Commit 623953d5 authored by Matías Hernández's avatar Matías Hernández
Browse files

Don't apply overrides to implicit rules, instead just update their conditions

Implicit rules are not known to the owning package, which just calls setInterruptionFilter(). Thus, a "stray" call to setInterruptionFilter(PRIORITY) without a corresponding "ALL" afterwards would leave the rule with a STATE_TRUE condition. And because a manual deactivation is not saved to storage, the rule would reactivate on each reboot.

Instead, when toggling one of these rules for whatever reason (including manually), just overwrite its condition instead. This behavior is also closer to the pre-MODES behavior, since manual DND was never itself snoozed.

Fixes: 379112535
Test: atest ZenModeHelperTest
Flag: android.app.modes_ui
Change-Id: I224b741f56bd5bc1ce71a5688622b8638530b92d
parent d984fd62
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -1015,7 +1015,13 @@ public class ZenModeHelper {
    private static void applyConditionAndReconsiderOverride(ZenRule rule, Condition condition,
            int origin) {
        if (Flags.modesApi() && Flags.modesUi()) {
            if (origin == ORIGIN_USER_IN_SYSTEMUI && condition != null
            if (isImplicitRuleId(rule.id)) {
                // Implicit rules do not use overrides, and always apply conditions directly.
                // This is compatible with the previous behavior (where the package set the
                // interruption filter, and no "snoozing" took place if the user changed it later).
                rule.condition = condition;
                rule.resetConditionOverride();
            } else if (origin == ORIGIN_USER_IN_SYSTEMUI && condition != null
                    && condition.source == SOURCE_USER_ACTION) {
                // Apply as override, instead of actual condition.
                // If the new override is the reverse of a previous (still active) override, try
+48 −3
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI
import static android.service.notification.ZenModeConfig.ZenRule.OVERRIDE_ACTIVATE;
import static android.service.notification.ZenModeConfig.ZenRule.OVERRIDE_DEACTIVATE;
import static android.service.notification.ZenModeConfig.ZenRule.OVERRIDE_NONE;
import static android.service.notification.ZenModeConfig.implicitRuleId;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_CONTACTS;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_NONE;
import static android.service.notification.ZenPolicy.PEOPLE_TYPE_STARRED;
@@ -201,9 +202,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.xmlpull.v1.XmlPullParserException;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
@@ -223,6 +221,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

@SmallTest
@SuppressLint("GuardedBy") // It's ok for this test to access guarded methods from the service.
@RunWith(ParameterizedAndroidJunit4.class)
@@ -7067,6 +7068,50 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void setAutomaticZenRuleState_implicitRuleManualActivation_doesNotUseOverride() {
        mContext.getTestablePermissions().setPermission(Manifest.permission.MANAGE_NOTIFICATIONS,
                PERMISSION_GRANTED); // So that canManageAZR succeeds.
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME,
                CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME,
                CUSTOM_PKG_UID, ZEN_MODE_OFF);
        ZenRule implicitRule = getZenRule(implicitRuleId(CUSTOM_PKG_NAME));
        assertThat(implicitRule.isActive()).isFalse();

        mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, implicitRule.id,
                new Condition(implicitRule.conditionId, "on!", STATE_TRUE, SOURCE_USER_ACTION),
                ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID);

        implicitRule = getZenRule(implicitRuleId(CUSTOM_PKG_NAME));
        assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, implicitRule.id))
                .isEqualTo(STATE_TRUE);
        assertThat(implicitRule.isActive()).isTrue();
        assertThat(implicitRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void setAutomaticZenRuleState_implicitRuleManualDeactivation_doesNotUseOverride() {
        mContext.getTestablePermissions().setPermission(Manifest.permission.MANAGE_NOTIFICATIONS,
                PERMISSION_GRANTED); // So that canManageAZR succeeds.
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME,
                CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
        ZenRule implicitRule = getZenRule(implicitRuleId(CUSTOM_PKG_NAME));
        assertThat(implicitRule.isActive()).isTrue();

        mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, implicitRule.id,
                new Condition(implicitRule.conditionId, "off!", STATE_FALSE, SOURCE_USER_ACTION),
                ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID);

        implicitRule = getZenRule(implicitRuleId(CUSTOM_PKG_NAME));
        assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, implicitRule.id))
                .isEqualTo(STATE_FALSE);
        assertThat(implicitRule.isActive()).isFalse();
        assertThat(implicitRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    private ZenRule getZenRule(String ruleId) {
        return checkNotNull(mZenModeHelper.mConfig.automaticRules.get(ruleId),
                "Didn't find rule with id %s", ruleId);