Download a touch icon without putting it into the database.

Adds functionality to DownloadTouchIcon to pass a message back containing
the touch icon bitmap rather than inserting it into the bookmarks database.

This will be used by the installable web apps work.

Change-Id: Ic87a571b84bf5a940a6c55a59423cd8556ebcef1
diff --git a/src/com/android/browser/DownloadTouchIcon.java b/src/com/android/browser/DownloadTouchIcon.java
index b5369ae..99925dc 100644
--- a/src/com/android/browser/DownloadTouchIcon.java
+++ b/src/com/android/browser/DownloadTouchIcon.java
@@ -24,10 +24,11 @@
 import android.graphics.BitmapFactory;
 import android.net.http.AndroidHttpClient;
 import android.os.AsyncTask;
+import android.os.Bundle;
+import android.os.Message;
 import android.provider.Browser;
 import android.webkit.WebView;
 
-
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
@@ -42,9 +43,16 @@
     private Cursor mCursor;
     private final String mOriginalUrl;
     private final String mUrl;
-    private final String mUserAgent;
+    private final String mUserAgent; // Sites may serve a different icon to different UAs
+    private Message mMessage;
+
     /* package */ Tab mTab;
 
+    /**
+     * Use this ctor to store the touch icon in the bookmarks database for
+     * the originalUrl so we take account of redirects. Used when the user
+     * bookmarks a page from outside the bookmarks activity.
+     */
     public DownloadTouchIcon(Tab tab, ContentResolver cr, WebView view) {
         mTab = tab;
         mContentResolver = cr;
@@ -54,6 +62,13 @@
         mUserAgent = view.getSettings().getUserAgentString();
     }
 
+    /**
+     * Use this ctor to download the touch icon and update the bookmarks database
+     * entry for the given url. Used when the user creates a bookmark from
+     * within the bookmarks activity and there haven't been any redirects.
+     * TODO: Would be nice to set the user agent here so that there is no
+     * potential for the three different ctors here to return different icons.
+     */
     public DownloadTouchIcon(ContentResolver cr, String url) {
         mTab = null;
         mContentResolver = cr;
@@ -62,15 +77,32 @@
         mUserAgent = null;
     }
 
+    /**
+     * Use this ctor to not store the touch icon in a database, rather add it to
+     * the passed Message's data bundle with the key "touchIcon" and then send
+     * the message.
+     */
+    public DownloadTouchIcon(Message msg, String userAgent) {
+        mMessage = msg;
+        mContentResolver = null;
+        mOriginalUrl = null;
+        mUrl = null;
+        mUserAgent = userAgent;
+    }
+
     @Override
     public Void doInBackground(String... values) {
-        mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver,
-                mOriginalUrl, mUrl, true);
-        if (mCursor != null && mCursor.getCount() > 0) {
-            String url = values[0];
+        if (mContentResolver != null) {
+            mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver,
+                    mOriginalUrl, mUrl, true);
+        }
 
-            AndroidHttpClient client = AndroidHttpClient.newInstance(
-                    mUserAgent);
+        boolean inBookmarksDatabase = mCursor != null && mCursor.getCount() > 0;
+
+        String url = values[0];
+
+        if (inBookmarksDatabase || mMessage != null) {
+            AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
             HttpGet request = new HttpGet(url);
 
             // Follow redirects
@@ -78,7 +110,6 @@
 
             try {
                 HttpResponse response = client.execute(request);
-
                 if (response.getStatusLine().getStatusCode() == 200) {
                     HttpEntity entity = response.getEntity();
                     if (entity != null) {
@@ -86,7 +117,12 @@
                         if (content != null) {
                             Bitmap icon = BitmapFactory.decodeStream(
                                     content, null, null);
-                            storeIcon(icon);
+                            if (inBookmarksDatabase) {
+                                storeIcon(icon);
+                            } else if (mMessage != null) {
+                                Bundle b = mMessage.getData();
+                                b.putParcelable("touchIcon", icon);
+                            }
                         }
                     }
                 }
@@ -98,9 +134,15 @@
                 client.close();
             }
         }
+
         if (mCursor != null) {
             mCursor.close();
         }
+
+        if (mMessage != null) {
+            mMessage.sendToTarget();
+        }
+
         return null;
     }