FindDialog.java revision b7775e1e46a6c8ce0f07747d44c1b1792bb0ce1c
1/* 2 * Copyright (C) 2007 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.Dialog; 20import android.content.res.Configuration; 21import android.os.Bundle; 22import android.os.Handler; 23import android.os.Message; 24import android.text.Editable; 25import android.text.Spannable; 26import android.text.TextWatcher; 27import android.view.Gravity; 28import android.view.KeyEvent; 29import android.view.View; 30import android.view.ViewGroup; 31import android.view.Window; 32import android.view.WindowManager; 33import android.webkit.WebView; 34import android.widget.EditText; 35import android.widget.TextView; 36 37/* package */ class FindDialog extends Dialog implements TextWatcher { 38 private WebView mWebView; 39 private TextView mMatches; 40 private BrowserActivity mBrowserActivity; 41 42 // Views with which the user can interact. 43 private View mOk; 44 private EditText mEditText; 45 private View mNextButton; 46 private View mPrevButton; 47 private View mMatchesView; 48 49 private View.OnClickListener mFindListener = new View.OnClickListener() { 50 public void onClick(View v) { 51 findNext(); 52 } 53 }; 54 55 private View.OnClickListener mFindCancelListener = 56 new View.OnClickListener() { 57 public void onClick(View v) { 58 dismiss(); 59 } 60 }; 61 62 private View.OnClickListener mFindPreviousListener = 63 new View.OnClickListener() { 64 public void onClick(View v) { 65 if (mWebView == null) { 66 throw new AssertionError("No WebView for FindDialog::onClick"); 67 } 68 mWebView.findNext(false); 69 } 70 }; 71 72 private void disableButtons() { 73 mPrevButton.setEnabled(false); 74 mNextButton.setEnabled(false); 75 mPrevButton.setFocusable(false); 76 mNextButton.setFocusable(false); 77 } 78 79 /* package */ void setWebView(WebView webview) { 80 mWebView = webview; 81 } 82 83 /* package */ FindDialog(BrowserActivity context) { 84 super(context, R.style.FindDialogTheme); 85 mBrowserActivity = context; 86 setCanceledOnTouchOutside(true); 87 } 88 89 /* package */ void onConfigurationChanged(Configuration newConfig) { 90 // FIXME: Would like to call mWebView.findAll again, so that the 91 // matches would refresh, but the new picture has not yet been 92 // created, so it is too soon. 93 mEditText.getText().clear(); 94 } 95 96 @Override 97 protected void onCreate(Bundle savedInstanceState) { 98 super.onCreate(savedInstanceState); 99 100 Window theWindow = getWindow(); 101 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL); 102 103 setContentView(R.layout.browser_find); 104 105 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT, 106 ViewGroup.LayoutParams.WRAP_CONTENT); 107 108 mEditText = (EditText) findViewById(R.id.edit); 109 110 View button = findViewById(R.id.next); 111 button.setOnClickListener(mFindListener); 112 mNextButton = button; 113 114 button = findViewById(R.id.previous); 115 button.setOnClickListener(mFindPreviousListener); 116 mPrevButton = button; 117 118 button = findViewById(R.id.done); 119 button.setOnClickListener(mFindCancelListener); 120 mOk = button; 121 122 mMatches = (TextView) findViewById(R.id.matches); 123 mMatchesView = findViewById(R.id.matches_view); 124 disableButtons(); 125 theWindow.setSoftInputMode( 126 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 127 } 128 129 public void dismiss() { 130 super.dismiss(); 131 mBrowserActivity.closeFind(); 132 mWebView.clearMatches(); 133 } 134 135 @Override 136 public boolean dispatchKeyEvent(KeyEvent event) { 137 int code = event.getKeyCode(); 138 boolean up = event.getAction() == KeyEvent.ACTION_UP; 139 switch (code) { 140 case KeyEvent.KEYCODE_DPAD_CENTER: 141 case KeyEvent.KEYCODE_ENTER: 142 if (!mEditText.hasFocus()) { 143 break; 144 } 145 if (up) { 146 findNext(); 147 } 148 return true; 149 default: 150 break; 151 } 152 return super.dispatchKeyEvent(event); 153 } 154 155 private void findNext() { 156 if (mWebView == null) { 157 throw new AssertionError("No WebView for FindDialog::findNext"); 158 } 159 mWebView.findNext(true); 160 } 161 162 public void show() { 163 super.show(); 164 mEditText.requestFocus(); 165 mEditText.setText(""); 166 Spannable span = (Spannable) mEditText.getText(); 167 span.setSpan(this, 0, span.length(), 168 Spannable.SPAN_INCLUSIVE_INCLUSIVE); 169 mMatches.setText(R.string.zero); 170 disableButtons(); 171 } 172 173 // TextWatcher methods 174 public void beforeTextChanged(CharSequence s, 175 int start, 176 int count, 177 int after) { 178 } 179 180 public void onTextChanged(CharSequence s, 181 int start, 182 int before, 183 int count) { 184 if (mWebView == null) { 185 throw new AssertionError( 186 "No WebView for FindDialog::onTextChanged"); 187 } 188 CharSequence find = mEditText.getText(); 189 if (0 == find.length()) { 190 disableButtons(); 191 mWebView.clearMatches(); 192 mMatchesView.setVisibility(View.INVISIBLE); 193 } else { 194 mMatchesView.setVisibility(View.VISIBLE); 195 int found = mWebView.findAll(find.toString()); 196 mMatches.setText(Integer.toString(found)); 197 if (found < 2) { 198 disableButtons(); 199 if (found == 0) { 200 mMatches.setText(R.string.zero); 201 } 202 } else { 203 mPrevButton.setFocusable(true); 204 mNextButton.setFocusable(true); 205 mPrevButton.setEnabled(true); 206 mNextButton.setEnabled(true); 207 } 208 } 209 } 210 211 public void afterTextChanged(Editable s) { 212 } 213} 214