Update each Tab when the bookmarks database changes.

Bug:3222677

When the bookmarks database changes, we may have added or
removed a bookmark.  For each Tab, make sure that it knows
whether or not it is a bookmark, and update the UI
accordingly.

Change-Id: Ie91008973c2841dc7367e38ad5db40bf25b1dddf
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 3e79edb..5b536f8 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -503,39 +503,7 @@
             // finally update the UI in the activity if it is in the foreground
             mWebViewController.onPageStarted(Tab.this, view, url, favicon);
 
-            final String urlInQuestion = url;
-            if (mBookmarkAsyncTask != null) {
-                mBookmarkAsyncTask.cancel(true);
-            }
-            mBookmarkAsyncTask = new AsyncTask<Void, Void, Boolean>() {
-                @Override
-                protected Boolean doInBackground(Void... unused) {
-                    // Check to see if the site is bookmarked
-                    Cursor cursor = null;
-                    try {
-                        cursor = mActivity.getContentResolver().query(
-                                BrowserContract.Bookmarks.CONTENT_URI,
-                                new String[] { BrowserContract.Bookmarks.URL },
-                                BrowserContract.Bookmarks.URL + " == ?",
-                                new String[] { urlInQuestion },
-                                null);
-                        return cursor.moveToFirst();
-                    } catch (SQLiteException e) {
-                        Log.e(LOGTAG, "Error checking for bookmark: " + e);
-                        return false;
-                    } finally {
-                        if (cursor != null) cursor.close();
-                    }
-                }
-                @Override
-                protected void onPostExecute(Boolean isBookmarked) {
-                    if (this == mBookmarkAsyncTask) {
-                        mIsBookmarkedSite = isBookmarked;
-                        mWebViewController.bookmarkedStatusHasChanged(Tab.this);
-                    }
-                }
-            };
-            mBookmarkAsyncTask.execute();
+            updateBookmarkedStatusForUrl(url);
         }
 
         @Override
@@ -1450,6 +1418,7 @@
         if (mQueuedErrors != null && mQueuedErrors.size() >  0) {
             showError(mQueuedErrors.getFirst());
         }
+        mWebViewController.bookmarkedStatusHasChanged(this);
     }
 
     void putInBackground() {
@@ -1827,4 +1796,55 @@
         return true;
     }
 
+    public void updateBookmarkedStatus() {
+        if (mMainView == null) {
+            return;
+        }
+        String url = mMainView.getUrl();
+        if (url == null) {
+            return;
+        }
+        updateBookmarkedStatusForUrl(url);
+    }
+
+    /**
+     * Update mIsBookmarkedSite, using urlInQuestion to compare.
+     * @param urlInQuestion URL of the current page, to be checked in the
+     *          bookmarks database.
+     */
+    private void updateBookmarkedStatusForUrl(final String urlInQuestion) {
+        if (mBookmarkAsyncTask != null) {
+            mBookmarkAsyncTask.cancel(true);
+        }
+        mBookmarkAsyncTask = new AsyncTask<Void, Void, Boolean>() {
+            @Override
+            protected Boolean doInBackground(Void... unused) {
+                // Check to see if the site is bookmarked
+                Cursor cursor = null;
+                try {
+                    cursor = mActivity.getContentResolver().query(
+                            BrowserContract.Bookmarks.CONTENT_URI,
+                            new String[] { BrowserContract.Bookmarks.URL },
+                            BrowserContract.Bookmarks.URL + " == ?",
+                            new String[] { urlInQuestion },
+                            null);
+                    return cursor.moveToFirst();
+                } catch (SQLiteException e) {
+                    Log.e(LOGTAG, "Error checking for bookmark: " + e);
+                    return false;
+                } finally {
+                    if (cursor != null) cursor.close();
+                }
+            }
+            @Override
+            protected void onPostExecute(Boolean isBookmarked) {
+                if (this == mBookmarkAsyncTask) {
+                    mIsBookmarkedSite = isBookmarked;
+                    mWebViewController.bookmarkedStatusHasChanged(Tab.this);
+                }
+            }
+        };
+        mBookmarkAsyncTask.execute();
+    }
+
 }