1 /*
2  * Copyright (C) 2022 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.rkpdapp;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.util.Log;
23 
24 import androidx.work.Constraints;
25 import androidx.work.ExistingPeriodicWorkPolicy;
26 import androidx.work.NetworkType;
27 import androidx.work.OneTimeWorkRequest;
28 import androidx.work.PeriodicWorkRequest;
29 import androidx.work.WorkManager;
30 
31 import com.android.rkpdapp.provisioner.PeriodicProvisioner;
32 import com.android.rkpdapp.provisioner.WidevineProvisioner;
33 import com.android.rkpdapp.utils.Settings;
34 
35 import java.util.concurrent.TimeUnit;
36 
37 /**
38  * A receiver class that listens for boot to be completed and then starts a recurring job that will
39  * monitor the status of the attestation key pool on device, purging old certificates and requesting
40  * new ones as needed.
41  */
42 public class BootReceiver extends BroadcastReceiver {
43     private static final String TAG = "RkpdBootReceiver";
44 
45     @Override
onReceive(Context context, Intent intent)46     public void onReceive(Context context, Intent intent) {
47         Log.i(TAG, "Caught boot intent, waking up.");
48         Settings.generateAndSetId(context);
49 
50         Constraints constraints = new Constraints.Builder()
51                 .setRequiredNetworkType(NetworkType.CONNECTED)
52                 .build();
53 
54         PeriodicWorkRequest workRequest =
55                 new PeriodicWorkRequest.Builder(PeriodicProvisioner.class, 1, TimeUnit.DAYS)
56                         .setConstraints(constraints)
57                         .build();
58         WorkManager
59                 .getInstance(context)
60                 .enqueueUniquePeriodicWork(PeriodicProvisioner.UNIQUE_WORK_NAME,
61                         ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, // Replace on reboot.
62                         workRequest);
63 
64         Log.i(TAG, "Queueing a one-time provisioning job for widevine provisioning.");
65         OneTimeWorkRequest wvRequest = new OneTimeWorkRequest.Builder(WidevineProvisioner.class)
66                 .addTag("WidevineProvisioner")
67                 .setConstraints(constraints)
68                 .build();
69         WorkManager.getInstance(context).enqueue(wvRequest);
70     }
71 }
72