-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathDevice.java
More file actions
330 lines (299 loc) · 10.4 KB
/
Device.java
File metadata and controls
330 lines (299 loc) · 10.4 KB
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
330
/* Android IMSI-Catcher Detector | (c) AIMSICD Privacy Project
* -----------------------------------------------------------
* LICENSE: http://git.io/vki47 | TERMS: http://git.io/vki4o
* -----------------------------------------------------------
*/
package com.secupwn.aimsicd.utils;
import android.content.Context;
import android.location.Location;
import android.os.Build;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import io.freefair.util.function.Optional;
import io.freefair.util.function.Supplier;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Getter
@Slf4j
public class Device {
public Cell cell;
/**
* integer representation of Phone Type
*/
private int phoneId = -1;
@Setter
private String cellInfo;
@Setter
private String dataState;
@Setter
private String dataStateShort;
/**
* Network Operator Name
*/
@Setter
private String networkName;
private String mncMcc;
private Optional<String> simCountry;
private String phoneType;
/**
* Device IMEI
*/
private String iMEI;
/**
* Device IMEI Version
*/
private String iMEIv;
private Optional<String> simOperator;
private Optional<String> simOperatorName;
private Optional<String> simSerial;
private Optional<String> simSubs;
@Setter
private String dataActivityType;
@Setter
private String dataActivityTypeShort;
private boolean roaming;
/**
* Cell object representing last known location
*/
@Setter
private Location lastLocation;
/**
* Refreshes all device specific details
*/
public void refreshDeviceInfo(TelephonyManager tm, Context context) {
//Phone type and associated details
iMEI = tm.getDeviceId();
iMEIv = tm.getDeviceSoftwareVersion();
phoneId = tm.getPhoneType();
roaming = tm.isNetworkRoaming();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
DeviceApi18.loadCellInfo(tm, this);
}
if (cell == null) {
cell = new Cell();
}
switch (phoneId) {
case TelephonyManager.PHONE_TYPE_NONE:
case TelephonyManager.PHONE_TYPE_SIP:
case TelephonyManager.PHONE_TYPE_GSM:
phoneType = "GSM";
mncMcc = tm.getNetworkOperator();
if (mncMcc != null && mncMcc.length() >= 5) {
try {
if (cell.getMobileCountryCode() == Integer.MAX_VALUE) {
cell.setMobileCountryCode(Integer.parseInt(tm.getNetworkOperator().substring(0, 3)));
}
if (cell.getMobileNetworkCode() == Integer.MAX_VALUE) {
cell.setMobileNetworkCode(Integer.parseInt(tm.getNetworkOperator().substring(3, 5)));
}
} catch (Exception e) {
log.info("MncMcc parse exception: ", e);
}
}
networkName = tm.getNetworkOperatorName();
if (!cell.isValid()) {
GsmCellLocation gsmCellLocation = (GsmCellLocation) tm.getCellLocation();
if (gsmCellLocation != null) {
cell.setCellId(gsmCellLocation.getCid());
cell.setLocationAreaCode(gsmCellLocation.getLac());
cell.setPrimaryScramblingCode(gsmCellLocation.getPsc());
}
}
break;
case TelephonyManager.PHONE_TYPE_CDMA:
phoneType = "CDMA";
if (!cell.isValid()) {
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) tm.getCellLocation();
if (cdmaCellLocation != null) {
cell.setCellId(cdmaCellLocation.getBaseStationId());
cell.setLocationAreaCode(cdmaCellLocation.getNetworkId());
cell.setSid(cdmaCellLocation.getSystemId()); // one of these must be a bug !!
// See: http://stackoverflow.com/questions/8088046/android-how-to-identify-carrier-on-cdma-network
// and: https://github.com/klinker41/android-smsmms/issues/26
cell.setMobileNetworkCode(cdmaCellLocation.getSystemId()); // todo: check! (Also CellTracker.java)
//Retrieve MCC through System Property
String homeOperator = Helpers.getSystemProp(context,
"ro.cdma.home.operator.numeric", "UNKNOWN");
if (!homeOperator.contains("UNKNOWN")) {
try {
if (cell.getMobileCountryCode() == Integer.MAX_VALUE) {
cell.setMobileCountryCode(Integer.valueOf(homeOperator.substring(0, 3)));
}
if (cell.getMobileNetworkCode() == Integer.MAX_VALUE) {
cell.setMobileNetworkCode(Integer.valueOf(homeOperator.substring(3, 5)));
}
} catch (Exception e) {
log.info("HomeOperator parse exception - {}", e.getMessage(), e);
}
}
}
}
break;
}
// SIM Information
simCountry = getSimCountry(tm);
// Get the operator code of the active SIM (MCC + MNC)
simOperator = getSimOperator(tm);
simOperatorName = getSimOperatorName(tm);
simSerial = getSimSerial(tm);
simSubs = getSimSubs(tm);
dataActivityType = getDataActivityType(tm);
dataState = getDataState(tm);
}
private static Optional<String> getSimInformation(Supplier<String> simInfoSupplier) {
try {
return Optional.ofNullable(simInfoSupplier.get());
} catch (Exception e) {
log.error("Failed to get SIM-Information", e);
}
return Optional.empty();
}
/**
* SIM Country
*
* @return string of SIM Country data
*/
Optional<String> getSimCountry(final TelephonyManager tm) {
return getSimInformation(new Supplier<String>() {
@Nullable
@Override
public String get() {
return tm.getSimCountryIso();
}
});
}
/**
* SIM Operator
*
* @return string of SIM Operator data
*/
public Optional<String> getSimOperator(final TelephonyManager tm) {
return getSimInformation(new Supplier<String>() {
@Nullable
@Override
public String get() {
return tm.getSimOperator();
}
});
}
/**
* SIM Operator Name
*
* @return string of SIM Operator Name
*/
Optional<String> getSimOperatorName(final TelephonyManager tm) {
return getSimInformation(new Supplier<String>() {
@Nullable
@Override
public String get() {
return tm.getSimOperatorName();
}
});
}
/**
* SIM Subscriber ID
*
* @return string of SIM Subscriber ID data
*/
Optional<String> getSimSubs(final TelephonyManager tm) {
return getSimInformation(new Supplier<String>() {
@Nullable
@Override
public String get() {
return tm.getSubscriberId();
}
});
}
/**
* SIM Serial Number
*
* @return string of SIM Serial Number data
*/
Optional<String> getSimSerial(final TelephonyManager tm) {
return getSimInformation(new Supplier<String>() {
@Nullable
@Override
public String get() {
return tm.getSimSerialNumber();
}
});
}
/**
* Network Type
*
* @return string representing device Network Type
*/
public String getNetworkTypeName() {
if (cell == null) {
return "Unknown";
}
return cell.getRat();
}
String getDataActivityType(TelephonyManager tm) {
int direction = tm.getDataActivity();
dataActivityTypeShort = "un";
dataActivityType = "undef";
switch (direction) {
case TelephonyManager.DATA_ACTIVITY_NONE:
dataActivityTypeShort = "No";
dataActivityType = "None";
break;
case TelephonyManager.DATA_ACTIVITY_IN:
dataActivityTypeShort = "In";
dataActivityType = "In";
break;
case TelephonyManager.DATA_ACTIVITY_OUT:
dataActivityTypeShort = "Ou";
dataActivityType = "Out";
break;
case TelephonyManager.DATA_ACTIVITY_INOUT:
dataActivityTypeShort = "IO";
dataActivityType = "In-Out";
break;
case TelephonyManager.DATA_ACTIVITY_DORMANT:
dataActivityTypeShort = "Do";
dataActivityType = "Dormant";
break;
}
return dataActivityType;
}
String getDataState(TelephonyManager tm) {
int state = tm.getDataState();
dataState = "undef";
dataStateShort = "un";
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
dataState = "Disconnected";
dataStateShort = "Di";
break;
case TelephonyManager.DATA_CONNECTING:
dataState = "Connecting";
dataStateShort = "Ct";
break;
case TelephonyManager.DATA_CONNECTED:
dataState = "Connected";
dataStateShort = "Cd";
break;
case TelephonyManager.DATA_SUSPENDED:
dataState = "Suspended";
dataStateShort = "Su";
break;
}
return dataState;
}
public void setSignalDbm(int signalDbm) {
cell.setDbm(signalDbm);
}
public int getSignalDBm() {
return cell.getDbm();
}
/**
* Update Network Type
*/
public void setNetID(TelephonyManager tm) {
cell.setNetType(tm.getNetworkType());
}
}