1 /* 2 * Copyright (C) 2021 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 package com.android.calendar.alerts 17 18 import android.app.Notification 19 import android.content.BroadcastReceiver 20 import android.content.Context 21 import android.content.Intent 22 import android.content.res.Resources 23 import android.util.Log 24 import com.android.calendar.R 25 import com.android.calendar.Utils 26 import com.android.calendar.alerts.AlertService.NotificationWrapper 27 28 /** 29 * Receives android.intent.action.EVENT_REMINDER intents and handles 30 * event reminders. The intent URI specifies an alert id in the 31 * CalendarAlerts database table. This class also receives the 32 * BOOT_COMPLETED intent so that it can add a status bar notification 33 * if there are Calendar event alarms that have not been dismissed. 34 * It also receives the TIME_CHANGED action so that it can fire off 35 * snoozed alarms that have become ready. The real work is done in 36 * the AlertService class. 37 * 38 * To trigger this code after pushing the apk to device: 39 * adb shell am broadcast -a "android.intent.action.EVENT_REMINDER" 40 * -n "com.android.calendar/.alerts.AlertReceiver" 41 */ 42 class AlertReceiver : BroadcastReceiver() { 43 @Override onReceivenull44 override fun onReceive(context: Context, intent: Intent) { 45 if (AlertService.DEBUG) { 46 Log.d(TAG, "onReceive: a=" + intent.getAction().toString() + " " + intent.toString()) 47 } 48 } 49 50 companion object { 51 private const val TAG = "AlertReceiver" 52 53 // The broadcast for notification refreshes scheduled by the app. This is to 54 // distinguish the EVENT_REMINDER broadcast sent by the provider. 55 const val EVENT_REMINDER_APP_ACTION = "com.android.calendar.EVENT_REMINDER_APP" 56 const val ACTION_DISMISS_OLD_REMINDERS = "removeOldReminders" makeBasicNotificationnull57 fun makeBasicNotification( 58 context: Context, 59 title: String, 60 summaryText: String, 61 startMillis: Long, 62 endMillis: Long, 63 eventId: Long, 64 notificationId: Int, 65 doPopup: Boolean, 66 priority: Int 67 ): NotificationWrapper { 68 val n: Notification = buildBasicNotification( 69 Notification.Builder(context), 70 context, 71 title, 72 summaryText, 73 startMillis, 74 endMillis, 75 eventId, 76 notificationId, 77 doPopup, 78 priority, false 79 ) 80 return NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup) 81 } 82 buildBasicNotificationnull83 private fun buildBasicNotification( 84 notificationBuilder: Notification.Builder, 85 context: Context, 86 title: String, 87 summaryText: String, 88 startMillis: Long, 89 endMillis: Long, 90 eventId: Long, 91 notificationId: Int, 92 doPopup: Boolean, 93 priority: Int, 94 addActionButtons: Boolean 95 ): Notification { 96 var title: String? = title 97 val resources: Resources = context.getResources() 98 if (title == null || title.length == 0) { 99 title = resources.getString(R.string.no_title_label) 100 } 101 102 // Create the base notification. 103 notificationBuilder.setContentTitle(title) 104 notificationBuilder.setContentText(summaryText) 105 notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar) 106 if (Utils.isJellybeanOrLater()) { 107 // Turn off timestamp. 108 notificationBuilder.setWhen(0) 109 110 // Should be one of the values in Notification 111 // (ie. Notification.PRIORITY_HIGH, etc). 112 // A higher priority will encourage notification manager to expand it. 113 notificationBuilder.setPriority(priority) 114 } 115 return notificationBuilder.getNotification() 116 } 117 } 118 }