Limit the touch area for opening search to the right edge of the textfield.

Now all single taps to the title bar will result in either
a search or opening the bookmarks picker, depending on whether
they were past the right edge of the textfield.

Fixes http://b/issue?id=2113429

Also move the lock icon in between the favicon and the url.  See
http://b/issue?id=2085847

Change-Id: I30447aa7517b6fc801d3cf34eff233db3a4ce635
diff --git a/res/layout/title_bar.xml b/res/layout/title_bar.xml
index eb9f7f9..ddfcd6e 100644
--- a/res/layout/title_bar.xml
+++ b/res/layout/title_bar.xml
@@ -36,29 +36,42 @@
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="3dip"
-        android:orientation="horizontal">
+        android:orientation="horizontal"
+        >
 
-        <TextView
-            android:id="@+id/title"
+        <LinearLayout
             android:background="@*android:drawable/textfield_default"
-            android:layout_height="wrap_content"
             android:layout_width="0dip"
             android:layout_weight="1.0"
-            android:paddingLeft="8dip"
-            android:paddingRight="6dip"
-            android:textAppearance="?android:attr/textAppearanceMedium"
-            android:textColor="@color/black"
-            android:gravity="center_vertical"
-            android:singleLine="true"
-            android:ellipsize="end"
-        />
-        <ImageView android:id="@+id/lock"
-            android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginLeft="3dip"
-            android:layout_gravity="center_vertical"
-            android:visibility="gone"
-            />
+            android:gravity="center_vertical"
+            android:orientation="horizontal"
+            >
+                <ImageView android:id="@+id/favicon"
+                    android:layout_width="20dip"
+                    android:layout_height="20dip"
+                    android:layout_marginLeft="3dip"
+                    />
+                <ImageView android:id="@+id/lock"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginLeft="6dip"
+                    android:visibility="gone"
+                    />
+                <TextView
+                    android:id="@+id/title"
+                    android:layout_height="wrap_content"
+                    android:layout_width="0dip"
+                    android:layout_weight="1.0"
+                    android:paddingLeft="8dip"
+                    android:paddingRight="6dip"
+                    android:textAppearance="?android:attr/textAppearanceMedium"
+                    android:textColor="@color/black"
+                    android:gravity="center_vertical"
+                    android:singleLine="true"
+                    android:ellipsize="end"
+                />
+        </LinearLayout>
         <ImageView
             android:id="@+id/rt_btn"
             android:layout_width="42dip"
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index adb9b7d..66a0d43 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -90,6 +90,7 @@
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.Window;
@@ -946,17 +947,31 @@
 
     private void showFakeTitleBar() {
         if (mFakeTitleBar == null) {
-            WebView webView = getTopWindow();
+            final WebView webView = getTopWindow();
             mFakeTitleBar = new TitleBar(this, webView);
             mFakeTitleBar.setTitleAndUrl(null, webView.getUrl());
             mFakeTitleBar.setProgress(webView.getProgress());
             mFakeTitleBar.setFavicon(webView.getFavicon());
             updateLockIconToLatest();
-            View title = mFakeTitleBar.findViewById(R.id.title);
-            title.setOnClickListener(new View.OnClickListener() {
-                    public void onClick(View v) {
-                        onSearchRequested();
-                        closeOptionsMenu();
+            final View title = mFakeTitleBar.findViewById(R.id.title);
+            mFakeTitleBar.setOnTouchListener(new View.OnTouchListener() {
+                    public boolean onTouch(View v, MotionEvent event) {
+                        if (event.getAction() == MotionEvent.ACTION_UP) {
+                            if (event.getX() > title.getRight()) {
+                                if (webView != null
+                                        && webView.getProgress() < 100) {
+                                    if (webView != null) {
+                                        webView.stopLoading();
+                                    }
+                                } else {
+                                    bookmarksOrHistoryPicker(false);
+                                }
+                            } else {
+                                onSearchRequested();
+                            }
+                            closeOptionsMenu();
+                        }
+                        return true;
                     }
             });
 
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index e830727..857aa38 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -45,7 +45,7 @@
     private ImageView       mRtButton;
     private Drawable        mCircularProgress;
     private ProgressBar     mHorizontalProgress;
-    private Drawable        mFavicon;
+    private ImageView       mFavicon;
     private ImageView       mLockIcon;
     private Drawable        mStopDrawable;
     private Drawable        mBookmarkDrawable;
@@ -65,19 +65,9 @@
         mTitle.setCompoundDrawablePadding(5);
 
         mLockIcon = (ImageView) findViewById(R.id.lock);
+        mFavicon = (ImageView) findViewById(R.id.favicon);
 
         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);
-                }
-            }
-        });
         Resources resources = context.getResources();
         mCircularProgress = (Drawable) resources.getDrawable(
                 com.android.internal.R.drawable.search_spinner);
@@ -123,10 +113,7 @@
         LayerDrawable d = new LayerDrawable(array);
         d.setLayerInset(1, 1, 1, 1, 1);
         d.setLayerInset(2, 2, 2, 2, 2);
-        d.setBounds(0, 0, mIconDimension, mIconDimension);
-        Drawable progress = mInLoad ? mCircularProgress : null;
-        mTitle.setCompoundDrawables(d, null, progress, null);
-        mFavicon = d;
+        mFavicon.setImageDrawable(d);
     }
 
     /**
@@ -146,7 +133,7 @@
      */
     /* package */ void setProgress(int newProgress) {
         if (newProgress == mHorizontalProgress.getMax()) {
-            mTitle.setCompoundDrawables(mFavicon, null, null, null);
+            mTitle.setCompoundDrawables(null, null, null, null);
             ((Animatable) mCircularProgress).stop();
             mHorizontalProgress.setVisibility(View.INVISIBLE);
             if (mBookmarkDrawable != null) {
@@ -156,7 +143,7 @@
         } else {
             mHorizontalProgress.setProgress(newProgress);
             if (!mInLoad) {
-                mTitle.setCompoundDrawables(mFavicon, null, mCircularProgress,
+                mTitle.setCompoundDrawables(null, null, mCircularProgress,
                         null);
                 ((Animatable) mCircularProgress).start();
                 mHorizontalProgress.setVisibility(View.VISIBLE);
diff --git a/src/com/android/browser/TitleBarSet.java b/src/com/android/browser/TitleBarSet.java
index 9cfce86..ba6c23a 100644
--- a/src/com/android/browser/TitleBarSet.java
+++ b/src/com/android/browser/TitleBarSet.java
@@ -177,6 +177,18 @@
         if (titleBar != getTitleBarAt(position)) {
             return false;
         }
+        View textfield = titleBar.findViewById(R.id.title);
+        // Interpret all touches past the right edge of the search field as
+        // a touch on the icon on the right.
+        if ((int) mLastTouchUp.getX() > textfield.getRight()) {
+            WebView webView = titleBar.getWebView();
+            if (webView != null && webView.getProgress() < 100) {
+                webView.stopLoading();
+            } else {
+                mBrowserActivity.bookmarksOrHistoryPicker(false);
+            }
+            return true;
+        }
         mBrowserActivity.onSearchRequested();
         return true;
     }