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