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

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

Show correct visual effects for modes with interruption filter NONE or ALARMS

In Settings, we were displaying a blocking-all-visual-effects policy for rules with nonstandard interruption filters. In reality, only the set of allowed sounds is fixed, while the visual effects come from the default policy (or, if other rules are active, from them). So merging with the default policy is way more accurate.

Fixes: 363999223
Fixes: 361591317
Test: atest ZenModeTest ZenModesBackendTest
Flag: android.app.modes_ui
Change-Id: I043862f327534851f96cd4daf596500e7f0ec012
parent 5f1c1da0
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -392,6 +392,46 @@ public final class ZenPolicy implements Parcelable {
        mAllowChannels = allowChannels;
    }

    /**
     * Base Zen Policy used when {@link android.app.NotificationManager#setInterruptionFilter} is
     * called with {@link android.app.NotificationManager#INTERRUPTION_FILTER_ALARMS} or an
     * {@link android.app.AutomaticZenRule} specifies said filter.
     *
     * <p>Note that <em>visual effects</em> for filtered notifications are unset in this base
     * policy, so should be merged on top of the default policy's visual effects (see
     * {@link #overwrittenWith(ZenPolicy)}).
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static ZenPolicy getBasePolicyInterruptionFilterAlarms() {
        return new ZenPolicy.Builder()
                .disallowAllSounds()
                .allowAlarms(true)
                .allowMedia(true)
                .allowPriorityChannels(false)
                .build();
    }

    /**
     * Base Zen Policy used when {@link android.app.NotificationManager#setInterruptionFilter} is
     * called with {@link android.app.NotificationManager#INTERRUPTION_FILTER_NONE} or an
     * {@link android.app.AutomaticZenRule} specifies said filter.
     *
     * <p>Note that <em>visual effects</em> for filtered notifications are unset in this base
     * policy, so it should be merged on top of the device default policy's visual effects (see
     * {@link #overwrittenWith(ZenPolicy)}).
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static ZenPolicy getBasePolicyInterruptionFilterNone() {
        return new ZenPolicy.Builder()
                .disallowAllSounds()
                .allowPriorityChannels(false)
                .build();
    }

    /**
     * Conversation type that can bypass DND.
     * @return {@link #CONVERSATION_SENDERS_UNSET}, {@link #CONVERSATION_SENDERS_ANYONE},
+4 −19
Original line number Diff line number Diff line
@@ -69,23 +69,6 @@ public class ZenMode implements Parcelable {
    static final String MANUAL_DND_MODE_ID = ZenModeConfig.MANUAL_RULE_ID;
    static final String TEMP_NEW_MODE_ID = "temp_new_mode";

    // Must match com.android.server.notification.ZenModeHelper#applyCustomPolicy.
    static final ZenPolicy POLICY_INTERRUPTION_FILTER_ALARMS =
            new ZenPolicy.Builder()
                    .disallowAllSounds()
                    .allowAlarms(true)
                    .allowMedia(true)
                    .allowPriorityChannels(false)
                    .build();

    // Must match com.android.server.notification.ZenModeHelper#applyCustomPolicy.
    static final ZenPolicy POLICY_INTERRUPTION_FILTER_NONE =
            new ZenPolicy.Builder()
                    .disallowAllSounds()
                    .hideAllVisualEffects()
                    .allowPriorityChannels(false)
                    .build();

    private static final Comparator<Integer> PRIORITIZED_TYPE_COMPARATOR = new Comparator<>() {

        private static final ImmutableList</* @AutomaticZenRule.Type */ Integer>
@@ -320,10 +303,12 @@ public class ZenMode implements Parcelable {
                return requireNonNull(mRule.getZenPolicy());

            case NotificationManager.INTERRUPTION_FILTER_ALARMS:
                return POLICY_INTERRUPTION_FILTER_ALARMS;
                return new ZenPolicy.Builder(ZenModeConfig.getDefaultZenPolicy()).build()
                        .overwrittenWith(ZenPolicy.getBasePolicyInterruptionFilterAlarms());

            case NotificationManager.INTERRUPTION_FILTER_NONE:
                return POLICY_INTERRUPTION_FILTER_NONE;
                return new ZenPolicy.Builder(ZenModeConfig.getDefaultZenPolicy()).build()
                        .overwrittenWith(ZenPolicy.getBasePolicyInterruptionFilterNone());

            case NotificationManager.INTERRUPTION_FILTER_UNKNOWN:
            default:
+2 −3
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ public class ZenModeTest {
        ZenMode zenMode = new ZenMode("id", azr, zenConfigRuleFor(azr, false));

        assertThat(zenMode.getPolicy()).isEqualTo(
                new ZenPolicy.Builder()
                new ZenPolicy.Builder(ZenModeConfig.getDefaultZenPolicy())
                        .disallowAllSounds()
                        .allowAlarms(true)
                        .allowMedia(true)
@@ -230,9 +230,8 @@ public class ZenModeTest {
        ZenMode zenMode = new ZenMode("id", azr, zenConfigRuleFor(azr, false));

        assertThat(zenMode.getPolicy()).isEqualTo(
                new ZenPolicy.Builder()
                new ZenPolicy.Builder(ZenModeConfig.getDefaultZenPolicy())
                        .disallowAllSounds()
                        .hideAllVisualEffects()
                        .allowPriorityChannels(false)
                        .build());
    }
+8 −1
Original line number Diff line number Diff line
@@ -238,9 +238,16 @@ public class ZenModesBackendTest {
        assertThat(mode.isManualDnd()).isTrue();
        assertThat(mode.isEnabled()).isTrue();
        assertThat(mode.isActive()).isTrue();

        // Mode itself has a special fixed policy, different to the rule.
        assertThat(mode.canEditPolicy()).isFalse();
        assertThat(mode.getPolicy()).isEqualTo(ZenMode.POLICY_INTERRUPTION_FILTER_ALARMS);
        assertThat(mode.getPolicy()).isEqualTo(
                new ZenPolicy.Builder(ZenModeConfig.getDefaultZenPolicy())
                        .disallowAllSounds()
                        .allowAlarms(true)
                        .allowMedia(true)
                        .allowPriorityChannels(false)
                        .build());
    }

    @Test
+18 −10
Original line number Diff line number Diff line
@@ -2125,17 +2125,25 @@ public class ZenModeHelper {
    @GuardedBy("mConfigLock")
    private void applyCustomPolicy(ZenPolicy policy, ZenRule rule, boolean useManualConfig) {
        if (rule.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
            if (Flags.modesApi() && Flags.modesUi()) {
                policy.apply(ZenPolicy.getBasePolicyInterruptionFilterNone());
            } else {
                policy.apply(new ZenPolicy.Builder()
                        .disallowAllSounds()
                        .allowPriorityChannels(false)
                        .build());
            }
        } else if (rule.zenMode == Global.ZEN_MODE_ALARMS) {
            if (Flags.modesApi() && Flags.modesUi()) {
                policy.apply(ZenPolicy.getBasePolicyInterruptionFilterAlarms());
            } else {
                policy.apply(new ZenPolicy.Builder()
                        .disallowAllSounds()
                        .allowAlarms(true)
                        .allowMedia(true)
                        .allowPriorityChannels(false)
                        .build());
            }
        } else if (rule.zenPolicy != null) {
            policy.apply(rule.zenPolicy);
        } else {