blob: 3482b85586163fd618037bd21b7aec855f97fdfa [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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.Context;
21import android.content.res.Configuration;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Message;
25import android.text.Editable;
26import android.text.Spannable;
27import android.text.TextWatcher;
28import android.view.Gravity;
29import android.view.KeyEvent;
30import android.view.View;
31import android.view.ViewGroup;
32import android.view.Window;
33import android.view.WindowManager;
34import android.view.inputmethod.InputMethodManager;
35import android.webkit.WebView;
36import android.widget.EditText;
37import android.widget.TextView;
38
39/* package */ class FindDialog extends Dialog implements TextWatcher {
40 private WebView mWebView;
41 private TextView mMatches;
42 private BrowserActivity mBrowserActivity;
43
44 // Views with which the user can interact.
45 private View mOk;
46 private EditText mEditText;
47 private View mNextButton;
48 private View mPrevButton;
49 private View mMatchesView;
50
51 private View.OnClickListener mFindListener = new View.OnClickListener() {
52 public void onClick(View v) {
53 findNext();
54 }
55 };
56
57 private View.OnClickListener mFindCancelListener =
58 new View.OnClickListener() {
59 public void onClick(View v) {
60 dismiss();
61 }
62 };
63
64 private View.OnClickListener mFindPreviousListener =
65 new View.OnClickListener() {
66 public void onClick(View v) {
67 if (mWebView == null) {
68 throw new AssertionError("No WebView for FindDialog::onClick");
69 }
70 mWebView.findNext(false);
71 hideSoftInput();
72 }
73 };
74
75 /*
76 * Remove the soft keyboard from the screen.
77 */
78 private void hideSoftInput() {
79 InputMethodManager imm = (InputMethodManager)
80 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
81 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
82 }
83
84 private void disableButtons() {
85 mPrevButton.setEnabled(false);
86 mNextButton.setEnabled(false);
87 mPrevButton.setFocusable(false);
88 mNextButton.setFocusable(false);
89 }
90
91 /* package */ void setWebView(WebView webview) {
92 mWebView = webview;
93 }
94
95 /* package */ FindDialog(BrowserActivity context) {
96 super(context, R.style.FindDialogTheme);
97 mBrowserActivity = context;
98 setCanceledOnTouchOutside(true);
99 }
100
101 /* package */ void onConfigurationChanged(Configuration newConfig) {
102 // FIXME: Would like to call mWebView.findAll again, so that the
103 // matches would refresh, but the new picture has not yet been
104 // created, so it is too soon.
105 mEditText.getText().clear();
106 }
107
108 @Override
109 protected void onCreate(Bundle savedInstanceState) {
110 super.onCreate(savedInstanceState);
111
112 Window theWindow = getWindow();
113 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
114
115 setContentView(R.layout.browser_find);
116
117 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
118 ViewGroup.LayoutParams.WRAP_CONTENT);
119
120 mEditText = (EditText) findViewById(R.id.edit);
121
122 View button = findViewById(R.id.next);
123 button.setOnClickListener(mFindListener);
124 mNextButton = button;
125
126 button = findViewById(R.id.previous);
127 button.setOnClickListener(mFindPreviousListener);
128 mPrevButton = button;
129
130 button = findViewById(R.id.done);
131 button.setOnClickListener(mFindCancelListener);
132 mOk = button;
133
134 mMatches = (TextView) findViewById(R.id.matches);
135 mMatchesView = findViewById(R.id.matches_view);
136 disableButtons();
137 theWindow.setSoftInputMode(
138 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
139 }
140
141 public void dismiss() {
142 super.dismiss();
143 mBrowserActivity.closeFind();
144 mWebView.clearMatches();
145 }
Leon Scroggins354bbd02009-04-16 15:24:07 -0400146
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 @Override
148 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins354bbd02009-04-16 15:24:07 -0400149 if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
150 && event.getAction() == KeyEvent.ACTION_UP
151 && mEditText.hasFocus()) {
152 findNext();
153 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
155 return super.dispatchKeyEvent(event);
156 }
157
158 private void findNext() {
159 if (mWebView == null) {
160 throw new AssertionError("No WebView for FindDialog::findNext");
161 }
162 mWebView.findNext(true);
163 hideSoftInput();
164 }
165
166 public void show() {
167 super.show();
168 mEditText.requestFocus();
169 mEditText.setText("");
170 Spannable span = (Spannable) mEditText.getText();
171 span.setSpan(this, 0, span.length(),
172 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Eric Fischer8963b292009-03-24 17:53:52 -0700173 setMatchesFound(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 disableButtons();
175 }
176
177 // TextWatcher methods
178 public void beforeTextChanged(CharSequence s,
179 int start,
180 int count,
181 int after) {
182 }
183
184 public void onTextChanged(CharSequence s,
185 int start,
186 int before,
187 int count) {
188 if (mWebView == null) {
189 throw new AssertionError(
190 "No WebView for FindDialog::onTextChanged");
191 }
192 CharSequence find = mEditText.getText();
193 if (0 == find.length()) {
194 disableButtons();
195 mWebView.clearMatches();
196 mMatchesView.setVisibility(View.INVISIBLE);
197 } else {
198 mMatchesView.setVisibility(View.VISIBLE);
199 int found = mWebView.findAll(find.toString());
Eric Fischer8963b292009-03-24 17:53:52 -0700200 setMatchesFound(found);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 if (found < 2) {
202 disableButtons();
203 if (found == 0) {
Eric Fischer8963b292009-03-24 17:53:52 -0700204 setMatchesFound(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 }
206 } else {
207 mPrevButton.setFocusable(true);
208 mNextButton.setFocusable(true);
209 mPrevButton.setEnabled(true);
210 mNextButton.setEnabled(true);
211 }
212 }
213 }
214
Eric Fischer8963b292009-03-24 17:53:52 -0700215 private void setMatchesFound(int found) {
216 String template = mBrowserActivity.getResources().
217 getQuantityString(R.plurals.matches_found, found, found);
218
219 mMatches.setText(template);
220 }
221
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 public void afterTextChanged(Editable s) {
223 }
224}