summaryrefslogtreecommitdiffstats
path: root/core/java/android/net/TrafficStats.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2011-04-21 18:45:43 -0700
committerJeff Sharkey <jsharkey@android.com>2011-05-13 18:19:00 -0700
commit43be174888684ef3404a43d8434015193c656cce (patch)
tree9e1f70d853de619e8c3222d195b46b1f1b455a9e /core/java/android/net/TrafficStats.java
parent8c2f85d94145a96f53e9041c609e283be7412a0f (diff)
downloadframeworks_base-43be174888684ef3404a43d8434015193c656cce.zip
frameworks_base-43be174888684ef3404a43d8434015193c656cce.tar.gz
frameworks_base-43be174888684ef3404a43d8434015193c656cce.tar.bz2
Add Socket tagging for granular data accounting.
Introduces public API to apply "tags" to track data traffic originating from the current thread. (Under the hood, the tags are maintained and applied in BlockGuard.) Also adds tag/untag methods for developers who maintain their own Socket pools. Change-Id: Ic2dd3155559a93a7b613c7853748d4c44fb3a39e
Diffstat (limited to 'core/java/android/net/TrafficStats.java')
-rw-r--r--core/java/android/net/TrafficStats.java64
1 files changed, 60 insertions, 4 deletions
diff --git a/core/java/android/net/TrafficStats.java b/core/java/android/net/TrafficStats.java
index eca06c5..7ee7a81 100644
--- a/core/java/android/net/TrafficStats.java
+++ b/core/java/android/net/TrafficStats.java
@@ -16,11 +16,10 @@
package android.net;
-import android.util.Log;
+import dalvik.system.BlockGuard;
-import java.io.File;
-import java.io.RandomAccessFile;
-import java.io.IOException;
+import java.net.Socket;
+import java.net.SocketException;
/**
* Class that provides network traffic statistics. These statistics include
@@ -37,6 +36,63 @@ public class TrafficStats {
public final static int UNSUPPORTED = -1;
/**
+ * Set active tag to use when accounting {@link Socket} traffic originating
+ * from the current thread. Only one active tag per thread is supported.
+ * <p>
+ * Changes only take effect during subsequent calls to
+ * {@link #tagSocket(Socket)}.
+ */
+ public static void setThreadStatsTag(String tag) {
+ BlockGuard.setThreadSocketStatsTag(tag);
+ }
+
+ public static void clearThreadStatsTag() {
+ BlockGuard.setThreadSocketStatsTag(null);
+ }
+
+ /**
+ * Set specific UID to use when accounting {@link Socket} traffic
+ * originating from the current thread. Designed for use when performing an
+ * operation on behalf of another application.
+ * <p>
+ * Changes only take effect during subsequent calls to
+ * {@link #tagSocket(Socket)}.
+ * <p>
+ * To take effect, caller must hold
+ * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
+ *
+ * {@hide}
+ */
+ public static void setThreadStatsUid(int uid) {
+ BlockGuard.setThreadSocketStatsUid(uid);
+ }
+
+ /** {@hide} */
+ public static void clearThreadStatsUid() {
+ BlockGuard.setThreadSocketStatsUid(-1);
+ }
+
+ /**
+ * Tag the given {@link Socket} with any statistics parameters active for
+ * the current thread. Subsequent calls always replace any existing
+ * parameters. When finished, call {@link #untagSocket(Socket)} to remove
+ * statistics parameters.
+ *
+ * @see #setThreadStatsTag(String)
+ * @see #setThreadStatsUid(int)
+ */
+ public static void tagSocket(Socket socket) throws SocketException {
+ BlockGuard.tagSocketFd(socket.getFileDescriptor$());
+ }
+
+ /**
+ * Remove any statistics parameters from the given {@link Socket}.
+ */
+ public static void untagSocket(Socket socket) throws SocketException {
+ BlockGuard.untagSocketFd(socket.getFileDescriptor$());
+ }
+
+ /**
* Get the total number of packets transmitted through the mobile interface.
*
* @return number of packets. If the statistics are not supported by this device,