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 
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.devicelockcontroller.policy.PolicyObjectsProvider;
26 import com.android.devicelockcontroller.stats.StatsLogger;
27 import com.android.devicelockcontroller.stats.StatsLoggerProvider;
28 import com.android.devicelockcontroller.storage.SetupParametersClient;
29 import com.android.devicelockcontroller.util.LogUtil;
30 
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
33 
34 import java.util.concurrent.Executor;
35 import java.util.concurrent.Executors;
36 
37 /**
38  * A broadcast receiver that will factory reset the device when it receives a broadcast.
39  */
40 public final class ResetDeviceReceiver extends BroadcastReceiver {
41     private static final String TAG = "ResetDeviceReceiver";
42     private final Executor mExecutor;
43 
ResetDeviceReceiver()44     public ResetDeviceReceiver() {
45         mExecutor = Executors.newSingleThreadExecutor();
46     }
47 
48     @VisibleForTesting
ResetDeviceReceiver(Executor executor)49     ResetDeviceReceiver(Executor executor) {
50         mExecutor = executor;
51     }
52 
53     @Override
onReceive(Context context, Intent intent)54     public void onReceive(Context context, Intent intent) {
55         if (!ResetDeviceReceiver.class.getName().equals(intent.getComponent().getClassName())) {
56             throw new IllegalArgumentException("Can not handle implicit intent!");
57         }
58         Futures.addCallback(SetupParametersClient.getInstance().isProvisionMandatory(),
59                 new FutureCallback<Boolean>() {
60                     @Override
61                     public void onSuccess(Boolean isProvisionMandatory) {
62                         StatsLogger logger = ((StatsLoggerProvider) context.getApplicationContext())
63                                 .getStatsLogger();
64                         logger.logDeviceReset(isProvisionMandatory);
65                         ((PolicyObjectsProvider) context.getApplicationContext())
66                                 .getPolicyController().wipeDevice();
67                     }
68 
69                     @Override
70                     public void onFailure(Throwable t) {
71                         // We don't want the statistics event to cancel the device reset, so
72                         // we just log the error here and proceed.
73                         LogUtil.e(TAG, "Error querying isProvisionMandatory", t);
74                         ((PolicyObjectsProvider) context.getApplicationContext())
75                                 .getPolicyController().wipeDevice();
76                     }
77                 }, mExecutor);
78     }
79 }
80