Loading core/jni/android_net_TrafficStats.cpp +33 −16 Original line number Original line Diff line number Diff line Loading @@ -67,10 +67,35 @@ static jlong readNumber(char const* filename) { #endif #endif } } // Return the number from the first file which exists and contains data static const char* mobile_iface_list[] = { static jlong tryBoth(char const* a, char const* b) { "rmnet0", jlong num = readNumber(a); "rmnet1", return num >= 0 ? num : readNumber(b); "rmnet2", "rmnet3", "ppp0", 0 }; static jlong getAll(const char** iface_list, const char* what) { char filename[80]; int idx = 0; bool supported = false; jlong total = 0; while (iface_list[idx] != 0) { snprintf(filename, sizeof(filename), "/sys/class/net/%s/statistics/%s", iface_list[idx], what); jlong number = readNumber(filename); if (number >= 0) { supported = true; total += number; } idx++; } if (supported) return total; return -1; } } // Returns the sum of numbers from the specified path under /sys/class/net/*, // Returns the sum of numbers from the specified path under /sys/class/net/*, Loading Loading @@ -108,27 +133,19 @@ static jlong readTotal(char const* suffix) { // each file every time (rather than caching which ones exist). // each file every time (rather than caching which ones exist). static jlong getMobileTxPackets(JNIEnv* env, jobject clazz) { static jlong getMobileTxPackets(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "tx_packets"); "/sys/class/net/rmnet0/statistics/tx_packets", "/sys/class/net/ppp0/statistics/tx_packets"); } } static jlong getMobileRxPackets(JNIEnv* env, jobject clazz) { static jlong getMobileRxPackets(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "rx_packets"); "/sys/class/net/rmnet0/statistics/rx_packets", "/sys/class/net/ppp0/statistics/rx_packets"); } } static jlong getMobileTxBytes(JNIEnv* env, jobject clazz) { static jlong getMobileTxBytes(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "tx_bytes"); "/sys/class/net/rmnet0/statistics/tx_bytes", "/sys/class/net/ppp0/statistics/tx_bytes"); } } static jlong getMobileRxBytes(JNIEnv* env, jobject clazz) { static jlong getMobileRxBytes(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "rx_bytes"); "/sys/class/net/rmnet0/statistics/rx_bytes", "/sys/class/net/ppp0/statistics/rx_bytes"); } } static jlong getData(JNIEnv* env, const char* what, jstring javaInterface) { static jlong getData(JNIEnv* env, const char* what, jstring javaInterface) { Loading Loading
core/jni/android_net_TrafficStats.cpp +33 −16 Original line number Original line Diff line number Diff line Loading @@ -67,10 +67,35 @@ static jlong readNumber(char const* filename) { #endif #endif } } // Return the number from the first file which exists and contains data static const char* mobile_iface_list[] = { static jlong tryBoth(char const* a, char const* b) { "rmnet0", jlong num = readNumber(a); "rmnet1", return num >= 0 ? num : readNumber(b); "rmnet2", "rmnet3", "ppp0", 0 }; static jlong getAll(const char** iface_list, const char* what) { char filename[80]; int idx = 0; bool supported = false; jlong total = 0; while (iface_list[idx] != 0) { snprintf(filename, sizeof(filename), "/sys/class/net/%s/statistics/%s", iface_list[idx], what); jlong number = readNumber(filename); if (number >= 0) { supported = true; total += number; } idx++; } if (supported) return total; return -1; } } // Returns the sum of numbers from the specified path under /sys/class/net/*, // Returns the sum of numbers from the specified path under /sys/class/net/*, Loading Loading @@ -108,27 +133,19 @@ static jlong readTotal(char const* suffix) { // each file every time (rather than caching which ones exist). // each file every time (rather than caching which ones exist). static jlong getMobileTxPackets(JNIEnv* env, jobject clazz) { static jlong getMobileTxPackets(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "tx_packets"); "/sys/class/net/rmnet0/statistics/tx_packets", "/sys/class/net/ppp0/statistics/tx_packets"); } } static jlong getMobileRxPackets(JNIEnv* env, jobject clazz) { static jlong getMobileRxPackets(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "rx_packets"); "/sys/class/net/rmnet0/statistics/rx_packets", "/sys/class/net/ppp0/statistics/rx_packets"); } } static jlong getMobileTxBytes(JNIEnv* env, jobject clazz) { static jlong getMobileTxBytes(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "tx_bytes"); "/sys/class/net/rmnet0/statistics/tx_bytes", "/sys/class/net/ppp0/statistics/tx_bytes"); } } static jlong getMobileRxBytes(JNIEnv* env, jobject clazz) { static jlong getMobileRxBytes(JNIEnv* env, jobject clazz) { return tryBoth( return getAll(mobile_iface_list, "rx_bytes"); "/sys/class/net/rmnet0/statistics/rx_bytes", "/sys/class/net/ppp0/statistics/rx_bytes"); } } static jlong getData(JNIEnv* env, const char* what, jstring javaInterface) { static jlong getData(JNIEnv* env, const char* what, jstring javaInterface) { Loading