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

Commit 8980e8ae authored by Yuri Lin's avatar Yuri Lin
Browse files

Change channels API to focus on priority channels.

This changes the ZenPolicy interface to also use booleans for setting channels, similar to how it was already implemented in NotificationManager.Policy.

Also renames the channels enum to be CHANNEL_POLICY instead of CHANNEL_TYPE, though the enum is also no longer surfaced in public API.

Bug: 308672314
Fixes: 315860311
Fixes: 315045597
Test: atest ZenModeHelperTest ZenPolicyTest ZenModeConfigTest NotificationManagerZenTest ZenModeFilteringTest

Change-Id: If19175a4aee030c51fbaa7dc3a1a83cae1759798
parent 6ca34576
Loading
Loading
Loading
Loading
+2 −5
Original line number Original line Diff line number Diff line
@@ -40921,7 +40921,6 @@ package android.service.notification {
  public final class ZenPolicy implements android.os.Parcelable {
  public final class ZenPolicy implements android.os.Parcelable {
    method public int describeContents();
    method public int describeContents();
    method @FlaggedApi("android.app.modes_api") public int getAllowedChannels();
    method public int getPriorityCallSenders();
    method public int getPriorityCallSenders();
    method public int getPriorityCategoryAlarms();
    method public int getPriorityCategoryAlarms();
    method public int getPriorityCategoryCalls();
    method public int getPriorityCategoryCalls();
@@ -40932,6 +40931,7 @@ package android.service.notification {
    method public int getPriorityCategoryReminders();
    method public int getPriorityCategoryReminders();
    method public int getPriorityCategoryRepeatCallers();
    method public int getPriorityCategoryRepeatCallers();
    method public int getPriorityCategorySystem();
    method public int getPriorityCategorySystem();
    method @FlaggedApi("android.app.modes_api") public int getPriorityChannels();
    method public int getPriorityConversationSenders();
    method public int getPriorityConversationSenders();
    method public int getPriorityMessageSenders();
    method public int getPriorityMessageSenders();
    method public int getVisualEffectAmbient();
    method public int getVisualEffectAmbient();
@@ -40942,9 +40942,6 @@ package android.service.notification {
    method public int getVisualEffectPeek();
    method public int getVisualEffectPeek();
    method public int getVisualEffectStatusBar();
    method public int getVisualEffectStatusBar();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
    field @FlaggedApi("android.app.modes_api") public static final int CHANNEL_TYPE_NONE = 2; // 0x2
    field @FlaggedApi("android.app.modes_api") public static final int CHANNEL_TYPE_PRIORITY = 1; // 0x1
    field @FlaggedApi("android.app.modes_api") public static final int CHANNEL_TYPE_UNSET = 0; // 0x0
    field public static final int CONVERSATION_SENDERS_ANYONE = 1; // 0x1
    field public static final int CONVERSATION_SENDERS_ANYONE = 1; // 0x1
    field public static final int CONVERSATION_SENDERS_IMPORTANT = 2; // 0x2
    field public static final int CONVERSATION_SENDERS_IMPORTANT = 2; // 0x2
    field public static final int CONVERSATION_SENDERS_NONE = 3; // 0x3
    field public static final int CONVERSATION_SENDERS_NONE = 3; // 0x3
@@ -40965,11 +40962,11 @@ package android.service.notification {
    method @NonNull public android.service.notification.ZenPolicy.Builder allowAlarms(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowAlarms(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowAllSounds();
    method @NonNull public android.service.notification.ZenPolicy.Builder allowAllSounds();
    method @NonNull public android.service.notification.ZenPolicy.Builder allowCalls(int);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowCalls(int);
    method @FlaggedApi("android.app.modes_api") @NonNull public android.service.notification.ZenPolicy.Builder allowChannels(int);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowConversations(int);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowConversations(int);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowEvents(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowEvents(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowMedia(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowMedia(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowMessages(int);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowMessages(int);
    method @FlaggedApi("android.app.modes_api") @NonNull public android.service.notification.ZenPolicy.Builder allowPriorityChannels(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowReminders(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowReminders(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowRepeatCallers(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowRepeatCallers(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowSystem(boolean);
    method @NonNull public android.service.notification.ZenPolicy.Builder allowSystem(boolean);
+8 −27
Original line number Original line Diff line number Diff line
@@ -204,7 +204,7 @@ public class ZenModeConfig implements Parcelable {
    private static final String ALLOW_ATT_SCREEN_ON = "visualScreenOn";
    private static final String ALLOW_ATT_SCREEN_ON = "visualScreenOn";
    private static final String ALLOW_ATT_CONV = "convos";
    private static final String ALLOW_ATT_CONV = "convos";
    private static final String ALLOW_ATT_CONV_FROM = "convosFrom";
    private static final String ALLOW_ATT_CONV_FROM = "convosFrom";
    private static final String ALLOW_ATT_CHANNELS = "channels";
    private static final String ALLOW_ATT_CHANNELS = "priorityChannels";
    private static final String USER_MODIFIED_FIELDS = "policyUserModifiedFields";
    private static final String USER_MODIFIED_FIELDS = "policyUserModifiedFields";
    private static final String DISALLOW_TAG = "disallow";
    private static final String DISALLOW_TAG = "disallow";
    private static final String DISALLOW_ATT_VISUAL_EFFECTS = "visualEffects";
    private static final String DISALLOW_ATT_VISUAL_EFFECTS = "visualEffects";
@@ -856,9 +856,9 @@ public class ZenModeConfig implements Parcelable {
        final int events = safeInt(parser, ALLOW_ATT_EVENTS, ZenPolicy.STATE_UNSET);
        final int events = safeInt(parser, ALLOW_ATT_EVENTS, ZenPolicy.STATE_UNSET);
        final int reminders = safeInt(parser, ALLOW_ATT_REMINDERS, ZenPolicy.STATE_UNSET);
        final int reminders = safeInt(parser, ALLOW_ATT_REMINDERS, ZenPolicy.STATE_UNSET);
        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            final int channels = safeInt(parser, ALLOW_ATT_CHANNELS, ZenPolicy.CHANNEL_TYPE_UNSET);
            final int channels = safeInt(parser, ALLOW_ATT_CHANNELS, ZenPolicy.STATE_UNSET);
            if (channels != ZenPolicy.CHANNEL_TYPE_UNSET) {
            if (channels != ZenPolicy.STATE_UNSET) {
                builder.allowChannels(channels);
                builder.allowPriorityChannels(channels == ZenPolicy.STATE_ALLOW);
                policySet = true;
                policySet = true;
            }
            }
            builder.setUserModifiedFields(safeInt(parser, USER_MODIFIED_FIELDS, 0));
            builder.setUserModifiedFields(safeInt(parser, USER_MODIFIED_FIELDS, 0));
@@ -973,7 +973,7 @@ public class ZenModeConfig implements Parcelable {
                out);
                out);


        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            writeZenPolicyState(ALLOW_ATT_CHANNELS, policy.getAllowedChannels(), out);
            writeZenPolicyState(ALLOW_ATT_CHANNELS, policy.getPriorityChannels(), out);
            out.attributeInt(null, USER_MODIFIED_FIELDS, policy.getUserModifiedFields());
            out.attributeInt(null, USER_MODIFIED_FIELDS, policy.getUserModifiedFields());
        }
        }
    }
    }
@@ -990,7 +990,7 @@ public class ZenModeConfig implements Parcelable {
                out.attributeInt(null, attr, val);
                out.attributeInt(null, attr, val);
            }
            }
        } else if (Flags.modesApi() && Objects.equals(attr, ALLOW_ATT_CHANNELS)) {
        } else if (Flags.modesApi() && Objects.equals(attr, ALLOW_ATT_CHANNELS)) {
            if (val != ZenPolicy.CHANNEL_TYPE_UNSET) {
            if (val != ZenPolicy.STATE_UNSET) {
                out.attributeInt(null, attr, val);
                out.attributeInt(null, attr, val);
            }
            }
        } else {
        } else {
@@ -1175,8 +1175,7 @@ public class ZenModeConfig implements Parcelable {
        }
        }


        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            builder.allowChannels(allowPriorityChannels ? ZenPolicy.CHANNEL_TYPE_PRIORITY
            builder.allowPriorityChannels(allowPriorityChannels);
                    : ZenPolicy.CHANNEL_TYPE_NONE);
        }
        }
        return builder.build();
        return builder.build();
    }
    }
@@ -1306,7 +1305,7 @@ public class ZenModeConfig implements Parcelable {
        int state = defaultPolicy.state;
        int state = defaultPolicy.state;
        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            state = Policy.policyState(defaultPolicy.hasPriorityChannels(),
            state = Policy.policyState(defaultPolicy.hasPriorityChannels(),
                    getAllowPriorityChannelsWithDefault(zenPolicy.getAllowedChannels(),
                    ZenPolicy.stateToBoolean(zenPolicy.getPriorityChannels(),
                            DEFAULT_ALLOW_PRIORITY_CHANNELS));
                            DEFAULT_ALLOW_PRIORITY_CHANNELS));
        }
        }


@@ -1348,24 +1347,6 @@ public class ZenModeConfig implements Parcelable {
        }
        }
    }
    }


    /**
     * Gets whether priority channels are permitted by this channel type, with the specified
     * default if the value is unset. This effectively converts the channel enum to a boolean,
     * where "true" indicates priority channels are allowed to break through and "false" means
     * they are not.
     */
    public static boolean getAllowPriorityChannelsWithDefault(
            @ZenPolicy.ChannelType int channelType, boolean defaultAllowChannels) {
        switch (channelType) {
            case ZenPolicy.CHANNEL_TYPE_PRIORITY:
                return true;
            case ZenPolicy.CHANNEL_TYPE_NONE:
                return false;
            default:
                return defaultAllowChannels;
        }
    }

    /**
    /**
     * Maps NotificationManager.Policy senders type to ZenPolicy.PeopleType
     * Maps NotificationManager.Policy senders type to ZenPolicy.PeopleType
     */
     */
+42 −39
Original line number Original line Diff line number Diff line
@@ -184,7 +184,8 @@ public final class ZenPolicy implements Parcelable {
    private @PeopleType int mPriorityMessages = PEOPLE_TYPE_UNSET;
    private @PeopleType int mPriorityMessages = PEOPLE_TYPE_UNSET;
    private @PeopleType int mPriorityCalls = PEOPLE_TYPE_UNSET;
    private @PeopleType int mPriorityCalls = PEOPLE_TYPE_UNSET;
    private @ConversationSenders int mConversationSenders = CONVERSATION_SENDERS_UNSET;
    private @ConversationSenders int mConversationSenders = CONVERSATION_SENDERS_UNSET;
    private @ChannelType int mAllowChannels = CHANNEL_TYPE_UNSET;
    @FlaggedApi(Flags.FLAG_MODES_API)
    private @ChannelType int mAllowChannels = CHANNEL_POLICY_UNSET;
    private final @ModifiableField int mUserModifiedFields; // Bitwise representation
    private final @ModifiableField int mUserModifiedFields; // Bitwise representation


    /** @hide */
    /** @hide */
@@ -354,35 +355,34 @@ public final class ZenPolicy implements Parcelable {
     */
     */
    public static final int STATE_DISALLOW = 2;
    public static final int STATE_DISALLOW = 2;


    /** @hide */
    @IntDef(prefix = { "CHANNEL_POLICY_" }, value = {
    @IntDef(prefix = { "CHANNEL_TYPE_" }, value = {
            CHANNEL_POLICY_UNSET,
            CHANNEL_TYPE_UNSET,
            CHANNEL_POLICY_PRIORITY,
            CHANNEL_TYPE_PRIORITY,
            CHANNEL_POLICY_NONE,
            CHANNEL_TYPE_NONE,
    })
    })
    @Retention(RetentionPolicy.SOURCE)
    @Retention(RetentionPolicy.SOURCE)
    public @interface ChannelType {}
    private @interface ChannelType {}


    /**
    /**
     * Indicates no explicit setting for which channels may bypass DND when this policy is active.
     * Indicates no explicit setting for which channels may bypass DND when this policy is active.
     * Defaults to {@link #CHANNEL_TYPE_PRIORITY}.
     * Defaults to {@link #CHANNEL_POLICY_PRIORITY}.
     */
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static final int CHANNEL_TYPE_UNSET = 0;
    private static final int CHANNEL_POLICY_UNSET = 0;


    /**
    /**
     * Indicates that channels marked as {@link NotificationChannel#canBypassDnd()} can bypass DND
     * Indicates that channels marked as {@link NotificationChannel#canBypassDnd()} can bypass DND
     * when this policy is active.
     * when this policy is active.
     */
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static final int CHANNEL_TYPE_PRIORITY = 1;
    private static final int CHANNEL_POLICY_PRIORITY = 1;


    /**
    /**
     * Indicates that no channels can bypass DND when this policy is active, even those marked as
     * Indicates that no channels can bypass DND when this policy is active, even those marked as
     * {@link NotificationChannel#canBypassDnd()}.
     * {@link NotificationChannel#canBypassDnd()}.
     */
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static final int CHANNEL_TYPE_NONE = 2;
    private static final int CHANNEL_POLICY_NONE = 2;


    /** @hide */
    /** @hide */
    public ZenPolicy() {
    public ZenPolicy() {
@@ -584,16 +584,21 @@ public final class ZenPolicy implements Parcelable {
    }
    }


    /**
    /**
     * Which types of {@link NotificationChannel channels} this policy allows to bypass DND. When
     * Whether this policy allows {@link NotificationChannel channels} marked as
     * this value is {@link #CHANNEL_TYPE_PRIORITY priority} channels, any channel with
     * {@link NotificationChannel#canBypassDnd()} to bypass DND. If {@link #STATE_ALLOW}, these
     * canBypassDnd() may bypass DND; when it is {@link #CHANNEL_TYPE_NONE none}, even channels
     * channels may bypass; if {@link #STATE_DISALLOW}, then even notifications from channels
     * with canBypassDnd() will be intercepted.
     * with {@link NotificationChannel#canBypassDnd()} will be intercepted.
     * @return {@link #CHANNEL_TYPE_UNSET}, {@link #CHANNEL_TYPE_PRIORITY}, or
     *   {@link #CHANNEL_TYPE_NONE}
     */
     */
    @FlaggedApi(Flags.FLAG_MODES_API)
    @FlaggedApi(Flags.FLAG_MODES_API)
    public @ChannelType int getAllowedChannels() {
    public @State int getPriorityChannels() {
        return mAllowChannels;
        switch (mAllowChannels) {
            case CHANNEL_POLICY_PRIORITY:
                return STATE_ALLOW;
            case CHANNEL_POLICY_NONE:
                return STATE_DISALLOW;
            default:
                return STATE_UNSET;
        }
    }
    }


    /**
    /**
@@ -1016,8 +1021,8 @@ public final class ZenPolicy implements Parcelable {
         * Set whether priority channels are permitted to break through DND.
         * Set whether priority channels are permitted to break through DND.
         */
         */
        @FlaggedApi(Flags.FLAG_MODES_API)
        @FlaggedApi(Flags.FLAG_MODES_API)
        public @NonNull Builder allowChannels(@ChannelType int channelType) {
        public @NonNull Builder allowPriorityChannels(boolean allow) {
            mZenPolicy.mAllowChannels = channelType;
            mZenPolicy.mAllowChannels = allow ? CHANNEL_POLICY_PRIORITY : CHANNEL_POLICY_NONE;
            return this;
            return this;
        }
        }


@@ -1305,11 +1310,11 @@ public final class ZenPolicy implements Parcelable {
    @FlaggedApi(Flags.FLAG_MODES_API)
    @FlaggedApi(Flags.FLAG_MODES_API)
    public static String channelTypeToString(@ChannelType int channelType) {
    public static String channelTypeToString(@ChannelType int channelType) {
        switch (channelType) {
        switch (channelType) {
            case CHANNEL_TYPE_UNSET:
            case CHANNEL_POLICY_UNSET:
                return "unset";
                return "unset";
            case CHANNEL_TYPE_PRIORITY:
            case CHANNEL_POLICY_PRIORITY:
                return "priority";
                return "priority";
            case CHANNEL_TYPE_NONE:
            case CHANNEL_POLICY_NONE:
                return "none";
                return "none";
        }
        }
        return "invalidChannelType{" + channelType + "}";
        return "invalidChannelType{" + channelType + "}";
@@ -1389,11 +1394,11 @@ public final class ZenPolicy implements Parcelable {
    }
    }


    /** @hide */
    /** @hide */
    public boolean isCategoryAllowed(@PriorityCategory int category, boolean defaultVal) {
    public static boolean stateToBoolean(@State int state, boolean defaultVal) {
        switch (getZenPolicyPriorityCategoryState(category)) {
        switch (state) {
            case ZenPolicy.STATE_ALLOW:
            case STATE_ALLOW:
                return true;
                return true;
            case ZenPolicy.STATE_DISALLOW:
            case STATE_DISALLOW:
                return false;
                return false;
            default:
            default:
                return defaultVal;
                return defaultVal;
@@ -1401,15 +1406,13 @@ public final class ZenPolicy implements Parcelable {
    }
    }


    /** @hide */
    /** @hide */
    public boolean isVisualEffectAllowed(@VisualEffect int effect, boolean defaultVal) {
    public boolean isCategoryAllowed(@PriorityCategory int category, boolean defaultVal) {
        switch (getZenPolicyVisualEffectState(effect)) {
        return stateToBoolean(getZenPolicyPriorityCategoryState(category), defaultVal);
            case ZenPolicy.STATE_ALLOW:
                return true;
            case ZenPolicy.STATE_DISALLOW:
                return false;
            default:
                return defaultVal;
    }
    }

    /** @hide */
    public boolean isVisualEffectAllowed(@VisualEffect int effect, boolean defaultVal) {
        return stateToBoolean(getZenPolicyVisualEffectState(effect), defaultVal);
    }
    }


    /**
    /**
@@ -1463,8 +1466,8 @@ public final class ZenPolicy implements Parcelable {
        // apply allowed channels
        // apply allowed channels
        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            // if no channels are allowed, can't newly allow them
            // if no channels are allowed, can't newly allow them
            if (mAllowChannels != CHANNEL_TYPE_NONE
            if (mAllowChannels != CHANNEL_POLICY_NONE
                    && policyToApply.mAllowChannels != CHANNEL_TYPE_UNSET) {
                    && policyToApply.mAllowChannels != CHANNEL_POLICY_UNSET) {
                mAllowChannels = policyToApply.mAllowChannels;
                mAllowChannels = policyToApply.mAllowChannels;
            }
            }
        }
        }
@@ -1530,7 +1533,7 @@ public final class ZenPolicy implements Parcelable {
        proto.write(DNDPolicyProto.ALLOW_CONVERSATIONS_FROM, getPriorityConversationSenders());
        proto.write(DNDPolicyProto.ALLOW_CONVERSATIONS_FROM, getPriorityConversationSenders());


        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            proto.write(DNDPolicyProto.ALLOW_CHANNELS, getAllowedChannels());
            proto.write(DNDPolicyProto.ALLOW_CHANNELS, getPriorityChannels());
        }
        }


        proto.flush();
        proto.flush();
+5 −5
Original line number Original line Diff line number Diff line
@@ -360,7 +360,7 @@ message DNDPolicyProto {


    optional ConversationType allow_conversations_from = 19;
    optional ConversationType allow_conversations_from = 19;


    optional ChannelType allow_channels = 20;
    optional ChannelPolicy allow_channels = 20;
}
}


// Enum identifying the type of rule that changed; values set to match ones used in the
// Enum identifying the type of rule that changed; values set to match ones used in the
@@ -373,8 +373,8 @@ enum RuleType {


// Enum used in DNDPolicyProto to indicate the type of channels permitted to
// Enum used in DNDPolicyProto to indicate the type of channels permitted to
// break through DND. Mirrors values in ZenPolicy.
// break through DND. Mirrors values in ZenPolicy.
enum ChannelType {
enum ChannelPolicy {
    CHANNEL_TYPE_UNSET = 0;
    CHANNEL_POLICY_UNSET = 0;
    CHANNEL_TYPE_PRIORITY = 1;
    CHANNEL_POLICY_PRIORITY = 1;
    CHANNEL_TYPE_NONE = 2;
    CHANNEL_POLICY_NONE = 2;
}
}
+1 −3
Original line number Original line Diff line number Diff line
@@ -59,9 +59,7 @@ class ZenAdapters {
        }
        }


        if (Flags.modesApi()) {
        if (Flags.modesApi()) {
            zenPolicyBuilder.allowChannels(
            zenPolicyBuilder.allowPriorityChannels(policy.allowPriorityChannels());
                    policy.allowPriorityChannels()
                            ? ZenPolicy.CHANNEL_TYPE_PRIORITY : ZenPolicy.CHANNEL_TYPE_NONE);
        }
        }


        return zenPolicyBuilder.build();
        return zenPolicyBuilder.build();
Loading