blob: 9379103508334ecf64e527604f165e19cc581f53 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import 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.view.inputmethod.InputMethodManager;
34import android.webkit.WebView;
35import android.widget.EditText;
36import android.widget.TextView;
37
38/* package */ class FindDialog extends Dialog implements TextWatcher {
39 private WebView mWebView;
40 private TextView mMatches;
41 private BrowserActivity mBrowserActivity;
42
43 // Views with which the user can interact.
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 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 hideSoftInput();
70 }
71 };
72
73 /*
74 * Remove the soft keyboard from the screen.
75 */
76 private void hideSoftInput() {
77 InputMethodManager imm = (InputMethodManager)
78 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
79 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
80 }
81
82 private void disableButtons() {
83 mPrevButton.setEnabled(false);
84 mNextButton.setEnabled(false);
85 mPrevButton.setFocusable(false);
86 mNextButton.setFocusable(false);
87 }
88
89 /* package */ void setWebView(WebView webview) {
90 mWebView = webview;
91 }
92
93 /* package */ FindDialog(BrowserActivity context) {
94 super(context, R.style.FindDialogTheme);
95 mBrowserActivity = context;
96 setCanceledOnTouchOutside(true);
97 }
98
The Android Open Source Project0c908882009-03-03 19:32:16 -080099 @Override
100 protected void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
102
103 Window theWindow = getWindow();
104 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
105
106 setContentView(R.layout.browser_find);
107
108 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
109 ViewGroup.LayoutParams.WRAP_CONTENT);
110
111 mEditText = (EditText) findViewById(R.id.edit);
112
113 View button = findViewById(R.id.next);
114 button.setOnClickListener(mFindListener);
115 mNextButton = button;
116
117 button = findViewById(R.id.previous);
118 button.setOnClickListener(mFindPreviousListener);
119 mPrevButton = button;
120
121 button = findViewById(R.id.done);
122 button.setOnClickListener(mFindCancelListener);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123
124 mMatches = (TextView) findViewById(R.id.matches);
125 mMatchesView = findViewById(R.id.matches_view);
126 disableButtons();
127 theWindow.setSoftInputMode(
128 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
129 }
130
131 public void dismiss() {
132 super.dismiss();
133 mBrowserActivity.closeFind();
134 mWebView.clearMatches();
135 }
Leon Scroggins354bbd02009-04-16 15:24:07 -0400136
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 @Override
138 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins354bbd02009-04-16 15:24:07 -0400139 if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
140 && event.getAction() == KeyEvent.ACTION_UP
141 && mEditText.hasFocus()) {
142 findNext();
143 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 }
145 return super.dispatchKeyEvent(event);
146 }
147
148 private void findNext() {
149 if (mWebView == null) {
150 throw new AssertionError("No WebView for FindDialog::findNext");
151 }
152 mWebView.findNext(true);
153 hideSoftInput();
154 }
155
156 public void show() {
157 super.show();
158 mEditText.requestFocus();
159 mEditText.setText("");
160 Spannable span = (Spannable) mEditText.getText();
161 span.setSpan(this, 0, span.length(),
162 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Eric Fischer8963b292009-03-24 17:53:52 -0700163 setMatchesFound(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 disableButtons();
165 }
166
167 // TextWatcher methods
168 public void beforeTextChanged(CharSequence s,
169 int start,
170 int count,
171 int after) {
172 }
173
174 public void onTextChanged(CharSequence s,
175 int start,
176 int before,
177 int count) {
178 if (mWebView == null) {
179 throw new AssertionError(
180 "No WebView for FindDialog::onTextChanged");
181 }
182 CharSequence find = mEditText.getText();
183 if (0 == find.length()) {
184 disableButtons();
185 mWebView.clearMatches();
186 mMatchesView.setVisibility(View.INVISIBLE);
187 } else {
188 mMatchesView.setVisibility(View.VISIBLE);
Cary Clark169158b2009-09-21 12:01:52 -0400189 mWebView.setFindDialogHeight(
190 getWindow().getDecorView().getHeight());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 int found = mWebView.findAll(find.toString());
Eric Fischer8963b292009-03-24 17:53:52 -0700192 setMatchesFound(found);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 if (found < 2) {
194 disableButtons();
195 if (found == 0) {
Eric Fischer8963b292009-03-24 17:53:52 -0700196 setMatchesFound(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 }
198 } else {
199 mPrevButton.setFocusable(true);
200 mNextButton.setFocusable(true);
201 mPrevButton.setEnabled(true);
202 mNextButton.setEnabled(true);
203 }
204 }
205 }
206
Eric Fischer8963b292009-03-24 17:53:52 -0700207 private void setMatchesFound(int found) {
208 String template = mBrowserActivity.getResources().
209 getQuantityString(R.plurals.matches_found, found, found);
210
211 mMatches.setText(template);
212 }
213
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 public void afterTextChanged(Editable s) {
215 }
216}