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 
18 package com.android.systemui.keyguard.ui.viewmodel
19 
20 import android.content.Context
21 import com.android.settingslib.Utils
22 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractor
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
25 import com.android.systemui.keyguard.shared.model.KeyguardState
26 import javax.inject.Inject
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.flatMapLatest
30 import kotlinx.coroutines.flow.flowOf
31 import kotlinx.coroutines.flow.map
32 import kotlinx.coroutines.flow.merge
33 import kotlinx.coroutines.flow.onStart
34 
35 /** Models the UI state for the device entry icon background view. */
36 @Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA")
37 @ExperimentalCoroutinesApi
38 @SysUISingleton
39 class DeviceEntryBackgroundViewModel
40 @Inject
41 constructor(
42     val context: Context,
43     val deviceEntryIconViewModel: DeviceEntryIconViewModel,
44     keyguardTransitionInteractor: KeyguardTransitionInteractor,
45     configurationInteractor: ConfigurationInteractor,
46     lockscreenToAodTransitionViewModel: LockscreenToAodTransitionViewModel,
47     aodToLockscreenTransitionViewModel: AodToLockscreenTransitionViewModel,
48     goneToAodTransitionViewModel: GoneToAodTransitionViewModel,
49     primaryBouncerToAodTransitionViewModel: PrimaryBouncerToAodTransitionViewModel,
50     occludedToAodTransitionViewModel: OccludedToAodTransitionViewModel,
51     occludedToLockscreenTransitionViewModel: OccludedToLockscreenTransitionViewModel,
52     dreamingToLockscreenTransitionViewModel: DreamingToLockscreenTransitionViewModel,
53     alternateBouncerToAodTransitionViewModel: AlternateBouncerToAodTransitionViewModel,
54     goneToLockscreenTransitionViewModel: GoneToLockscreenTransitionViewModel,
55     goneToDozingTransitionViewModel: GoneToDozingTransitionViewModel,
56     primaryBouncerToDozingTransitionViewModel: PrimaryBouncerToDozingTransitionViewModel,
57     lockscreenToDozingTransitionViewModel: LockscreenToDozingTransitionViewModel,
58     dozingToLockscreenTransitionViewModel: DozingToLockscreenTransitionViewModel,
59     alternateBouncerToDozingTransitionViewModel: AlternateBouncerToDozingTransitionViewModel,
60     dreamingToAodTransitionViewModel: DreamingToAodTransitionViewModel,
61     primaryBouncerToLockscreenTransitionViewModel: PrimaryBouncerToLockscreenTransitionViewModel,
62 ) {
63     val color: Flow<Int> =
64         deviceEntryIconViewModel.useBackgroundProtection.flatMapLatest { useBackground ->
65             if (useBackground) {
66                 configurationInteractor.onAnyConfigurationChange
67                     .map {
68                         Utils.getColorAttrDefaultColor(
69                             context,
70                             com.android.internal.R.attr.colorSurface
71                         )
72                     }
73                     .onStart {
74                         emit(
75                             Utils.getColorAttrDefaultColor(
76                                 context,
77                                 com.android.internal.R.attr.colorSurface
78                             )
79                         )
80                     }
81             } else {
82                 flowOf(0)
83             }
84         }
85     val alpha: Flow<Float> =
86         deviceEntryIconViewModel.useBackgroundProtection.flatMapLatest { useBackground ->
87             if (useBackground) {
88                 setOf(
89                         lockscreenToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
90                         aodToLockscreenTransitionViewModel.deviceEntryBackgroundViewAlpha,
91                         goneToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
92                         primaryBouncerToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
93                         occludedToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
94                         occludedToLockscreenTransitionViewModel.deviceEntryBackgroundViewAlpha,
95                         dreamingToLockscreenTransitionViewModel.deviceEntryBackgroundViewAlpha,
96                         alternateBouncerToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
97                         goneToLockscreenTransitionViewModel.deviceEntryBackgroundViewAlpha,
98                         goneToDozingTransitionViewModel.deviceEntryBackgroundViewAlpha,
99                         primaryBouncerToDozingTransitionViewModel.deviceEntryBackgroundViewAlpha,
100                         lockscreenToDozingTransitionViewModel.deviceEntryBackgroundViewAlpha,
101                         dozingToLockscreenTransitionViewModel.deviceEntryBackgroundViewAlpha,
102                         alternateBouncerToDozingTransitionViewModel.deviceEntryBackgroundViewAlpha,
103                         dreamingToAodTransitionViewModel.deviceEntryBackgroundViewAlpha,
104                         primaryBouncerToLockscreenTransitionViewModel
105                             .deviceEntryBackgroundViewAlpha,
106                     )
107                     .merge()
108                     .onStart {
109                         when (
110                             keyguardTransitionInteractor.currentKeyguardState.replayCache.last()
111                         ) {
112                             KeyguardState.GLANCEABLE_HUB,
113                             KeyguardState.DREAMING_LOCKSCREEN_HOSTED,
114                             KeyguardState.GONE,
115                             KeyguardState.OCCLUDED,
116                             KeyguardState.OFF,
117                             KeyguardState.DOZING,
118                             KeyguardState.DREAMING,
119                             KeyguardState.PRIMARY_BOUNCER,
120                             KeyguardState.AOD,
121                             KeyguardState.UNDEFINED -> emit(0f)
122                             KeyguardState.ALTERNATE_BOUNCER,
123                             KeyguardState.LOCKSCREEN -> emit(1f)
124                         }
125                     }
126             } else {
127                 flowOf(0f)
128             }
129         }
130 }
131