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.NotificationManager.Policy; 21 22 /** 23 * Converters between different Zen representations. 24 * @hide 25 */ 26 public class ZenAdapters { 27 28 /** Maps {@link Policy} to {@link ZenPolicy}. */ 29 @NonNull notificationPolicyToZenPolicy(@onNull Policy policy)30 public static ZenPolicy notificationPolicyToZenPolicy(@NonNull Policy policy) { 31 ZenPolicy.Builder zenPolicyBuilder = new ZenPolicy.Builder() 32 .allowAlarms(policy.allowAlarms()) 33 .allowCalls( 34 policy.allowCalls() 35 ? prioritySendersToPeopleType( 36 policy.allowCallsFrom()) 37 : ZenPolicy.PEOPLE_TYPE_NONE) 38 .allowConversations( 39 policy.allowConversations() 40 ? notificationPolicyConversationSendersToZenPolicy( 41 policy.allowConversationsFrom()) 42 : ZenPolicy.CONVERSATION_SENDERS_NONE) 43 .allowEvents(policy.allowEvents()) 44 .allowMedia(policy.allowMedia()) 45 .allowMessages( 46 policy.allowMessages() 47 ? prioritySendersToPeopleType( 48 policy.allowMessagesFrom()) 49 : ZenPolicy.PEOPLE_TYPE_NONE) 50 .allowReminders(policy.allowReminders()) 51 .allowRepeatCallers(policy.allowRepeatCallers()) 52 .allowSystem(policy.allowSystem()) 53 .allowPriorityChannels(policy.allowPriorityChannels()); 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 return zenPolicyBuilder.build(); 66 } 67 68 /** Maps {@link ZenPolicy.PeopleType} enum to {@link Policy.PrioritySenders}. */ 69 @Policy.PrioritySenders peopleTypeToPrioritySenders( @enPolicy.PeopleType int zpPeopleType, @Policy.PrioritySenders int defaultResult)70 public static int peopleTypeToPrioritySenders( 71 @ZenPolicy.PeopleType int zpPeopleType, @Policy.PrioritySenders int defaultResult) { 72 switch (zpPeopleType) { 73 case ZenPolicy.PEOPLE_TYPE_ANYONE: 74 return Policy.PRIORITY_SENDERS_ANY; 75 case ZenPolicy.PEOPLE_TYPE_CONTACTS: 76 return Policy.PRIORITY_SENDERS_CONTACTS; 77 case ZenPolicy.PEOPLE_TYPE_STARRED: 78 return Policy.PRIORITY_SENDERS_STARRED; 79 default: 80 return defaultResult; 81 } 82 } 83 84 /** Maps {@link Policy.PrioritySenders} enum to {@link ZenPolicy.PeopleType}. */ 85 @ZenPolicy.PeopleType prioritySendersToPeopleType( @olicy.PrioritySenders int npPrioritySenders)86 public static int prioritySendersToPeopleType( 87 @Policy.PrioritySenders int npPrioritySenders) { 88 switch (npPrioritySenders) { 89 case Policy.PRIORITY_SENDERS_ANY: 90 return ZenPolicy.PEOPLE_TYPE_ANYONE; 91 case Policy.PRIORITY_SENDERS_CONTACTS: 92 return ZenPolicy.PEOPLE_TYPE_CONTACTS; 93 case Policy.PRIORITY_SENDERS_STARRED: 94 default: 95 return ZenPolicy.PEOPLE_TYPE_STARRED; 96 } 97 } 98 99 /** Maps {@link ZenPolicy.ConversationSenders} enum to {@link Policy.ConversationSenders}. */ 100 @Policy.ConversationSenders zenPolicyConversationSendersToNotificationPolicy( @enPolicy.ConversationSenders int zpConversationSenders, @Policy.ConversationSenders int defaultResult)101 public static int zenPolicyConversationSendersToNotificationPolicy( 102 @ZenPolicy.ConversationSenders int zpConversationSenders, 103 @Policy.ConversationSenders int defaultResult) { 104 switch (zpConversationSenders) { 105 case ZenPolicy.CONVERSATION_SENDERS_ANYONE: 106 return Policy.CONVERSATION_SENDERS_ANYONE; 107 case ZenPolicy.CONVERSATION_SENDERS_IMPORTANT: 108 return Policy.CONVERSATION_SENDERS_IMPORTANT; 109 case ZenPolicy.CONVERSATION_SENDERS_NONE: 110 return Policy.CONVERSATION_SENDERS_NONE; 111 default: 112 return defaultResult; 113 } 114 } 115 116 /** Maps {@link Policy.ConversationSenders} enum to {@link ZenPolicy.ConversationSenders}. */ 117 @ZenPolicy.ConversationSenders notificationPolicyConversationSendersToZenPolicy( @olicy.ConversationSenders int npPriorityConversationSenders)118 private static int notificationPolicyConversationSendersToZenPolicy( 119 @Policy.ConversationSenders int npPriorityConversationSenders) { 120 switch (npPriorityConversationSenders) { 121 case Policy.CONVERSATION_SENDERS_ANYONE: 122 return ZenPolicy.CONVERSATION_SENDERS_ANYONE; 123 case Policy.CONVERSATION_SENDERS_IMPORTANT: 124 return ZenPolicy.CONVERSATION_SENDERS_IMPORTANT; 125 case Policy.CONVERSATION_SENDERS_NONE: 126 return ZenPolicy.CONVERSATION_SENDERS_NONE; 127 default: // including Policy.CONVERSATION_SENDERS_UNSET 128 return ZenPolicy.CONVERSATION_SENDERS_UNSET; 129 } 130 } 131 } 132