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.res.Configuration;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.text.Editable;
25 import android.text.Spannable;
26 import android.text.TextWatcher;
27 import android.view.Gravity;
28 import android.view.KeyEvent;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.view.Window;
32 import android.webkit.WebView;
33 import android.widget.EditText;
34 import android.widget.TextView;
36 /* package */ class FindDialog extends Dialog implements TextWatcher {
37 private WebView mWebView;
38 private TextView mMatches;
39 private BrowserActivity mBrowserActivity;
41 // Views with which the user can interact.
43 private EditText mEditText;
44 private View mNextButton;
45 private View mPrevButton;
46 private View mMatchesView;
48 private View.OnClickListener mFindListener = new View.OnClickListener() {
49 public void onClick(View v) {
54 private View.OnClickListener mFindCancelListener =
55 new View.OnClickListener() {
56 public void onClick(View v) {
61 private View.OnClickListener mFindPreviousListener =
62 new View.OnClickListener() {
63 public void onClick(View v) {
64 if (mWebView == null) {
65 throw new AssertionError("No WebView for FindDialog::onClick");
67 mWebView.findNext(false);
71 private void disableButtons() {
72 mPrevButton.setEnabled(false);
73 mNextButton.setEnabled(false);
74 mPrevButton.setFocusable(false);
75 mNextButton.setFocusable(false);
78 /* package */ void setWebView(WebView webview) {
82 /* package */ FindDialog(BrowserActivity context) {
83 super(context, R.style.FindDialogTheme);
84 mBrowserActivity = context;
85 setCanceledOnTouchOutside(true);
88 /* package */ void onConfigurationChanged(Configuration newConfig) {
89 // FIXME: Would like to call mWebView.findAll again, so that the
90 // matches would refresh, but the new picture has not yet been
91 // created, so it is too soon.
92 mEditText.getText().clear();
96 protected void onCreate(Bundle savedInstanceState) {
97 super.onCreate(savedInstanceState);
99 Window theWindow = getWindow();
100 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
102 setContentView(R.layout.browser_find);
104 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
105 ViewGroup.LayoutParams.WRAP_CONTENT);
107 mEditText = (EditText) findViewById(R.id.edit);
109 View button = findViewById(R.id.next);
110 button.setOnClickListener(mFindListener);
111 mNextButton = button;
113 button = findViewById(R.id.previous);
114 button.setOnClickListener(mFindPreviousListener);
115 mPrevButton = button;
117 button = findViewById(R.id.done);
118 button.setOnClickListener(mFindCancelListener);
121 mMatches = (TextView) findViewById(R.id.matches);
122 mMatchesView = findViewById(R.id.matches_view);
126 public void dismiss() {
128 mBrowserActivity.closeFind();
129 mWebView.clearMatches();
133 public boolean dispatchKeyEvent(KeyEvent event) {
134 int code = event.getKeyCode();
135 boolean up = event.getAction() == KeyEvent.ACTION_UP;
137 case KeyEvent.KEYCODE_DPAD_CENTER:
138 case KeyEvent.KEYCODE_ENTER:
139 if (!mEditText.hasFocus()) {
149 return super.dispatchKeyEvent(event);
152 private void findNext() {
153 if (mWebView == null) {
154 throw new AssertionError("No WebView for FindDialog::findNext");
156 mWebView.findNext(true);
161 mEditText.requestFocus();
162 mEditText.setText("");
163 Spannable span = (Spannable) mEditText.getText();
164 span.setSpan(this, 0, span.length(),
165 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
166 mMatches.setText(R.string.zero);
170 // TextWatcher methods
171 public void beforeTextChanged(CharSequence s,
177 public void onTextChanged(CharSequence s,
181 if (mWebView == null) {
182 throw new AssertionError(
183 "No WebView for FindDialog::onTextChanged");
185 CharSequence find = mEditText.getText();
186 if (0 == find.length()) {
188 mWebView.clearMatches();
189 mMatchesView.setVisibility(View.INVISIBLE);
191 mMatchesView.setVisibility(View.VISIBLE);
192 int found = mWebView.findAll(find.toString());
193 mMatches.setText(Integer.toString(found));
197 mMatches.setText(R.string.zero);
200 mPrevButton.setFocusable(true);
201 mNextButton.setFocusable(true);
202 mPrevButton.setEnabled(true);
203 mNextButton.setEnabled(true);
208 public void afterTextChanged(Editable s) {