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.AlarmManager; 20 import android.app.PendingIntent; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.util.Log; 25 26 import androidx.core.app.NotificationManagerCompat; 27 28 import java.util.concurrent.TimeUnit; 29 30 public final class AlertNotificationReceiver extends BroadcastReceiver { 31 32 private static final String TAG = AlertNotificationReceiver.class.getSimpleName(); 33 private static final long SNOOZE_TIME_MS = TimeUnit.MINUTES.toMillis(10); 34 35 @Override onReceive(Context context, Intent intent)36 public void onReceive(Context context, Intent intent) { 37 int notificationId = intent.getIntExtra(AlertNotificationHelper.EXTRA_KEY_NOTIFICATION_ID, 38 AlertNotificationHelper.INVALID_NOTIFICATION_ID); 39 if (notificationId == AlertNotificationHelper.INVALID_NOTIFICATION_ID) { 40 Log.e(TAG, "Invalid notification id"); 41 return; 42 } 43 String action = intent.getAction(); 44 Log.i(TAG, action + " for alert notification " + notificationId); 45 46 NotificationManagerCompat notificationManagerCompat = 47 NotificationManagerCompat.from(context); 48 if (AlertNotificationHelper.ACTION_DISMISS.equals(action)) { 49 notificationManagerCompat.cancel(notificationId); 50 } else if (AlertNotificationHelper.ACTION_SNOOZE.equals(action)) { 51 String title = intent.getStringExtra(AlertNotificationHelper.EXTRA_KEY_TITLE); 52 String text = intent.getStringExtra(AlertNotificationHelper.EXTRA_KEY_TEXT); 53 long alertTimeMs = intent.getLongExtra(AlertNotificationHelper.EXTRA_KEY_ALERT_TIME_MS, 54 System.currentTimeMillis()); 55 scheduleSnooze(context, title, text, alertTimeMs, notificationId); 56 } else { 57 Log.e(TAG, "Undefined action " + action); 58 } 59 } 60 scheduleSnooze(Context context, String title, String text, long alertTimeMs, int notificationId)61 private void scheduleSnooze(Context context, String title, String text, long alertTimeMs, 62 int notificationId) { 63 PendingIntent snoozePendingIntent = createSnoozePendingIntent(context, title, text, 64 alertTimeMs, notificationId); 65 AlarmManager alarmManager = context.getSystemService(AlarmManager.class); 66 if (alarmManager != null && alarmManager.canScheduleExactAlarms()) { 67 alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, 68 System.currentTimeMillis() + SNOOZE_TIME_MS, snoozePendingIntent); 69 } else { 70 Log.e(TAG, "Cannot schedule snooze"); 71 } 72 } 73 createSnoozePendingIntent(Context activityContext, String title, String text, long alertTimeMs, int notificationId)74 private static PendingIntent createSnoozePendingIntent(Context activityContext, String title, 75 String text, long alertTimeMs, int notificationId) { 76 Intent intent = new Intent(activityContext, AlertSnoozeReceiver.class); 77 intent.setAction(AlertNotificationHelper.ACTION_NOTIFICATION); 78 intent.putExtra(AlertNotificationHelper.EXTRA_KEY_NOTIFICATION_ID, notificationId); 79 intent.putExtra(AlertNotificationHelper.EXTRA_KEY_TITLE, title); 80 intent.putExtra(AlertNotificationHelper.EXTRA_KEY_TEXT, text); 81 intent.putExtra(AlertNotificationHelper.EXTRA_KEY_ALERT_TIME_MS, alertTimeMs); 82 return PendingIntent.getBroadcast(activityContext, notificationId, intent, 83 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); 84 } 85 } 86