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