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     private val _defaultMobileIconMapping = MutableStateFlow(TEST_MAPPING)
72     override val defaultMobileIconMapping = _defaultMobileIconMapping
73 
74     private val _defaultMobileIconGroup = MutableStateFlow(DEFAULT_ICON)
75     override val defaultMobileIconGroup = _defaultMobileIconGroup
76 
77     private val _isUserSetup = MutableStateFlow(true)
78     override val isUserSetup = _isUserSetup
79 
80     override val isForceHidden = MutableStateFlow(false)
81 
82     /** Always returns a new fake interactor */
83     override fun getMobileConnectionInteractorForSubId(subId: Int): MobileIconInteractor {
84         return FakeMobileIconInteractor(tableLogBuffer).also { interactorCache[subId] = it }
85     }
86 
87     /**
88      * Returns the most recently created interactor for the given subId, or null if an interactor
89      * has never been created for that sub.
90      */
91     fun getInteractorForSubId(subId: Int): FakeMobileIconInteractor? {
92         return interactorCache[subId]
93     }
94 
95     companion object {
96         val DEFAULT_ICON = TelephonyIcons.G
97 
98         const val DEFAULT_DATA_SUB_ID = 1
99 
100         // Use [MobileMappings] to define some simple definitions
101         const val THREE_G = NETWORK_TYPE_GSM
102         const val LTE = NETWORK_TYPE_LTE
103         const val FOUR_G = NETWORK_TYPE_UMTS
104         const val FIVE_G_OVERRIDE = OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
105     }
106 }
107