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 {
164             networkToIconLookup.put(toIconKey(
165                     TelephonyManager.NETWORK_TYPE_LTE),
166                     TelephonyIcons.LTE);
167             if (config.hideLtePlus) {
168                 networkToIconLookup.put(toDisplayIconKey(
169                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
170                         TelephonyIcons.LTE);
171             } else {
172                 networkToIconLookup.put(toDisplayIconKey(
173                         TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA),
174                         TelephonyIcons.LTE_PLUS);
175             }
176         }
177         networkToIconLookup.put(toIconKey(
178                 TelephonyManager.NETWORK_TYPE_IWLAN),
179                 TelephonyIcons.WFC);
180         networkToIconLookup.put(toDisplayIconKey(
181                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO),
182                 TelephonyIcons.LTE_CA_5G_E);
183         networkToIconLookup.put(toDisplayIconKey(
184                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA),
185                 TelephonyIcons.NR_5G);
186         networkToIconLookup.put(toDisplayIconKey(
187                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED),
188                 TelephonyIcons.NR_5G_PLUS);
189         networkToIconLookup.put(toIconKey(
190                 TelephonyManager.NETWORK_TYPE_NR),
191                 TelephonyIcons.NR_5G);
192         return networkToIconLookup;
193     }
194 
195     /**
196      * Wrapper class of system configs and Carrier configs.
197      */
198     public static class Config {
199         public boolean showAtLeast3G = false;
200         public boolean show4gFor3g = false;
201         public boolean alwaysShowCdmaRssi = false;
202         public boolean show4gForLte = false;
203         public boolean hideLtePlus = false;
204         public boolean hspaDataDistinguishable;
205         public boolean alwaysShowDataRatIcon = false;
206 
207         /**
208          * Reads the latest configs.
209          */
readConfig(Context context)210         public static Config readConfig(Context context) {
211             Config config = new Config();
212             Resources res = context.getResources();
213 
214             config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G);
215             config.alwaysShowCdmaRssi =
216                     res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi);
217             config.hspaDataDistinguishable =
218                     res.getBoolean(R.bool.config_hspa_data_distinguishable);
219 
220             CarrierConfigManager configMgr = (CarrierConfigManager)
221                     context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
222             // Handle specific carrier config values for the default data SIM
223             int defaultDataSubId = SubscriptionManager.from(context)
224                     .getDefaultDataSubscriptionId();
225             PersistableBundle b = configMgr.getConfigForSubId(defaultDataSubId);
226             if (b != null) {
227                 config.alwaysShowDataRatIcon = b.getBoolean(
228                         CarrierConfigManager.KEY_ALWAYS_SHOW_DATA_RAT_ICON_BOOL);
229                 config.show4gForLte = b.getBoolean(
230                         CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL);
231                 config.show4gFor3g = b.getBoolean(
232                         CarrierConfigManager.KEY_SHOW_4G_FOR_3G_DATA_ICON_BOOL);
233                 config.hideLtePlus = b.getBoolean(
234                         CarrierConfigManager.KEY_HIDE_LTE_PLUS_DATA_ICON_BOOL);
235             }
236             return config;
237         }
238     }
239 }
240