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 19 import android.app.AlertDialog; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.preference.EditTextPreference; 23 import android.widget.Button; 24 import android.widget.EditText; 25 import android.widget.LinearLayout; 26 import android.view.Gravity; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.Window; 30 import android.view.WindowManager; 31 import android.util.AttributeSet; 32 33 public class BrowserHomepagePreference extends EditTextPreference { 34 private String mCurrentPage; 35 36 public BrowserHomepagePreference(Context context, AttributeSet attrs, 37 int defStyle) { 38 super(context, attrs, defStyle); 39 } 40 41 public BrowserHomepagePreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 } 44 45 public BrowserHomepagePreference(Context context) { 46 super(context); 47 } 48 49 @Override 50 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. 55 ViewGroup parent = (ViewGroup) editText.getParent(); 56 Button button = new Button(getContext()); 57 button.setText(R.string.pref_use_current); 58 button.setOnClickListener(new View.OnClickListener() { 59 public void onClick(View v) { 60 getEditText().setText(mCurrentPage); 61 } 62 }); 63 if (parent instanceof LinearLayout) { 64 ((LinearLayout) parent).setGravity(Gravity.CENTER_HORIZONTAL); 65 } 66 parent.addView(button, ViewGroup.LayoutParams.WRAP_CONTENT, 67 ViewGroup.LayoutParams.WRAP_CONTENT); 68 } 69 70 @Override 71 protected void onDialogClosed(boolean positiveResult) { 72 if (positiveResult) { 73 String url = getEditText().getText().toString(); 74 if (url.length() > 0 75 && !BrowserActivity.ACCEPTED_URI_SCHEMA.matcher(url) 76 .matches()) { 77 int colon = url.indexOf(':'); 78 int space = url.indexOf(' '); 79 if (colon == -1 && space == -1) { 80 // if no colon, no space, add "http://" to make it a url 81 getEditText().setText("http://" + url); 82 } else { 83 // show an error dialog and change the positiveResult to 84 // false so that the bad url will not override the old url 85 new AlertDialog.Builder(this.getContext()).setMessage( 86 R.string.bookmark_url_not_valid).setPositiveButton( 87 R.string.ok, null).show(); 88 positiveResult = false; 89 } 90 } 91 } 92 super.onDialogClosed(positiveResult); 93 } 94 95 /** 96 * Set the current page of the browser. 97 * @param currentPage This String will replace the text in the EditText 98 * when the user clicks the "Use current page" button. 99 */ 100 /* package */ void setCurrentPage(String currentPage) { 101 mCurrentPage = currentPage; 102 } 103 104 @Override 105 protected void showDialog(Bundle state) { 106 super.showDialog(state); 107 // The dialog has its width set to wrap_content. Change it to 108 // match_parent so there is more room to type in a url. 109 Window window = getDialog().getWindow(); 110 View decorView = window.getDecorView(); 111 WindowManager.LayoutParams params 112 = (WindowManager.LayoutParams) decorView.getLayoutParams(); 113 params.width = ViewGroup.LayoutParams.MATCH_PARENT; 114 window.getWindowManager().updateViewLayout(decorView, params); 115 } 116 } 117