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.util.LogUtil;
25 
26 import com.google.common.util.concurrent.ListenableFuture;
27 
28 import java.util.concurrent.Executor;
29 
30 /** Handles device lock controller keep-alive. */
31 public final class ControllerKeepAlivePolicyHandler implements PolicyHandler {
32     private static final String TAG = "ControllerKeepAlivePolicyHandler";
33 
34     private final SystemDeviceLockManager mSystemDeviceLockManager;
35     private final Executor mBgExecutor;
36 
ControllerKeepAlivePolicyHandler(SystemDeviceLockManager systemDeviceLockManager, Executor bgExecutor)37     ControllerKeepAlivePolicyHandler(SystemDeviceLockManager systemDeviceLockManager,
38             Executor bgExecutor) {
39         mSystemDeviceLockManager = systemDeviceLockManager;
40         mBgExecutor = bgExecutor;
41     }
42 
getEnableControllerKeepAliveFuture()43     private ListenableFuture<Boolean> getEnableControllerKeepAliveFuture() {
44         return CallbackToFutureAdapter.getFuture(
45                 completer -> {
46                     mSystemDeviceLockManager.enableControllerKeepalive(
47                             mBgExecutor,
48                             new OutcomeReceiver<>() {
49                                 @Override
50                                 public void onResult(Void result) {
51                                     completer.set(true);
52                                 }
53 
54                                 @Override
55                                 public void onError(Exception ex) {
56                                     LogUtil.e(TAG, "Failed to enable controller keep-alive",
57                                             ex);
58                                     // Return SUCCESS since we don't want to fail the transition
59                                     completer.set(true);
60                                 }
61                             });
62                     // Used only for debugging.
63                     return "getEnableControllerKeepAliveFuture";
64                 });
65     }
66 
67     private ListenableFuture<Boolean> getDisableControllerKeepAliveFuture() {
68         return CallbackToFutureAdapter.getFuture(
69                 completer -> {
70                     mSystemDeviceLockManager.disableControllerKeepalive(mBgExecutor,
71                             new OutcomeReceiver<>() {
72                                 @Override
73                                 public void onResult(Void result) {
74                                     completer.set(true);
75                                 }
76 
77                                 @Override
78                                 public void onError(Exception ex) {
79                                     LogUtil.e(TAG, "Failed to disable controller keep-alive",
80                                             ex);
81                                     // Return SUCCESS since we don't want to fail the transition
82                                     completer.set(true);
83                                 }
84                             });
85                     // Used only for debugging.
86                     return "getDisableControllerKeepAliveFuture";
87                 });
88     }
89 
90     @Override
91     public ListenableFuture<Boolean> onProvisionInProgress() {
92         return getEnableControllerKeepAliveFuture();
93     }
94 
95     @Override
96     public ListenableFuture<Boolean> onProvisionPaused() {
97         return getDisableControllerKeepAliveFuture();
98     }
99 
100     @Override
101     public ListenableFuture<Boolean> onProvisioned() {
102         return getDisableControllerKeepAliveFuture();
103     }
104 }
105