Add TrafficStats integration

Allow Request subclasses to set their TrafficStats tags. By default,
this is the hostname's hashCode() so that requests to different hosts
have different colors in DDMS.

Change-Id: Ibedf06fdcee108dcd170f5eb70aaecea5647d55d
diff --git a/Android.mk b/Android.mk
index c7cbba3..553eae8 100644
--- a/Android.mk
+++ b/Android.mk
@@ -19,7 +19,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := volley
-LOCAL_SDK_VERSION := 8
+LOCAL_SDK_VERSION := 14
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/project.properties b/project.properties
index ea89160..730e911 100644
--- a/project.properties
+++ b/project.properties
@@ -8,4 +8,4 @@
 # project structure.
 
 # Project target.
-target=android-8
+target=android-14
diff --git a/src/com/android/volley/NetworkDispatcher.java b/src/com/android/volley/NetworkDispatcher.java
index 53fa288..0a82c81 100644
--- a/src/com/android/volley/NetworkDispatcher.java
+++ b/src/com/android/volley/NetworkDispatcher.java
@@ -16,6 +16,8 @@
 
 package com.android.volley;
 
+import android.net.TrafficStats;
+import android.os.Build;
 import android.os.Process;
 
 import java.util.concurrent.BlockingQueue;
@@ -94,6 +96,11 @@
                     continue;
                 }
 
+                // Tag the request (if API >= 14)
+                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+                    TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
+                }
+
                 // Perform the network request.
                 NetworkResponse networkResponse = mNetwork.performRequest(request);
                 request.addMarker("network-http-complete");
diff --git a/src/com/android/volley/Request.java b/src/com/android/volley/Request.java
index f37727d..9fee0a0 100644
--- a/src/com/android/volley/Request.java
+++ b/src/com/android/volley/Request.java
@@ -16,11 +16,14 @@
 
 package com.android.volley;
 
-import com.android.volley.VolleyLog.MarkerLog;
-
+import android.net.TrafficStats;
+import android.net.Uri;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.SystemClock;
+import android.text.TextUtils;
+
+import com.android.volley.VolleyLog.MarkerLog;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
@@ -59,6 +62,9 @@
     /** URL of this request. */
     private final String mUrl;
 
+    /** Default tag for {@link TrafficStats}. */
+    private final int mDefaultTrafficStatsTag;
+
     /** Listener interface for errors. */
     private final Response.ErrorListener mErrorListener;
 
@@ -119,6 +125,8 @@
         mUrl = url;
         mErrorListener = listener;
         setRetryPolicy(new DefaultRetryPolicy());
+
+        mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode();
     }
 
     /**
@@ -145,6 +153,13 @@
     }
 
     /**
+     * @return A tag for use with {@link TrafficStats#setThreadStatsTag(int)}
+     */
+    public int getTrafficStatsTag() {
+        return mDefaultTrafficStatsTag;
+    }
+
+    /**
      * Sets the retry policy for this request.
      */
     public void setRetryPolicy(RetryPolicy retryPolicy) {
@@ -521,6 +536,8 @@
 
     @Override
     public String toString() {
-        return (mCanceled ? "[X] " : "[ ] ") + getUrl() + " " + getPriority() + " " + mSequence;
+        String trafficStatsTag = "0x" + Integer.toHexString(getTrafficStatsTag());
+        return (mCanceled ? "[X] " : "[ ] ") + getUrl() + " " + trafficStatsTag + " "
+                + getPriority() + " " + mSequence;
     }
 }