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.FromAlternateBouncerTransitionInteractor.Companion.TO_GONE_DURATION
22 import com.android.systemui.keyguard.shared.model.Edge
23 import com.android.systemui.keyguard.shared.model.KeyguardState.ALTERNATE_BOUNCER
24 import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
25 import com.android.systemui.keyguard.shared.model.ScrimAlpha
26 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow
27 import com.android.systemui.keyguard.ui.transitions.DeviceEntryIconTransition
28 import com.android.systemui.scene.shared.model.Scenes
29 import com.android.systemui.statusbar.SysuiStatusBarStateController
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 ALTERNATE_BOUNCER->GONE transition into discrete steps for corresponding views to
37  * consume.
38  */
39 @OptIn(ExperimentalCoroutinesApi::class)
40 @SysUISingleton
41 class AlternateBouncerToGoneTransitionViewModel
42 @Inject
43 constructor(
44     bouncerToGoneFlows: BouncerToGoneFlows,
45     animationFlow: KeyguardTransitionAnimationFlow,
46     private val statusBarStateController: SysuiStatusBarStateController,
47 ) : DeviceEntryIconTransition {
48     private val transitionAnimation =
49         animationFlow
50             .setup(
51                 duration = TO_GONE_DURATION,
52                 edge = Edge.create(from = ALTERNATE_BOUNCER, to = Scenes.Gone),
53             )
54             .setupWithoutSceneContainer(
55                 edge = Edge.create(from = ALTERNATE_BOUNCER, to = GONE),
56             )
57 
lockscreenAlphanull58     fun lockscreenAlpha(viewState: ViewStateAccessor): Flow<Float> {
59         var startAlpha = 1f
60         return transitionAnimation.sharedFlow(
61             duration = 200.milliseconds,
62             onStart = { startAlpha = viewState.alpha() },
63             onStep = { MathUtils.lerp(startAlpha, 0f, it) },
64             onFinish = { 0f },
65             onCancel = { startAlpha },
66         )
67     }
68 
notificationAlphanull69     fun notificationAlpha(viewState: ViewStateAccessor): Flow<Float> {
70         var startAlpha = 1f
71         var leaveShadeOpen = false
72 
73         return transitionAnimation.sharedFlow(
74             duration = 200.milliseconds,
75             onStart = {
76                 leaveShadeOpen = statusBarStateController.leaveOpenOnKeyguardHide()
77                 startAlpha = viewState.alpha()
78             },
79             onStep = {
80                 if (leaveShadeOpen) {
81                     1f
82                 } else {
83                     MathUtils.lerp(startAlpha, 0f, it)
84                 }
85             },
86         )
87     }
88 
89     /** See [BouncerToGoneFlows#showAllNotifications] */
90     val showAllNotifications: Flow<Boolean> =
91         bouncerToGoneFlows.showAllNotifications(TO_GONE_DURATION, ALTERNATE_BOUNCER)
92 
93     /** Scrim alpha values */
94     val scrimAlpha: Flow<ScrimAlpha> =
95         bouncerToGoneFlows.scrimAlpha(TO_GONE_DURATION, ALTERNATE_BOUNCER)
96 
97     override val deviceEntryParentViewAlpha: Flow<Float> =
98         transitionAnimation.immediatelyTransitionTo(0f)
99 }
100