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

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

Make rule updates "by user in app" more powerful

User actions in app should be capable of removing previous "user action from systemui" overrides.

However, they do not add new overrides. The rationale is that an override in systemui is possibly not known to the app (and certainly unknown unless they listen to the broadcasts added in VIC) but a user action inside the app _is_ known, so it would be strange to disregard fiurther state updates from the app until it "agrees" with itself.

Fixes: 360794520
Test: atest ZenModeHelperTest
Flag: android.app.modes_ui
Change-Id: If86a13f09d3273f4caa83808701252e313dab788
parent d0615c0f
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -977,7 +977,16 @@ public class ZenModeHelper {
                        rule.setConditionOverride(OVERRIDE_DEACTIVATE);
                    }
                }
            } else if (origin == ORIGIN_USER_IN_APP && condition != null
                    && condition.source == SOURCE_USER_ACTION) {
                // Remove override and just apply the condition. Since the app is reporting that the
                // user asked for it, by definition it knows that, and will adjust its automatic
                // behavior accordingly -> no need to override.
                rule.condition = condition;
                rule.resetConditionOverride();
            } else {
                // Update the condition, and check whether we can remove the override (if automatic
                // and manual decisions agree).
                rule.condition = condition;
                rule.reconsiderConditionOverride();
            }
+86 −0
Original line number Diff line number Diff line
@@ -82,6 +82,7 @@ import static com.android.os.dnd.DNDProtoEnums.STATE_DISALLOW;
import static com.android.server.notification.ZenModeEventLogger.ACTIVE_RULE_TYPE_MANUAL;
import static com.android.server.notification.ZenModeHelper.RULE_LIMIT_PER_PACKAGE;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
@@ -6672,6 +6673,91 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void setAutomaticZenRuleState_withActivationOverride_userActionFromAppCanDeactivate() {
        AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond"))
                .setPackage(mPkg)
                .build();
        String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding",
                CUSTOM_PKG_UID);

        // User manually turns on rule from SysUI / Settings...
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "manual-on-from-sysui", STATE_TRUE,
                        SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isTrue();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE);

        // ... and they can turn it off manually from inside the app.
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "manual-off-from-app", STATE_FALSE,
                        SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isFalse();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void setAutomaticZenRuleState_withDeactivationOverride_userActionFromAppCanActivate() {
        AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond"))
                .setPackage(mPkg)
                .build();
        String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding",
                CUSTOM_PKG_UID);

        // Rule is activated due to its schedule.
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "auto-on-from-app", STATE_TRUE,
                        SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isTrue();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);

        // User manually turns off rule from SysUI / Settings...
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "manual-off-from-sysui", STATE_FALSE,
                        SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isFalse();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE);

        // ... and they can turn it on manually from inside the app.
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "manual-on-from-app", STATE_TRUE,
                        SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isTrue();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void setAutomaticZenRuleState_manualActionFromApp_isNotOverride() {
        AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond"))
                .setPackage(mPkg)
                .build();
        String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding",
                CUSTOM_PKG_UID);

        // Rule is manually activated by the user in the app.
        // This turns the rule on, but is NOT an override...
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "manual-on-from-app", STATE_TRUE,
                        SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isTrue();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);

        // ... so the app can turn it off when its schedule is over.
        mZenModeHelper.setAutomaticZenRuleState(ruleId,
                new Condition(rule.getConditionId(), "auto-off-from-app", STATE_FALSE,
                        SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID);
        assertThat(getZenRule(ruleId).isAutomaticActive()).isFalse();
        assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE);
    }

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

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