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.policy;
18 
19 import android.os.OutcomeReceiver;
20 
21 import androidx.concurrent.futures.CallbackToFutureAdapter;
22 
23 import com.android.devicelockcontroller.SystemDeviceLockManager;
24 import com.android.devicelockcontroller.storage.SetupParametersClient;
25 import com.android.devicelockcontroller.storage.SetupParametersClientInterface;
26 import com.android.devicelockcontroller.util.LogUtil;
27 
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 import java.util.concurrent.Executor;
33 
34 /** Handles kiosk app role. */
35 public final class RolePolicyHandler implements PolicyHandler {
36     private static final String TAG = "RolePolicyHandler";
37 
38     private final SystemDeviceLockManager mSystemDeviceLockManager;
39     private final SetupParametersClientInterface mSetupParametersClient;
40     private final Executor mBgExecutor;
41 
RolePolicyHandler(SystemDeviceLockManager systemDeviceLockManager, Executor bgExecutor)42     RolePolicyHandler(SystemDeviceLockManager systemDeviceLockManager, Executor bgExecutor) {
43         mSystemDeviceLockManager = systemDeviceLockManager;
44         mBgExecutor = bgExecutor;
45         mSetupParametersClient = SetupParametersClient.getInstance();
46     }
47 
getAddFinancedDeviceLockKioskRoleFuture(String packageName)48     private ListenableFuture<Boolean> getAddFinancedDeviceLockKioskRoleFuture(String packageName) {
49         return CallbackToFutureAdapter.getFuture(
50                 completer -> {
51                     mSystemDeviceLockManager.addFinancedDeviceKioskRole(packageName,
52                             mBgExecutor,
53                             new OutcomeReceiver<>() {
54                                 @Override
55                                 public void onResult(Void result) {
56                                     completer.set(true);
57                                 }
58 
59                                 @Override
60                                 public void onError(Exception ex) {
61                                     LogUtil.e(TAG, "Failed to add financed device kiosk role",
62                                             ex);
63                                     completer.set(false);
64                                 }
65                             });
66                     // Used only for debugging.
67                     return "getAddFinancedDeviceLockKioskRoleFuture";
68                 });
69     }
70 
71     private ListenableFuture<Boolean> getAddFinancedDeviceLockKioskRoleFuture() {
72         return Futures.transformAsync(mSetupParametersClient.getKioskPackage(),
73                 kioskPackageName -> kioskPackageName == null
74                         ? Futures.immediateFuture(false)
75                         : getAddFinancedDeviceLockKioskRoleFuture(kioskPackageName),
76                 MoreExecutors.directExecutor());
77     }
78 
79     private ListenableFuture<Boolean> getRemoveFinancedDeviceLockKioskRoleFuture(
80             String packageName) {
81         return CallbackToFutureAdapter.getFuture(
82                 completer -> {
83                     mSystemDeviceLockManager.removeFinancedDeviceKioskRole(packageName,
84                             mBgExecutor,
85                             new OutcomeReceiver<>() {
86                                 @Override
87                                 public void onResult(Void result) {
88                                     completer.set(true);
89                                 }
90 
91                                 @Override
92                                 public void onError(Exception ex) {
93                                     LogUtil.e(TAG, "Failed to remove financed device kiosk role",
94                                             ex);
95                                     completer.set(false);
96                                 }
97                             });
98                     // Used only for debugging.
99                     return "getRemoveFinancedDeviceLockKioskRoleFuture";
100                 });
101     }
102 
103     private ListenableFuture<Boolean> getRemoveFinancedDeviceLockKioskRoleFuture() {
104         return Futures.transformAsync(mSetupParametersClient.getKioskPackage(),
105                 kioskPackageName -> kioskPackageName == null
106                         ? Futures.immediateFuture(false)
107                         : getRemoveFinancedDeviceLockKioskRoleFuture(kioskPackageName),
108                 MoreExecutors.directExecutor());
109     }
110 
111     @Override
112     public ListenableFuture<Boolean> onProvisioned() {
113         return getAddFinancedDeviceLockKioskRoleFuture();
114     }
115 
116     @Override
117     public ListenableFuture<Boolean> onCleared() {
118         return getRemoveFinancedDeviceLockKioskRoleFuture();
119     }
120 }
121