The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.text.Editable; |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 21 | import android.text.Selection; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.text.Spannable; |
| 23 | import android.text.TextWatcher; |
| 24 | import android.view.Gravity; |
| 25 | import android.view.KeyEvent; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 29 | import android.view.animation.AnimationUtils; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.view.inputmethod.InputMethodManager; |
| 31 | import android.webkit.WebView; |
| 32 | import android.widget.EditText; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.widget.TextView; |
| 35 | |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 36 | /* package */ class FindDialog extends WebDialog implements TextWatcher { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | private TextView mMatches; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | |
| 39 | // Views with which the user can interact. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | private EditText mEditText; |
| 41 | private View mNextButton; |
| 42 | private View mPrevButton; |
| 43 | private View mMatchesView; |
| 44 | |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 45 | // When the dialog is opened up with old text, enter needs to be pressed |
| 46 | // (or the text needs to be changed) before WebView.findAll can be called. |
| 47 | // Once it has been called, enter should move to the next match. |
| 48 | private boolean mMatchesFound; |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 49 | private int mNumberOfMatches; |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 50 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | private View.OnClickListener mFindListener = new View.OnClickListener() { |
| 52 | public void onClick(View v) { |
| 53 | findNext(); |
| 54 | } |
| 55 | }; |
| 56 | |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 57 | private View.OnClickListener mFindPreviousListener = |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | new View.OnClickListener() { |
| 59 | public void onClick(View v) { |
| 60 | if (mWebView == null) { |
| 61 | throw new AssertionError("No WebView for FindDialog::onClick"); |
| 62 | } |
| 63 | mWebView.findNext(false); |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 64 | updateMatchesString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | hideSoftInput(); |
| 66 | } |
| 67 | }; |
| 68 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 69 | private void disableButtons() { |
| 70 | mPrevButton.setEnabled(false); |
| 71 | mNextButton.setEnabled(false); |
| 72 | mPrevButton.setFocusable(false); |
| 73 | mNextButton.setFocusable(false); |
| 74 | } |
| 75 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 76 | /* package */ FindDialog(BrowserActivity context) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 77 | super(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 79 | LayoutInflater factory = LayoutInflater.from(context); |
| 80 | factory.inflate(R.layout.browser_find, this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 81 | |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 82 | addCancel(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 83 | mEditText = (EditText) findViewById(R.id.edit); |
| 84 | |
| 85 | View button = findViewById(R.id.next); |
| 86 | button.setOnClickListener(mFindListener); |
| 87 | mNextButton = button; |
| 88 | |
| 89 | button = findViewById(R.id.previous); |
| 90 | button.setOnClickListener(mFindPreviousListener); |
| 91 | mPrevButton = button; |
| 92 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | mMatches = (TextView) findViewById(R.id.matches); |
| 94 | mMatchesView = findViewById(R.id.matches_view); |
| 95 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 96 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 97 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 98 | |
| 99 | /** |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 100 | * Called by BrowserActivity.closeDialog. Start the animation to hide |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 101 | * the dialog, inform the WebView that the dialog is being dismissed, |
| 102 | * and hide the soft keyboard. |
| 103 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 104 | public void dismiss() { |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 105 | super.dismiss(); |
Cary Clark | 7d3ac79 | 2010-03-03 10:11:44 -0500 | [diff] [blame] | 106 | mWebView.notifyFindDialogDismissed(); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 107 | hideSoftInput(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | } |
Leon Scroggins | 354bbd0 | 2009-04-16 15:24:07 -0400 | [diff] [blame] | 109 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 110 | @Override |
Leon Scroggins III | cda8600 | 2010-05-06 14:27:07 -0400 | [diff] [blame] | 111 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
| 112 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 113 | KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 114 | if (state != null) { |
| 115 | int action = event.getAction(); |
| 116 | if (KeyEvent.ACTION_DOWN == action |
| 117 | && event.getRepeatCount() == 0) { |
| 118 | state.startTracking(event, this); |
| 119 | return true; |
| 120 | } else if (KeyEvent.ACTION_UP == action |
| 121 | && !event.isCanceled() && state.isTracking(event)) { |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 122 | mBrowserActivity.closeDialogs(); |
Leon Scroggins III | cda8600 | 2010-05-06 14:27:07 -0400 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return super.dispatchKeyEventPreIme(event); |
| 128 | } |
| 129 | |
| 130 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 131 | public boolean dispatchKeyEvent(KeyEvent event) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 132 | int keyCode = event.getKeyCode(); |
Leon Scroggins III | cda8600 | 2010-05-06 14:27:07 -0400 | [diff] [blame] | 133 | if (event.getAction() == KeyEvent.ACTION_UP) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 134 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 135 | && mEditText.hasFocus()) { |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 136 | if (mMatchesFound) { |
| 137 | findNext(); |
| 138 | } else { |
| 139 | findAll(); |
| 140 | // Set the selection to the end. |
| 141 | Spannable span = (Spannable) mEditText.getText(); |
| 142 | Selection.setSelection(span, span.length()); |
| 143 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 144 | return true; |
| 145 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 146 | } |
| 147 | return super.dispatchKeyEvent(event); |
| 148 | } |
| 149 | |
| 150 | private void findNext() { |
| 151 | if (mWebView == null) { |
| 152 | throw new AssertionError("No WebView for FindDialog::findNext"); |
| 153 | } |
| 154 | mWebView.findNext(true); |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 155 | updateMatchesString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 156 | hideSoftInput(); |
| 157 | } |
| 158 | |
| 159 | public void show() { |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 160 | super.show(); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 161 | // In case the matches view is showing from a previous search |
| 162 | mMatchesView.setVisibility(View.INVISIBLE); |
| 163 | mMatchesFound = false; |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 164 | // This text is only here to ensure that mMatches has a height. |
| 165 | mMatches.setText("0"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 166 | mEditText.requestFocus(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 167 | Spannable span = (Spannable) mEditText.getText(); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 168 | int length = span.length(); |
| 169 | Selection.setSelection(span, 0, length); |
| 170 | span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 171 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 172 | InputMethodManager imm = (InputMethodManager) |
| 173 | mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 174 | imm.showSoftInput(mEditText, 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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) { |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 188 | findAll(); |
| 189 | } |
| 190 | |
| 191 | private void findAll() { |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 192 | if (mWebView == null) { |
| 193 | throw new AssertionError( |
| 194 | "No WebView for FindDialog::findAll"); |
| 195 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | CharSequence find = mEditText.getText(); |
| 197 | if (0 == find.length()) { |
| 198 | disableButtons(); |
| 199 | mWebView.clearMatches(); |
| 200 | mMatchesView.setVisibility(View.INVISIBLE); |
| 201 | } else { |
| 202 | mMatchesView.setVisibility(View.VISIBLE); |
| 203 | int found = mWebView.findAll(find.toString()); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame] | 204 | mMatchesFound = true; |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 205 | setMatchesFound(found); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | if (found < 2) { |
| 207 | disableButtons(); |
| 208 | if (found == 0) { |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 209 | // Cannot use getQuantityString, which ignores the "zero" |
| 210 | // quantity. |
| 211 | mMatches.setText(mBrowserActivity.getResources().getString( |
| 212 | R.string.no_matches)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 213 | } |
| 214 | } else { |
| 215 | mPrevButton.setFocusable(true); |
| 216 | mNextButton.setFocusable(true); |
| 217 | mPrevButton.setEnabled(true); |
| 218 | mNextButton.setEnabled(true); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 223 | private void setMatchesFound(int found) { |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 224 | mNumberOfMatches = found; |
| 225 | updateMatchesString(); |
| 226 | } |
| 227 | |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame^] | 228 | public void setText(String text) { |
| 229 | mEditText.setText(text); |
| 230 | findAll(); |
| 231 | } |
| 232 | |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 233 | private void updateMatchesString() { |
| 234 | // Note: updateMatchesString is only called by methods that have already |
| 235 | // checked mWebView for null. |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 236 | String template = mBrowserActivity.getResources(). |
Leon Scroggins | bff4038 | 2010-05-06 16:36:42 -0400 | [diff] [blame] | 237 | getQuantityString(R.plurals.matches_found, mNumberOfMatches, |
| 238 | mWebView.findIndex() + 1, mNumberOfMatches); |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 239 | |
| 240 | mMatches.setText(template); |
| 241 | } |
| 242 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 243 | public void afterTextChanged(Editable s) { |
| 244 | } |
| 245 | } |