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 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.content.Context; |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 20 | import android.content.DialogInterface; |
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; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 24 | import android.view.KeyEvent; |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 26 | import android.view.View; |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 27 | import android.view.View.OnClickListener; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 28 | import android.view.ViewGroup; |
| 29 | import android.view.Window; |
| 30 | import android.view.WindowManager; |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 31 | import android.view.inputmethod.EditorInfo; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 32 | import android.widget.EditText; |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 33 | import android.widget.TextView; |
| 34 | import android.widget.TextView.OnEditorActionListener; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 36 | public class BrowserHomepagePreference extends EditTextPreference |
| 37 | implements OnEditorActionListener { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 38 | private String mCurrentPage; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | |
| 40 | public BrowserHomepagePreference(Context context, AttributeSet attrs, |
| 41 | int defStyle) { |
| 42 | super(context, attrs, defStyle); |
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); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public BrowserHomepagePreference(Context context) { |
| 50 | super(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 53 | @Override |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 54 | protected void onAddEditTextToDialogView(View dialogView, |
| 55 | EditText editText) { |
| 56 | super.onAddEditTextToDialogView(dialogView, editText); |
John Reck | 2a40b07 | 2011-01-05 12:59:02 -0800 | [diff] [blame] | 57 | editText.setSelectAllOnFocus(true); |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 58 | editText.setSingleLine(true); |
| 59 | editText.setImeActionLabel(null, EditorInfo.IME_ACTION_DONE); |
| 60 | editText.setOnEditorActionListener(this); |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 61 | // Now the EditText has a parent. Add a button to set to the current |
| 62 | // page. |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 63 | createButtons((ViewGroup) editText.getParent()); |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 64 | } |
| 65 | |
John Reck | c7141c2 | 2011-03-03 14:26:46 -0800 | [diff] [blame^] | 66 | @Override |
| 67 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 68 | if (actionId == EditorInfo.IME_ACTION_DONE) { |
| 69 | onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); |
| 70 | getDialog().dismiss(); |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 76 | void createButtons(ViewGroup parent) { |
| 77 | LayoutInflater inflater = (LayoutInflater) getContext() |
| 78 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 79 | View v = inflater.inflate(R.layout.pref_homepage_buttons, parent); |
| 80 | v.findViewById(R.id.use_current).setOnClickListener(mOnClick); |
| 81 | v.findViewById(R.id.use_default).setOnClickListener(mOnClick); |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame] | 82 | } |
| 83 | |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 84 | OnClickListener mOnClick = new OnClickListener() { |
| 85 | @Override |
| 86 | public void onClick(View v) { |
| 87 | switch (v.getId()) { |
| 88 | case R.id.use_current: |
| 89 | getEditText().setText(mCurrentPage); |
| 90 | break; |
| 91 | case R.id.use_default: |
| 92 | getEditText().setText( |
| 93 | BrowserSettings.getFactoryResetHomeUrl(getContext())); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 99 | @Override |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 100 | protected void onDialogClosed(boolean positiveResult) { |
| 101 | if (positiveResult) { |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 102 | String url = getEditText().getText().toString().trim(); |
| 103 | if (url.length() > 0) { |
| 104 | url = UrlUtils.smartUrlFilter(url); |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 105 | } |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 106 | getEditText().setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 107 | } |
Grace Kloba | 7ec2ba3 | 2009-09-10 21:28:18 -0700 | [diff] [blame] | 108 | super.onDialogClosed(positiveResult); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 109 | } |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Set the current page of the browser. |
| 113 | * @param currentPage This String will replace the text in the EditText |
| 114 | * when the user clicks the "Use current page" button. |
| 115 | */ |
Jeff Hamilton | 462b8e8 | 2010-09-23 14:33:43 -0500 | [diff] [blame] | 116 | public void setCurrentPage(String currentPage) { |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 117 | mCurrentPage = currentPage; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void showDialog(Bundle state) { |
| 122 | super.showDialog(state); |
| 123 | // The dialog has its width set to wrap_content. Change it to |
Romain Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 124 | // match_parent so there is more room to type in a url. |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 125 | Window window = getDialog().getWindow(); |
| 126 | View decorView = window.getDecorView(); |
| 127 | WindowManager.LayoutParams params |
| 128 | = (WindowManager.LayoutParams) decorView.getLayoutParams(); |
Romain Guy | 15b8ec6 | 2010-01-08 15:06:43 -0800 | [diff] [blame] | 129 | params.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Leon Scroggins | d530494 | 2009-12-10 16:11:39 -0500 | [diff] [blame] | 130 | window.getWindowManager().updateViewLayout(decorView, params); |
| 131 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | } |