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