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_CDMA), 117 TelephonyIcons.ONE_X); 118 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_1xRTT), 119 TelephonyIcons.ONE_X); 120 } else { 121 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_UNKNOWN), 122 TelephonyIcons.THREE_G); 123 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_EDGE), 124 TelephonyIcons.THREE_G); 125 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_CDMA), 126 TelephonyIcons.THREE_G); 127 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_1xRTT), 128 TelephonyIcons.THREE_G); 129 } 130 131 MobileIconGroup hGroup = TelephonyIcons.THREE_G; 132 MobileIconGroup hPlusGroup = TelephonyIcons.THREE_G; 133 if (config.show4gFor3g) { 134 hGroup = TelephonyIcons.FOUR_G; 135 hPlusGroup = TelephonyIcons.FOUR_G; 136 } else if (config.hspaDataDistinguishable) { 137 hGroup = TelephonyIcons.H; 138 hPlusGroup = TelephonyIcons.H_PLUS; 139 } 140 141 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSDPA), hGroup); 142 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSUPA), hGroup); 143 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSPA), hGroup); 144 networkToIconLookup.put(toIconKey(TelephonyManager.NETWORK_TYPE_HSPAP), hPlusGroup); 145 146 if (config.show4gForLte) { 147 networkToIconLookup.put(toIconKey( 148 TelephonyManager.NETWORK_TYPE_LTE), 149 TelephonyIcons.FOUR_G); 150 if (config.hideLtePlus) { 151 networkToIconLookup.put(toDisplayIconKey( 152 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA), 153 TelephonyIcons.FOUR_G); 154 } else { 155 networkToIconLookup.put(toDisplayIconKey( 156 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA), 157 TelephonyIcons.FOUR_G_PLUS); 158 } 159 } else { 160 networkToIconLookup.put(toIconKey( 161 TelephonyManager.NETWORK_TYPE_LTE), 162 TelephonyIcons.LTE); 163 if (config.hideLtePlus) { 164 networkToIconLookup.put(toDisplayIconKey( 165 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA), 166 TelephonyIcons.LTE); 167 } else { 168 networkToIconLookup.put(toDisplayIconKey( 169 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA), 170 TelephonyIcons.LTE_PLUS); 171 } 172 } 173 networkToIconLookup.put(toIconKey( 174 TelephonyManager.NETWORK_TYPE_IWLAN), 175 TelephonyIcons.WFC); 176 networkToIconLookup.put(toDisplayIconKey( 177 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO), 178 TelephonyIcons.LTE_CA_5G_E); 179 networkToIconLookup.put(toDisplayIconKey( 180 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA), 181 TelephonyIcons.NR_5G); 182 networkToIconLookup.put(toDisplayIconKey( 183 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED), 184 TelephonyIcons.NR_5G_PLUS); 185 networkToIconLookup.put(toIconKey( 186 TelephonyManager.NETWORK_TYPE_NR), 187 TelephonyIcons.NR_5G); 188 return networkToIconLookup; 189 } 190 191 /** 192 * Wrapper class of system configs and Carrier configs. 193 */ 194 public static class Config { 195 public boolean showAtLeast3G = false; 196 public boolean show4gFor3g = false; 197 public boolean alwaysShowCdmaRssi = false; 198 public boolean show4gForLte = false; 199 public boolean hideLtePlus = false; 200 public boolean hspaDataDistinguishable; 201 public boolean alwaysShowDataRatIcon = false; 202 203 /** 204 * Reads the latest configs. 205 */ readConfig(Context context)206 public static Config readConfig(Context context) { 207 Config config = new Config(); 208 Resources res = context.getResources(); 209 210 config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G); 211 config.alwaysShowCdmaRssi = 212 res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi); 213 config.hspaDataDistinguishable = 214 res.getBoolean(R.bool.config_hspa_data_distinguishable); 215 216 CarrierConfigManager configMgr = (CarrierConfigManager) 217 context.getSystemService(Context.CARRIER_CONFIG_SERVICE); 218 // Handle specific carrier config values for the default data SIM 219 int defaultDataSubId = SubscriptionManager.from(context) 220 .getDefaultDataSubscriptionId(); 221 PersistableBundle b = configMgr.getConfigForSubId(defaultDataSubId); 222 if (b != null) { 223 config.alwaysShowDataRatIcon = b.getBoolean( 224 CarrierConfigManager.KEY_ALWAYS_SHOW_DATA_RAT_ICON_BOOL); 225 config.show4gForLte = b.getBoolean( 226 CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL); 227 config.show4gFor3g = b.getBoolean( 228 CarrierConfigManager.KEY_SHOW_4G_FOR_3G_DATA_ICON_BOOL); 229 config.hideLtePlus = b.getBoolean( 230 CarrierConfigManager.KEY_HIDE_LTE_PLUS_DATA_ICON_BOOL); 231 } 232 return config; 233 } 234 } 235 } 236