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.devicelockcontroller.receivers;
18 
19 import static com.android.devicelockcontroller.policy.ProvisionStateController.ProvisionState.PROVISION_FAILED;
20 import static com.android.devicelockcontroller.policy.ProvisionStateController.ProvisionState.PROVISION_PAUSED;
21 
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.UserManager;
26 
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.devicelockcontroller.policy.PolicyObjectsProvider;
30 import com.android.devicelockcontroller.policy.ProvisionStateController;
31 import com.android.devicelockcontroller.policy.ProvisionStateController.ProvisionState;
32 import com.android.devicelockcontroller.schedule.DeviceLockControllerScheduler;
33 import com.android.devicelockcontroller.schedule.DeviceLockControllerSchedulerProvider;
34 import com.android.devicelockcontroller.util.LogUtil;
35 
36 import com.google.common.util.concurrent.FutureCallback;
37 import com.google.common.util.concurrent.Futures;
38 
39 import java.util.concurrent.Executor;
40 import java.util.concurrent.Executors;
41 
42 /**
43  * Handle {@link  Intent#ACTION_LOCKED_BOOT_COMPLETED}. This receiver runs for any user
44  * (singleUser="false").
45  * <p>
46  * This receiver reschedules alarms if needed.
47  */
48 public final class LockedBootCompletedReceiver extends BroadcastReceiver {
49     private static final String TAG = "LockedBootCompletedReceiver";
50     private final Executor mExecutor;
51 
LockedBootCompletedReceiver()52     public LockedBootCompletedReceiver() {
53         mExecutor = Executors.newSingleThreadExecutor();
54     }
55 
56     @VisibleForTesting
LockedBootCompletedReceiver(Executor executor)57     LockedBootCompletedReceiver(Executor executor) {
58         mExecutor = executor;
59     }
60 
61     @Override
onReceive(Context context, Intent intent)62     public void onReceive(Context context, Intent intent) {
63         LogUtil.d(TAG, "Locked Boot completed");
64         if (!intent.getAction().equals(Intent.ACTION_LOCKED_BOOT_COMPLETED)) {
65             return;
66         }
67 
68         final boolean isUserProfile =
69                 context.getSystemService(UserManager.class).isProfile();
70         if (isUserProfile) {
71             return;
72         }
73 
74         Context applicationContext = context.getApplicationContext();
75         ProvisionStateController stateController = ((PolicyObjectsProvider) applicationContext)
76                 .getProvisionStateController();
77 
78         DeviceLockControllerSchedulerProvider schedulerProvider =
79                 (DeviceLockControllerSchedulerProvider) applicationContext;
80         DeviceLockControllerScheduler scheduler =
81                 schedulerProvider.getDeviceLockControllerScheduler();
82         Futures.addCallback(stateController.getState(),
83                 new FutureCallback<>() {
84                     @Override
85                     public void onSuccess(@ProvisionState Integer state) {
86                         if (state == PROVISION_PAUSED) {
87                             scheduler.notifyRebootWhenProvisionPaused();
88                         } else if (state == PROVISION_FAILED) {
89                             scheduler.notifyRebootWhenProvisionFailed();
90                         }
91                     }
92 
93                     @Override
94                     public void onFailure(Throwable t) {
95                         throw new RuntimeException(t);
96                     }
97                 }, mExecutor);
98     }
99 }
100