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.AlarmManager
19 import android.app.PendingIntent
20 import android.content.Context
21 import android.content.Intent
22 import android.net.Uri
23 import android.provider.CalendarContract
24 import com.android.calendar.EventInfoActivity
25 import com.android.calendar.Utils
26 
27 object AlertUtils {
28     private const val TAG = "AlertUtils"
29     const val DEBUG = true
30     const val SNOOZE_DELAY = 5 * 60 * 1000L
31 
32     // We use one notification id for the expired events notification.  All
33     // other notifications (the 'active' future/concurrent ones) use a unique ID.
34     const val EXPIRED_GROUP_NOTIFICATION_ID = 0
35     const val EVENT_ID_KEY = "eventid"
36     const val EVENT_START_KEY = "eventstart"
37     const val EVENT_END_KEY = "eventend"
38     const val NOTIFICATION_ID_KEY = "notificationid"
39     const val EVENT_IDS_KEY = "eventids"
40     const val EVENT_STARTS_KEY = "starts"
41 
42     // A flag for using local storage to save alert state instead of the alerts DB table.
43     // This allows the unbundled app to run alongside other calendar apps without eating
44     // alerts from other apps.
45     var BYPASS_DB = true
46 
47     /**
48      * Creates an AlarmManagerInterface that wraps a real AlarmManager.  The alarm code
49      * was abstracted to an interface to make it testable.
50      */
createAlarmManagernull51     @JvmStatic fun createAlarmManager(context: Context): AlarmManagerInterface {
52         val mgr: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
53         return object : AlarmManagerInterface {
54             override operator fun set(type: Int, triggerAtMillis: Long, operation: PendingIntent?) {
55                 if (com.android.calendar.Utils.isKeyLimePieOrLater()) {
56                     mgr.setExact(type, triggerAtMillis, operation!!)
57                 } else {
58                     mgr.set(type, triggerAtMillis, operation!!)
59                 }
60             }
61         }
62     }
63 
64     /**
65      * Schedules an alarm intent with the system AlarmManager that will notify
66      * listeners when a reminder should be fired. The provider will keep
67      * scheduled reminders up to date but apps may use this to implement snooze
68      * functionality without modifying the reminders table. Scheduled alarms
69      * will generate an intent using AlertReceiver.EVENT_REMINDER_APP_ACTION.
70      *
71      * @param context A context for referencing system resources
72      * @param manager The AlarmManager to use or null
73      * @param alarmTime The time to fire the intent in UTC millis since epoch
74      */
scheduleAlarmnull75     @JvmStatic fun scheduleAlarm(
76         context: Context?,
77         manager: AlarmManagerInterface?,
78         alarmTime: Long
79     ) {
80     }
81 
buildEventViewIntentnull82     @JvmStatic fun buildEventViewIntent(c: Context, eventId: Long, begin: Long, end: Long): Intent {
83         val i = Intent(Intent.ACTION_VIEW)
84         val builder: Uri.Builder = CalendarContract.CONTENT_URI.buildUpon()
85         builder.appendEncodedPath("events/$eventId")
86         i.setData(builder.build())
87         i.setClass(c, EventInfoActivity::class.java)
88         i.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
89         i.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end)
90         return i
91     }
92 }
93