• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MobileConnectivityModel
27 import com.android.systemui.statusbar.pipeline.mobile.data.model.SubscriptionModel
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,
35     val tableLogBuffer: TableLogBuffer,
36 ) : MobileConnectionsRepository {
37     val GSM_KEY = mobileMappings.toIconKey(GSM)
38     val LTE_KEY = mobileMappings.toIconKey(LTE)
39     val UMTS_KEY = mobileMappings.toIconKey(UMTS)
40     val LTE_ADVANCED_KEY = mobileMappings.toIconKeyOverride(LTE_ADVANCED_PRO)
41 
42     /**
43      * To avoid a reliance on [MobileMappings], we'll build a simpler map from network type to
44      * mobile icon. See TelephonyManager.NETWORK_TYPES for a list of types and [TelephonyIcons] for
45      * the exhaustive set of icons
46      */
47     val TEST_MAPPING: Map<String, SignalIcon.MobileIconGroup> =
48         mapOf(
49             GSM_KEY to TelephonyIcons.THREE_G,
50             LTE_KEY to TelephonyIcons.LTE,
51             UMTS_KEY to TelephonyIcons.FOUR_G,
52             LTE_ADVANCED_KEY to TelephonyIcons.NR_5G,
53         )
54 
55     private val _subscriptions = MutableStateFlow<List<SubscriptionModel>>(listOf())
56     override val subscriptions = _subscriptions
57 
58     private val _activeMobileDataSubscriptionId = MutableStateFlow<Int?>(null)
59     override val activeMobileDataSubscriptionId = _activeMobileDataSubscriptionId
60 
61     private val _activeMobileRepository = MutableStateFlow<MobileConnectionRepository?>(null)
62     override val activeMobileDataRepository = _activeMobileRepository
63 
64     override val activeSubChangedInGroupEvent: MutableSharedFlow<Unit> = MutableSharedFlow()
65 
66     private val _defaultDataSubId = MutableStateFlow(INVALID_SUBSCRIPTION_ID)
67     override val defaultDataSubId = _defaultDataSubId
68 
69     private val _mobileConnectivity = MutableStateFlow(MobileConnectivityModel())
70     override val defaultMobileNetworkConnectivity = _mobileConnectivity
71 
72     private val subIdRepos = mutableMapOf<Int, MobileConnectionRepository>()
73 
74     override fun getRepoForSubId(subId: Int): MobileConnectionRepository {
75         return subIdRepos[subId]
76             ?: FakeMobileConnectionRepository(subId, tableLogBuffer).also { subIdRepos[subId] = it }
77     }
78 
79     override val defaultDataSubRatConfig = MutableStateFlow(MobileMappings.Config())
80 
81     private val _defaultMobileIconMapping = MutableStateFlow(TEST_MAPPING)
82     override val defaultMobileIconMapping = _defaultMobileIconMapping
83 
84     private val _defaultMobileIconGroup = MutableStateFlow(DEFAULT_ICON)
85     override val defaultMobileIconGroup = _defaultMobileIconGroup
86 
87     fun setSubscriptions(subs: List<SubscriptionModel>) {
88         _subscriptions.value = subs
89     }
90 
91     fun setDefaultDataSubId(id: Int) {
92         _defaultDataSubId.value = id
93     }
94 
95     fun setMobileConnectivity(model: MobileConnectivityModel) {
96         _mobileConnectivity.value = model
97     }
98 
99     fun setActiveMobileDataSubscriptionId(subId: Int) {
100         // Simulate the filtering that the repo does
101         if (subId == INVALID_SUBSCRIPTION_ID) {
102             _activeMobileDataSubscriptionId.value = null
103             _activeMobileRepository.value = null
104         } else {
105             _activeMobileDataSubscriptionId.value = subId
106             _activeMobileRepository.value = getRepoForSubId(subId)
107         }
108     }
109 
110     fun setMobileConnectionRepositoryMap(connections: Map<Int, MobileConnectionRepository>) {
111         connections.forEach { entry -> subIdRepos[entry.key] = entry.value }
112     }
113 
114     companion object {
115         val DEFAULT_ICON = TelephonyIcons.G
116 
117         // Use [MobileMappings] to define some simple definitions
118         const val GSM = TelephonyManager.NETWORK_TYPE_GSM
119         const val LTE = TelephonyManager.NETWORK_TYPE_LTE
120         const val UMTS = TelephonyManager.NETWORK_TYPE_UMTS
121         const val LTE_ADVANCED_PRO = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
122     }
123 }
124