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

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

Moved zen-conversion-related methods into ZenAdapters

This includes any "pure" Policy<->ZenPolicy conversion function that doesn't require internal state of the callers.

(Also moved the class so it's accessible to Settings).

Bug: 309922141
Test: atest ZenModeHelperTest ZenAdaptersTest
Change-Id: Ieadd237bb7d7ab5ea41c76f2770edc1af68a0510
parent ef666780
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2052,10 +2052,12 @@ public class NotificationManager {

        /** Notification senders to prioritize for calls. One of:
         * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
        @PrioritySenders
        public final int priorityCallSenders;

        /** Notification senders to prioritize for messages. One of:
         * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
        @PrioritySenders
        public final int priorityMessageSenders;

        /**
@@ -2063,6 +2065,7 @@ public class NotificationManager {
         * {@link #CONVERSATION_SENDERS_NONE}, {@link #CONVERSATION_SENDERS_IMPORTANT},
         * {@link #CONVERSATION_SENDERS_ANYONE}.
         */
        @ConversationSenders
        public final int priorityConversationSenders;

        /**
@@ -2630,16 +2633,19 @@ public class NotificationManager {
        }

        /** @hide **/
        @PrioritySenders
        public int allowCallsFrom() {
            return priorityCallSenders;
        }

        /** @hide **/
        @PrioritySenders
        public int allowMessagesFrom() {
            return priorityMessageSenders;
        }

        /** @hide **/
        @ConversationSenders
        public int allowConversationsFrom() {
            return priorityConversationSenders;
        }
+63 −11
Original line number Diff line number Diff line
@@ -14,24 +14,27 @@
 * limitations under the License.
 */

package com.android.server.notification;
package android.service.notification;

import android.annotation.NonNull;
import android.app.Flags;
import android.app.NotificationManager.Policy;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenPolicy;

/**
 * Converters between different Zen representations.
 * @hide
 */
class ZenAdapters {
public class ZenAdapters {

    static ZenPolicy notificationPolicyToZenPolicy(Policy policy) {
    /** Maps {@link Policy} to {@link ZenPolicy}. */
    @NonNull
    public static ZenPolicy notificationPolicyToZenPolicy(@NonNull Policy policy) {
        ZenPolicy.Builder zenPolicyBuilder = new ZenPolicy.Builder()
                .allowAlarms(policy.allowAlarms())
                .allowCalls(
                        policy.allowCalls()
                                ? ZenModeConfig.getZenPolicySenders(policy.allowCallsFrom())
                                ? notificationPolicySendersToZenPolicyPeopleType(
                                        policy.allowCallsFrom())
                        : ZenPolicy.PEOPLE_TYPE_NONE)
                .allowConversations(
                        policy.allowConversations()
@@ -42,7 +45,8 @@ class ZenAdapters {
                .allowMedia(policy.allowMedia())
                .allowMessages(
                        policy.allowMessages()
                                ? ZenModeConfig.getZenPolicySenders(policy.allowMessagesFrom())
                                ? notificationPolicySendersToZenPolicyPeopleType(
                                        policy.allowMessagesFrom())
                                : ZenPolicy.PEOPLE_TYPE_NONE)
                .allowReminders(policy.allowReminders())
                .allowRepeatCallers(policy.allowRepeatCallers())
@@ -65,9 +69,58 @@ class ZenAdapters {
        return zenPolicyBuilder.build();
    }

    /** Maps {@link ZenPolicy.PeopleType} enum to {@link Policy.PrioritySenders}. */
    @Policy.PrioritySenders
    public static int zenPolicyPeopleTypeToNotificationPolicySenders(
            @ZenPolicy.PeopleType int zpPeopleType, @Policy.PrioritySenders int defaultResult) {
        switch (zpPeopleType) {
            case ZenPolicy.PEOPLE_TYPE_ANYONE:
                return Policy.PRIORITY_SENDERS_ANY;
            case ZenPolicy.PEOPLE_TYPE_CONTACTS:
                return Policy.PRIORITY_SENDERS_CONTACTS;
            case ZenPolicy.PEOPLE_TYPE_STARRED:
                return Policy.PRIORITY_SENDERS_STARRED;
            default:
                return defaultResult;
        }
    }

    /** Maps {@link Policy.PrioritySenders} enum to {@link ZenPolicy.PeopleType}. */
    @ZenPolicy.PeopleType
    public static int notificationPolicySendersToZenPolicyPeopleType(
            @Policy.PrioritySenders int npPrioritySenders) {
        switch (npPrioritySenders) {
            case Policy.PRIORITY_SENDERS_ANY:
                return ZenPolicy.PEOPLE_TYPE_ANYONE;
            case Policy.PRIORITY_SENDERS_CONTACTS:
                return ZenPolicy.PEOPLE_TYPE_CONTACTS;
            case Policy.PRIORITY_SENDERS_STARRED:
            default:
                return ZenPolicy.PEOPLE_TYPE_STARRED;
        }
    }

    /** Maps {@link ZenPolicy.ConversationSenders} enum to {@link Policy.ConversationSenders}. */
    @Policy.ConversationSenders
    public static int zenPolicyConversationSendersToNotificationPolicy(
            @ZenPolicy.ConversationSenders int zpConversationSenders,
            @Policy.ConversationSenders int defaultResult) {
        switch (zpConversationSenders) {
            case ZenPolicy.CONVERSATION_SENDERS_ANYONE:
                return Policy.CONVERSATION_SENDERS_ANYONE;
            case ZenPolicy.CONVERSATION_SENDERS_IMPORTANT:
                return Policy.CONVERSATION_SENDERS_IMPORTANT;
            case ZenPolicy.CONVERSATION_SENDERS_NONE:
                return Policy.CONVERSATION_SENDERS_NONE;
            default:
                return defaultResult;
        }
    }

    /** Maps {@link Policy.ConversationSenders} enum to {@link ZenPolicy.ConversationSenders}. */
    @ZenPolicy.ConversationSenders
    private static int notificationPolicyConversationSendersToZenPolicy(
            int npPriorityConversationSenders) {
            @Policy.ConversationSenders int npPriorityConversationSenders) {
        switch (npPriorityConversationSenders) {
            case Policy.CONVERSATION_SENDERS_ANYONE:
                return ZenPolicy.CONVERSATION_SENDERS_ANYONE;
@@ -75,8 +128,7 @@ class ZenAdapters {
                return ZenPolicy.CONVERSATION_SENDERS_IMPORTANT;
            case Policy.CONVERSATION_SENDERS_NONE:
                return ZenPolicy.CONVERSATION_SENDERS_NONE;
            case Policy.CONVERSATION_SENDERS_UNSET:
            default:
            default: // including Policy.CONVERSATION_SENDERS_UNSET
                return ZenPolicy.CONVERSATION_SENDERS_UNSET;
        }
    }
+11 −58
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCRE
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.service.notification.ZenAdapters.notificationPolicySendersToZenPolicyPeopleType;
import static android.service.notification.ZenAdapters.zenPolicyConversationSendersToNotificationPolicy;
import static android.service.notification.ZenAdapters.zenPolicyPeopleTypeToNotificationPolicySenders;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
@@ -1269,11 +1272,11 @@ public class ZenModeConfig implements Parcelable {
    public ZenPolicy toZenPolicy() {
        ZenPolicy.Builder builder = new ZenPolicy.Builder()
                .allowCalls(allowCalls
                        ? ZenModeConfig.getZenPolicySenders(allowCallsFrom)
                        ? notificationPolicySendersToZenPolicyPeopleType(allowCallsFrom)
                        : ZenPolicy.PEOPLE_TYPE_NONE)
                .allowRepeatCallers(allowRepeatCallers)
                .allowMessages(allowMessages
                        ? ZenModeConfig.getZenPolicySenders(allowMessagesFrom)
                        ? notificationPolicySendersToZenPolicyPeopleType(allowMessagesFrom)
                        : ZenPolicy.PEOPLE_TYPE_NONE)
                .allowReminders(allowReminders)
                .allowEvents(allowEvents)
@@ -1333,14 +1336,14 @@ public class ZenModeConfig implements Parcelable {
        if (zenPolicy.isCategoryAllowed(ZenPolicy.PRIORITY_CATEGORY_MESSAGES,
                isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_MESSAGES, defaultPolicy))) {
            priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES;
            messageSenders = getNotificationPolicySenders(zenPolicy.getPriorityMessageSenders(),
                    messageSenders);
            messageSenders = zenPolicyPeopleTypeToNotificationPolicySenders(
                    zenPolicy.getPriorityMessageSenders(), messageSenders);
        }

        if (zenPolicy.isCategoryAllowed(ZenPolicy.PRIORITY_CATEGORY_CONVERSATIONS,
                isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CONVERSATIONS, defaultPolicy))) {
            priorityCategories |= Policy.PRIORITY_CATEGORY_CONVERSATIONS;
            conversationSenders = getConversationSendersWithDefault(
            conversationSenders = zenPolicyConversationSendersToNotificationPolicy(
                    zenPolicy.getPriorityConversationSenders(), conversationSenders);
        } else {
            conversationSenders = CONVERSATION_SENDERS_NONE;
@@ -1349,8 +1352,8 @@ public class ZenModeConfig implements Parcelable {
        if (zenPolicy.isCategoryAllowed(ZenPolicy.PRIORITY_CATEGORY_CALLS,
                isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS, defaultPolicy))) {
            priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS;
            callSenders = getNotificationPolicySenders(zenPolicy.getPriorityCallSenders(),
                    callSenders);
            callSenders = zenPolicyPeopleTypeToNotificationPolicySenders(
                    zenPolicy.getPriorityCallSenders(), callSenders);
        }

        if (zenPolicy.isCategoryAllowed(ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS,
@@ -1449,47 +1452,6 @@ public class ZenModeConfig implements Parcelable {
        return (policy.suppressedVisualEffects & visualEffect) == 0;
    }

    private static int getNotificationPolicySenders(@ZenPolicy.PeopleType int senders,
            int defaultPolicySender) {
        switch (senders) {
            case ZenPolicy.PEOPLE_TYPE_ANYONE:
                return Policy.PRIORITY_SENDERS_ANY;
            case ZenPolicy.PEOPLE_TYPE_CONTACTS:
                return Policy.PRIORITY_SENDERS_CONTACTS;
            case ZenPolicy.PEOPLE_TYPE_STARRED:
                return Policy.PRIORITY_SENDERS_STARRED;
            default:
                return defaultPolicySender;
        }
    }

    private static int getConversationSendersWithDefault(@ZenPolicy.ConversationSenders int senders,
            int defaultPolicySender) {
        switch (senders) {
            case ZenPolicy.CONVERSATION_SENDERS_ANYONE:
            case ZenPolicy.CONVERSATION_SENDERS_IMPORTANT:
            case ZenPolicy.CONVERSATION_SENDERS_NONE:
                return senders;
            default:
                return defaultPolicySender;
        }
    }

    /**
     * Maps NotificationManager.Policy senders type to ZenPolicy.PeopleType
     */
    public static @ZenPolicy.PeopleType int getZenPolicySenders(int senders) {
        switch (senders) {
            case Policy.PRIORITY_SENDERS_ANY:
                return ZenPolicy.PEOPLE_TYPE_ANYONE;
            case Policy.PRIORITY_SENDERS_CONTACTS:
                return ZenPolicy.PEOPLE_TYPE_CONTACTS;
            case Policy.PRIORITY_SENDERS_STARRED:
            default:
                return ZenPolicy.PEOPLE_TYPE_STARRED;
        }
    }

    public Policy toNotificationPolicy() {
        int priorityCategories = 0;
        int priorityCallSenders = Policy.PRIORITY_SENDERS_CONTACTS;
@@ -1524,7 +1486,7 @@ public class ZenModeConfig implements Parcelable {
        }
        priorityCallSenders = sourceToPrioritySenders(allowCallsFrom, priorityCallSenders);
        priorityMessageSenders = sourceToPrioritySenders(allowMessagesFrom, priorityMessageSenders);
        priorityConversationSenders = getConversationSendersWithDefault(
        priorityConversationSenders = zenPolicyConversationSendersToNotificationPolicy(
                allowConversationsFrom, priorityConversationSenders);

        int state = areChannelsBypassingDnd ? Policy.STATE_CHANNELS_BYPASSING_DND : 0;
@@ -1559,15 +1521,6 @@ public class ZenModeConfig implements Parcelable {
        }
    }

    private static int prioritySendersToSource(int prioritySenders, int def) {
        switch (prioritySenders) {
            case Policy.PRIORITY_SENDERS_CONTACTS: return SOURCE_CONTACT;
            case Policy.PRIORITY_SENDERS_STARRED: return SOURCE_STAR;
            case Policy.PRIORITY_SENDERS_ANY: return SOURCE_ANYONE;
            default: return def;
        }
    }

    private static int normalizePrioritySenders(int prioritySenders, int def) {
        if (!(prioritySenders == Policy.PRIORITY_SENDERS_CONTACTS
                || prioritySenders == Policy.PRIORITY_SENDERS_STARRED
+5 −2
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Process;
import android.service.notification.DNDPolicyProto;
import android.service.notification.ZenAdapters;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ConfigChangeOrigin;
import android.service.notification.ZenModeConfig.ZenRule;
@@ -591,9 +592,11 @@ class ZenModeEventLogger {
                // This applies to both call and message senders, but not conversation senders,
                // where they use the same enum values.
                proto.write(DNDPolicyProto.ALLOW_CALLS_FROM,
                        ZenModeConfig.getZenPolicySenders(mNewPolicy.allowCallsFrom()));
                        ZenAdapters.notificationPolicySendersToZenPolicyPeopleType(
                                mNewPolicy.allowCallsFrom()));
                proto.write(DNDPolicyProto.ALLOW_MESSAGES_FROM,
                        ZenModeConfig.getZenPolicySenders(mNewPolicy.allowMessagesFrom()));
                        ZenAdapters.notificationPolicySendersToZenPolicyPeopleType(
                                mNewPolicy.allowMessagesFrom()));
                proto.write(DNDPolicyProto.ALLOW_CONVERSATIONS_FROM,
                        mNewPolicy.allowConversationsFrom());

+1 −0
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ import android.provider.Settings.Global;
import android.service.notification.Condition;
import android.service.notification.ConditionProviderService;
import android.service.notification.DeviceEffectsApplier;
import android.service.notification.ZenAdapters;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ConfigChangeOrigin;
Loading