2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.text.Editable;
26 import android.text.Spannable;
27 import android.text.TextWatcher;
28 import android.view.Gravity;
29 import android.view.KeyEvent;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.Window;
33 import android.view.WindowManager;
34 import android.view.inputmethod.InputMethodManager;
35 import android.webkit.WebView;
36 import android.widget.EditText;
37 import android.widget.TextView;
39 /* package */ class FindDialog extends Dialog implements TextWatcher {
40 private WebView mWebView;
41 private TextView mMatches;
42 private BrowserActivity mBrowserActivity;
44 // Views with which the user can interact.
45 private EditText mEditText;
46 private View mNextButton;
47 private View mPrevButton;
48 private View mMatchesView;
50 private View.OnClickListener mFindListener = new View.OnClickListener() {
51 public void onClick(View v) {
56 private View.OnClickListener mFindCancelListener =
57 new View.OnClickListener() {
58 public void onClick(View v) {
63 private View.OnClickListener mFindPreviousListener =
64 new View.OnClickListener() {
65 public void onClick(View v) {
66 if (mWebView == null) {
67 throw new AssertionError("No WebView for FindDialog::onClick");
69 mWebView.findNext(false);
75 * Remove the soft keyboard from the screen.
77 private void hideSoftInput() {
78 InputMethodManager imm = (InputMethodManager)
79 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
80 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
83 private void disableButtons() {
84 mPrevButton.setEnabled(false);
85 mNextButton.setEnabled(false);
86 mPrevButton.setFocusable(false);
87 mNextButton.setFocusable(false);
90 /* package */ void setWebView(WebView webview) {
94 /* package */ FindDialog(BrowserActivity context) {
95 super(context, R.style.FindDialogTheme);
96 mBrowserActivity = context;
97 setCanceledOnTouchOutside(true);
100 /* package */ void onConfigurationChanged(Configuration newConfig) {
101 // FIXME: Would like to call mWebView.findAll again, so that the
102 // matches would refresh, but the new picture has not yet been
103 // created, so it is too soon.
104 mEditText.getText().clear();
108 protected void onCreate(Bundle savedInstanceState) {
109 super.onCreate(savedInstanceState);
111 Window theWindow = getWindow();
112 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
114 setContentView(R.layout.browser_find);
116 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
117 ViewGroup.LayoutParams.WRAP_CONTENT);
119 mEditText = (EditText) findViewById(R.id.edit);
121 View button = findViewById(R.id.next);
122 button.setOnClickListener(mFindListener);
123 mNextButton = button;
125 button = findViewById(R.id.previous);
126 button.setOnClickListener(mFindPreviousListener);
127 mPrevButton = button;
129 button = findViewById(R.id.done);
130 button.setOnClickListener(mFindCancelListener);
132 mMatches = (TextView) findViewById(R.id.matches);
133 mMatchesView = findViewById(R.id.matches_view);
135 theWindow.setSoftInputMode(
136 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
139 public void dismiss() {
141 mBrowserActivity.closeFind();
142 mWebView.clearMatches();
146 public boolean dispatchKeyEvent(KeyEvent event) {
147 if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
148 && event.getAction() == KeyEvent.ACTION_UP
149 && mEditText.hasFocus()) {
153 return super.dispatchKeyEvent(event);
156 private void findNext() {
157 if (mWebView == null) {
158 throw new AssertionError("No WebView for FindDialog::findNext");
160 mWebView.findNext(true);
166 mEditText.requestFocus();
167 mEditText.setText("");
168 Spannable span = (Spannable) mEditText.getText();
169 span.setSpan(this, 0, span.length(),
170 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
175 // TextWatcher methods
176 public void beforeTextChanged(CharSequence s,
182 public void onTextChanged(CharSequence s,
186 if (mWebView == null) {
187 throw new AssertionError(
188 "No WebView for FindDialog::onTextChanged");
190 CharSequence find = mEditText.getText();
191 if (0 == find.length()) {
193 mWebView.clearMatches();
194 mMatchesView.setVisibility(View.INVISIBLE);
196 mMatchesView.setVisibility(View.VISIBLE);
197 mWebView.setFindDialogHeight(
198 getWindow().getDecorView().getHeight());
199 int found = mWebView.findAll(find.toString());
200 setMatchesFound(found);
207 mPrevButton.setFocusable(true);
208 mNextButton.setFocusable(true);
209 mPrevButton.setEnabled(true);
210 mNextButton.setEnabled(true);
215 private void setMatchesFound(int found) {
216 String template = mBrowserActivity.getResources().
217 getQuantityString(R.plurals.matches_found, found, found);
219 mMatches.setText(template);
222 public void afterTextChanged(Editable s) {