Update the appearance of the title bar in the browser.

Change the title bar to use the look of the search bar.  Remove
the new tab button, which is now a part of the updated menu.  Make
the title bar for each WebView span the width of the screen.  Make
the title bar appear when pressing the menu button.

Change-Id: I7ad1c955412d422be0fc6f26ec8d286172a0a99a
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 0360a4f..5a27554 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -496,13 +496,13 @@
         }
 
         if (CUSTOM_BROWSER_BAR) {
+            mTitleBar.init(this);
             // Create title bars for all of the tabs that have been created
             for (int i = 0; i < mTabControl.getTabCount(); i ++) {
                 WebView view = mTabControl.getTab(i).getWebView();
                 mTitleBar.addTab(view, false);
             }
 
-            mTitleBar.setBrowserActivity(this);
             mTitleBar.setCurrentTab(mTabControl.getCurrentIndex());
         }
 
@@ -891,6 +891,12 @@
         }
     }
 
+    @Override
+    public boolean onMenuOpened(int featureId, Menu menu) {
+        mTitleBar.setVisibility(View.VISIBLE);
+        return true;
+    }
+
     /**
      *  onSaveInstanceState(Bundle map)
      *  onSaveInstanceState is called right before onStop(). The map contains
@@ -1293,6 +1299,11 @@
         }
         switch (item.getItemId()) {
             // -- Main menu
+            case R.id.new_tab_menu_id:
+                openTabAndShow(EMPTY_URL_DATA, false, null);
+                bookmarksOrHistoryPicker(false, true);
+                break;
+
             case R.id.goto_menu_id:
                 bookmarksOrHistoryPicker(false, false);
                 break;
@@ -1463,6 +1474,9 @@
                 menu.findItem(R.id.forward_menu_id)
                         .setEnabled(canGoForward);
 
+                menu.findItem(R.id.new_tab_menu_id).setEnabled(
+                        mTabControl.getTabCount() < TabControl.MAX_TABS);
+
                 // decide whether to show the share link option
                 PackageManager pm = getPackageManager();
                 Intent send = new Intent(Intent.ACTION_SEND);
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 1d53e8d..42729d8 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -34,32 +34,42 @@
  */
 public class TitleBar extends LinearLayout {
     private TextView        mTitle;
-    private TextView        mUrl;
     private Drawable        mCloseDrawable;
     private ImageView       mRtButton;
     private ProgressBar     mCircularProgress;
     private ProgressBar     mHorizontalProgress;
-    private ImageView       mFavicon;
-    private ImageView       mLockIcon;  // FIXME: Needs to be below the favicon
+    private Drawable        mFavicon;
+    private Drawable        mLockIcon;
+    private Drawable        mStopDrawable;
+    private Drawable        mBookmarkDrawable;
     private boolean         mInLoad;
-    private boolean         mTitleSet;
     private WebView         mWebView;
+    private BrowserActivity mBrowserActivity;
 
-    public TitleBar(Context context, WebView webview) {
+    public TitleBar(Context context, WebView webview, BrowserActivity ba) {
         super(context, null);
         LayoutInflater factory = LayoutInflater.from(context);
         factory.inflate(R.layout.title_bar, this);
+        mBrowserActivity = ba;
 
         mTitle = (TextView) findViewById(R.id.title);
-        mUrl = (TextView) findViewById(R.id.url);
+        mTitle.setCompoundDrawablePadding(5);
 
-        mRtButton = (ImageView) findViewById(R.id.rt_button);
-
+        mRtButton = (ImageView) findViewById(R.id.rt_btn);
+        mRtButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                if (mInLoad) {
+                    if (mWebView != null) {
+                        mWebView.stopLoading();
+                    }
+                } else {
+                    mBrowserActivity.bookmarksOrHistoryPicker(false, false);
+                }
+            }
+        });
         mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular);
         mHorizontalProgress = (ProgressBar) findViewById(
                 R.id.progress_horizontal);
-        mFavicon = (ImageView) findViewById(R.id.favicon);
-        mLockIcon = (ImageView) findViewById(R.id.lock_icon);
         mWebView = webview;
     }
 
@@ -71,15 +81,6 @@
     }
 
     /**
-     * Determine whether a point (from a touch) hits the right button.
-     */
-    /* package */ boolean hitRightButton(int x, int y) {
-        Rect hitRect = new Rect();
-        mRtButton.getHitRect(hitRect);
-        return hitRect.contains(x - getLeft(), y - getTop());
-    }
-
-    /**
      * Return whether the associated WebView is currently loading.  Needed to
      * determine whether a click should stop the load or close the tab.
      */
