1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net; 6 7 import android.net.TrafficStats; 8 import android.os.Process; 9 10 import org.jni_zero.CalledByNative; 11 import org.jni_zero.JNINamespace; 12 13 /** This class interacts with TrafficStats API provided by Android. */ 14 @JNINamespace("net::android::traffic_stats") 15 public class AndroidTrafficStats { AndroidTrafficStats()16 private AndroidTrafficStats() {} 17 18 /** 19 * @return Number of bytes transmitted since device boot. Counts packets across all network 20 * interfaces, and always increases monotonically since device boot. Statistics are 21 * measured at the network layer, so they include both TCP and UDP usage. 22 */ 23 @CalledByNative getTotalTxBytes()24 private static long getTotalTxBytes() { 25 long bytes = TrafficStats.getTotalTxBytes(); 26 return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED; 27 } 28 29 /** 30 * @return Number of bytes received since device boot. Counts packets across all network 31 * interfaces, and always increases monotonically since device boot. Statistics are 32 * measured at the network layer, so they include both TCP and UDP usage. 33 */ 34 @CalledByNative getTotalRxBytes()35 private static long getTotalRxBytes() { 36 long bytes = TrafficStats.getTotalRxBytes(); 37 return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED; 38 } 39 40 /** 41 * @return Number of bytes transmitted since device boot that were attributed to caller's UID. 42 * Counts packets across all network interfaces, and always increases monotonically 43 * since device boot. Statistics are measured at the network layer, so they include 44 * both TCP and UDP usage. 45 */ 46 @CalledByNative getCurrentUidTxBytes()47 private static long getCurrentUidTxBytes() { 48 long bytes = TrafficStats.getUidTxBytes(Process.myUid()); 49 return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED; 50 } 51 52 /** 53 * @return Number of bytes received since device boot that were attributed to caller's UID. 54 * Counts packets across all network interfaces, and always increases monotonically 55 * since device boot. Statistics are measured at the network layer, so they include 56 * both TCP and UDP usage. 57 */ 58 @CalledByNative getCurrentUidRxBytes()59 private static long getCurrentUidRxBytes() { 60 long bytes = TrafficStats.getUidRxBytes(Process.myUid()); 61 return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED; 62 } 63 } 64