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 com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.deviceentry.domain.interactor.DeviceEntryUdfpsInteractor
21 import com.android.systemui.keyguard.domain.interactor.FromGoneTransitionInteractor.Companion.TO_DOZING_DURATION
22 import com.android.systemui.keyguard.shared.model.Edge
23 import com.android.systemui.keyguard.shared.model.KeyguardState.DOZING
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 import kotlinx.coroutines.flow.emptyFlow
33 import kotlinx.coroutines.flow.flatMapLatest
34 
35 /** Breaks down GONE->DOZING transition into discrete steps for corresponding views to consume. */
36 @ExperimentalCoroutinesApi
37 @SysUISingleton
38 class GoneToDozingTransitionViewModel
39 @Inject
40 constructor(
41     deviceEntryUdfpsInteractor: DeviceEntryUdfpsInteractor,
42     animationFlow: KeyguardTransitionAnimationFlow,
43 ) : DeviceEntryIconTransition {
44 
45     private val transitionAnimation =
46         animationFlow
47             .setup(
48                 duration = TO_DOZING_DURATION,
49                 edge = Edge.create(from = Scenes.Gone, to = DOZING),
50             )
51             .setupWithoutSceneContainer(
52                 edge = Edge.create(from = GONE, to = DOZING),
53             )
54 
55     val lockscreenAlpha: Flow<Float> =
56         transitionAnimation.sharedFlow(
57             duration = 500.milliseconds,
58             onStep = { 0f },
59             onCancel = { 1f },
60             onFinish = { 1f },
61         )
62 
63     val notificationAlpha: Flow<Float> =
64         transitionAnimation.sharedFlow(
65             duration = 500.milliseconds,
66             onStep = { 1f - it },
67             // Needs to be 1f in order for HUNs to appear on AOD
68             onFinish = { 1f },
69         )
70 
71     val deviceEntryBackgroundViewAlpha: Flow<Float> =
72         transitionAnimation.immediatelyTransitionTo(0f)
73 
74     override val deviceEntryParentViewAlpha: Flow<Float> =
75         deviceEntryUdfpsInteractor.isUdfpsEnrolledAndEnabled.flatMapLatest {
76             isUdfpsEnrolledAndEnabled ->
77             if (isUdfpsEnrolledAndEnabled) {
78                 transitionAnimation.immediatelyTransitionTo(1f)
79             } else {
80                 emptyFlow()
81             }
82         }
83 }
84