1 /* 2 * Copyright (C) 2024 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 setting POST_NOTIFICATIONS as SYSTEM_FIXED. */ 31 public final class NotificationsPolicyHandler implements PolicyHandler { 32 private static final String TAG = "NotificationsPolicyHandler"; 33 34 private final SystemDeviceLockManager mSystemDeviceLockManager; 35 private final Executor mBgExecutor; 36 NotificationsPolicyHandler(SystemDeviceLockManager systemDeviceLockManager, Executor bgExecutor)37 NotificationsPolicyHandler(SystemDeviceLockManager systemDeviceLockManager, 38 Executor bgExecutor) { 39 mSystemDeviceLockManager = systemDeviceLockManager; 40 mBgExecutor = bgExecutor; 41 } 42 setPostNotificationsSystemFixedFuture(boolean systemFixed)43 private ListenableFuture<Boolean> setPostNotificationsSystemFixedFuture(boolean systemFixed) { 44 return CallbackToFutureAdapter.getFuture( 45 completer -> { 46 mSystemDeviceLockManager.setPostNotificationsSystemFixed( 47 systemFixed, 48 mBgExecutor, 49 new OutcomeReceiver<>() { 50 @Override 51 public void onResult(Void result) { 52 completer.set(true); 53 } 54 55 @Override 56 public void onError(Exception ex) { 57 LogUtil.e(TAG, "Failed to set POST_NOTIFICATIONS system fixed " 58 + "flag to: " + systemFixed, ex); 59 // Return true since we don't want to fail the transition 60 completer.set(true); 61 } 62 }); 63 // Used only for debugging. 64 return "setPostNotificationsSystemFixedFuture"; 65 }); 66 } 67 68 @Override 69 public ListenableFuture<Boolean> onProvisionInProgress() { 70 return setPostNotificationsSystemFixedFuture(/* systemFixed= */ true); 71 } 72 73 @Override 74 public ListenableFuture<Boolean> onCleared() { 75 return setPostNotificationsSystemFixedFuture(/* systemFixed= */ false); 76 } 77 } 78