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(-1, R.layout.group_notification_template, NotificationViewType.GROUP, 69 false), 70 GROUP_SUMMARY(-1, R.layout.group_summary_notification_template, 71 NotificationViewType.GROUP_SUMMARY, false); 72 73 private final int mHeadsUpTemplate; 74 private final int mNotificationCenterTemplate; 75 private final int mNotificationType; 76 private final boolean mIsInGroup; 77 78 private static final Map<Integer, CarNotificationTypeItem> 79 VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP = new HashMap<>( 80 values().length, /* loadFactor= */ 1); 81 82 static { 83 for (CarNotificationTypeItem carNotificationTypeItem : values()) { 84 VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP.put( carNotificationTypeItem.getNotificationType()85 carNotificationTypeItem.getNotificationType(), carNotificationTypeItem); 86 } 87 } 88 CarNotificationTypeItem(int headsUpTemplate, int notificationCenterTemplate, int notificationType, boolean isInGroup)89 CarNotificationTypeItem(int headsUpTemplate, int notificationCenterTemplate, 90 int notificationType, boolean isInGroup) { 91 mHeadsUpTemplate = headsUpTemplate; 92 mNotificationCenterTemplate = notificationCenterTemplate; 93 mNotificationType = notificationType; 94 mIsInGroup = isInGroup; 95 } 96 97 /** 98 * Return the heads up notification template id defined in enum. If no template is defined this 99 * will return -1. 100 */ getHeadsUpTemplate()101 public int getHeadsUpTemplate() { 102 return mHeadsUpTemplate; 103 } 104 105 /** 106 * Return the notification center template id defined in enum. If no template is defined this 107 * will return -1. 108 */ getNotificationCenterTemplate()109 public int getNotificationCenterTemplate() { 110 return mNotificationCenterTemplate; 111 } 112 113 /** 114 * Returns the notification type defined for this enum. 115 */ getNotificationType()116 public int getNotificationType() { 117 return mNotificationType; 118 } 119 120 /** 121 * Returns the {@link CarNotificationBaseViewHolder} based on the view type defined in enum. 122 */ getViewHolder(View view, NotificationClickHandlerFactory clickHandlerFactory)123 public CarNotificationBaseViewHolder getViewHolder(View view, 124 NotificationClickHandlerFactory clickHandlerFactory) { 125 switch (mNotificationType) { 126 case NotificationViewType.CAR_EMERGENCY: 127 return new EmergencyNotificationViewHolder(view, clickHandlerFactory); 128 case NotificationViewType.NAVIGATION: 129 return new NavigationNotificationViewHolder(view, clickHandlerFactory); 130 case NotificationViewType.CALL: 131 return new CallNotificationViewHolder(view, clickHandlerFactory); 132 case NotificationViewType.MESSAGE: 133 case NotificationViewType.MESSAGE_IN_GROUP: 134 return new MessageNotificationViewHolder(view, clickHandlerFactory); 135 case NotificationViewType.INBOX: 136 case NotificationViewType.INBOX_IN_GROUP: 137 return new InboxNotificationViewHolder(view, clickHandlerFactory); 138 case NotificationViewType.GROUP: 139 return new GroupNotificationViewHolder(view, clickHandlerFactory); 140 case NotificationViewType.GROUP_SUMMARY: 141 return new GroupSummaryNotificationViewHolder(view, clickHandlerFactory); 142 case NotificationViewType.PROGRESS: 143 case NotificationViewType.PROGRESS_IN_GROUP: 144 return new ProgressNotificationViewHolder(view, clickHandlerFactory); 145 case NotificationViewType.BASIC: 146 case NotificationViewType.BASIC_IN_GROUP: 147 case NotificationViewType.CAR_WARNING: 148 case NotificationViewType.CAR_INFORMATION: 149 case NotificationViewType.CAR_INFORMATION_IN_GROUP: 150 return new BasicNotificationViewHolder(view, clickHandlerFactory); 151 default: 152 throw new AssertionError("invalid notification type" + mNotificationType); 153 } 154 } 155 156 /** 157 * Binds a {@link AlertEntry} to a notification template. 158 */ bind(AlertEntry alertEntry, boolean isHeadsUp, CarNotificationBaseViewHolder holder, boolean isSeen)159 public void bind(AlertEntry alertEntry, boolean isHeadsUp, 160 CarNotificationBaseViewHolder holder, boolean isSeen) { 161 holder.bind(alertEntry, mIsInGroup, isHeadsUp, isSeen); 162 } 163 164 /** 165 * Returns the enum object based on the view type defined in the enum. 166 */ of(int viewType)167 public static CarNotificationTypeItem of(int viewType) { 168 CarNotificationTypeItem result = VIEW_TYPE_CAR_NOTIFICATION_TYPE_ITEM_MAP.get(viewType); 169 if (result == null) { 170 throw new IllegalArgumentException("Invalid view type id: " + viewType); 171 } 172 return result; 173 } 174 } 175