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.app.animation.Interpolators.FAST_OUT_SLOW_IN
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.keyguard.domain.interactor.FromAodTransitionInteractor.Companion.TO_LOCKSCREEN_DURATION
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.StateToValue
28 import com.android.systemui.keyguard.ui.transitions.DeviceEntryIconTransition
29 import com.android.systemui.shade.domain.interactor.ShadeInteractor
30 import javax.inject.Inject
31 import kotlin.time.Duration.Companion.milliseconds
32 import kotlinx.coroutines.ExperimentalCoroutinesApi
33 import kotlinx.coroutines.flow.Flow
34 
35 /**
36  * Breaks down AOD->LOCKSCREEN transition into discrete steps for corresponding views to consume.
37  */
38 @ExperimentalCoroutinesApi
39 @SysUISingleton
40 class AodToLockscreenTransitionViewModel
41 @Inject
42 constructor(
43     shadeInteractor: ShadeInteractor,
44     animationFlow: KeyguardTransitionAnimationFlow,
45 ) : DeviceEntryIconTransition {
46 
47     private val transitionAnimation =
48         animationFlow.setup(
49             duration = TO_LOCKSCREEN_DURATION,
50             edge = Edge.create(from = AOD, to = LOCKSCREEN),
51         )
52 
53     private var isShadeExpanded = false
54 
55     /**
56      * Begin the transition from wherever the y-translation value is currently. This helps ensure a
57      * smooth transition if a transition in canceled.
58      */
translationYnull59     fun translationY(currentTranslationY: () -> Float?): Flow<StateToValue> {
60         var startValue = 0f
61         return transitionAnimation.sharedFlowWithState(
62             duration = 500.milliseconds,
63             onStart = { startValue = currentTranslationY() ?: 0f },
64             onStep = { MathUtils.lerp(startValue, 0f, FAST_OUT_SLOW_IN.getInterpolation(it)) },
65         )
66     }
67 
68     /** Ensure alpha is set to be visible */
lockscreenAlphanull69     fun lockscreenAlpha(viewState: ViewStateAccessor): Flow<Float> {
70         var startAlpha = 1f
71         return transitionAnimation.sharedFlow(
72             duration = 500.milliseconds,
73             onStart = { startAlpha = viewState.alpha() },
74             onStep = { MathUtils.lerp(startAlpha, 1f, it) },
75         )
76     }
77 
78     val notificationAlpha: Flow<Float> =
79         transitionAnimation.sharedFlow(
80             duration = 500.milliseconds,
<lambda>null81             onStart = {
82                 isShadeExpanded =
83                     shadeInteractor.shadeExpansion.value > 0f ||
84                         shadeInteractor.qsExpansion.value > 0f
85             },
<lambda>null86             onStep = {
87                 if (isShadeExpanded) {
88                     1f
89                 } else {
90                     it
91                 }
92             },
93         )
94 
95     val shortcutsAlpha: Flow<Float> =
96         transitionAnimation.sharedFlow(
97             duration = 167.milliseconds,
98             startTime = 67.milliseconds,
<lambda>null99             onStep = { it },
<lambda>null100             onCancel = { 0f },
101         )
102 
103     val deviceEntryBackgroundViewAlpha: Flow<Float> =
104         transitionAnimation.sharedFlow(
105             duration = 250.milliseconds,
<lambda>null106             onStep = { it },
<lambda>null107             onFinish = { 1f },
108         )
109 
110     override val deviceEntryParentViewAlpha: Flow<Float> =
111         transitionAnimation.sharedFlow(
112             duration = 500.milliseconds,
<lambda>null113             onStep = { 1f },
114         )
115 }
116