1 /*
2  * Copyright (C) 2020 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 package com.android.settingslib.mobile;
17 
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.os.PersistableBundle;
21 import android.telephony.Annotation;
22 import android.telephony.CarrierConfigManager;
23 import android.telephony.SubscriptionManager;
24 import android.telephony.TelephonyDisplayInfo;
25 import android.telephony.TelephonyManager;
26 
27 import com.android.settingslib.R;
28 import com.android.settingslib.SignalIcon.MobileIconGroup;
29 
30 import java.util.HashMap;
31 import java.util.Map;
32 
33 /**
34  * Holds the utility functions to create the RAT to MobileIconGroup mappings.
35  */
36 public class MobileMappings {
37 
38     /**
39      * Generates the RAT key from the TelephonyDisplayInfo.
40      */
getIconKey(TelephonyDisplayInfo telephonyDisplayInfo)41     public static String getIconKey(TelephonyDisplayInfo telephonyDisplayInfo) {
42         if (telephonyDisplayInfo.getOverrideNetworkType()
43                 == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE) {
44             return toIconKey(telephonyDisplayInfo.getNetworkType());
45         } else {
46             return toDisplayIconKey(telephonyDisplayInfo.getOverrideNetworkType());
47         }
48     }
49 
50     /**
51      * Converts the networkType into the RAT key.
52      */
toIconKey(@nnotation.NetworkType int networkType)53     public static String toIconKey(@Annotation.NetworkType int networkType) {
54         return Integer.toString(networkType);
55     }
56 
57     /**
58      * Converts the displayNetworkType into the RAT key.
59      */
toDisplayIconKey(@nnotation.OverrideNetworkType int displayNetworkType)60     public static String toDisplayIconKey(@Annotation.OverrideNetworkType int displayNetworkType) {
61         switch (displayNetworkType) {
62             case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA:
63                 return toIconKey(TelephonyManager.NETWORK_TYPE_LTE) + "_CA";
64             case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO:
65                 return toIconKey(TelephonyManager.NETWORK_TYPE_LTE) + "_CA_Plus";
66             case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA:
67                 return toIconKey(TelephonyManager.NETWORK_TYPE_NR);
68             case TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED:
69                 return toIconKey(TelephonyManager.NETWORK_TYPE_NR) + "_Plus";
70             default:
71                 return "unsupported";
72         }
73     }
74 
75     /**
76      * Produce the default MobileIconGroup.
77      */
getDefaultIcons(Config config)78     public static MobileIconGroup getDefaultIcons(Config config) {
79         if (!config.showAtLeast3G) {
80             return TelephonyIcons.G;
81         } else {
82             return TelephonyIcons.THREE_G;
83         }
84     }
85 
86     /**
87      * Produce a mapping of data network types to icon groups for simple and quick use in
88      * updateTelephony.
89      */
mapIconSets(Config config)90     public static Map<String, MobileIconGroup> mapIconSets(Config config) {
91         final Map<String, MobileIconGroup> networkToIconLookup = new HashMap<>();
92 
93         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EVDO_0),
94                 TelephonyIcons.THREE_G);
95         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EVDO_A),
96                 TelephonyIcons.THREE_G);
97         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EVDO_B),
98                 TelephonyIcons.THREE_G);
99         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EHRPD),
100                 TelephonyIcons.THREE_G);
101         if (config.show4gFor3g) {
102             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_UMTS),
103                     TelephonyIcons.FOUR_G);
104         } else {
105             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_UMTS),
106                     TelephonyIcons.THREE_G);
107         }
108         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_TD_SCDMA),
109                 TelephonyIcons.THREE_G);
110 
111         if (!config.showAtLeast3G) {
112             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_UNKNOWN),
113                     TelephonyIcons.UNKNOWN);
114             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EDGE),
115                     TelephonyIcons.E);
116             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_GPRS),
117                     TelephonyIcons.G);
118             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_CDMA),
119                     TelephonyIcons.ONE_X);
120             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_1xRTT),
121                     TelephonyIcons.ONE_X);
122         } else {
123             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_UNKNOWN),
124                     TelephonyIcons.THREE_G);
125             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EDGE),
126                     TelephonyIcons.THREE_G);
127             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_GPRS),
128                     TelephonyIcons.THREE_G);
129             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_CDMA),
130                     TelephonyIcons.THREE_G);
131             networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_1xRTT),
132                     TelephonyIcons.THREE_G);
133         }
134 
135         MobileIconGroup hGroup = TelephonyIcons.THREE_G;
136         MobileIconGroup hPlusGroup = TelephonyIcons.THREE_G;
137         if (config.show4gFor3g) {
138             hGroup = TelephonyIcons.FOUR_G;
139             hPlusGroup = TelephonyIcons.FOUR_G;
140         } else if (config.hspaDataDistinguishable) {
141             hGroup = TelephonyIcons.H;
142             hPlusGroup = TelephonyIcons.H_PLUS;
143         }
144 
145         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSDPA), hGroup);
146         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSUPA), hGroup);
147         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSPA), hGroup);
148         networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSPAP), hPlusGroup);
149 
150         if (config.show4gForLte) {
151             networkToIconLookup.put(toIconKey(
152                     TelephonyManager.NETWORK_TYPE_LTE),
153                     TelephonyIcons.FOUR_G);
154             if (config.hideLtePlus) {
155                 networkToIconLookup.put(toDisplayIconKey(
156                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
157                         TelephonyIcons.FOUR_G);
158             } else {
159                 networkToIconLookup.put(toDisplayIconKey(
160                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
161                         TelephonyIcons.FOUR_G_PLUS);
162             }
163         } else if (config.show4glteForLte) {
164             networkToIconLookup.put(toIconKey(
165                     TelephonyManager.NETWORK_TYPE_LTE),
166                     TelephonyIcons.FOUR_G_LTE);
167             if (config.hideLtePlus) {
168                 networkToIconLookup.put(toDisplayIconKey(
169                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
170                         TelephonyIcons.FOUR_G_LTE);
171             } else {
172                 networkToIconLookup.put(toDisplayIconKey(
173                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
174                         TelephonyIcons.FOUR_G_LTE_PLUS);
175             }
176         } else {
177             networkToIconLookup.put(toIconKey(
178                     TelephonyManager.NETWORK_TYPE_LTE),
179                     TelephonyIcons.LTE);
180             if (config.hideLtePlus) {
181                 networkToIconLookup.put(toDisplayIconKey(
182                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
183                         TelephonyIcons.LTE);
184             } else {
185                 networkToIconLookup.put(toDisplayIconKey(
186                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
187                         TelephonyIcons.LTE_PLUS);
188             }
189         }
190         networkToIconLookup.put(toIconKey(
191                 TelephonyManager.NETWORK_TYPE_IWLAN),
192                 TelephonyIcons.WFC);
193         networkToIconLookup.put(toDisplayIconKey(
194                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO),
195                 TelephonyIcons.LTE_CA_5G_E);
196         networkToIconLookup.put(toDisplayIconKey(
197                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA),
198                 TelephonyIcons.NR_5G);
199         networkToIconLookup.put(toDisplayIconKey(
200                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED),
201                 TelephonyIcons.NR_5G_PLUS);
202         networkToIconLookup.put(toIconKey(
203                 TelephonyManager.NETWORK_TYPE_NR),
204                 TelephonyIcons.NR_5G);
205         return networkToIconLookup;
206     }
207 
208     /**
209      * Wrapper class of system configs and Carrier configs.
210      */
211     public static class Config {
212         public boolean showAtLeast3G = false;
213         public boolean show4gFor3g = false;
214         public boolean alwaysShowCdmaRssi = false;
215         public boolean show4gForLte = false;
216         public boolean show4glteForLte = false;
217         public boolean hideLtePlus = false;
218         public boolean hspaDataDistinguishable;
219         public boolean alwaysShowDataRatIcon = false;
220 
221         /**
222          * Reads the latest configs.
223          */
readConfig(Context context)224         public static Config readConfig(Context context) {
225             Config config = new Config();
226             Resources res = context.getResources();
227 
228             config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G);
229             config.alwaysShowCdmaRssi =
230                     res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi);
231             config.hspaDataDistinguishable =
232                     res.getBoolean(R.bool.config_hspa_data_distinguishable);
233 
234             CarrierConfigManager configMgr = (CarrierConfigManager)
235                     context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
236             // Handle specific carrier config values for the default data SIM
237             int defaultDataSubId = SubscriptionManager.from(context)
238                     .getDefaultDataSubscriptionId();
239             PersistableBundle b = configMgr == null ? null
240                         : configMgr.getConfigForSubId(defaultDataSubId);
241             if (b != null) {
242                 config.alwaysShowDataRatIcon = b.getBoolean(
243                         CarrierConfigManager.KEY_ALWAYS_SHOW_DATA_RAT_ICON_BOOL);
244                 config.show4gForLte = b.getBoolean(
245                         CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL);
246                 config.show4glteForLte = b.getBoolean(
247                         CarrierConfigManager.KEY_SHOW_4GLTE_FOR_LTE_DATA_ICON_BOOL);
248                 config.show4gFor3g = b.getBoolean(
249                         CarrierConfigManager.KEY_SHOW_4G_FOR_3G_DATA_ICON_BOOL);
250                 config.hideLtePlus = b.getBoolean(
251                         CarrierConfigManager.KEY_HIDE_LTE_PLUS_DATA_ICON_BOOL);
252             }
253             return config;
254         }
255 
256         /**
257          * Returns true if this config and the other config are semantically equal.
258          *
259          * Does not override isEquals because existing clients may be relying on the currently
260          * defined equals behavior.
261          */
areEqual(Config other)262         public boolean areEqual(Config other) {
263             return showAtLeast3G == other.showAtLeast3G
264                     && show4gFor3g == other.show4gFor3g
265                     && alwaysShowCdmaRssi == other.alwaysShowCdmaRssi
266                     && show4gForLte == other.show4gForLte
267                     && show4glteForLte == other.show4glteForLte
268                     && hideLtePlus == other.hideLtePlus
269                     && hspaDataDistinguishable == other.hspaDataDistinguishable
270                     && alwaysShowDataRatIcon == other.alwaysShowDataRatIcon;
271         }
272     }
273 }
274