-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonParser.java
199 lines (171 loc) · 7.8 KB
/
JsonParser.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
package edu.nctu.wirelab.testsignalv1;
import android.os.Build;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class JsonParser {
private static final String TagName = "JsonParser";
public static String Account = "Account";
public static String Password = "Password";
public static boolean FileFirstWrite = true;
public static void SetAccount(String account){
Account = account;
}
public static String AtCellInfoToJson(){
JSONObject obj = new JSONObject();
JSONArray ary = new JSONArray();
try {
if( FileFirstWrite ) {
RunIntentService2.recordwriter.write("[\n");
FileFirstWrite = false;
}
obj.put("AppicationType", "Android");
obj.put("ApplicationVersion", Build.VERSION.RELEASE);
obj.put("Account", Account);
obj.put("Password", Password);
obj.put("equipmentId", MainActivity.tm.getDeviceId());
obj.put("Event", "Measurement");
obj.put("BatteryLevel", BatteryInfoReceiver.level);
if( LocationUpdater.userlocation!=null ){
obj.put("Lat", LocationUpdater.userlocation.getLatitude());
obj.put("Lng", LocationUpdater.userlocation.getLongitude());
}
else{
obj.put("Lat", "Unknown");
obj.put("Lng", "Unknown");
}
JSONObject cellinfo = new JSONObject();
if( SignalStrengthListener.CellInfoType.equals("LTE") ){
cellinfo.put("Time", System.currentTimeMillis() );
cellinfo.put("CellInfoType", SignalStrengthListener.CellInfoType);
cellinfo.put("CellID", SignalStrengthListener.AtCellID);
cellinfo.put("CellMCC", SignalStrengthListener.AtCellMCC);
cellinfo.put("CellMNC", SignalStrengthListener.AtCellMNC);
cellinfo.put("CellPCI", SignalStrengthListener.AtCellPCI);
cellinfo.put("CellTAC", SignalStrengthListener.AtCellTAC);
cellinfo.put("RSSI", SignalStrengthListener.AtCellRSSI);
cellinfo.put("SINR", "null");
cellinfo.put("RSRQ", SignalStrengthListener.AtCellRSRQ);
cellinfo.put("RSRP", SignalStrengthListener.AtCellRSRP);
}
else if( SignalStrengthListener.CellInfoType.equals("Wcdma") ){
cellinfo.put("Time", System.currentTimeMillis() );
cellinfo.put("CellInfoType", SignalStrengthListener.CellInfoType);
cellinfo.put("CellID", SignalStrengthListener.WcdmaAtCellID);
cellinfo.put("CellMCC", SignalStrengthListener.WcdmaAtCellMCC);
cellinfo.put("CellMNC", SignalStrengthListener.WcdmaAtCellMNC);
cellinfo.put("CellPSC", SignalStrengthListener.WcdmaAtCellPsc);
cellinfo.put("CellLAC", SignalStrengthListener.WcdmaAtCellLac);
cellinfo.put("SignalStrength", SignalStrengthListener.WcdmaAtCellSignalStrength);
}
ary.put(cellinfo);
obj.put("CellularInfo", ary);
return obj.toString(2);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
public static String HandoverInfoToJson(){
JSONObject obj = new JSONObject();
try {
if( FileFirstWrite ) {
RunIntentService2.recordwriter.write("[\n");
FileFirstWrite = false;
}
obj.put("AppicationType", "Android");
obj.put("ApplicationVersion", Build.VERSION.RELEASE);
obj.put("Account", Account);
obj.put("Password", Password);
obj.put("equipmentId", MainActivity.tm.getDeviceId());
obj.put("Event", "Handover");
obj.put("BatteryLevel", BatteryInfoReceiver.level);
if( LocationUpdater.userlocation!=null ){
obj.put("Lat", LocationUpdater.userlocation.getLatitude());
obj.put("Lng", LocationUpdater.userlocation.getLongitude());
}
else{
obj.put("Lat", "Unknown");
obj.put("Lng", "Unknown");
}
obj.put("Time", System.currentTimeMillis() );
obj.put("CellResidenceTime", SignalStrengthListener.CellHoldTime);
return obj.toString(2);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
public static String PhoneStateToJson(){
JSONObject obj = new JSONObject();
try {
if( FileFirstWrite ) {
RunIntentService2.recordwriter.write("[\n");
FileFirstWrite = false;
}
obj.put("AppicationType", "Android");
obj.put("ApplicationVersion", Build.VERSION.RELEASE);
obj.put("Account", Account);
obj.put("Password", Password);
obj.put("equipmentId", MainActivity.tm.getDeviceId());
obj.put("Event", "PhoneStateChanged");
obj.put("BatteryLevel", BatteryInfoReceiver.level);
obj.put("Time", System.currentTimeMillis());
obj.put("PhoneState", PSListener.PhoneState );
if( SignalStrengthListener.CellInfoType.equals("LTE") )
obj.put("CellID", SignalStrengthListener.AtCellID );
else if( SignalStrengthListener.CellInfoType.equals("Wcdma") )
obj.put("CellID", SignalStrengthListener.WcdmaAtCellID );
if( PSListener.PhoneState.equals("IDLE") ) {
obj.put("CallHoldingTime", PSListener.CallHoldingTime);
obj.put("ExcessLife", PSListener.ExcessLife);
}
return obj.toString(2);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
public static String AppsInfoToJson(){
JSONObject obj = new JSONObject();
try {
if( FileFirstWrite ) {
RunIntentService2.recordwriter.write("[\n");
FileFirstWrite = false;
}
obj.put("AppicationType", "Android");
obj.put("ApplicationVersion", Build.VERSION.RELEASE);
obj.put("Account", Account);
obj.put("Password", Password);
obj.put("equipmentId", MainActivity.tm.getDeviceId());
obj.put("Event", "AppsInfo");
obj.put("BatteryLevel", BatteryInfoReceiver.level);
obj.put("Time", System.currentTimeMillis());
obj.put("LastTime", System.currentTimeMillis() - MainActivity.StartServiceTime );
//get the apps' usage
JSONArray ary = new JSONArray();
for( Integer appuid: MainActivity.latest.delta.keySet() ){
if( MainActivity.latest.apps.get(appuid).tx==0 ){
continue;
}
String appName = MainActivity.latest.delta.get(appuid).tag;
TrafficSnapshot.TrafficRecord trafficrecord = MainActivity.latest.delta.get(appuid);
//Log.d(TagName, "appName:" + appName);
JSONObject appinfo = new JSONObject();
appinfo.put("AppName", appName);
appinfo.put("TotalTx", MainActivity.latest.apps.get(appuid).tx);
appinfo.put("TotalRx", MainActivity.latest.apps.get(appuid).rx);
appinfo.put("DeltaTx", trafficrecord.tx);
appinfo.put("DeltaRx", trafficrecord.rx);
ary.put(appinfo);
}
obj.put("AppsInfo", ary);
return obj.toString(2);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
}