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.telephony.TelephonyManager; 30 import android.text.BidiFormatter; 31 import android.text.TextDirectionHeuristics; 32 import android.text.TextUtils; 33 import android.util.Log; 34 35 import com.android.internal.telephony.OperatorInfo; 36 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 * Get the network type from a CellInfo. Network types include 51 * {@link TelephonyManager#NETWORK_TYPE_LTE}, {@link TelephonyManager#NETWORK_TYPE_UMTS}, 52 * {@link TelephonyManager#NETWORK_TYPE_GSM}, {@link TelephonyManager#NETWORK_TYPE_CDMA} and 53 * {@link TelephonyManager#NETWORK_TYPE_UNKNOWN} 54 * @return network types 55 */ getNetworkType(CellInfo cellInfo)56 public static int getNetworkType(CellInfo cellInfo) { 57 if (cellInfo instanceof CellInfoLte) { 58 return TelephonyManager.NETWORK_TYPE_LTE; 59 } else if (cellInfo instanceof CellInfoWcdma) { 60 return TelephonyManager.NETWORK_TYPE_UMTS; 61 } else if (cellInfo instanceof CellInfoGsm) { 62 return TelephonyManager.NETWORK_TYPE_GSM; 63 } else if (cellInfo instanceof CellInfoCdma) { 64 return TelephonyManager.NETWORK_TYPE_CDMA; 65 } else { 66 Log.e(TAG, "Invalid CellInfo type"); 67 return TelephonyManager.NETWORK_TYPE_UNKNOWN; 68 } 69 } 70 71 /** 72 * Get signal level as an int from 0..4. 73 * @return Signal strength level 74 */ getLevel(CellInfo cellInfo)75 public static int getLevel(CellInfo cellInfo) { 76 if (cellInfo instanceof CellInfoLte) { 77 return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); 78 } else if (cellInfo instanceof CellInfoWcdma) { 79 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel(); 80 } else if (cellInfo instanceof CellInfoGsm) { 81 return ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel(); 82 } else if (cellInfo instanceof CellInfoCdma) { 83 return ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel(); 84 } else { 85 Log.e(TAG, "Invalid CellInfo type"); 86 return 0; 87 } 88 } 89 90 /** 91 * Wrap a CellIdentity into a CellInfo. 92 */ wrapCellInfoWithCellIdentity(CellIdentity cellIdentity)93 public static CellInfo wrapCellInfoWithCellIdentity(CellIdentity cellIdentity) { 94 if (cellIdentity instanceof CellIdentityLte) { 95 CellInfoLte cellInfo = new CellInfoLte(); 96 cellInfo.setCellIdentity((CellIdentityLte) cellIdentity); 97 return cellInfo; 98 } else if (cellIdentity instanceof CellIdentityCdma) { 99 CellInfoCdma cellInfo = new CellInfoCdma(); 100 cellInfo.setCellIdentity((CellIdentityCdma) cellIdentity); 101 return cellInfo; 102 } else if (cellIdentity instanceof CellIdentityWcdma) { 103 CellInfoWcdma cellInfo = new CellInfoWcdma(); 104 cellInfo.setCellIdentity((CellIdentityWcdma) cellIdentity); 105 return cellInfo; 106 } else if (cellIdentity instanceof CellIdentityGsm) { 107 CellInfoGsm cellInfo = new CellInfoGsm(); 108 cellInfo.setCellIdentity((CellIdentityGsm) cellIdentity); 109 return cellInfo; 110 } else { 111 Log.e(TAG, "Invalid CellInfo type"); 112 return null; 113 } 114 } 115 116 /** 117 * Returns the title of the network obtained in the manual search. 118 * 119 * @param cellInfo contains the information of the network. 120 * @return Long Name if not null/empty, otherwise Short Name if not null/empty, 121 * else MCCMNC string. 122 */ getNetworkTitle(CellInfo cellInfo)123 public static String getNetworkTitle(CellInfo cellInfo) { 124 OperatorInfo oi = getOperatorInfoFromCellInfo(cellInfo); 125 126 if (!TextUtils.isEmpty(oi.getOperatorAlphaLong())) { 127 return oi.getOperatorAlphaLong(); 128 } else if (!TextUtils.isEmpty(oi.getOperatorAlphaShort())) { 129 return oi.getOperatorAlphaShort(); 130 } else { 131 BidiFormatter bidiFormatter = BidiFormatter.getInstance(); 132 return bidiFormatter.unicodeWrap(oi.getOperatorNumeric(), TextDirectionHeuristics.LTR); 133 } 134 } 135 136 /** 137 * Wrap a cell info into an operator info. 138 */ getOperatorInfoFromCellInfo(CellInfo cellInfo)139 public static OperatorInfo getOperatorInfoFromCellInfo(CellInfo cellInfo) { 140 OperatorInfo oi; 141 if (cellInfo instanceof CellInfoLte) { 142 CellInfoLte lte = (CellInfoLte) cellInfo; 143 oi = new OperatorInfo( 144 (String) lte.getCellIdentity().getOperatorAlphaLong(), 145 (String) lte.getCellIdentity().getOperatorAlphaShort(), 146 lte.getCellIdentity().getMobileNetworkOperator()); 147 } else if (cellInfo instanceof CellInfoWcdma) { 148 CellInfoWcdma wcdma = (CellInfoWcdma) cellInfo; 149 oi = new OperatorInfo( 150 (String) wcdma.getCellIdentity().getOperatorAlphaLong(), 151 (String) wcdma.getCellIdentity().getOperatorAlphaShort(), 152 wcdma.getCellIdentity().getMobileNetworkOperator()); 153 } else if (cellInfo instanceof CellInfoGsm) { 154 CellInfoGsm gsm = (CellInfoGsm) cellInfo; 155 oi = new OperatorInfo( 156 (String) gsm.getCellIdentity().getOperatorAlphaLong(), 157 (String) gsm.getCellIdentity().getOperatorAlphaShort(), 158 gsm.getCellIdentity().getMobileNetworkOperator()); 159 } else if (cellInfo instanceof CellInfoCdma) { 160 CellInfoCdma cdma = (CellInfoCdma) cellInfo; 161 oi = new OperatorInfo( 162 (String) cdma.getCellIdentity().getOperatorAlphaLong(), 163 (String) cdma.getCellIdentity().getOperatorAlphaShort(), 164 "" /* operator numeric */); 165 } else { 166 Log.e(TAG, "Invalid CellInfo type"); 167 oi = new OperatorInfo("", "", ""); 168 } 169 return oi; 170 } 171 172 /** Checks whether the network operator is forbidden. */ isForbidden(CellInfo cellInfo, List<String> forbiddenPlmns)173 public static boolean isForbidden(CellInfo cellInfo, List<String> forbiddenPlmns) { 174 String plmn = CellInfoUtil.getOperatorInfoFromCellInfo(cellInfo).getOperatorNumeric(); 175 return forbiddenPlmns != null && forbiddenPlmns.contains(plmn); 176 } 177 } 178