@@ -91,19 +92,22 @@
      * Set a new Drawable for the Favicon.
      */
     /* package */ void setFavicon(Drawable d) {
-        mFavicon.setImageDrawable(d);
+        if (d != null) {
+            d.setBounds(0, 0, 20, 20);
+        }
+        mTitle.setCompoundDrawables(d, null, mLockIcon, null);
+        mFavicon = d;
     }
 
     /**
      * Set the Drawable for the lock icon, or null to hide it.
      */
     /* package */ void setLock(Drawable d) {
-        if (d == null) {
-            mLockIcon.setVisibility(View.GONE);
-        } else {
-            mLockIcon.setImageDrawable(d);
-            mLockIcon.setVisibility(View.VISIBLE);
+        if (d != null) {
+            d.setBounds(0, 0, 20, 20);
         }
+        mTitle.setCompoundDrawables(mFavicon, null, d, null);
+        mLockIcon = d;
     }
 
     /**
@@ -113,31 +117,25 @@
         if (newProgress == mCircularProgress.getMax()) {
             mCircularProgress.setVisibility(View.GONE);
             mHorizontalProgress.setVisibility(View.GONE);
-            mRtButton.setVisibility(View.VISIBLE);
-            mUrl.setVisibility(View.VISIBLE);
-            if (mCloseDrawable != null) {
-                mRtButton.setImageDrawable(mCloseDrawable);
+            if (mBookmarkDrawable != null) {
+                mRtButton.setImageDrawable(mBookmarkDrawable);
             }
             mInLoad = false;
-            if (!mTitleSet) {
-                mTitle.setText(mUrl.getText());
-                mUrl.setText(null);
-                mTitleSet = true;
-            }
         } else {
             mCircularProgress.setProgress(newProgress);
             mHorizontalProgress.setProgress(newProgress);
             mCircularProgress.setVisibility(View.VISIBLE);
             mHorizontalProgress.setVisibility(View.VISIBLE);
-            mUrl.setVisibility(View.VISIBLE);
-            if (mCloseDrawable == null) {
-                // The drawable was assigned in the xml file, so it already
-                // exists.  Keep a pointer to it when we switch to the resource
-                // so we can easily switch back.
-                mCloseDrawable = mRtButton.getDrawable();
+            if (mBookmarkDrawable == null) {
+                mBookmarkDrawable = mRtButton.getDrawable();
             }
-            mRtButton.setImageResource(
-                    com.android.internal.R.drawable.ic_menu_stop);
+            if (mStopDrawable == null) {
+                mRtButton.setImageResource(
+                        com.android.internal.R.drawable.ic_menu_stop);
+                mStopDrawable = mRtButton.getDrawable();
+            } else {
+                mRtButton.setImageDrawable(mStopDrawable);
+            }
             mInLoad = true;
         }
     }
@@ -146,26 +144,11 @@
      * Update the title and url.
      */
     /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
-        if (url != null) {
-            url = BrowserActivity.buildTitleUrl(url.toString());
-        }
-        if (null == title) {
-            if (mInLoad) {
-                mTitleSet = false;
-                mTitle.setText(R.string.title_bar_loading);
-            } else {
-                // If the page has no title, put the url in the title space
-                // and leave the url blank.
-                mTitle.setText(url);
-                mUrl.setText(null);
-                mTitleSet = true;
-                return;
-            }
+        if (url == null) {
+            mTitle.setText(R.string.title_bar_loading);
         } else {
-            mTitle.setText(title);
-            mTitleSet = true;
+            mTitle.setText(BrowserActivity.buildTitleUrl(url.toString()));
         }
-        mUrl.setText(url);
     }
 
     /* package */ void setToTabPicker() {
@@ -174,6 +157,5 @@
         setLock(null);
         mCircularProgress.setVisibility(View.GONE);
         mHorizontalProgress.setVisibility(View.GONE);
-        mUrl.setVisibility(View.INVISIBLE);
     }
 }
