-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficSnapshot.java
68 lines (56 loc) · 2.19 KB
/
TrafficSnapshot.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
package edu.nctu.wirelab.testsignalv1;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.net.TrafficStats;
import android.util.Log;
import java.util.HashMap;
public class TrafficSnapshot{
String TagName = "TrafficSnapshot";
HashMap<Integer, TrafficRecord> apps = new HashMap<Integer, TrafficRecord>();
HashMap<Integer, TrafficRecord> delta;//AppName <-> delta usage (calculate by function CalculateUsage)
TrafficSnapshot(Context ctxt) {
HashMap<Integer, String> appNames=new HashMap<Integer, String>();
for (ApplicationInfo app : ctxt.getPackageManager().getInstalledApplications(0)) {
appNames.put(app.uid, app.packageName);
}
for (Integer uid : appNames.keySet()) {
apps.put(uid, new TrafficRecord(uid, appNames.get(uid)));
// Log.d(TagName, "uid:"+uid+" AppName:"+appNames.get(uid));
}
}
public class TrafficRecord{
long tx=0;
long rx=0;
String tag=null;
TrafficRecord() {
tx= TrafficStats.getTotalTxBytes();
rx=TrafficStats.getTotalRxBytes();
}
TrafficRecord(int uid, String tag) {
tx=TrafficStats.getUidTxBytes(uid);
rx=TrafficStats.getUidRxBytes(uid);
this.tag=tag;
}
}
public void CalculateUsage( TrafficSnapshot previous ){
delta = new HashMap<Integer, TrafficRecord>();
if( apps!=null && previous.apps!=null ){
for( Integer uid : apps.keySet() ){
String appName = apps.get(uid).tag;
TrafficRecord tr = new TrafficRecord();
if( previous.apps.get(uid).tag!=null && previous.apps.get(uid).tag.equals(appName) ){
tr.tx=apps.get(uid).tx-previous.apps.get(uid).tx;
tr.rx=apps.get(uid).rx-previous.apps.get(uid).rx;
tr.tag = appName;
delta.put(uid, tr);
}
else{
tr.tx=apps.get(uid).tx;
tr.rx=apps.get(uid).rx;
tr.tag = appName;
delta.put(uid, tr);
}
}
}
}
}