1 /*
<lambda>null2  * 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.deviceentry.domain.interactor.DeviceEntryUdfpsInteractor
22 import com.android.systemui.keyguard.domain.interactor.FromLockscreenTransitionInteractor
23 import com.android.systemui.keyguard.shared.model.Edge
24 import com.android.systemui.keyguard.shared.model.KeyguardState.AOD
25 import com.android.systemui.keyguard.shared.model.KeyguardState.LOCKSCREEN
26 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow
27 import com.android.systemui.keyguard.ui.transitions.DeviceEntryIconTransition
28 import javax.inject.Inject
29 import kotlin.time.Duration.Companion.milliseconds
30 import kotlinx.coroutines.ExperimentalCoroutinesApi
31 import kotlinx.coroutines.flow.Flow
32 import kotlinx.coroutines.flow.flatMapLatest
33 
34 /**
35  * Breaks down LOCKSCREEN->AOD transition into discrete steps for corresponding views to consume.
36  */
37 @ExperimentalCoroutinesApi
38 @SysUISingleton
39 class LockscreenToAodTransitionViewModel
40 @Inject
41 constructor(
42     deviceEntryUdfpsInteractor: DeviceEntryUdfpsInteractor,
43     shadeDependentFlows: ShadeDependentFlows,
44     animationFlow: KeyguardTransitionAnimationFlow,
45 ) : DeviceEntryIconTransition {
46 
47     private val transitionAnimation =
48         animationFlow.setup(
49             duration = FromLockscreenTransitionInteractor.TO_AOD_DURATION,
50             edge = Edge.create(from = LOCKSCREEN, to = AOD),
51         )
52 
53     val deviceEntryBackgroundViewAlpha: Flow<Float> =
54         shadeDependentFlows.transitionFlow(
55             flowWhenShadeIsExpanded = transitionAnimation.immediatelyTransitionTo(0f),
56             flowWhenShadeIsNotExpanded =
57                 transitionAnimation.sharedFlow(
58                     duration = 300.milliseconds,
59                     onStep = { 1 - it },
60                     onFinish = { 0f },
61                 ),
62         )
63 
64     val shortcutsAlpha: Flow<Float> =
65         transitionAnimation.sharedFlow(
66             duration = 250.milliseconds,
67             onStep = { 1 - it },
68             onFinish = { 0f },
69             onCancel = { 1f },
70         )
71 
72     fun lockscreenAlpha(viewState: ViewStateAccessor): Flow<Float> {
73         var startAlpha = 1f
74         return transitionAnimation.sharedFlow(
75             duration = 500.milliseconds,
76             onStart = { startAlpha = viewState.alpha() },
77             onStep = { MathUtils.lerp(startAlpha, 1f, it) },
78         )
79     }
80 
81     override val deviceEntryParentViewAlpha: Flow<Float> =
82         deviceEntryUdfpsInteractor.isUdfpsEnrolledAndEnabled.flatMapLatest {
83             isUdfpsEnrolledAndEnabled ->
84             if (isUdfpsEnrolledAndEnabled) {
85                 shadeDependentFlows.transitionFlow(
86                     flowWhenShadeIsExpanded = // fade in
87                     transitionAnimation.sharedFlow(
88                             duration = 300.milliseconds,
89                             onStep = { it },
90                             onFinish = { 1f },
91                         ),
92                     flowWhenShadeIsNotExpanded = transitionAnimation.immediatelyTransitionTo(1f),
93                 )
94             } else {
95                 shadeDependentFlows.transitionFlow(
96                     flowWhenShadeIsExpanded = transitionAnimation.immediatelyTransitionTo(0f),
97                     flowWhenShadeIsNotExpanded = // fade out
98                     transitionAnimation.sharedFlow(
99                             duration = 200.milliseconds,
100                             onStep = { 1f - it },
101                             onFinish = { 0f },
102                         ),
103                 )
104             }
105         }
106 }
107