• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserManager;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.devicelockcontroller.policy.DeviceStateController;
27 import com.android.devicelockcontroller.policy.PolicyObjectsInterface;
28 import com.android.devicelockcontroller.provision.worker.DeviceCheckInHelper;
29 import com.android.devicelockcontroller.storage.GlobalParametersClient;
30 import com.android.devicelockcontroller.util.LogUtil;
31 
32 import com.google.common.util.concurrent.FutureCallback;
33 import com.google.common.util.concurrent.Futures;
34 import com.google.common.util.concurrent.MoreExecutors;
35 
36 /**
37  * Boot completed broadcast receiver to enqueue the check-in work for provision when device boots
38  * for the first time.
39  * Note that this boot completed receiver differs with {@link LockTaskBootCompletedReceiver} in the
40  * way that it only runs for system user.
41  */
42 public final class CheckInBootCompletedReceiver extends BroadcastReceiver {
43 
44     private static final String TAG = "CheckInBootCompletedReceiver";
45 
46     @VisibleForTesting
checkInIfNeeded(DeviceStateController stateController, DeviceCheckInHelper checkInHelper)47     static void checkInIfNeeded(DeviceStateController stateController,
48             DeviceCheckInHelper checkInHelper) {
49         if (stateController.isCheckInNeeded()) {
50             Futures.addCallback(GlobalParametersClient.getInstance().needCheckIn(),
51                     new FutureCallback<>() {
52                         @Override
53                         public void onSuccess(Boolean needCheckIn) {
54                             if (needCheckIn) {
55                                 checkInHelper.enqueueDeviceCheckInWork(/* isExpedited= */ false);
56                             }
57                         }
58 
59                         @Override
60                         public void onFailure(Throwable t) {
61                             LogUtil.e(TAG, "Failed to know if we need to perform check-in!", t);
62                         }
63                     }, MoreExecutors.directExecutor());
64         }
65     }
66 
67     @Override
onReceive(Context context, Intent intent)68     public void onReceive(Context context, Intent intent) {
69         if (!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) return;
70 
71         LogUtil.i(TAG, "Received boot completed intent");
72 
73         final boolean isUserProfile =
74                 context.getSystemService(UserManager.class).isProfile();
75 
76         if (isUserProfile) {
77             return;
78         }
79 
80         checkInIfNeeded(
81                 ((PolicyObjectsInterface) context.getApplicationContext()).getStateController(),
82                 new DeviceCheckInHelper(context));
83     }
84 }
85