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 android.telephony.CellIdentity; 20 import android.telephony.CellIdentityGsm; 21 import android.telephony.CellIdentityLte; 22 import android.telephony.CellIdentityNr; 23 import android.telephony.CellIdentityTdscdma; 24 import android.telephony.CellIdentityWcdma; 25 import android.telephony.CellInfo; 26 import android.telephony.CellInfoCdma; 27 import android.telephony.CellInfoGsm; 28 import android.telephony.CellInfoLte; 29 import android.telephony.CellInfoNr; 30 import android.telephony.CellInfoTdscdma; 31 import android.telephony.CellInfoWcdma; 32 import android.text.BidiFormatter; 33 import android.text.TextDirectionHeuristics; 34 import android.text.TextUtils; 35 36 import com.android.internal.telephony.OperatorInfo; 37 38 import java.util.Collections; 39 import java.util.List; 40 import java.util.Objects; 41 import java.util.stream.Collectors; 42 43 /** 44 * Add static Utility functions to get information from the CellInfo object. 45 * TODO: Modify {@link CellInfo} for simplify those functions 46 */ 47 public final class CellInfoUtil { 48 private static final String TAG = "NetworkSelectSetting"; 49 CellInfoUtil()50 private CellInfoUtil() { 51 } 52 53 /** 54 * Returns the title of the network obtained in the manual search. 55 * 56 * @param cellId contains the identity of the network. 57 * @param networkMccMnc contains the MCCMNC string of the network 58 * @return Long Name if not null/empty, otherwise Short Name if not null/empty, 59 * else MCCMNC string. 60 */ getNetworkTitle(CellIdentity cellId, String networkMccMnc)61 public static String getNetworkTitle(CellIdentity cellId, String networkMccMnc) { 62 if (cellId != null) { 63 String title = Objects.toString(cellId.getOperatorAlphaLong(), ""); 64 if (TextUtils.isEmpty(title)) { 65 title = Objects.toString(cellId.getOperatorAlphaShort(), ""); 66 } 67 if (!TextUtils.isEmpty(title)) { 68 return title; 69 } 70 } 71 if (TextUtils.isEmpty(networkMccMnc)) { 72 return ""; 73 } 74 final BidiFormatter bidiFormatter = BidiFormatter.getInstance(); 75 return bidiFormatter.unicodeWrap(networkMccMnc, TextDirectionHeuristics.LTR); 76 } 77 78 /** 79 * Returns the CellIdentity from CellInfo 80 * 81 * @param cellInfo contains the information of the network. 82 * @return CellIdentity within CellInfo 83 */ getCellIdentity(CellInfo cellInfo)84 public static CellIdentity getCellIdentity(CellInfo cellInfo) { 85 if (cellInfo == null) { 86 return null; 87 } 88 CellIdentity cellId = null; 89 if (cellInfo instanceof CellInfoGsm) { 90 cellId = ((CellInfoGsm) cellInfo).getCellIdentity(); 91 } else if (cellInfo instanceof CellInfoCdma) { 92 cellId = ((CellInfoCdma) cellInfo).getCellIdentity(); 93 } else if (cellInfo instanceof CellInfoWcdma) { 94 cellId = ((CellInfoWcdma) cellInfo).getCellIdentity(); 95 } else if (cellInfo instanceof CellInfoTdscdma) { 96 cellId = ((CellInfoTdscdma) cellInfo).getCellIdentity(); 97 } else if (cellInfo instanceof CellInfoLte) { 98 cellId = ((CellInfoLte) cellInfo).getCellIdentity(); 99 } else if (cellInfo instanceof CellInfoNr) { 100 cellId = ((CellInfoNr) cellInfo).getCellIdentity(); 101 } 102 return cellId; 103 } 104 105 /** 106 * Creates a CellInfo object from OperatorInfo. GsmCellInfo is used here only because 107 * operatorInfo does not contain technology type while CellInfo is an abstract object that 108 * requires to specify technology type. It doesn't matter which CellInfo type to use here, since 109 * we only want to wrap the operator info and PLMN to a CellInfo object. 110 */ convertOperatorInfoToCellInfo(OperatorInfo operatorInfo)111 public static CellInfo convertOperatorInfoToCellInfo(OperatorInfo operatorInfo) { 112 final String operatorNumeric = operatorInfo.getOperatorNumeric(); 113 String mcc = null; 114 String mnc = null; 115 if (operatorNumeric != null && operatorNumeric.matches("^[0-9]{5,6}$")) { 116 mcc = operatorNumeric.substring(0, 3); 117 mnc = operatorNumeric.substring(3); 118 } 119 final CellIdentityGsm cig = new CellIdentityGsm( 120 Integer.MAX_VALUE /* lac */, 121 Integer.MAX_VALUE /* cid */, 122 Integer.MAX_VALUE /* arfcn */, 123 Integer.MAX_VALUE /* bsic */, 124 mcc, 125 mnc, 126 operatorInfo.getOperatorAlphaLong(), 127 operatorInfo.getOperatorAlphaShort(), 128 Collections.emptyList()); 129 130 final CellInfoGsm ci = new CellInfoGsm(); 131 ci.setCellIdentity(cig); 132 return ci; 133 } 134 135 /** Convert a list of cellInfos to readable string without sensitive info. */ cellInfoListToString(List<CellInfo> cellInfos)136 public static String cellInfoListToString(List<CellInfo> cellInfos) { 137 return cellInfos.stream() 138 .map(cellInfo -> cellInfoToString(cellInfo)) 139 .collect(Collectors.joining(", ")); 140 } 141 142 /** Convert {@code cellInfo} to a readable string without sensitive info. */ cellInfoToString(CellInfo cellInfo)143 public static String cellInfoToString(CellInfo cellInfo) { 144 final String cellType = cellInfo.getClass().getSimpleName(); 145 final CellIdentity cid = getCellIdentity(cellInfo); 146 String mcc = getCellIdentityMcc(cid); 147 String mnc = getCellIdentityMnc(cid); 148 return String.format( 149 "{CellType = %s, isRegistered = %b, mcc = %s, mnc = %s, alphaL = %s, alphaS = %s}", 150 cellType, cellInfo.isRegistered(), mcc, mnc, 151 cid.getOperatorAlphaLong(), cid.getOperatorAlphaShort()); 152 } 153 154 /** 155 * Returns the MccMnc. 156 * 157 * @param cid contains the identity of the network. 158 * @return MccMnc string. 159 */ getCellIdentityMccMnc(CellIdentity cid)160 public static String getCellIdentityMccMnc(CellIdentity cid) { 161 String mcc = getCellIdentityMcc(cid); 162 String mnc = getCellIdentityMnc(cid); 163 return (mcc == null || mnc == null) ? null : mcc + mnc; 164 } 165 166 /** 167 * Returns the Mcc. 168 * 169 * @param cid contains the identity of the network. 170 * @return Mcc string. 171 */ getCellIdentityMcc(CellIdentity cid)172 public static String getCellIdentityMcc(CellIdentity cid) { 173 String mcc = null; 174 if (cid != null) { 175 if (cid instanceof CellIdentityGsm) { 176 mcc = ((CellIdentityGsm) cid).getMccString(); 177 } else if (cid instanceof CellIdentityWcdma) { 178 mcc = ((CellIdentityWcdma) cid).getMccString(); 179 } else if (cid instanceof CellIdentityTdscdma) { 180 mcc = ((CellIdentityTdscdma) cid).getMccString(); 181 } else if (cid instanceof CellIdentityLte) { 182 mcc = ((CellIdentityLte) cid).getMccString(); 183 } else if (cid instanceof CellIdentityNr) { 184 mcc = ((CellIdentityNr) cid).getMccString(); 185 } 186 } 187 return (mcc == null) ? null : mcc; 188 } 189 190 /** 191 * Returns the Mnc. 192 * 193 * @param cid contains the identity of the network. 194 * @return Mcc string. 195 */ getCellIdentityMnc(CellIdentity cid)196 public static String getCellIdentityMnc(CellIdentity cid) { 197 String mnc = null; 198 if (cid != null) { 199 if (cid instanceof CellIdentityGsm) { 200 mnc = ((CellIdentityGsm) cid).getMncString(); 201 } else if (cid instanceof CellIdentityWcdma) { 202 mnc = ((CellIdentityWcdma) cid).getMncString(); 203 } else if (cid instanceof CellIdentityTdscdma) { 204 mnc = ((CellIdentityTdscdma) cid).getMncString(); 205 } else if (cid instanceof CellIdentityLte) { 206 mnc = ((CellIdentityLte) cid).getMncString(); 207 } else if (cid instanceof CellIdentityNr) { 208 mnc = ((CellIdentityNr) cid).getMncString(); 209 } 210 } 211 return (mnc == null) ? null : mnc; 212 } 213 } 214