-
Notifications
You must be signed in to change notification settings - Fork 6
/
CellState.java_14349550551512b900d4fee5b73cf95e
219 lines (190 loc) · 7.58 KB
/
CellState.java_14349550551512b900d4fee5b73cf95e
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
package android.telephony;
import android.telephony.ServiceState;
import android.telephony.CellIdentity;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityTdscdma;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellIdentityNr;
import android.telephony.CellLocation;
import android.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class CellState {
public String radioTechnology;
// cell info
public int arfcn;
public int bsic;
public int cid;
public int lac;
public int mcc;
public String mccString;
public int mnc;
public String mncString;
public int tac;
public int[] cellBandwidths;
public int channelNumber;
public int rat;
// signal
public int signalStrength = 0; // RSSI, in average
public int totalSignals = 0;
public int signalLevel; // abstract level of signal strength
public List<Integer> signals = new ArrayList<>();
public int MAX_SIGNAL_LENGTH = 20;
// operator info
public String operatorAlphaLong;
public String operatorAlphaShort;
public String operatorNumeric;
public CellState() {
}
public void initFromCellIdentity(CellIdentity cellIdentity) {
if (cellIdentity == null)
return;
rat = cellIdentity.getType();
switch(cellIdentity.getType()) {
case CellInfo.TYPE_GSM:
radioTechnology = "GSM";
CellIdentityGsm gsm = ((CellIdentityGsm) cellIdentity);
cid = gsm.getCid();
lac = gsm.getLac();
arfcn = gsm.getArfcn();
bsic = gsm.getBsic();
mcc = gsm.getMcc();
mnc = gsm.getMnc();
mccString = gsm.getMccString();
mncString = gsm.getMncString();
operatorAlphaLong = (String) gsm.getOperatorAlphaLong();
operatorAlphaShort = (String) gsm.getOperatorAlphaShort();
operatorNumeric = gsm.getMobileNetworkOperator();
break;
case CellInfo.TYPE_WCDMA:
radioTechnology = "WCDMA";
CellIdentityWcdma wcdma = ((CellIdentityWcdma) cellIdentity);
cid = wcdma.getCid();
lac = wcdma.getLac();
arfcn = wcdma.getUarfcn();
mcc = wcdma.getMcc();
mnc = wcdma.getMnc();
mccString = wcdma.getMccString();
mncString = wcdma.getMncString();
operatorAlphaLong = (String) wcdma.getOperatorAlphaLong();
operatorAlphaShort = (String) wcdma.getOperatorAlphaShort();
operatorNumeric = wcdma.getMobileNetworkOperator();
break;
case CellInfo.TYPE_TDSCDMA:
radioTechnology = "TDSCDMA";
CellIdentityTdscdma tdscdma = ((CellIdentityTdscdma) cellIdentity);
cid = tdscdma.getCid();
lac = tdscdma.getLac();
mccString = tdscdma.getMccString();
mncString = tdscdma.getMncString();
arfcn = tdscdma.getUarfcn();
operatorAlphaLong = (String) tdscdma.getOperatorAlphaLong();
operatorAlphaShort = (String) tdscdma.getOperatorAlphaShort();
operatorNumeric = tdscdma.getMobileNetworkOperator();
break;
case CellInfo.TYPE_LTE:
radioTechnology = "LTE";
CellIdentityLte lte = ((CellIdentityLte) cellIdentity);
cid = lte.getCi();
lac = lte.getTac();
arfcn = lte.getEarfcn();
mcc = lte.getMcc();
mnc = lte.getMnc();
mccString = lte.getMccString();
mncString = lte.getMncString();
tac = lte.getTac();
operatorAlphaLong = (String) lte.getOperatorAlphaLong();
operatorAlphaShort = (String) lte.getOperatorAlphaShort();
operatorNumeric = lte.getMobileNetworkOperator();
break;
case CellInfo.TYPE_NR:
radioTechnology = "NR";
CellIdentityNr nr = ((CellIdentityNr) cellIdentity);
cid = (int) nr.getNci();
lac = nr.getTac();
arfcn = nr.getNrarfcn();
tac = nr.getTac();
mccString = nr.getMccString();
mncString = nr.getMncString();
operatorAlphaLong = (String) nr.getOperatorAlphaLong();
operatorAlphaShort = (String) nr.getOperatorAlphaShort();
operatorNumeric = "";
break;
default:
break;
}
}
public void updateSignalStrength(int newSig) {
// calculate new signal average (RSSI)
if (newSig >= 0) {
// filter wrong signals
return;
}
if (signals.size() > MAX_SIGNAL_LENGTH)
signals.remove(0);
signals.add(newSig);
signalStrength = (signalStrength * totalSignals + newSig) / (totalSignals + 1);
totalSignals += 1;
}
// TODO: android will sometimes scan incomplete info, but this may vary of different devices
public boolean isValid() {
if (cid > 65535 || lac > 65535 || mcc > 65535 || mnc > 65535)
return false;
if (mcc == 0 && mnc == 0)
return false;
return true;
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other instanceof CellState) {
CellState ocs = (CellState) other;
if (ocs.cid != this.cid)
return false;
else if (ocs.lac != this.lac)
return false;
else if (ocs.arfcn != this.arfcn)
return false;
else if (ocs.mcc != this.mcc)
return false;
else if (ocs.mnc != this.mnc)
return false;
}
else {
return false;
}
return true;
}
@Override
public String toString() {
return new StringBuilder().append("\n=========Begin CellState=========\n")
.append(radioTechnology)
.append("\ncid=" + cid)
.append("\nlac=" + lac)
.append("\narfcn=" + arfcn)
.append("\nmcc=" + mcc)
.append("\nmnc=" + mnc)
.append("\naverageRSSI=" + signalStrength)
.append("\noperatorAlphaLong=" + operatorAlphaLong)
.append("\noperatorAlphaShort=" + operatorAlphaShort)
.append("\noperatorNumeric=" + operatorNumeric)
.append("\n=========End CellState=========")
.toString();
}
public int getArfcn() {return arfcn;}
public int getCid() {return cid;}
public int getLac() {return lac;}
public int getMcc() {return mcc;}
public int getMnc() {return mnc;}
public int getSignalStrength() {return signalStrength;}
public void setOperatorAlphaLong(@Nullable String s) {operatorAlphaLong = s;}
public @Nullable String getOperatorAlphaLong() {return operatorAlphaLong;}
public void setOperatorAlphaShort(@Nullable String s) {operatorAlphaShort = s;}
public @Nullable String getOperatorAlphaShort() {return operatorAlphaShort;}
public void setOperatorNumeric(@Nullable String s) {operatorNumeric = s;}
public @Nullable String getOperatorNumeric() {return operatorNumeric;}
}