Fix miniature thumbnail problem and delay saving of a screenshot by 500ms after a page has completed loading to ensure the WebView has completed painting and that we therefore save an accurate thumbnail.

Change-Id: I77f5d12062de0b9703e5d3ec0c53910f4d62743d
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 589fff0..f898d11 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -2316,12 +2316,15 @@
     private static final int CANCEL_CREDS_REQUEST    = 103;
     private static final int RELEASE_WAKELOCK        = 107;
 
+    private static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
+
     // Private handler for handling javascript and saving passwords
     private Handler mHandler = new Handler() {
 
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case FOCUS_NODE_HREF:
+                {
                     String url = (String) msg.getData().get("url");
                     if (url == null || url.length() == 0) {
                         break;
@@ -2364,6 +2367,7 @@
                             break;
                     }
                     break;
+                }
 
                 case LOAD_URL:
                     loadURL(getTopWindow(), (String) msg.obj);
@@ -2382,6 +2386,13 @@
                         mWakeLock.release();
                     }
                     break;
+
+                case UPDATE_BOOKMARK_THUMBNAIL:
+                    WebView view = (WebView) msg.obj;
+                    if (view != null) {
+                        updateScreenshot(view);
+                    }
+                    break;
             }
         }
     };
@@ -2459,10 +2470,10 @@
         Canvas canvas = new Canvas(bm);
         // May need to tweak these values to determine what is the
         // best scale factor
-        int contentWidth = view.getContentWidth();
-        if (contentWidth > 0) {
-            float scaleFactor = (float) getDesiredThumbnailWidth(this)
-                    / (float) contentWidth;
+        int thumbnailWidth = thumbnail.getWidth();
+        if (thumbnailWidth > 0) {
+            float scaleFactor = (float) getDesiredThumbnailWidth(this) /
+                    (float)thumbnailWidth;
             canvas.scale(scaleFactor, scaleFactor);
         }
         thumbnail.draw(canvas);
@@ -2505,6 +2516,12 @@
             resetLockIcon(url);
             setUrlTitle(url, null);
 
+            // We've started to load a new page. If there was a pending message
+            // to save a screenshot then we will now take the new page and
+            // save an incorrect screenshot. Therefore, remove any pending
+            // thumbnail messages from the queue.
+            mHandler.removeMessages(UPDATE_BOOKMARK_THUMBNAIL);
+
             // If we start a touch icon load and then load a new page, we don't
             // want to cancel the current touch icon loader. But, we do want to
             // create a new one when the touch icon url is known.
@@ -2585,7 +2602,8 @@
             if (!mDidStopLoad) {
                 // Only update the bookmark screenshot if the user did not
                 // cancel the load early.
-                updateScreenshot(view);
+                Message updateScreenshot = Message.obtain(mHandler, UPDATE_BOOKMARK_THUMBNAIL, view);
+                mHandler.sendMessageDelayed(updateScreenshot, 500);
             }
 
             // Update the lock icon image only once we are done loading
@@ -2903,8 +2921,6 @@
             Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
                     + " " + description);
 
-                mNeedExtraScreenShot = true;
-
             // We need to reset the title after an error.
             resetTitleAndRevertLockIcon();
         }
@@ -3234,14 +3250,6 @@
                         hideFakeTitleBar();
                     }
                 }
-                if (mNeedExtraScreenShot) {
-                    // if there was an error loading this page, capture a new
-                    // screenshot to ensure that we get the correct thumbnail
-                    // as onPageFinished may have been called before the error
-                    // page was displayed.
-                    updateScreenshot(view);
-                    mNeedExtraScreenShot = false;
-                }
             } else if (!mInLoad) {
                 // onPageFinished may have already been called but a subframe
                 // is still loading and updating the progress. Reset mInLoad
@@ -4413,10 +4421,6 @@
     private boolean mPageStarted;
     private boolean mActivityInPause = true;
 
-    // If the frame fails to load, we should snap a second screenshot
-    // to ensure that we get the right thumbnail (i.e. of the error page).
-    private boolean mNeedExtraScreenShot = false;
-
     private boolean mMenuIsDown;
 
     private static boolean mInTrace;