blob: 82720291d485d32cf50a43ae9f35c43d71b6e325 [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
Cassiee2de67e2018-05-25 11:00:20 -070036import java.util.List;
37
Cassie22b69172018-03-05 14:28:22 -080038/**
39 * Add static Utility functions to get information from the CellInfo object.
40 * TODO: Modify {@link CellInfo} for simplify those functions
41 */
42public final class CellInfoUtil {
43 private static final String TAG = "NetworkSelectSetting";
44
45 private CellInfoUtil() {
46 }
47
48 /**
Cassie22b69172018-03-05 14:28:22 -080049 * 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 }
Cassiee2de67e2018-05-25 11:00:20 -0700129
Pengquan Meng08648412018-09-27 16:26:17 -0700130 /**
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
Cassiee2de67e2018-05-25 11:00:20 -0700159 /** 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 }
Cassie22b69172018-03-05 14:28:22 -0800164}