blob: 9886053200a47dfb517e24c45a737f33489f133b [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;
John Reck33bbb802010-10-26 17:13:42 -070021import android.content.DialogInterface;
Leon Scrogginsd5304942009-12-10 16:11:39 -050022import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.preference.EditTextPreference;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.util.AttributeSet;
Leon Scrogginsd5304942009-12-10 16:11:39 -050025import android.view.Gravity;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.Window;
29import android.view.WindowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.widget.Button;
31import android.widget.EditText;
32import android.widget.LinearLayout;
33import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034
Grace Kloba7ec2ba32009-09-10 21:28:18 -070035public class BrowserHomepagePreference extends EditTextPreference {
Leon Scrogginsd5304942009-12-10 16:11:39 -050036 private String mCurrentPage;
John Reck33bbb802010-10-26 17:13:42 -070037 private AlertDialog mSetHomepageTo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038
39 public BrowserHomepagePreference(Context context, AttributeSet attrs,
40 int defStyle) {
41 super(context, attrs, defStyle);
John Reck33bbb802010-10-26 17:13:42 -070042 createSetHomepageToDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 }
44
45 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
46 super(context, attrs);
John Reck33bbb802010-10-26 17:13:42 -070047 createSetHomepageToDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -080048 }
49
50 public BrowserHomepagePreference(Context context) {
51 super(context);
John Reck33bbb802010-10-26 17:13:42 -070052 createSetHomepageToDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 }
54
Grace Kloba7ec2ba32009-09-10 21:28:18 -070055 @Override
Leon Scrogginsd5304942009-12-10 16:11:39 -050056 protected void onAddEditTextToDialogView(View dialogView,
57 EditText editText) {
58 super.onAddEditTextToDialogView(dialogView, editText);
59 // Now the EditText has a parent. Add a button to set to the current
60 // page.
61 ViewGroup parent = (ViewGroup) editText.getParent();
62 Button button = new Button(getContext());
John Reck33bbb802010-10-26 17:13:42 -070063 button.setText(R.string.pref_set_homepage_to);
Leon Scrogginsd5304942009-12-10 16:11:39 -050064 button.setOnClickListener(new View.OnClickListener() {
65 public void onClick(View v) {
John Reck33bbb802010-10-26 17:13:42 -070066 mSetHomepageTo.show();
Leon Scrogginsd5304942009-12-10 16:11:39 -050067 }
68 });
69 if (parent instanceof LinearLayout) {
70 ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL);
71 }
72 parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT,
73 ViewGroup.LayoutParams.WRAP_CONTENT);
74 }
75
John Reck33bbb802010-10-26 17:13:42 -070076 private void createSetHomepageToDialog() {
77 Context context = getContext();
78 CharSequence[] setToChoices = new CharSequence[] {
79 context.getText(R.string.pref_use_current),
80 context.getText(R.string.pref_use_blank),
81 context.getText(R.string.pref_use_default),
82 };
83 AlertDialog.Builder builder = new AlertDialog.Builder(context);
84 builder.setTitle(R.string.pref_set_homepage_to);
85 builder.setItems(setToChoices, new DialogInterface.OnClickListener() {
86 @Override
87 public void onClick(DialogInterface dialog, int which) {
88 if (which == 0) {
89 getEditText().setText(mCurrentPage);
90 } else if (which == 1) {
91 getEditText().setText("about:blank");
92 } else if (which == 2) {
93 getEditText().setText(BrowserSettings
94 .getFactoryResetHomeUrl(getContext()));
95 }
96 }
97 });
98 mSetHomepageTo = builder.create();
99 }
100
Leon Scrogginsd5304942009-12-10 16:11:39 -0500101 @Override
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700102 protected void onDialogClosed(boolean positiveResult) {
103 if (positiveResult) {
104 String url = getEditText().getText().toString();
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 if (url.length() > 0
106 && !UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url)
107 .matches()) {
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700108 int colon = url.indexOf(':');
109 int space = url.indexOf(' ');
John Reck33bbb802010-10-26 17:13:42 -0700110 if (colon == -1 && space == -1 && url.length() > 0) {
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700111 // if no colon, no space, add "http://" to make it a url
112 getEditText().setText("http://" + url);
113 } else {
John Reck33bbb802010-10-26 17:13:42 -0700114 // show an error toast and change the positiveResult to
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700115 // false so that the bad url will not override the old url
John Reck33bbb802010-10-26 17:13:42 -0700116 Toast.makeText(getContext(), R.string.bookmark_url_not_valid,
117 Toast.LENGTH_SHORT).show();
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700118 positiveResult = false;
119 }
120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700122 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 }
Leon Scrogginsd5304942009-12-10 16:11:39 -0500124
125 /**
126 * Set the current page of the browser.
127 * @param currentPage This String will replace the text in the EditText
128 * when the user clicks the "Use current page" button.
129 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500130 public void setCurrentPage(String currentPage) {
Leon Scrogginsd5304942009-12-10 16:11:39 -0500131 mCurrentPage = currentPage;
132 }
133
134 @Override
135 protected void showDialog(Bundle state) {
136 super.showDialog(state);
137 // The dialog has its width set to wrap_content. Change it to
Romain Guy15b8ec62010-01-08 15:06:43 -0800138 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500139 Window window = getDialog().getWindow();
140 View decorView = window.getDecorView();
141 WindowManager.LayoutParams params
142 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800143 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500144 window.getWindowManager().updateViewLayout(decorView, params);
145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146}