diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 44e926a..1368e4a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="34" + android:versionName="2.5.1" > @@ -52,6 +52,5 @@ - \ No newline at end of file diff --git a/src/com/netease/qa/emmagee/utils/TrafficInfo.java b/src/com/netease/qa/emmagee/utils/TrafficInfo.java index 5e6a04f..c0f4b00 100644 --- a/src/com/netease/qa/emmagee/utils/TrafficInfo.java +++ b/src/com/netease/qa/emmagee/utils/TrafficInfo.java @@ -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; @@ -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; } + }