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 @file:OptIn(ExperimentalCoroutinesApi::class)
18 
19 package com.android.systemui.keyguard.ui.viewmodel
20 
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.keyguard.MigrateClocksToBlueprint
23 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
24 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
25 import com.android.systemui.keyguard.shared.model.KeyguardState.AOD
26 import com.android.systemui.keyguard.shared.model.KeyguardState.DOZING
27 import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
28 import com.android.systemui.keyguard.shared.model.KeyguardState.UNDEFINED
29 import com.android.systemui.scene.domain.interactor.SceneInteractor
30 import com.android.systemui.scene.shared.flag.SceneContainerFlag
31 import com.android.systemui.scene.shared.model.Scenes
32 import javax.inject.Inject
33 import kotlinx.coroutines.ExperimentalCoroutinesApi
34 import kotlinx.coroutines.flow.Flow
35 import kotlinx.coroutines.flow.combineTransform
36 import kotlinx.coroutines.flow.onStart
37 
38 /** Models UI state for the alpha of the AOD (always-on display). */
39 @SysUISingleton
40 class AodAlphaViewModel
41 @Inject
42 constructor(
43     keyguardTransitionInteractor: KeyguardTransitionInteractor,
44     goneToAodTransitionViewModel: GoneToAodTransitionViewModel,
45     goneToDozingTransitionViewModel: GoneToDozingTransitionViewModel,
46     keyguardInteractor: KeyguardInteractor,
47     sceneInteractor: SceneInteractor,
48 ) {
49 
50     /** The alpha level for the entire lockscreen while in AOD. */
51     val alpha: Flow<Float> =
52         if (SceneContainerFlag.isEnabled) {
53             combineTransform(
54                 keyguardTransitionInteractor.transitions,
55                 sceneInteractor.transitionState,
56                 goneToAodTransitionViewModel.enterFromTopAnimationAlpha.onStart { emit(0f) },
57                 goneToDozingTransitionViewModel.lockscreenAlpha.onStart { emit(0f) },
58                 keyguardInteractor.keyguardAlpha.onStart { emit(1f) },
59             ) { step, sceneTransitionState, goneToAodAlpha, goneToDozingAlpha, keyguardAlpha ->
60                 if (sceneTransitionState.isIdle(Scenes.Gone)) {
61                     emit(0f)
62                 } else if (
63                     step.from == UNDEFINED &&
64                         step.to == AOD &&
65                         sceneTransitionState.isTransitioning(Scenes.Gone, Scenes.Lockscreen)
66                 ) {
67                     emit(goneToAodAlpha)
68                 } else if (
69                     step.from == UNDEFINED &&
70                         step.to == DOZING &&
71                         sceneTransitionState.isTransitioning(Scenes.Gone, Scenes.Lockscreen)
72                 ) {
73                     emit(goneToDozingAlpha)
74                 } else if (!MigrateClocksToBlueprint.isEnabled) {
75                     emit(keyguardAlpha)
76                 }
77             }
78         } else {
79             combineTransform(
80                 keyguardTransitionInteractor.transitions,
81                 goneToAodTransitionViewModel.enterFromTopAnimationAlpha.onStart { emit(0f) },
82                 goneToDozingTransitionViewModel.lockscreenAlpha.onStart { emit(0f) },
83                 keyguardInteractor.keyguardAlpha.onStart { emit(1f) },
84             ) { step, goneToAodAlpha, goneToDozingAlpha, keyguardAlpha ->
85                 if (step.to == GONE) {
86                     // When transitioning to GONE, only emit a value when complete as other
87                     // transitions may be controlling the alpha fade
88                     if (step.value == 1f) {
89                         emit(0f)
90                     }
91                 } else if (step.from == GONE && step.to == AOD) {
92                     emit(goneToAodAlpha)
93                 } else if (step.from == GONE && step.to == DOZING) {
94                     emit(goneToDozingAlpha)
95                 } else if (!MigrateClocksToBlueprint.isEnabled) {
96                     emit(keyguardAlpha)
97                 }
98             }
99         }
100 }
101