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