blob: 2b2678488c2682c6e57f04154594772ab45f692d [file] [log] [blame]
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -07001/*
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 Projected217d92008-12-17 18:05:52 -080019import android.app.Dialog;
20import android.content.res.Configuration;
21import android.os.Bundle;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070022import android.os.Handler;
23import android.os.Message;
24import android.text.Editable;
25import android.text.Spannable;
26import android.text.TextWatcher;
The Android Open Source Projected217d92008-12-17 18:05:52 -080027import android.view.Gravity;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070028import android.view.KeyEvent;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070029import android.view.View;
30import android.view.ViewGroup;
The Android Open Source Projected217d92008-12-17 18:05:52 -080031import android.view.Window;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070032import android.webkit.WebView;
33import android.widget.EditText;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070034import android.widget.TextView;
35
The Android Open Source Projected217d92008-12-17 18:05:52 -080036/* package */ class FindDialog extends Dialog implements TextWatcher {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070037 private WebView mWebView;
38 private TextView mMatches;
39 private BrowserActivity mBrowserActivity;
40
41 // Views with which the user can interact.
42 private View mOk;
43 private EditText mEditText;
44 private View mNextButton;
45 private View mPrevButton;
The Android Open Source Projected217d92008-12-17 18:05:52 -080046 private View mMatchesView;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070047
48 private View.OnClickListener mFindListener = new View.OnClickListener() {
49 public void onClick(View v) {
50 findNext();
51 }
52 };
53
54 private View.OnClickListener mFindCancelListener =
55 new View.OnClickListener() {
56 public void onClick(View v) {
57 dismiss();
58 }
59 };
60
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");
66 }
The Android Open Source Projected217d92008-12-17 18:05:52 -080067 mWebView.findNext(false);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070068 }
69 };
70
71 private void disableButtons() {
72 mPrevButton.setEnabled(false);
73 mNextButton.setEnabled(false);
74 mPrevButton.setFocusable(false);
75 mNextButton.setFocusable(false);
76 }
77
The Android Open Source Projected217d92008-12-17 18:05:52 -080078 /* package */ void setWebView(WebView webview) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070079 mWebView = webview;
80 }
81
82 /* package */ FindDialog(BrowserActivity context) {
The Android Open Source Projected217d92008-12-17 18:05:52 -080083 super(context, R.style.FindDialogTheme);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070084 mBrowserActivity = context;
The Android Open Source Projected217d92008-12-17 18:05:52 -080085 setCanceledOnTouchOutside(true);
86 }
87
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();
93 }
94
95 @Override
96 protected void onCreate(Bundle savedInstanceState) {
97 super.onCreate(savedInstanceState);
98
99 Window theWindow = getWindow();
100 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
101
102 setContentView(R.layout.browser_find);
103
104 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
105 ViewGroup.LayoutParams.WRAP_CONTENT);
106
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700107 mEditText = (EditText) findViewById(R.id.edit);
108
109 View button = findViewById(R.id.next);
110 button.setOnClickListener(mFindListener);
111 mNextButton = button;
112
113 button = findViewById(R.id.previous);
114 button.setOnClickListener(mFindPreviousListener);
115 mPrevButton = button;
116
117 button = findViewById(R.id.done);
118 button.setOnClickListener(mFindCancelListener);
119 mOk = button;
120
121 mMatches = (TextView) findViewById(R.id.matches);
The Android Open Source Projected217d92008-12-17 18:05:52 -0800122 mMatchesView = findViewById(R.id.matches_view);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700123 disableButtons();
124 }
125
126 public void dismiss() {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800127 super.dismiss();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700128 mBrowserActivity.closeFind();
The Android Open Source Projected217d92008-12-17 18:05:52 -0800129 mWebView.clearMatches();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700130 }
131
132 @Override
133 public boolean dispatchKeyEvent(KeyEvent event) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700134 int code = event.getKeyCode();
135 boolean up = event.getAction() == KeyEvent.ACTION_UP;
136 switch (code) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700137 case KeyEvent.KEYCODE_DPAD_CENTER:
138 case KeyEvent.KEYCODE_ENTER:
139 if (!mEditText.hasFocus()) {
140 break;
141 }
142 if (up) {
143 findNext();
144 }
145 return true;
146 default:
147 break;
148 }
149 return super.dispatchKeyEvent(event);
150 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700151
152 private void findNext() {
153 if (mWebView == null) {
154 throw new AssertionError("No WebView for FindDialog::findNext");
155 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800156 mWebView.findNext(true);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700157 }
158
159 public void show() {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800160 super.show();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700161 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);
167 disableButtons();
168 }
169
170 // TextWatcher methods
171 public void beforeTextChanged(CharSequence s,
172 int start,
173 int count,
174 int after) {
175 }
176
177 public void onTextChanged(CharSequence s,
178 int start,
179 int before,
180 int count) {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800181 if (mWebView == null) {
182 throw new AssertionError(
183 "No WebView for FindDialog::onTextChanged");
184 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700185 CharSequence find = mEditText.getText();
186 if (0 == find.length()) {
187 disableButtons();
The Android Open Source Projected217d92008-12-17 18:05:52 -0800188 mWebView.clearMatches();
189 mMatchesView.setVisibility(View.INVISIBLE);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700190 } else {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800191 mMatchesView.setVisibility(View.VISIBLE);
192 int found = mWebView.findAll(find.toString());
193 mMatches.setText(Integer.toString(found));
194 if (found < 2) {
195 disableButtons();
196 if (found == 0) {
197 mMatches.setText(R.string.zero);
198 }
199 } else {
200 mPrevButton.setFocusable(true);
201 mNextButton.setFocusable(true);
202 mPrevButton.setEnabled(true);
203 mNextButton.setEnabled(true);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700204 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700205 }
206 }
207
208 public void afterTextChanged(Editable s) {
209 }
210}