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