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.phone; 18 19 import android.telephony.CellIdentity; 20 import android.telephony.CellIdentityCdma; 21 import android.telephony.CellIdentityGsm; 22 import android.telephony.CellIdentityLte; 23 import android.telephony.CellIdentityWcdma; 24 import android.telephony.CellInfo; 25 import android.telephony.CellInfoCdma; 26 import android.telephony.CellInfoGsm; 27 import android.telephony.CellInfoLte; 28 import android.telephony.CellInfoWcdma; 29 import android.text.BidiFormatter; 30 import android.text.TextDirectionHeuristics; 31 import android.text.TextUtils; 32 import android.util.Log; 33 34 import com.android.internal.telephony.OperatorInfo; 35 36 import java.util.Collections; 37 import java.util.List; 38 39 /** 40 * Add static Utility functions to get information from the CellInfo object. 41 * TODO: Modify {@link CellInfo} for simplify those functions 42 */ 43 public final class CellInfoUtil { 44 private static final String TAG = "NetworkSelectSetting"; 45 CellInfoUtil()46 private CellInfoUtil() { 47 } 48 49 /** 50 * Wrap a CellIdentity into a CellInfo. 51 */ wrapCellInfoWithCellIdentity(CellIdentity cellIdentity)52 public static CellInfo wrapCellInfoWithCellIdentity(CellIdentity cellIdentity) { 53 if (cellIdentity instanceof CellIdentityLte) { 54 CellInfoLte cellInfo = new CellInfoLte(); 55 cellInfo.setCellIdentity((CellIdentityLte) cellIdentity); 56 return cellInfo; 57 } else if (cellIdentity instanceof CellIdentityCdma) { 58 CellInfoCdma cellInfo = new CellInfoCdma(); 59 cellInfo.setCellIdentity((CellIdentityCdma) cellIdentity); 60 return cellInfo; 61 } else if (cellIdentity instanceof CellIdentityWcdma) { 62 CellInfoWcdma cellInfo = new CellInfoWcdma(); 63 cellInfo.setCellIdentity((CellIdentityWcdma) cellIdentity); 64 return cellInfo; 65 } else if (cellIdentity instanceof CellIdentityGsm) { 66 CellInfoGsm cellInfo = new CellInfoGsm(); 67 cellInfo.setCellIdentity((CellIdentityGsm) cellIdentity); 68 return cellInfo; 69 } else { 70 Log.e(TAG, "Invalid CellInfo type"); 71 return null; 72 } 73 } 74 75 /** 76 * Returns the title of the network obtained in the manual search. 77 * 78 * @param cellInfo contains the information of the network. 79 * @return Long Name if not null/empty, otherwise Short Name if not null/empty, 80 * else MCCMNC string. 81 */ getNetworkTitle(CellInfo cellInfo)82 public static String getNetworkTitle(CellInfo cellInfo) { 83 OperatorInfo oi = getOperatorInfoFromCellInfo(cellInfo); 84 85 if (!TextUtils.isEmpty(oi.getOperatorAlphaLong())) { 86 return oi.getOperatorAlphaLong(); 87 } else if (!TextUtils.isEmpty(oi.getOperatorAlphaShort())) { 88 return oi.getOperatorAlphaShort(); 89 } else { 90 BidiFormatter bidiFormatter = BidiFormatter.getInstance(); 91 return bidiFormatter.unicodeWrap(oi.getOperatorNumeric(), TextDirectionHeuristics.LTR); 92 } 93 } 94 95 /** 96 * Wrap a cell info into an operator info. 97 */ getOperatorInfoFromCellInfo(CellInfo cellInfo)98 public static OperatorInfo getOperatorInfoFromCellInfo(CellInfo cellInfo) { 99 OperatorInfo oi; 100 if (cellInfo instanceof CellInfoLte) { 101 CellInfoLte lte = (CellInfoLte) cellInfo; 102 oi = new OperatorInfo( 103 (String) lte.getCellIdentity().getOperatorAlphaLong(), 104 (String) lte.getCellIdentity().getOperatorAlphaShort(), 105 lte.getCellIdentity().getMobileNetworkOperator()); 106 } else if (cellInfo instanceof CellInfoWcdma) { 107 CellInfoWcdma wcdma = (CellInfoWcdma) cellInfo; 108 oi = new OperatorInfo( 109 (String) wcdma.getCellIdentity().getOperatorAlphaLong(), 110 (String) wcdma.getCellIdentity().getOperatorAlphaShort(), 111 wcdma.getCellIdentity().getMobileNetworkOperator()); 112 } else if (cellInfo instanceof CellInfoGsm) { 113 CellInfoGsm gsm = (CellInfoGsm) cellInfo; 114 oi = new OperatorInfo( 115 (String) gsm.getCellIdentity().getOperatorAlphaLong(), 116 (String) gsm.getCellIdentity().getOperatorAlphaShort(), 117 gsm.getCellIdentity().getMobileNetworkOperator()); 118 } else if (cellInfo instanceof CellInfoCdma) { 119 CellInfoCdma cdma = (CellInfoCdma) cellInfo; 120 oi = new OperatorInfo( 121 (String) cdma.getCellIdentity().getOperatorAlphaLong(), 122 (String) cdma.getCellIdentity().getOperatorAlphaShort(), 123 "" /* operator numeric */); 124 } else { 125 Log.e(TAG, "Invalid CellInfo type"); 126 oi = new OperatorInfo("", "", ""); 127 } 128 return oi; 129 } 130 131 /** 132 * Creates a CellInfo object from OperatorInfo. GsmCellInfo is used here only because 133 * operatorInfo does not contain technology type while CellInfo is an abstract object that 134 * requires to specify technology type. It doesn't matter which CellInfo type to use here, since 135 * we only want to wrap the operator info and PLMN to a CellInfo object. 136 */ convertOperatorInfoToCellInfo(OperatorInfo operatorInfo)137 public static CellInfo convertOperatorInfoToCellInfo(OperatorInfo operatorInfo) { 138 String operatorNumeric = operatorInfo.getOperatorNumeric(); 139 String mcc = null; 140 String mnc = null; 141 if (operatorNumeric != null && operatorNumeric.matches("^[0-9]{5,6}$")) { 142 mcc = operatorNumeric.substring(0, 3); 143 mnc = operatorNumeric.substring(3); 144 } 145 CellIdentityGsm cig = new CellIdentityGsm( 146 Integer.MAX_VALUE /* lac */, 147 Integer.MAX_VALUE /* cid */, 148 Integer.MAX_VALUE /* arfcn */, 149 Integer.MAX_VALUE /* bsic */, 150 mcc, 151 mnc, 152 operatorInfo.getOperatorAlphaLong(), 153 operatorInfo.getOperatorAlphaShort(), 154 Collections.emptyList()); 155 156 CellInfoGsm ci = new CellInfoGsm(); 157 ci.setCellIdentity(cig); 158 return ci; 159 } 160 161 /** Checks whether the network operator is forbidden. */ isForbidden(CellInfo cellInfo, List<String> forbiddenPlmns)162 public static boolean isForbidden(CellInfo cellInfo, List<String> forbiddenPlmns) { 163 String plmn = CellInfoUtil.getOperatorInfoFromCellInfo(cellInfo).getOperatorNumeric(); 164 return forbiddenPlmns != null && forbiddenPlmns.contains(plmn); 165 } 166 } 167