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.debug;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.os.UserManager;
24 import android.text.TextUtils;
25 
26 import com.android.devicelockcontroller.storage.SetupParametersClient;
27 import com.android.devicelockcontroller.util.LogUtil;
28 
29 import com.google.common.util.concurrent.FutureCallback;
30 import com.google.common.util.concurrent.Futures;
31 import com.google.common.util.concurrent.MoreExecutors;
32 
33 /**
34  * A helper class used to receive commands from ADB.
35  * Used for testing purpose only.
36  */
37 public final class SetupParametersOverrider extends BroadcastReceiver {
38 
39     private static final String TAG = "SetupParametersOverrider";
40 
41     @Override
onReceive(Context context, Intent intent)42     public void onReceive(Context context, Intent intent) {
43         if (!Build.isDebuggable()) {
44             LogUtil.w(TAG, "Adb command is not supported in non-debuggable build!");
45             return;
46         }
47         if (!TextUtils.equals(intent.getComponent().getClassName(), this.getClass().getName())) {
48             LogUtil.w(TAG, "Implicit intent should not be used!");
49             return;
50         }
51 
52         final boolean isUserProfile =
53                 context.getSystemService(UserManager.class).isProfile();
54         if (isUserProfile) {
55             LogUtil.w(TAG, "Broadcast should not target user profiles");
56             return;
57         }
58 
59         Futures.addCallback(
60                 SetupParametersClient.getInstance().overridePrefs(intent.getExtras()),
61                 new FutureCallback<>() {
62                     @Override
63                     public void onSuccess(Void result) {
64                         LogUtil.i(TAG, "Successfully override setup parameters!");
65                     }
66 
67                     @Override
68                     public void onFailure(Throwable t) {
69                         LogUtil.e(TAG, "Failed to override setup parameters!", t);
70                     }
71                 }, MoreExecutors.directExecutor());
72     }
73 }
74