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.data.repository
18 
19 import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
20 import android.telephony.TelephonyDisplayInfo
21 import android.telephony.TelephonyManager
22 import com.android.settingslib.SignalIcon
23 import com.android.settingslib.mobile.MobileMappings
24 import com.android.settingslib.mobile.TelephonyIcons
25 import com.android.systemui.log.table.TableLogBuffer
26 import com.android.systemui.statusbar.pipeline.mobile.data.model.SubscriptionModel
27 import com.android.systemui.statusbar.pipeline.mobile.util.FakeMobileMappingsProxy
28 import com.android.systemui.statusbar.pipeline.mobile.util.MobileMappingsProxy
29 import kotlinx.coroutines.flow.MutableSharedFlow
30 import kotlinx.coroutines.flow.MutableStateFlow
31 
32 // TODO(b/261632894): remove this in favor of the real impl or DemoMobileConnectionsRepository
33 class FakeMobileConnectionsRepository(
34     mobileMappings: MobileMappingsProxy = FakeMobileMappingsProxy(),
35     val tableLogBuffer: TableLogBuffer,
36 ) : MobileConnectionsRepository {
37 
38     val GSM_KEY = mobileMappings.toIconKey(GSM)
39     val LTE_KEY = mobileMappings.toIconKey(LTE)
40     val UMTS_KEY = mobileMappings.toIconKey(UMTS)
41     val LTE_ADVANCED_KEY = mobileMappings.toIconKeyOverride(LTE_ADVANCED_PRO)
42 
43     /**
44      * To avoid a reliance on [MobileMappings], we'll build a simpler map from network type to
45      * mobile icon. See TelephonyManager.NETWORK_TYPES for a list of types and [TelephonyIcons] for
46      * the exhaustive set of icons
47      */
48     val TEST_MAPPING: Map<String, SignalIcon.MobileIconGroup> =
49         mapOf(
50             GSM_KEY to TelephonyIcons.THREE_G,
51             LTE_KEY to TelephonyIcons.LTE,
52             UMTS_KEY to TelephonyIcons.FOUR_G,
53             LTE_ADVANCED_KEY to TelephonyIcons.NR_5G,
54         )
55 
56     private val _subscriptions = MutableStateFlow<List<SubscriptionModel>>(listOf())
57     override val subscriptions = _subscriptions
58 
59     private val _activeMobileDataSubscriptionId = MutableStateFlow<Int?>(null)
60     override val activeMobileDataSubscriptionId = _activeMobileDataSubscriptionId
61 
62     private val _activeMobileRepository = MutableStateFlow<MobileConnectionRepository?>(null)
63     override val activeMobileDataRepository = _activeMobileRepository
64 
65     override val activeSubChangedInGroupEvent: MutableSharedFlow<Unit> = MutableSharedFlow()
66 
67     private val _defaultDataSubId = MutableStateFlow(INVALID_SUBSCRIPTION_ID)
68     override val defaultDataSubId = _defaultDataSubId
69 
70     override val mobileIsDefault = MutableStateFlow(false)
71 
72     override val hasCarrierMergedConnection = MutableStateFlow(false)
73 
74     override val defaultConnectionIsValidated = MutableStateFlow(false)
75 
76     private val subIdRepos = mutableMapOf<Int, MobileConnectionRepository>()
77 
78     override fun getRepoForSubId(subId: Int): MobileConnectionRepository {
79         return subIdRepos[subId]
80             ?: FakeMobileConnectionRepository(subId, tableLogBuffer).also { subIdRepos[subId] = it }
81     }
82 
83     override val defaultDataSubRatConfig = MutableStateFlow(MobileMappings.Config())
84 
85     private val _defaultMobileIconMapping = MutableStateFlow(TEST_MAPPING)
86     override val defaultMobileIconMapping = _defaultMobileIconMapping
87 
88     private val _defaultMobileIconGroup = MutableStateFlow(DEFAULT_ICON)
89     override val defaultMobileIconGroup = _defaultMobileIconGroup
90 
91     override val isDeviceEmergencyCallCapable = MutableStateFlow(false)
92 
93     override val isAnySimSecure = MutableStateFlow(false)
94 
95     override fun getIsAnySimSecure(): Boolean = isAnySimSecure.value
96 
97     private var isInEcmMode: Boolean = false
98 
99     fun setSubscriptions(subs: List<SubscriptionModel>) {
100         _subscriptions.value = subs
101     }
102 
103     fun setActiveMobileDataSubscriptionId(subId: Int) {
104         // Simulate the filtering that the repo does
105         if (subId == INVALID_SUBSCRIPTION_ID) {
106             _activeMobileDataSubscriptionId.value = null
107             _activeMobileRepository.value = null
108         } else {
109             _activeMobileDataSubscriptionId.value = subId
110             _activeMobileRepository.value = getRepoForSubId(subId)
111         }
112     }
113 
114     fun setMobileConnectionRepositoryMap(connections: Map<Int, MobileConnectionRepository>) {
115         connections.forEach { entry -> subIdRepos[entry.key] = entry.value }
116     }
117 
118     fun setIsInEcmState(isInEcmState: Boolean) {
119         this.isInEcmMode = isInEcmState
120     }
121 
122     override suspend fun isInEcmMode(): Boolean = isInEcmMode
123 
124     companion object {
125         val DEFAULT_ICON = TelephonyIcons.G
126 
127         // Use [MobileMappings] to define some simple definitions
128         const val GSM = TelephonyManager.NETWORK_TYPE_GSM
129         const val LTE = TelephonyManager.NETWORK_TYPE_LTE
130         const val UMTS = TelephonyManager.NETWORK_TYPE_UMTS
131         const val LTE_ADVANCED_PRO = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
132     }
133 }
134 
135 val MobileConnectionsRepository.fake
136     get() = this as FakeMobileConnectionsRepository
137