1 /*
2  * Copyright (C) 2020 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.android.statementservice.domain
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import androidx.work.Constraints
23 import androidx.work.ExistingWorkPolicy
24 import androidx.work.NetworkType
25 import androidx.work.OneTimeWorkRequestBuilder
26 import androidx.work.WorkManager
27 import com.android.statementservice.domain.worker.RetryRequestWorker
28 
29 /**
30  * Handles [Intent.ACTION_BOOT_COMPLETED] to schedule recurring maintenance [WorkManager] tasks and
31  * run a one-time retry request to attempt to verify domains that may have failed or been added
32  * since last device reboot.
33  *
34  * Note that this requires the user to have unlocked the device, since [WorkManager] cannot handle
35  * the encrypted user data directories.
36  */
37 class BootCompletedReceiver : BroadcastReceiver() {
38 
39     companion object {
40         private const val PACKAGE_BOOT_REQUEST_KEY = "package_boot_request"
41     }
42 
onReceivenull43     override fun onReceive(context: Context, intent: Intent) {
44         if (intent.action != Intent.ACTION_BOOT_COMPLETED) return
45         val workManager = WorkManager.getInstance(context)
46         DomainVerificationUtils.schedulePeriodicCheckUnlocked(workManager)
47         workManager.beginUniqueWork(
48             PACKAGE_BOOT_REQUEST_KEY,
49             ExistingWorkPolicy.REPLACE,
50             OneTimeWorkRequestBuilder<RetryRequestWorker>()
51                 .setConstraints(
52                     Constraints.Builder()
53                         .setRequiredNetworkType(NetworkType.CONNECTED)
54                         .build()
55                 )
56                 .build()
57         ).enqueue()
58     }
59 }
60