2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.browser;
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.preference.EditTextPreference;
23 import android.widget.Button;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.view.Gravity;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.Window;
30 import android.view.WindowManager;
31 import android.util.AttributeSet;
33 public class BrowserHomepagePreference extends EditTextPreference {
34 private String mCurrentPage;
36 public BrowserHomepagePreference(Context context, AttributeSet attrs,
38 super(context, attrs, defStyle);
41 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
45 public BrowserHomepagePreference(Context context) {
50 protected void onAddEditTextToDialogView(View dialogView,
52 super.onAddEditTextToDialogView(dialogView, editText);
53 // Now the EditText has a parent. Add a button to set to the current
55 ViewGroup parent = (ViewGroup) editText.getParent();
56 Button button = new Button(getContext());
57 button.setText(R.string.pref_use_current);
58 button.setOnClickListener(new View.OnClickListener() {
59 public void onClick(View v) {
60 getEditText().setText(mCurrentPage);
63 if (parent instanceof LinearLayout) {
64 ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL);
66 parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT,
67 ViewGroup.LayoutParams.WRAP_CONTENT);
71 protected void onDialogClosed(boolean positiveResult) {
73 String url = getEditText().getText().toString();
75 && !BrowserActivity.ACCEPTED_URI_SCHEMA.matcher(url)
77 int colon = url.indexOf(':');
78 int space = url.indexOf(' ');
79 if (colon == -1 && space == -1) {
80 // if no colon, no space, add "http://" to make it a url
81 getEditText().setText("http://" + url);
83 // show an error dialog and change the positiveResult to
84 // false so that the bad url will not override the old url
85 new AlertDialog.Builder(this.getContext()).setMessage(
86 R.string.bookmark_url_not_valid).setPositiveButton(
87 R.string.ok, null).show();
88 positiveResult = false;
92 super.onDialogClosed(positiveResult);
96 * Set the current page of the browser.
97 * @param currentPage This String will replace the text in the EditText
98 * when the user clicks the "Use current page" button.
100 /* package */ void setCurrentPage(String currentPage) {
101 mCurrentPage = currentPage;
105 protected void showDialog(Bundle state) {
106 super.showDialog(state);
107 // The dialog has its width set to wrap_content. Change it to
108 // match_parent so there is more room to type in a url.
109 Window window = getDialog().getWindow();
110 View decorView = window.getDecorView();
111 WindowManager.LayoutParams params
112 = (WindowManager.LayoutParams) decorView.getLayoutParams();
113 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
114 window.getWindowManager().updateViewLayout(decorView, params);