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