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