diff --git a/src/com/android/browser/TitleBarSet.java b/src/com/android/browser/TitleBarSet.java
index d26f0af..4bd3fd8 100644
--- a/src/com/android/browser/TitleBarSet.java
+++ b/src/com/android/browser/TitleBarSet.java
@@ -39,7 +39,6 @@
         implements AdapterView.OnItemSelectedListener {
     private Vector<TitleBar>    mTitleBars;
     private BrowserActivity     mBrowserActivity;
-    private View                mNewButton;
     private int                 mCount;
     private TitleAdapter        mTitleAdapter;
     private boolean             mIgnoreSelectedListener;
@@ -53,9 +52,6 @@
         super(context, attrs);
         mTitleBars = new Vector<TitleBar>(TabControl.MAX_TABS);
         mCount = 0;
-        // Now create the Plus button that goes on the right.
-        LayoutInflater factory = LayoutInflater.from(context);
-        mNewButton = factory.inflate(R.layout.new_window_button, null);
         mTitleAdapter = new TitleAdapter();
         setAdapter(mTitleAdapter);
         setCallbackDuringFling(false);
@@ -76,12 +72,9 @@
             return;
         }
         int newSelection = mCount;
-        TitleBar titleBar = new TitleBar(getContext(), view);
+        TitleBar titleBar = new TitleBar(getContext(), view, mBrowserActivity);
         mTitleBars.add(titleBar);
         mCount++;
-        if (TabControl.MAX_TABS == mCount) {
-            mNewButton.setEnabled(false);
-        }
         // Need to refresh our list
         setAdapter(mTitleAdapter);
         mIgnoreSelectedListener = true;
@@ -167,7 +160,6 @@
      */
     public boolean performItemClick(View view, int position, long id) {
         if (!(view instanceof TitleBar)) {
-            // For new window button
             return super.performItemClick(view, position, id);
         }
         // If we have no mLastTouchUp, this was not called from onSingleTapUp,
@@ -181,21 +173,7 @@
         if (titleBar != getTitleBarAt(position)) {
             return false;
         }
-        if (titleBar.hitRightButton((int) mLastTouchUp.getX() - mScrollX,
-                (int) mLastTouchUp.getY() - mScrollY)) {
-            if (titleBar.isInLoad()) {
-                WebView webView = titleBar.getWebView();
-                if (null == webView) {
-                    // FIXME: How did we get into this situation?
-                    return false;
-                }
-                webView.stopLoading();
-            } else {
-                mBrowserActivity.closeCurrentWindow();
-            }
-        } else {
-            mBrowserActivity.bookmarksOrHistoryPicker(false, false);
-        }
+        mBrowserActivity.onSearchRequested();
         return true;
     }
 
@@ -203,9 +181,6 @@
      * Remove the tab at the given position.
      */
     /* package */ void removeTab(int position) {
-        if (TabControl.MAX_TABS == mCount) {
-            mNewButton.setEnabled(true);
-        }
         mTitleBars.remove(position);
         mCount--;
         // Need to refresh our list
@@ -221,17 +196,10 @@
 
     /**
      * Set the owning BrowserActivity.  Necessary so that we can call methods
-     * on it.
+     * on it.  Only called once before adding any title bars.
      */
-    /* package */ void setBrowserActivity(final BrowserActivity ba) {
+    /* package */ void init(final BrowserActivity ba) {
         mBrowserActivity = ba;
-        View.OnClickListener listener = new View.OnClickListener() {
-            public void onClick(View v) {
-                ba.openTabAndShow(BrowserActivity.EMPTY_URL_DATA, false, null);
-                ba.bookmarksOrHistoryPicker(false, true);
-            }
-        };
-        mNewButton.findViewById(R.id.button).setOnClickListener(listener);
     }
 
     /**
@@ -319,8 +287,7 @@
         public void registerDataSetObserver(DataSetObserver observer) {}
         public void unregisterDataSetObserver(DataSetObserver observer) {}
         public int getCount() {
-            // To account for new window
-            return mCount + 1;
+            return mCount;
         }
         public Object getItem(int position) {
             return null;
@@ -332,13 +299,9 @@
             return true;
         }
         public View getView(int position, View convertView, ViewGroup parent) {
-            if (mCount == position) {
-                return mNewButton;
-            }
             TitleBar titleBar = getTitleBarAt(position);
             Gallery.LayoutParams lp;
-            int desiredWidth = TitleBarSet.this.getWidth()
-                    - (2 * mNewButton.getWidth());
+            int desiredWidth = TitleBarSet.this.getWidth();
             ViewGroup.LayoutParams old = titleBar.getLayoutParams();
             if (old == null || !(old instanceof Gallery.LayoutParams)) {
                 lp = new Gallery.LayoutParams(desiredWidth,