blob: cbe5b0d8bbc0d30fc0f8f67d83463f961c7a1dd3 [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.Context;
John Reckc7141c22011-03-03 14:26:46 -080020import android.content.DialogInterface;
Leon Scrogginsd5304942009-12-10 16:11:39 -050021import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.preference.EditTextPreference;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.util.AttributeSet;
John Reckc7141c22011-03-03 14:26:46 -080024import android.view.KeyEvent;
John Reckef074262010-12-02 16:09:14 -080025import android.view.LayoutInflater;
Leon Scrogginsd5304942009-12-10 16:11:39 -050026import android.view.View;
John Reckef074262010-12-02 16:09:14 -080027import android.view.View.OnClickListener;
Leon Scrogginsd5304942009-12-10 16:11:39 -050028import android.view.ViewGroup;
29import android.view.Window;
30import android.view.WindowManager;
John Reckc7141c22011-03-03 14:26:46 -080031import android.view.inputmethod.EditorInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.widget.EditText;
John Reckc7141c22011-03-03 14:26:46 -080033import android.widget.TextView;
34import android.widget.TextView.OnEditorActionListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035
John Reckc7141c22011-03-03 14:26:46 -080036public class BrowserHomepagePreference extends EditTextPreference
37 implements OnEditorActionListener {
Leon Scrogginsd5304942009-12-10 16:11:39 -050038 private String mCurrentPage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039
40 public BrowserHomepagePreference(Context context, AttributeSet attrs,
41 int defStyle) {
42 super(context, attrs, defStyle);
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 }
44
45 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
46 super(context, attrs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 }
48
49 public BrowserHomepagePreference(Context context) {
50 super(context);
The Android Open Source Project0c908882009-03-03 19:32:16 -080051 }
52
Grace Kloba7ec2ba32009-09-10 21:28:18 -070053 @Override
Leon Scrogginsd5304942009-12-10 16:11:39 -050054 protected void onAddEditTextToDialogView(View dialogView,
55 EditText editText) {
56 super.onAddEditTextToDialogView(dialogView, editText);
John Reck2a40b072011-01-05 12:59:02 -080057 editText.setSelectAllOnFocus(true);
John Reckc7141c22011-03-03 14:26:46 -080058 editText.setSingleLine(true);
59 editText.setImeActionLabel(null, EditorInfo.IME_ACTION_DONE);
60 editText.setOnEditorActionListener(this);
Leon Scrogginsd5304942009-12-10 16:11:39 -050061 // Now the EditText has a parent. Add a button to set to the current
62 // page.
John Reckef074262010-12-02 16:09:14 -080063 createButtons((ViewGroup) editText.getParent());
Leon Scrogginsd5304942009-12-10 16:11:39 -050064 }
65
John Reckc7141c22011-03-03 14:26:46 -080066 @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 Reckef074262010-12-02 16:09:14 -080076 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 Reck33bbb802010-10-26 17:13:42 -070082 }
83
John Reckef074262010-12-02 16:09:14 -080084 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 Scrogginsd5304942009-12-10 16:11:39 -050099 @Override
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700100 protected void onDialogClosed(boolean positiveResult) {
101 if (positiveResult) {
John Reckef074262010-12-02 16:09:14 -0800102 String url = getEditText().getText().toString().trim();
103 if (url.length() > 0) {
104 url = UrlUtils.smartUrlFilter(url);
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700105 }
John Reckef074262010-12-02 16:09:14 -0800106 getEditText().setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -0700108 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 }
Leon Scrogginsd5304942009-12-10 16:11:39 -0500110
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 Hamilton462b8e82010-09-23 14:33:43 -0500116 public void setCurrentPage(String currentPage) {
Leon Scrogginsd5304942009-12-10 16:11:39 -0500117 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 Guy15b8ec62010-01-08 15:06:43 -0800124 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500125 Window window = getDialog().getWindow();
126 View decorView = window.getDecorView();
127 WindowManager.LayoutParams params
128 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800129 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500130 window.getWindowManager().updateViewLayout(decorView, params);
131 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132}