| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.UiThread; |
| 22 |
import android.annotation.WorkerThread; |
| 23 |
import android.app.RemoteAction; |
| 24 |
import android.content.Context; |
| 25 |
import android.graphics.Canvas; |
| 26 |
import android.graphics.PointF; |
| 27 |
import android.graphics.RectF; |
| 28 |
import android.os.AsyncTask; |
| 29 |
import android.os.Build; |
| 30 |
import android.os.Bundle; |
| 31 |
import android.os.LocaleList; |
| 32 |
import android.text.Layout; |
| 33 |
import android.text.Selection; |
| 34 |
import android.text.Spannable; |
| 35 |
import android.text.TextUtils; |
| 36 |
import android.text.util.Linkify; |
| 37 |
import android.util.Log; |
| 38 |
import android.view.ActionMode; |
| 39 |
import android.view.ViewConfiguration; |
| 40 |
import android.view.textclassifier.ExtrasUtils; |
| 41 |
import android.view.textclassifier.SelectionEvent; |
| 42 |
import android.view.textclassifier.SelectionEvent.InvocationMethod; |
| 43 |
import android.view.textclassifier.TextClassification; |
| 44 |
import android.view.textclassifier.TextClassificationConstants; |
| 45 |
import android.view.textclassifier.TextClassificationContext; |
| 46 |
import android.view.textclassifier.TextClassificationManager; |
| 47 |
import android.view.textclassifier.TextClassifier; |
| 48 |
import android.view.textclassifier.TextClassifierEvent; |
| 49 |
import android.view.textclassifier.TextSelection; |
| 50 |
import android.widget.Editor.SelectionModifierCursorController; |
| 51 |
|
| 52 |
import com.android.internal.annotations.VisibleForTesting; |
| 53 |
import com.android.internal.util.Preconditions; |
| 54 |
|
| 55 |
import java.text.BreakIterator; |
| 56 |
import java.util.ArrayList; |
| 57 |
import java.util.Comparator; |
| 58 |
import java.util.List; |
| 59 |
import java.util.Objects; |
| 60 |
import java.util.function.Consumer; |
| 61 |
import java.util.function.Function; |
| 62 |
import java.util.function.Supplier; |
| 63 |
import java.util.regex.Pattern; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@UiThread |
| 71 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 72 |
public final class SelectionActionModeHelper { |
| 73 |
|
| 74 |
private static final String LOG_TAG = "SelectActionModeHelper"; |
| 75 |
|
| 76 |
private final Editor mEditor; |
| 77 |
private final TextView mTextView; |
| 78 |
private final TextClassificationHelper mTextClassificationHelper; |
| 79 |
|
| 80 |
@Nullable private TextClassification mTextClassification; |
| 81 |
private AsyncTask mTextClassificationAsyncTask; |
| 82 |
|
| 83 |
private final SelectionTracker mSelectionTracker; |
| 84 |
|
| 85 |
|
| 86 |
@Nullable |
| 87 |
private final SmartSelectSprite mSmartSelectSprite; |
| 88 |
|
| 89 |
SelectionActionModeHelper(@NonNull Editor editor) { |
| 90 |
mEditor = Objects.requireNonNull(editor); |
| 91 |
mTextView = mEditor.getTextView(); |
| 92 |
mTextClassificationHelper = new TextClassificationHelper( |
| 93 |
mTextView.getContext(), |
| 94 |
mTextView::getTextClassificationSession, |
| 95 |
getText(mTextView), |
| 96 |
0, 1, mTextView.getTextLocales()); |
| 97 |
mSelectionTracker = new SelectionTracker(mTextView); |
| 98 |
|
| 99 |
if (getTextClassificationSettings().isSmartSelectionAnimationEnabled()) { |
| 100 |
mSmartSelectSprite = new SmartSelectSprite(mTextView.getContext(), |
| 101 |
editor.getTextView().mHighlightColor, mTextView::invalidate); |
| 102 |
} else { |
| 103 |
mSmartSelectSprite = null; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private static int[] sortSelectionIndices(int selectionStart, int selectionEnd) { |
| 113 |
if (selectionStart < selectionEnd) { |
| 114 |
return new int[]{selectionStart, selectionEnd}; |
| 115 |
} |
| 116 |
return new int[]{selectionEnd, selectionStart}; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private static int[] sortSelectionIndicesFromTextView(TextView textView) { |
| 127 |
int selectionStart = textView.getSelectionStart(); |
| 128 |
int selectionEnd = textView.getSelectionEnd(); |
| 129 |
|
| 130 |
return sortSelectionIndices(selectionStart, selectionEnd); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void startSelectionActionModeAsync(boolean adjustSelection) { |
| 137 |
|
| 138 |
adjustSelection &= getTextClassificationSettings().isSmartSelectionEnabled(); |
| 139 |
int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 140 |
|
| 141 |
mSelectionTracker.onOriginalSelection( |
| 142 |
getText(mTextView), |
| 143 |
sortedSelectionIndices[0], |
| 144 |
sortedSelectionIndices[1], |
| 145 |
false ); |
| 146 |
cancelAsyncTask(); |
| 147 |
if (skipTextClassification()) { |
| 148 |
startSelectionActionMode(null); |
| 149 |
} else { |
| 150 |
resetTextClassificationHelper(); |
| 151 |
mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
| 152 |
mTextView, |
| 153 |
mTextClassificationHelper.getTimeoutDuration(), |
| 154 |
adjustSelection |
| 155 |
? mTextClassificationHelper::suggestSelection |
| 156 |
: mTextClassificationHelper::classifyText, |
| 157 |
mSmartSelectSprite != null |
| 158 |
? this::startSelectionActionModeWithSmartSelectAnimation |
| 159 |
: this::startSelectionActionMode, |
| 160 |
mTextClassificationHelper::getOriginalSelection) |
| 161 |
.execute(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public void startLinkActionModeAsync(int start, int end) { |
| 169 |
int[] indexResult = sortSelectionIndices(start, end); |
| 170 |
mSelectionTracker.onOriginalSelection(getText(mTextView), indexResult[0], indexResult[1], |
| 171 |
true ); |
| 172 |
cancelAsyncTask(); |
| 173 |
if (skipTextClassification()) { |
| 174 |
startLinkActionMode(null); |
| 175 |
} else { |
| 176 |
resetTextClassificationHelper(indexResult[0], indexResult[1]); |
| 177 |
mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
| 178 |
mTextView, |
| 179 |
mTextClassificationHelper.getTimeoutDuration(), |
| 180 |
mTextClassificationHelper::classifyText, |
| 181 |
this::startLinkActionMode, |
| 182 |
mTextClassificationHelper::getOriginalSelection) |
| 183 |
.execute(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public void invalidateActionModeAsync() { |
| 188 |
cancelAsyncTask(); |
| 189 |
if (skipTextClassification()) { |
| 190 |
invalidateActionMode(null); |
| 191 |
} else { |
| 192 |
resetTextClassificationHelper(); |
| 193 |
mTextClassificationAsyncTask = new TextClassificationAsyncTask( |
| 194 |
mTextView, |
| 195 |
mTextClassificationHelper.getTimeoutDuration(), |
| 196 |
mTextClassificationHelper::classifyText, |
| 197 |
this::invalidateActionMode, |
| 198 |
mTextClassificationHelper::getOriginalSelection) |
| 199 |
.execute(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
public void onSelectionAction(int menuItemId, @Nullable String actionLabel) { |
| 205 |
int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 206 |
mSelectionTracker.onSelectionAction( |
| 207 |
sortedSelectionIndices[0], sortedSelectionIndices[1], |
| 208 |
getActionType(menuItemId), actionLabel, mTextClassification); |
| 209 |
} |
| 210 |
|
| 211 |
public void onSelectionDrag() { |
| 212 |
int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 213 |
mSelectionTracker.onSelectionAction( |
| 214 |
sortedSelectionIndices[0], sortedSelectionIndices[1], |
| 215 |
SelectionEvent.ACTION_DRAG, null, mTextClassification); |
| 216 |
} |
| 217 |
|
| 218 |
public void onTextChanged(int start, int end) { |
| 219 |
int[] sortedSelectionIndices = sortSelectionIndices(start, end); |
| 220 |
mSelectionTracker.onTextChanged(sortedSelectionIndices[0], sortedSelectionIndices[1], |
| 221 |
mTextClassification); |
| 222 |
} |
| 223 |
|
| 224 |
public boolean resetSelection(int textIndex) { |
| 225 |
if (mSelectionTracker.resetSelection(textIndex, mEditor)) { |
| 226 |
invalidateActionModeAsync(); |
| 227 |
return true; |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
@Nullable |
| 233 |
public TextClassification getTextClassification() { |
| 234 |
return mTextClassification; |
| 235 |
} |
| 236 |
|
| 237 |
public void onDestroyActionMode() { |
| 238 |
cancelSmartSelectAnimation(); |
| 239 |
mSelectionTracker.onSelectionDestroyed(); |
| 240 |
cancelAsyncTask(); |
| 241 |
} |
| 242 |
|
| 243 |
public void onDraw(final Canvas canvas) { |
| 244 |
if (isDrawingHighlight() && mSmartSelectSprite != null) { |
| 245 |
mSmartSelectSprite.draw(canvas); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public boolean isDrawingHighlight() { |
| 250 |
return mSmartSelectSprite != null && mSmartSelectSprite.isAnimationActive(); |
| 251 |
} |
| 252 |
|
| 253 |
private TextClassificationConstants getTextClassificationSettings() { |
| 254 |
return TextClassificationManager.getSettings(mTextView.getContext()); |
| 255 |
} |
| 256 |
|
| 257 |
private void cancelAsyncTask() { |
| 258 |
if (mTextClassificationAsyncTask != null) { |
| 259 |
mTextClassificationAsyncTask.cancel(true); |
| 260 |
mTextClassificationAsyncTask = null; |
| 261 |
} |
| 262 |
mTextClassification = null; |
| 263 |
} |
| 264 |
|
| 265 |
private boolean skipTextClassification() { |
| 266 |
|
| 267 |
final boolean noOpTextClassifier = mTextView.usesNoOpTextClassifier(); |
| 268 |
|
| 269 |
final boolean noSelection = mTextView.getSelectionEnd() == mTextView.getSelectionStart(); |
| 270 |
|
| 271 |
final boolean password = mTextView.hasPasswordTransformationMethod() |
| 272 |
|| TextView.isPasswordInputType(mTextView.getInputType()); |
| 273 |
return noOpTextClassifier || noSelection || password; |
| 274 |
} |
| 275 |
|
| 276 |
private void startLinkActionMode(@Nullable SelectionResult result) { |
| 277 |
startActionMode(Editor.TextActionMode.TEXT_LINK, result); |
| 278 |
} |
| 279 |
|
| 280 |
private void startSelectionActionMode(@Nullable SelectionResult result) { |
| 281 |
startActionMode(Editor.TextActionMode.SELECTION, result); |
| 282 |
} |
| 283 |
|
| 284 |
private void startActionMode( |
| 285 |
@Editor.TextActionMode int actionMode, @Nullable SelectionResult result) { |
| 286 |
final CharSequence text = getText(mTextView); |
| 287 |
if (result != null && text instanceof Spannable |
| 288 |
&& (mTextView.isTextSelectable() || mTextView.isTextEditable())) { |
| 289 |
|
| 290 |
if (!getTextClassificationSettings().isModelDarkLaunchEnabled()) { |
| 291 |
Selection.setSelection((Spannable) text, result.mStart, result.mEnd); |
| 292 |
mTextView.invalidate(); |
| 293 |
} |
| 294 |
mTextClassification = result.mClassification; |
| 295 |
} else if (result != null && actionMode == Editor.TextActionMode.TEXT_LINK) { |
| 296 |
mTextClassification = result.mClassification; |
| 297 |
} else { |
| 298 |
mTextClassification = null; |
| 299 |
} |
| 300 |
if (mEditor.startActionModeInternal(actionMode)) { |
| 301 |
final SelectionModifierCursorController controller = mEditor.getSelectionController(); |
| 302 |
if (controller != null |
| 303 |
&& (mTextView.isTextSelectable() || mTextView.isTextEditable())) { |
| 304 |
controller.show(); |
| 305 |
} |
| 306 |
if (result != null) { |
| 307 |
switch (actionMode) { |
| 308 |
case Editor.TextActionMode.SELECTION: |
| 309 |
mSelectionTracker.onSmartSelection(result); |
| 310 |
break; |
| 311 |
case Editor.TextActionMode.TEXT_LINK: |
| 312 |
mSelectionTracker.onLinkSelected(result); |
| 313 |
break; |
| 314 |
default: |
| 315 |
break; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
mEditor.setRestartActionModeOnNextRefresh(false); |
| 320 |
mTextClassificationAsyncTask = null; |
| 321 |
} |
| 322 |
|
| 323 |
private void startSelectionActionModeWithSmartSelectAnimation( |
| 324 |
@Nullable SelectionResult result) { |
| 325 |
final Layout layout = mTextView.getLayout(); |
| 326 |
|
| 327 |
final Runnable onAnimationEndCallback = () -> { |
| 328 |
final SelectionResult startSelectionResult; |
| 329 |
if (result != null && result.mStart >= 0 && result.mEnd <= getText(mTextView).length() |
| 330 |
&& result.mStart <= result.mEnd) { |
| 331 |
startSelectionResult = result; |
| 332 |
} else { |
| 333 |
startSelectionResult = null; |
| 334 |
} |
| 335 |
startSelectionActionMode(startSelectionResult); |
| 336 |
}; |
| 337 |
|
| 338 |
int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 339 |
final boolean didSelectionChange = |
| 340 |
result != null && (sortedSelectionIndices[0] != result.mStart |
| 341 |
|| sortedSelectionIndices[1] != result.mEnd); |
| 342 |
if (!didSelectionChange) { |
| 343 |
onAnimationEndCallback.run(); |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
final List<SmartSelectSprite.RectangleWithTextSelectionLayout> selectionRectangles = |
| 348 |
convertSelectionToRectangles(layout, result.mStart, result.mEnd); |
| 349 |
|
| 350 |
final PointF touchPoint = new PointF( |
| 351 |
mEditor.getLastUpPositionX(), |
| 352 |
mEditor.getLastUpPositionY()); |
| 353 |
|
| 354 |
final PointF animationStartPoint = |
| 355 |
movePointInsideNearestRectangle(touchPoint, selectionRectangles, |
| 356 |
SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle); |
| 357 |
|
| 358 |
mSmartSelectSprite.startAnimation( |
| 359 |
animationStartPoint, |
| 360 |
selectionRectangles, |
| 361 |
onAnimationEndCallback); |
| 362 |
} |
| 363 |
|
| 364 |
private List<SmartSelectSprite.RectangleWithTextSelectionLayout> convertSelectionToRectangles( |
| 365 |
final Layout layout, final int start, final int end) { |
| 366 |
final List<SmartSelectSprite.RectangleWithTextSelectionLayout> result = new ArrayList<>(); |
| 367 |
|
| 368 |
final Layout.SelectionRectangleConsumer consumer = |
| 369 |
(left, top, right, bottom, textSelectionLayout) -> mergeRectangleIntoList( |
| 370 |
result, |
| 371 |
new RectF(left, top, right, bottom), |
| 372 |
SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle, |
| 373 |
r -> new SmartSelectSprite.RectangleWithTextSelectionLayout(r, |
| 374 |
textSelectionLayout) |
| 375 |
); |
| 376 |
|
| 377 |
layout.getSelection(start, end, consumer); |
| 378 |
|
| 379 |
result.sort(Comparator.comparing( |
| 380 |
SmartSelectSprite.RectangleWithTextSelectionLayout::getRectangle, |
| 381 |
SmartSelectSprite.RECTANGLE_COMPARATOR)); |
| 382 |
|
| 383 |
return result; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
@VisibleForTesting |
| 406 |
public static <T> void mergeRectangleIntoList(final List<T> list, |
| 407 |
final RectF candidate, final Function<T, RectF> extractor, |
| 408 |
final Function<RectF, T> packer) { |
| 409 |
if (candidate.isEmpty()) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
final int elementCount = list.size(); |
| 414 |
for (int index = 0; index < elementCount; ++index) { |
| 415 |
final RectF existingRectangle = extractor.apply(list.get(index)); |
| 416 |
if (existingRectangle.contains(candidate)) { |
| 417 |
return; |
| 418 |
} |
| 419 |
if (candidate.contains(existingRectangle)) { |
| 420 |
existingRectangle.setEmpty(); |
| 421 |
continue; |
| 422 |
} |
| 423 |
|
| 424 |
final boolean rectanglesContinueEachOther = candidate.left == existingRectangle.right |
| 425 |
|| candidate.right == existingRectangle.left; |
| 426 |
final boolean canMerge = candidate.top == existingRectangle.top |
| 427 |
&& candidate.bottom == existingRectangle.bottom |
| 428 |
&& (RectF.intersects(candidate, existingRectangle) |
| 429 |
|| rectanglesContinueEachOther); |
| 430 |
|
| 431 |
if (canMerge) { |
| 432 |
candidate.union(existingRectangle); |
| 433 |
existingRectangle.setEmpty(); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
for (int index = elementCount - 1; index >= 0; --index) { |
| 438 |
final RectF rectangle = extractor.apply(list.get(index)); |
| 439 |
if (rectangle.isEmpty()) { |
| 440 |
list.remove(index); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
list.add(packer.apply(candidate)); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@VisibleForTesting |
| 450 |
public static <T> PointF movePointInsideNearestRectangle(final PointF point, |
| 451 |
final List<T> list, final Function<T, RectF> extractor) { |
| 452 |
float bestX = -1; |
| 453 |
float bestY = -1; |
| 454 |
double bestDistance = Double.MAX_VALUE; |
| 455 |
|
| 456 |
final int elementCount = list.size(); |
| 457 |
for (int index = 0; index < elementCount; ++index) { |
| 458 |
final RectF rectangle = extractor.apply(list.get(index)); |
| 459 |
final float candidateY = rectangle.centerY(); |
| 460 |
final float candidateX; |
| 461 |
|
| 462 |
if (point.x > rectangle.right) { |
| 463 |
candidateX = rectangle.right; |
| 464 |
} else if (point.x < rectangle.left) { |
| 465 |
candidateX = rectangle.left; |
| 466 |
} else { |
| 467 |
candidateX = point.x; |
| 468 |
} |
| 469 |
|
| 470 |
final double candidateDistance = Math.pow(point.x - candidateX, 2) |
| 471 |
+ Math.pow(point.y - candidateY, 2); |
| 472 |
|
| 473 |
if (candidateDistance < bestDistance) { |
| 474 |
bestX = candidateX; |
| 475 |
bestY = candidateY; |
| 476 |
bestDistance = candidateDistance; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
return new PointF(bestX, bestY); |
| 481 |
} |
| 482 |
|
| 483 |
private void invalidateActionMode(@Nullable SelectionResult result) { |
| 484 |
cancelSmartSelectAnimation(); |
| 485 |
mTextClassification = result != null ? result.mClassification : null; |
| 486 |
final ActionMode actionMode = mEditor.getTextActionMode(); |
| 487 |
if (actionMode != null) { |
| 488 |
actionMode.invalidate(); |
| 489 |
} |
| 490 |
final int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 491 |
mSelectionTracker.onSelectionUpdated( |
| 492 |
sortedSelectionIndices[0], sortedSelectionIndices[1], mTextClassification); |
| 493 |
mTextClassificationAsyncTask = null; |
| 494 |
} |
| 495 |
|
| 496 |
private void resetTextClassificationHelper(int selectionStart, int selectionEnd) { |
| 497 |
if (selectionStart < 0 || selectionEnd < 0) { |
| 498 |
|
| 499 |
int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(mTextView); |
| 500 |
selectionStart = sortedSelectionIndices[0]; |
| 501 |
selectionEnd = sortedSelectionIndices[1]; |
| 502 |
} |
| 503 |
mTextClassificationHelper.init( |
| 504 |
mTextView::getTextClassificationSession, |
| 505 |
getText(mTextView), |
| 506 |
selectionStart, selectionEnd, |
| 507 |
mTextView.getTextLocales()); |
| 508 |
} |
| 509 |
|
| 510 |
private void resetTextClassificationHelper() { |
| 511 |
resetTextClassificationHelper(-1, -1); |
| 512 |
} |
| 513 |
|
| 514 |
private void cancelSmartSelectAnimation() { |
| 515 |
if (mSmartSelectSprite != null) { |
| 516 |
mSmartSelectSprite.cancelAnimation(); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
private static final class SelectionTracker { |
| 526 |
|
| 527 |
private final TextView mTextView; |
| 528 |
private SelectionMetricsLogger mLogger; |
| 529 |
|
| 530 |
private int mOriginalStart; |
| 531 |
private int mOriginalEnd; |
| 532 |
private int mSelectionStart; |
| 533 |
private int mSelectionEnd; |
| 534 |
private boolean mAllowReset; |
| 535 |
private final LogAbandonRunnable mDelayedLogAbandon = new LogAbandonRunnable(); |
| 536 |
|
| 537 |
SelectionTracker(TextView textView) { |
| 538 |
mTextView = Objects.requireNonNull(textView); |
| 539 |
mLogger = new SelectionMetricsLogger(textView); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public void onOriginalSelection( |
| 546 |
CharSequence text, int selectionStart, int selectionEnd, boolean isLink) { |
| 547 |
|
| 548 |
|
| 549 |
mDelayedLogAbandon.flush(); |
| 550 |
|
| 551 |
mOriginalStart = mSelectionStart = selectionStart; |
| 552 |
mOriginalEnd = mSelectionEnd = selectionEnd; |
| 553 |
mAllowReset = false; |
| 554 |
maybeInvalidateLogger(); |
| 555 |
mLogger.logSelectionStarted( |
| 556 |
mTextView.getTextClassificationSession(), |
| 557 |
mTextView.getTextClassificationContext(), |
| 558 |
text, |
| 559 |
selectionStart, |
| 560 |
isLink ? SelectionEvent.INVOCATION_LINK : SelectionEvent.INVOCATION_MANUAL); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public void onSmartSelection(SelectionResult result) { |
| 567 |
onClassifiedSelection(result); |
| 568 |
mTextView.notifyContentCaptureTextChanged(); |
| 569 |
mLogger.logSelectionModified( |
| 570 |
result.mStart, result.mEnd, result.mClassification, result.mSelection); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
public void onLinkSelected(SelectionResult result) { |
| 577 |
onClassifiedSelection(result); |
| 578 |
|
| 579 |
} |
| 580 |
|
| 581 |
private void onClassifiedSelection(SelectionResult result) { |
| 582 |
if (isSelectionStarted()) { |
| 583 |
mSelectionStart = result.mStart; |
| 584 |
mSelectionEnd = result.mEnd; |
| 585 |
mAllowReset = mSelectionStart != mOriginalStart || mSelectionEnd != mOriginalEnd; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public void onSelectionUpdated( |
| 593 |
int selectionStart, int selectionEnd, |
| 594 |
@Nullable TextClassification classification) { |
| 595 |
if (isSelectionStarted()) { |
| 596 |
mSelectionStart = selectionStart; |
| 597 |
mSelectionEnd = selectionEnd; |
| 598 |
mAllowReset = false; |
| 599 |
mTextView.notifyContentCaptureTextChanged(); |
| 600 |
mLogger.logSelectionModified(selectionStart, selectionEnd, classification, null); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public void onSelectionDestroyed() { |
| 608 |
mAllowReset = false; |
| 609 |
mTextView.notifyContentCaptureTextChanged(); |
| 610 |
|
| 611 |
mDelayedLogAbandon.schedule(100 ); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public void onSelectionAction( |
| 618 |
int selectionStart, int selectionEnd, |
| 619 |
@SelectionEvent.ActionType int action, |
| 620 |
@Nullable String actionLabel, |
| 621 |
@Nullable TextClassification classification) { |
| 622 |
if (isSelectionStarted()) { |
| 623 |
mAllowReset = false; |
| 624 |
mLogger.logSelectionAction( |
| 625 |
selectionStart, selectionEnd, action, actionLabel, classification); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public boolean resetSelection(int textIndex, Editor editor) { |
| 636 |
final TextView textView = editor.getTextView(); |
| 637 |
if (isSelectionStarted() |
| 638 |
&& mAllowReset |
| 639 |
&& textIndex >= mSelectionStart && textIndex <= mSelectionEnd |
| 640 |
&& getText(textView) instanceof Spannable) { |
| 641 |
mAllowReset = false; |
| 642 |
boolean selected = editor.selectCurrentWord(); |
| 643 |
if (selected) { |
| 644 |
final int[] sortedSelectionIndices = sortSelectionIndicesFromTextView(textView); |
| 645 |
mSelectionStart = sortedSelectionIndices[0]; |
| 646 |
mSelectionEnd = sortedSelectionIndices[1]; |
| 647 |
mLogger.logSelectionAction( |
| 648 |
sortedSelectionIndices[0], sortedSelectionIndices[1], |
| 649 |
SelectionEvent.ACTION_RESET, |
| 650 |
null, null); |
| 651 |
} |
| 652 |
return selected; |
| 653 |
} |
| 654 |
return false; |
| 655 |
} |
| 656 |
|
| 657 |
public void onTextChanged(int start, int end, TextClassification classification) { |
| 658 |
if (isSelectionStarted() && start == mSelectionStart && end == mSelectionEnd) { |
| 659 |
onSelectionAction( |
| 660 |
start, end, SelectionEvent.ACTION_OVERTYPE, |
| 661 |
null, classification); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
private void maybeInvalidateLogger() { |
| 666 |
if (mLogger.isEditTextLogger() != mTextView.isTextEditable()) { |
| 667 |
mLogger = new SelectionMetricsLogger(mTextView); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
private boolean isSelectionStarted() { |
| 672 |
return mSelectionStart >= 0 && mSelectionEnd >= 0 && mSelectionStart != mSelectionEnd; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
private final class LogAbandonRunnable implements Runnable { |
| 677 |
private boolean mIsPending; |
| 678 |
|
| 679 |
|
| 680 |
void schedule(int delayMillis) { |
| 681 |
if (mIsPending) { |
| 682 |
Log.e(LOG_TAG, "Force flushing abandon due to new scheduling request"); |
| 683 |
flush(); |
| 684 |
} |
| 685 |
mIsPending = true; |
| 686 |
mTextView.postDelayed(this, delayMillis); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
void flush() { |
| 691 |
mTextView.removeCallbacks(this); |
| 692 |
run(); |
| 693 |
} |
| 694 |
|
| 695 |
@Override |
| 696 |
public void run() { |
| 697 |
if (mIsPending) { |
| 698 |
mLogger.logSelectionAction( |
| 699 |
mSelectionStart, mSelectionEnd, |
| 700 |
SelectionEvent.ACTION_ABANDON, |
| 701 |
null, null); |
| 702 |
mSelectionStart = mSelectionEnd = -1; |
| 703 |
mLogger.endTextClassificationSession(); |
| 704 |
mIsPending = false; |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
private static final class SelectionMetricsLogger { |
| 728 |
|
| 729 |
private static final String LOG_TAG = "SelectionMetricsLogger"; |
| 730 |
private static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+"); |
| 731 |
|
| 732 |
private final boolean mEditTextLogger; |
| 733 |
private final BreakIterator mTokenIterator; |
| 734 |
|
| 735 |
@Nullable private TextClassifier mClassificationSession; |
| 736 |
@Nullable private TextClassificationContext mClassificationContext; |
| 737 |
|
| 738 |
@Nullable private TextClassifierEvent mTranslateViewEvent; |
| 739 |
@Nullable private TextClassifierEvent mTranslateClickEvent; |
| 740 |
|
| 741 |
private int mStartIndex; |
| 742 |
private String mText; |
| 743 |
|
| 744 |
SelectionMetricsLogger(TextView textView) { |
| 745 |
Objects.requireNonNull(textView); |
| 746 |
mEditTextLogger = textView.isTextEditable(); |
| 747 |
mTokenIterator = BreakIterator.getWordInstance(textView.getTextLocale()); |
| 748 |
} |
| 749 |
|
| 750 |
public void logSelectionStarted( |
| 751 |
TextClassifier classificationSession, |
| 752 |
TextClassificationContext classificationContext, |
| 753 |
CharSequence text, int index, |
| 754 |
@InvocationMethod int invocationMethod) { |
| 755 |
try { |
| 756 |
Objects.requireNonNull(text); |
| 757 |
Preconditions.checkArgumentInRange(index, 0, text.length(), "index"); |
| 758 |
if (mText == null || !mText.contentEquals(text)) { |
| 759 |
mText = text.toString(); |
| 760 |
} |
| 761 |
mTokenIterator.setText(mText); |
| 762 |
mStartIndex = index; |
| 763 |
mClassificationSession = classificationSession; |
| 764 |
mClassificationContext = classificationContext; |
| 765 |
if (hasActiveClassificationSession()) { |
| 766 |
mClassificationSession.onSelectionEvent( |
| 767 |
SelectionEvent.createSelectionStartedEvent(invocationMethod, 0)); |
| 768 |
} |
| 769 |
} catch (Exception e) { |
| 770 |
|
| 771 |
Log.e(LOG_TAG, "" + e.getMessage(), e); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
public void logSelectionModified(int start, int end, |
| 776 |
@Nullable TextClassification classification, @Nullable TextSelection selection) { |
| 777 |
try { |
| 778 |
if (hasActiveClassificationSession()) { |
| 779 |
Preconditions.checkArgumentInRange(start, 0, mText.length(), "start"); |
| 780 |
Preconditions.checkArgumentInRange(end, start, mText.length(), "end"); |
| 781 |
int[] wordIndices = getWordDelta(start, end); |
| 782 |
if (selection != null) { |
| 783 |
mClassificationSession.onSelectionEvent( |
| 784 |
SelectionEvent.createSelectionModifiedEvent( |
| 785 |
wordIndices[0], wordIndices[1], selection)); |
| 786 |
} else if (classification != null) { |
| 787 |
mClassificationSession.onSelectionEvent( |
| 788 |
SelectionEvent.createSelectionModifiedEvent( |
| 789 |
wordIndices[0], wordIndices[1], classification)); |
| 790 |
} else { |
| 791 |
mClassificationSession.onSelectionEvent( |
| 792 |
SelectionEvent.createSelectionModifiedEvent( |
| 793 |
wordIndices[0], wordIndices[1])); |
| 794 |
} |
| 795 |
maybeGenerateTranslateViewEvent(classification); |
| 796 |
} |
| 797 |
} catch (Exception e) { |
| 798 |
|
| 799 |
Log.e(LOG_TAG, "" + e.getMessage(), e); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
public void logSelectionAction( |
| 804 |
int start, int end, |
| 805 |
@SelectionEvent.ActionType int action, |
| 806 |
@Nullable String actionLabel, |
| 807 |
@Nullable TextClassification classification) { |
| 808 |
try { |
| 809 |
if (hasActiveClassificationSession()) { |
| 810 |
Preconditions.checkArgumentInRange(start, 0, mText.length(), "start"); |
| 811 |
Preconditions.checkArgumentInRange(end, start, mText.length(), "end"); |
| 812 |
int[] wordIndices = getWordDelta(start, end); |
| 813 |
if (classification != null) { |
| 814 |
mClassificationSession.onSelectionEvent( |
| 815 |
SelectionEvent.createSelectionActionEvent( |
| 816 |
wordIndices[0], wordIndices[1], action, |
| 817 |
classification)); |
| 818 |
} else { |
| 819 |
mClassificationSession.onSelectionEvent( |
| 820 |
SelectionEvent.createSelectionActionEvent( |
| 821 |
wordIndices[0], wordIndices[1], action)); |
| 822 |
} |
| 823 |
|
| 824 |
maybeGenerateTranslateClickEvent(classification, actionLabel); |
| 825 |
|
| 826 |
if (SelectionEvent.isTerminal(action)) { |
| 827 |
endTextClassificationSession(); |
| 828 |
} |
| 829 |
} |
| 830 |
} catch (Exception e) { |
| 831 |
|
| 832 |
Log.e(LOG_TAG, "" + e.getMessage(), e); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
public boolean isEditTextLogger() { |
| 837 |
return mEditTextLogger; |
| 838 |
} |
| 839 |
|
| 840 |
public void endTextClassificationSession() { |
| 841 |
if (hasActiveClassificationSession()) { |
| 842 |
maybeReportTranslateEvents(); |
| 843 |
mClassificationSession.destroy(); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
private boolean hasActiveClassificationSession() { |
| 848 |
return mClassificationSession != null && !mClassificationSession.isDestroyed(); |
| 849 |
} |
| 850 |
|
| 851 |
private int[] getWordDelta(int start, int end) { |
| 852 |
int[] wordIndices = new int[2]; |
| 853 |
|
| 854 |
if (start == mStartIndex) { |
| 855 |
wordIndices[0] = 0; |
| 856 |
} else if (start < mStartIndex) { |
| 857 |
wordIndices[0] = -countWordsForward(start); |
| 858 |
} else { |
| 859 |
wordIndices[0] = countWordsBackward(start); |
| 860 |
|
| 861 |
|
| 862 |
if (!mTokenIterator.isBoundary(start) |
| 863 |
&& !isWhitespace( |
| 864 |
mTokenIterator.preceding(start), |
| 865 |
mTokenIterator.following(start))) { |
| 866 |
|
| 867 |
wordIndices[0]--; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
if (end == mStartIndex) { |
| 872 |
wordIndices[1] = 0; |
| 873 |
} else if (end < mStartIndex) { |
| 874 |
wordIndices[1] = -countWordsForward(end); |
| 875 |
} else { |
| 876 |
wordIndices[1] = countWordsBackward(end); |
| 877 |
} |
| 878 |
|
| 879 |
return wordIndices; |
| 880 |
} |
| 881 |
|
| 882 |
private int countWordsBackward(int from) { |
| 883 |
Preconditions.checkArgument(from >= mStartIndex); |
| 884 |
int wordCount = 0; |
| 885 |
int offset = from; |
| 886 |
while (offset > mStartIndex) { |
| 887 |
int start = mTokenIterator.preceding(offset); |
| 888 |
if (!isWhitespace(start, offset)) { |
| 889 |
wordCount++; |
| 890 |
} |
| 891 |
offset = start; |
| 892 |
} |
| 893 |
return wordCount; |
| 894 |
} |
| 895 |
|
| 896 |
private int countWordsForward(int from) { |
| 897 |
Preconditions.checkArgument(from <= mStartIndex); |
| 898 |
int wordCount = 0; |
| 899 |
int offset = from; |
| 900 |
while (offset < mStartIndex) { |
| 901 |
int end = mTokenIterator.following(offset); |
| 902 |
if (!isWhitespace(offset, end)) { |
| 903 |
wordCount++; |
| 904 |
} |
| 905 |
offset = end; |
| 906 |
} |
| 907 |
return wordCount; |
| 908 |
} |
| 909 |
|
| 910 |
private boolean isWhitespace(int start, int end) { |
| 911 |
return PATTERN_WHITESPACE.matcher(mText.substring(start, end)).matches(); |
| 912 |
} |
| 913 |
|
| 914 |
private void maybeGenerateTranslateViewEvent(@Nullable TextClassification classification) { |
| 915 |
if (classification != null) { |
| 916 |
final TextClassifierEvent event = generateTranslateEvent( |
| 917 |
TextClassifierEvent.TYPE_ACTIONS_SHOWN, |
| 918 |
classification, mClassificationContext, null); |
| 919 |
mTranslateViewEvent = (event != null) ? event : mTranslateViewEvent; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
private void maybeGenerateTranslateClickEvent( |
| 924 |
@Nullable TextClassification classification, String actionLabel) { |
| 925 |
if (classification != null) { |
| 926 |
mTranslateClickEvent = generateTranslateEvent( |
| 927 |
TextClassifierEvent.TYPE_SMART_ACTION, |
| 928 |
classification, mClassificationContext, actionLabel); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
private void maybeReportTranslateEvents() { |
| 933 |
|
| 934 |
if (mTranslateViewEvent != null) { |
| 935 |
mClassificationSession.onTextClassifierEvent(mTranslateViewEvent); |
| 936 |
mTranslateViewEvent = null; |
| 937 |
} |
| 938 |
if (mTranslateClickEvent != null) { |
| 939 |
mClassificationSession.onTextClassifierEvent(mTranslateClickEvent); |
| 940 |
mTranslateClickEvent = null; |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
@Nullable |
| 945 |
private static TextClassifierEvent generateTranslateEvent( |
| 946 |
int eventType, TextClassification classification, |
| 947 |
TextClassificationContext classificationContext, @Nullable String actionLabel) { |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
final RemoteAction translateAction = ExtrasUtils.findTranslateAction(classification); |
| 959 |
if (translateAction == null) { |
| 960 |
|
| 961 |
return null; |
| 962 |
} |
| 963 |
|
| 964 |
if (eventType == TextClassifierEvent.TYPE_SMART_ACTION |
| 965 |
&& !translateAction.getTitle().toString().equals(actionLabel)) { |
| 966 |
|
| 967 |
|
| 968 |
return null; |
| 969 |
} |
| 970 |
|
| 971 |
final Bundle foreignLanguageExtra = ExtrasUtils.getForeignLanguageExtra(classification); |
| 972 |
final String language = ExtrasUtils.getEntityType(foreignLanguageExtra); |
| 973 |
final float score = ExtrasUtils.getScore(foreignLanguageExtra); |
| 974 |
final String model = ExtrasUtils.getModelName(foreignLanguageExtra); |
| 975 |
return new TextClassifierEvent.LanguageDetectionEvent.Builder(eventType) |
| 976 |
.setEventContext(classificationContext) |
| 977 |
.setResultId(classification.getId()) |
| 978 |
|
| 979 |
|
| 980 |
.setScores(score) |
| 981 |
.setActionIndices(classification.getActions().indexOf(translateAction)) |
| 982 |
.setModelName(model) |
| 983 |
.build(); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
private static final class TextClassificationAsyncTask |
| 993 |
extends AsyncTask<Void, Void, SelectionResult> { |
| 994 |
|
| 995 |
private final int mTimeOutDuration; |
| 996 |
private final Supplier<SelectionResult> mSelectionResultSupplier; |
| 997 |
private final Consumer<SelectionResult> mSelectionResultCallback; |
| 998 |
private final Supplier<SelectionResult> mTimeOutResultSupplier; |
| 999 |
private final TextView mTextView; |
| 1000 |
private final String mOriginalText; |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
TextClassificationAsyncTask( |
| 1010 |
@NonNull TextView textView, int timeOut, |
| 1011 |
@NonNull Supplier<SelectionResult> selectionResultSupplier, |
| 1012 |
@NonNull Consumer<SelectionResult> selectionResultCallback, |
| 1013 |
@NonNull Supplier<SelectionResult> timeOutResultSupplier) { |
| 1014 |
super(textView != null ? textView.getHandler() : null); |
| 1015 |
mTextView = Objects.requireNonNull(textView); |
| 1016 |
mTimeOutDuration = timeOut; |
| 1017 |
mSelectionResultSupplier = Objects.requireNonNull(selectionResultSupplier); |
| 1018 |
mSelectionResultCallback = Objects.requireNonNull(selectionResultCallback); |
| 1019 |
mTimeOutResultSupplier = Objects.requireNonNull(timeOutResultSupplier); |
| 1020 |
|
| 1021 |
mOriginalText = getText(mTextView).toString(); |
| 1022 |
} |
| 1023 |
|
| 1024 |
@Override |
| 1025 |
@WorkerThread |
| 1026 |
protected SelectionResult doInBackground(Void... params) { |
| 1027 |
final Runnable onTimeOut = this::onTimeOut; |
| 1028 |
mTextView.postDelayed(onTimeOut, mTimeOutDuration); |
| 1029 |
SelectionResult result = null; |
| 1030 |
try { |
| 1031 |
result = mSelectionResultSupplier.get(); |
| 1032 |
} catch (IllegalStateException e) { |
| 1033 |
|
| 1034 |
Log.w(LOG_TAG, "TextClassificationAsyncTask failed.", e); |
| 1035 |
} |
| 1036 |
mTextView.removeCallbacks(onTimeOut); |
| 1037 |
return result; |
| 1038 |
} |
| 1039 |
|
| 1040 |
@Override |
| 1041 |
@UiThread |
| 1042 |
protected void onPostExecute(SelectionResult result) { |
| 1043 |
result = TextUtils.equals(mOriginalText, getText(mTextView)) ? result : null; |
| 1044 |
mSelectionResultCallback.accept(result); |
| 1045 |
} |
| 1046 |
|
| 1047 |
private void onTimeOut() { |
| 1048 |
Log.d(LOG_TAG, "Timeout in TextClassificationAsyncTask"); |
| 1049 |
if (getStatus() == Status.RUNNING) { |
| 1050 |
onPostExecute(mTimeOutResultSupplier.get()); |
| 1051 |
} |
| 1052 |
cancel(true); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
private static final class TextClassificationHelper { |
| 1062 |
|
| 1063 |
|
| 1064 |
private static final int TRIM_DELTA_UPPER_BOUND = 240; |
| 1065 |
|
| 1066 |
private final Context mContext; |
| 1067 |
private Supplier<TextClassifier> mTextClassifier; |
| 1068 |
private final ViewConfiguration mViewConfiguration; |
| 1069 |
|
| 1070 |
|
| 1071 |
private String mText; |
| 1072 |
|
| 1073 |
private int mSelectionStart; |
| 1074 |
|
| 1075 |
private int mSelectionEnd; |
| 1076 |
|
| 1077 |
@Nullable |
| 1078 |
private LocaleList mDefaultLocales; |
| 1079 |
|
| 1080 |
|
| 1081 |
private CharSequence mTrimmedText; |
| 1082 |
|
| 1083 |
private int mTrimStart; |
| 1084 |
|
| 1085 |
private int mRelativeStart; |
| 1086 |
|
| 1087 |
private int mRelativeEnd; |
| 1088 |
|
| 1089 |
|
| 1090 |
private CharSequence mLastClassificationText; |
| 1091 |
private int mLastClassificationSelectionStart; |
| 1092 |
private int mLastClassificationSelectionEnd; |
| 1093 |
private LocaleList mLastClassificationLocales; |
| 1094 |
private SelectionResult mLastClassificationResult; |
| 1095 |
|
| 1096 |
|
| 1097 |
private boolean mInitialized; |
| 1098 |
|
| 1099 |
TextClassificationHelper(Context context, Supplier<TextClassifier> textClassifier, |
| 1100 |
CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) { |
| 1101 |
init(textClassifier, text, selectionStart, selectionEnd, locales); |
| 1102 |
mContext = Objects.requireNonNull(context); |
| 1103 |
mViewConfiguration = ViewConfiguration.get(mContext); |
| 1104 |
} |
| 1105 |
|
| 1106 |
@UiThread |
| 1107 |
public void init(Supplier<TextClassifier> textClassifier, CharSequence text, |
| 1108 |
int selectionStart, int selectionEnd, LocaleList locales) { |
| 1109 |
mTextClassifier = Objects.requireNonNull(textClassifier); |
| 1110 |
mText = Objects.requireNonNull(text).toString(); |
| 1111 |
mLastClassificationText = null; |
| 1112 |
Preconditions.checkArgument(selectionEnd > selectionStart); |
| 1113 |
mSelectionStart = selectionStart; |
| 1114 |
mSelectionEnd = selectionEnd; |
| 1115 |
mDefaultLocales = locales; |
| 1116 |
} |
| 1117 |
|
| 1118 |
@WorkerThread |
| 1119 |
public SelectionResult classifyText() { |
| 1120 |
mInitialized = true; |
| 1121 |
return performClassification(null ); |
| 1122 |
} |
| 1123 |
|
| 1124 |
@WorkerThread |
| 1125 |
public SelectionResult suggestSelection() { |
| 1126 |
mInitialized = true; |
| 1127 |
trimText(); |
| 1128 |
final TextSelection selection; |
| 1129 |
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { |
| 1130 |
final TextSelection.Request request = new TextSelection.Request.Builder( |
| 1131 |
mTrimmedText, mRelativeStart, mRelativeEnd) |
| 1132 |
.setDefaultLocales(mDefaultLocales) |
| 1133 |
.setDarkLaunchAllowed(true) |
| 1134 |
.setIncludeTextClassification(true) |
| 1135 |
.build(); |
| 1136 |
selection = mTextClassifier.get().suggestSelection(request); |
| 1137 |
} else { |
| 1138 |
|
| 1139 |
selection = mTextClassifier.get().suggestSelection( |
| 1140 |
mTrimmedText, mRelativeStart, mRelativeEnd, mDefaultLocales); |
| 1141 |
} |
| 1142 |
|
| 1143 |
if (!isDarkLaunchEnabled()) { |
| 1144 |
mSelectionStart = Math.max(0, selection.getSelectionStartIndex() + mTrimStart); |
| 1145 |
mSelectionEnd = Math.min( |
| 1146 |
mText.length(), selection.getSelectionEndIndex() + mTrimStart); |
| 1147 |
} |
| 1148 |
return performClassification(selection); |
| 1149 |
} |
| 1150 |
|
| 1151 |
public SelectionResult getOriginalSelection() { |
| 1152 |
return new SelectionResult(mSelectionStart, mSelectionEnd, null, null); |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
public int getTimeoutDuration() { |
| 1159 |
if (mInitialized) { |
| 1160 |
return mViewConfiguration.getSmartSelectionInitializedTimeout(); |
| 1161 |
} else { |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
return mViewConfiguration.getSmartSelectionInitializingTimeout(); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
private boolean isDarkLaunchEnabled() { |
| 1171 |
return TextClassificationManager.getSettings(mContext).isModelDarkLaunchEnabled(); |
| 1172 |
} |
| 1173 |
|
| 1174 |
private SelectionResult performClassification(@Nullable TextSelection selection) { |
| 1175 |
if (!Objects.equals(mText, mLastClassificationText) |
| 1176 |
|| mSelectionStart != mLastClassificationSelectionStart |
| 1177 |
|| mSelectionEnd != mLastClassificationSelectionEnd |
| 1178 |
|| !Objects.equals(mDefaultLocales, mLastClassificationLocales)) { |
| 1179 |
|
| 1180 |
mLastClassificationText = mText; |
| 1181 |
mLastClassificationSelectionStart = mSelectionStart; |
| 1182 |
mLastClassificationSelectionEnd = mSelectionEnd; |
| 1183 |
mLastClassificationLocales = mDefaultLocales; |
| 1184 |
|
| 1185 |
trimText(); |
| 1186 |
final TextClassification classification; |
| 1187 |
if (Linkify.containsUnsupportedCharacters(mText)) { |
| 1188 |
|
| 1189 |
android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, ""); |
| 1190 |
classification = TextClassification.EMPTY; |
| 1191 |
} else if (selection != null && selection.getTextClassification() != null) { |
| 1192 |
classification = selection.getTextClassification(); |
| 1193 |
} else if (mContext.getApplicationInfo().targetSdkVersion |
| 1194 |
>= Build.VERSION_CODES.P) { |
| 1195 |
final TextClassification.Request request = |
| 1196 |
new TextClassification.Request.Builder( |
| 1197 |
mTrimmedText, mRelativeStart, mRelativeEnd) |
| 1198 |
.setDefaultLocales(mDefaultLocales) |
| 1199 |
.build(); |
| 1200 |
classification = mTextClassifier.get().classifyText(request); |
| 1201 |
} else { |
| 1202 |
|
| 1203 |
classification = mTextClassifier.get().classifyText( |
| 1204 |
mTrimmedText, mRelativeStart, mRelativeEnd, mDefaultLocales); |
| 1205 |
} |
| 1206 |
mLastClassificationResult = new SelectionResult( |
| 1207 |
mSelectionStart, mSelectionEnd, classification, selection); |
| 1208 |
|
| 1209 |
} |
| 1210 |
return mLastClassificationResult; |
| 1211 |
} |
| 1212 |
|
| 1213 |
private void trimText() { |
| 1214 |
final int trimDelta = Math.min( |
| 1215 |
TextClassificationManager.getSettings(mContext).getSmartSelectionTrimDelta(), |
| 1216 |
TRIM_DELTA_UPPER_BOUND); |
| 1217 |
mTrimStart = Math.max(0, mSelectionStart - trimDelta); |
| 1218 |
final int referenceEnd = Math.min(mText.length(), mSelectionEnd + trimDelta); |
| 1219 |
mTrimmedText = mText.subSequence(mTrimStart, referenceEnd); |
| 1220 |
mRelativeStart = mSelectionStart - mTrimStart; |
| 1221 |
mRelativeEnd = mSelectionEnd - mTrimStart; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
private static final class SelectionResult { |
| 1229 |
private final int mStart; |
| 1230 |
private final int mEnd; |
| 1231 |
@Nullable private final TextClassification mClassification; |
| 1232 |
@Nullable private final TextSelection mSelection; |
| 1233 |
|
| 1234 |
SelectionResult(int start, int end, |
| 1235 |
@Nullable TextClassification classification, @Nullable TextSelection selection) { |
| 1236 |
int[] sortedIndices = sortSelectionIndices(start, end); |
| 1237 |
mStart = sortedIndices[0]; |
| 1238 |
mEnd = sortedIndices[1]; |
| 1239 |
mClassification = classification; |
| 1240 |
mSelection = selection; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
@SelectionEvent.ActionType |
| 1245 |
private static int getActionType(int menuItemId) { |
| 1246 |
switch (menuItemId) { |
| 1247 |
case TextView.ID_SELECT_ALL: |
| 1248 |
return SelectionEvent.ACTION_SELECT_ALL; |
| 1249 |
case TextView.ID_CUT: |
| 1250 |
return SelectionEvent.ACTION_CUT; |
| 1251 |
case TextView.ID_COPY: |
| 1252 |
return SelectionEvent.ACTION_COPY; |
| 1253 |
case TextView.ID_PASTE: |
| 1254 |
case TextView.ID_PASTE_AS_PLAIN_TEXT: |
| 1255 |
return SelectionEvent.ACTION_PASTE; |
| 1256 |
case TextView.ID_SHARE: |
| 1257 |
return SelectionEvent.ACTION_SHARE; |
| 1258 |
case TextView.ID_ASSIST: |
| 1259 |
return SelectionEvent.ACTION_SMART_SHARE; |
| 1260 |
default: |
| 1261 |
return SelectionEvent.ACTION_OTHER; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
private static CharSequence getText(TextView textView) { |
| 1266 |
|
| 1267 |
|
| 1268 |
final CharSequence text = textView.getText(); |
| 1269 |
if (text != null) { |
| 1270 |
return text; |
| 1271 |
} |
| 1272 |
return ""; |
| 1273 |
} |
| 1274 |
} |