blob: 4f18bd5dba4e1b1f9d95c7bf2c6618baaac8cf59 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.browser;
18
19import android.app.AlertDialog;
20import android.content.Context;
Leon Scrogginsd5304942009-12-10 16:11:39 -050021import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.preference.EditTextPreference;
Leon Scrogginsd5304942009-12-10 16:11:39 -050023import android.widget.Button;
24import android.widget.EditText;
25import android.widget.LinearLayout;
26import android.view.Gravity;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.Window;
30import android.view.WindowManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.util.AttributeSet;
32
Grace Kloba7ec2ba32009-09-10 21:28:18 -070033public class BrowserHomepagePreference extends EditTextPreference {
Leon Scrogginsd5304942009-12-10 16:11:39 -050034 private String mCurrentPage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035
36 public BrowserHomepagePreference(Context context, AttributeSet attrs,
37 int defStyle) {
38 super(context, attrs, defStyle);
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 }
40
41 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 }
44
45 public BrowserHomepagePreference(Context context) {
46 super(context);
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 }
48
Grace Kloba7ec2ba32009-09-10 21:28:18 -070049 @Override
Leon Scrogginsd5304942009-12-10 16:11:39 -050050 protected void onAddEditTextToDialogView(View dialogView,
51 EditText editText) {
52 super.onAddEditTextToDialogView(dialogView, editText);
53 // Now the EditText has a parent. Add a button to set to the current
54 // page.
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);
61 }
62 });
63 if (parent instanceof LinearLayout) {
64 ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL);
65 }
66 parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT,
67 ViewGroup.LayoutParams.WRAP_CONTENT);
68 }
69
70 @Override
Grace Kloba7ec2ba32009-09-10 21:28:18 -070071 protected void onDialogClosed(boolean positiveResult) {
72 if (positiveResult) {
73 String url = getEditText().getText().toString();
74 if (url.length() > 0
75 && !BrowserActivity.ACCEPTED_URI_SCHEMA.matcher(url)
76 .matches()) {
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);
82 } else {
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;
89 }
90 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -070092 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 }
Leon Scrogginsd5304942009-12-10 16:11:39 -050094
95 /**
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.
99 */
100 /* package */ void setCurrentPage(String currentPage) {
101 mCurrentPage = currentPage;
102 }
103
104 @Override
105 protected void showDialog(Bundle state) {
106 super.showDialog(state);
107 // The dialog has its width set to wrap_content. Change it to
Romain Guy15b8ec62010-01-08 15:06:43 -0800108 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500109 Window window = getDialog().getWindow();
110 View decorView = window.getDecorView();
111 WindowManager.LayoutParams params
112 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800113 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500114 window.getWindowManager().updateViewLayout(decorView, params);
115 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116}