Support multi-valued HTTP headers, handle new API changes
Steve Howard [Wed, 22 Sep 2010 22:57:30 +0000 (15:57 -0700)]
* backend support for multiple values for the same HTTP headers, for
  corresponding API changes

* other minor changes in response to DownloadManager API changes

Change-Id: I7c595e94a60ed7afaca6cc3fb4c05aaeeff20c2a

src/com/android/providers/downloads/DownloadInfo.java
src/com/android/providers/downloads/DownloadThread.java
tests/public_api_access/src/com/android/providers/downloads/public_api_access_tests/PublicApiAccessTest.java
tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java

index 467af83..2196cfc 100644 (file)
@@ -30,10 +30,12 @@ import android.net.Uri;
 import android.provider.Downloads;
 import android.provider.Downloads.Impl;
 import android.util.Log;
+import android.util.Pair;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.List;
 
 /**
  * Stores information about an individual download.
@@ -106,21 +108,24 @@ public class DownloadInfo {
                 int valueIndex =
                         cursor.getColumnIndexOrThrow(Downloads.Impl.RequestHeaders.COLUMN_VALUE);
                 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
-                    info.mRequestHeaders.put(
-                            cursor.getString(headerIndex), cursor.getString(valueIndex));
+                    addHeader(info, cursor.getString(headerIndex), cursor.getString(valueIndex));
                 }
             } finally {
                 cursor.close();
             }
 
             if (info.mCookies != null) {
-                info.mRequestHeaders.put("Cookie", info.mCookies);
+                addHeader(info, "Cookie", info.mCookies);
             }
             if (info.mReferer != null) {
-                info.mRequestHeaders.put("Referer", info.mReferer);
+                addHeader(info, "Referer", info.mReferer);
             }
         }
 
+        private void addHeader(DownloadInfo info, String header, String value) {
+            info.mRequestHeaders.add(Pair.create(header, value));
+        }
+
         /**
          * Returns a String that holds the current value of the column, optimizing for the case
          * where the value hasn't changed.
@@ -228,7 +233,7 @@ public class DownloadInfo {
 
     public volatile boolean mHasActiveThread;
 
-    private Map<String, String> mRequestHeaders = new HashMap<String, String>();
+    private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
     private SystemFacade mSystemFacade;
     private Context mContext;
 
@@ -238,8 +243,8 @@ public class DownloadInfo {
         mFuzz = Helpers.sRandom.nextInt(1001);
     }
 
-    public Map<String, String> getHeaders() {
-        return Collections.unmodifiableMap(mRequestHeaders);
+    public Collection<Pair<String, String>> getHeaders() {
+        return Collections.unmodifiableList(mRequestHeaders);
     }
 
     public void sendIntentIfRequested() {
@@ -415,9 +420,6 @@ public class DownloadInfo {
             case ConnectivityManager.TYPE_WIFI:
                 return DownloadManager.Request.NETWORK_WIFI;
 
-            case ConnectivityManager.TYPE_WIMAX:
-                return DownloadManager.Request.NETWORK_WIMAX;
-
             default:
                 return 0;
         }
index 79778b0..12ddfa6 100644 (file)
@@ -28,6 +28,7 @@ import android.provider.Downloads;
 import android.provider.DrmStore;
 import android.util.Config;
 import android.util.Log;
+import android.util.Pair;
 
 import org.apache.http.Header;
 import org.apache.http.HttpResponse;
@@ -42,7 +43,6 @@ import java.io.SyncFailedException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Locale;
-import java.util.Map;
 
 /**
  * Runs an actual download
@@ -857,8 +857,8 @@ public class DownloadThread extends Thread {
      * Add custom headers for this download to the HTTP request.
      */
     private void addRequestHeaders(InnerState innerState, HttpGet request) {
-        for (Map.Entry<String, String> header : mInfo.getHeaders().entrySet()) {
-            request.addHeader(header.getKey(), header.getValue());
+        for (Pair<String, String> header : mInfo.getHeaders()) {
+            request.addHeader(header.first, header.second);
         }
 
         if (innerState.mContinuingDownload) {
index 3929c59..13bec33 100644 (file)
@@ -149,8 +149,8 @@ public class PublicApiAccessTest extends AndroidTestCase {
         request.setAllowedOverRoaming(false);
         request.setTitle("test");
         request.setDescription("test");
-        request.setMediaType("text/html");
-        request.setRequestHeader("X-Some-Header", "value");
+        request.setMimeType("text/html");
+        request.addRequestHeader("X-Some-Header", "value");
         mManager.enqueue(request);
     }
 }
index 6c81bc6..91cc3f3 100644 (file)
@@ -306,8 +306,8 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
 
     public void testRequestHeaders() throws Exception {
         enqueueEmptyResponse(HTTP_OK);
-        Download download = enqueueRequest(getRequest().setRequestHeader("Header1", "value1")
-                                           .setRequestHeader("Header2", "value2"));
+        Download download = enqueueRequest(getRequest().addRequestHeader("Header1", "value1")
+                                           .addRequestHeader("Header2", "value2"));
         download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL);
 
         List<String> headers = takeRequest().getHeaders();
@@ -316,7 +316,7 @@ public class PublicApiFunctionalTest extends AbstractPublicApiTest {
     }
 
     public void testDelete() throws Exception {
-        Download download = enqueueRequest(getRequest().setRequestHeader("header", "value"));
+        Download download = enqueueRequest(getRequest().addRequestHeader("header", "value"));
         mManager.remove(download.mId);
         Cursor cursor = mManager.query(new DownloadManager.Query());
         try {