| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.webkit; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.res.Resources; |
| 23 |
import android.graphics.Point; |
| 24 |
import android.graphics.Rect; |
| 25 |
import android.text.Editable; |
| 26 |
import android.text.Selection; |
| 27 |
import android.text.Spannable; |
| 28 |
import android.text.TextWatcher; |
| 29 |
import android.util.PluralsMessageFormatter; |
| 30 |
import android.view.ActionMode; |
| 31 |
import android.view.LayoutInflater; |
| 32 |
import android.view.Menu; |
| 33 |
import android.view.MenuItem; |
| 34 |
import android.view.View; |
| 35 |
import android.view.inputmethod.InputMethodManager; |
| 36 |
import android.widget.EditText; |
| 37 |
import android.widget.TextView; |
| 38 |
|
| 39 |
import com.android.internal.R; |
| 40 |
|
| 41 |
import java.util.HashMap; |
| 42 |
import java.util.Map; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@SystemApi |
| 48 |
public class FindActionModeCallback implements ActionMode.Callback, TextWatcher, |
| 49 |
View.OnClickListener, WebView.FindListener { |
| 50 |
private View mCustomView; |
| 51 |
private EditText mEditText; |
| 52 |
private TextView mMatches; |
| 53 |
private WebView mWebView; |
| 54 |
private InputMethodManager mInput; |
| 55 |
private Resources mResources; |
| 56 |
private boolean mMatchesFound; |
| 57 |
private int mNumberOfMatches; |
| 58 |
private int mActiveMatchIndex; |
| 59 |
private ActionMode mActionMode; |
| 60 |
|
| 61 |
public FindActionModeCallback(Context context) { |
| 62 |
mCustomView = LayoutInflater.from(context).inflate( |
| 63 |
com.android.internal.R.layout.webview_find, null); |
| 64 |
mEditText = (EditText) mCustomView.findViewById( |
| 65 |
com.android.internal.R.id.edit); |
| 66 |
mEditText.setCustomSelectionActionModeCallback(new NoAction()); |
| 67 |
mEditText.setOnClickListener(this); |
| 68 |
setText(""); |
| 69 |
mMatches = (TextView) mCustomView.findViewById( |
| 70 |
com.android.internal.R.id.matches); |
| 71 |
mInput = context.getSystemService(InputMethodManager.class); |
| 72 |
mResources = context.getResources(); |
| 73 |
} |
| 74 |
|
| 75 |
public void finish() { |
| 76 |
mActionMode.finish(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void setText(String text) { |
| 84 |
mEditText.setText(text); |
| 85 |
Spannable span = (Spannable) mEditText.getText(); |
| 86 |
int length = span.length(); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
Selection.setSelection(span, length, length); |
| 91 |
|
| 92 |
|
| 93 |
span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 94 |
mMatchesFound = false; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setWebView(@NonNull WebView webView) { |
| 103 |
if (null == webView) { |
| 104 |
throw new AssertionError("WebView supplied to " |
| 105 |
+ "FindActionModeCallback cannot be null"); |
| 106 |
} |
| 107 |
mWebView = webView; |
| 108 |
mWebView.setFindDialogFindListener(this); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, |
| 113 |
boolean isDoneCounting) { |
| 114 |
if (isDoneCounting) { |
| 115 |
updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private void findNext(boolean next) { |
| 125 |
if (mWebView == null) { |
| 126 |
throw new AssertionError( |
| 127 |
"No WebView for FindActionModeCallback::findNext"); |
| 128 |
} |
| 129 |
if (!mMatchesFound) { |
| 130 |
findAll(); |
| 131 |
return; |
| 132 |
} |
| 133 |
if (0 == mNumberOfMatches) { |
| 134 |
|
| 135 |
|
| 136 |
return; |
| 137 |
} |
| 138 |
mWebView.findNext(next); |
| 139 |
updateMatchesString(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void findAll() { |
| 146 |
if (mWebView == null) { |
| 147 |
throw new AssertionError( |
| 148 |
"No WebView for FindActionModeCallback::findAll"); |
| 149 |
} |
| 150 |
CharSequence find = mEditText.getText(); |
| 151 |
if (0 == find.length()) { |
| 152 |
mWebView.clearMatches(); |
| 153 |
mMatches.setVisibility(View.GONE); |
| 154 |
mMatchesFound = false; |
| 155 |
mWebView.findAll(null); |
| 156 |
} else { |
| 157 |
mMatchesFound = true; |
| 158 |
mMatches.setVisibility(View.INVISIBLE); |
| 159 |
mNumberOfMatches = 0; |
| 160 |
mWebView.findAllAsync(find.toString()); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public void showSoftInput() { |
| 165 |
if (mEditText.requestFocus()) { |
| 166 |
mInput.showSoftInput(mEditText, 0); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public void updateMatchCount(int matchIndex, int matchCount, boolean isEmptyFind) { |
| 171 |
if (!isEmptyFind) { |
| 172 |
mNumberOfMatches = matchCount; |
| 173 |
mActiveMatchIndex = matchIndex; |
| 174 |
updateMatchesString(); |
| 175 |
} else { |
| 176 |
mMatches.setVisibility(View.GONE); |
| 177 |
mNumberOfMatches = 0; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private void updateMatchesString() { |
| 186 |
if (mNumberOfMatches == 0) { |
| 187 |
mMatches.setText(com.android.internal.R.string.no_matches); |
| 188 |
} else { |
| 189 |
Map<String, Object> arguments = new HashMap<>(); |
| 190 |
arguments.put("count", mActiveMatchIndex + 1); |
| 191 |
arguments.put("total", mNumberOfMatches); |
| 192 |
|
| 193 |
mMatches.setText(PluralsMessageFormatter.format( |
| 194 |
mResources, |
| 195 |
arguments, |
| 196 |
R.string.matches_found)); |
| 197 |
} |
| 198 |
mMatches.setVisibility(View.VISIBLE); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
@Override |
| 204 |
public void onClick(View v) { |
| 205 |
findNext(true); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 212 |
if (!mode.isUiFocusable()) { |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
mode.setCustomView(mCustomView); |
| 221 |
mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find, |
| 222 |
menu); |
| 223 |
mActionMode = mode; |
| 224 |
Editable edit = mEditText.getText(); |
| 225 |
Selection.setSelection(edit, edit.length()); |
| 226 |
mMatches.setVisibility(View.GONE); |
| 227 |
mMatchesFound = false; |
| 228 |
mMatches.setText("0"); |
| 229 |
mEditText.requestFocus(); |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public void onDestroyActionMode(ActionMode mode) { |
| 235 |
mActionMode = null; |
| 236 |
mWebView.notifyFindDialogDismissed(); |
| 237 |
mWebView.setFindDialogFindListener(null); |
| 238 |
mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0); |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 248 |
if (mWebView == null) { |
| 249 |
throw new AssertionError( |
| 250 |
"No WebView for FindActionModeCallback::onActionItemClicked"); |
| 251 |
} |
| 252 |
mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0); |
| 253 |
switch(item.getItemId()) { |
| 254 |
case com.android.internal.R.id.find_prev: |
| 255 |
findNext(false); |
| 256 |
break; |
| 257 |
case com.android.internal.R.id.find_next: |
| 258 |
findNext(true); |
| 259 |
break; |
| 260 |
default: |
| 261 |
return false; |
| 262 |
} |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@Override |
| 269 |
public void beforeTextChanged(CharSequence s, |
| 270 |
int start, |
| 271 |
int count, |
| 272 |
int after) { |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public void onTextChanged(CharSequence s, |
| 278 |
int start, |
| 279 |
int before, |
| 280 |
int count) { |
| 281 |
findAll(); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public void afterTextChanged(Editable s) { |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
private Rect mGlobalVisibleRect = new Rect(); |
| 290 |
private Point mGlobalVisibleOffset = new Point(); |
| 291 |
public int getActionModeGlobalBottom() { |
| 292 |
if (mActionMode == null) { |
| 293 |
return 0; |
| 294 |
} |
| 295 |
View view = (View) mCustomView.getParent(); |
| 296 |
if (view == null) { |
| 297 |
view = mCustomView; |
| 298 |
} |
| 299 |
view.getGlobalVisibleRect(mGlobalVisibleRect, mGlobalVisibleOffset); |
| 300 |
return mGlobalVisibleRect.bottom; |
| 301 |
} |
| 302 |
|
| 303 |
public static class NoAction implements ActionMode.Callback { |
| 304 |
@Override |
| 305 |
public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public void onDestroyActionMode(ActionMode mode) { |
| 321 |
} |
| 322 |
} |
| 323 |
} |