blob: f6d741f0a2fa35f4429abd651e883865e6764608 [file] [log] [blame]
Cassie22b69172018-03-05 14:28:22 -08001/*
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
17package com.android.phone;
18
19import android.telephony.CellIdentity;
20import android.telephony.CellIdentityCdma;
21import android.telephony.CellIdentityGsm;
22import android.telephony.CellIdentityLte;
23import android.telephony.CellIdentityWcdma;
24import android.telephony.CellInfo;
25import android.telephony.CellInfoCdma;
26import android.telephony.CellInfoGsm;
27import android.telephony.CellInfoLte;
28import android.telephony.CellInfoWcdma;
Cassie22b69172018-03-05 14:28:22 -080029import android.text.BidiFormatter;
30import android.text.TextDirectionHeuristics;
31import android.text.TextUtils;
32import android.util.Log;
33
34import com.android.internal.telephony.OperatorInfo;
35
Nathan Haroldfcb175d2020-01-20 22:10:52 -080036import java.util.Collections;
Cassiee2de67e2018-05-25 11:00:20 -070037import java.util.List;
38
Cassie22b69172018-03-05 14:28:22 -080039/**
40 * Add static Utility functions to get information from the CellInfo object.
41 * TODO: Modify {@link CellInfo} for simplify those functions
42 */
43public final class CellInfoUtil {
44 private static final String TAG = "NetworkSelectSetting";
45
46 private CellInfoUtil() {
47 }
48
49 /**
Cassie22b69172018-03-05 14:28:22 -080050 * Wrap a CellIdentity into a CellInfo.
51 */
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 */
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 */
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 }
Cassiee2de67e2018-05-25 11:00:20 -0700130
Pengquan Meng08648412018-09-27 16:26:17 -0700131 /**
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 */
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(),
Nathan Haroldfcb175d2020-01-20 22:10:52 -0800153 operatorInfo.getOperatorAlphaShort(),
154 Collections.emptyList());
Pengquan Meng08648412018-09-27 16:26:17 -0700155
156 CellInfoGsm ci = new CellInfoGsm();
157 ci.setCellIdentity(cig);
158 return ci;
159 }
160
Cassiee2de67e2018-05-25 11:00:20 -0700161 /** Checks whether the network operator is forbidden. */
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 }
Cassie22b69172018-03-05 14:28:22 -0800166}