summaryrefslogtreecommitdiff
path: root/src/com/android/calendar/alerts/AlertService.kt
blob: bc1b4e049a95c10c6be0d13c5586cbed1decfb16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.calendar.alerts

import android.app.Notification
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.os.Looper
import android.provider.CalendarContract.CalendarAlerts
import java.util.ArrayList

/**
 * This service is used to handle calendar event reminders.
 */
class AlertService : Service() {
    @Volatile
    private var mServiceLooper: Looper? = null

    // Added wrapper for testing
    class NotificationWrapper {
        var mNotification: Notification
        var mEventId: Long = 0
        var mBegin: Long = 0
        var mEnd: Long = 0
        var mNw: ArrayList<NotificationWrapper>? = null

        constructor(
            n: Notification,
            notificationId: Int,
            eventId: Long,
            startMillis: Long,
            endMillis: Long,
            doPopup: Boolean
        ) {
            mNotification = n
            mEventId = eventId
            mBegin = startMillis
            mEnd = endMillis

            // popup?
            // notification id?
        }

        constructor(n: Notification) {
            mNotification = n
        }

        fun add(nw: NotificationWrapper?) {
            val temp = mNw
            if (temp == null) {
                mNw = ArrayList<NotificationWrapper>()
            }
            mNw?.add(nw as AlertService.NotificationWrapper)
        }
    }

    // Added wrapper for testing
    class NotificationMgrWrapper(nm: NotificationManager) : NotificationMgr() {
        var mNm: NotificationManager
        @Override
        override fun cancel(id: Int) {
            mNm.cancel(id)
        }

        @Override
        override fun notify(id: Int, nw: NotificationWrapper?) {
            mNm.notify(id, nw?.mNotification)
        }

        init {
            mNm = nm
        }
    }

    internal class NotificationInfo(
        var eventName: String,
        var location: String,
        var description: String,
        var startMillis: Long,
        var endMillis: Long,
        var eventId: Long,
        var allDay: Boolean,
        var newAlert: Boolean
    )

    @Override
    override fun onCreate() {
    }

    @Override
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return START_REDELIVER_INTENT
    }

    @Override
    override fun onDestroy() {
        mServiceLooper?.quit()
    }

    @Override
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    companion object {
        const val DEBUG = true
        private const val TAG = "AlertService"
        val ALERT_PROJECTION = arrayOf<String>(
            CalendarAlerts._ID, // 0
            CalendarAlerts.EVENT_ID, // 1
            CalendarAlerts.STATE, // 2
            CalendarAlerts.TITLE, // 3
            CalendarAlerts.EVENT_LOCATION, // 4
            CalendarAlerts.SELF_ATTENDEE_STATUS, // 5
            CalendarAlerts.ALL_DAY, // 6
            CalendarAlerts.ALARM_TIME, // 7
            CalendarAlerts.MINUTES, // 8
            CalendarAlerts.BEGIN, // 9
            CalendarAlerts.END, // 10
            CalendarAlerts.DESCRIPTION
        )
        private const val ALERT_INDEX_ID = 0
        private const val ALERT_INDEX_EVENT_ID = 1
        private const val ALERT_INDEX_STATE = 2
        private const val ALERT_INDEX_TITLE = 3
        private const val ALERT_INDEX_EVENT_LOCATION = 4
        private const val ALERT_INDEX_SELF_ATTENDEE_STATUS = 5
        private const val ALERT_INDEX_ALL_DAY = 6
        private const val ALERT_INDEX_ALARM_TIME = 7
        private const val ALERT_INDEX_MINUTES = 8
        private const val ALERT_INDEX_BEGIN = 9
        private const val ALERT_INDEX_END = 10
        private const val ALERT_INDEX_DESCRIPTION = 11
        private val ACTIVE_ALERTS_SELECTION = ("(" + CalendarAlerts.STATE.toString() + "=? OR " +
            CalendarAlerts.STATE.toString() + "=?) AND " +
            CalendarAlerts.ALARM_TIME.toString() + "<=")
        private val ACTIVE_ALERTS_SELECTION_ARGS = arrayOf<String>(
            Integer.toString(CalendarAlerts.STATE_FIRED),
            Integer.toString(CalendarAlerts.STATE_SCHEDULED)
        )
        private const val ACTIVE_ALERTS_SORT = "begin DESC, end DESC"
        private val DISMISS_OLD_SELECTION: String = (CalendarAlerts.END.toString() + "<? AND " +
            CalendarAlerts.STATE + "=?")
        private const val MINUTE_MS = 60 * 1000

        // The grace period before changing a notification's priority bucket.
        private const val MIN_DEPRIORITIZE_GRACE_PERIOD_MS = 15 * MINUTE_MS

        // Hard limit to the number of notifications displayed.
        const val MAX_NOTIFICATIONS = 20
    }
}