This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
CellIdentityGsm.java
329 lines (289 loc) · 10.5 KB
/
CellIdentityGsm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.telephony;
import static android.text.TextUtils.formatSimple;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build;
import android.os.Parcel;
import android.telephony.gsm.GsmCellLocation;
import android.text.TextUtils;
import android.util.ArraySet;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
/**
* CellIdentity to represent a unique GSM cell
*/
public final class CellIdentityGsm extends CellIdentity {
private static final String TAG = CellIdentityGsm.class.getSimpleName();
private static final boolean DBG = false;
private static final int MAX_LAC = 65535;
private static final int MAX_CID = 65535;
private static final int MAX_ARFCN = 65535;
private static final int MAX_BSIC = 63;
// 16-bit Location Area Code, 0..65535
private final int mLac;
// 16-bit GSM Cell Identity described in TS 27.007, 0..65535
private final int mCid;
// 16-bit GSM Absolute RF Channel Number
private final int mArfcn;
// 6-bit Base Station Identity Code
private final int mBsic;
// a list of additional PLMN-IDs reported for this cell
private final ArraySet<String> mAdditionalPlmns;
/**
* @hide
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public CellIdentityGsm() {
super(TAG, CellInfo.TYPE_GSM, null, null, null, null);
mLac = CellInfo.UNAVAILABLE;
mCid = CellInfo.UNAVAILABLE;
mArfcn = CellInfo.UNAVAILABLE;
mBsic = CellInfo.UNAVAILABLE;
mAdditionalPlmns = new ArraySet<>();
mGlobalCellId = null;
}
/**
* public constructor
* @param lac 16-bit Location Area Code, 0..65535
* @param cid 16-bit GSM Cell Identity or 28-bit UMTS Cell Identity
* @param arfcn 16-bit GSM Absolute RF Channel Number
* @param bsic 6-bit Base Station Identity Code
* @param mccStr 3-digit Mobile Country Code in string format
* @param mncStr 2 or 3-digit Mobile Network Code in string format
* @param alphal long alpha Operator Name String or Enhanced Operator Name String
* @param alphas short alpha Operator Name String or Enhanced Operator Name String
* @param additionalPlmns a list of additional PLMN IDs broadcast by the cell
*
* @hide
*/
public CellIdentityGsm(int lac, int cid, int arfcn, int bsic, @Nullable String mccStr,
@Nullable String mncStr, @Nullable String alphal, @Nullable String alphas,
@NonNull Collection<String> additionalPlmns) {
super(TAG, CellInfo.TYPE_GSM, mccStr, mncStr, alphal, alphas);
mLac = inRangeOrUnavailable(lac, 0, MAX_LAC);
mCid = inRangeOrUnavailable(cid, 0, MAX_CID);
mArfcn = inRangeOrUnavailable(arfcn, 0, MAX_ARFCN);
mBsic = inRangeOrUnavailable(bsic, 0, MAX_BSIC);
mAdditionalPlmns = new ArraySet<>(additionalPlmns.size());
for (String plmn : additionalPlmns) {
if (isValidPlmn(plmn)) {
mAdditionalPlmns.add(plmn);
}
}
updateGlobalCellId();
}
private CellIdentityGsm(@NonNull CellIdentityGsm cid) {
this(cid.mLac, cid.mCid, cid.mArfcn, cid.mBsic, cid.mMccStr,
cid.mMncStr, cid.mAlphaLong, cid.mAlphaShort, cid.mAdditionalPlmns);
}
@NonNull CellIdentityGsm copy() {
return new CellIdentityGsm(this);
}
/** @hide */
@Override
public @NonNull CellIdentityGsm sanitizeLocationInfo() {
return new CellIdentityGsm(CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE,
CellInfo.UNAVAILABLE, mMccStr, mMncStr, mAlphaLong, mAlphaShort, mAdditionalPlmns);
}
/** @hide */
@Override
protected void updateGlobalCellId() {
mGlobalCellId = null;
String plmn = getPlmn();
if (plmn == null) return;
if (mLac == CellInfo.UNAVAILABLE || mCid == CellInfo.UNAVAILABLE) return;
mGlobalCellId = plmn + formatSimple("%04x%04x", mLac, mCid);
}
/**
* @return 3-digit Mobile Country Code, 0..999,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
* @deprecated Use {@link #getMccString} instead.
*/
@Deprecated
public int getMcc() {
return (mMccStr != null) ? Integer.valueOf(mMccStr) : CellInfo.UNAVAILABLE;
}
/**
* @return 2 or 3-digit Mobile Network Code, 0..999,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
* @deprecated Use {@link #getMncString} instead.
*/
@Deprecated
public int getMnc() {
return (mMncStr != null) ? Integer.valueOf(mMncStr) : CellInfo.UNAVAILABLE;
}
/**
* @return 16-bit Location Area Code, 0..65535,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
*/
public int getLac() {
return mLac;
}
/**
* @return 16-bit GSM Cell Identity described in TS 27.007, 0..65535,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
*/
public int getCid() {
return mCid;
}
/**
* @return 16-bit GSM Absolute RF Channel Number,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
*/
public int getArfcn() {
return mArfcn;
}
/**
* @return 6-bit Base Station Identity Code,
* {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} if unavailable.
*/
public int getBsic() {
return mBsic;
}
/**
* @return a 5 or 6 character string (MCC+MNC), null if any field is unknown.
*/
@Nullable
public String getMobileNetworkOperator() {
return (mMccStr == null || mMncStr == null) ? null : mMccStr + mMncStr;
}
/**
* @return Mobile Country Code in string format, null if unavailable.
*/
@Nullable
public String getMccString() {
return mMccStr;
}
/**
* @return Mobile Network Code in string format, null if unavailable.
*/
@Nullable
public String getMncString() {
return mMncStr;
}
/** @hide */
@Override
public int getChannelNumber() {
return mArfcn;
}
/**
* @return a list of additional PLMN IDs supported by this cell.
*/
@NonNull
public Set<String> getAdditionalPlmns() {
return Collections.unmodifiableSet(mAdditionalPlmns);
}
/**
* @deprecated Primary Scrambling Code is not applicable to GSM.
* @return {@link android.telephony.CellInfo#UNAVAILABLE UNAVAILABLE} - undefined for GSM
*/
@Deprecated
public int getPsc() {
return CellInfo.UNAVAILABLE;
}
/** @hide */
@NonNull
@Override
public GsmCellLocation asCellLocation() {
GsmCellLocation cl = new GsmCellLocation();
int lac = mLac != CellInfo.UNAVAILABLE ? mLac : -1;
int cid = mCid != CellInfo.UNAVAILABLE ? mCid : -1;
cl.setLacAndCid(lac, cid);
cl.setPsc(-1);
return cl;
}
@Override
public int hashCode() {
return Objects.hash(mLac, mCid, mAdditionalPlmns.hashCode(), super.hashCode());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CellIdentityGsm)) {
return false;
}
CellIdentityGsm o = (CellIdentityGsm) other;
return mLac == o.mLac
&& mCid == o.mCid
&& mArfcn == o.mArfcn
&& mBsic == o.mBsic
&& TextUtils.equals(mMccStr, o.mMccStr)
&& TextUtils.equals(mMncStr, o.mMncStr)
&& mAdditionalPlmns.equals(o.mAdditionalPlmns)
&& super.equals(other);
}
@Override
public String toString() {
return new StringBuilder(TAG)
.append(":{ mLac=").append(mLac)
.append(" mCid=").append(mCid)
.append(" mArfcn=").append(mArfcn)
.append(" mBsic=").append("0x").append(Integer.toHexString(mBsic))
.append(" mMcc=").append(mMccStr)
.append(" mMnc=").append(mMncStr)
.append(" mAlphaLong=").append(mAlphaLong)
.append(" mAlphaShort=").append(mAlphaShort)
.append(" mAdditionalPlmns=").append(mAdditionalPlmns)
.append("}").toString();
}
/** Implement the Parcelable interface */
@Override
public void writeToParcel(Parcel dest, int flags) {
if (DBG) log("writeToParcel(Parcel, int): " + toString());
super.writeToParcel(dest, CellInfo.TYPE_GSM);
dest.writeInt(mLac);
dest.writeInt(mCid);
dest.writeInt(mArfcn);
dest.writeInt(mBsic);
dest.writeArraySet(mAdditionalPlmns);
}
/** Construct from Parcel, type has already been processed */
private CellIdentityGsm(Parcel in) {
super(TAG, CellInfo.TYPE_GSM, in);
mLac = in.readInt();
mCid = in.readInt();
mArfcn = in.readInt();
mBsic = in.readInt();
mAdditionalPlmns = (ArraySet<String>) in.readArraySet(null);
updateGlobalCellId();
if (DBG) log(toString());
}
/** Implement the Parcelable interface */
@SuppressWarnings("hiding")
public static final @android.annotation.NonNull Creator<CellIdentityGsm> CREATOR =
new Creator<CellIdentityGsm>() {
@Override
public CellIdentityGsm createFromParcel(Parcel in) {
in.readInt(); // skip
return createFromParcelBody(in);
}
@Override
public CellIdentityGsm[] newArray(int size) {
return new CellIdentityGsm[size];
}
};
/** @hide */
protected static CellIdentityGsm createFromParcelBody(Parcel in) {
return new CellIdentityGsm(in);
}
}