1 /* 2 * Copyright (C) 2018 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.settings.network.telephony; 18 19 import static android.telephony.SignalStrength.NUM_SIGNAL_STRENGTH_BINS; 20 21 import android.content.Context; 22 import android.telephony.AccessNetworkConstants.AccessNetworkType; 23 import android.telephony.CellIdentity; 24 import android.telephony.CellIdentityGsm; 25 import android.telephony.CellIdentityLte; 26 import android.telephony.CellIdentityNr; 27 import android.telephony.CellIdentityTdscdma; 28 import android.telephony.CellIdentityWcdma; 29 import android.telephony.CellInfo; 30 import android.telephony.CellInfoCdma; 31 import android.telephony.CellInfoGsm; 32 import android.telephony.CellInfoLte; 33 import android.telephony.CellInfoNr; 34 import android.telephony.CellInfoTdscdma; 35 import android.telephony.CellInfoWcdma; 36 import android.telephony.CellSignalStrength; 37 import android.util.Log; 38 39 import androidx.annotation.VisibleForTesting; 40 import androidx.preference.Preference; 41 42 import com.android.internal.telephony.OperatorInfo; 43 import com.android.settings.R; 44 45 import java.util.List; 46 import java.util.Objects; 47 48 /** 49 * A Preference represents a network operator in the NetworkSelectSetting fragment. 50 */ 51 public class NetworkOperatorPreference extends Preference { 52 53 private static final String TAG = "NetworkOperatorPref"; 54 private static final boolean DBG = false; 55 56 private static final int LEVEL_NONE = -1; 57 58 private CellInfo mCellInfo; 59 private CellIdentity mCellId; 60 private List<String> mForbiddenPlmns; 61 private int mLevel = LEVEL_NONE; 62 private boolean mShow4GForLTE; 63 private boolean mUseNewApi; 64 NetworkOperatorPreference(Context context, CellInfo cellinfo, List<String> forbiddenPlmns, boolean show4GForLTE)65 public NetworkOperatorPreference(Context context, CellInfo cellinfo, 66 List<String> forbiddenPlmns, boolean show4GForLTE) { 67 this(context, forbiddenPlmns, show4GForLTE); 68 updateCell(cellinfo); 69 } 70 NetworkOperatorPreference(Context context, CellIdentity connectedCellId, List<String> forbiddenPlmns, boolean show4GForLTE)71 public NetworkOperatorPreference(Context context, CellIdentity connectedCellId, 72 List<String> forbiddenPlmns, boolean show4GForLTE) { 73 this(context, forbiddenPlmns, show4GForLTE); 74 updateCell(null, connectedCellId); 75 } 76 NetworkOperatorPreference( Context context, List<String> forbiddenPlmns, boolean show4GForLTE)77 private NetworkOperatorPreference( 78 Context context, List<String> forbiddenPlmns, boolean show4GForLTE) { 79 super(context); 80 mForbiddenPlmns = forbiddenPlmns; 81 mShow4GForLTE = show4GForLTE; 82 mUseNewApi = context.getResources().getBoolean( 83 com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI); 84 } 85 86 /** 87 * Change cell information 88 */ updateCell(CellInfo cellinfo)89 public void updateCell(CellInfo cellinfo) { 90 updateCell(cellinfo, CellInfoUtil.getCellIdentity(cellinfo)); 91 } 92 93 @VisibleForTesting updateCell(CellInfo cellinfo, CellIdentity cellId)94 protected void updateCell(CellInfo cellinfo, CellIdentity cellId) { 95 mCellInfo = cellinfo; 96 mCellId = cellId; 97 refresh(); 98 } 99 100 /** 101 * Compare cell within preference 102 */ isSameCell(CellInfo cellinfo)103 public boolean isSameCell(CellInfo cellinfo) { 104 if (cellinfo == null) { 105 return false; 106 } 107 return mCellId.equals(CellInfoUtil.getCellIdentity(cellinfo)); 108 } 109 110 /** 111 * Return true when this preference is for forbidden network 112 */ isForbiddenNetwork()113 public boolean isForbiddenNetwork() { 114 return ((mForbiddenPlmns != null) && mForbiddenPlmns.contains(getOperatorNumeric())); 115 } 116 117 /** 118 * Refresh the NetworkOperatorPreference by updating the title and the icon. 119 */ refresh()120 public void refresh() { 121 String networkTitle = getOperatorName(); 122 123 if (isForbiddenNetwork()) { 124 if (DBG) Log.d(TAG, "refresh forbidden network: " + networkTitle); 125 networkTitle += " " 126 + getContext().getResources().getString(R.string.forbidden_network); 127 } else { 128 if (DBG) Log.d(TAG, "refresh the network: " + networkTitle); 129 } 130 setTitle(Objects.toString(networkTitle, "")); 131 132 if (mCellInfo == null) { 133 return; 134 } 135 136 final CellSignalStrength signalStrength = getCellSignalStrength(mCellInfo); 137 final int level = signalStrength != null ? signalStrength.getLevel() : LEVEL_NONE; 138 if (DBG) Log.d(TAG, "refresh level: " + String.valueOf(level)); 139 mLevel = level; 140 updateIcon(mLevel); 141 } 142 143 /** 144 * Update the icon according to the input signal strength level. 145 */ setIcon(int level)146 public void setIcon(int level) { 147 updateIcon(level); 148 } 149 150 /** 151 * Operator numeric of this cell 152 */ getOperatorNumeric()153 public String getOperatorNumeric() { 154 final CellIdentity cellId = mCellId; 155 if (cellId == null) { 156 return null; 157 } 158 if (cellId instanceof CellIdentityGsm) { 159 return ((CellIdentityGsm) cellId).getMobileNetworkOperator(); 160 } 161 if (cellId instanceof CellIdentityWcdma) { 162 return ((CellIdentityWcdma) cellId).getMobileNetworkOperator(); 163 } 164 if (cellId instanceof CellIdentityTdscdma) { 165 return ((CellIdentityTdscdma) cellId).getMobileNetworkOperator(); 166 } 167 if (cellId instanceof CellIdentityLte) { 168 return ((CellIdentityLte) cellId).getMobileNetworkOperator(); 169 } 170 if (cellId instanceof CellIdentityNr) { 171 final String mcc = ((CellIdentityNr) cellId).getMccString(); 172 if (mcc == null) { 173 return null; 174 } 175 return mcc.concat(((CellIdentityNr) cellId).getMncString()); 176 } 177 return null; 178 } 179 180 /** 181 * Operator name of this cell 182 */ getOperatorName()183 public String getOperatorName() { 184 return CellInfoUtil.getNetworkTitle(mCellId, getOperatorNumeric()); 185 } 186 187 /** 188 * Operator info of this cell 189 */ getOperatorInfo()190 public OperatorInfo getOperatorInfo() { 191 return new OperatorInfo(Objects.toString(mCellId.getOperatorAlphaLong(), ""), 192 Objects.toString(mCellId.getOperatorAlphaShort(), ""), 193 getOperatorNumeric(), getAccessNetworkTypeFromCellInfo(mCellInfo)); 194 } 195 getIconIdForCell(CellInfo ci)196 private int getIconIdForCell(CellInfo ci) { 197 if (ci instanceof CellInfoGsm) { 198 return R.drawable.signal_strength_g; 199 } 200 if (ci instanceof CellInfoCdma) { 201 return R.drawable.signal_strength_1x; 202 } 203 if ((ci instanceof CellInfoWcdma) || (ci instanceof CellInfoTdscdma)) { 204 return R.drawable.signal_strength_3g; 205 } 206 if (ci instanceof CellInfoLte) { 207 return mShow4GForLTE 208 ? R.drawable.ic_signal_strength_4g : R.drawable.signal_strength_lte; 209 } 210 if (ci instanceof CellInfoNr) { 211 return R.drawable.signal_strength_5g; 212 } 213 return MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON; 214 } 215 getCellSignalStrength(CellInfo ci)216 private CellSignalStrength getCellSignalStrength(CellInfo ci) { 217 if (ci instanceof CellInfoGsm) { 218 return ((CellInfoGsm) ci).getCellSignalStrength(); 219 } 220 if (ci instanceof CellInfoCdma) { 221 return ((CellInfoCdma) ci).getCellSignalStrength(); 222 } 223 if (ci instanceof CellInfoWcdma) { 224 return ((CellInfoWcdma) ci).getCellSignalStrength(); 225 } 226 if (ci instanceof CellInfoTdscdma) { 227 return ((CellInfoTdscdma) ci).getCellSignalStrength(); 228 } 229 if (ci instanceof CellInfoLte) { 230 return ((CellInfoLte) ci).getCellSignalStrength(); 231 } 232 if (ci instanceof CellInfoNr) { 233 return ((CellInfoNr) ci).getCellSignalStrength(); 234 } 235 return null; 236 } 237 getAccessNetworkTypeFromCellInfo(CellInfo ci)238 private int getAccessNetworkTypeFromCellInfo(CellInfo ci) { 239 if (ci instanceof CellInfoGsm) { 240 return AccessNetworkType.GERAN; 241 } 242 if (ci instanceof CellInfoCdma) { 243 return AccessNetworkType.CDMA2000; 244 } 245 if ((ci instanceof CellInfoWcdma) || (ci instanceof CellInfoTdscdma)) { 246 return AccessNetworkType.UTRAN; 247 } 248 if (ci instanceof CellInfoLte) { 249 return AccessNetworkType.EUTRAN; 250 } 251 if (ci instanceof CellInfoNr) { 252 return AccessNetworkType.NGRAN; 253 } 254 return AccessNetworkType.UNKNOWN; 255 } 256 updateIcon(int level)257 private void updateIcon(int level) { 258 if (!mUseNewApi || level < 0 || level >= NUM_SIGNAL_STRENGTH_BINS) { 259 return; 260 } 261 final Context context = getContext(); 262 setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS, 263 getIconIdForCell(mCellInfo), false, false)); 264 } 265 } 266