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