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