1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.service.notification;
18 
19 import android.annotation.NonNull;
20 import android.app.Flags;
21 import android.app.NotificationManager.Policy;
22 
23 /**
24  * Converters between different Zen representations.
25  * @hide
26  */
27 public class ZenAdapters {
28 
29     /** Maps {@link Policy} to {@link ZenPolicy}. */
30     @NonNull
notificationPolicyToZenPolicy(@onNull Policy policy)31     public static ZenPolicy notificationPolicyToZenPolicy(@NonNull Policy policy) {
32         ZenPolicy.Builder zenPolicyBuilder = new ZenPolicy.Builder()
33                 .allowAlarms(policy.allowAlarms())
34                 .allowCalls(
35                         policy.allowCalls()
36                                 ? prioritySendersToPeopleType(
37                                         policy.allowCallsFrom())
38                         : ZenPolicy.PEOPLE_TYPE_NONE)
39                 .allowConversations(
40                         policy.allowConversations()
41                                 ? notificationPolicyConversationSendersToZenPolicy(
42                                         policy.allowConversationsFrom())
43                                 : ZenPolicy.CONVERSATION_SENDERS_NONE)
44                 .allowEvents(policy.allowEvents())
45                 .allowMedia(policy.allowMedia())
46                 .allowMessages(
47                         policy.allowMessages()
48                                 ? prioritySendersToPeopleType(
49                                         policy.allowMessagesFrom())
50                                 : ZenPolicy.PEOPLE_TYPE_NONE)
51                 .allowReminders(policy.allowReminders())
52                 .allowRepeatCallers(policy.allowRepeatCallers())
53                 .allowSystem(policy.allowSystem());
54 
55         if (policy.suppressedVisualEffects != Policy.SUPPRESSED_EFFECTS_UNSET) {
56             zenPolicyBuilder.showBadges(policy.showBadges())
57                     .showFullScreenIntent(policy.showFullScreenIntents())
58                     .showInAmbientDisplay(policy.showAmbient())
59                     .showInNotificationList(policy.showInNotificationList())
60                     .showLights(policy.showLights())
61                     .showPeeking(policy.showPeeking())
62                     .showStatusBarIcons(policy.showStatusBarIcons());
63         }
64 
65         if (Flags.modesApi()) {
66             zenPolicyBuilder.allowPriorityChannels(policy.allowPriorityChannels());
67         }
68 
69         return zenPolicyBuilder.build();
70     }
71 
72     /** Maps {@link ZenPolicy.PeopleType} enum to {@link Policy.PrioritySenders}. */
73     @Policy.PrioritySenders
peopleTypeToPrioritySenders( @enPolicy.PeopleType int zpPeopleType, @Policy.PrioritySenders int defaultResult)74     public static int peopleTypeToPrioritySenders(
75             @ZenPolicy.PeopleType int zpPeopleType, @Policy.PrioritySenders int defaultResult) {
76         switch (zpPeopleType) {
77             case ZenPolicy.PEOPLE_TYPE_ANYONE:
78                 return Policy.PRIORITY_SENDERS_ANY;
79             case ZenPolicy.PEOPLE_TYPE_CONTACTS:
80                 return Policy.PRIORITY_SENDERS_CONTACTS;
81             case ZenPolicy.PEOPLE_TYPE_STARRED:
82                 return Policy.PRIORITY_SENDERS_STARRED;
83             default:
84                 return defaultResult;
85         }
86     }
87 
88     /** Maps {@link Policy.PrioritySenders} enum to {@link ZenPolicy.PeopleType}. */
89     @ZenPolicy.PeopleType
prioritySendersToPeopleType( @olicy.PrioritySenders int npPrioritySenders)90     public static int prioritySendersToPeopleType(
91             @Policy.PrioritySenders int npPrioritySenders) {
92         switch (npPrioritySenders) {
93             case Policy.PRIORITY_SENDERS_ANY:
94                 return ZenPolicy.PEOPLE_TYPE_ANYONE;
95             case Policy.PRIORITY_SENDERS_CONTACTS:
96                 return ZenPolicy.PEOPLE_TYPE_CONTACTS;
97             case Policy.PRIORITY_SENDERS_STARRED:
98             default:
99                 return ZenPolicy.PEOPLE_TYPE_STARRED;
100         }
101     }
102 
103     /** Maps {@link ZenPolicy.ConversationSenders} enum to {@link Policy.ConversationSenders}. */
104     @Policy.ConversationSenders
zenPolicyConversationSendersToNotificationPolicy( @enPolicy.ConversationSenders int zpConversationSenders, @Policy.ConversationSenders int defaultResult)105     public static int zenPolicyConversationSendersToNotificationPolicy(
106             @ZenPolicy.ConversationSenders int zpConversationSenders,
107             @Policy.ConversationSenders int defaultResult) {
108         switch (zpConversationSenders) {
109             case ZenPolicy.CONVERSATION_SENDERS_ANYONE:
110                 return Policy.CONVERSATION_SENDERS_ANYONE;
111             case ZenPolicy.CONVERSATION_SENDERS_IMPORTANT:
112                 return Policy.CONVERSATION_SENDERS_IMPORTANT;
113             case ZenPolicy.CONVERSATION_SENDERS_NONE:
114                 return Policy.CONVERSATION_SENDERS_NONE;
115             default:
116                 return defaultResult;
117         }
118     }
119 
120     /** Maps {@link Policy.ConversationSenders} enum to {@link ZenPolicy.ConversationSenders}. */
121     @ZenPolicy.ConversationSenders
notificationPolicyConversationSendersToZenPolicy( @olicy.ConversationSenders int npPriorityConversationSenders)122     private static int notificationPolicyConversationSendersToZenPolicy(
123             @Policy.ConversationSenders int npPriorityConversationSenders) {
124         switch (npPriorityConversationSenders) {
125             case Policy.CONVERSATION_SENDERS_ANYONE:
126                 return ZenPolicy.CONVERSATION_SENDERS_ANYONE;
127             case Policy.CONVERSATION_SENDERS_IMPORTANT:
128                 return ZenPolicy.CONVERSATION_SENDERS_IMPORTANT;
129             case Policy.CONVERSATION_SENDERS_NONE:
130                 return ZenPolicy.CONVERSATION_SENDERS_NONE;
131             default: // including Policy.CONVERSATION_SENDERS_UNSET
132                 return ZenPolicy.CONVERSATION_SENDERS_UNSET;
133         }
134     }
135 }
136