Skip to content

Commit

Permalink
Fixed: #94 Q2:read uid_stat to get traffic info while TrafficStats re…
Browse files Browse the repository at this point in the history
…turn 0
  • Loading branch information
andrewleo committed Aug 25, 2017
1 parent 14f8fcc commit 3af7a3c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
5 changes: 2 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netease.qa.emmagee"
android:versionCode="33"
android:versionName="2.5" >
android:versionCode="34"
android:versionName="2.5.1" >

<uses-sdk android:minSdkVersion="8" />

Expand Down Expand Up @@ -52,6 +52,5 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

</manifest>
Expand Down
55 changes: 46 additions & 9 deletions src/com/netease/qa/emmagee/utils/TrafficInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package com.netease.qa.emmagee.utils;

import java.io.IOException;
import java.io.RandomAccessFile;

import android.net.TrafficStats;
import android.util.Log;

Expand Down Expand Up @@ -43,18 +46,52 @@ public TrafficInfo(String uid) {
*/
public long getTrafficInfo() {
Log.i(LOG_TAG, "get traffic information");
Log.d(LOG_TAG, "uid = " + uid);
long traffic = trafficFromApi();
return traffic <= 0 ? trafficFromFiles() : traffic;
}

long rcvTraffic = UNSUPPORTED;
long sndTraffic = UNSUPPORTED;

// Use getUidRxBytes and getUidTxBytes to get network traffic,these API
// return both tcp and udp usage
/**
* Use TrafficStats getUidRxBytes and getUidTxBytes to get network
* traffic,these API return both tcp and udp usage
*
* @return
*/
private long trafficFromApi() {
long rcvTraffic = UNSUPPORTED, sndTraffic = UNSUPPORTED;
rcvTraffic = TrafficStats.getUidRxBytes(Integer.parseInt(uid));
sndTraffic = TrafficStats.getUidTxBytes(Integer.parseInt(uid));
return rcvTraffic + sndTraffic < 0 ? UNSUPPORTED : rcvTraffic + sndTraffic;
}

if (rcvTraffic == UNSUPPORTED || sndTraffic == UNSUPPORTED) {
return UNSUPPORTED;
} else
return rcvTraffic + sndTraffic;
/**
* read files in uid_stat to get traffic info
*
* @return
*/
private long trafficFromFiles() {
RandomAccessFile rafRcv = null, rafSnd = null;
long rcvTraffic = UNSUPPORTED, sndTraffic = UNSUPPORTED;
String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv";
String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd";
try {
rafRcv = new RandomAccessFile(rcvPath, "r");
rafSnd = new RandomAccessFile(sndPath, "r");
rcvTraffic = Long.parseLong(rafRcv.readLine());
sndTraffic = Long.parseLong(rafSnd.readLine());
Log.d(LOG_TAG, String.format("rcvTraffic, sndTraffic = %s, %s", rcvTraffic, sndTraffic));
} catch (Exception e) {
}
finally {
try {
if (rafRcv != null) {
rafRcv.close();
}
if (rafSnd != null)
rafSnd.close();
} catch (IOException e) {}
}
return rcvTraffic + sndTraffic < 0 ? UNSUPPORTED : rcvTraffic + sndTraffic;
}

}

0 comments on commit 3af7a3c

Please sign in to comment.