Add Socket tagging for granular data accounting.
Jeff Sharkey [Fri, 22 Apr 2011 20:36:59 +0000 (13:36 -0700)]
Tag active downloads, and account data usage towards UID that made
the original request.  Also release WakeLock only after we've cleaned
up the download.

Change-Id: I72d58c6a51beaeb357e59aae4d7c0f5ac9abaa8d

AndroidManifest.xml
src/com/android/providers/downloads/DownloadThread.java

index ad621af..d41702d 100644 (file)
@@ -52,6 +52,7 @@
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.INSTALL_DRM" />
     <uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" />
+    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
 
     <application android:process="android.process.media"
                  android:label="@string/app_label">
index fbd3b82..082caa2 100644 (file)
@@ -24,6 +24,7 @@ import android.content.Intent;
 import android.drm.mobile1.DrmRawContent;
 import android.net.http.AndroidHttpClient;
 import android.net.Proxy;
+import android.net.TrafficStats;
 import android.os.FileUtils;
 import android.os.PowerManager;
 import android.os.Process;
@@ -138,13 +139,17 @@ public class DownloadThread extends Thread {
             wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
             wakeLock.acquire();
 
-
             if (Constants.LOGV) {
                 Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
             }
 
             client = AndroidHttpClient.newInstance(userAgent(), mContext);
 
+            // network traffic on this thread should be counted against the
+            // requesting uid, and is tagged with well-known value.
+            TrafficStats.setThreadStatsTag("android:DownloadManager");
+            TrafficStats.setThreadStatsUid(mInfo.mUid);
+
             boolean finished = false;
             while(!finished) {
                 Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
@@ -186,10 +191,9 @@ public class DownloadThread extends Thread {
             finalStatus = Downloads.Impl.STATUS_UNKNOWN_ERROR;
             // falls through to the code that reports an error
         } finally {
-            if (wakeLock != null) {
-                wakeLock.release();
-                wakeLock = null;
-            }
+            TrafficStats.clearThreadStatsTag();
+            TrafficStats.clearThreadStatsUid();
+
             if (client != null) {
                 client.close();
                 client = null;
@@ -199,6 +203,11 @@ public class DownloadThread extends Thread {
                                     state.mGotData, state.mFilename,
                                     state.mNewUri, state.mMimeType, errorMsg);
             DownloadHandler.getInstance().dequeueDownload(mInfo.mId);
+
+            if (wakeLock != null) {
+                wakeLock.release();
+                wakeLock = null;
+            }
         }
         mStorageManager.incrementNumDownloadsSoFar();
     }