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.domain.interactor
18 
19 import android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
20 import android.telephony.TelephonyManager.NETWORK_TYPE_GSM
21 import android.telephony.TelephonyManager.NETWORK_TYPE_LTE
22 import android.telephony.TelephonyManager.NETWORK_TYPE_UMTS
23 import com.android.settingslib.SignalIcon.MobileIconGroup
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.MutableStateFlow
29 
30 class FakeMobileIconsInteractor(
31     mobileMappings: MobileMappingsProxy,
32     val tableLogBuffer: TableLogBuffer,
33 ) : MobileIconsInteractor {
34     val THREE_G_KEY = mobileMappings.toIconKey(THREE_G)
35     val LTE_KEY = mobileMappings.toIconKey(LTE)
36     val FOUR_G_KEY = mobileMappings.toIconKey(FOUR_G)
37     val FIVE_G_OVERRIDE_KEY = mobileMappings.toIconKeyOverride(FIVE_G_OVERRIDE)
38 
39     /**
40      * To avoid a reliance on [MobileMappings], we'll build a simpler map from network type to
41      * mobile icon. See TelephonyManager.NETWORK_TYPES for a list of types and [TelephonyIcons] for
42      * the exhaustive set of icons
43      */
44     val TEST_MAPPING: Map<String, MobileIconGroup> =
45         mapOf(
46             THREE_G_KEY to TelephonyIcons.THREE_G,
47             LTE_KEY to TelephonyIcons.LTE,
48             FOUR_G_KEY to TelephonyIcons.FOUR_G,
49             FIVE_G_OVERRIDE_KEY to TelephonyIcons.NR_5G,
50         )
51 
52     private val interactorCache: MutableMap<Int, FakeMobileIconInteractor> = mutableMapOf()
53 
54     override val isDefaultConnectionFailed = MutableStateFlow(false)
55 
56     override val filteredSubscriptions = MutableStateFlow<List<SubscriptionModel>>(listOf())
57 
58     private val _activeDataConnectionHasDataEnabled = MutableStateFlow(false)
59     override val activeDataConnectionHasDataEnabled = _activeDataConnectionHasDataEnabled
60 
61     override val activeDataIconInteractor = MutableStateFlow(null)
62 
63     override val alwaysShowDataRatIcon = MutableStateFlow(false)
64 
65     override val alwaysUseCdmaLevel = MutableStateFlow(false)
66 
67     override val mobileIsDefault = MutableStateFlow(false)
68 
69     override val isSingleCarrier = MutableStateFlow(true)
70 
71     override val icons: MutableStateFlow<List<MobileIconInteractor>> = MutableStateFlow(emptyList())
72 
73     private val _defaultMobileIconMapping = MutableStateFlow(TEST_MAPPING)
74     override val defaultMobileIconMapping = _defaultMobileIconMapping
75 
76     private val _defaultMobileIconGroup = MutableStateFlow(DEFAULT_ICON)
77     override val defaultMobileIconGroup = _defaultMobileIconGroup
78 
79     private val _isUserSetUp = MutableStateFlow(true)
80     override val isUserSetUp = _isUserSetUp
81 
82     override val isForceHidden = MutableStateFlow(false)
83 
84     override val isDeviceInEmergencyCallsOnlyMode = MutableStateFlow(false)
85 
86     /** Always returns a new fake interactor */
getMobileConnectionInteractorForSubIdnull87     override fun getMobileConnectionInteractorForSubId(subId: Int): FakeMobileIconInteractor {
88         return FakeMobileIconInteractor(tableLogBuffer).also {
89             interactorCache[subId] = it
90             // Also update the icons
91             icons.value = interactorCache.values.toList()
92         }
93     }
94 
95     /**
96      * Returns the most recently created interactor for the given subId, or null if an interactor
97      * has never been created for that sub.
98      */
getInteractorForSubIdnull99     fun getInteractorForSubId(subId: Int): FakeMobileIconInteractor? {
100         return interactorCache[subId]
101     }
102 
103     companion object {
104         val DEFAULT_ICON = TelephonyIcons.G
105 
106         const val DEFAULT_DATA_SUB_ID = 1
107 
108         // Use [MobileMappings] to define some simple definitions
109         const val THREE_G = NETWORK_TYPE_GSM
110         const val LTE = NETWORK_TYPE_LTE
111         const val FOUR_G = NETWORK_TYPE_UMTS
112         const val FIVE_G_OVERRIDE = OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
113     }
114 }
115