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.statusbar.ui.viewmodel
18 
19 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
23 import com.android.systemui.keyguard.shared.model.StatusBarState
24 import com.android.systemui.statusbar.domain.interactor.KeyguardStatusBarInteractor
25 import com.android.systemui.statusbar.notification.domain.interactor.HeadsUpNotificationInteractor
26 import com.android.systemui.statusbar.policy.BatteryController
27 import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback
28 import javax.inject.Inject
29 import kotlinx.coroutines.CoroutineScope
30 import kotlinx.coroutines.channels.awaitClose
31 import kotlinx.coroutines.flow.Flow
32 import kotlinx.coroutines.flow.SharingStarted
33 import kotlinx.coroutines.flow.StateFlow
34 import kotlinx.coroutines.flow.combine
35 import kotlinx.coroutines.flow.stateIn
36 
37 /**
38  * A view model for the status bar displayed on keyguard (lockscreen).
39  *
40  * Note: This view model is for the status bar view as a whole. Certain icons may have their own
41  * individual view models, such as
42  * [com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.WifiViewModel] or
43  * [com.android.systemui.statusbar.pipeline.mobile.ui.viewmodel.MobileIconsViewModel].
44  */
45 @SysUISingleton
46 class KeyguardStatusBarViewModel
47 @Inject
48 constructor(
49     @Application scope: CoroutineScope,
50     headsUpNotificationInteractor: HeadsUpNotificationInteractor,
51     keyguardInteractor: KeyguardInteractor,
52     keyguardStatusBarInteractor: KeyguardStatusBarInteractor,
53     batteryController: BatteryController,
54 ) {
55     /** True if this view should be visible and false otherwise. */
56     val isVisible: StateFlow<Boolean> =
57         combine(
58                 keyguardInteractor.isDozing,
59                 keyguardInteractor.statusBarState,
60                 headsUpNotificationInteractor.showHeadsUpStatusBar,
61             ) { isDozing, statusBarState, showHeadsUpStatusBar ->
62                 !isDozing && statusBarState == StatusBarState.KEYGUARD && !showHeadsUpStatusBar
63             }
64             .stateIn(scope, SharingStarted.WhileSubscribed(), false)
65 
66     /** True if the device's battery is currently charging and false otherwise. */
67     // Note: Never make this an eagerly-started state flow so that the callback is removed when the
68     // keyguard status bar view isn't attached.
69     val isBatteryCharging: Flow<Boolean> = conflatedCallbackFlow {
70         val callback =
71             object : BatteryStateChangeCallback {
72                 override fun onBatteryLevelChanged(
73                     level: Int,
74                     pluggedIn: Boolean,
75                     charging: Boolean,
76                 ) {
77                     trySend(charging)
78                 }
79             }
80         batteryController.addCallback(callback)
81         awaitClose { batteryController.removeCallback(callback) }
82     }
83 
84     /** True if we can show the user switcher on keyguard and false otherwise. */
85     val isKeyguardUserSwitcherEnabled: Flow<Boolean> =
86         keyguardStatusBarInteractor.isKeyguardUserSwitcherEnabled
87 }
88