1 /* 2 * Copyright (C) 2024 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.google.android.car.kitchensink.radio; 18 19 import android.app.Notification; 20 import android.app.PendingIntent; 21 import android.content.Context; 22 import android.content.Intent; 23 24 import androidx.core.app.NotificationCompat; 25 import androidx.core.app.NotificationCompat.Action; 26 import androidx.core.app.NotificationManagerCompat; 27 28 import com.google.android.car.kitchensink.R; 29 30 final class AlertNotificationHelper { 31 AlertNotificationHelper()32 private AlertNotificationHelper() { 33 throw new UnsupportedOperationException("AlertNotificationHelper class is noninstantiable"); 34 } 35 36 static final int INVALID_NOTIFICATION_ID = -1; 37 static final String TEXT_DISMISS = "Dismiss"; 38 static final String TEXT_SNOOZE = "Snooze"; 39 static final String ACTION_DISMISS = "DISMISS"; 40 static final String ACTION_SNOOZE = "SNOOZE"; 41 static final String ACTION_NOTIFICATION = "NOTIFICATION"; 42 static final String EXTRA_KEY_NOTIFICATION_ID = "EXTRA_NOTIFICATION_ID"; 43 static final String EXTRA_KEY_TITLE = "TITLE_TEXT"; 44 static final String EXTRA_KEY_TEXT = "EXTRA_TEXT"; 45 static final String EXTRA_KEY_ALERT_TIME_MS = "EXTRA_ALERT_TIME_MS"; 46 47 static final String IMPORTANCE_ALERT_ID = "importance_high"; 48 createRadioAlertNotification(Context activityContext, String title, String text, long alertTimeMs, int notificationId)49 static void createRadioAlertNotification(Context activityContext, String title, String text, 50 long alertTimeMs, int notificationId) { 51 NotificationManagerCompat notificationManager = 52 NotificationManagerCompat.from(activityContext); 53 54 PendingIntent dismissPendingIntent = createPendingIntent(activityContext, ACTION_DISMISS, 55 notificationId, title, text, alertTimeMs); 56 PendingIntent snoozePendingIntent = createPendingIntent(activityContext, ACTION_SNOOZE, 57 notificationId, title, text, alertTimeMs); 58 59 NotificationCompat.Builder builder = new NotificationCompat 60 .Builder(activityContext, IMPORTANCE_ALERT_ID) 61 .setContentTitle(title) 62 .setContentText(text) 63 .setShowWhen(true) 64 .setWhen(alertTimeMs) 65 .setCategory(Notification.CATEGORY_MESSAGE) 66 .setSmallIcon(R.drawable.ic_warning) 67 .setColor(activityContext.getColor(android.R.color.holo_green_light)) 68 .addAction(new Action.Builder(/* icon= */ null, TEXT_SNOOZE, snoozePendingIntent) 69 .setShowsUserInterface(false).build()) 70 .addAction(new Action.Builder(/* icon= */ null, TEXT_DISMISS, dismissPendingIntent) 71 .setShowsUserInterface(false).build()); 72 73 notificationManager.notify(notificationId, builder.build()); 74 } 75 createPendingIntent(Context activityContext, String action, int notificationId, String title, String text, long alertTimeMs)76 private static PendingIntent createPendingIntent(Context activityContext, String action, 77 int notificationId, String title, String text, long alertTimeMs) { 78 Intent intent = new Intent(activityContext, AlertNotificationReceiver.class); 79 intent.setAction(action); 80 intent.putExtra(EXTRA_KEY_NOTIFICATION_ID, notificationId); 81 intent.putExtra(EXTRA_KEY_TITLE, title); 82 intent.putExtra(EXTRA_KEY_TEXT, text); 83 intent.putExtra(EXTRA_KEY_ALERT_TIME_MS, alertTimeMs); 84 return PendingIntent.getBroadcast(activityContext, notificationId, intent, 85 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); 86 } 87 } 88