blob: caff852a6f95d2209b6a46fee389195a39e4f81c [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.text.Editable;
21import android.text.Spannable;
22import android.text.TextWatcher;
23import android.view.Gravity;
24import android.view.KeyEvent;
Leon Scroggins III211ba542010-04-19 13:21:13 -040025import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.view.View;
27import android.view.ViewGroup;
Leon Scroggins III211ba542010-04-19 13:21:13 -040028import android.view.animation.AnimationUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.view.inputmethod.InputMethodManager;
30import android.webkit.WebView;
31import android.widget.EditText;
Leon Scroggins III211ba542010-04-19 13:21:13 -040032import android.widget.LinearLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.widget.TextView;
34
Leon Scroggins III211ba542010-04-19 13:21:13 -040035/* package */ class FindDialog extends LinearLayout implements TextWatcher {
The Android Open Source Project0c908882009-03-03 19:32:16 -080036 private WebView mWebView;
37 private TextView mMatches;
38 private BrowserActivity mBrowserActivity;
39
40 // Views with which the user can interact.
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 private EditText mEditText;
42 private View mNextButton;
43 private View mPrevButton;
44 private View mMatchesView;
45
46 private View.OnClickListener mFindListener = new View.OnClickListener() {
47 public void onClick(View v) {
48 findNext();
49 }
50 };
51
52 private View.OnClickListener mFindCancelListener =
53 new View.OnClickListener() {
54 public void onClick(View v) {
Leon Scroggins III211ba542010-04-19 13:21:13 -040055 mBrowserActivity.closeFind();
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 }
57 };
58
59 private View.OnClickListener mFindPreviousListener =
60 new View.OnClickListener() {
61 public void onClick(View v) {
62 if (mWebView == null) {
63 throw new AssertionError("No WebView for FindDialog::onClick");
64 }
65 mWebView.findNext(false);
66 hideSoftInput();
67 }
68 };
69
70 /*
71 * Remove the soft keyboard from the screen.
72 */
73 private void hideSoftInput() {
74 InputMethodManager imm = (InputMethodManager)
75 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
76 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
77 }
78
79 private void disableButtons() {
80 mPrevButton.setEnabled(false);
81 mNextButton.setEnabled(false);
82 mPrevButton.setFocusable(false);
83 mNextButton.setFocusable(false);
84 }
85
86 /* package */ void setWebView(WebView webview) {
87 mWebView = webview;
88 }
89
90 /* package */ FindDialog(BrowserActivity context) {
Leon Scroggins III211ba542010-04-19 13:21:13 -040091 super(context);
The Android Open Source Project0c908882009-03-03 19:32:16 -080092 mBrowserActivity = context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093
Leon Scroggins III211ba542010-04-19 13:21:13 -040094 LayoutInflater factory = LayoutInflater.from(context);
95 factory.inflate(R.layout.browser_find, this);
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
97 mEditText = (EditText) findViewById(R.id.edit);
98
99 View button = findViewById(R.id.next);
100 button.setOnClickListener(mFindListener);
101 mNextButton = button;
102
103 button = findViewById(R.id.previous);
104 button.setOnClickListener(mFindPreviousListener);
105 mPrevButton = button;
106
107 button = findViewById(R.id.done);
108 button.setOnClickListener(mFindCancelListener);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
110 mMatches = (TextView) findViewById(R.id.matches);
111 mMatchesView = findViewById(R.id.matches_view);
112 disableButtons();
Leon Scroggins III211ba542010-04-19 13:21:13 -0400113
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 }
Leon Scroggins III211ba542010-04-19 13:21:13 -0400115
116 /**
117 * Called by BrowserActivity.closeFind. Start the animation to hide
118 * the dialog, inform the WebView that the dialog is being dismissed,
119 * and hide the soft keyboard.
120 */
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 public void dismiss() {
Cary Clark7d3ac792010-03-03 10:11:44 -0500122 mWebView.notifyFindDialogDismissed();
Leon Scroggins III211ba542010-04-19 13:21:13 -0400123 startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
124 R.anim.find_dialog_exit));
125 InputMethodManager imm = (InputMethodManager)
126 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
127
128 imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 }
Leon Scroggins354bbd02009-04-16 15:24:07 -0400130
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131 @Override
132 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins III211ba542010-04-19 13:21:13 -0400133 int keyCode = event.getKeyCode();
134 if (keyCode == KeyEvent.KEYCODE_BACK) {
135 if (event.getAction() == KeyEvent.ACTION_UP) {
136 mBrowserActivity.closeFind();
137 return true;
138 }
139 } else if (event.getAction() == KeyEvent.ACTION_UP) {
140 if (keyCode == KeyEvent.KEYCODE_ENTER
141 && mEditText.hasFocus()) {
142 findNext();
143 return true;
144 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 }
146 return super.dispatchKeyEvent(event);
147 }
148
149 private void findNext() {
150 if (mWebView == null) {
151 throw new AssertionError("No WebView for FindDialog::findNext");
152 }
153 mWebView.findNext(true);
154 hideSoftInput();
155 }
156
157 public void show() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 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();
Leon Scroggins III211ba542010-04-19 13:21:13 -0400165 startAnimation(AnimationUtils.loadAnimation(mBrowserActivity,
166 R.anim.find_dialog_enter));
167 InputMethodManager imm = (InputMethodManager)
168 mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
169 imm.showSoftInput(mEditText, 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 }
171
172 // TextWatcher methods
173 public void beforeTextChanged(CharSequence s,
174 int start,
175 int count,
176 int after) {
177 }
178
179 public void onTextChanged(CharSequence s,
180 int start,
181 int before,
182 int count) {
183 if (mWebView == null) {
184 throw new AssertionError(
185 "No WebView for FindDialog::onTextChanged");
186 }
187 CharSequence find = mEditText.getText();
188 if (0 == find.length()) {
189 disableButtons();
190 mWebView.clearMatches();
191 mMatchesView.setVisibility(View.INVISIBLE);
192 } else {
193 mMatchesView.setVisibility(View.VISIBLE);
194 int found = mWebView.findAll(find.toString());
Eric Fischer8963b292009-03-24 17:53:52 -0700195 setMatchesFound(found);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 if (found < 2) {
197 disableButtons();
198 if (found == 0) {
Eric Fischer8963b292009-03-24 17:53:52 -0700199 setMatchesFound(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 }
201 } else {
202 mPrevButton.setFocusable(true);
203 mNextButton.setFocusable(true);
204 mPrevButton.setEnabled(true);
205 mNextButton.setEnabled(true);
206 }
207 }
208 }
209
Eric Fischer8963b292009-03-24 17:53:52 -0700210 private void setMatchesFound(int found) {
211 String template = mBrowserActivity.getResources().
212 getQuantityString(R.plurals.matches_found, found, found);
213
214 mMatches.setText(template);
215 }
216
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 public void afterTextChanged(Editable s) {
218 }
219}