1 /*
2  * Copyright (C) 2022 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.keyguard.mediator
18 
19 import android.annotation.BinderThread
20 import android.os.Handler
21 import android.os.Trace
22 import com.android.systemui.Flags
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.dagger.qualifiers.Main
25 import com.android.systemui.unfold.SysUIUnfoldComponent
26 import com.android.systemui.util.concurrency.PendingTasksContainer
27 import com.android.systemui.util.kotlin.getOrNull
28 import java.util.Optional
29 import javax.inject.Inject
30 
31 /**
32  * Coordinates screen on/turning on animations for the KeyguardViewMediator. Specifically for screen
33  * on events, this will invoke the onDrawn Runnable after all tasks have completed. This should
34  * route back to the [com.android.systemui.keyguard.KeyguardService], which informs the
35  * system_server that keyguard has drawn.
36  */
37 @SysUISingleton
38 class ScreenOnCoordinator
39 @Inject
40 constructor(
41     unfoldComponent: Optional<SysUIUnfoldComponent>,
42     @Main private val mainHandler: Handler,
43 ) {
44 
45     private val foldAodAnimationController =
46         unfoldComponent.map(SysUIUnfoldComponent::getFoldAodAnimationController).getOrNull()
47     private val fullScreenLightRevealAnimations =
48         unfoldComponent.map(SysUIUnfoldComponent::getFullScreenLightRevealAnimations).getOrNull()
49     private val pendingTasks = PendingTasksContainer()
50 
51     /**
52      * When turning on, registers tasks that may need to run before invoking [onDrawn]. This is
53      * called on a binder thread from [com.android.systemui.keyguard.KeyguardService].
54      */
55     @BinderThread
onScreenTurningOnnull56     fun onScreenTurningOn(onDrawn: Runnable) {
57         Trace.beginSection("ScreenOnCoordinator#onScreenTurningOn")
58 
59         pendingTasks.reset()
60 
61         foldAodAnimationController?.onScreenTurningOn(pendingTasks.registerTask("fold-to-aod"))
62         fullScreenLightRevealAnimations?.forEach {
63             it.onScreenTurningOn(pendingTasks.registerTask(it::class.java.simpleName))
64         }
65 
66         pendingTasks.onTasksComplete {
67             if (Flags.enableBackgroundKeyguardOndrawnCallback()) {
68                 // called by whatever thread completes the last task registered.
69                 onDrawn.run()
70             } else {
71                 mainHandler.post { onDrawn.run() }
72             }
73         }
74         Trace.endSection()
75     }
76 
77     /**
78      * Called when screen is fully turned on and screen on blocker is removed. This is called on a
79      * binder thread from [com.android.systemui.keyguard.KeyguardService].
80      */
81     @BinderThread
onScreenTurnedOnnull82     fun onScreenTurnedOn() {
83         foldAodAnimationController?.onScreenTurnedOn()
84     }
85 
86     @BinderThread
onScreenTurnedOffnull87     fun onScreenTurnedOff() {
88         pendingTasks.reset()
89     }
90 }
91