Provide a button to allow the user to set the home page to the current page.

Fixes http://b/issue?id=2243560
diff --git a/src/com/android/browser/BrowserHomepagePreference.java b/src/com/android/browser/BrowserHomepagePreference.java
index be96db3..ec603d3 100644
--- a/src/com/android/browser/BrowserHomepagePreference.java
+++ b/src/com/android/browser/BrowserHomepagePreference.java
@@ -18,10 +18,20 @@
 
 import android.app.AlertDialog;
 import android.content.Context;
+import android.os.Bundle;
 import android.preference.EditTextPreference;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.view.Gravity;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.view.WindowManager;
 import android.util.AttributeSet;
 
 public class BrowserHomepagePreference extends EditTextPreference {
+    private String mCurrentPage;
 
     public BrowserHomepagePreference(Context context, AttributeSet attrs,
             int defStyle) {
@@ -37,6 +47,27 @@
     }
 
     @Override
+    protected void onAddEditTextToDialogView(View dialogView,
+            EditText editText) {
+        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_use_current);
+        button.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                getEditText().setText(mCurrentPage);
+            }
+        });
+        if (parent instanceof LinearLayout) {
+            ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL);
+        }
+        parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT);
+    }
+
+    @Override
     protected void onDialogClosed(boolean positiveResult) {
         if (positiveResult) {
             String url = getEditText().getText().toString();
@@ -60,4 +91,26 @@
         }
         super.onDialogClosed(positiveResult);
     }
+
+    /**
+     * Set the current page of the browser.
+     * @param currentPage This String will replace the text in the EditText
+     *          when the user clicks the "Use current page" button.
+     */
+    /* package */ void setCurrentPage(String currentPage) {
+        mCurrentPage = currentPage;
+    }
+
+    @Override
+    protected void showDialog(Bundle state) {
+        super.showDialog(state);
+        // The dialog has its width set to wrap_content.  Change it to
+        // fill_parent so there is more room to type in a url.
+        Window window = getDialog().getWindow();
+        View decorView = window.getDecorView();
+        WindowManager.LayoutParams params
+                = (WindowManager.LayoutParams) decorView.getLayoutParams();
+        params.width = ViewGroup.LayoutParams.FILL_PARENT;
+        window.getWindowManager().updateViewLayout(decorView, params);
+    }
 }