1 /* 2 * Copyright (C) 2020 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.admin; 18 19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 20 21 import android.annotation.NonNull; 22 import android.app.Notification; 23 import android.app.NotificationChannel; 24 import android.app.NotificationManager; 25 import android.content.Context; 26 import android.os.Bundle; 27 28 import com.android.car.R; 29 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 30 import com.android.internal.annotations.VisibleForTesting; 31 32 import java.util.Objects; 33 34 // TODO: move this class to CarSettings or at least to some common package (not admin) 35 36 /** 37 * Helper for notification-related tasks 38 */ 39 public final class NotificationHelper { 40 41 static final int FACTORY_RESET_NOTIFICATION_ID = 42; 42 43 static final int NEW_USER_DISCLAIMER_NOTIFICATION_ID = 108; 44 45 /* 46 * NOTE: IDs in the range {@code [RESOURCE_OVERUSE_NOTIFICATION_BASE_ID, 47 * RESOURCE_OVERUSE_NOTIFICATION_BASE_ID + RESOURCE_OVERUSE_NOTIFICATION_MAX_OFFSET)} are 48 * reserved for car watchdog's resource overuse notifications. 49 */ 50 /** Base notification id for car watchdog's resource overuse notifications. */ 51 public static final int RESOURCE_OVERUSE_NOTIFICATION_BASE_ID = 150; 52 53 /** Maximum notification offset for car watchdog's resource overuse notifications. */ 54 public static final int RESOURCE_OVERUSE_NOTIFICATION_MAX_OFFSET = 20; 55 56 @VisibleForTesting 57 static final String CHANNEL_ID_DEFAULT = "channel_id_default"; 58 @VisibleForTesting 59 static final String CHANNEL_ID_HIGH = "channel_id_high"; 60 61 /** 62 * Creates a notification (and its notification channel) for the given importance type, setting 63 * its name to be {@code Android System}. 64 * 65 * @param context context for showing the notification 66 * @param importance notification importance. Currently only 67 * {@link NotificationManager.IMPORTANCE_HIGH} is supported. 68 */ 69 @NonNull newNotificationBuilder(Context context, @NotificationManager.Importance int importance)70 public static Notification.Builder newNotificationBuilder(Context context, 71 @NotificationManager.Importance int importance) { 72 Objects.requireNonNull(context, "context cannot be null"); 73 74 String channelId, importanceName; 75 switch (importance) { 76 case NotificationManager.IMPORTANCE_DEFAULT: 77 channelId = CHANNEL_ID_DEFAULT; 78 importanceName = context.getString(R.string.importance_default); 79 break; 80 case NotificationManager.IMPORTANCE_HIGH: 81 channelId = CHANNEL_ID_HIGH; 82 importanceName = context.getString(R.string.importance_high); 83 break; 84 default: 85 throw new IllegalArgumentException("Unsupported importance: " + importance); 86 } 87 NotificationManager notificationMgr = context.getSystemService(NotificationManager.class); 88 notificationMgr.createNotificationChannel( 89 new NotificationChannel(channelId, importanceName, importance)); 90 91 Bundle extras = new Bundle(); 92 extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, 93 context.getString(com.android.internal.R.string.android_system_label)); 94 95 return new Notification.Builder(context, channelId).addExtras(extras); 96 } 97 98 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE, 99 details = "private constructor") NotificationHelper()100 private NotificationHelper() { 101 throw new UnsupportedOperationException("Contains only static methods"); 102 } 103 } 104