Strip http://(www.)? from url input

 Bug: 4982126

Change-Id: Ia8a9ade2ad4f578e40333f42e02edc161f7fa1c2
diff --git a/src/com/android/browser/NavigationBarBase.java b/src/com/android/browser/NavigationBarBase.java
index 724dcc8..e6eed18 100644
--- a/src/com/android/browser/NavigationBarBase.java
+++ b/src/com/android/browser/NavigationBarBase.java
@@ -167,7 +167,7 @@
             if (mUrlInput.getText().length() == 0) {
                 Tab currentTab = mUiController.getTabControl().getCurrentTab();
                 if (currentTab != null) {
-                    mUrlInput.setText(currentTab.getUrl(), false);
+                    setDisplayTitle(currentTab.getUrl());
                 }
             }
             mBaseUi.suggestHideTitleBar();
diff --git a/src/com/android/browser/NavigationBarPhone.java b/src/com/android/browser/NavigationBarPhone.java
index 8173377..49228db 100644
--- a/src/com/android/browser/NavigationBarPhone.java
+++ b/src/com/android/browser/NavigationBarPhone.java
@@ -128,11 +128,12 @@
      */
     @Override
     void setDisplayTitle(String title) {
+        mUrlInput.setTag(title);
         if (!isEditingUrl()) {
             if (title == null) {
                 mUrlInput.setText(R.string.new_tab);
             } else {
-                mUrlInput.setText(title);
+                mUrlInput.setText(UrlUtils.stripUrl(title), false);
             }
             mUrlInput.setSelection(0);
         }
@@ -198,6 +199,18 @@
     }
 
     @Override
+    public void onFocusChange(View view, boolean hasFocus) {
+        if (view == mUrlInput) {
+            if (hasFocus) {
+                mUrlInput.setText((String) mUrlInput.getTag(), false);
+            } else {
+                setDisplayTitle(mUrlInput.getText().toString());
+            }
+        }
+        super.onFocusChange(view, hasFocus);
+    }
+
+    @Override
     public void onStateChanged(int state) {
         switch(state) {
         case StateListener.STATE_NORMAL:
diff --git a/src/com/android/browser/UrlUtils.java b/src/com/android/browser/UrlUtils.java
index c922e55..681b242 100644
--- a/src/com/android/browser/UrlUtils.java
+++ b/src/com/android/browser/UrlUtils.java
@@ -40,28 +40,29 @@
     private final static String QUICKSEARCH_G = "http://www.google.com/m?q=%s";
     private final static String QUERY_PLACE_HOLDER = "%s";
 
-    // Regular expression which matches http://, followed by some stuff, followed by
-    // optionally a trailing slash, all matched as separate groups.
-    private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
+    // Regular expression to strip http://, optionally www., and optionally
+    // the trailing slash
+    private static final Pattern STRIP_URL_PATTERN =
+            Pattern.compile("^http://(?:www\\.)?(.*?)/?$");
 
     private UrlUtils() { /* cannot be instantiated */ }
 
     /**
-     * Strips the provided url of preceding "http://" and any trailing "/". Does not
+     * Strips the provided url of preceding "http://", "www.", and any trailing "/". Does not
      * strip "https://". If the provided string cannot be stripped, the original string
      * is returned.
      *
      * TODO: Put this in TextUtils to be used by other packages doing something similar.
      *
      * @param url a url to strip, like "http://www.google.com/"
-     * @return a stripped url like "www.google.com", or the original string if it could
+     * @return a stripped url like "google.com", or the original string if it could
      *         not be stripped
      */
     public static String stripUrl(String url) {
         if (url == null) return null;
         Matcher m = STRIP_URL_PATTERN.matcher(url);
-        if (m.matches() && m.groupCount() == 3) {
-            return m.group(2);
+        if (m.matches()) {
+            return m.group(1);
         } else {
             return url;
         }