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

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

Split ORIGIN_USER into "user from systemui" and "user from app"

In practice this applies to setAutomaticZenRuleState() only, since that call is the only one that allowed apps to claim to act on behalf of the user.

A follow-up will use this information to snooze rules that are turned off manually.

Bug: 333527800
Test: atest DefaultDeviceEffectsApplierTest NotificationManagerServiceTest ZenModeConfigTest ZenModeHelperTest
Flag: android.app.modes_ui
Change-Id: I7426315cd92c299327b73fcf0e992bfe5f1e4d43
parent f43ac720
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package android.service.notification;

import android.service.notification.ZenModeConfig.ConfigChangeOrigin;

/**
 * Responsible for making any service calls needed to apply the set of {@link ZenDeviceEffects} that
 * make sense for the current platform.
@@ -43,5 +41,5 @@ public interface DeviceEffectsApplier {
     *               changing as a result of an explicit user action, then it makes sense to
     *               apply them immediately regardless.
     */
    void apply(ZenDeviceEffects effects, @ConfigChangeOrigin int source);
    void apply(ZenDeviceEffects effects, @ZenModeConfig.ConfigOrigin int source);
}
+53 −37
Original line number Diff line number Diff line
@@ -112,68 +112,83 @@ public class ZenModeConfig implements Parcelable {
    private static final String TAG = "ZenModeConfig";

    /**
     * The {@link ZenModeConfig} is being updated because of an unknown reason.
     * The {@link ZenModeConfig} is updated because of an unknown reason.
     */
    public static final int UPDATE_ORIGIN_UNKNOWN = 0;
    public static final int ORIGIN_UNKNOWN = 0;

    /**
     * The {@link ZenModeConfig} is being updated because of system initialization (i.e. load from
     * The {@link ZenModeConfig} is updated because of system initialization (i.e. load from
     * storage, on device boot).
     */
    public static final int UPDATE_ORIGIN_INIT = 1;
    public static final int ORIGIN_INIT = 1;

    /** The {@link ZenModeConfig} is being updated (replaced) because of a user switch or unlock. */
    public static final int UPDATE_ORIGIN_INIT_USER = 2;
    /** The {@link ZenModeConfig} is updated (replaced) because of a user switch or unlock. */
    public static final int ORIGIN_INIT_USER = 2;

    /** The {@link ZenModeConfig} is being updated because of a user action, for example:
    /**
     * The {@link ZenModeConfig} is updated because of a <em>user action</em> performed from a
     * system surface, such as:
     * <ul>
     *     <li>{@link NotificationManager#setAutomaticZenRuleState} with a
     *     {@link Condition#source} equal to {@link Condition#SOURCE_USER_ACTION}.</li>
     *     <li>Adding, updating, or removing a rule from Settings.</li>
     *     <li>Directly activating or deactivating/snoozing a rule through some UI affordance (e.g.
     *     Quick Settings).</li>
     *     <li>Adding, updating, or removing a rule from Settings.
     *     <li>Activating or deactivating a rule through the System (e.g. from Settings/Modes).
     *     <li>Activating or deactivating a rule through SystemUi (e.g. with Quick Settings).
     * </ul>
     *
     * <p>This does <em>not</em> include user actions from apps ({@link #ORIGIN_USER_IN_APP} nor
     * non-user actions from the system ({@link #ORIGIN_SYSTEM}).
     */
    public static final int UPDATE_ORIGIN_USER = 3;
    public static final int ORIGIN_USER_IN_SYSTEMUI = 3;

    /**
     * The {@link ZenModeConfig} is being "independently" updated by an app, and not as a result of
     * a user's action inside that app (for example, activating an {@link AutomaticZenRule} based on
     * a previously set schedule).
     * The {@link ZenModeConfig} is updated by an app, but (probably) not as a result of a user
     * action (for example, activating an {@link AutomaticZenRule} based on a previously set
     * schedule).
     *
     * <p>Note that {@code ORIGIN_APP} is the only option for all public APIs except
     * {@link NotificationManager#setAutomaticZenRuleState} -- apps cannot claim to be adding or
     * updating a rule on behalf of the user.
     */
    public static final int UPDATE_ORIGIN_APP = 4;
    public static final int ORIGIN_APP = 4;

    /**
     * The {@link ZenModeConfig} is being updated by the System or SystemUI. Note that this only
     * includes cases where the call is coming from the System/SystemUI but the change is not due to
     * a user action (e.g. automatically activating a schedule-based rule). If the change is a
     * result of a user action (e.g. activating a rule by tapping on its QS tile) then
     * {@link #UPDATE_ORIGIN_USER} is used instead.
     * The {@link ZenModeConfig} is updated by the System (or SystemUI). This only includes cases
     * where the call is coming from the System/SystemUI but the change is not due to a user action
     * (e.g. automatically activating a schedule-based rule, or some service toggling Do Not
     * Disturb). See {@link #ORIGIN_USER_IN_SYSTEMUI}.
     */
    public static final int UPDATE_ORIGIN_SYSTEM_OR_SYSTEMUI = 5;
    public static final int ORIGIN_SYSTEM = 5;

    /**
     * The {@link ZenModeConfig} is being updated (replaced) because the user's DND configuration
     * is being restored from a backup.
     */
    public static final int UPDATE_ORIGIN_RESTORE_BACKUP = 6;

    @IntDef(prefix = { "UPDATE_ORIGIN_" }, value = {
            UPDATE_ORIGIN_UNKNOWN,
            UPDATE_ORIGIN_INIT,
            UPDATE_ORIGIN_INIT_USER,
            UPDATE_ORIGIN_USER,
            UPDATE_ORIGIN_APP,
            UPDATE_ORIGIN_SYSTEM_OR_SYSTEMUI,
            UPDATE_ORIGIN_RESTORE_BACKUP
    public static final int ORIGIN_RESTORE_BACKUP = 6;

    /**
     * The {@link ZenModeConfig} is updated from an app, and the app reports it's the result
     * of a user action (e.g. tapping a button in the Wellbeing App to start Bedtime Mode).
     * Corresponds to {@link NotificationManager#setAutomaticZenRuleState} with a
     * {@link Condition#source} equal to {@link Condition#SOURCE_USER_ACTION}.</li>
     */
    public static final int ORIGIN_USER_IN_APP = 7;

    @IntDef(prefix = { "ORIGIN_" }, value = {
            ORIGIN_UNKNOWN,
            ORIGIN_INIT,
            ORIGIN_INIT_USER,
            ORIGIN_USER_IN_SYSTEMUI,
            ORIGIN_APP,
            ORIGIN_SYSTEM,
            ORIGIN_RESTORE_BACKUP,
            ORIGIN_USER_IN_APP
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ConfigChangeOrigin {}
    public @interface ConfigOrigin {}

    public static final int SOURCE_ANYONE = Policy.PRIORITY_SENDERS_ANY;
    public static final int SOURCE_CONTACT = Policy.PRIORITY_SENDERS_CONTACTS;
    public static final int SOURCE_STAR = Policy.PRIORITY_SENDERS_STARRED;
    public static final int MAX_SOURCE = SOURCE_STAR;
    private static final int MAX_SOURCE = SOURCE_STAR;
    private static final int DEFAULT_SOURCE = SOURCE_STAR;
    private static final int DEFAULT_CALLS_SOURCE = SOURCE_STAR;

@@ -1174,7 +1189,7 @@ public class ZenModeConfig implements Parcelable {
            }
            if (Flags.modesUi()) {
                rt.disabledOrigin = safeInt(parser, RULE_ATT_DISABLED_ORIGIN,
                        UPDATE_ORIGIN_UNKNOWN);
                        ORIGIN_UNKNOWN);
                rt.legacySuppressedEffects = safeInt(parser,
                        RULE_ATT_LEGACY_SUPPRESSED_EFFECTS, 0);
            }
@@ -2537,7 +2552,8 @@ public class ZenModeConfig implements Parcelable {
        @ZenDeviceEffects.ModifiableField public int zenDeviceEffectsUserModifiedFields;
        @Nullable public Instant deletionInstant; // Only set on deleted rules.
        @FlaggedApi(Flags.FLAG_MODES_UI)
        @ConfigChangeOrigin public int disabledOrigin = UPDATE_ORIGIN_UNKNOWN;
        @ConfigOrigin
        public int disabledOrigin = ORIGIN_UNKNOWN;
        // The obsolete suppressed effects in NM.Policy (SCREEN_ON, SCREEN_OFF) cannot be put in a
        // ZenPolicy, so we store them here, only for the manual rule.
        @FlaggedApi(Flags.FLAG_MODES_UI)
+3 −3
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package com.android.settingslib.notification.modes;

import static android.service.notification.ZenModeConfig.UPDATE_ORIGIN_UNKNOWN;
import static android.service.notification.ZenModeConfig.UPDATE_ORIGIN_USER;
import static android.service.notification.ZenModeConfig.ORIGIN_UNKNOWN;
import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI;

import android.app.AutomaticZenRule;
import android.app.NotificationManager;
@@ -154,7 +154,7 @@ public class TestModeBuilder {
        mRule.setEnabled(enabled);
        mConfigZenRule.enabled = enabled;
        if (!enabled) {
            mConfigZenRule.disabledOrigin = byUser ? UPDATE_ORIGIN_USER : UPDATE_ORIGIN_UNKNOWN;
            mConfigZenRule.disabledOrigin = byUser ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_UNKNOWN;
        }
        return this;
    }
+2 −2
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public class ZenMode implements Parcelable {

    private static final String TAG = "ZenMode";

    static final String MANUAL_DND_MODE_ID = "manual_dnd";
    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.
@@ -119,7 +119,7 @@ public class ZenMode implements Parcelable {
                return Status.ENABLED;
            }
        } else {
            if (zenRuleExtraData.disabledOrigin == ZenModeConfig.UPDATE_ORIGIN_USER) {
            if (zenRuleExtraData.disabledOrigin == ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI) {
                return Status.DISABLED_BY_USER;
            } else {
                return Status.DISABLED_BY_OTHER; // by APP, SYSTEM, UNKNOWN.
+2 −2
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ public class ZenModeTest {
    public void constructor_disabledRuleByUser_statusDisabledByUser() {
        AutomaticZenRule azr = new AutomaticZenRule.Builder(ZEN_RULE).setEnabled(false).build();
        ZenModeConfig.ZenRule configZenRule = zenConfigRuleFor(azr, false);
        configZenRule.disabledOrigin = ZenModeConfig.UPDATE_ORIGIN_USER;
        configZenRule.disabledOrigin = ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI;

        ZenMode mode = new ZenMode("id", azr, configZenRule);
        assertThat(mode.getStatus()).isEqualTo(ZenMode.Status.DISABLED_BY_USER);
@@ -103,7 +103,7 @@ public class ZenModeTest {
    public void constructor_disabledRuleByOther_statusDisabledByOther() {
        AutomaticZenRule azr = new AutomaticZenRule.Builder(ZEN_RULE).setEnabled(false).build();
        ZenModeConfig.ZenRule configZenRule = zenConfigRuleFor(azr, false);
        configZenRule.disabledOrigin = ZenModeConfig.UPDATE_ORIGIN_APP;
        configZenRule.disabledOrigin = ZenModeConfig.ORIGIN_APP;

        ZenMode mode = new ZenMode("id", azr, configZenRule);
        assertThat(mode.getStatus()).isEqualTo(ZenMode.Status.DISABLED_BY_OTHER);
Loading