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 | |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 36 | /* package */ class FindDialog extends LinearLayout implements TextWatcher { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | private WebView mWebView; |
| 38 | private TextView mMatches; |
| 39 | private BrowserActivity mBrowserActivity; |
| 40 | |
| 41 | // Views with which the user can interact. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | private EditText mEditText; |
| 43 | private View mNextButton; |
| 44 | private View mPrevButton; |
| 45 | private View mMatchesView; |
| 46 | |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 47 | // When the dialog is opened up with old text, enter needs to be pressed |
| 48 | // (or the text needs to be changed) before WebView.findAll can be called. |
| 49 | // Once it has been called, enter should move to the next match. |
| 50 | private boolean mMatchesFound; |
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 | |
| 57 | private View.OnClickListener mFindCancelListener = |
| 58 | new View.OnClickListener() { |
| 59 | public void onClick(View v) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 60 | mBrowserActivity.closeFind(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | private View.OnClickListener mFindPreviousListener = |
| 65 | new View.OnClickListener() { |
| 66 | public void onClick(View v) { |
| 67 | if (mWebView == null) { |
| 68 | throw new AssertionError("No WebView for FindDialog::onClick"); |
| 69 | } |
| 70 | mWebView.findNext(false); |
| 71 | hideSoftInput(); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Remove the soft keyboard from the screen. |
| 77 | */ |
| 78 | private void hideSoftInput() { |
| 79 | InputMethodManager imm = (InputMethodManager) |
| 80 | mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 81 | imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); |
| 82 | } |
| 83 | |
| 84 | private void disableButtons() { |
| 85 | mPrevButton.setEnabled(false); |
| 86 | mNextButton.setEnabled(false); |
| 87 | mPrevButton.setFocusable(false); |
| 88 | mNextButton.setFocusable(false); |
| 89 | } |
| 90 | |
| 91 | /* package */ void setWebView(WebView webview) { |
| 92 | mWebView = webview; |
| 93 | } |
| 94 | |
| 95 | /* package */ FindDialog(BrowserActivity context) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 96 | super(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 97 | mBrowserActivity = context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 98 | |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 99 | LayoutInflater factory = LayoutInflater.from(context); |
| 100 | factory.inflate(R.layout.browser_find, this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 101 | |
| 102 | mEditText = (EditText) findViewById(R.id.edit); |
| 103 | |
| 104 | View button = findViewById(R.id.next); |
| 105 | button.setOnClickListener(mFindListener); |
| 106 | mNextButton = button; |
| 107 | |
| 108 | button = findViewById(R.id.previous); |
| 109 | button.setOnClickListener(mFindPreviousListener); |
| 110 | mPrevButton = button; |
| 111 | |
| 112 | button = findViewById(R.id.done); |
| 113 | button.setOnClickListener(mFindCancelListener); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 114 | |
| 115 | mMatches = (TextView) findViewById(R.id.matches); |
| 116 | mMatchesView = findViewById(R.id.matches_view); |
| 117 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 118 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 119 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Called by BrowserActivity.closeFind. Start the animation to hide |
| 123 | * the dialog, inform the WebView that the dialog is being dismissed, |
| 124 | * and hide the soft keyboard. |
| 125 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 126 | public void dismiss() { |
Cary Clark | 7d3ac79 | 2010-03-03 10:11:44 -0500 | [diff] [blame] | 127 | mWebView.notifyFindDialogDismissed(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 128 | startAnimation(AnimationUtils.loadAnimation(mBrowserActivity, |
| 129 | R.anim.find_dialog_exit)); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 130 | hideSoftInput(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 131 | } |
Leon Scroggins | 354bbd0 | 2009-04-16 15:24:07 -0400 | [diff] [blame] | 132 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public boolean dispatchKeyEvent(KeyEvent event) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 135 | int keyCode = event.getKeyCode(); |
| 136 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 137 | if (event.getAction() == KeyEvent.ACTION_UP) { |
| 138 | mBrowserActivity.closeFind(); |
| 139 | return true; |
| 140 | } |
| 141 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 142 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 143 | && mEditText.hasFocus()) { |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 144 | if (mMatchesFound) { |
| 145 | findNext(); |
| 146 | } else { |
| 147 | findAll(); |
| 148 | // Set the selection to the end. |
| 149 | Spannable span = (Spannable) mEditText.getText(); |
| 150 | Selection.setSelection(span, span.length()); |
| 151 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 152 | return true; |
| 153 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | } |
| 155 | return super.dispatchKeyEvent(event); |
| 156 | } |
| 157 | |
| 158 | private void findNext() { |
| 159 | if (mWebView == null) { |
| 160 | throw new AssertionError("No WebView for FindDialog::findNext"); |
| 161 | } |
| 162 | mWebView.findNext(true); |
| 163 | hideSoftInput(); |
| 164 | } |
| 165 | |
| 166 | public void show() { |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 167 | // In case the matches view is showing from a previous search |
| 168 | mMatchesView.setVisibility(View.INVISIBLE); |
| 169 | mMatchesFound = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 170 | mEditText.requestFocus(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 171 | Spannable span = (Spannable) mEditText.getText(); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 172 | int length = span.length(); |
| 173 | Selection.setSelection(span, 0, length); |
| 174 | span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 175 | setMatchesFound(0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 176 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 177 | startAnimation(AnimationUtils.loadAnimation(mBrowserActivity, |
| 178 | R.anim.find_dialog_enter)); |
| 179 | InputMethodManager imm = (InputMethodManager) |
| 180 | mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 181 | imm.showSoftInput(mEditText, 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // TextWatcher methods |
| 185 | public void beforeTextChanged(CharSequence s, |
| 186 | int start, |
| 187 | int count, |
| 188 | int after) { |
| 189 | } |
| 190 | |
| 191 | public void onTextChanged(CharSequence s, |
| 192 | int start, |
| 193 | int before, |
| 194 | int count) { |
| 195 | if (mWebView == null) { |
| 196 | throw new AssertionError( |
| 197 | "No WebView for FindDialog::onTextChanged"); |
| 198 | } |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 199 | findAll(); |
| 200 | } |
| 201 | |
| 202 | private void findAll() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 203 | CharSequence find = mEditText.getText(); |
| 204 | if (0 == find.length()) { |
| 205 | disableButtons(); |
| 206 | mWebView.clearMatches(); |
| 207 | mMatchesView.setVisibility(View.INVISIBLE); |
| 208 | } else { |
| 209 | mMatchesView.setVisibility(View.VISIBLE); |
| 210 | int found = mWebView.findAll(find.toString()); |
Leon Scroggins III | 36529cf | 2010-05-06 13:34:28 -0400 | [diff] [blame^] | 211 | mMatchesFound = true; |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 212 | setMatchesFound(found); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 213 | if (found < 2) { |
| 214 | disableButtons(); |
| 215 | if (found == 0) { |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 216 | setMatchesFound(0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 217 | } |
| 218 | } else { |
| 219 | mPrevButton.setFocusable(true); |
| 220 | mNextButton.setFocusable(true); |
| 221 | mPrevButton.setEnabled(true); |
| 222 | mNextButton.setEnabled(true); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 227 | private void setMatchesFound(int found) { |
| 228 | String template = mBrowserActivity.getResources(). |
| 229 | getQuantityString(R.plurals.matches_found, found, found); |
| 230 | |
| 231 | mMatches.setText(template); |
| 232 | } |
| 233 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 234 | public void afterTextChanged(Editable s) { |
| 235 | } |
| 236 | } |