1 /*
<lambda>null2  * Copyright (C) 2022 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.pipeline.mobile.ui.viewmodel
18 
19 import android.graphics.Color
20 import com.android.systemui.statusbar.phone.StatusBarLocation
21 import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconInteractor
22 import com.android.systemui.statusbar.pipeline.mobile.ui.VerboseMobileViewLogger
23 import kotlinx.coroutines.CoroutineScope
24 import kotlinx.coroutines.flow.SharingStarted
25 import kotlinx.coroutines.flow.StateFlow
26 import kotlinx.coroutines.flow.combine
27 import kotlinx.coroutines.flow.stateIn
28 
29 /**
30  * A view model for an individual mobile icon that embeds the notion of a [StatusBarLocation]. This
31  * allows the mobile icon to change some view parameters at different locations
32  *
33  * @param commonImpl for convenience, this class wraps a base interface that can provides all of the
34  *   common implementations between locations. See [MobileIconViewModel]
35  * @property location the [StatusBarLocation] of this VM.
36  * @property verboseLogger an optional logger to log extremely verbose view updates.
37  */
38 abstract class LocationBasedMobileViewModel(
39     val commonImpl: MobileIconViewModelCommon,
40     val location: StatusBarLocation,
41     val verboseLogger: VerboseMobileViewLogger?,
42 ) : MobileIconViewModelCommon by commonImpl {
43     val defaultColor: Int = Color.WHITE
44 
45     companion object {
46         fun viewModelForLocation(
47             commonImpl: MobileIconViewModelCommon,
48             interactor: MobileIconInteractor,
49             verboseMobileViewLogger: VerboseMobileViewLogger,
50             location: StatusBarLocation,
51             scope: CoroutineScope,
52         ): LocationBasedMobileViewModel =
53             when (location) {
54                 StatusBarLocation.HOME ->
55                     HomeMobileIconViewModel(
56                         commonImpl,
57                         verboseMobileViewLogger,
58                     )
59                 StatusBarLocation.KEYGUARD -> KeyguardMobileIconViewModel(commonImpl)
60                 StatusBarLocation.QS -> QsMobileIconViewModel(commonImpl)
61                 StatusBarLocation.SHADE_CARRIER_GROUP ->
62                     ShadeCarrierGroupMobileIconViewModel(
63                         commonImpl,
64                         interactor,
65                         scope,
66                     )
67             }
68     }
69 }
70 
71 class HomeMobileIconViewModel(
72     commonImpl: MobileIconViewModelCommon,
73     verboseMobileViewLogger: VerboseMobileViewLogger,
74 ) :
75     MobileIconViewModelCommon,
76     LocationBasedMobileViewModel(
77         commonImpl,
78         location = StatusBarLocation.HOME,
79         verboseMobileViewLogger,
80     )
81 
82 class QsMobileIconViewModel(
83     commonImpl: MobileIconViewModelCommon,
84 ) :
85     MobileIconViewModelCommon,
86     LocationBasedMobileViewModel(
87         commonImpl,
88         location = StatusBarLocation.QS,
89         // Only do verbose logging for the Home location.
90         verboseLogger = null,
91     )
92 
93 class ShadeCarrierGroupMobileIconViewModel(
94     commonImpl: MobileIconViewModelCommon,
95     interactor: MobileIconInteractor,
96     scope: CoroutineScope,
97 ) :
98     MobileIconViewModelCommon,
99     LocationBasedMobileViewModel(
100         commonImpl,
101         location = StatusBarLocation.SHADE_CARRIER_GROUP,
102         // Only do verbose logging for the Home location.
103         verboseLogger = null,
104     ) {
105     private val isSingleCarrier = interactor.isSingleCarrier
106     val carrierName = interactor.carrierName
107 
108     override val isVisible: StateFlow<Boolean> =
isSingleCarriernull109         combine(super.isVisible, isSingleCarrier) { isVisible, isSingleCarrier ->
110                 if (isSingleCarrier) false else isVisible
111             }
112             .stateIn(scope, SharingStarted.WhileSubscribed(), super.isVisible.value)
113 }
114 
115 class KeyguardMobileIconViewModel(
116     commonImpl: MobileIconViewModelCommon,
117 ) :
118     MobileIconViewModelCommon,
119     LocationBasedMobileViewModel(
120         commonImpl,
121         location = StatusBarLocation.KEYGUARD,
122         // Only do verbose logging for the Home location.
123         verboseLogger = null,
124     )
125