blob: 1e2697b40ad4cd0a5f6d2017612174385a9b3af5 [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;
Leon Scrogginsd5304942009-12-10 16:11:39 -050020import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.preference.EditTextPreference;
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.util.AttributeSet;
John Reckef074262010-12-02 16:09:14 -080023import android.view.LayoutInflater;
Leon Scrogginsd5304942009-12-10 16:11:39 -050024import android.view.View;
John Reckef074262010-12-02 16:09:14 -080025import android.view.View.OnClickListener;
Leon Scrogginsd5304942009-12-10 16:11:39 -050026import android.view.ViewGroup;
27import android.view.Window;
28import android.view.WindowManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.widget.EditText;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030
Grace Kloba7ec2ba32009-09-10 21:28:18 -070031public class BrowserHomepagePreference extends EditTextPreference {
Leon Scrogginsd5304942009-12-10 16:11:39 -050032 private String mCurrentPage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
34 public BrowserHomepagePreference(Context context, AttributeSet attrs,
35 int defStyle) {
36 super(context, attrs, defStyle);
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 }
38
39 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
40 super(context, attrs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 }
42
43 public BrowserHomepagePreference(Context context) {
44 super(context);
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 }
46
Grace Kloba7ec2ba32009-09-10 21:28:18 -070047 @Override
Leon Scrogginsd5304942009-12-10 16:11:39 -050048 protected void onAddEditTextToDialogView(View dialogView,
49 EditText editText) {
50 super.onAddEditTextToDialogView(dialogView, editText);
John Reck2a40b072011-01-05 12:59:02 -080051 editText.setSelectAllOnFocus(true);
Leon Scrogginsd5304942009-12-10 16:11:39 -050052 // Now the EditText has a parent. Add a button to set to the current
53 // page.
John Reckef074262010-12-02 16:09:14 -080054 createButtons((ViewGroup) editText.getParent());
Leon Scrogginsd5304942009-12-10 16:11:39 -050055 }
56
John Reckef074262010-12-02 16:09:14 -080057 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 Reck33bbb802010-10-26 17:13:42 -070063 }
64
John Reckef074262010-12-02 16:09:14 -080065 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 Scrogginsd5304942009-12-10 16:11:39 -050080 @Override
Grace Kloba7ec2ba32009-09-10 21:28:18 -070081 protected void onDialogClosed(boolean positiveResult) {
82 if (positiveResult) {
John Reckef074262010-12-02 16:09:14 -080083 String url = getEditText().getText().toString().trim();
84 if (url.length() > 0) {
85 url = UrlUtils.smartUrlFilter(url);
Grace Kloba7ec2ba32009-09-10 21:28:18 -070086 }
John Reckef074262010-12-02 16:09:14 -080087 getEditText().setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -070089 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 }
Leon Scrogginsd5304942009-12-10 16:11:39 -050091
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 Hamilton462b8e82010-09-23 14:33:43 -050097 public void setCurrentPage(String currentPage) {
Leon Scrogginsd5304942009-12-10 16:11:39 -050098 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 Guy15b8ec62010-01-08 15:06:43 -0800105 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500106 Window window = getDialog().getWindow();
107 View decorView = window.getDecorView();
108 WindowManager.LayoutParams params
109 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800110 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500111 window.getWindowManager().updateViewLayout(decorView, params);
112 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113}