blob: 80f7ec2518b2a848bc32ef02871f5fca8124e328 [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
19import android.app.AlertDialog;
20import android.content.Context;
John Reck33bbb802010-10-26 17:13:42 -070021import android.content.DialogInterface;
Leon Scrogginsd5304942009-12-10 16:11:39 -050022import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.preference.EditTextPreference;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.util.AttributeSet;
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;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.widget.EditText;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032
Grace Kloba7ec2ba32009-09-10 21:28:18 -070033public class BrowserHomepagePreference extends EditTextPreference {
Leon Scrogginsd5304942009-12-10 16:11:39 -050034 private String mCurrentPage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035
36 public BrowserHomepagePreference(Context context, AttributeSet attrs,
37 int defStyle) {
38 super(context, attrs, defStyle);
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 }
40
41 public BrowserHomepagePreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 }
44
45 public BrowserHomepagePreference(Context context) {
46 super(context);
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 }
48
Grace Kloba7ec2ba32009-09-10 21:28:18 -070049 @Override
Leon Scrogginsd5304942009-12-10 16:11:39 -050050 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.
John Reckef074262010-12-02 16:09:14 -080055 createButtons((ViewGroup) editText.getParent());
Leon Scrogginsd5304942009-12-10 16:11:39 -050056 }
57
John Reckef074262010-12-02 16:09:14 -080058 void createButtons(ViewGroup parent) {
59 LayoutInflater inflater = (LayoutInflater) getContext()
60 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
61 View v = inflater.inflate(R.layout.pref_homepage_buttons, parent);
62 v.findViewById(R.id.use_current).setOnClickListener(mOnClick);
63 v.findViewById(R.id.use_default).setOnClickListener(mOnClick);
John Reck33bbb802010-10-26 17:13:42 -070064 }
65
John Reckef074262010-12-02 16:09:14 -080066 OnClickListener mOnClick = new OnClickListener() {
67 @Override
68 public void onClick(View v) {
69 switch (v.getId()) {
70 case R.id.use_current:
71 getEditText().setText(mCurrentPage);
72 break;
73 case R.id.use_default:
74 getEditText().setText(
75 BrowserSettings.getFactoryResetHomeUrl(getContext()));
76 break;
77 }
78 }
79 };
80
Leon Scrogginsd5304942009-12-10 16:11:39 -050081 @Override
Grace Kloba7ec2ba32009-09-10 21:28:18 -070082 protected void onDialogClosed(boolean positiveResult) {
83 if (positiveResult) {
John Reckef074262010-12-02 16:09:14 -080084 String url = getEditText().getText().toString().trim();
85 if (url.length() > 0) {
86 url = UrlUtils.smartUrlFilter(url);
Grace Kloba7ec2ba32009-09-10 21:28:18 -070087 }
John Reckef074262010-12-02 16:09:14 -080088 getEditText().setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -080089 }
Grace Kloba7ec2ba32009-09-10 21:28:18 -070090 super.onDialogClosed(positiveResult);
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 }
Leon Scrogginsd5304942009-12-10 16:11:39 -050092
93 /**
94 * Set the current page of the browser.
95 * @param currentPage This String will replace the text in the EditText
96 * when the user clicks the "Use current page" button.
97 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -050098 public void setCurrentPage(String currentPage) {
Leon Scrogginsd5304942009-12-10 16:11:39 -050099 mCurrentPage = currentPage;
100 }
101
102 @Override
103 protected void showDialog(Bundle state) {
104 super.showDialog(state);
105 // The dialog has its width set to wrap_content. Change it to
Romain Guy15b8ec62010-01-08 15:06:43 -0800106 // match_parent so there is more room to type in a url.
Leon Scrogginsd5304942009-12-10 16:11:39 -0500107 Window window = getDialog().getWindow();
108 View decorView = window.getDecorView();
109 WindowManager.LayoutParams params
110 = (WindowManager.LayoutParams) decorView.getLayoutParams();
Romain Guy15b8ec62010-01-08 15:06:43 -0800111 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Leon Scrogginsd5304942009-12-10 16:11:39 -0500112 window.getWindowManager().updateViewLayout(decorView, params);
113 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114}