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.systemui.ambient.touch.scrim; 18 19 import static com.android.systemui.ambient.touch.scrim.dagger.ScrimModule.BOUNCERLESS_SCRIM_CONTROLLER; 20 import static com.android.systemui.ambient.touch.scrim.dagger.ScrimModule.BOUNCER_SCRIM_CONTROLLER; 21 22 import com.android.systemui.dagger.qualifiers.Main; 23 import com.android.systemui.statusbar.policy.KeyguardStateController; 24 25 import java.util.HashSet; 26 import java.util.concurrent.Executor; 27 28 import javax.inject.Inject; 29 import javax.inject.Named; 30 31 /** 32 * {@link ScrimManager} helps manage multiple {@link ScrimController} instances, specifying the 33 * appropriate one to use at the current moment and managing the handoff between controllers. 34 */ 35 public class ScrimManager { 36 private final ScrimController mBouncerScrimController; 37 private final ScrimController mBouncerlessScrimController; 38 private final KeyguardStateController mKeyguardStateController; 39 private final Executor mExecutor; 40 41 private ScrimController mCurrentController; 42 private final HashSet<Callback> mCallbacks; 43 44 /** 45 * Interface implemented for receiving updates to the active {@link ScrimController}. 46 */ 47 public interface Callback { 48 /** 49 * Invoked when the controller changes. 50 * @param controller The currently active {@link ScrimController}. 51 */ onScrimControllerChanged(ScrimController controller)52 void onScrimControllerChanged(ScrimController controller); 53 } 54 55 private final KeyguardStateController.Callback mKeyguardStateCallback = 56 new KeyguardStateController.Callback() { 57 @Override 58 public void onKeyguardShowingChanged() { 59 mExecutor.execute(() -> updateController()); 60 } 61 }; 62 63 @Inject ScrimManager(@ain Executor executor, @Named(BOUNCER_SCRIM_CONTROLLER) ScrimController bouncerScrimController, @Named(BOUNCERLESS_SCRIM_CONTROLLER)ScrimController bouncerlessScrimController, KeyguardStateController keyguardStateController)64 ScrimManager(@Main Executor executor, 65 @Named(BOUNCER_SCRIM_CONTROLLER) ScrimController bouncerScrimController, 66 @Named(BOUNCERLESS_SCRIM_CONTROLLER)ScrimController bouncerlessScrimController, 67 KeyguardStateController keyguardStateController) { 68 mExecutor = executor; 69 mCallbacks = new HashSet<>(); 70 mBouncerlessScrimController = bouncerlessScrimController; 71 mBouncerScrimController = bouncerScrimController; 72 mKeyguardStateController = keyguardStateController; 73 74 mKeyguardStateController.addCallback(mKeyguardStateCallback); 75 updateController(); 76 } 77 updateController()78 private void updateController() { 79 final ScrimController existingController = mCurrentController; 80 mCurrentController = mKeyguardStateController.canDismissLockScreen() 81 ? mBouncerlessScrimController 82 : mBouncerScrimController; 83 84 if (existingController == mCurrentController) { 85 return; 86 } 87 88 mCallbacks.forEach(callback -> callback.onScrimControllerChanged(mCurrentController)); 89 } 90 91 /** 92 * Adds a {@link Callback} to receive future changes to the active {@link ScrimController}. 93 */ addCallback(Callback callback)94 public void addCallback(Callback callback) { 95 mExecutor.execute(() -> mCallbacks.add(callback)); 96 } 97 98 /** 99 * Removes the {@link Callback} from receiving further updates. 100 */ removeCallback(Callback callback)101 public void removeCallback(Callback callback) { 102 mExecutor.execute(() -> mCallbacks.remove(callback)); 103 } 104 105 /** 106 * Returns the currently get {@link ScrimController}. 107 * @return 108 */ getCurrentController()109 public ScrimController getCurrentController() { 110 return mCurrentController; 111 } 112 } 113