blob: 44109ffeb0fc7411c59bfbef31f590d4660cd83f [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 Projectb7775e12009-02-10 15:44:04 -080032import android.view.WindowManager;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070033import android.webkit.WebView;
34import android.widget.EditText;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070035import android.widget.TextView;
36
The Android Open Source Projected217d92008-12-17 18:05:52 -080037/* package */ class FindDialog extends Dialog implements TextWatcher {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070038 private WebView mWebView;
39 private TextView mMatches;
40 private BrowserActivity mBrowserActivity;
41
42 // Views with which the user can interact.
43 private View mOk;
44 private EditText mEditText;
45 private View mNextButton;
46 private View mPrevButton;
The Android Open Source Projected217d92008-12-17 18:05:52 -080047 private View mMatchesView;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070048
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 }
The Android Open Source Projected217d92008-12-17 18:05:52 -080068 mWebView.findNext(false);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070069 }
70 };
The Android Open Source Project1658a9b2009-03-03 14:04:29 -080071
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070072 private void disableButtons() {
73 mPrevButton.setEnabled(false);
74 mNextButton.setEnabled(false);
75 mPrevButton.setFocusable(false);
76 mNextButton.setFocusable(false);
77 }
78
The Android Open Source Projected217d92008-12-17 18:05:52 -080079 /* package */ void setWebView(WebView webview) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070080 mWebView = webview;
81 }
82
83 /* package */ FindDialog(BrowserActivity context) {
The Android Open Source Projected217d92008-12-17 18:05:52 -080084 super(context, R.style.FindDialogTheme);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070085 mBrowserActivity = context;
The Android Open Source Projected217d92008-12-17 18:05:52 -080086 setCanceledOnTouchOutside(true);
87 }
88
89 /* package */ void onConfigurationChanged(Configuration newConfig) {
90 // FIXME: Would like to call mWebView.findAll again, so that the
91 // matches would refresh, but the new picture has not yet been
92 // created, so it is too soon.
93 mEditText.getText().clear();
94 }
95
96 @Override
97 protected void onCreate(Bundle savedInstanceState) {
98 super.onCreate(savedInstanceState);
99
100 Window theWindow = getWindow();
101 theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
102
103 setContentView(R.layout.browser_find);
104
105 theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
106 ViewGroup.LayoutParams.WRAP_CONTENT);
107
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700108 mEditText = (EditText) findViewById(R.id.edit);
109
110 View button = findViewById(R.id.next);
111 button.setOnClickListener(mFindListener);
112 mNextButton = button;
113
114 button = findViewById(R.id.previous);
115 button.setOnClickListener(mFindPreviousListener);
116 mPrevButton = button;
117
118 button = findViewById(R.id.done);
119 button.setOnClickListener(mFindCancelListener);
120 mOk = button;
121
122 mMatches = (TextView) findViewById(R.id.matches);
The Android Open Source Projected217d92008-12-17 18:05:52 -0800123 mMatchesView = findViewById(R.id.matches_view);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700124 disableButtons();
The Android Open Source Projectb7775e12009-02-10 15:44:04 -0800125 theWindow.setSoftInputMode(
126 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700127 }
128
129 public void dismiss() {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800130 super.dismiss();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700131 mBrowserActivity.closeFind();
The Android Open Source Projected217d92008-12-17 18:05:52 -0800132 mWebView.clearMatches();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700133 }
134
135 @Override
136 public boolean dispatchKeyEvent(KeyEvent event) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700137 int code = event.getKeyCode();
138 boolean up = event.getAction() == KeyEvent.ACTION_UP;
139 switch (code) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700140 case KeyEvent.KEYCODE_DPAD_CENTER:
141 case KeyEvent.KEYCODE_ENTER:
142 if (!mEditText.hasFocus()) {
143 break;
144 }
145 if (up) {
146 findNext();
147 }
148 return true;
149 default:
150 break;
151 }
152 return super.dispatchKeyEvent(event);
153 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700154
155 private void findNext() {
156 if (mWebView == null) {
157 throw new AssertionError("No WebView for FindDialog::findNext");
158 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800159 mWebView.findNext(true);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700160 }
The Android Open Source Project1658a9b2009-03-03 14:04:29 -0800161
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700162 public void show() {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800163 super.show();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700164 mEditText.requestFocus();
165 mEditText.setText("");
166 Spannable span = (Spannable) mEditText.getText();
167 span.setSpan(this, 0, span.length(),
168 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
169 mMatches.setText(R.string.zero);
170 disableButtons();
171 }
172
173 // TextWatcher methods
174 public void beforeTextChanged(CharSequence s,
175 int start,
176 int count,
177 int after) {
178 }
179
180 public void onTextChanged(CharSequence s,
181 int start,
182 int before,
183 int count) {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800184 if (mWebView == null) {
185 throw new AssertionError(
186 "No WebView for FindDialog::onTextChanged");
187 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700188 CharSequence find = mEditText.getText();
189 if (0 == find.length()) {
190 disableButtons();
The Android Open Source Projected217d92008-12-17 18:05:52 -0800191 mWebView.clearMatches();
192 mMatchesView.setVisibility(View.INVISIBLE);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700193 } else {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800194 mMatchesView.setVisibility(View.VISIBLE);
195 int found = mWebView.findAll(find.toString());
196 mMatches.setText(Integer.toString(found));
197 if (found < 2) {
198 disableButtons();
199 if (found == 0) {
200 mMatches.setText(R.string.zero);
201 }
202 } else {
203 mPrevButton.setFocusable(true);
204 mNextButton.setFocusable(true);
205 mPrevButton.setEnabled(true);
206 mNextButton.setEnabled(true);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700207 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700208 }
209 }
210
211 public void afterTextChanged(Editable s) {
212 }
213}