Change homepage setting handling

 Bug: 3247095
 Tweaks the setting of the homepage setting. Replaces the "set to..."
 dialog with 2 buttons, and user can now enter a blank url to get a
 blank page with title "New Tab".

Change-Id: I95bcfcd241db4b9efbde645100d2fd44c980e061
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index 79c3327..f5851df 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -245,15 +245,14 @@
 
     @Override
     public void onPageStarted(Tab tab, String url, Bitmap favicon) {
-        if (mXLargeScreenSize) {
-            mTabBar.onPageStarted(tab, url, favicon);
-        }
         if (tab.inForeground()) {
             resetLockIcon(tab, url);
             setUrlTitle(tab, url, null);
             setFavicon(tab, favicon);
         }
-
+        if (mXLargeScreenSize) {
+            mTabBar.onPageStarted(tab, url, favicon);
+        }
     }
 
     @Override
@@ -706,7 +705,7 @@
             setUrlTitle(tab, item.getUrl(), item.getTitle());
             setFavicon(tab, item.getFavicon());
         } else {
-            setUrlTitle(tab, null, null);
+            setUrlTitle(tab, null, mActivity.getString(R.string.new_tab));
             setFavicon(tab, null);
         }
     }
@@ -850,12 +849,7 @@
     @Override
     public void setUrlTitle(Tab tab, String url, String title) {
         if (TextUtils.isEmpty(title)) {
-            if (TextUtils.isEmpty(url)) {
-                title = mActivity.getResources()
-                        .getString(R.string.title_bar_loading);
-            } else {
-                title = url;
-            }
+            title = url;
         }
         if (tab.isInVoiceSearchMode()) return;
         if (tab.inForeground()) {
diff --git a/src/com/android/browser/BrowserHomepagePreference.java b/src/com/android/browser/BrowserHomepagePreference.java
index 9886053..80f7ec2 100644
--- a/src/com/android/browser/BrowserHomepagePreference.java
+++ b/src/com/android/browser/BrowserHomepagePreference.java
@@ -22,34 +22,28 @@
 import android.os.Bundle;
 import android.preference.EditTextPreference;
 import android.util.AttributeSet;
-import android.view.Gravity;
+import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
-import android.widget.Button;
 import android.widget.EditText;
-import android.widget.LinearLayout;
-import android.widget.Toast;
 
 public class BrowserHomepagePreference extends EditTextPreference {
     private String mCurrentPage;
-    private AlertDialog mSetHomepageTo;
 
     public BrowserHomepagePreference(Context context, AttributeSet attrs,
             int defStyle) {
         super(context, attrs, defStyle);
-        createSetHomepageToDialog();
     }
 
     public BrowserHomepagePreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        createSetHomepageToDialog();
     }
 
     public BrowserHomepagePreference(Context context) {
         super(context);
-        createSetHomepageToDialog();
     }
 
     @Override
@@ -58,66 +52,40 @@
         super.onAddEditTextToDialogView(dialogView, editText);
         // Now the EditText has a parent.  Add a button to set to the current
         // page.
-        ViewGroup parent = (ViewGroup) editText.getParent();
-        Button button = new Button(getContext());
-        button.setText(R.string.pref_set_homepage_to);
-        button.setOnClickListener(new View.OnClickListener() {
-            public void onClick(View v) {
-                mSetHomepageTo.show();
-            }
-        });
-        if (parent instanceof LinearLayout) {
-            ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL);
-        }
-        parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT,
-                ViewGroup.LayoutParams.WRAP_CONTENT);
+        createButtons((ViewGroup) editText.getParent());
     }
 
-    private void createSetHomepageToDialog() {
-        Context context = getContext();
-        CharSequence[] setToChoices = new CharSequence[] {
-                context.getText(R.string.pref_use_current),
-                context.getText(R.string.pref_use_blank),
-                context.getText(R.string.pref_use_default),
-        };
-        AlertDialog.Builder builder = new AlertDialog.Builder(context);
-        builder.setTitle(R.string.pref_set_homepage_to);
-        builder.setItems(setToChoices, new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                if (which == 0) {
-                    getEditText().setText(mCurrentPage);
-                } else if (which == 1) {
-                    getEditText().setText("about:blank");
-                } else if (which == 2) {
-                    getEditText().setText(BrowserSettings
-                            .getFactoryResetHomeUrl(getContext()));
-                }
-            }
-        });
-        mSetHomepageTo = builder.create();
+    void createButtons(ViewGroup parent) {
+        LayoutInflater inflater = (LayoutInflater) getContext()
+                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        View v = inflater.inflate(R.layout.pref_homepage_buttons, parent);
+        v.findViewById(R.id.use_current).setOnClickListener(mOnClick);
+        v.findViewById(R.id.use_default).setOnClickListener(mOnClick);
     }
 
+    OnClickListener mOnClick = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            switch (v.getId()) {
+            case R.id.use_current:
+                getEditText().setText(mCurrentPage);
+                break;
+            case R.id.use_default:
+                getEditText().setText(
+                        BrowserSettings.getFactoryResetHomeUrl(getContext()));
+                break;
+            }
+        }
+    };
+
     @Override
     protected void onDialogClosed(boolean positiveResult) {
         if (positiveResult) {
-            String url = getEditText().getText().toString();
-            if (url.length() > 0
-                    && !UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url)
-                            .matches()) {
-                int colon = url.indexOf(':');
-                int space = url.indexOf(' ');
-                if (colon == -1 && space == -1 && url.length() > 0) {
-                    // if no colon, no space, add "http://" to make it a url
-                    getEditText().setText("http://" + url);
-                } else {
-                    // show an error toast and change the positiveResult to
-                    // false so that the bad url will not override the old url
-                    Toast.makeText(getContext(), R.string.bookmark_url_not_valid,
-                            Toast.LENGTH_SHORT).show();
-                    positiveResult = false;
-                }
+            String url = getEditText().getText().toString().trim();
+            if (url.length() > 0) {
+                url = UrlUtils.smartUrlFilter(url);
             }
+            getEditText().setText(url);
         }
         super.onDialogClosed(positiveResult);
     }
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 61ef76b..1f091e2 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -337,7 +337,7 @@
             // Set the default value for the Geolocation database path.
             geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
 
