Give DownloadManager a useful user agent.
Jeff Sharkey [Fri, 6 Apr 2012 23:11:18 +0000 (16:11 -0700)]
Include platform version, model, and build, when available.  Matches
current logic used by Browser.

Bug: 6257336
Change-Id: I0addfd944aff13bd4ce4226bb8d6328da6b0f5d5

src/com/android/providers/downloads/Constants.java
src/com/android/providers/downloads/DownloadThread.java

index 977f00b..27e2179 100644 (file)
@@ -16,7 +16,9 @@
 
 package com.android.providers.downloads;
 
+import android.os.Build;
 import android.os.Environment;
+import android.text.TextUtils;
 import android.util.Log;
 
 /**
@@ -89,7 +91,37 @@ public class Constants {
     public static final String RECOVERY_DIRECTORY = "recovery";
 
     /** The default user agent used for downloads */
-    public static final String DEFAULT_USER_AGENT = "AndroidDownloadManager";
+    public static final String DEFAULT_USER_AGENT;
+
+    static {
+        final StringBuilder builder = new StringBuilder();
+
+        final boolean validRelease = !TextUtils.isEmpty(Build.VERSION.RELEASE);
+        final boolean validId = !TextUtils.isEmpty(Build.ID);
+        final boolean includeModel = "REL".equals(Build.VERSION.CODENAME)
+                && !TextUtils.isEmpty(Build.MODEL);
+
+        builder.append("AndroidDownloadManager");
+        if (validRelease) {
+            builder.append("/").append(Build.VERSION.RELEASE);
+        }
+        builder.append(" (Linux; U; Android");
+        if (validRelease) {
+            builder.append(" ").append(Build.VERSION.RELEASE);
+        }
+        if (includeModel || validId) {
+            builder.append(";");
+            if (includeModel) {
+                builder.append(" ").append(Build.MODEL);
+            }
+            if (validId) {
+                builder.append(" Build/").append(Build.ID);
+            }
+        }
+        builder.append(")");
+
+        DEFAULT_USER_AGENT = builder.toString();
+    }
 
     /** The MIME type of APKs */
     public static final String MIMETYPE_APK = "application/vnd.android.package";
index d8ea68d..7108cec 100644 (file)
@@ -72,8 +72,6 @@ public class DownloadThread extends Thread {
      */
     private String userAgent() {
         String userAgent = mInfo.mUserAgent;
-        if (userAgent != null) {
-        }
         if (userAgent == null) {
             userAgent = Constants.DEFAULT_USER_AGENT;
         }