1 /*
2  * Copyright (C) 2023 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.ui.viewmodel
18 
19 import android.util.MathUtils
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.keyguard.domain.interactor.FromAodTransitionInteractor
22 import com.android.systemui.keyguard.shared.model.Edge
23 import com.android.systemui.keyguard.shared.model.KeyguardState.AOD
24 import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
25 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow
26 import com.android.systemui.keyguard.ui.transitions.DeviceEntryIconTransition
27 import com.android.systemui.scene.shared.model.Scenes
28 import javax.inject.Inject
29 import kotlin.time.Duration.Companion.milliseconds
30 import kotlinx.coroutines.ExperimentalCoroutinesApi
31 import kotlinx.coroutines.flow.Flow
32 
33 /** Breaks down AOD->GONE transition into discrete steps for corresponding views to consume. */
34 @ExperimentalCoroutinesApi
35 @SysUISingleton
36 class AodToGoneTransitionViewModel
37 @Inject
38 constructor(
39     animationFlow: KeyguardTransitionAnimationFlow,
40 ) : DeviceEntryIconTransition {
41 
42     private val transitionAnimation =
43         animationFlow
44             .setup(
45                 duration = FromAodTransitionInteractor.TO_GONE_DURATION,
46                 edge = Edge.create(from = AOD, to = Scenes.Gone),
47             )
48             .setupWithoutSceneContainer(
49                 edge = Edge.create(from = AOD, to = GONE),
50             )
51 
52     /**
53      * AOD -> GONE should fade out the lockscreen contents. This transition plays both during wake
54      * and unlock, and also during insecure camera launch (which is GONE -> AOD (canceled) -> GONE).
55      */
lockscreenAlphanull56     fun lockscreenAlpha(viewState: ViewStateAccessor): Flow<Float> {
57         var startAlpha = 1f
58         return transitionAnimation.sharedFlow(
59             duration = 200.milliseconds,
60             onStart = { startAlpha = viewState.alpha() },
61             onStep = { MathUtils.lerp(startAlpha, 0f, it) },
62             onFinish = { 0f },
63         )
64     }
65 
notificationAlphanull66     fun notificationAlpha(viewState: ViewStateAccessor): Flow<Float> {
67         var startAlpha = 1f
68         return transitionAnimation.sharedFlow(
69             duration = 200.milliseconds,
70             onStart = { startAlpha = viewState.alpha() },
71             onStep = { startAlpha },
72             onFinish = { 1f },
73         )
74     }
75 
76     override val deviceEntryParentViewAlpha = transitionAnimation.immediatelyTransitionTo(0f)
77 }
78