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; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 21 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.preference.EditTextPreference; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 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; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.util.AttributeSet; |
| 32 | |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 33 | public class BrowserHomepagePreference extends EditTextPreference { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 34 | private String mCurrentPage; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | |
| 36 | public BrowserHomepagePreference(Context context, AttributeSet attrs, |
| 37 | int defStyle) { |
| 38 | super(context, attrs, defStyle); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | public BrowserHomepagePreference(Context context, AttributeSet attrs) { |
| 42 | super(context, attrs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public BrowserHomepagePreference(Context context) { |
| 46 | super(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 49 | @Override |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 50 | 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 Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 71 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 91 | } |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 92 | super.onDialogClosed(positiveResult); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | } |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 94 | |
| 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 | */ |
Jeff Hamilton | 462b8e8 | 2010-09-23 14:33:43 -0500 | [diff] [blame^] | 100 | public void setCurrentPage(String currentPage) { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 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 Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 108 | // match_parent so there is more room to type in a url. |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 109 | Window window = getDialog().getWindow(); |
| 110 | View decorView = window.getDecorView(); |
| 111 | WindowManager.LayoutParams params |
| 112 | = (WindowManager.LayoutParams) decorView.getLayoutParams(); |
Romain Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 113 | params.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 114 | window.getWindowManager().updateViewLayout(decorView, params); |
| 115 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 116 | } |