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.keyguard.domain.interactor
18 
19 import android.animation.ValueAnimator
20 import android.view.ViewGroup
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.dagger.qualifiers.Main
24 import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepository
25 import com.android.systemui.keyguard.shared.model.TransitionInfo
26 import com.android.systemui.keyguard.shared.model.TransitionModeOnCanceled
27 import com.android.systemui.shade.NotificationPanelViewController
28 import com.android.systemui.shade.ShadeFoldAnimator
29 import javax.inject.Inject
30 import kotlinx.coroutines.CoroutineDispatcher
31 import kotlinx.coroutines.CoroutineScope
32 import kotlinx.coroutines.launch
33 
34 @SysUISingleton
35 class ToAodFoldTransitionInteractor
36 @Inject
37 constructor(
38     private val keyguardClockInteractor: KeyguardClockInteractor,
39     private val transitionInteractor: KeyguardTransitionInteractor,
40     private val transitionRepository: KeyguardTransitionRepository,
41     @Application private val mainScope: CoroutineScope,
42     @Main private val mainDispatcher: CoroutineDispatcher,
43 ) {
44     private var parentAnimator: NotificationPanelViewController.ShadeFoldAnimatorImpl? = null
45 
46     // TODO(b/331770313): Migrate to PowerInteractor; Deprecate ShadeFoldAnimator again
47     val foldAnimator =
48         object : ShadeFoldAnimator {
49             override val view: ViewGroup?
50                 get() = throw NotImplementedError("Deprecated. Do not call.")
51 
prepareFoldToAodAnimationnull52             override fun prepareFoldToAodAnimation() {
53                 forceToAod()
54                 parentAnimator?.prepareFoldToAodAnimation()
55             }
56 
startFoldToAodAnimationnull57             override fun startFoldToAodAnimation(
58                 startAction: Runnable,
59                 endAction: Runnable,
60                 cancelAction: Runnable
61             ) {
62                 parentAnimator?.let {
63                     it.buildViewAnimator(startAction, endAction, cancelAction)
64                         .setUpdateListener {
65                             keyguardClockInteractor.animateFoldToAod(it.animatedFraction)
66                         }
67                         .start()
68                 }
69             }
70 
cancelFoldToAodAnimationnull71             override fun cancelFoldToAodAnimation() {
72                 parentAnimator?.cancelFoldToAodAnimation()
73             }
74         }
75 
initializenull76     fun initialize(parentAnimator: ShadeFoldAnimator) {
77         this.parentAnimator =
78             parentAnimator as? NotificationPanelViewController.ShadeFoldAnimatorImpl?
79     }
80 
81     /** Forces the keyguard into AOD or Doze */
forceToAodnull82     private fun forceToAod() {
83         mainScope.launch(mainDispatcher) {
84             transitionRepository.startTransition(
85                 TransitionInfo(
86                     "$TAG (Fold transition triggered)",
87                     transitionInteractor.getCurrentState(),
88                     transitionInteractor.asleepKeyguardState.value,
89                     ValueAnimator().apply { duration = 0 },
90                     TransitionModeOnCanceled.LAST_VALUE,
91                 )
92             )
93         }
94     }
95 
96     companion object {
97         private val TAG = ToAodFoldTransitionInteractor::class.simpleName!!
98     }
99 }
100