Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 android.webkit; |
| 18 | |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
John Reck | d6ac727 | 2011-11-08 15:00:17 -0800 | [diff] [blame] | 23 | import android.graphics.Point; |
| 24 | import android.graphics.Rect; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 25 | import android.text.Editable; |
| 26 | import android.text.Selection; |
| 27 | import android.text.Spannable; |
| 28 | import android.text.TextWatcher; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 29 | import android.view.ActionMode; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.Menu; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 32 | import android.view.MenuItem; |
| 33 | import android.view.View; |
| 34 | import android.view.inputmethod.InputMethodManager; |
John Reck | fffce6f | 2011-10-06 20:37:01 -0700 | [diff] [blame] | 35 | import android.widget.EditText; |
| 36 | import android.widget.TextView; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 37 | |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 38 | /** |
| 39 | * @hide |
| 40 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 41 | @SystemApi |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 42 | public class FindActionModeCallback implements ActionMode.Callback, TextWatcher, |
| 43 | View.OnClickListener, WebView.FindListener { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 44 | private View mCustomView; |
| 45 | private EditText mEditText; |
| 46 | private TextView mMatches; |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 47 | private WebView mWebView; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 48 | private InputMethodManager mInput; |
| 49 | private Resources mResources; |
| 50 | private boolean mMatchesFound; |
| 51 | private int mNumberOfMatches; |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 52 | private int mActiveMatchIndex; |
Leon Scroggins | 05919f2 | 2010-09-14 17:22:36 -0400 | [diff] [blame] | 53 | private ActionMode mActionMode; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 54 | |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 55 | public FindActionModeCallback(Context context) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 56 | mCustomView = LayoutInflater.from(context).inflate( |
| 57 | com.android.internal.R.layout.webview_find, null); |
| 58 | mEditText = (EditText) mCustomView.findViewById( |
| 59 | com.android.internal.R.id.edit); |
George Mount | 7e9f62e | 2012-05-23 16:55:46 -0700 | [diff] [blame] | 60 | mEditText.setCustomSelectionActionModeCallback(new NoAction()); |
Leon Scroggins | 7014b12 | 2011-01-11 15:17:34 -0500 | [diff] [blame] | 61 | mEditText.setOnClickListener(this); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 62 | setText(""); |
| 63 | mMatches = (TextView) mCustomView.findViewById( |
| 64 | com.android.internal.R.id.matches); |
Yohei Yukawa | 777ef95 | 2015-11-25 20:32:24 -0800 | [diff] [blame] | 65 | mInput = context.getSystemService(InputMethodManager.class); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 66 | mResources = context.getResources(); |
| 67 | } |
| 68 | |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 69 | public void finish() { |
Leon Scroggins | 05919f2 | 2010-09-14 17:22:36 -0400 | [diff] [blame] | 70 | mActionMode.finish(); |
| 71 | } |
| 72 | |
Nate Fischer | 5cca7bd | 2017-09-26 18:51:49 -0700 | [diff] [blame] | 73 | /** |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 74 | * Place text in the text field so it can be searched for. Need to press |
| 75 | * the find next or find previous button to find all of the matches. |
| 76 | */ |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 77 | public void setText(String text) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 78 | mEditText.setText(text); |
| 79 | Spannable span = (Spannable) mEditText.getText(); |
| 80 | int length = span.length(); |
| 81 | // Ideally, we would like to set the selection to the whole field, |
| 82 | // but this brings up the Text selection CAB, which dismisses this |
| 83 | // one. |
| 84 | Selection.setSelection(span, length, length); |
| 85 | // Necessary each time we set the text, so that this will watch |
| 86 | // changes to it. |
| 87 | span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 88 | mMatchesFound = false; |
| 89 | } |
| 90 | |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Set the WebView to search. |
| 93 | * |
| 94 | * @param webView an implementation of WebView |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 95 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 96 | public void setWebView(@NonNull WebView webView) { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 97 | if (null == webView) { |
| 98 | throw new AssertionError("WebView supplied to " |
| 99 | + "FindActionModeCallback cannot be null"); |
| 100 | } |
| 101 | mWebView = webView; |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 102 | mWebView.setFindDialogFindListener(this); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, |
| 107 | boolean isDoneCounting) { |
| 108 | if (isDoneCounting) { |
| 109 | updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0); |
| 110 | } |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Nate Fischer | 5cca7bd | 2017-09-26 18:51:49 -0700 | [diff] [blame] | 113 | /** |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 114 | * Move the highlight to the next match. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 115 | * @param next If {@code true}, find the next match further down in the document. |
| 116 | * If {@code false}, find the previous match, up in the document. |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 117 | */ |
| 118 | private void findNext(boolean next) { |
| 119 | if (mWebView == null) { |
| 120 | throw new AssertionError( |
| 121 | "No WebView for FindActionModeCallback::findNext"); |
| 122 | } |
Leon Scroggins | 7014b12 | 2011-01-11 15:17:34 -0500 | [diff] [blame] | 123 | if (!mMatchesFound) { |
| 124 | findAll(); |
| 125 | return; |
| 126 | } |
| 127 | if (0 == mNumberOfMatches) { |
| 128 | // There are no matches, so moving to the next match will not do |
| 129 | // anything. |
| 130 | return; |
| 131 | } |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 132 | mWebView.findNext(next); |
Leon Scroggins | 7014b12 | 2011-01-11 15:17:34 -0500 | [diff] [blame] | 133 | updateMatchesString(); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Nate Fischer | 5cca7bd | 2017-09-26 18:51:49 -0700 | [diff] [blame] | 136 | /** |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 137 | * Highlight all the instances of the string from mEditText in mWebView. |
| 138 | */ |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 139 | public void findAll() { |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 140 | if (mWebView == null) { |
| 141 | throw new AssertionError( |
| 142 | "No WebView for FindActionModeCallback::findAll"); |
| 143 | } |
| 144 | CharSequence find = mEditText.getText(); |
| 145 | if (0 == find.length()) { |
| 146 | mWebView.clearMatches(); |
| 147 | mMatches.setVisibility(View.GONE); |
| 148 | mMatchesFound = false; |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 149 | mWebView.findAll(null); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 150 | } else { |
| 151 | mMatchesFound = true; |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 152 | mMatches.setVisibility(View.INVISIBLE); |
| 153 | mNumberOfMatches = 0; |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 154 | mWebView.findAllAsync(find.toString()); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Leon Scroggins | 571354f | 2011-01-04 10:12:41 -0500 | [diff] [blame] | 158 | public void showSoftInput() { |
Yohei Yukawa | 5f05965 | 2015-05-14 22:16:41 -0700 | [diff] [blame] | 159 | if (mEditText.requestFocus()) { |
| 160 | mInput.showSoftInput(mEditText, 0); |
| 161 | } |
Leon Scroggins | 571354f | 2011-01-04 10:12:41 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 164 | public void updateMatchCount(int matchIndex, int matchCount, boolean isEmptyFind) { |
| 165 | if (!isEmptyFind) { |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 166 | mNumberOfMatches = matchCount; |
| 167 | mActiveMatchIndex = matchIndex; |
| 168 | updateMatchesString(); |
| 169 | } else { |
Stefan Wysocki | 1c15b8d | 2012-09-18 14:11:20 +0200 | [diff] [blame] | 170 | mMatches.setVisibility(View.GONE); |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 171 | mNumberOfMatches = 0; |
| 172 | } |
| 173 | } |
| 174 | |
Nate Fischer | 5cca7bd | 2017-09-26 18:51:49 -0700 | [diff] [blame] | 175 | /** |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 176 | * Update the string which tells the user how many matches were found, and |
| 177 | * which match is currently highlighted. |
| 178 | */ |
| 179 | private void updateMatchesString() { |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 180 | if (mNumberOfMatches == 0) { |
| 181 | mMatches.setText(com.android.internal.R.string.no_matches); |
| 182 | } else { |
| 183 | mMatches.setText(mResources.getQuantityString( |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 184 | com.android.internal.R.plurals.matches_found, mNumberOfMatches, |
Victoria Lease | 47d0ee9 | 2012-02-13 15:47:48 -0800 | [diff] [blame] | 185 | mActiveMatchIndex + 1, mNumberOfMatches)); |
| 186 | } |
| 187 | mMatches.setVisibility(View.VISIBLE); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Leon Scroggins | 7014b12 | 2011-01-11 15:17:34 -0500 | [diff] [blame] | 190 | // OnClickListener implementation |
| 191 | |
| 192 | @Override |
| 193 | public void onClick(View v) { |
| 194 | findNext(true); |
| 195 | } |
| 196 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 197 | // ActionMode.Callback implementation |
| 198 | |
| 199 | @Override |
| 200 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Adam Powell | f8419a0 | 2011-10-03 12:08:54 -0700 | [diff] [blame] | 201 | if (!mode.isUiFocusable()) { |
| 202 | // If the action mode we're running in is not focusable the user |
| 203 | // will not be able to type into the find on page field. This |
| 204 | // should only come up when we're running in a dialog which is |
| 205 | // already less than ideal; disable the option for now. |
| 206 | return false; |
| 207 | } |
| 208 | |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 209 | mode.setCustomView(mCustomView); |
| 210 | mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find, |
| 211 | menu); |
Leon Scroggins | 05919f2 | 2010-09-14 17:22:36 -0400 | [diff] [blame] | 212 | mActionMode = mode; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 213 | Editable edit = mEditText.getText(); |
| 214 | Selection.setSelection(edit, edit.length()); |
| 215 | mMatches.setVisibility(View.GONE); |
| 216 | mMatchesFound = false; |
| 217 | mMatches.setText("0"); |
| 218 | mEditText.requestFocus(); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void onDestroyActionMode(ActionMode mode) { |
John Reck | fffce6f | 2011-10-06 20:37:01 -0700 | [diff] [blame] | 224 | mActionMode = null; |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 225 | mWebView.notifyFindDialogDismissed(); |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 226 | mWebView.setFindDialogFindListener(null); |
| 227 | mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Leon Scroggins | 261f42d | 2010-12-09 13:46:28 -0500 | [diff] [blame] | 237 | if (mWebView == null) { |
| 238 | throw new AssertionError( |
| 239 | "No WebView for FindActionModeCallback::onActionItemClicked"); |
| 240 | } |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 241 | mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 242 | switch(item.getItemId()) { |
| 243 | case com.android.internal.R.id.find_prev: |
| 244 | findNext(false); |
| 245 | break; |
| 246 | case com.android.internal.R.id.find_next: |
| 247 | findNext(true); |
| 248 | break; |
| 249 | default: |
| 250 | return false; |
| 251 | } |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | |
| 255 | // TextWatcher implementation |
| 256 | |
| 257 | @Override |
| 258 | public void beforeTextChanged(CharSequence s, |
| 259 | int start, |
| 260 | int count, |
| 261 | int after) { |
| 262 | // Does nothing. Needed to implement TextWatcher. |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onTextChanged(CharSequence s, |
| 267 | int start, |
| 268 | int before, |
| 269 | int count) { |
| 270 | findAll(); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void afterTextChanged(Editable s) { |
| 275 | // Does nothing. Needed to implement TextWatcher. |
| 276 | } |
| 277 | |
John Reck | d6ac727 | 2011-11-08 15:00:17 -0800 | [diff] [blame] | 278 | private Rect mGlobalVisibleRect = new Rect(); |
| 279 | private Point mGlobalVisibleOffset = new Point(); |
| 280 | public int getActionModeGlobalBottom() { |
John Reck | fffce6f | 2011-10-06 20:37:01 -0700 | [diff] [blame] | 281 | if (mActionMode == null) { |
| 282 | return 0; |
| 283 | } |
John Reck | d6ac727 | 2011-11-08 15:00:17 -0800 | [diff] [blame] | 284 | View view = (View) mCustomView.getParent(); |
| 285 | if (view == null) { |
| 286 | view = mCustomView; |
| 287 | } |
| 288 | view.getGlobalVisibleRect(mGlobalVisibleRect, mGlobalVisibleOffset); |
| 289 | return mGlobalVisibleRect.bottom; |
John Reck | fffce6f | 2011-10-06 20:37:01 -0700 | [diff] [blame] | 290 | } |
| 291 | |
George Mount | 7e9f62e | 2012-05-23 16:55:46 -0700 | [diff] [blame] | 292 | public static class NoAction implements ActionMode.Callback { |
| 293 | @Override |
| 294 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void onDestroyActionMode(ActionMode mode) { |
| 310 | } |
| 311 | } |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 312 | } |