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 keep-alive. */ 35 public final class KioskKeepAlivePolicyHandler implements PolicyHandler { 36 private static final String TAG = "KioskKeepAlivePolicyHandler"; 37 38 private final SystemDeviceLockManager mSystemDeviceLockManager; 39 private final SetupParametersClientInterface mSetupParametersClient; 40 private final Executor mBgExecutor; 41 KioskKeepAlivePolicyHandler(SystemDeviceLockManager systemDeviceLockManager, Executor bgExecutor)42 KioskKeepAlivePolicyHandler(SystemDeviceLockManager systemDeviceLockManager, 43 Executor bgExecutor) { 44 mSystemDeviceLockManager = systemDeviceLockManager; 45 mBgExecutor = bgExecutor; 46 mSetupParametersClient = SetupParametersClient.getInstance(); 47 } 48 getEnableKioskKeepAliveFuture(String packageName)49 private ListenableFuture<Boolean> getEnableKioskKeepAliveFuture(String packageName) { 50 return CallbackToFutureAdapter.getFuture( 51 completer -> { 52 mSystemDeviceLockManager.enableKioskKeepalive(packageName, 53 mBgExecutor, 54 new OutcomeReceiver<>() { 55 @Override 56 public void onResult(Void result) { 57 completer.set(true); 58 } 59 60 @Override 61 public void onError(Exception ex) { 62 // Return true since the keep-alive service is optional 63 LogUtil.d(TAG, "Failed to enable kiosk keep-alive", ex); 64 completer.set(true); 65 } 66 }); 67 // Used only for debugging. 68 return "getEnableKioskKeepAliveFuture"; 69 }); 70 } 71 72 private ListenableFuture<Boolean> getEnableKioskKeepAliveFuture() { 73 return Futures.transformAsync(mSetupParametersClient.getKioskPackage(), 74 kioskPackageName -> kioskPackageName == null 75 ? Futures.immediateFuture(false) 76 : getEnableKioskKeepAliveFuture(kioskPackageName), 77 MoreExecutors.directExecutor()); 78 } 79 80 private ListenableFuture<Boolean> getDisableKioskKeepAliveFuture() { 81 return CallbackToFutureAdapter.getFuture( 82 completer -> { 83 mSystemDeviceLockManager.disableKioskKeepalive(mBgExecutor, 84 new OutcomeReceiver<>() { 85 @Override 86 public void onResult(Void result) { 87 completer.set(true); 88 } 89 90 @Override 91 public void onError(Exception ex) { 92 // Return true since the keep-alive service is optional 93 LogUtil.d(TAG, "Failed to disable kiosk keep-alive", ex); 94 completer.set(true); 95 } 96 }); 97 // Used only for debugging. 98 return "getDisableKioskKeepAliveFuture"; 99 }); 100 } 101 102 @Override 103 public ListenableFuture<Boolean> onLocked() { 104 return getEnableKioskKeepAliveFuture(); 105 } 106 107 @Override 108 public ListenableFuture<Boolean> onUnlocked() { 109 return getDisableKioskKeepAliveFuture(); 110 } 111 112 @Override 113 public ListenableFuture<Boolean> onProvisioned() { 114 return getEnableKioskKeepAliveFuture(); 115 } 116 117 @Override 118 public ListenableFuture<Boolean> onCleared() { 119 return getDisableKioskKeepAliveFuture(); 120 } 121 } 122