Always update screenshots for bookmarks

 Bug: 3425221
 Changes the call to updateScreenshot to take the Tab so that it
 can more intelligently determine if it needs to update the screenshot.
 Specifically, if it is an HTTP/HTTPS URL, we always update (for things
 like most visited), and if it is a bookmark we always update (users
 are free to bookmark file:// urls, for example)

Change-Id: I582284d612e77190404eb98de6ca8a0b21083ed1
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 74a66b1..f4588bd 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -508,9 +508,9 @@
                         break;
 
                     case UPDATE_BOOKMARK_THUMBNAIL:
-                        WebView view = (WebView) msg.obj;
-                        if (view != null) {
-                            updateScreenshot(view);
+                        Tab tab = (Tab) msg.obj;
+                        if (tab != null) {
+                            updateScreenshot(tab);
                         }
                         break;
                 }
@@ -747,7 +747,7 @@
         // an incorrect screenshot. Therefore, remove any pending thumbnail
         // messages from the queue.
         mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
-                view);
+                tab);
 
         // reset sync timer to avoid sync starts during loading a page
         CookieSyncManager.getInstance().resetSync();
@@ -794,7 +794,7 @@
                 // Only update the bookmark screenshot if the user did not
                 // cancel the load early.
                 mHandler.sendMessageDelayed(mHandler.obtainMessage(
-                        UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab.getWebView()),
+                        UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
                         500);
             }
         }
@@ -1905,13 +1905,27 @@
         return ret;
     }
 
-    private void updateScreenshot(WebView view) {
+    private void updateScreenshot(Tab tab) {
         // If this is a bookmarked site, add a screenshot to the database.
-        // FIXME: When should we update?  Every time?
         // FIXME: Would like to make sure there is actually something to
         // draw, but the API for that (WebViewCore.pictureReady()) is not
         // currently accessible here.
 
+        WebView view = tab.getWebView();
+        final String url = tab.getUrl();
+        final String originalUrl = view.getOriginalUrl();
+
+        if (TextUtils.isEmpty(url)) {
+            return;
+        }
+
+        // Only update thumbnails for web urls (http(s)://), not for
+        // about:, javascript:, data:, etc...
+        // Unless it is a bookmarked site, then always update
+        if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
+            return;
+        }
+
         final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
                 getDesiredThumbnailHeight(mActivity));
         if (bm == null) {
@@ -1919,41 +1933,34 @@
         }
 
         final ContentResolver cr = mActivity.getContentResolver();
-        final String url = view.getUrl();
-        final String originalUrl = view.getOriginalUrl();
+        new AsyncTask<Void, Void, Void>() {
+            @Override
+            protected Void doInBackground(Void... unused) {
+                Cursor cursor = null;
+                try {
+                    // TODO: Clean this up
+                    cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
+                    if (cursor != null && cursor.moveToFirst()) {
+                        final ByteArrayOutputStream os =
+                                new ByteArrayOutputStream();
+                        bm.compress(Bitmap.CompressFormat.PNG, 100, os);
 
-        // Only update thumbnails for web urls (http(s)://), not for
-        // about:, javascript:, data:, etc...
-        if (url != null && Patterns.WEB_URL.matcher(url).matches()) {
-            new AsyncTask<Void, Void, Void>() {
-                @Override
-                protected Void doInBackground(Void... unused) {
-                    Cursor cursor = null;
-                    try {
-                        // TODO: Clean this up
-                        cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
-                        if (cursor != null && cursor.moveToFirst()) {
-                            final ByteArrayOutputStream os =
-                                    new ByteArrayOutputStream();
-                            bm.compress(Bitmap.CompressFormat.PNG, 100, os);
+                        ContentValues values = new ContentValues();
+                        values.put(Images.THUMBNAIL, os.toByteArray());
+                        values.put(Images.URL, cursor.getString(0));
 
-                            ContentValues values = new ContentValues();
-                            values.put(Images.THUMBNAIL, os.toByteArray());
-                            values.put(Images.URL, cursor.getString(0));
-
-                            do {
-                                cr.update(Images.CONTENT_URI, values, null, null);
-                            } while (cursor.moveToNext());
-                        }
-                    } catch (IllegalStateException e) {
-                        // Ignore
-                    } finally {
-                        if (cursor != null) cursor.close();
+                        do {
+                            cr.update(Images.CONTENT_URI, values, null, null);
+                        } while (cursor.moveToNext());
                     }
-                    return null;
+                } catch (IllegalStateException e) {
+                    // Ignore
+                } finally {
+                    if (cursor != null) cursor.close();
                 }
-            }.execute();
-        }
+                return null;
+            }
+        }.execute();
     }
 
     private class Copy implements OnMenuItemClickListener {