Show a highlighted star for bookmarked pages.

Bug:3222677

Change-Id: Ifeb6e7a922c0defb1e4a88ded0c188b97e0a4a56
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index f5851df..c4f3bd5 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -256,6 +256,15 @@
     }
 
     @Override
+    public void bookmarkedStatusHasChanged(Tab tab) {
+        if (tab.inForeground() && mXLargeScreenSize) {
+            boolean isBookmark = tab.isBookmarkedSite();
+            ((TitleBarXLarge) mTitleBar).setCurrentUrlIsBookmark(isBookmark);
+            ((TitleBarXLarge) mFakeTitleBar).setCurrentUrlIsBookmark(isBookmark);
+        }
+    }
+
+    @Override
     public void onPageFinished(Tab tab, String url) {
         if (mXLargeScreenSize) {
             mTabBar.onPageFinished(tab);
@@ -336,6 +345,7 @@
             // Request focus on the top window.
             mTabBar.onSetActiveTab(tab);
         }
+        bookmarkedStatusHasChanged(tab);
         resetTitleIconAndProgress(tab);
         updateLockIconToLatest(tab);
         tab.getTopWindow().requestFocus();
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 5642d4d..7acdfe5 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1026,6 +1026,11 @@
         }
     }
 
+    @Override
+    public void bookmarkedStatusHasChanged(Tab tab) {
+        mUi.bookmarkedStatusHasChanged(tab);
+    }
+
     // end WebViewController
 
     protected void pageUp() {
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 8a3bc27..3e79edb 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -25,12 +25,16 @@
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnCancelListener;
 import android.content.Intent;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteException;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.net.http.SslError;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Message;
 import android.os.SystemClock;
+import android.provider.BrowserContract;
 import android.speech.RecognizerResultsIntent;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -144,6 +148,14 @@
         Bitmap  mFavicon;
     }
 
+    // Whether or not the currently shown page is a bookmarked site.  Will be
+    // out of date when loading a new page until the mBookmarkAsyncTask returns.
+    private boolean mIsBookmarkedSite;
+    // Used to determine whether the current site is bookmarked.
+    private AsyncTask<Void, Void, Boolean> mBookmarkAsyncTask;
+
+    public boolean isBookmarkedSite() { return mIsBookmarkedSite; }
+
     // Used for saving and restoring each Tab
     static final String WEBVIEW = "webview";
     static final String NUMTABS = "numTabs";
@@ -488,9 +500,42 @@
                 }
             }
 
-
             // 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();
         }
 
         @Override
diff --git a/src/com/android/browser/TitleBarXLarge.java b/src/com/android/browser/TitleBarXLarge.java
index 0aa09db..cd3b230 100644
--- a/src/com/android/browser/TitleBarXLarge.java
+++ b/src/com/android/browser/TitleBarXLarge.java
@@ -26,9 +26,11 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
+import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.widget.CheckBox;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -48,7 +50,7 @@
     private View mContainer;
     private View mBackButton;
     private View mForwardButton;
-    private View mStar;
+    private CheckBox mStar;
     private View mSearchButton;
     private View mFocusContainer;
     private View mUnfocusContainer;
@@ -82,7 +84,7 @@
         // back/forward.  Probably should be done inside onPageStarted.
         mBackButton = findViewById(R.id.back);
         mForwardButton = findViewById(R.id.forward);
-        mStar = findViewById(R.id.star);
+        mStar = (CheckBox) findViewById(R.id.star);
         mStopButton = (ImageView) findViewById(R.id.stop);
         mSearchButton = findViewById(R.id.search);
         mLockIcon = (ImageView) findViewById(R.id.lock);
@@ -106,6 +108,10 @@
         mUnfocusContainer.setOnClickListener(this);
     }
 
+    public void setCurrentUrlIsBookmark(boolean isBookmark) {
+        mStar.setChecked(isBookmark);
+    }
+
     @Override
     public void onClick(View v) {
         if (mUnfocusContainer == v) {
@@ -237,4 +243,25 @@
         mUrlUnfocused.setText(title);
     }
 
+    /**
+     * Custom CheckBox which does not toggle when pressed.  Used by mStar.
+     */
+    public static class CustomCheck extends CheckBox {
+        public CustomCheck(Context context) {
+            super(context);
+        }
+
+        public CustomCheck(Context context, AttributeSet attrs) {
+            super(context, attrs);
+        }
+
+        public CustomCheck(Context context, AttributeSet attrs, int defStyle) {
+            super(context, attrs, defStyle);
+        }
+
+        @Override
+        public void toggle() {
+            // Do nothing
+        }
+    }
 }
diff --git a/src/com/android/browser/UI.java b/src/com/android/browser/UI.java
index e7f67f2..2bfec44 100644
--- a/src/com/android/browser/UI.java
+++ b/src/com/android/browser/UI.java
@@ -130,4 +130,5 @@
 
     View getVideoLoadingProgressView();
 
+    void bookmarkedStatusHasChanged(Tab tab);
 }
diff --git a/src/com/android/browser/WebViewController.java b/src/com/android/browser/WebViewController.java
index 11a6959..894bbec 100644
--- a/src/com/android/browser/WebViewController.java
+++ b/src/com/android/browser/WebViewController.java
@@ -107,4 +107,6 @@
 
     void setupAutoFill(Message message);
 
+    void bookmarkedStatusHasChanged(Tab tab);
+
 }