1 /* 2 * Copyright (C) 2019 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 com.android.car.notification; 18 19 import android.view.View; 20 21 import com.android.car.notification.template.BasicNotificationViewHolder; 22 import com.android.car.notification.template.CallNotificationViewHolder; 23 import com.android.car.notification.template.CarNotificationBaseViewHolder; 24 import com.android.car.notification.template.EmergencyNotificationViewHolder; 25 import com.android.car.notification.template.GroupNotificationViewHolder; 26 import com.android.car.notification.template.GroupSummaryNotificationViewHolder; 27 import com.android.car.notification.template.InboxNotificationViewHolder; 28 import com.android.car.notification.template.MessageNotificationViewHolder; 29 import com.android.car.notification.template.NavigationNotificationViewHolder; 30 import com.android.car.notification.template.ProgressNotificationViewHolder; 31 32 import java.util.HashMap; 33 import java.util.Map; 34 35 /** 36 * Enum for storing the templates and view type for a particular notification. 37 */ 38 public enum CarNotificationTypeItem { 39 EMERGENCY(R.layout.car_emergency_headsup_notification_template, 40 R.layout.car_emergency_notification_template, NotificationViewType.CAR_EMERGENCY, 41 false), 42 NAVIGATION(R.layout.navigation_headsup_notification_template, 43 -1, NotificationViewType.NAVIGATION, false), 44 CALL(R.layout.call_headsup_notification_template, 45 R.layout.call_notification_template, NotificationViewType.CALL, false), 46 WARNING(R.layout.car_warning_headsup_notification_template, 47 R.layout.car_warning_notification_template, NotificationViewType.CAR_WARNING, false), 48 INFORMATION(R.layout.car_information_headsup_notification_template, 49 R.layout.car_information_notification_template, NotificationViewType.CAR_INFORMATION, 50 false), 51 INFORMATION_IN_GROUP(-1, R.layout.car_information_notification_template_inner, 52 NotificationViewType.CAR_INFORMATION_IN_GROUP, true), 53 MESSAGE(R.layout.message_headsup_notification_template, 54 R.layout.message_notification_template, NotificationViewType.MESSAGE, false), 55 MESSAGE_IN_GROUP(-1, R.layout.message_notification_template_inner, 56 NotificationViewType.MESSAGE_IN_GROUP, true), 57 INBOX(R.layout.inbox_headsup_notification_template, 58 R.layout.inbox_notification_template, NotificationViewType.INBOX, false), 59 INBOX_IN_GROUP(-1, R.layout.inbox_notification_template_inner, 60 NotificationViewType.INBOX_IN_GROUP, true), 61 PROGRESS(-1, R.layout.progress_notification_template, NotificationViewType.PROGRESS, false), 62 PROGRESS_IN_GROUP(-1, R.layout.progress_notification_template_inner, 63 NotificationViewType.PROGRESS_IN_GROUP, true), 64 BASIC(R.layout.basic_headsup_notification_template, 65 R.layout.basic_notification_template, NotificationViewType.BASIC, false), 66 BASIC_IN_GROUP(-1, R.layout.basic_notification_template_inner, 67 NotificationViewType.BASIC_IN_GROUP, true), 68 GROUP_EXPANDED(-1, R.layout.group_notification_template, NotificationViewType.GROUP_EXPANDED, 69 false), 70 GROUP_COLLAPSED(-1, R.layout.group_notification_template, NotificationViewType.GROUP_COLLAPSED, 71 false), 72 GROUP_SUMMARY(-1, R.layout.group_summary_notification_template, 73 NotificationViewType.GROUP_SUMMARY, false); 74 75 private final int mHeadsUpTemplate; 76 private final int mNotificationCenterTemplate; 77 private final int mNotificationType; 78 private final boolean mIsInGroup; 79 80 private static final Map<Integer, CarNotificationTypeItem> 81 VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP = new HashMap<>( 82 values().length, /* loadFactor= */ 1); 83 84 static { 85 for (CarNotificationTypeItem carNotificationTypeItem : values()) { 86 VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP.put( carNotificationTypeItem.getNotificationType()87 carNotificationTypeItem.getNotificationType(), carNotificationTypeItem); 88 } 89 } 90 CarNotificationTypeItem(int headsUpTemplate, int notificationCenterTemplate, int notificationType, boolean isInGroup)91 CarNotificationTypeItem(int headsUpTemplate, int notificationCenterTemplate, 92 int notificationType, boolean isInGroup) { 93 mHeadsUpTemplate = headsUpTemplate; 94 mNotificationCenterTemplate = notificationCenterTemplate; 95 mNotificationType = notificationType; 96 mIsInGroup = isInGroup; 97 } 98 99 /** 100 * Return the heads up notification template id defined in enum. If no template is defined this 101 * will return -1. 102 */ getHeadsUpTemplate()103 public int getHeadsUpTemplate() { 104 return mHeadsUpTemplate; 105 } 106 107 /** 108 * Return the notification center template id defined in enum. If no template is defined this 109 * will return -1. 110 */ getNotificationCenterTemplate()111 public int getNotificationCenterTemplate() { 112 return mNotificationCenterTemplate; 113 } 114 115 /** 116 * Returns the notification type defined for this enum. 117 */ getNotificationType()118 public int getNotificationType() { 119 return mNotificationType; 120 } 121 122 /** 123 * Returns the {@link CarNotificationBaseViewHolder} based on the view type defined in enum. 124 */ getViewHolder(View view, NotificationClickHandlerFactory clickHandlerFactory)125 public CarNotificationBaseViewHolder getViewHolder(View view, 126 NotificationClickHandlerFactory clickHandlerFactory) { 127 switch (mNotificationType) { 128 case NotificationViewType.CAR_EMERGENCY: 129 return new EmergencyNotificationViewHolder(view, clickHandlerFactory); 130 case NotificationViewType.NAVIGATION: 131 return new NavigationNotificationViewHolder(view, clickHandlerFactory); 132 case NotificationViewType.CALL: 133 return new CallNotificationViewHolder(view, clickHandlerFactory); 134 case NotificationViewType.MESSAGE: 135 case NotificationViewType.MESSAGE_IN_GROUP: 136 return new MessageNotificationViewHolder(view, clickHandlerFactory); 137 case NotificationViewType.INBOX: 138 case NotificationViewType.INBOX_IN_GROUP: 139 return new InboxNotificationViewHolder(view, clickHandlerFactory); 140 case NotificationViewType.GROUP_EXPANDED: 141 case NotificationViewType.GROUP_COLLAPSED: 142 return new GroupNotificationViewHolder(view, clickHandlerFactory); 143 case NotificationViewType.GROUP_SUMMARY: 144 return new GroupSummaryNotificationViewHolder(view, clickHandlerFactory); 145 case NotificationViewType.PROGRESS: 146 case NotificationViewType.PROGRESS_IN_GROUP: 147 return new ProgressNotificationViewHolder(view, clickHandlerFactory); 148 case NotificationViewType.BASIC: 149 case NotificationViewType.BASIC_IN_GROUP: 150 case NotificationViewType.CAR_WARNING: 151 case NotificationViewType.CAR_INFORMATION: 152 case NotificationViewType.CAR_INFORMATION_IN_GROUP: 153 return new BasicNotificationViewHolder(view, clickHandlerFactory); 154 default: 155 throw new AssertionError("invalid notification type" + mNotificationType); 156 } 157 } 158 159 /** 160 * Binds a {@link AlertEntry} to a notification template. 161 */ bind(AlertEntry alertEntry, boolean isHeadsUp, CarNotificationBaseViewHolder holder)162 public void bind(AlertEntry alertEntry, boolean isHeadsUp, 163 CarNotificationBaseViewHolder holder) { 164 holder.bind(alertEntry, mIsInGroup, isHeadsUp); 165 } 166 167 /** 168 * Returns the enum object based on the view type defined in the enum. 169 */ of(int viewType)170 public static CarNotificationTypeItem of(int viewType) { 171 CarNotificationTypeItem result = VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP.get(viewType); 172 if (result == null) { 173 throw new IllegalArgumentException("Invalid view type id: " + viewType); 174 } 175 return result; 176 } 177 } 178