-            if (p.getString(PREF_HOMEPAGE, "") == "") {
+            if (p.getString(PREF_HOMEPAGE, null) == null) {
                 // No home page preferences is set, set it to default.
                 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
             }
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 75dd913..b26e9f0 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -108,10 +108,6 @@
 
     private static final int EMPTY_MENU = -1;
 
-    // Keep this initial progress in sync with initialProgressValue (* 100)
-    // in ProgressTracker.cpp
-    private final static int INITIAL_PROGRESS = 10;
-
     // activity requestCode
     final static int PREFERENCES_PAGE = 3;
     final static int FILE_SELECTED = 4;
@@ -750,10 +746,6 @@
 
         mUi.onPageStarted(tab, url, favicon);
 
-        // Show some progress so that the user knows the page is beginning to
-        // load
-        onProgressChanged(tab, INITIAL_PROGRESS);
-
         // update the bookmark database for favicon
         maybeUpdateFavicon(tab, null, url, favicon);
 
@@ -2247,7 +2239,6 @@
      * @param url The URL to load.
      */
     protected void loadUrl(WebView view, String url) {
-        updateTitleBarForNewLoad(view, url);
         view.loadUrl(url);
     }
 
@@ -2258,7 +2249,6 @@
      * @param data The UrlData being loaded.
      */
     protected void loadUrlDataIn(Tab t, UrlData data) {
-        updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
         data.loadIn(t);
     }
 
@@ -2279,25 +2269,6 @@
     }
 
     /**
-     * If the WebView is the top window, update the title bar to reflect
-     * loading the new URL.  i.e. set its text, clear the favicon (which
-     * will be set once the page begins loading), and set the progress to
-     * INITIAL_PROGRESS to show that the page has begun to load. Called
-     * by loadUrl and loadUrlDataIn.
-     * @param view The WebView that is starting a load.
-     * @param url The URL that is being loaded.
-     */
-    private void updateTitleBarForNewLoad(WebView view, String url) {
-        if (view == getCurrentTopWebView()) {
-            // TODO we should come with a tab and not with a view
-            Tab tab = mTabControl.getTabFromView(view);
-            setUrlTitle(tab, url, null);
-            mUi.setFavicon(tab, null);
-            onProgressChanged(tab, INITIAL_PROGRESS);
-        }
-    }
-
-    /**
      * Sets a title composed of the URL and the title string.
      * @param url The URL of the site being loaded.
      * @param title The title of the site being loaded.
diff --git a/src/com/android/browser/TabBar.java b/src/com/android/browser/TabBar.java
index 69e0bd2..14b1845 100644
--- a/src/com/android/browser/TabBar.java
+++ b/src/com/android/browser/TabBar.java
@@ -454,7 +454,6 @@
         mHasReceivedTitle = false;
         TabViewData tvd = mTabMap.get(tab);
         if (tvd != null) {
-            tvd.setUrlAndTitle(url, null);
             tvd.setFavicon(favicon);
             tvd.setUrlAndTitle(url, mLoadingText);
         }
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 6dabd76..bdef82e 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -320,13 +320,13 @@
 
     /**
      * Update the text displayed in the title bar.
-     * @param title String to display.  If null, the loading string will be
+     * @param title String to display.  If null, the new tab string will be
      *      shown.
      */
     @Override
     void setDisplayTitle(String title) {
         if (title == null) {
-            mTitle.setText(R.string.title_bar_loading);
+            mTitle.setText(R.string.new_tab);
         } else {
             if (mInVoiceMode) {
                 // Add two spaces.  The second one will be replaced with an
diff --git a/src/com/android/browser/preferences/PageContentPreferencesFragment.java b/src/com/android/browser/preferences/PageContentPreferencesFragment.java
index 1b5d0fe..4be14a6 100644
--- a/src/com/android/browser/preferences/PageContentPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PageContentPreferencesFragment.java
@@ -72,29 +72,8 @@
         }
 
         if (pref.getKey().equals(BrowserSettings.PREF_HOMEPAGE)) {
-            String value = (String) objValue;
-            boolean needUpdate = value.indexOf(' ') != -1;
-            if (needUpdate) {
-                value = value.trim().replace(" ", "%20");
-            }
-            if (value.length() != 0 && Uri.parse(value).getScheme() == null) {
-                value = "http://" + value;
-                needUpdate = true;
-            }
-            // Set the summary value.
-            pref.setSummary(value);
-            if (needUpdate) {
-                // Update through the EditText control as it has a cached copy
-                // of the string and it will handle persisting the value
-                ((EditTextPreference) pref).setText(value);
-
-                // as we update the value above, we need to return false
-                // here so that setText() is not called by EditTextPref
-                // with the old value.
-                return false;
-            } else {
-                return true;
-            }
+            pref.setSummary((String) objValue);
+            return true;
         } else if (pref.getKey().equals(BrowserSettings.PREF_TEXT_SIZE)) {
             pref.setSummary(getVisualTextSizeName((String) objValue));
             return true;