The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 21 | import android.content.DialogInterface; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 22 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.preference.EditTextPreference; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 24 | import android.widget.Button; |
| 25 | import android.widget.EditText; |
| 26 | import android.widget.LinearLayout; |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 27 | import android.widget.Toast; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 28 | import android.view.Gravity; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | import android.view.Window; |
| 32 | import android.view.WindowManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.util.AttributeSet; |
| 34 | |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 35 | public class BrowserHomepagePreference extends EditTextPreference { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 36 | private String mCurrentPage; |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 37 | private AlertDialog mSetHomepageTo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | |
| 39 | public BrowserHomepagePreference(Context context, AttributeSet attrs, |
| 40 | int defStyle) { |
| 41 | super(context, attrs, defStyle); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 42 | createSetHomepageToDialog(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public BrowserHomepagePreference(Context context, AttributeSet attrs) { |
| 46 | super(context, attrs); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 47 | createSetHomepageToDialog(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public BrowserHomepagePreference(Context context) { |
| 51 | super(context); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 52 | createSetHomepageToDialog(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 55 | @Override |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 56 | 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 Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 63 | button.setText(R.string.pref_set_homepage_to); |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 64 | button.setOnClickListener(new View.OnClickListener() { |
| 65 | public void onClick(View v) { |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 66 | mSetHomepageTo.show(); |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 67 | } |
| 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 Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 76 | 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 Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 101 | @Override |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 102 | protected void onDialogClosed(boolean positiveResult) { |
| 103 | if (positiveResult) { |
| 104 | String url = getEditText().getText().toString(); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 105 | if (!BrowserActivity.ACCEPTED_URI_SCHEMA.matcher(url).matches()) { |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 106 | int colon = url.indexOf(':'); |
| 107 | int space = url.indexOf(' '); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 108 | if (colon == -1 && space == -1 && url.length() > 0) { |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 109 | // if no colon, no space, add "http://" to make it a url |
| 110 | getEditText().setText("http://" + url); |
| 111 | } else { |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 112 | // show an error toast and change the positiveResult to |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 113 | // false so that the bad url will not override the old url |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame^] | 114 | Toast.makeText(getContext(), R.string.bookmark_url_not_valid, |
| 115 | Toast.LENGTH_SHORT).show(); |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 116 | positiveResult = false; |
| 117 | } |
| 118 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 119 | } |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 120 | super.onDialogClosed(positiveResult); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 121 | } |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 122 | |
| 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 Hamilton | 462b8e8 | 2010-09-23 14:33:43 -0500 | [diff] [blame] | 128 | public void setCurrentPage(String currentPage) { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 129 | 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 Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 136 | // match_parent so there is more room to type in a url. |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 137 | Window window = getDialog().getWindow(); |
| 138 | View decorView = window.getDecorView(); |
| 139 | WindowManager.LayoutParams params |
| 140 | = (WindowManager.LayoutParams) decorView.getLayoutParams(); |
Romain Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 141 | params.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 142 | window.getWindowManager().updateViewLayout(decorView, params); |
| 143 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 144 | } |