blob: fefe1015327f33322e34a3cd9ea049354528b04d [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;
Leon Scrogginsd5304942009-12-10 16:11:39 -050024import android.widget.Button;
25import android.widget.EditText;
26import android.widget.LinearLayout;
John Reck33bbb802010-10-26 17:13:42 -070027import android.widget.Toast;
Leon Scrogginsd5304942009-12-10 16:11:39 -050028import android.view.Gravity;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.Window;
32import android.view.WindowManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.util.AttributeSet;
34
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();
John Reck33bbb802010-10-26 17:13:42 -0700105 if (!BrowserActivity.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700106 int colon = url.indexOf(':');
107 int space = url.indexOf(' ');
John Reck33bbb802010-10-26 17:13:42 -0700108 if (colon == -1 && space == -1 && url.length() > 0) {
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700109 // if no colon, no space, add "http://" to make it a url
110 getEditText().setText("http://" + url);
111 } else {
John Reck33bbb802010-10-26 17:13:42 -0700112 // show an error toast and change the positiveResult to
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700113 // false so that the bad url will not override the old url
John Reck33bbb802010-10-26 17:13:42 -0700114 Toast.makeText(getContext(), R.string.bookmark_url_not_valid,
115 Toast.LENGTH_SHORT).show();
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700116 positiveResult = false;
117 }
118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700120 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 }
Leon Scrogginsd5304942009-12-10 16:11:39 -0500122
123 /**
124 * Set the current page of the browser.
125 * @param currentPage This String will replace the text in the EditText
126 * when the user clicks the "Use current page" button.
127 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500128 public void setCurrentPage(String currentPage) {
Leon Scrogginsd5304942009-12-10 16:11:39 -0500129 mCurrentPage = currentPage;
130 }
131
132 @Override
133 protected void showDialog(Bundle state) {
134 super.showDialog(state);
135 // The dialog has its width set to wrap_content. Change it to
Romain Guy15b8ec62010-01-08 15:06:43 -0800136 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500137 Window window = getDialog().getWindow();
138 View decorView = window.getDecorView();
139 WindowManager.LayoutParams params
140 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800141 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500142 window.getWindowManager().updateViewLayout(decorView, params);
143 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144}