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 com.android.devicelockcontroller.policy.PolicyObjectsProvider;
25 import com.android.devicelockcontroller.util.LogUtil;
26 
27 import com.google.common.util.concurrent.FutureCallback;
28 import com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import com.google.common.util.concurrent.MoreExecutors;
31 
32 /**
33  * Boot complete receiver to initialize finalization state on device.
34  */
35 public final class FinalizationBootCompletedReceiver extends BroadcastReceiver {
36     private static final String TAG = FinalizationBootCompletedReceiver.class.getSimpleName();
37 
38     @Override
onReceive(Context context, Intent intent)39     public void onReceive(Context context, Intent intent) {
40         if (!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) return;
41 
42         final boolean isUserProfile =
43                 context.getSystemService(UserManager.class).isProfile();
44 
45         if (isUserProfile) {
46             // Not needed as the receiver will run in the parent user
47             return;
48         }
49 
50         // Initialize finalization controller to apply device finalization state
51         ListenableFuture<Void> enforced = ((PolicyObjectsProvider) context.getApplicationContext())
52                 .getFinalizationController().enforceDiskState(/* force= */ false);
53         Futures.addCallback(enforced, new FutureCallback<>() {
54             @Override
55             public void onSuccess(Void result) {
56                 // no-op
57             }
58 
59             @Override
60             public void onFailure(Throwable t) {
61                 LogUtil.e(TAG, "Failed to enforce finalization state on boot!", t);
62             }
63         }, MoreExecutors.directExecutor());
64     }
65 }
66