summaryrefslogtreecommitdiff
path: root/src/com/android/calendar/alerts/InitAlarmsService.kt
blob: 0ac8a474f0f1b6e5fe06e9a08e7314da0c748a83 (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
/*
 * 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.IntentService
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import android.os.SystemClock
import android.provider.CalendarContract
import android.util.Log

/**
 * Service for clearing all scheduled alerts from the CalendarAlerts table and
 * rescheduling them.  This is expected to be called only on boot up, to restore
 * the AlarmManager alarms that were lost on device restart.
 */
class InitAlarmsService : IntentService("InitAlarmsService") {
    @Override
    protected override fun onHandleIntent(intent: Intent?) {
        // Delay to avoid race condition of in-progress alarm scheduling in provider.
        SystemClock.sleep(DELAY_MS)
        Log.d(TAG, "Clearing and rescheduling alarms.")
        try {
            getContentResolver().update(
                SCHEDULE_ALARM_REMOVE_URI, ContentValues(), null,
                null
            )
        } catch (e: java.lang.IllegalArgumentException) {
            // java.lang.IllegalArgumentException:
            //     Unknown URI content://com.android.calendar/schedule_alarms_remove

            // Until b/7742576 is resolved, just catch the exception so the app won't crash
            Log.e(TAG, "update failed: " + e.toString())
        }
    }

    companion object {
        private const val TAG = "InitAlarmsService"
        private const val SCHEDULE_ALARM_REMOVE_PATH = "schedule_alarms_remove"
        private val SCHEDULE_ALARM_REMOVE_URI: Uri = Uri.withAppendedPath(
            CalendarContract.CONTENT_URI, SCHEDULE_ALARM_REMOVE_PATH
        )

        // Delay for rescheduling the alarms must be great enough to minimize race
        // conditions with the provider's boot up actions.
        private const val DELAY_MS: Long = 30000
    }
}