Editor.java revision b1b423a46f187c978ba7d7b7572890ea8e75ba56
1/* 2 * Copyright (C) 2012 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 17package android.widget; 18 19import android.R; 20import android.animation.ValueAnimator; 21import android.annotation.IntDef; 22import android.annotation.NonNull; 23import android.annotation.Nullable; 24import android.app.PendingIntent; 25import android.app.PendingIntent.CanceledException; 26import android.app.RemoteAction; 27import android.content.ClipData; 28import android.content.ClipData.Item; 29import android.content.Context; 30import android.content.Intent; 31import android.content.UndoManager; 32import android.content.UndoOperation; 33import android.content.UndoOwner; 34import android.content.pm.PackageManager; 35import android.content.pm.ResolveInfo; 36import android.content.res.TypedArray; 37import android.graphics.Canvas; 38import android.graphics.Color; 39import android.graphics.Matrix; 40import android.graphics.Paint; 41import android.graphics.Path; 42import android.graphics.PointF; 43import android.graphics.Rect; 44import android.graphics.RectF; 45import android.graphics.drawable.ColorDrawable; 46import android.graphics.drawable.Drawable; 47import android.os.Bundle; 48import android.os.LocaleList; 49import android.os.Parcel; 50import android.os.Parcelable; 51import android.os.ParcelableParcel; 52import android.os.SystemClock; 53import android.provider.Settings; 54import android.text.DynamicLayout; 55import android.text.Editable; 56import android.text.InputFilter; 57import android.text.InputType; 58import android.text.Layout; 59import android.text.ParcelableSpan; 60import android.text.Selection; 61import android.text.SpanWatcher; 62import android.text.Spannable; 63import android.text.SpannableStringBuilder; 64import android.text.Spanned; 65import android.text.StaticLayout; 66import android.text.TextUtils; 67import android.text.method.KeyListener; 68import android.text.method.MetaKeyKeyListener; 69import android.text.method.MovementMethod; 70import android.text.method.WordIterator; 71import android.text.style.EasyEditSpan; 72import android.text.style.SuggestionRangeSpan; 73import android.text.style.SuggestionSpan; 74import android.text.style.TextAppearanceSpan; 75import android.text.style.URLSpan; 76import android.util.ArraySet; 77import android.util.DisplayMetrics; 78import android.util.Log; 79import android.util.SparseArray; 80import android.view.ActionMode; 81import android.view.ActionMode.Callback; 82import android.view.ContextMenu; 83import android.view.ContextThemeWrapper; 84import android.view.DisplayListCanvas; 85import android.view.DragAndDropPermissions; 86import android.view.DragEvent; 87import android.view.Gravity; 88import android.view.HapticFeedbackConstants; 89import android.view.InputDevice; 90import android.view.LayoutInflater; 91import android.view.Menu; 92import android.view.MenuItem; 93import android.view.MotionEvent; 94import android.view.RenderNode; 95import android.view.SubMenu; 96import android.view.View; 97import android.view.View.DragShadowBuilder; 98import android.view.View.OnClickListener; 99import android.view.ViewConfiguration; 100import android.view.ViewGroup; 101import android.view.ViewGroup.LayoutParams; 102import android.view.ViewTreeObserver; 103import android.view.WindowManager; 104import android.view.accessibility.AccessibilityNodeInfo; 105import android.view.animation.LinearInterpolator; 106import android.view.inputmethod.CorrectionInfo; 107import android.view.inputmethod.CursorAnchorInfo; 108import android.view.inputmethod.EditorInfo; 109import android.view.inputmethod.ExtractedText; 110import android.view.inputmethod.ExtractedTextRequest; 111import android.view.inputmethod.InputConnection; 112import android.view.inputmethod.InputMethodManager; 113import android.view.textclassifier.TextClassification; 114import android.view.textclassifier.TextClassificationManager; 115import android.widget.AdapterView.OnItemClickListener; 116import android.widget.TextView.Drawables; 117import android.widget.TextView.OnEditorActionListener; 118 119import com.android.internal.annotations.VisibleForTesting; 120import com.android.internal.logging.MetricsLogger; 121import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 122import com.android.internal.util.ArrayUtils; 123import com.android.internal.util.GrowingArrayUtils; 124import com.android.internal.util.Preconditions; 125import com.android.internal.widget.EditableInputConnection; 126 127import java.lang.annotation.Retention; 128import java.lang.annotation.RetentionPolicy; 129import java.text.BreakIterator; 130import java.util.ArrayList; 131import java.util.Arrays; 132import java.util.Comparator; 133import java.util.HashMap; 134import java.util.List; 135import java.util.Map; 136 137/** 138 * Helper class used by TextView to handle editable text views. 139 * 140 * @hide 141 */ 142public class Editor { 143 private static final String TAG = "Editor"; 144 private static final boolean DEBUG_UNDO = false; 145 // Specifies whether to use or not the magnifier when pressing the insertion or selection 146 // handles. 147 private static final boolean FLAG_USE_MAGNIFIER = true; 148 149 static final int BLINK = 500; 150 private static final int DRAG_SHADOW_MAX_TEXT_LENGTH = 20; 151 private static final float LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS = 0.5f; 152 private static final int UNSET_X_VALUE = -1; 153 private static final int UNSET_LINE = -1; 154 // Tag used when the Editor maintains its own separate UndoManager. 155 private static final String UNDO_OWNER_TAG = "Editor"; 156 157 // Ordering constants used to place the Action Mode or context menu items in their menu. 158 private static final int MENU_ITEM_ORDER_ASSIST = 0; 159 private static final int MENU_ITEM_ORDER_UNDO = 2; 160 private static final int MENU_ITEM_ORDER_REDO = 3; 161 private static final int MENU_ITEM_ORDER_CUT = 4; 162 private static final int MENU_ITEM_ORDER_COPY = 5; 163 private static final int MENU_ITEM_ORDER_PASTE = 6; 164 private static final int MENU_ITEM_ORDER_SHARE = 7; 165 private static final int MENU_ITEM_ORDER_SELECT_ALL = 8; 166 private static final int MENU_ITEM_ORDER_REPLACE = 9; 167 private static final int MENU_ITEM_ORDER_AUTOFILL = 10; 168 private static final int MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT = 11; 169 private static final int MENU_ITEM_ORDER_SECONDARY_ASSIST_ACTIONS_START = 50; 170 private static final int MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START = 100; 171 172 @IntDef({MagnifierHandleTrigger.SELECTION_START, 173 MagnifierHandleTrigger.SELECTION_END, 174 MagnifierHandleTrigger.INSERTION}) 175 @Retention(RetentionPolicy.SOURCE) 176 private @interface MagnifierHandleTrigger { 177 int INSERTION = 0; 178 int SELECTION_START = 1; 179 int SELECTION_END = 2; 180 } 181 182 @IntDef({TextActionMode.SELECTION, TextActionMode.INSERTION, TextActionMode.TEXT_LINK}) 183 @interface TextActionMode { 184 int SELECTION = 0; 185 int INSERTION = 1; 186 int TEXT_LINK = 2; 187 } 188 189 // Each Editor manages its own undo stack. 190 private final UndoManager mUndoManager = new UndoManager(); 191 private UndoOwner mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); 192 final UndoInputFilter mUndoInputFilter = new UndoInputFilter(this); 193 boolean mAllowUndo = true; 194 195 private final MetricsLogger mMetricsLogger = new MetricsLogger(); 196 197 // Cursor Controllers. 198 private InsertionPointCursorController mInsertionPointCursorController; 199 SelectionModifierCursorController mSelectionModifierCursorController; 200 // Action mode used when text is selected or when actions on an insertion cursor are triggered. 201 private ActionMode mTextActionMode; 202 private boolean mInsertionControllerEnabled; 203 private boolean mSelectionControllerEnabled; 204 205 private final boolean mHapticTextHandleEnabled; 206 207 private final MagnifierMotionAnimator mMagnifierAnimator; 208 private final Runnable mUpdateMagnifierRunnable = new Runnable() { 209 @Override 210 public void run() { 211 mMagnifierAnimator.update(); 212 } 213 }; 214 // Update the magnifier contents whenever anything in the view hierarchy is updated. 215 // Note: this only captures UI thread-visible changes, so it's a known issue that an animating 216 // VectorDrawable or Ripple animation will not trigger capture, since they're owned by 217 // RenderThread. 218 private final ViewTreeObserver.OnDrawListener mMagnifierOnDrawListener = 219 new ViewTreeObserver.OnDrawListener() { 220 @Override 221 public void onDraw() { 222 if (mMagnifierAnimator != null) { 223 // Posting the method will ensure that updating the magnifier contents will 224 // happen right after the rendering of the current frame. 225 mTextView.post(mUpdateMagnifierRunnable); 226 } 227 } 228 }; 229 230 // Used to highlight a word when it is corrected by the IME 231 private CorrectionHighlighter mCorrectionHighlighter; 232 233 InputContentType mInputContentType; 234 InputMethodState mInputMethodState; 235 236 private static class TextRenderNode { 237 // Render node has 3 recording states: 238 // 1. Recorded operations are valid. 239 // #needsRecord() returns false, but needsToBeShifted is false. 240 // 2. Recorded operations are not valid, but just the position needed to be updated. 241 // #needsRecord() returns false, but needsToBeShifted is true. 242 // 3. Recorded operations are not valid. Need to record operations. #needsRecord() returns 243 // true. 244 RenderNode renderNode; 245 boolean isDirty; 246 // Becomes true when recorded operations can be reused, but the position has to be updated. 247 boolean needsToBeShifted; 248 public TextRenderNode(String name) { 249 renderNode = RenderNode.create(name, null); 250 isDirty = true; 251 needsToBeShifted = true; 252 } 253 boolean needsRecord() { 254 return isDirty || !renderNode.isValid(); 255 } 256 } 257 private TextRenderNode[] mTextRenderNodes; 258 259 boolean mFrozenWithFocus; 260 boolean mSelectionMoved; 261 boolean mTouchFocusSelected; 262 263 KeyListener mKeyListener; 264 int mInputType = EditorInfo.TYPE_NULL; 265 266 boolean mDiscardNextActionUp; 267 boolean mIgnoreActionUpEvent; 268 269 private long mShowCursor; 270 private boolean mRenderCursorRegardlessTiming; 271 private Blink mBlink; 272 273 boolean mCursorVisible = true; 274 boolean mSelectAllOnFocus; 275 boolean mTextIsSelectable; 276 277 CharSequence mError; 278 boolean mErrorWasChanged; 279 private ErrorPopup mErrorPopup; 280 281 /** 282 * This flag is set if the TextView tries to display an error before it 283 * is attached to the window (so its position is still unknown). 284 * It causes the error to be shown later, when onAttachedToWindow() 285 * is called. 286 */ 287 private boolean mShowErrorAfterAttach; 288 289 boolean mInBatchEditControllers; 290 boolean mShowSoftInputOnFocus = true; 291 private boolean mPreserveSelection; 292 private boolean mRestartActionModeOnNextRefresh; 293 private boolean mRequestingLinkActionMode; 294 295 private SelectionActionModeHelper mSelectionActionModeHelper; 296 297 boolean mIsBeingLongClicked; 298 299 private SuggestionsPopupWindow mSuggestionsPopupWindow; 300 SuggestionRangeSpan mSuggestionRangeSpan; 301 private Runnable mShowSuggestionRunnable; 302 303 Drawable mDrawableForCursor = null; 304 305 private Drawable mSelectHandleLeft; 306 private Drawable mSelectHandleRight; 307 private Drawable mSelectHandleCenter; 308 309 // Global listener that detects changes in the global position of the TextView 310 private PositionListener mPositionListener; 311 312 private float mLastDownPositionX, mLastDownPositionY; 313 private float mLastUpPositionX, mLastUpPositionY; 314 private float mContextMenuAnchorX, mContextMenuAnchorY; 315 Callback mCustomSelectionActionModeCallback; 316 Callback mCustomInsertionActionModeCallback; 317 318 // Set when this TextView gained focus with some text selected. Will start selection mode. 319 boolean mCreatedWithASelection; 320 321 // Indicates the current tap state (first tap, double tap, or triple click). 322 private int mTapState = TAP_STATE_INITIAL; 323 private long mLastTouchUpTime = 0; 324 private static final int TAP_STATE_INITIAL = 0; 325 private static final int TAP_STATE_FIRST_TAP = 1; 326 private static final int TAP_STATE_DOUBLE_TAP = 2; 327 // Only for mouse input. 328 private static final int TAP_STATE_TRIPLE_CLICK = 3; 329 330 // The button state as of the last time #onTouchEvent is called. 331 private int mLastButtonState; 332 333 private Runnable mInsertionActionModeRunnable; 334 335 // The span controller helps monitoring the changes to which the Editor needs to react: 336 // - EasyEditSpans, for which we have some UI to display on attach and on hide 337 // - SelectionSpans, for which we need to call updateSelection if an IME is attached 338 private SpanController mSpanController; 339 340 private WordIterator mWordIterator; 341 SpellChecker mSpellChecker; 342 343 // This word iterator is set with text and used to determine word boundaries 344 // when a user is selecting text. 345 private WordIterator mWordIteratorWithText; 346 // Indicate that the text in the word iterator needs to be updated. 347 private boolean mUpdateWordIteratorText; 348 349 private Rect mTempRect; 350 351 private final TextView mTextView; 352 353 final ProcessTextIntentActionsHandler mProcessTextIntentActionsHandler; 354 355 private final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier = 356 new CursorAnchorInfoNotifier(); 357 358 private final Runnable mShowFloatingToolbar = new Runnable() { 359 @Override 360 public void run() { 361 if (mTextActionMode != null) { 362 mTextActionMode.hide(0); // hide off. 363 } 364 } 365 }; 366 367 boolean mIsInsertionActionModeStartPending = false; 368 369 private final SuggestionHelper mSuggestionHelper = new SuggestionHelper(); 370 371 Editor(TextView textView) { 372 mTextView = textView; 373 // Synchronize the filter list, which places the undo input filter at the end. 374 mTextView.setFilters(mTextView.getFilters()); 375 mProcessTextIntentActionsHandler = new ProcessTextIntentActionsHandler(this); 376 mHapticTextHandleEnabled = mTextView.getContext().getResources().getBoolean( 377 com.android.internal.R.bool.config_enableHapticTextHandle); 378 379 if (FLAG_USE_MAGNIFIER) { 380 mMagnifierAnimator = new MagnifierMotionAnimator(new Magnifier(mTextView)); 381 } 382 } 383 384 ParcelableParcel saveInstanceState() { 385 ParcelableParcel state = new ParcelableParcel(getClass().getClassLoader()); 386 Parcel parcel = state.getParcel(); 387 mUndoManager.saveInstanceState(parcel); 388 mUndoInputFilter.saveInstanceState(parcel); 389 return state; 390 } 391 392 void restoreInstanceState(ParcelableParcel state) { 393 Parcel parcel = state.getParcel(); 394 mUndoManager.restoreInstanceState(parcel, state.getClassLoader()); 395 mUndoInputFilter.restoreInstanceState(parcel); 396 // Re-associate this object as the owner of undo state. 397 mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this); 398 } 399 400 /** 401 * Forgets all undo and redo operations for this Editor. 402 */ 403 void forgetUndoRedo() { 404 UndoOwner[] owners = { mUndoOwner }; 405 mUndoManager.forgetUndos(owners, -1 /* all */); 406 mUndoManager.forgetRedos(owners, -1 /* all */); 407 } 408 409 boolean canUndo() { 410 UndoOwner[] owners = { mUndoOwner }; 411 return mAllowUndo && mUndoManager.countUndos(owners) > 0; 412 } 413 414 boolean canRedo() { 415 UndoOwner[] owners = { mUndoOwner }; 416 return mAllowUndo && mUndoManager.countRedos(owners) > 0; 417 } 418 419 void undo() { 420 if (!mAllowUndo) { 421 return; 422 } 423 UndoOwner[] owners = { mUndoOwner }; 424 mUndoManager.undo(owners, 1); // Undo 1 action. 425 } 426 427 void redo() { 428 if (!mAllowUndo) { 429 return; 430 } 431 UndoOwner[] owners = { mUndoOwner }; 432 mUndoManager.redo(owners, 1); // Redo 1 action. 433 } 434 435 void replace() { 436 if (mSuggestionsPopupWindow == null) { 437 mSuggestionsPopupWindow = new SuggestionsPopupWindow(); 438 } 439 hideCursorAndSpanControllers(); 440 mSuggestionsPopupWindow.show(); 441 442 int middle = (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2; 443 Selection.setSelection((Spannable) mTextView.getText(), middle); 444 } 445 446 void onAttachedToWindow() { 447 if (mShowErrorAfterAttach) { 448 showError(); 449 mShowErrorAfterAttach = false; 450 } 451 452 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 453 if (observer.isAlive()) { 454 // No need to create the controller. 455 // The get method will add the listener on controller creation. 456 if (mInsertionPointCursorController != null) { 457 observer.addOnTouchModeChangeListener(mInsertionPointCursorController); 458 } 459 if (mSelectionModifierCursorController != null) { 460 mSelectionModifierCursorController.resetTouchOffsets(); 461 observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); 462 } 463 if (FLAG_USE_MAGNIFIER) { 464 observer.addOnDrawListener(mMagnifierOnDrawListener); 465 } 466 } 467 468 updateSpellCheckSpans(0, mTextView.getText().length(), 469 true /* create the spell checker if needed */); 470 471 if (mTextView.hasSelection()) { 472 refreshTextActionMode(); 473 } 474 475 getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true); 476 resumeBlink(); 477 } 478 479 void onDetachedFromWindow() { 480 getPositionListener().removeSubscriber(mCursorAnchorInfoNotifier); 481 482 if (mError != null) { 483 hideError(); 484 } 485 486 suspendBlink(); 487 488 if (mInsertionPointCursorController != null) { 489 mInsertionPointCursorController.onDetached(); 490 } 491 492 if (mSelectionModifierCursorController != null) { 493 mSelectionModifierCursorController.onDetached(); 494 } 495 496 if (mShowSuggestionRunnable != null) { 497 mTextView.removeCallbacks(mShowSuggestionRunnable); 498 } 499 500 // Cancel the single tap delayed runnable. 501 if (mInsertionActionModeRunnable != null) { 502 mTextView.removeCallbacks(mInsertionActionModeRunnable); 503 } 504 505 mTextView.removeCallbacks(mShowFloatingToolbar); 506 507 discardTextDisplayLists(); 508 509 if (mSpellChecker != null) { 510 mSpellChecker.closeSession(); 511 // Forces the creation of a new SpellChecker next time this window is created. 512 // Will handle the cases where the settings has been changed in the meantime. 513 mSpellChecker = null; 514 } 515 516 if (FLAG_USE_MAGNIFIER) { 517 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 518 if (observer.isAlive()) { 519 observer.removeOnDrawListener(mMagnifierOnDrawListener); 520 } 521 } 522 523 hideCursorAndSpanControllers(); 524 stopTextActionModeWithPreservingSelection(); 525 } 526 527 private void discardTextDisplayLists() { 528 if (mTextRenderNodes != null) { 529 for (int i = 0; i < mTextRenderNodes.length; i++) { 530 RenderNode displayList = mTextRenderNodes[i] != null 531 ? mTextRenderNodes[i].renderNode : null; 532 if (displayList != null && displayList.isValid()) { 533 displayList.discardDisplayList(); 534 } 535 } 536 } 537 } 538 539 private void showError() { 540 if (mTextView.getWindowToken() == null) { 541 mShowErrorAfterAttach = true; 542 return; 543 } 544 545 if (mErrorPopup == null) { 546 LayoutInflater inflater = LayoutInflater.from(mTextView.getContext()); 547 final TextView err = (TextView) inflater.inflate( 548 com.android.internal.R.layout.textview_hint, null); 549 550 final float scale = mTextView.getResources().getDisplayMetrics().density; 551 mErrorPopup = 552 new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f)); 553 mErrorPopup.setFocusable(false); 554 // The user is entering text, so the input method is needed. We 555 // don't want the popup to be displayed on top of it. 556 mErrorPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); 557 } 558 559 TextView tv = (TextView) mErrorPopup.getContentView(); 560 chooseSize(mErrorPopup, mError, tv); 561 tv.setText(mError); 562 563 mErrorPopup.showAsDropDown(mTextView, getErrorX(), getErrorY(), 564 Gravity.TOP | Gravity.LEFT); 565 mErrorPopup.fixDirection(mErrorPopup.isAboveAnchor()); 566 } 567 568 public void setError(CharSequence error, Drawable icon) { 569 mError = TextUtils.stringOrSpannedString(error); 570 mErrorWasChanged = true; 571 572 if (mError == null) { 573 setErrorIcon(null); 574 if (mErrorPopup != null) { 575 if (mErrorPopup.isShowing()) { 576 mErrorPopup.dismiss(); 577 } 578 579 mErrorPopup = null; 580 } 581 mShowErrorAfterAttach = false; 582 } else { 583 setErrorIcon(icon); 584 if (mTextView.isFocused()) { 585 showError(); 586 } 587 } 588 } 589 590 private void setErrorIcon(Drawable icon) { 591 Drawables dr = mTextView.mDrawables; 592 if (dr == null) { 593 mTextView.mDrawables = dr = new Drawables(mTextView.getContext()); 594 } 595 dr.setErrorDrawable(icon, mTextView); 596 597 mTextView.resetResolvedDrawables(); 598 mTextView.invalidate(); 599 mTextView.requestLayout(); 600 } 601 602 private void hideError() { 603 if (mErrorPopup != null) { 604 if (mErrorPopup.isShowing()) { 605 mErrorPopup.dismiss(); 606 } 607 } 608 609 mShowErrorAfterAttach = false; 610 } 611 612 /** 613 * Returns the X offset to make the pointy top of the error point 614 * at the middle of the error icon. 615 */ 616 private int getErrorX() { 617 /* 618 * The "25" is the distance between the point and the right edge 619 * of the background 620 */ 621 final float scale = mTextView.getResources().getDisplayMetrics().density; 622 623 final Drawables dr = mTextView.mDrawables; 624 625 final int layoutDirection = mTextView.getLayoutDirection(); 626 int errorX; 627 int offset; 628 switch (layoutDirection) { 629 default: 630 case View.LAYOUT_DIRECTION_LTR: 631 offset = -(dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f); 632 errorX = mTextView.getWidth() - mErrorPopup.getWidth() 633 - mTextView.getPaddingRight() + offset; 634 break; 635 case View.LAYOUT_DIRECTION_RTL: 636 offset = (dr != null ? dr.mDrawableSizeLeft : 0) / 2 - (int) (25 * scale + 0.5f); 637 errorX = mTextView.getPaddingLeft() + offset; 638 break; 639 } 640 return errorX; 641 } 642 643 /** 644 * Returns the Y offset to make the pointy top of the error point 645 * at the bottom of the error icon. 646 */ 647 private int getErrorY() { 648 /* 649 * Compound, not extended, because the icon is not clipped 650 * if the text height is smaller. 651 */ 652 final int compoundPaddingTop = mTextView.getCompoundPaddingTop(); 653 int vspace = mTextView.getBottom() - mTextView.getTop() 654 - mTextView.getCompoundPaddingBottom() - compoundPaddingTop; 655 656 final Drawables dr = mTextView.mDrawables; 657 658 final int layoutDirection = mTextView.getLayoutDirection(); 659 int height; 660 switch (layoutDirection) { 661 default: 662 case View.LAYOUT_DIRECTION_LTR: 663 height = (dr != null ? dr.mDrawableHeightRight : 0); 664 break; 665 case View.LAYOUT_DIRECTION_RTL: 666 height = (dr != null ? dr.mDrawableHeightLeft : 0); 667 break; 668 } 669 670 int icontop = compoundPaddingTop + (vspace - height) / 2; 671 672 /* 673 * The "2" is the distance between the point and the top edge 674 * of the background. 675 */ 676 final float scale = mTextView.getResources().getDisplayMetrics().density; 677 return icontop + height - mTextView.getHeight() - (int) (2 * scale + 0.5f); 678 } 679 680 void createInputContentTypeIfNeeded() { 681 if (mInputContentType == null) { 682 mInputContentType = new InputContentType(); 683 } 684 } 685 686 void createInputMethodStateIfNeeded() { 687 if (mInputMethodState == null) { 688 mInputMethodState = new InputMethodState(); 689 } 690 } 691 692 private boolean isCursorVisible() { 693 // The default value is true, even when there is no associated Editor 694 return mCursorVisible && mTextView.isTextEditable(); 695 } 696 697 boolean shouldRenderCursor() { 698 if (!isCursorVisible()) { 699 return false; 700 } 701 if (mRenderCursorRegardlessTiming) { 702 return true; 703 } 704 final long showCursorDelta = SystemClock.uptimeMillis() - mShowCursor; 705 return showCursorDelta % (2 * BLINK) < BLINK; 706 } 707 708 void prepareCursorControllers() { 709 boolean windowSupportsHandles = false; 710 711 ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams(); 712 if (params instanceof WindowManager.LayoutParams) { 713 WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params; 714 windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW 715 || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW; 716 } 717 718 boolean enabled = windowSupportsHandles && mTextView.getLayout() != null; 719 mInsertionControllerEnabled = enabled && isCursorVisible(); 720 mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected(); 721 722 if (!mInsertionControllerEnabled) { 723 hideInsertionPointCursorController(); 724 if (mInsertionPointCursorController != null) { 725 mInsertionPointCursorController.onDetached(); 726 mInsertionPointCursorController = null; 727 } 728 } 729 730 if (!mSelectionControllerEnabled) { 731 stopTextActionMode(); 732 if (mSelectionModifierCursorController != null) { 733 mSelectionModifierCursorController.onDetached(); 734 mSelectionModifierCursorController = null; 735 } 736 } 737 } 738 739 void hideInsertionPointCursorController() { 740 if (mInsertionPointCursorController != null) { 741 mInsertionPointCursorController.hide(); 742 } 743 } 744 745 /** 746 * Hides the insertion and span controllers. 747 */ 748 void hideCursorAndSpanControllers() { 749 hideCursorControllers(); 750 hideSpanControllers(); 751 } 752 753 private void hideSpanControllers() { 754 if (mSpanController != null) { 755 mSpanController.hide(); 756 } 757 } 758 759 private void hideCursorControllers() { 760 // When mTextView is not ExtractEditText, we need to distinguish two kinds of focus-lost. 761 // One is the true focus lost where suggestions pop-up (if any) should be dismissed, and the 762 // other is an side effect of showing the suggestions pop-up itself. We use isShowingUp() 763 // to distinguish one from the other. 764 if (mSuggestionsPopupWindow != null && ((mTextView.isInExtractedMode()) 765 || !mSuggestionsPopupWindow.isShowingUp())) { 766 // Should be done before hide insertion point controller since it triggers a show of it 767 mSuggestionsPopupWindow.hide(); 768 } 769 hideInsertionPointCursorController(); 770 } 771 772 /** 773 * Create new SpellCheckSpans on the modified region. 774 */ 775 private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) { 776 // Remove spans whose adjacent characters are text not punctuation 777 mTextView.removeAdjacentSuggestionSpans(start); 778 mTextView.removeAdjacentSuggestionSpans(end); 779 780 if (mTextView.isTextEditable() && mTextView.isSuggestionsEnabled() 781 && !(mTextView.isInExtractedMode())) { 782 if (mSpellChecker == null && createSpellChecker) { 783 mSpellChecker = new SpellChecker(mTextView); 784 } 785 if (mSpellChecker != null) { 786 mSpellChecker.spellCheck(start, end); 787 } 788 } 789 } 790 791 void onScreenStateChanged(int screenState) { 792 switch (screenState) { 793 case View.SCREEN_STATE_ON: 794 resumeBlink(); 795 break; 796 case View.SCREEN_STATE_OFF: 797 suspendBlink(); 798 break; 799 } 800 } 801 802 private void suspendBlink() { 803 if (mBlink != null) { 804 mBlink.cancel(); 805 } 806 } 807 808 private void resumeBlink() { 809 if (mBlink != null) { 810 mBlink.uncancel(); 811 makeBlink(); 812 } 813 } 814 815 void adjustInputType(boolean password, boolean passwordInputType, 816 boolean webPasswordInputType, boolean numberPasswordInputType) { 817 // mInputType has been set from inputType, possibly modified by mInputMethod. 818 // Specialize mInputType to [web]password if we have a text class and the original input 819 // type was a password. 820 if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { 821 if (password || passwordInputType) { 822 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 823 | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD; 824 } 825 if (webPasswordInputType) { 826 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 827 | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD; 828 } 829 } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) { 830 if (numberPasswordInputType) { 831 mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION)) 832 | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD; 833 } 834 } 835 } 836 837 private void chooseSize(@NonNull PopupWindow pop, @NonNull CharSequence text, 838 @NonNull TextView tv) { 839 final int wid = tv.getPaddingLeft() + tv.getPaddingRight(); 840 final int ht = tv.getPaddingTop() + tv.getPaddingBottom(); 841 842 final int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize( 843 com.android.internal.R.dimen.textview_error_popup_default_width); 844 final StaticLayout l = StaticLayout.Builder.obtain(text, 0, text.length(), tv.getPaint(), 845 defaultWidthInPixels) 846 .setUseLineSpacingFromFallbacks(tv.mUseFallbackLineSpacing) 847 .build(); 848 849 float max = 0; 850 for (int i = 0; i < l.getLineCount(); i++) { 851 max = Math.max(max, l.getLineWidth(i)); 852 } 853 854 /* 855 * Now set the popup size to be big enough for the text plus the border capped 856 * to DEFAULT_MAX_POPUP_WIDTH 857 */ 858 pop.setWidth(wid + (int) Math.ceil(max)); 859 pop.setHeight(ht + l.getHeight()); 860 } 861 862 void setFrame() { 863 if (mErrorPopup != null) { 864 TextView tv = (TextView) mErrorPopup.getContentView(); 865 chooseSize(mErrorPopup, mError, tv); 866 mErrorPopup.update(mTextView, getErrorX(), getErrorY(), 867 mErrorPopup.getWidth(), mErrorPopup.getHeight()); 868 } 869 } 870 871 private int getWordStart(int offset) { 872 // FIXME - For this and similar methods we're not doing anything to check if there's 873 // a LocaleSpan in the text, this may be something we should try handling or checking for. 874 int retOffset = getWordIteratorWithText().prevBoundary(offset); 875 if (getWordIteratorWithText().isOnPunctuation(retOffset)) { 876 // On punctuation boundary or within group of punctuation, find punctuation start. 877 retOffset = getWordIteratorWithText().getPunctuationBeginning(offset); 878 } else { 879 // Not on a punctuation boundary, find the word start. 880 retOffset = getWordIteratorWithText().getPrevWordBeginningOnTwoWordsBoundary(offset); 881 } 882 if (retOffset == BreakIterator.DONE) { 883 return offset; 884 } 885 return retOffset; 886 } 887 888 private int getWordEnd(int offset) { 889 int retOffset = getWordIteratorWithText().nextBoundary(offset); 890 if (getWordIteratorWithText().isAfterPunctuation(retOffset)) { 891 // On punctuation boundary or within group of punctuation, find punctuation end. 892 retOffset = getWordIteratorWithText().getPunctuationEnd(offset); 893 } else { 894 // Not on a punctuation boundary, find the word end. 895 retOffset = getWordIteratorWithText().getNextWordEndOnTwoWordBoundary(offset); 896 } 897 if (retOffset == BreakIterator.DONE) { 898 return offset; 899 } 900 return retOffset; 901 } 902 903 private boolean needsToSelectAllToSelectWordOrParagraph() { 904 if (mTextView.hasPasswordTransformationMethod()) { 905 // Always select all on a password field. 906 // Cut/copy menu entries are not available for passwords, but being able to select all 907 // is however useful to delete or paste to replace the entire content. 908 return true; 909 } 910 911 int inputType = mTextView.getInputType(); 912 int klass = inputType & InputType.TYPE_MASK_CLASS; 913 int variation = inputType & InputType.TYPE_MASK_VARIATION; 914 915 // Specific text field types: select the entire text for these 916 if (klass == InputType.TYPE_CLASS_NUMBER 917 || klass == InputType.TYPE_CLASS_PHONE 918 || klass == InputType.TYPE_CLASS_DATETIME 919 || variation == InputType.TYPE_TEXT_VARIATION_URI 920 || variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS 921 || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS 922 || variation == InputType.TYPE_TEXT_VARIATION_FILTER) { 923 return true; 924 } 925 return false; 926 } 927 928 /** 929 * Adjusts selection to the word under last touch offset. Return true if the operation was 930 * successfully performed. 931 */ 932 boolean selectCurrentWord() { 933 if (!mTextView.canSelectText()) { 934 return false; 935 } 936 937 if (needsToSelectAllToSelectWordOrParagraph()) { 938 return mTextView.selectAllText(); 939 } 940 941 long lastTouchOffsets = getLastTouchOffsets(); 942 final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); 943 final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); 944 945 // Safety check in case standard touch event handling has been bypassed 946 if (minOffset < 0 || minOffset > mTextView.getText().length()) return false; 947 if (maxOffset < 0 || maxOffset > mTextView.getText().length()) return false; 948 949 int selectionStart, selectionEnd; 950 951 // If a URLSpan (web address, email, phone...) is found at that position, select it. 952 URLSpan[] urlSpans = 953 ((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class); 954 if (urlSpans.length >= 1) { 955 URLSpan urlSpan = urlSpans[0]; 956 selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan); 957 selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan); 958 } else { 959 // FIXME - We should check if there's a LocaleSpan in the text, this may be 960 // something we should try handling or checking for. 961 final WordIterator wordIterator = getWordIterator(); 962 wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset); 963 964 selectionStart = wordIterator.getBeginning(minOffset); 965 selectionEnd = wordIterator.getEnd(maxOffset); 966 967 if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE 968 || selectionStart == selectionEnd) { 969 // Possible when the word iterator does not properly handle the text's language 970 long range = getCharClusterRange(minOffset); 971 selectionStart = TextUtils.unpackRangeStartFromLong(range); 972 selectionEnd = TextUtils.unpackRangeEndFromLong(range); 973 } 974 } 975 976 Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); 977 return selectionEnd > selectionStart; 978 } 979 980 /** 981 * Adjusts selection to the paragraph under last touch offset. Return true if the operation was 982 * successfully performed. 983 */ 984 private boolean selectCurrentParagraph() { 985 if (!mTextView.canSelectText()) { 986 return false; 987 } 988 989 if (needsToSelectAllToSelectWordOrParagraph()) { 990 return mTextView.selectAllText(); 991 } 992 993 long lastTouchOffsets = getLastTouchOffsets(); 994 final int minLastTouchOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets); 995 final int maxLastTouchOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets); 996 997 final long paragraphsRange = getParagraphsRange(minLastTouchOffset, maxLastTouchOffset); 998 final int start = TextUtils.unpackRangeStartFromLong(paragraphsRange); 999 final int end = TextUtils.unpackRangeEndFromLong(paragraphsRange); 1000 if (start < end) { 1001 Selection.setSelection((Spannable) mTextView.getText(), start, end); 1002 return true; 1003 } 1004 return false; 1005 } 1006 1007 /** 1008 * Get the minimum range of paragraphs that contains startOffset and endOffset. 1009 */ 1010 private long getParagraphsRange(int startOffset, int endOffset) { 1011 final Layout layout = mTextView.getLayout(); 1012 if (layout == null) { 1013 return TextUtils.packRangeInLong(-1, -1); 1014 } 1015 final CharSequence text = mTextView.getText(); 1016 int minLine = layout.getLineForOffset(startOffset); 1017 // Search paragraph start. 1018 while (minLine > 0) { 1019 final int prevLineEndOffset = layout.getLineEnd(minLine - 1); 1020 if (text.charAt(prevLineEndOffset - 1) == '\n') { 1021 break; 1022 } 1023 minLine--; 1024 } 1025 int maxLine = layout.getLineForOffset(endOffset); 1026 // Search paragraph end. 1027 while (maxLine < layout.getLineCount() - 1) { 1028 final int lineEndOffset = layout.getLineEnd(maxLine); 1029 if (text.charAt(lineEndOffset - 1) == '\n') { 1030 break; 1031 } 1032 maxLine++; 1033 } 1034 return TextUtils.packRangeInLong(layout.getLineStart(minLine), layout.getLineEnd(maxLine)); 1035 } 1036 1037 void onLocaleChanged() { 1038 // Will be re-created on demand in getWordIterator and getWordIteratorWithText with the 1039 // proper new locale 1040 mWordIterator = null; 1041 mWordIteratorWithText = null; 1042 } 1043 1044 public WordIterator getWordIterator() { 1045 if (mWordIterator == null) { 1046 mWordIterator = new WordIterator(mTextView.getTextServicesLocale()); 1047 } 1048 return mWordIterator; 1049 } 1050 1051 private WordIterator getWordIteratorWithText() { 1052 if (mWordIteratorWithText == null) { 1053 mWordIteratorWithText = new WordIterator(mTextView.getTextServicesLocale()); 1054 mUpdateWordIteratorText = true; 1055 } 1056 if (mUpdateWordIteratorText) { 1057 // FIXME - Shouldn't copy all of the text as only the area of the text relevant 1058 // to the user's selection is needed. A possible solution would be to 1059 // copy some number N of characters near the selection and then when the 1060 // user approaches N then we'd do another copy of the next N characters. 1061 CharSequence text = mTextView.getText(); 1062 mWordIteratorWithText.setCharSequence(text, 0, text.length()); 1063 mUpdateWordIteratorText = false; 1064 } 1065 return mWordIteratorWithText; 1066 } 1067 1068 private int getNextCursorOffset(int offset, boolean findAfterGivenOffset) { 1069 final Layout layout = mTextView.getLayout(); 1070 if (layout == null) return offset; 1071 return findAfterGivenOffset == layout.isRtlCharAt(offset) 1072 ? layout.getOffsetToLeftOf(offset) : layout.getOffsetToRightOf(offset); 1073 } 1074 1075 private long getCharClusterRange(int offset) { 1076 final int textLength = mTextView.getText().length(); 1077 if (offset < textLength) { 1078 final int clusterEndOffset = getNextCursorOffset(offset, true); 1079 return TextUtils.packRangeInLong( 1080 getNextCursorOffset(clusterEndOffset, false), clusterEndOffset); 1081 } 1082 if (offset - 1 >= 0) { 1083 final int clusterStartOffset = getNextCursorOffset(offset, false); 1084 return TextUtils.packRangeInLong(clusterStartOffset, 1085 getNextCursorOffset(clusterStartOffset, true)); 1086 } 1087 return TextUtils.packRangeInLong(offset, offset); 1088 } 1089 1090 private boolean touchPositionIsInSelection() { 1091 int selectionStart = mTextView.getSelectionStart(); 1092 int selectionEnd = mTextView.getSelectionEnd(); 1093 1094 if (selectionStart == selectionEnd) { 1095 return false; 1096 } 1097 1098 if (selectionStart > selectionEnd) { 1099 int tmp = selectionStart; 1100 selectionStart = selectionEnd; 1101 selectionEnd = tmp; 1102 Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); 1103 } 1104 1105 SelectionModifierCursorController selectionController = getSelectionController(); 1106 int minOffset = selectionController.getMinTouchOffset(); 1107 int maxOffset = selectionController.getMaxTouchOffset(); 1108 1109 return ((minOffset >= selectionStart) && (maxOffset < selectionEnd)); 1110 } 1111 1112 private PositionListener getPositionListener() { 1113 if (mPositionListener == null) { 1114 mPositionListener = new PositionListener(); 1115 } 1116 return mPositionListener; 1117 } 1118 1119 private interface TextViewPositionListener { 1120 public void updatePosition(int parentPositionX, int parentPositionY, 1121 boolean parentPositionChanged, boolean parentScrolled); 1122 } 1123 1124 private boolean isOffsetVisible(int offset) { 1125 Layout layout = mTextView.getLayout(); 1126 if (layout == null) return false; 1127 1128 final int line = layout.getLineForOffset(offset); 1129 final int lineBottom = layout.getLineBottom(line); 1130 final int primaryHorizontal = (int) layout.getPrimaryHorizontal(offset); 1131 return mTextView.isPositionVisible( 1132 primaryHorizontal + mTextView.viewportToContentHorizontalOffset(), 1133 lineBottom + mTextView.viewportToContentVerticalOffset()); 1134 } 1135 1136 /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed 1137 * in the view. Returns false when the position is in the empty space of left/right of text. 1138 */ 1139 private boolean isPositionOnText(float x, float y) { 1140 Layout layout = mTextView.getLayout(); 1141 if (layout == null) return false; 1142 1143 final int line = mTextView.getLineAtCoordinate(y); 1144 x = mTextView.convertToLocalHorizontalCoordinate(x); 1145 1146 if (x < layout.getLineLeft(line)) return false; 1147 if (x > layout.getLineRight(line)) return false; 1148 return true; 1149 } 1150 1151 private void startDragAndDrop() { 1152 getSelectionActionModeHelper().onSelectionDrag(); 1153 1154 // TODO: Fix drag and drop in full screen extracted mode. 1155 if (mTextView.isInExtractedMode()) { 1156 return; 1157 } 1158 final int start = mTextView.getSelectionStart(); 1159 final int end = mTextView.getSelectionEnd(); 1160 CharSequence selectedText = mTextView.getTransformedText(start, end); 1161 ClipData data = ClipData.newPlainText(null, selectedText); 1162 DragLocalState localState = new DragLocalState(mTextView, start, end); 1163 mTextView.startDragAndDrop(data, getTextThumbnailBuilder(start, end), localState, 1164 View.DRAG_FLAG_GLOBAL); 1165 stopTextActionMode(); 1166 if (hasSelectionController()) { 1167 getSelectionController().resetTouchOffsets(); 1168 } 1169 } 1170 1171 public boolean performLongClick(boolean handled) { 1172 // Long press in empty space moves cursor and starts the insertion action mode. 1173 if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) 1174 && mInsertionControllerEnabled) { 1175 final int offset = mTextView.getOffsetForPosition(mLastDownPositionX, 1176 mLastDownPositionY); 1177 Selection.setSelection((Spannable) mTextView.getText(), offset); 1178 getInsertionController().show(); 1179 mIsInsertionActionModeStartPending = true; 1180 handled = true; 1181 MetricsLogger.action( 1182 mTextView.getContext(), 1183 MetricsEvent.TEXT_LONGPRESS, 1184 TextViewMetrics.SUBTYPE_LONG_PRESS_OTHER); 1185 } 1186 1187 if (!handled && mTextActionMode != null) { 1188 if (touchPositionIsInSelection()) { 1189 startDragAndDrop(); 1190 MetricsLogger.action( 1191 mTextView.getContext(), 1192 MetricsEvent.TEXT_LONGPRESS, 1193 TextViewMetrics.SUBTYPE_LONG_PRESS_DRAG_AND_DROP); 1194 } else { 1195 stopTextActionMode(); 1196 selectCurrentWordAndStartDrag(); 1197 MetricsLogger.action( 1198 mTextView.getContext(), 1199 MetricsEvent.TEXT_LONGPRESS, 1200 TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION); 1201 } 1202 handled = true; 1203 } 1204 1205 // Start a new selection 1206 if (!handled) { 1207 handled = selectCurrentWordAndStartDrag(); 1208 if (handled) { 1209 MetricsLogger.action( 1210 mTextView.getContext(), 1211 MetricsEvent.TEXT_LONGPRESS, 1212 TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION); 1213 } 1214 } 1215 1216 return handled; 1217 } 1218 1219 float getLastUpPositionX() { 1220 return mLastUpPositionX; 1221 } 1222 1223 float getLastUpPositionY() { 1224 return mLastUpPositionY; 1225 } 1226 1227 private long getLastTouchOffsets() { 1228 SelectionModifierCursorController selectionController = getSelectionController(); 1229 final int minOffset = selectionController.getMinTouchOffset(); 1230 final int maxOffset = selectionController.getMaxTouchOffset(); 1231 return TextUtils.packRangeInLong(minOffset, maxOffset); 1232 } 1233 1234 void onFocusChanged(boolean focused, int direction) { 1235 mShowCursor = SystemClock.uptimeMillis(); 1236 ensureEndedBatchEdit(); 1237 1238 if (focused) { 1239 int selStart = mTextView.getSelectionStart(); 1240 int selEnd = mTextView.getSelectionEnd(); 1241 1242 // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection 1243 // mode for these, unless there was a specific selection already started. 1244 final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 1245 && selEnd == mTextView.getText().length(); 1246 1247 mCreatedWithASelection = mFrozenWithFocus && mTextView.hasSelection() 1248 && !isFocusHighlighted; 1249 1250 if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) { 1251 // If a tap was used to give focus to that view, move cursor at tap position. 1252 // Has to be done before onTakeFocus, which can be overloaded. 1253 final int lastTapPosition = getLastTapPosition(); 1254 if (lastTapPosition >= 0) { 1255 Selection.setSelection((Spannable) mTextView.getText(), lastTapPosition); 1256 } 1257 1258 // Note this may have to be moved out of the Editor class 1259 MovementMethod mMovement = mTextView.getMovementMethod(); 1260 if (mMovement != null) { 1261 mMovement.onTakeFocus(mTextView, (Spannable) mTextView.getText(), direction); 1262 } 1263 1264 // The DecorView does not have focus when the 'Done' ExtractEditText button is 1265 // pressed. Since it is the ViewAncestor's mView, it requests focus before 1266 // ExtractEditText clears focus, which gives focus to the ExtractEditText. 1267 // This special case ensure that we keep current selection in that case. 1268 // It would be better to know why the DecorView does not have focus at that time. 1269 if (((mTextView.isInExtractedMode()) || mSelectionMoved) 1270 && selStart >= 0 && selEnd >= 0) { 1271 /* 1272 * Someone intentionally set the selection, so let them 1273 * do whatever it is that they wanted to do instead of 1274 * the default on-focus behavior. We reset the selection 1275 * here instead of just skipping the onTakeFocus() call 1276 * because some movement methods do something other than 1277 * just setting the selection in theirs and we still 1278 * need to go through that path. 1279 */ 1280 Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd); 1281 } 1282 1283 if (mSelectAllOnFocus) { 1284 mTextView.selectAllText(); 1285 } 1286 1287 mTouchFocusSelected = true; 1288 } 1289 1290 mFrozenWithFocus = false; 1291 mSelectionMoved = false; 1292 1293 if (mError != null) { 1294 showError(); 1295 } 1296 1297 makeBlink(); 1298 } else { 1299 if (mError != null) { 1300 hideError(); 1301 } 1302 // Don't leave us in the middle of a batch edit. 1303 mTextView.onEndBatchEdit(); 1304 1305 if (mTextView.isInExtractedMode()) { 1306 hideCursorAndSpanControllers(); 1307 stopTextActionModeWithPreservingSelection(); 1308 } else { 1309 hideCursorAndSpanControllers(); 1310 if (mTextView.isTemporarilyDetached()) { 1311 stopTextActionModeWithPreservingSelection(); 1312 } else { 1313 stopTextActionMode(); 1314 } 1315 downgradeEasyCorrectionSpans(); 1316 } 1317 // No need to create the controller 1318 if (mSelectionModifierCursorController != null) { 1319 mSelectionModifierCursorController.resetTouchOffsets(); 1320 } 1321 1322 ensureNoSelectionIfNonSelectable(); 1323 } 1324 } 1325 1326 private void ensureNoSelectionIfNonSelectable() { 1327 // This could be the case if a TextLink has been tapped. 1328 if (!mTextView.textCanBeSelected() && mTextView.hasSelection()) { 1329 Selection.setSelection((Spannable) mTextView.getText(), 1330 mTextView.length(), mTextView.length()); 1331 } 1332 } 1333 1334 /** 1335 * Downgrades to simple suggestions all the easy correction spans that are not a spell check 1336 * span. 1337 */ 1338 private void downgradeEasyCorrectionSpans() { 1339 CharSequence text = mTextView.getText(); 1340 if (text instanceof Spannable) { 1341 Spannable spannable = (Spannable) text; 1342 SuggestionSpan[] suggestionSpans = spannable.getSpans(0, 1343 spannable.length(), SuggestionSpan.class); 1344 for (int i = 0; i < suggestionSpans.length; i++) { 1345 int flags = suggestionSpans[i].getFlags(); 1346 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 1347 && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) { 1348 flags &= ~SuggestionSpan.FLAG_EASY_CORRECT; 1349 suggestionSpans[i].setFlags(flags); 1350 } 1351 } 1352 } 1353 } 1354 1355 void sendOnTextChanged(int start, int before, int after) { 1356 getSelectionActionModeHelper().onTextChanged(start, start + before); 1357 updateSpellCheckSpans(start, start + after, false); 1358 1359 // Flip flag to indicate the word iterator needs to have the text reset. 1360 mUpdateWordIteratorText = true; 1361 1362 // Hide the controllers as soon as text is modified (typing, procedural...) 1363 // We do not hide the span controllers, since they can be added when a new text is 1364 // inserted into the text view (voice IME). 1365 hideCursorControllers(); 1366 // Reset drag accelerator. 1367 if (mSelectionModifierCursorController != null) { 1368 mSelectionModifierCursorController.resetTouchOffsets(); 1369 } 1370 stopTextActionMode(); 1371 } 1372 1373 private int getLastTapPosition() { 1374 // No need to create the controller at that point, no last tap position saved 1375 if (mSelectionModifierCursorController != null) { 1376 int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset(); 1377 if (lastTapPosition >= 0) { 1378 // Safety check, should not be possible. 1379 if (lastTapPosition > mTextView.getText().length()) { 1380 lastTapPosition = mTextView.getText().length(); 1381 } 1382 return lastTapPosition; 1383 } 1384 } 1385 1386 return -1; 1387 } 1388 1389 void onWindowFocusChanged(boolean hasWindowFocus) { 1390 if (hasWindowFocus) { 1391 if (mBlink != null) { 1392 mBlink.uncancel(); 1393 makeBlink(); 1394 } 1395 if (mTextView.hasSelection() && !extractedTextModeWillBeStarted()) { 1396 refreshTextActionMode(); 1397 } 1398 } else { 1399 if (mBlink != null) { 1400 mBlink.cancel(); 1401 } 1402 if (mInputContentType != null) { 1403 mInputContentType.enterDown = false; 1404 } 1405 // Order matters! Must be done before onParentLostFocus to rely on isShowingUp 1406 hideCursorAndSpanControllers(); 1407 stopTextActionModeWithPreservingSelection(); 1408 if (mSuggestionsPopupWindow != null) { 1409 mSuggestionsPopupWindow.onParentLostFocus(); 1410 } 1411 1412 // Don't leave us in the middle of a batch edit. Same as in onFocusChanged 1413 ensureEndedBatchEdit(); 1414 1415 ensureNoSelectionIfNonSelectable(); 1416 } 1417 } 1418 1419 private void updateTapState(MotionEvent event) { 1420 final int action = event.getActionMasked(); 1421 if (action == MotionEvent.ACTION_DOWN) { 1422 final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); 1423 // Detect double tap and triple click. 1424 if (((mTapState == TAP_STATE_FIRST_TAP) 1425 || ((mTapState == TAP_STATE_DOUBLE_TAP) && isMouse)) 1426 && (SystemClock.uptimeMillis() - mLastTouchUpTime) 1427 <= ViewConfiguration.getDoubleTapTimeout()) { 1428 if (mTapState == TAP_STATE_FIRST_TAP) { 1429 mTapState = TAP_STATE_DOUBLE_TAP; 1430 } else { 1431 mTapState = TAP_STATE_TRIPLE_CLICK; 1432 } 1433 } else { 1434 mTapState = TAP_STATE_FIRST_TAP; 1435 } 1436 } 1437 if (action == MotionEvent.ACTION_UP) { 1438 mLastTouchUpTime = SystemClock.uptimeMillis(); 1439 } 1440 } 1441 1442 private boolean shouldFilterOutTouchEvent(MotionEvent event) { 1443 if (!event.isFromSource(InputDevice.SOURCE_MOUSE)) { 1444 return false; 1445 } 1446 final boolean primaryButtonStateChanged = 1447 ((mLastButtonState ^ event.getButtonState()) & MotionEvent.BUTTON_PRIMARY) != 0; 1448 final int action = event.getActionMasked(); 1449 if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) 1450 && !primaryButtonStateChanged) { 1451 return true; 1452 } 1453 if (action == MotionEvent.ACTION_MOVE 1454 && !event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) { 1455 return true; 1456 } 1457 return false; 1458 } 1459 1460 void onTouchEvent(MotionEvent event) { 1461 final boolean filterOutEvent = shouldFilterOutTouchEvent(event); 1462 mLastButtonState = event.getButtonState(); 1463 if (filterOutEvent) { 1464 if (event.getActionMasked() == MotionEvent.ACTION_UP) { 1465 mDiscardNextActionUp = true; 1466 } 1467 return; 1468 } 1469 updateTapState(event); 1470 updateFloatingToolbarVisibility(event); 1471 1472 if (hasSelectionController()) { 1473 getSelectionController().onTouchEvent(event); 1474 } 1475 1476 if (mShowSuggestionRunnable != null) { 1477 mTextView.removeCallbacks(mShowSuggestionRunnable); 1478 mShowSuggestionRunnable = null; 1479 } 1480 1481 if (event.getActionMasked() == MotionEvent.ACTION_UP) { 1482 mLastUpPositionX = event.getX(); 1483 mLastUpPositionY = event.getY(); 1484 } 1485 1486 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { 1487 mLastDownPositionX = event.getX(); 1488 mLastDownPositionY = event.getY(); 1489 1490 // Reset this state; it will be re-set if super.onTouchEvent 1491 // causes focus to move to the view. 1492 mTouchFocusSelected = false; 1493 mIgnoreActionUpEvent = false; 1494 } 1495 } 1496 1497 private void updateFloatingToolbarVisibility(MotionEvent event) { 1498 if (mTextActionMode != null) { 1499 switch (event.getActionMasked()) { 1500 case MotionEvent.ACTION_MOVE: 1501 hideFloatingToolbar(ActionMode.DEFAULT_HIDE_DURATION); 1502 break; 1503 case MotionEvent.ACTION_UP: // fall through 1504 case MotionEvent.ACTION_CANCEL: 1505 showFloatingToolbar(); 1506 } 1507 } 1508 } 1509 1510 void hideFloatingToolbar(int duration) { 1511 if (mTextActionMode != null) { 1512 mTextView.removeCallbacks(mShowFloatingToolbar); 1513 mTextActionMode.hide(duration); 1514 } 1515 } 1516 1517 private void showFloatingToolbar() { 1518 if (mTextActionMode != null) { 1519 // Delay "show" so it doesn't interfere with click confirmations 1520 // or double-clicks that could "dismiss" the floating toolbar. 1521 int delay = ViewConfiguration.getDoubleTapTimeout(); 1522 mTextView.postDelayed(mShowFloatingToolbar, delay); 1523 1524 // This classifies the text and most likely returns before the toolbar is actually 1525 // shown. If not, it will update the toolbar with the result when classification 1526 // returns. We would rather not wait for a long running classification process. 1527 invalidateActionModeAsync(); 1528 } 1529 } 1530 1531 public void beginBatchEdit() { 1532 mInBatchEditControllers = true; 1533 final InputMethodState ims = mInputMethodState; 1534 if (ims != null) { 1535 int nesting = ++ims.mBatchEditNesting; 1536 if (nesting == 1) { 1537 ims.mCursorChanged = false; 1538 ims.mChangedDelta = 0; 1539 if (ims.mContentChanged) { 1540 // We already have a pending change from somewhere else, 1541 // so turn this into a full update. 1542 ims.mChangedStart = 0; 1543 ims.mChangedEnd = mTextView.getText().length(); 1544 } else { 1545 ims.mChangedStart = EXTRACT_UNKNOWN; 1546 ims.mChangedEnd = EXTRACT_UNKNOWN; 1547 ims.mContentChanged = false; 1548 } 1549 mUndoInputFilter.beginBatchEdit(); 1550 mTextView.onBeginBatchEdit(); 1551 } 1552 } 1553 } 1554 1555 public void endBatchEdit() { 1556 mInBatchEditControllers = false; 1557 final InputMethodState ims = mInputMethodState; 1558 if (ims != null) { 1559 int nesting = --ims.mBatchEditNesting; 1560 if (nesting == 0) { 1561 finishBatchEdit(ims); 1562 } 1563 } 1564 } 1565 1566 void ensureEndedBatchEdit() { 1567 final InputMethodState ims = mInputMethodState; 1568 if (ims != null && ims.mBatchEditNesting != 0) { 1569 ims.mBatchEditNesting = 0; 1570 finishBatchEdit(ims); 1571 } 1572 } 1573 1574 void finishBatchEdit(final InputMethodState ims) { 1575 mTextView.onEndBatchEdit(); 1576 mUndoInputFilter.endBatchEdit(); 1577 1578 if (ims.mContentChanged || ims.mSelectionModeChanged) { 1579 mTextView.updateAfterEdit(); 1580 reportExtractedText(); 1581 } else if (ims.mCursorChanged) { 1582 // Cheesy way to get us to report the current cursor location. 1583 mTextView.invalidateCursor(); 1584 } 1585 // sendUpdateSelection knows to avoid sending if the selection did 1586 // not actually change. 1587 sendUpdateSelection(); 1588 1589 // Show drag handles if they were blocked by batch edit mode. 1590 if (mTextActionMode != null) { 1591 final CursorController cursorController = mTextView.hasSelection() 1592 ? getSelectionController() : getInsertionController(); 1593 if (cursorController != null && !cursorController.isActive() 1594 && !cursorController.isCursorBeingModified()) { 1595 cursorController.show(); 1596 } 1597 } 1598 } 1599 1600 static final int EXTRACT_NOTHING = -2; 1601 static final int EXTRACT_UNKNOWN = -1; 1602 1603 boolean extractText(ExtractedTextRequest request, ExtractedText outText) { 1604 return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN, 1605 EXTRACT_UNKNOWN, outText); 1606 } 1607 1608 private boolean extractTextInternal(@Nullable ExtractedTextRequest request, 1609 int partialStartOffset, int partialEndOffset, int delta, 1610 @Nullable ExtractedText outText) { 1611 if (request == null || outText == null) { 1612 return false; 1613 } 1614 1615 final CharSequence content = mTextView.getText(); 1616 if (content == null) { 1617 return false; 1618 } 1619 1620 if (partialStartOffset != EXTRACT_NOTHING) { 1621 final int N = content.length(); 1622 if (partialStartOffset < 0) { 1623 outText.partialStartOffset = outText.partialEndOffset = -1; 1624 partialStartOffset = 0; 1625 partialEndOffset = N; 1626 } else { 1627 // Now use the delta to determine the actual amount of text 1628 // we need. 1629 partialEndOffset += delta; 1630 // Adjust offsets to ensure we contain full spans. 1631 if (content instanceof Spanned) { 1632 Spanned spanned = (Spanned) content; 1633 Object[] spans = spanned.getSpans(partialStartOffset, 1634 partialEndOffset, ParcelableSpan.class); 1635 int i = spans.length; 1636 while (i > 0) { 1637 i--; 1638 int j = spanned.getSpanStart(spans[i]); 1639 if (j < partialStartOffset) partialStartOffset = j; 1640 j = spanned.getSpanEnd(spans[i]); 1641 if (j > partialEndOffset) partialEndOffset = j; 1642 } 1643 } 1644 outText.partialStartOffset = partialStartOffset; 1645 outText.partialEndOffset = partialEndOffset - delta; 1646 1647 if (partialStartOffset > N) { 1648 partialStartOffset = N; 1649 } else if (partialStartOffset < 0) { 1650 partialStartOffset = 0; 1651 } 1652 if (partialEndOffset > N) { 1653 partialEndOffset = N; 1654 } else if (partialEndOffset < 0) { 1655 partialEndOffset = 0; 1656 } 1657 } 1658 if ((request.flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) { 1659 outText.text = content.subSequence(partialStartOffset, 1660 partialEndOffset); 1661 } else { 1662 outText.text = TextUtils.substring(content, partialStartOffset, 1663 partialEndOffset); 1664 } 1665 } else { 1666 outText.partialStartOffset = 0; 1667 outText.partialEndOffset = 0; 1668 outText.text = ""; 1669 } 1670 outText.flags = 0; 1671 if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) { 1672 outText.flags |= ExtractedText.FLAG_SELECTING; 1673 } 1674 if (mTextView.isSingleLine()) { 1675 outText.flags |= ExtractedText.FLAG_SINGLE_LINE; 1676 } 1677 outText.startOffset = 0; 1678 outText.selectionStart = mTextView.getSelectionStart(); 1679 outText.selectionEnd = mTextView.getSelectionEnd(); 1680 outText.hint = mTextView.getHint(); 1681 return true; 1682 } 1683 1684 boolean reportExtractedText() { 1685 final Editor.InputMethodState ims = mInputMethodState; 1686 if (ims == null) { 1687 return false; 1688 } 1689 ims.mSelectionModeChanged = false; 1690 final ExtractedTextRequest req = ims.mExtractedTextRequest; 1691 if (req == null) { 1692 return false; 1693 } 1694 final InputMethodManager imm = InputMethodManager.peekInstance(); 1695 if (imm == null) { 1696 return false; 1697 } 1698 if (TextView.DEBUG_EXTRACT) { 1699 Log.v(TextView.LOG_TAG, "Retrieving extracted start=" 1700 + ims.mChangedStart 1701 + " end=" + ims.mChangedEnd 1702 + " delta=" + ims.mChangedDelta); 1703 } 1704 if (ims.mChangedStart < 0 && !ims.mContentChanged) { 1705 ims.mChangedStart = EXTRACT_NOTHING; 1706 } 1707 if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd, 1708 ims.mChangedDelta, ims.mExtractedText)) { 1709 if (TextView.DEBUG_EXTRACT) { 1710 Log.v(TextView.LOG_TAG, 1711 "Reporting extracted start=" 1712 + ims.mExtractedText.partialStartOffset 1713 + " end=" + ims.mExtractedText.partialEndOffset 1714 + ": " + ims.mExtractedText.text); 1715 } 1716 1717 imm.updateExtractedText(mTextView, req.token, ims.mExtractedText); 1718 ims.mChangedStart = EXTRACT_UNKNOWN; 1719 ims.mChangedEnd = EXTRACT_UNKNOWN; 1720 ims.mChangedDelta = 0; 1721 ims.mContentChanged = false; 1722 return true; 1723 } 1724 return false; 1725 } 1726 1727 private void sendUpdateSelection() { 1728 if (null != mInputMethodState && mInputMethodState.mBatchEditNesting <= 0) { 1729 final InputMethodManager imm = InputMethodManager.peekInstance(); 1730 if (null != imm) { 1731 final int selectionStart = mTextView.getSelectionStart(); 1732 final int selectionEnd = mTextView.getSelectionEnd(); 1733 int candStart = -1; 1734 int candEnd = -1; 1735 if (mTextView.getText() instanceof Spannable) { 1736 final Spannable sp = (Spannable) mTextView.getText(); 1737 candStart = EditableInputConnection.getComposingSpanStart(sp); 1738 candEnd = EditableInputConnection.getComposingSpanEnd(sp); 1739 } 1740 // InputMethodManager#updateSelection skips sending the message if 1741 // none of the parameters have changed since the last time we called it. 1742 imm.updateSelection(mTextView, 1743 selectionStart, selectionEnd, candStart, candEnd); 1744 } 1745 } 1746 } 1747 1748 void onDraw(Canvas canvas, Layout layout, Path highlight, Paint highlightPaint, 1749 int cursorOffsetVertical) { 1750 final int selectionStart = mTextView.getSelectionStart(); 1751 final int selectionEnd = mTextView.getSelectionEnd(); 1752 1753 final InputMethodState ims = mInputMethodState; 1754 if (ims != null && ims.mBatchEditNesting == 0) { 1755 InputMethodManager imm = InputMethodManager.peekInstance(); 1756 if (imm != null) { 1757 if (imm.isActive(mTextView)) { 1758 if (ims.mContentChanged || ims.mSelectionModeChanged) { 1759 // We are in extract mode and the content has changed 1760 // in some way... just report complete new text to the 1761 // input method. 1762 reportExtractedText(); 1763 } 1764 } 1765 } 1766 } 1767 1768 if (mCorrectionHighlighter != null) { 1769 mCorrectionHighlighter.draw(canvas, cursorOffsetVertical); 1770 } 1771 1772 if (highlight != null && selectionStart == selectionEnd && mDrawableForCursor != null) { 1773 drawCursor(canvas, cursorOffsetVertical); 1774 // Rely on the drawable entirely, do not draw the cursor line. 1775 // Has to be done after the IMM related code above which relies on the highlight. 1776 highlight = null; 1777 } 1778 1779 if (mSelectionActionModeHelper != null) { 1780 mSelectionActionModeHelper.onDraw(canvas); 1781 if (mSelectionActionModeHelper.isDrawingHighlight()) { 1782 highlight = null; 1783 } 1784 } 1785 1786 if (mTextView.canHaveDisplayList() && canvas.isHardwareAccelerated()) { 1787 drawHardwareAccelerated(canvas, layout, highlight, highlightPaint, 1788 cursorOffsetVertical); 1789 } else { 1790 layout.draw(canvas, highlight, highlightPaint, cursorOffsetVertical); 1791 } 1792 } 1793 1794 private void drawHardwareAccelerated(Canvas canvas, Layout layout, Path highlight, 1795 Paint highlightPaint, int cursorOffsetVertical) { 1796 final long lineRange = layout.getLineRangeForDraw(canvas); 1797 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange); 1798 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange); 1799 if (lastLine < 0) return; 1800 1801 layout.drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical, 1802 firstLine, lastLine); 1803 1804 if (layout instanceof DynamicLayout) { 1805 if (mTextRenderNodes == null) { 1806 mTextRenderNodes = ArrayUtils.emptyArray(TextRenderNode.class); 1807 } 1808 1809 DynamicLayout dynamicLayout = (DynamicLayout) layout; 1810 int[] blockEndLines = dynamicLayout.getBlockEndLines(); 1811 int[] blockIndices = dynamicLayout.getBlockIndices(); 1812 final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); 1813 final int indexFirstChangedBlock = dynamicLayout.getIndexFirstChangedBlock(); 1814 1815 final ArraySet<Integer> blockSet = dynamicLayout.getBlocksAlwaysNeedToBeRedrawn(); 1816 if (blockSet != null) { 1817 for (int i = 0; i < blockSet.size(); i++) { 1818 final int blockIndex = dynamicLayout.getBlockIndex(blockSet.valueAt(i)); 1819 if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX 1820 && mTextRenderNodes[blockIndex] != null) { 1821 mTextRenderNodes[blockIndex].needsToBeShifted = true; 1822 } 1823 } 1824 } 1825 1826 int startBlock = Arrays.binarySearch(blockEndLines, 0, numberOfBlocks, firstLine); 1827 if (startBlock < 0) { 1828 startBlock = -(startBlock + 1); 1829 } 1830 startBlock = Math.min(indexFirstChangedBlock, startBlock); 1831 1832 int startIndexToFindAvailableRenderNode = 0; 1833 int lastIndex = numberOfBlocks; 1834 1835 for (int i = startBlock; i < numberOfBlocks; i++) { 1836 final int blockIndex = blockIndices[i]; 1837 if (i >= indexFirstChangedBlock 1838 && blockIndex != DynamicLayout.INVALID_BLOCK_INDEX 1839 && mTextRenderNodes[blockIndex] != null) { 1840 mTextRenderNodes[blockIndex].needsToBeShifted = true; 1841 } 1842 if (blockEndLines[i] < firstLine) { 1843 // Blocks in [indexFirstChangedBlock, firstLine) are not redrawn here. They will 1844 // be redrawn after they get scrolled into drawing range. 1845 continue; 1846 } 1847 startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas, layout, 1848 highlight, highlightPaint, cursorOffsetVertical, blockEndLines, 1849 blockIndices, i, numberOfBlocks, startIndexToFindAvailableRenderNode); 1850 if (blockEndLines[i] >= lastLine) { 1851 lastIndex = Math.max(indexFirstChangedBlock, i + 1); 1852 break; 1853 } 1854 } 1855 if (blockSet != null) { 1856 for (int i = 0; i < blockSet.size(); i++) { 1857 final int block = blockSet.valueAt(i); 1858 final int blockIndex = dynamicLayout.getBlockIndex(block); 1859 if (blockIndex == DynamicLayout.INVALID_BLOCK_INDEX 1860 || mTextRenderNodes[blockIndex] == null 1861 || mTextRenderNodes[blockIndex].needsToBeShifted) { 1862 startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas, 1863 layout, highlight, highlightPaint, cursorOffsetVertical, 1864 blockEndLines, blockIndices, block, numberOfBlocks, 1865 startIndexToFindAvailableRenderNode); 1866 } 1867 } 1868 } 1869 1870 dynamicLayout.setIndexFirstChangedBlock(lastIndex); 1871 } else { 1872 // Boring layout is used for empty and hint text 1873 layout.drawText(canvas, firstLine, lastLine); 1874 } 1875 } 1876 1877 private int drawHardwareAcceleratedInner(Canvas canvas, Layout layout, Path highlight, 1878 Paint highlightPaint, int cursorOffsetVertical, int[] blockEndLines, 1879 int[] blockIndices, int blockInfoIndex, int numberOfBlocks, 1880 int startIndexToFindAvailableRenderNode) { 1881 final int blockEndLine = blockEndLines[blockInfoIndex]; 1882 int blockIndex = blockIndices[blockInfoIndex]; 1883 1884 final boolean blockIsInvalid = blockIndex == DynamicLayout.INVALID_BLOCK_INDEX; 1885 if (blockIsInvalid) { 1886 blockIndex = getAvailableDisplayListIndex(blockIndices, numberOfBlocks, 1887 startIndexToFindAvailableRenderNode); 1888 // Note how dynamic layout's internal block indices get updated from Editor 1889 blockIndices[blockInfoIndex] = blockIndex; 1890 if (mTextRenderNodes[blockIndex] != null) { 1891 mTextRenderNodes[blockIndex].isDirty = true; 1892 } 1893 startIndexToFindAvailableRenderNode = blockIndex + 1; 1894 } 1895 1896 if (mTextRenderNodes[blockIndex] == null) { 1897 mTextRenderNodes[blockIndex] = new TextRenderNode("Text " + blockIndex); 1898 } 1899 1900 final boolean blockDisplayListIsInvalid = mTextRenderNodes[blockIndex].needsRecord(); 1901 RenderNode blockDisplayList = mTextRenderNodes[blockIndex].renderNode; 1902 if (mTextRenderNodes[blockIndex].needsToBeShifted || blockDisplayListIsInvalid) { 1903 final int blockBeginLine = blockInfoIndex == 0 ? 1904 0 : blockEndLines[blockInfoIndex - 1] + 1; 1905 final int top = layout.getLineTop(blockBeginLine); 1906 final int bottom = layout.getLineBottom(blockEndLine); 1907 int left = 0; 1908 int right = mTextView.getWidth(); 1909 if (mTextView.getHorizontallyScrolling()) { 1910 float min = Float.MAX_VALUE; 1911 float max = Float.MIN_VALUE; 1912 for (int line = blockBeginLine; line <= blockEndLine; line++) { 1913 min = Math.min(min, layout.getLineLeft(line)); 1914 max = Math.max(max, layout.getLineRight(line)); 1915 } 1916 left = (int) min; 1917 right = (int) (max + 0.5f); 1918 } 1919 1920 // Rebuild display list if it is invalid 1921 if (blockDisplayListIsInvalid) { 1922 final DisplayListCanvas displayListCanvas = blockDisplayList.start( 1923 right - left, bottom - top); 1924 try { 1925 // drawText is always relative to TextView's origin, this translation 1926 // brings this range of text back to the top left corner of the viewport 1927 displayListCanvas.translate(-left, -top); 1928 layout.drawText(displayListCanvas, blockBeginLine, blockEndLine); 1929 mTextRenderNodes[blockIndex].isDirty = false; 1930 // No need to untranslate, previous context is popped after 1931 // drawDisplayList 1932 } finally { 1933 blockDisplayList.end(displayListCanvas); 1934 // Same as drawDisplayList below, handled by our TextView's parent 1935 blockDisplayList.setClipToBounds(false); 1936 } 1937 } 1938 1939 // Valid display list only needs to update its drawing location. 1940 blockDisplayList.setLeftTopRightBottom(left, top, right, bottom); 1941 mTextRenderNodes[blockIndex].needsToBeShifted = false; 1942 } 1943 ((DisplayListCanvas) canvas).drawRenderNode(blockDisplayList); 1944 return startIndexToFindAvailableRenderNode; 1945 } 1946 1947 private int getAvailableDisplayListIndex(int[] blockIndices, int numberOfBlocks, 1948 int searchStartIndex) { 1949 int length = mTextRenderNodes.length; 1950 for (int i = searchStartIndex; i < length; i++) { 1951 boolean blockIndexFound = false; 1952 for (int j = 0; j < numberOfBlocks; j++) { 1953 if (blockIndices[j] == i) { 1954 blockIndexFound = true; 1955 break; 1956 } 1957 } 1958 if (blockIndexFound) continue; 1959 return i; 1960 } 1961 1962 // No available index found, the pool has to grow 1963 mTextRenderNodes = GrowingArrayUtils.append(mTextRenderNodes, length, null); 1964 return length; 1965 } 1966 1967 private void drawCursor(Canvas canvas, int cursorOffsetVertical) { 1968 final boolean translate = cursorOffsetVertical != 0; 1969 if (translate) canvas.translate(0, cursorOffsetVertical); 1970 if (mDrawableForCursor != null) { 1971 mDrawableForCursor.draw(canvas); 1972 } 1973 if (translate) canvas.translate(0, -cursorOffsetVertical); 1974 } 1975 1976 void invalidateHandlesAndActionMode() { 1977 if (mSelectionModifierCursorController != null) { 1978 mSelectionModifierCursorController.invalidateHandles(); 1979 } 1980 if (mInsertionPointCursorController != null) { 1981 mInsertionPointCursorController.invalidateHandle(); 1982 } 1983 if (mTextActionMode != null) { 1984 invalidateActionMode(); 1985 } 1986 } 1987 1988 /** 1989 * Invalidates all the sub-display lists that overlap the specified character range 1990 */ 1991 void invalidateTextDisplayList(Layout layout, int start, int end) { 1992 if (mTextRenderNodes != null && layout instanceof DynamicLayout) { 1993 final int firstLine = layout.getLineForOffset(start); 1994 final int lastLine = layout.getLineForOffset(end); 1995 1996 DynamicLayout dynamicLayout = (DynamicLayout) layout; 1997 int[] blockEndLines = dynamicLayout.getBlockEndLines(); 1998 int[] blockIndices = dynamicLayout.getBlockIndices(); 1999 final int numberOfBlocks = dynamicLayout.getNumberOfBlocks(); 2000 2001 int i = 0; 2002 // Skip the blocks before firstLine 2003 while (i < numberOfBlocks) { 2004 if (blockEndLines[i] >= firstLine) break; 2005 i++; 2006 } 2007 2008 // Invalidate all subsequent blocks until lastLine is passed 2009 while (i < numberOfBlocks) { 2010 final int blockIndex = blockIndices[i]; 2011 if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX) { 2012 mTextRenderNodes[blockIndex].isDirty = true; 2013 } 2014 if (blockEndLines[i] >= lastLine) break; 2015 i++; 2016 } 2017 } 2018 } 2019 2020 void invalidateTextDisplayList() { 2021 if (mTextRenderNodes != null) { 2022 for (int i = 0; i < mTextRenderNodes.length; i++) { 2023 if (mTextRenderNodes[i] != null) mTextRenderNodes[i].isDirty = true; 2024 } 2025 } 2026 } 2027 2028 void updateCursorPosition() { 2029 if (mTextView.mCursorDrawableRes == 0) { 2030 mDrawableForCursor = null; 2031 return; 2032 } 2033 2034 final Layout layout = mTextView.getLayout(); 2035 final int offset = mTextView.getSelectionStart(); 2036 final int line = layout.getLineForOffset(offset); 2037 final int top = layout.getLineTop(line); 2038 final int bottom = layout.getLineBottomWithoutSpacing(line); 2039 2040 final boolean clamped = layout.shouldClampCursor(line); 2041 updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(offset, clamped)); 2042 } 2043 2044 void refreshTextActionMode() { 2045 if (extractedTextModeWillBeStarted()) { 2046 mRestartActionModeOnNextRefresh = false; 2047 return; 2048 } 2049 final boolean hasSelection = mTextView.hasSelection(); 2050 final SelectionModifierCursorController selectionController = getSelectionController(); 2051 final InsertionPointCursorController insertionController = getInsertionController(); 2052 if ((selectionController != null && selectionController.isCursorBeingModified()) 2053 || (insertionController != null && insertionController.isCursorBeingModified())) { 2054 // ActionMode should be managed by the currently active cursor controller. 2055 mRestartActionModeOnNextRefresh = false; 2056 return; 2057 } 2058 if (hasSelection) { 2059 hideInsertionPointCursorController(); 2060 if (mTextActionMode == null) { 2061 if (mRestartActionModeOnNextRefresh) { 2062 // To avoid distraction, newly start action mode only when selection action 2063 // mode is being restarted. 2064 startSelectionActionModeAsync(false); 2065 } 2066 } else if (selectionController == null || !selectionController.isActive()) { 2067 // Insertion action mode is active. Avoid dismissing the selection. 2068 stopTextActionModeWithPreservingSelection(); 2069 startSelectionActionModeAsync(false); 2070 } else { 2071 mTextActionMode.invalidateContentRect(); 2072 } 2073 } else { 2074 // Insertion action mode is started only when insertion controller is explicitly 2075 // activated. 2076 if (insertionController == null || !insertionController.isActive()) { 2077 stopTextActionMode(); 2078 } else if (mTextActionMode != null) { 2079 mTextActionMode.invalidateContentRect(); 2080 } 2081 } 2082 mRestartActionModeOnNextRefresh = false; 2083 } 2084 2085 /** 2086 * Start an Insertion action mode. 2087 */ 2088 void startInsertionActionMode() { 2089 if (mInsertionActionModeRunnable != null) { 2090 mTextView.removeCallbacks(mInsertionActionModeRunnable); 2091 } 2092 if (extractedTextModeWillBeStarted()) { 2093 return; 2094 } 2095 stopTextActionMode(); 2096 2097 ActionMode.Callback actionModeCallback = 2098 new TextActionModeCallback(TextActionMode.INSERTION); 2099 mTextActionMode = mTextView.startActionMode( 2100 actionModeCallback, ActionMode.TYPE_FLOATING); 2101 if (mTextActionMode != null && getInsertionController() != null) { 2102 getInsertionController().show(); 2103 } 2104 } 2105 2106 @NonNull 2107 TextView getTextView() { 2108 return mTextView; 2109 } 2110 2111 @Nullable 2112 ActionMode getTextActionMode() { 2113 return mTextActionMode; 2114 } 2115 2116 void setRestartActionModeOnNextRefresh(boolean value) { 2117 mRestartActionModeOnNextRefresh = value; 2118 } 2119 2120 /** 2121 * Asynchronously starts a selection action mode using the TextClassifier. 2122 */ 2123 void startSelectionActionModeAsync(boolean adjustSelection) { 2124 getSelectionActionModeHelper().startSelectionActionModeAsync(adjustSelection); 2125 } 2126 2127 void startLinkActionModeAsync(int start, int end) { 2128 if (!(mTextView.getText() instanceof Spannable)) { 2129 return; 2130 } 2131 stopTextActionMode(); 2132 mRequestingLinkActionMode = true; 2133 getSelectionActionModeHelper().startLinkActionModeAsync(start, end); 2134 } 2135 2136 /** 2137 * Asynchronously invalidates an action mode using the TextClassifier. 2138 */ 2139 void invalidateActionModeAsync() { 2140 getSelectionActionModeHelper().invalidateActionModeAsync(); 2141 } 2142 2143 /** 2144 * Synchronously invalidates an action mode without the TextClassifier. 2145 */ 2146 private void invalidateActionMode() { 2147 if (mTextActionMode != null) { 2148 mTextActionMode.invalidate(); 2149 } 2150 } 2151 2152 private SelectionActionModeHelper getSelectionActionModeHelper() { 2153 if (mSelectionActionModeHelper == null) { 2154 mSelectionActionModeHelper = new SelectionActionModeHelper(this); 2155 } 2156 return mSelectionActionModeHelper; 2157 } 2158 2159 /** 2160 * If the TextView allows text selection, selects the current word when no existing selection 2161 * was available and starts a drag. 2162 * 2163 * @return true if the drag was started. 2164 */ 2165 private boolean selectCurrentWordAndStartDrag() { 2166 if (mInsertionActionModeRunnable != null) { 2167 mTextView.removeCallbacks(mInsertionActionModeRunnable); 2168 } 2169 if (extractedTextModeWillBeStarted()) { 2170 return false; 2171 } 2172 if (!checkField()) { 2173 return false; 2174 } 2175 if (!mTextView.hasSelection() && !selectCurrentWord()) { 2176 // No selection and cannot select a word. 2177 return false; 2178 } 2179 stopTextActionModeWithPreservingSelection(); 2180 getSelectionController().enterDrag( 2181 SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_WORD); 2182 return true; 2183 } 2184 2185 /** 2186 * Checks whether a selection can be performed on the current TextView. 2187 * 2188 * @return true if a selection can be performed 2189 */ 2190 boolean checkField() { 2191 if (!mTextView.canSelectText() || !mTextView.requestFocus()) { 2192 Log.w(TextView.LOG_TAG, 2193 "TextView does not support text selection. Selection cancelled."); 2194 return false; 2195 } 2196 return true; 2197 } 2198 2199 boolean startActionModeInternal(@TextActionMode int actionMode) { 2200 if (extractedTextModeWillBeStarted()) { 2201 return false; 2202 } 2203 if (mTextActionMode != null) { 2204 // Text action mode is already started 2205 invalidateActionMode(); 2206 return false; 2207 } 2208 2209 if (actionMode != TextActionMode.TEXT_LINK 2210 && (!checkField() || !mTextView.hasSelection())) { 2211 return false; 2212 } 2213 2214 ActionMode.Callback actionModeCallback = new TextActionModeCallback(actionMode); 2215 mTextActionMode = mTextView.startActionMode(actionModeCallback, ActionMode.TYPE_FLOATING); 2216 2217 final boolean selectionStarted = mTextActionMode != null; 2218 if (selectionStarted 2219 && mTextView.isTextEditable() && !mTextView.isTextSelectable() 2220 && mShowSoftInputOnFocus) { 2221 // Show the IME to be able to replace text, except when selecting non editable text. 2222 final InputMethodManager imm = InputMethodManager.peekInstance(); 2223 if (imm != null) { 2224 imm.showSoftInput(mTextView, 0, null); 2225 } 2226 } 2227 return selectionStarted; 2228 } 2229 2230 private boolean extractedTextModeWillBeStarted() { 2231 if (!(mTextView.isInExtractedMode())) { 2232 final InputMethodManager imm = InputMethodManager.peekInstance(); 2233 return imm != null && imm.isFullscreenMode(); 2234 } 2235 return false; 2236 } 2237 2238 /** 2239 * @return <code>true</code> if it's reasonable to offer to show suggestions depending on 2240 * the current cursor position or selection range. This method is consistent with the 2241 * method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}. 2242 */ 2243 private boolean shouldOfferToShowSuggestions() { 2244 CharSequence text = mTextView.getText(); 2245 if (!(text instanceof Spannable)) return false; 2246 2247 final Spannable spannable = (Spannable) text; 2248 final int selectionStart = mTextView.getSelectionStart(); 2249 final int selectionEnd = mTextView.getSelectionEnd(); 2250 final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd, 2251 SuggestionSpan.class); 2252 if (suggestionSpans.length == 0) { 2253 return false; 2254 } 2255 if (selectionStart == selectionEnd) { 2256 // Spans overlap the cursor. 2257 for (int i = 0; i < suggestionSpans.length; i++) { 2258 if (suggestionSpans[i].getSuggestions().length > 0) { 2259 return true; 2260 } 2261 } 2262 return false; 2263 } 2264 int minSpanStart = mTextView.getText().length(); 2265 int maxSpanEnd = 0; 2266 int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length(); 2267 int unionOfSpansCoveringSelectionStartEnd = 0; 2268 boolean hasValidSuggestions = false; 2269 for (int i = 0; i < suggestionSpans.length; i++) { 2270 final int spanStart = spannable.getSpanStart(suggestionSpans[i]); 2271 final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]); 2272 minSpanStart = Math.min(minSpanStart, spanStart); 2273 maxSpanEnd = Math.max(maxSpanEnd, spanEnd); 2274 if (selectionStart < spanStart || selectionStart > spanEnd) { 2275 // The span doesn't cover the current selection start point. 2276 continue; 2277 } 2278 hasValidSuggestions = 2279 hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0; 2280 unionOfSpansCoveringSelectionStartStart = 2281 Math.min(unionOfSpansCoveringSelectionStartStart, spanStart); 2282 unionOfSpansCoveringSelectionStartEnd = 2283 Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd); 2284 } 2285 if (!hasValidSuggestions) { 2286 return false; 2287 } 2288 if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) { 2289 // No spans cover the selection start point. 2290 return false; 2291 } 2292 if (minSpanStart < unionOfSpansCoveringSelectionStartStart 2293 || maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) { 2294 // There is a span that is not covered by the union. In this case, we soouldn't offer 2295 // to show suggestions as it's confusing. 2296 return false; 2297 } 2298 return true; 2299 } 2300 2301 /** 2302 * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with 2303 * {@link SuggestionSpan#FLAG_EASY_CORRECT} set. 2304 */ 2305 private boolean isCursorInsideEasyCorrectionSpan() { 2306 Spannable spannable = (Spannable) mTextView.getText(); 2307 SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(), 2308 mTextView.getSelectionEnd(), SuggestionSpan.class); 2309 for (int i = 0; i < suggestionSpans.length; i++) { 2310 if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) { 2311 return true; 2312 } 2313 } 2314 return false; 2315 } 2316 2317 void onTouchUpEvent(MotionEvent event) { 2318 if (getSelectionActionModeHelper().resetSelection( 2319 getTextView().getOffsetForPosition(event.getX(), event.getY()))) { 2320 return; 2321 } 2322 2323 boolean selectAllGotFocus = mSelectAllOnFocus && mTextView.didTouchFocusSelect(); 2324 hideCursorAndSpanControllers(); 2325 stopTextActionMode(); 2326 CharSequence text = mTextView.getText(); 2327 if (!selectAllGotFocus && text.length() > 0) { 2328 // Move cursor 2329 final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); 2330 2331 final boolean shouldInsertCursor = !mRequestingLinkActionMode; 2332 if (shouldInsertCursor) { 2333 Selection.setSelection((Spannable) text, offset); 2334 if (mSpellChecker != null) { 2335 // When the cursor moves, the word that was typed may need spell check 2336 mSpellChecker.onSelectionChanged(); 2337 } 2338 } 2339 2340 if (!extractedTextModeWillBeStarted()) { 2341 if (isCursorInsideEasyCorrectionSpan()) { 2342 // Cancel the single tap delayed runnable. 2343 if (mInsertionActionModeRunnable != null) { 2344 mTextView.removeCallbacks(mInsertionActionModeRunnable); 2345 } 2346 2347 mShowSuggestionRunnable = this::replace; 2348 2349 // removeCallbacks is performed on every touch 2350 mTextView.postDelayed(mShowSuggestionRunnable, 2351 ViewConfiguration.getDoubleTapTimeout()); 2352 } else if (hasInsertionController()) { 2353 if (shouldInsertCursor) { 2354 getInsertionController().show(); 2355 } else { 2356 getInsertionController().hide(); 2357 } 2358 } 2359 } 2360 } 2361 } 2362 2363 protected void stopTextActionMode() { 2364 if (mTextActionMode != null) { 2365 // This will hide the mSelectionModifierCursorController 2366 mTextActionMode.finish(); 2367 } 2368 } 2369 2370 private void stopTextActionModeWithPreservingSelection() { 2371 if (mTextActionMode != null) { 2372 mRestartActionModeOnNextRefresh = true; 2373 } 2374 mPreserveSelection = true; 2375 stopTextActionMode(); 2376 mPreserveSelection = false; 2377 } 2378 2379 /** 2380 * @return True if this view supports insertion handles. 2381 */ 2382 boolean hasInsertionController() { 2383 return mInsertionControllerEnabled; 2384 } 2385 2386 /** 2387 * @return True if this view supports selection handles. 2388 */ 2389 boolean hasSelectionController() { 2390 return mSelectionControllerEnabled; 2391 } 2392 2393 private InsertionPointCursorController getInsertionController() { 2394 if (!mInsertionControllerEnabled) { 2395 return null; 2396 } 2397 2398 if (mInsertionPointCursorController == null) { 2399 mInsertionPointCursorController = new InsertionPointCursorController(); 2400 2401 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 2402 observer.addOnTouchModeChangeListener(mInsertionPointCursorController); 2403 } 2404 2405 return mInsertionPointCursorController; 2406 } 2407 2408 @Nullable 2409 SelectionModifierCursorController getSelectionController() { 2410 if (!mSelectionControllerEnabled) { 2411 return null; 2412 } 2413 2414 if (mSelectionModifierCursorController == null) { 2415 mSelectionModifierCursorController = new SelectionModifierCursorController(); 2416 2417 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 2418 observer.addOnTouchModeChangeListener(mSelectionModifierCursorController); 2419 } 2420 2421 return mSelectionModifierCursorController; 2422 } 2423 2424 @VisibleForTesting 2425 @Nullable 2426 public Drawable getCursorDrawable() { 2427 return mDrawableForCursor; 2428 } 2429 2430 private void updateCursorPosition(int top, int bottom, float horizontal) { 2431 if (mDrawableForCursor == null) { 2432 mDrawableForCursor = mTextView.getContext().getDrawable( 2433 mTextView.mCursorDrawableRes); 2434 } 2435 final int left = clampHorizontalPosition(mDrawableForCursor, horizontal); 2436 final int width = mDrawableForCursor.getIntrinsicWidth(); 2437 mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width, 2438 bottom + mTempRect.bottom); 2439 } 2440 2441 /** 2442 * Return clamped position for the drawable. If the drawable is within the boundaries of the 2443 * view, then it is offset with the left padding of the cursor drawable. If the drawable is at 2444 * the beginning or the end of the text then its drawable edge is aligned with left or right of 2445 * the view boundary. If the drawable is null, horizontal parameter is aligned to left or right 2446 * of the view. 2447 * 2448 * @param drawable Drawable. Can be null. 2449 * @param horizontal Horizontal position for the drawable. 2450 * @return The clamped horizontal position for the drawable. 2451 */ 2452 private int clampHorizontalPosition(@Nullable final Drawable drawable, float horizontal) { 2453 horizontal = Math.max(0.5f, horizontal - 0.5f); 2454 if (mTempRect == null) mTempRect = new Rect(); 2455 2456 int drawableWidth = 0; 2457 if (drawable != null) { 2458 drawable.getPadding(mTempRect); 2459 drawableWidth = drawable.getIntrinsicWidth(); 2460 } else { 2461 mTempRect.setEmpty(); 2462 } 2463 2464 int scrollX = mTextView.getScrollX(); 2465 float horizontalDiff = horizontal - scrollX; 2466 int viewClippedWidth = mTextView.getWidth() - mTextView.getCompoundPaddingLeft() 2467 - mTextView.getCompoundPaddingRight(); 2468 2469 final int left; 2470 if (horizontalDiff >= (viewClippedWidth - 1f)) { 2471 // at the rightmost position 2472 left = viewClippedWidth + scrollX - (drawableWidth - mTempRect.right); 2473 } else if (Math.abs(horizontalDiff) <= 1f 2474 || (TextUtils.isEmpty(mTextView.getText()) 2475 && (TextView.VERY_WIDE - scrollX) <= (viewClippedWidth + 1f) 2476 && horizontal <= 1f)) { 2477 // at the leftmost position 2478 left = scrollX - mTempRect.left; 2479 } else { 2480 left = (int) horizontal - mTempRect.left; 2481 } 2482 return left; 2483 } 2484 2485 /** 2486 * Called by the framework in response to a text auto-correction (such as fixing a typo using a 2487 * a dictionary) from the current input method, provided by it calling 2488 * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default 2489 * implementation flashes the background of the corrected word to provide feedback to the user. 2490 * 2491 * @param info The auto correct info about the text that was corrected. 2492 */ 2493 public void onCommitCorrection(CorrectionInfo info) { 2494 if (mCorrectionHighlighter == null) { 2495 mCorrectionHighlighter = new CorrectionHighlighter(); 2496 } else { 2497 mCorrectionHighlighter.invalidate(false); 2498 } 2499 2500 mCorrectionHighlighter.highlight(info); 2501 mUndoInputFilter.freezeLastEdit(); 2502 } 2503 2504 void onScrollChanged() { 2505 if (mPositionListener != null) { 2506 mPositionListener.onScrollChanged(); 2507 } 2508 if (mTextActionMode != null) { 2509 mTextActionMode.invalidateContentRect(); 2510 } 2511 } 2512 2513 /** 2514 * @return True when the TextView isFocused and has a valid zero-length selection (cursor). 2515 */ 2516 private boolean shouldBlink() { 2517 if (!isCursorVisible() || !mTextView.isFocused()) return false; 2518 2519 final int start = mTextView.getSelectionStart(); 2520 if (start < 0) return false; 2521 2522 final int end = mTextView.getSelectionEnd(); 2523 if (end < 0) return false; 2524 2525 return start == end; 2526 } 2527 2528 void makeBlink() { 2529 if (shouldBlink()) { 2530 mShowCursor = SystemClock.uptimeMillis(); 2531 if (mBlink == null) mBlink = new Blink(); 2532 mTextView.removeCallbacks(mBlink); 2533 mTextView.postDelayed(mBlink, BLINK); 2534 } else { 2535 if (mBlink != null) mTextView.removeCallbacks(mBlink); 2536 } 2537 } 2538 2539 private class Blink implements Runnable { 2540 private boolean mCancelled; 2541 2542 public void run() { 2543 if (mCancelled) { 2544 return; 2545 } 2546 2547 mTextView.removeCallbacks(this); 2548 2549 if (shouldBlink()) { 2550 if (mTextView.getLayout() != null) { 2551 mTextView.invalidateCursorPath(); 2552 } 2553 2554 mTextView.postDelayed(this, BLINK); 2555 } 2556 } 2557 2558 void cancel() { 2559 if (!mCancelled) { 2560 mTextView.removeCallbacks(this); 2561 mCancelled = true; 2562 } 2563 } 2564 2565 void uncancel() { 2566 mCancelled = false; 2567 } 2568 } 2569 2570 private DragShadowBuilder getTextThumbnailBuilder(int start, int end) { 2571 TextView shadowView = (TextView) View.inflate(mTextView.getContext(), 2572 com.android.internal.R.layout.text_drag_thumbnail, null); 2573 2574 if (shadowView == null) { 2575 throw new IllegalArgumentException("Unable to inflate text drag thumbnail"); 2576 } 2577 2578 if (end - start > DRAG_SHADOW_MAX_TEXT_LENGTH) { 2579 final long range = getCharClusterRange(start + DRAG_SHADOW_MAX_TEXT_LENGTH); 2580 end = TextUtils.unpackRangeEndFromLong(range); 2581 } 2582 final CharSequence text = mTextView.getTransformedText(start, end); 2583 shadowView.setText(text); 2584 shadowView.setTextColor(mTextView.getTextColors()); 2585 2586 shadowView.setTextAppearance(R.styleable.Theme_textAppearanceLarge); 2587 shadowView.setGravity(Gravity.CENTER); 2588 2589 shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 2590 ViewGroup.LayoutParams.WRAP_CONTENT)); 2591 2592 final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 2593 shadowView.measure(size, size); 2594 2595 shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight()); 2596 shadowView.invalidate(); 2597 return new DragShadowBuilder(shadowView); 2598 } 2599 2600 private static class DragLocalState { 2601 public TextView sourceTextView; 2602 public int start, end; 2603 2604 public DragLocalState(TextView sourceTextView, int start, int end) { 2605 this.sourceTextView = sourceTextView; 2606 this.start = start; 2607 this.end = end; 2608 } 2609 } 2610 2611 void onDrop(DragEvent event) { 2612 SpannableStringBuilder content = new SpannableStringBuilder(); 2613 2614 final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event); 2615 if (permissions != null) { 2616 permissions.takeTransient(); 2617 } 2618 2619 try { 2620 ClipData clipData = event.getClipData(); 2621 final int itemCount = clipData.getItemCount(); 2622 for (int i = 0; i < itemCount; i++) { 2623 Item item = clipData.getItemAt(i); 2624 content.append(item.coerceToStyledText(mTextView.getContext())); 2625 } 2626 } finally { 2627 if (permissions != null) { 2628 permissions.release(); 2629 } 2630 } 2631 2632 mTextView.beginBatchEdit(); 2633 mUndoInputFilter.freezeLastEdit(); 2634 try { 2635 final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); 2636 Object localState = event.getLocalState(); 2637 DragLocalState dragLocalState = null; 2638 if (localState instanceof DragLocalState) { 2639 dragLocalState = (DragLocalState) localState; 2640 } 2641 boolean dragDropIntoItself = dragLocalState != null 2642 && dragLocalState.sourceTextView == mTextView; 2643 2644 if (dragDropIntoItself) { 2645 if (offset >= dragLocalState.start && offset < dragLocalState.end) { 2646 // A drop inside the original selection discards the drop. 2647 return; 2648 } 2649 } 2650 2651 final int originalLength = mTextView.getText().length(); 2652 int min = offset; 2653 int max = offset; 2654 2655 Selection.setSelection((Spannable) mTextView.getText(), max); 2656 mTextView.replaceText_internal(min, max, content); 2657 2658 if (dragDropIntoItself) { 2659 int dragSourceStart = dragLocalState.start; 2660 int dragSourceEnd = dragLocalState.end; 2661 if (max <= dragSourceStart) { 2662 // Inserting text before selection has shifted positions 2663 final int shift = mTextView.getText().length() - originalLength; 2664 dragSourceStart += shift; 2665 dragSourceEnd += shift; 2666 } 2667 2668 // Delete original selection 2669 mTextView.deleteText_internal(dragSourceStart, dragSourceEnd); 2670 2671 // Make sure we do not leave two adjacent spaces. 2672 final int prevCharIdx = Math.max(0, dragSourceStart - 1); 2673 final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1); 2674 if (nextCharIdx > prevCharIdx + 1) { 2675 CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx); 2676 if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) { 2677 mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1); 2678 } 2679 } 2680 } 2681 } finally { 2682 mTextView.endBatchEdit(); 2683 mUndoInputFilter.freezeLastEdit(); 2684 } 2685 } 2686 2687 public void addSpanWatchers(Spannable text) { 2688 final int textLength = text.length(); 2689 2690 if (mKeyListener != null) { 2691 text.setSpan(mKeyListener, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 2692 } 2693 2694 if (mSpanController == null) { 2695 mSpanController = new SpanController(); 2696 } 2697 text.setSpan(mSpanController, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 2698 } 2699 2700 void setContextMenuAnchor(float x, float y) { 2701 mContextMenuAnchorX = x; 2702 mContextMenuAnchorY = y; 2703 } 2704 2705 void onCreateContextMenu(ContextMenu menu) { 2706 if (mIsBeingLongClicked || Float.isNaN(mContextMenuAnchorX) 2707 || Float.isNaN(mContextMenuAnchorY)) { 2708 return; 2709 } 2710 final int offset = mTextView.getOffsetForPosition(mContextMenuAnchorX, mContextMenuAnchorY); 2711 if (offset == -1) { 2712 return; 2713 } 2714 2715 stopTextActionModeWithPreservingSelection(); 2716 if (mTextView.canSelectText()) { 2717 final boolean isOnSelection = mTextView.hasSelection() 2718 && offset >= mTextView.getSelectionStart() 2719 && offset <= mTextView.getSelectionEnd(); 2720 if (!isOnSelection) { 2721 // Right clicked position is not on the selection. Remove the selection and move the 2722 // cursor to the right clicked position. 2723 Selection.setSelection((Spannable) mTextView.getText(), offset); 2724 stopTextActionMode(); 2725 } 2726 } 2727 2728 if (shouldOfferToShowSuggestions()) { 2729 final SuggestionInfo[] suggestionInfoArray = 2730 new SuggestionInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE]; 2731 for (int i = 0; i < suggestionInfoArray.length; i++) { 2732 suggestionInfoArray[i] = new SuggestionInfo(); 2733 } 2734 final SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.NONE, MENU_ITEM_ORDER_REPLACE, 2735 com.android.internal.R.string.replace); 2736 final int numItems = mSuggestionHelper.getSuggestionInfo(suggestionInfoArray, null); 2737 for (int i = 0; i < numItems; i++) { 2738 final SuggestionInfo info = suggestionInfoArray[i]; 2739 subMenu.add(Menu.NONE, Menu.NONE, i, info.mText) 2740 .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 2741 @Override 2742 public boolean onMenuItemClick(MenuItem item) { 2743 replaceWithSuggestion(info); 2744 return true; 2745 } 2746 }); 2747 } 2748 } 2749 2750 menu.add(Menu.NONE, TextView.ID_UNDO, MENU_ITEM_ORDER_UNDO, 2751 com.android.internal.R.string.undo) 2752 .setAlphabeticShortcut('z') 2753 .setOnMenuItemClickListener(mOnContextMenuItemClickListener) 2754 .setEnabled(mTextView.canUndo()); 2755 menu.add(Menu.NONE, TextView.ID_REDO, MENU_ITEM_ORDER_REDO, 2756 com.android.internal.R.string.redo) 2757 .setOnMenuItemClickListener(mOnContextMenuItemClickListener) 2758 .setEnabled(mTextView.canRedo()); 2759 2760 menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, 2761 com.android.internal.R.string.cut) 2762 .setAlphabeticShortcut('x') 2763 .setOnMenuItemClickListener(mOnContextMenuItemClickListener) 2764 .setEnabled(mTextView.canCut()); 2765 menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, 2766 com.android.internal.R.string.copy) 2767 .setAlphabeticShortcut('c') 2768 .setOnMenuItemClickListener(mOnContextMenuItemClickListener) 2769 .setEnabled(mTextView.canCopy()); 2770 menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, 2771 com.android.internal.R.string.paste) 2772 .setAlphabeticShortcut('v') 2773 .setEnabled(mTextView.canPaste()) 2774 .setOnMenuItemClickListener(mOnContextMenuItemClickListener); 2775 menu.add(Menu.NONE, TextView.ID_PASTE_AS_PLAIN_TEXT, MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT, 2776 com.android.internal.R.string.paste_as_plain_text) 2777 .setEnabled(mTextView.canPasteAsPlainText()) 2778 .setOnMenuItemClickListener(mOnContextMenuItemClickListener); 2779 menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, 2780 com.android.internal.R.string.share) 2781 .setEnabled(mTextView.canShare()) 2782 .setOnMenuItemClickListener(mOnContextMenuItemClickListener); 2783 menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, 2784 com.android.internal.R.string.selectAll) 2785 .setAlphabeticShortcut('a') 2786 .setEnabled(mTextView.canSelectAllText()) 2787 .setOnMenuItemClickListener(mOnContextMenuItemClickListener); 2788 menu.add(Menu.NONE, TextView.ID_AUTOFILL, MENU_ITEM_ORDER_AUTOFILL, 2789 android.R.string.autofill) 2790 .setEnabled(mTextView.canRequestAutofill()) 2791 .setOnMenuItemClickListener(mOnContextMenuItemClickListener); 2792 2793 mPreserveSelection = true; 2794 } 2795 2796 @Nullable 2797 private SuggestionSpan findEquivalentSuggestionSpan( 2798 @NonNull SuggestionSpanInfo suggestionSpanInfo) { 2799 final Editable editable = (Editable) mTextView.getText(); 2800 if (editable.getSpanStart(suggestionSpanInfo.mSuggestionSpan) >= 0) { 2801 // Exactly same span is found. 2802 return suggestionSpanInfo.mSuggestionSpan; 2803 } 2804 // Suggestion span couldn't be found. Try to find a suggestion span that has the same 2805 // contents. 2806 final SuggestionSpan[] suggestionSpans = editable.getSpans(suggestionSpanInfo.mSpanStart, 2807 suggestionSpanInfo.mSpanEnd, SuggestionSpan.class); 2808 for (final SuggestionSpan suggestionSpan : suggestionSpans) { 2809 final int start = editable.getSpanStart(suggestionSpan); 2810 if (start != suggestionSpanInfo.mSpanStart) { 2811 continue; 2812 } 2813 final int end = editable.getSpanEnd(suggestionSpan); 2814 if (end != suggestionSpanInfo.mSpanEnd) { 2815 continue; 2816 } 2817 if (suggestionSpan.equals(suggestionSpanInfo.mSuggestionSpan)) { 2818 return suggestionSpan; 2819 } 2820 } 2821 return null; 2822 } 2823 2824 private void replaceWithSuggestion(@NonNull final SuggestionInfo suggestionInfo) { 2825 final SuggestionSpan targetSuggestionSpan = findEquivalentSuggestionSpan( 2826 suggestionInfo.mSuggestionSpanInfo); 2827 if (targetSuggestionSpan == null) { 2828 // Span has been removed 2829 return; 2830 } 2831 final Editable editable = (Editable) mTextView.getText(); 2832 final int spanStart = editable.getSpanStart(targetSuggestionSpan); 2833 final int spanEnd = editable.getSpanEnd(targetSuggestionSpan); 2834 if (spanStart < 0 || spanEnd <= spanStart) { 2835 // Span has been removed 2836 return; 2837 } 2838 2839 final String originalText = TextUtils.substring(editable, spanStart, spanEnd); 2840 // SuggestionSpans are removed by replace: save them before 2841 SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, 2842 SuggestionSpan.class); 2843 final int length = suggestionSpans.length; 2844 int[] suggestionSpansStarts = new int[length]; 2845 int[] suggestionSpansEnds = new int[length]; 2846 int[] suggestionSpansFlags = new int[length]; 2847 for (int i = 0; i < length; i++) { 2848 final SuggestionSpan suggestionSpan = suggestionSpans[i]; 2849 suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan); 2850 suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan); 2851 suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan); 2852 2853 // Remove potential misspelled flags 2854 int suggestionSpanFlags = suggestionSpan.getFlags(); 2855 if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) != 0) { 2856 suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED; 2857 suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT; 2858 suggestionSpan.setFlags(suggestionSpanFlags); 2859 } 2860 } 2861 2862 // Notify source IME of the suggestion pick. Do this before swapping texts. 2863 targetSuggestionSpan.notifySelection( 2864 mTextView.getContext(), originalText, suggestionInfo.mSuggestionIndex); 2865 2866 // Swap text content between actual text and Suggestion span 2867 final int suggestionStart = suggestionInfo.mSuggestionStart; 2868 final int suggestionEnd = suggestionInfo.mSuggestionEnd; 2869 final String suggestion = suggestionInfo.mText.subSequence( 2870 suggestionStart, suggestionEnd).toString(); 2871 mTextView.replaceText_internal(spanStart, spanEnd, suggestion); 2872 2873 String[] suggestions = targetSuggestionSpan.getSuggestions(); 2874 suggestions[suggestionInfo.mSuggestionIndex] = originalText; 2875 2876 // Restore previous SuggestionSpans 2877 final int lengthDelta = suggestion.length() - (spanEnd - spanStart); 2878 for (int i = 0; i < length; i++) { 2879 // Only spans that include the modified region make sense after replacement 2880 // Spans partially included in the replaced region are removed, there is no 2881 // way to assign them a valid range after replacement 2882 if (suggestionSpansStarts[i] <= spanStart && suggestionSpansEnds[i] >= spanEnd) { 2883 mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i], 2884 suggestionSpansEnds[i] + lengthDelta, suggestionSpansFlags[i]); 2885 } 2886 } 2887 // Move cursor at the end of the replaced word 2888 final int newCursorPosition = spanEnd + lengthDelta; 2889 mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition); 2890 } 2891 2892 private final MenuItem.OnMenuItemClickListener mOnContextMenuItemClickListener = 2893 new MenuItem.OnMenuItemClickListener() { 2894 @Override 2895 public boolean onMenuItemClick(MenuItem item) { 2896 if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { 2897 return true; 2898 } 2899 return mTextView.onTextContextMenuItem(item.getItemId()); 2900 } 2901 }; 2902 2903 /** 2904 * Controls the {@link EasyEditSpan} monitoring when it is added, and when the related 2905 * pop-up should be displayed. 2906 * Also monitors {@link Selection} to call back to the attached input method. 2907 */ 2908 private class SpanController implements SpanWatcher { 2909 2910 private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs 2911 2912 private EasyEditPopupWindow mPopupWindow; 2913 2914 private Runnable mHidePopup; 2915 2916 // This function is pure but inner classes can't have static functions 2917 private boolean isNonIntermediateSelectionSpan(final Spannable text, 2918 final Object span) { 2919 return (Selection.SELECTION_START == span || Selection.SELECTION_END == span) 2920 && (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0; 2921 } 2922 2923 @Override 2924 public void onSpanAdded(Spannable text, Object span, int start, int end) { 2925 if (isNonIntermediateSelectionSpan(text, span)) { 2926 sendUpdateSelection(); 2927 } else if (span instanceof EasyEditSpan) { 2928 if (mPopupWindow == null) { 2929 mPopupWindow = new EasyEditPopupWindow(); 2930 mHidePopup = new Runnable() { 2931 @Override 2932 public void run() { 2933 hide(); 2934 } 2935 }; 2936 } 2937 2938 // Make sure there is only at most one EasyEditSpan in the text 2939 if (mPopupWindow.mEasyEditSpan != null) { 2940 mPopupWindow.mEasyEditSpan.setDeleteEnabled(false); 2941 } 2942 2943 mPopupWindow.setEasyEditSpan((EasyEditSpan) span); 2944 mPopupWindow.setOnDeleteListener(new EasyEditDeleteListener() { 2945 @Override 2946 public void onDeleteClick(EasyEditSpan span) { 2947 Editable editable = (Editable) mTextView.getText(); 2948 int start = editable.getSpanStart(span); 2949 int end = editable.getSpanEnd(span); 2950 if (start >= 0 && end >= 0) { 2951 sendEasySpanNotification(EasyEditSpan.TEXT_DELETED, span); 2952 mTextView.deleteText_internal(start, end); 2953 } 2954 editable.removeSpan(span); 2955 } 2956 }); 2957 2958 if (mTextView.getWindowVisibility() != View.VISIBLE) { 2959 // The window is not visible yet, ignore the text change. 2960 return; 2961 } 2962 2963 if (mTextView.getLayout() == null) { 2964 // The view has not been laid out yet, ignore the text change 2965 return; 2966 } 2967 2968 if (extractedTextModeWillBeStarted()) { 2969 // The input is in extract mode. Do not handle the easy edit in 2970 // the original TextView, as the ExtractEditText will do 2971 return; 2972 } 2973 2974 mPopupWindow.show(); 2975 mTextView.removeCallbacks(mHidePopup); 2976 mTextView.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS); 2977 } 2978 } 2979 2980 @Override 2981 public void onSpanRemoved(Spannable text, Object span, int start, int end) { 2982 if (isNonIntermediateSelectionSpan(text, span)) { 2983 sendUpdateSelection(); 2984 } else if (mPopupWindow != null && span == mPopupWindow.mEasyEditSpan) { 2985 hide(); 2986 } 2987 } 2988 2989 @Override 2990 public void onSpanChanged(Spannable text, Object span, int previousStart, int previousEnd, 2991 int newStart, int newEnd) { 2992 if (isNonIntermediateSelectionSpan(text, span)) { 2993 sendUpdateSelection(); 2994 } else if (mPopupWindow != null && span instanceof EasyEditSpan) { 2995 EasyEditSpan easyEditSpan = (EasyEditSpan) span; 2996 sendEasySpanNotification(EasyEditSpan.TEXT_MODIFIED, easyEditSpan); 2997 text.removeSpan(easyEditSpan); 2998 } 2999 } 3000 3001 public void hide() { 3002 if (mPopupWindow != null) { 3003 mPopupWindow.hide(); 3004 mTextView.removeCallbacks(mHidePopup); 3005 } 3006 } 3007 3008 private void sendEasySpanNotification(int textChangedType, EasyEditSpan span) { 3009 try { 3010 PendingIntent pendingIntent = span.getPendingIntent(); 3011 if (pendingIntent != null) { 3012 Intent intent = new Intent(); 3013 intent.putExtra(EasyEditSpan.EXTRA_TEXT_CHANGED_TYPE, textChangedType); 3014 pendingIntent.send(mTextView.getContext(), 0, intent); 3015 } 3016 } catch (CanceledException e) { 3017 // This should not happen, as we should try to send the intent only once. 3018 Log.w(TAG, "PendingIntent for notification cannot be sent", e); 3019 } 3020 } 3021 } 3022 3023 /** 3024 * Listens for the delete event triggered by {@link EasyEditPopupWindow}. 3025 */ 3026 private interface EasyEditDeleteListener { 3027 3028 /** 3029 * Clicks the delete pop-up. 3030 */ 3031 void onDeleteClick(EasyEditSpan span); 3032 } 3033 3034 /** 3035 * Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled 3036 * by {@link SpanController}. 3037 */ 3038 private class EasyEditPopupWindow extends PinnedPopupWindow 3039 implements OnClickListener { 3040 private static final int POPUP_TEXT_LAYOUT = 3041 com.android.internal.R.layout.text_edit_action_popup_text; 3042 private TextView mDeleteTextView; 3043 private EasyEditSpan mEasyEditSpan; 3044 private EasyEditDeleteListener mOnDeleteListener; 3045 3046 @Override 3047 protected void createPopupWindow() { 3048 mPopupWindow = new PopupWindow(mTextView.getContext(), null, 3049 com.android.internal.R.attr.textSelectHandleWindowStyle); 3050 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 3051 mPopupWindow.setClippingEnabled(true); 3052 } 3053 3054 @Override 3055 protected void initContentView() { 3056 LinearLayout linearLayout = new LinearLayout(mTextView.getContext()); 3057 linearLayout.setOrientation(LinearLayout.HORIZONTAL); 3058 mContentView = linearLayout; 3059 mContentView.setBackgroundResource( 3060 com.android.internal.R.drawable.text_edit_side_paste_window); 3061 3062 LayoutInflater inflater = (LayoutInflater) mTextView.getContext() 3063 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 3064 3065 LayoutParams wrapContent = new LayoutParams( 3066 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 3067 3068 mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null); 3069 mDeleteTextView.setLayoutParams(wrapContent); 3070 mDeleteTextView.setText(com.android.internal.R.string.delete); 3071 mDeleteTextView.setOnClickListener(this); 3072 mContentView.addView(mDeleteTextView); 3073 } 3074 3075 public void setEasyEditSpan(EasyEditSpan easyEditSpan) { 3076 mEasyEditSpan = easyEditSpan; 3077 } 3078 3079 private void setOnDeleteListener(EasyEditDeleteListener listener) { 3080 mOnDeleteListener = listener; 3081 } 3082 3083 @Override 3084 public void onClick(View view) { 3085 if (view == mDeleteTextView 3086 && mEasyEditSpan != null && mEasyEditSpan.isDeleteEnabled() 3087 && mOnDeleteListener != null) { 3088 mOnDeleteListener.onDeleteClick(mEasyEditSpan); 3089 } 3090 } 3091 3092 @Override 3093 public void hide() { 3094 if (mEasyEditSpan != null) { 3095 mEasyEditSpan.setDeleteEnabled(false); 3096 } 3097 mOnDeleteListener = null; 3098 super.hide(); 3099 } 3100 3101 @Override 3102 protected int getTextOffset() { 3103 // Place the pop-up at the end of the span 3104 Editable editable = (Editable) mTextView.getText(); 3105 return editable.getSpanEnd(mEasyEditSpan); 3106 } 3107 3108 @Override 3109 protected int getVerticalLocalPosition(int line) { 3110 final Layout layout = mTextView.getLayout(); 3111 return layout.getLineBottomWithoutSpacing(line); 3112 } 3113 3114 @Override 3115 protected int clipVertically(int positionY) { 3116 // As we display the pop-up below the span, no vertical clipping is required. 3117 return positionY; 3118 } 3119 } 3120 3121 private class PositionListener implements ViewTreeObserver.OnPreDrawListener { 3122 // 3 handles 3123 // 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others) 3124 // 1 CursorAnchorInfoNotifier 3125 private static final int MAXIMUM_NUMBER_OF_LISTENERS = 7; 3126 private TextViewPositionListener[] mPositionListeners = 3127 new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS]; 3128 private boolean[] mCanMove = new boolean[MAXIMUM_NUMBER_OF_LISTENERS]; 3129 private boolean mPositionHasChanged = true; 3130 // Absolute position of the TextView with respect to its parent window 3131 private int mPositionX, mPositionY; 3132 private int mPositionXOnScreen, mPositionYOnScreen; 3133 private int mNumberOfListeners; 3134 private boolean mScrollHasChanged; 3135 final int[] mTempCoords = new int[2]; 3136 3137 public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) { 3138 if (mNumberOfListeners == 0) { 3139 updatePosition(); 3140 ViewTreeObserver vto = mTextView.getViewTreeObserver(); 3141 vto.addOnPreDrawListener(this); 3142 } 3143 3144 int emptySlotIndex = -1; 3145 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 3146 TextViewPositionListener listener = mPositionListeners[i]; 3147 if (listener == positionListener) { 3148 return; 3149 } else if (emptySlotIndex < 0 && listener == null) { 3150 emptySlotIndex = i; 3151 } 3152 } 3153 3154 mPositionListeners[emptySlotIndex] = positionListener; 3155 mCanMove[emptySlotIndex] = canMove; 3156 mNumberOfListeners++; 3157 } 3158 3159 public void removeSubscriber(TextViewPositionListener positionListener) { 3160 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 3161 if (mPositionListeners[i] == positionListener) { 3162 mPositionListeners[i] = null; 3163 mNumberOfListeners--; 3164 break; 3165 } 3166 } 3167 3168 if (mNumberOfListeners == 0) { 3169 ViewTreeObserver vto = mTextView.getViewTreeObserver(); 3170 vto.removeOnPreDrawListener(this); 3171 } 3172 } 3173 3174 public int getPositionX() { 3175 return mPositionX; 3176 } 3177 3178 public int getPositionY() { 3179 return mPositionY; 3180 } 3181 3182 public int getPositionXOnScreen() { 3183 return mPositionXOnScreen; 3184 } 3185 3186 public int getPositionYOnScreen() { 3187 return mPositionYOnScreen; 3188 } 3189 3190 @Override 3191 public boolean onPreDraw() { 3192 updatePosition(); 3193 3194 for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) { 3195 if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) { 3196 TextViewPositionListener positionListener = mPositionListeners[i]; 3197 if (positionListener != null) { 3198 positionListener.updatePosition(mPositionX, mPositionY, 3199 mPositionHasChanged, mScrollHasChanged); 3200 } 3201 } 3202 } 3203 3204 mScrollHasChanged = false; 3205 return true; 3206 } 3207 3208 private void updatePosition() { 3209 mTextView.getLocationInWindow(mTempCoords); 3210 3211 mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY; 3212 3213 mPositionX = mTempCoords[0]; 3214 mPositionY = mTempCoords[1]; 3215 3216 mTextView.getLocationOnScreen(mTempCoords); 3217 3218 mPositionXOnScreen = mTempCoords[0]; 3219 mPositionYOnScreen = mTempCoords[1]; 3220 } 3221 3222 public void onScrollChanged() { 3223 mScrollHasChanged = true; 3224 } 3225 } 3226 3227 private abstract class PinnedPopupWindow implements TextViewPositionListener { 3228 protected PopupWindow mPopupWindow; 3229 protected ViewGroup mContentView; 3230 int mPositionX, mPositionY; 3231 int mClippingLimitLeft, mClippingLimitRight; 3232 3233 protected abstract void createPopupWindow(); 3234 protected abstract void initContentView(); 3235 protected abstract int getTextOffset(); 3236 protected abstract int getVerticalLocalPosition(int line); 3237 protected abstract int clipVertically(int positionY); 3238 protected void setUp() { 3239 } 3240 3241 public PinnedPopupWindow() { 3242 // Due to calling subclass methods in base constructor, subclass constructor is not 3243 // called before subclass methods, e.g. createPopupWindow or initContentView. To give 3244 // a chance to initialize subclasses, call setUp() method here. 3245 // TODO: It is good to extract non trivial initialization code from constructor. 3246 setUp(); 3247 3248 createPopupWindow(); 3249 3250 mPopupWindow.setWindowLayoutType( 3251 WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL); 3252 mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 3253 mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 3254 3255 initContentView(); 3256 3257 LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 3258 ViewGroup.LayoutParams.WRAP_CONTENT); 3259 mContentView.setLayoutParams(wrapContent); 3260 3261 mPopupWindow.setContentView(mContentView); 3262 } 3263 3264 public void show() { 3265 getPositionListener().addSubscriber(this, false /* offset is fixed */); 3266 3267 computeLocalPosition(); 3268 3269 final PositionListener positionListener = getPositionListener(); 3270 updatePosition(positionListener.getPositionX(), positionListener.getPositionY()); 3271 } 3272 3273 protected void measureContent() { 3274 final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); 3275 mContentView.measure( 3276 View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, 3277 View.MeasureSpec.AT_MOST), 3278 View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, 3279 View.MeasureSpec.AT_MOST)); 3280 } 3281 3282 /* The popup window will be horizontally centered on the getTextOffset() and vertically 3283 * positioned according to viewportToContentHorizontalOffset. 3284 * 3285 * This method assumes that mContentView has properly been measured from its content. */ 3286 private void computeLocalPosition() { 3287 measureContent(); 3288 final int width = mContentView.getMeasuredWidth(); 3289 final int offset = getTextOffset(); 3290 mPositionX = (int) (mTextView.getLayout().getPrimaryHorizontal(offset) - width / 2.0f); 3291 mPositionX += mTextView.viewportToContentHorizontalOffset(); 3292 3293 final int line = mTextView.getLayout().getLineForOffset(offset); 3294 mPositionY = getVerticalLocalPosition(line); 3295 mPositionY += mTextView.viewportToContentVerticalOffset(); 3296 } 3297 3298 private void updatePosition(int parentPositionX, int parentPositionY) { 3299 int positionX = parentPositionX + mPositionX; 3300 int positionY = parentPositionY + mPositionY; 3301 3302 positionY = clipVertically(positionY); 3303 3304 // Horizontal clipping 3305 final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); 3306 final int width = mContentView.getMeasuredWidth(); 3307 positionX = Math.min( 3308 displayMetrics.widthPixels - width + mClippingLimitRight, positionX); 3309 positionX = Math.max(-mClippingLimitLeft, positionX); 3310 3311 if (isShowing()) { 3312 mPopupWindow.update(positionX, positionY, -1, -1); 3313 } else { 3314 mPopupWindow.showAtLocation(mTextView, Gravity.NO_GRAVITY, 3315 positionX, positionY); 3316 } 3317 } 3318 3319 public void hide() { 3320 if (!isShowing()) { 3321 return; 3322 } 3323 mPopupWindow.dismiss(); 3324 getPositionListener().removeSubscriber(this); 3325 } 3326 3327 @Override 3328 public void updatePosition(int parentPositionX, int parentPositionY, 3329 boolean parentPositionChanged, boolean parentScrolled) { 3330 // Either parentPositionChanged or parentScrolled is true, check if still visible 3331 if (isShowing() && isOffsetVisible(getTextOffset())) { 3332 if (parentScrolled) computeLocalPosition(); 3333 updatePosition(parentPositionX, parentPositionY); 3334 } else { 3335 hide(); 3336 } 3337 } 3338 3339 public boolean isShowing() { 3340 return mPopupWindow.isShowing(); 3341 } 3342 } 3343 3344 private static final class SuggestionInfo { 3345 // Range of actual suggestion within mText 3346 int mSuggestionStart, mSuggestionEnd; 3347 3348 // The SuggestionSpan that this TextView represents 3349 final SuggestionSpanInfo mSuggestionSpanInfo = new SuggestionSpanInfo(); 3350 3351 // The index of this suggestion inside suggestionSpan 3352 int mSuggestionIndex; 3353 3354 final SpannableStringBuilder mText = new SpannableStringBuilder(); 3355 3356 void clear() { 3357 mSuggestionSpanInfo.clear(); 3358 mText.clear(); 3359 } 3360 3361 // Utility method to set attributes about a SuggestionSpan. 3362 void setSpanInfo(SuggestionSpan span, int spanStart, int spanEnd) { 3363 mSuggestionSpanInfo.mSuggestionSpan = span; 3364 mSuggestionSpanInfo.mSpanStart = spanStart; 3365 mSuggestionSpanInfo.mSpanEnd = spanEnd; 3366 } 3367 } 3368 3369 private static final class SuggestionSpanInfo { 3370 // The SuggestionSpan; 3371 @Nullable 3372 SuggestionSpan mSuggestionSpan; 3373 3374 // The SuggestionSpan start position 3375 int mSpanStart; 3376 3377 // The SuggestionSpan end position 3378 int mSpanEnd; 3379 3380 void clear() { 3381 mSuggestionSpan = null; 3382 } 3383 } 3384 3385 private class SuggestionHelper { 3386 private final Comparator<SuggestionSpan> mSuggestionSpanComparator = 3387 new SuggestionSpanComparator(); 3388 private final HashMap<SuggestionSpan, Integer> mSpansLengths = 3389 new HashMap<SuggestionSpan, Integer>(); 3390 3391 private class SuggestionSpanComparator implements Comparator<SuggestionSpan> { 3392 public int compare(SuggestionSpan span1, SuggestionSpan span2) { 3393 final int flag1 = span1.getFlags(); 3394 final int flag2 = span2.getFlags(); 3395 if (flag1 != flag2) { 3396 // The order here should match what is used in updateDrawState 3397 final boolean easy1 = (flag1 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; 3398 final boolean easy2 = (flag2 & SuggestionSpan.FLAG_EASY_CORRECT) != 0; 3399 final boolean misspelled1 = (flag1 & SuggestionSpan.FLAG_MISSPELLED) != 0; 3400 final boolean misspelled2 = (flag2 & SuggestionSpan.FLAG_MISSPELLED) != 0; 3401 if (easy1 && !misspelled1) return -1; 3402 if (easy2 && !misspelled2) return 1; 3403 if (misspelled1) return -1; 3404 if (misspelled2) return 1; 3405 } 3406 3407 return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue(); 3408 } 3409 } 3410 3411 /** 3412 * Returns the suggestion spans that cover the current cursor position. The suggestion 3413 * spans are sorted according to the length of text that they are attached to. 3414 */ 3415 private SuggestionSpan[] getSortedSuggestionSpans() { 3416 int pos = mTextView.getSelectionStart(); 3417 Spannable spannable = (Spannable) mTextView.getText(); 3418 SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); 3419 3420 mSpansLengths.clear(); 3421 for (SuggestionSpan suggestionSpan : suggestionSpans) { 3422 int start = spannable.getSpanStart(suggestionSpan); 3423 int end = spannable.getSpanEnd(suggestionSpan); 3424 mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start)); 3425 } 3426 3427 // The suggestions are sorted according to their types (easy correction first, then 3428 // misspelled) and to the length of the text that they cover (shorter first). 3429 Arrays.sort(suggestionSpans, mSuggestionSpanComparator); 3430 mSpansLengths.clear(); 3431 3432 return suggestionSpans; 3433 } 3434 3435 /** 3436 * Gets the SuggestionInfo list that contains suggestion information at the current cursor 3437 * position. 3438 * 3439 * @param suggestionInfos SuggestionInfo array the results will be set. 3440 * @param misspelledSpanInfo a struct the misspelled SuggestionSpan info will be set. 3441 * @return the number of suggestions actually fetched. 3442 */ 3443 public int getSuggestionInfo(SuggestionInfo[] suggestionInfos, 3444 @Nullable SuggestionSpanInfo misspelledSpanInfo) { 3445 final Spannable spannable = (Spannable) mTextView.getText(); 3446 final SuggestionSpan[] suggestionSpans = getSortedSuggestionSpans(); 3447 final int nbSpans = suggestionSpans.length; 3448 if (nbSpans == 0) return 0; 3449 3450 int numberOfSuggestions = 0; 3451 for (final SuggestionSpan suggestionSpan : suggestionSpans) { 3452 final int spanStart = spannable.getSpanStart(suggestionSpan); 3453 final int spanEnd = spannable.getSpanEnd(suggestionSpan); 3454 3455 if (misspelledSpanInfo != null 3456 && (suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) { 3457 misspelledSpanInfo.mSuggestionSpan = suggestionSpan; 3458 misspelledSpanInfo.mSpanStart = spanStart; 3459 misspelledSpanInfo.mSpanEnd = spanEnd; 3460 } 3461 3462 final String[] suggestions = suggestionSpan.getSuggestions(); 3463 final int nbSuggestions = suggestions.length; 3464 suggestionLoop: 3465 for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) { 3466 final String suggestion = suggestions[suggestionIndex]; 3467 for (int i = 0; i < numberOfSuggestions; i++) { 3468 final SuggestionInfo otherSuggestionInfo = suggestionInfos[i]; 3469 if (otherSuggestionInfo.mText.toString().equals(suggestion)) { 3470 final int otherSpanStart = 3471 otherSuggestionInfo.mSuggestionSpanInfo.mSpanStart; 3472 final int otherSpanEnd = 3473 otherSuggestionInfo.mSuggestionSpanInfo.mSpanEnd; 3474 if (spanStart == otherSpanStart && spanEnd == otherSpanEnd) { 3475 continue suggestionLoop; 3476 } 3477 } 3478 } 3479 3480 SuggestionInfo suggestionInfo = suggestionInfos[numberOfSuggestions]; 3481 suggestionInfo.setSpanInfo(suggestionSpan, spanStart, spanEnd); 3482 suggestionInfo.mSuggestionIndex = suggestionIndex; 3483 suggestionInfo.mSuggestionStart = 0; 3484 suggestionInfo.mSuggestionEnd = suggestion.length(); 3485 suggestionInfo.mText.replace(0, suggestionInfo.mText.length(), suggestion); 3486 numberOfSuggestions++; 3487 if (numberOfSuggestions >= suggestionInfos.length) { 3488 return numberOfSuggestions; 3489 } 3490 } 3491 } 3492 return numberOfSuggestions; 3493 } 3494 } 3495 3496 @VisibleForTesting 3497 public class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener { 3498 private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE; 3499 3500 // Key of intent extras for inserting new word into user dictionary. 3501 private static final String USER_DICTIONARY_EXTRA_WORD = "word"; 3502 private static final String USER_DICTIONARY_EXTRA_LOCALE = "locale"; 3503 3504 private SuggestionInfo[] mSuggestionInfos; 3505 private int mNumberOfSuggestions; 3506 private boolean mCursorWasVisibleBeforeSuggestions; 3507 private boolean mIsShowingUp = false; 3508 private SuggestionAdapter mSuggestionsAdapter; 3509 private TextAppearanceSpan mHighlightSpan; // TODO: Make mHighlightSpan final. 3510 private TextView mAddToDictionaryButton; 3511 private TextView mDeleteButton; 3512 private ListView mSuggestionListView; 3513 private final SuggestionSpanInfo mMisspelledSpanInfo = new SuggestionSpanInfo(); 3514 private int mContainerMarginWidth; 3515 private int mContainerMarginTop; 3516 private LinearLayout mContainerView; 3517 private Context mContext; // TODO: Make mContext final. 3518 3519 private class CustomPopupWindow extends PopupWindow { 3520 3521 @Override 3522 public void dismiss() { 3523 if (!isShowing()) { 3524 return; 3525 } 3526 super.dismiss(); 3527 getPositionListener().removeSubscriber(SuggestionsPopupWindow.this); 3528 3529 // Safe cast since show() checks that mTextView.getText() is an Editable 3530 ((Spannable) mTextView.getText()).removeSpan(mSuggestionRangeSpan); 3531 3532 mTextView.setCursorVisible(mCursorWasVisibleBeforeSuggestions); 3533 if (hasInsertionController() && !extractedTextModeWillBeStarted()) { 3534 getInsertionController().show(); 3535 } 3536 } 3537 } 3538 3539 public SuggestionsPopupWindow() { 3540 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 3541 } 3542 3543 @Override 3544 protected void setUp() { 3545 mContext = applyDefaultTheme(mTextView.getContext()); 3546 mHighlightSpan = new TextAppearanceSpan(mContext, 3547 mTextView.mTextEditSuggestionHighlightStyle); 3548 } 3549 3550 private Context applyDefaultTheme(Context originalContext) { 3551 TypedArray a = originalContext.obtainStyledAttributes( 3552 new int[]{com.android.internal.R.attr.isLightTheme}); 3553 boolean isLightTheme = a.getBoolean(0, true); 3554 int themeId = isLightTheme ? R.style.ThemeOverlay_Material_Light 3555 : R.style.ThemeOverlay_Material_Dark; 3556 a.recycle(); 3557 return new ContextThemeWrapper(originalContext, themeId); 3558 } 3559 3560 @Override 3561 protected void createPopupWindow() { 3562 mPopupWindow = new CustomPopupWindow(); 3563 mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 3564 mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 3565 mPopupWindow.setFocusable(true); 3566 mPopupWindow.setClippingEnabled(false); 3567 } 3568 3569 @Override 3570 protected void initContentView() { 3571 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( 3572 Context.LAYOUT_INFLATER_SERVICE); 3573 mContentView = (ViewGroup) inflater.inflate( 3574 mTextView.mTextEditSuggestionContainerLayout, null); 3575 3576 mContainerView = (LinearLayout) mContentView.findViewById( 3577 com.android.internal.R.id.suggestionWindowContainer); 3578 ViewGroup.MarginLayoutParams lp = 3579 (ViewGroup.MarginLayoutParams) mContainerView.getLayoutParams(); 3580 mContainerMarginWidth = lp.leftMargin + lp.rightMargin; 3581 mContainerMarginTop = lp.topMargin; 3582 mClippingLimitLeft = lp.leftMargin; 3583 mClippingLimitRight = lp.rightMargin; 3584 3585 mSuggestionListView = (ListView) mContentView.findViewById( 3586 com.android.internal.R.id.suggestionContainer); 3587 3588 mSuggestionsAdapter = new SuggestionAdapter(); 3589 mSuggestionListView.setAdapter(mSuggestionsAdapter); 3590 mSuggestionListView.setOnItemClickListener(this); 3591 3592 // Inflate the suggestion items once and for all. 3593 mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS]; 3594 for (int i = 0; i < mSuggestionInfos.length; i++) { 3595 mSuggestionInfos[i] = new SuggestionInfo(); 3596 } 3597 3598 mAddToDictionaryButton = (TextView) mContentView.findViewById( 3599 com.android.internal.R.id.addToDictionaryButton); 3600 mAddToDictionaryButton.setOnClickListener(new View.OnClickListener() { 3601 public void onClick(View v) { 3602 final SuggestionSpan misspelledSpan = 3603 findEquivalentSuggestionSpan(mMisspelledSpanInfo); 3604 if (misspelledSpan == null) { 3605 // Span has been removed. 3606 return; 3607 } 3608 final Editable editable = (Editable) mTextView.getText(); 3609 final int spanStart = editable.getSpanStart(misspelledSpan); 3610 final int spanEnd = editable.getSpanEnd(misspelledSpan); 3611 if (spanStart < 0 || spanEnd <= spanStart) { 3612 return; 3613 } 3614 final String originalText = TextUtils.substring(editable, spanStart, spanEnd); 3615 3616 final Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT); 3617 intent.putExtra(USER_DICTIONARY_EXTRA_WORD, originalText); 3618 intent.putExtra(USER_DICTIONARY_EXTRA_LOCALE, 3619 mTextView.getTextServicesLocale().toString()); 3620 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); 3621 mTextView.getContext().startActivity(intent); 3622 // There is no way to know if the word was indeed added. Re-check. 3623 // TODO The ExtractEditText should remove the span in the original text instead 3624 editable.removeSpan(mMisspelledSpanInfo.mSuggestionSpan); 3625 Selection.setSelection(editable, spanEnd); 3626 updateSpellCheckSpans(spanStart, spanEnd, false); 3627 hideWithCleanUp(); 3628 } 3629 }); 3630 3631 mDeleteButton = (TextView) mContentView.findViewById( 3632 com.android.internal.R.id.deleteButton); 3633 mDeleteButton.setOnClickListener(new View.OnClickListener() { 3634 public void onClick(View v) { 3635 final Editable editable = (Editable) mTextView.getText(); 3636 3637 final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan); 3638 int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan); 3639 if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) { 3640 // Do not leave two adjacent spaces after deletion, or one at beginning of 3641 // text 3642 if (spanUnionEnd < editable.length() 3643 && Character.isSpaceChar(editable.charAt(spanUnionEnd)) 3644 && (spanUnionStart == 0 3645 || Character.isSpaceChar( 3646 editable.charAt(spanUnionStart - 1)))) { 3647 spanUnionEnd = spanUnionEnd + 1; 3648 } 3649 mTextView.deleteText_internal(spanUnionStart, spanUnionEnd); 3650 } 3651 hideWithCleanUp(); 3652 } 3653 }); 3654 3655 } 3656 3657 public boolean isShowingUp() { 3658 return mIsShowingUp; 3659 } 3660 3661 public void onParentLostFocus() { 3662 mIsShowingUp = false; 3663 } 3664 3665 private class SuggestionAdapter extends BaseAdapter { 3666 private LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService( 3667 Context.LAYOUT_INFLATER_SERVICE); 3668 3669 @Override 3670 public int getCount() { 3671 return mNumberOfSuggestions; 3672 } 3673 3674 @Override 3675 public Object getItem(int position) { 3676 return mSuggestionInfos[position]; 3677 } 3678 3679 @Override 3680 public long getItemId(int position) { 3681 return position; 3682 } 3683 3684 @Override 3685 public View getView(int position, View convertView, ViewGroup parent) { 3686 TextView textView = (TextView) convertView; 3687 3688 if (textView == null) { 3689 textView = (TextView) mInflater.inflate(mTextView.mTextEditSuggestionItemLayout, 3690 parent, false); 3691 } 3692 3693 final SuggestionInfo suggestionInfo = mSuggestionInfos[position]; 3694 textView.setText(suggestionInfo.mText); 3695 return textView; 3696 } 3697 } 3698 3699 @VisibleForTesting 3700 public ViewGroup getContentViewForTesting() { 3701 return mContentView; 3702 } 3703 3704 @Override 3705 public void show() { 3706 if (!(mTextView.getText() instanceof Editable)) return; 3707 if (extractedTextModeWillBeStarted()) { 3708 return; 3709 } 3710 3711 if (updateSuggestions()) { 3712 mCursorWasVisibleBeforeSuggestions = mCursorVisible; 3713 mTextView.setCursorVisible(false); 3714 mIsShowingUp = true; 3715 super.show(); 3716 } 3717 3718 mSuggestionListView.setVisibility(mNumberOfSuggestions == 0 ? View.GONE : View.VISIBLE); 3719 } 3720 3721 @Override 3722 protected void measureContent() { 3723 final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); 3724 final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec( 3725 displayMetrics.widthPixels, View.MeasureSpec.AT_MOST); 3726 final int verticalMeasure = View.MeasureSpec.makeMeasureSpec( 3727 displayMetrics.heightPixels, View.MeasureSpec.AT_MOST); 3728 3729 int width = 0; 3730 View view = null; 3731 for (int i = 0; i < mNumberOfSuggestions; i++) { 3732 view = mSuggestionsAdapter.getView(i, view, mContentView); 3733 view.getLayoutParams().width = LayoutParams.WRAP_CONTENT; 3734 view.measure(horizontalMeasure, verticalMeasure); 3735 width = Math.max(width, view.getMeasuredWidth()); 3736 } 3737 3738 if (mAddToDictionaryButton.getVisibility() != View.GONE) { 3739 mAddToDictionaryButton.measure(horizontalMeasure, verticalMeasure); 3740 width = Math.max(width, mAddToDictionaryButton.getMeasuredWidth()); 3741 } 3742 3743 mDeleteButton.measure(horizontalMeasure, verticalMeasure); 3744 width = Math.max(width, mDeleteButton.getMeasuredWidth()); 3745 3746 width += mContainerView.getPaddingLeft() + mContainerView.getPaddingRight() 3747 + mContainerMarginWidth; 3748 3749 // Enforce the width based on actual text widths 3750 mContentView.measure( 3751 View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), 3752 verticalMeasure); 3753 3754 Drawable popupBackground = mPopupWindow.getBackground(); 3755 if (popupBackground != null) { 3756 if (mTempRect == null) mTempRect = new Rect(); 3757 popupBackground.getPadding(mTempRect); 3758 width += mTempRect.left + mTempRect.right; 3759 } 3760 mPopupWindow.setWidth(width); 3761 } 3762 3763 @Override 3764 protected int getTextOffset() { 3765 return (mTextView.getSelectionStart() + mTextView.getSelectionStart()) / 2; 3766 } 3767 3768 @Override 3769 protected int getVerticalLocalPosition(int line) { 3770 final Layout layout = mTextView.getLayout(); 3771 return layout.getLineBottomWithoutSpacing(line) - mContainerMarginTop; 3772 } 3773 3774 @Override 3775 protected int clipVertically(int positionY) { 3776 final int height = mContentView.getMeasuredHeight(); 3777 final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics(); 3778 return Math.min(positionY, displayMetrics.heightPixels - height); 3779 } 3780 3781 private void hideWithCleanUp() { 3782 for (final SuggestionInfo info : mSuggestionInfos) { 3783 info.clear(); 3784 } 3785 mMisspelledSpanInfo.clear(); 3786 hide(); 3787 } 3788 3789 private boolean updateSuggestions() { 3790 Spannable spannable = (Spannable) mTextView.getText(); 3791 mNumberOfSuggestions = 3792 mSuggestionHelper.getSuggestionInfo(mSuggestionInfos, mMisspelledSpanInfo); 3793 if (mNumberOfSuggestions == 0 && mMisspelledSpanInfo.mSuggestionSpan == null) { 3794 return false; 3795 } 3796 3797 int spanUnionStart = mTextView.getText().length(); 3798 int spanUnionEnd = 0; 3799 3800 for (int i = 0; i < mNumberOfSuggestions; i++) { 3801 final SuggestionSpanInfo spanInfo = mSuggestionInfos[i].mSuggestionSpanInfo; 3802 spanUnionStart = Math.min(spanUnionStart, spanInfo.mSpanStart); 3803 spanUnionEnd = Math.max(spanUnionEnd, spanInfo.mSpanEnd); 3804 } 3805 if (mMisspelledSpanInfo.mSuggestionSpan != null) { 3806 spanUnionStart = Math.min(spanUnionStart, mMisspelledSpanInfo.mSpanStart); 3807 spanUnionEnd = Math.max(spanUnionEnd, mMisspelledSpanInfo.mSpanEnd); 3808 } 3809 3810 for (int i = 0; i < mNumberOfSuggestions; i++) { 3811 highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd); 3812 } 3813 3814 // Make "Add to dictionary" item visible if there is a span with the misspelled flag 3815 int addToDictionaryButtonVisibility = View.GONE; 3816 if (mMisspelledSpanInfo.mSuggestionSpan != null) { 3817 if (mMisspelledSpanInfo.mSpanStart >= 0 3818 && mMisspelledSpanInfo.mSpanEnd > mMisspelledSpanInfo.mSpanStart) { 3819 addToDictionaryButtonVisibility = View.VISIBLE; 3820 } 3821 } 3822 mAddToDictionaryButton.setVisibility(addToDictionaryButtonVisibility); 3823 3824 if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan(); 3825 final int underlineColor; 3826 if (mNumberOfSuggestions != 0) { 3827 underlineColor = 3828 mSuggestionInfos[0].mSuggestionSpanInfo.mSuggestionSpan.getUnderlineColor(); 3829 } else { 3830 underlineColor = mMisspelledSpanInfo.mSuggestionSpan.getUnderlineColor(); 3831 } 3832 3833 if (underlineColor == 0) { 3834 // Fallback on the default highlight color when the first span does not provide one 3835 mSuggestionRangeSpan.setBackgroundColor(mTextView.mHighlightColor); 3836 } else { 3837 final float BACKGROUND_TRANSPARENCY = 0.4f; 3838 final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY); 3839 mSuggestionRangeSpan.setBackgroundColor( 3840 (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); 3841 } 3842 spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, 3843 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 3844 3845 mSuggestionsAdapter.notifyDataSetChanged(); 3846 return true; 3847 } 3848 3849 private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart, 3850 int unionEnd) { 3851 final Spannable text = (Spannable) mTextView.getText(); 3852 final int spanStart = suggestionInfo.mSuggestionSpanInfo.mSpanStart; 3853 final int spanEnd = suggestionInfo.mSuggestionSpanInfo.mSpanEnd; 3854 3855 // Adjust the start/end of the suggestion span 3856 suggestionInfo.mSuggestionStart = spanStart - unionStart; 3857 suggestionInfo.mSuggestionEnd = suggestionInfo.mSuggestionStart 3858 + suggestionInfo.mText.length(); 3859 3860 suggestionInfo.mText.setSpan(mHighlightSpan, 0, suggestionInfo.mText.length(), 3861 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 3862 3863 // Add the text before and after the span. 3864 final String textAsString = text.toString(); 3865 suggestionInfo.mText.insert(0, textAsString.substring(unionStart, spanStart)); 3866 suggestionInfo.mText.append(textAsString.substring(spanEnd, unionEnd)); 3867 } 3868 3869 @Override 3870 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 3871 SuggestionInfo suggestionInfo = mSuggestionInfos[position]; 3872 replaceWithSuggestion(suggestionInfo); 3873 hideWithCleanUp(); 3874 } 3875 } 3876 3877 /** 3878 * An ActionMode Callback class that is used to provide actions while in text insertion or 3879 * selection mode. 3880 * 3881 * The default callback provides a subset of Select All, Cut, Copy, Paste, Share and Replace 3882 * actions, depending on which of these this TextView supports and the current selection. 3883 */ 3884 private class TextActionModeCallback extends ActionMode.Callback2 { 3885 private final Path mSelectionPath = new Path(); 3886 private final RectF mSelectionBounds = new RectF(); 3887 private final boolean mHasSelection; 3888 private final int mHandleHeight; 3889 private final Map<MenuItem, OnClickListener> mAssistClickHandlers = new HashMap<>(); 3890 3891 TextActionModeCallback(@TextActionMode int mode) { 3892 mHasSelection = mode == TextActionMode.SELECTION 3893 || (mTextIsSelectable && mode == TextActionMode.TEXT_LINK); 3894 if (mHasSelection) { 3895 SelectionModifierCursorController selectionController = getSelectionController(); 3896 if (selectionController.mStartHandle == null) { 3897 // As these are for initializing selectionController, hide() must be called. 3898 selectionController.initDrawables(); 3899 selectionController.initHandles(); 3900 selectionController.hide(); 3901 } 3902 mHandleHeight = Math.max( 3903 mSelectHandleLeft.getMinimumHeight(), 3904 mSelectHandleRight.getMinimumHeight()); 3905 } else { 3906 InsertionPointCursorController insertionController = getInsertionController(); 3907 if (insertionController != null) { 3908 insertionController.getHandle(); 3909 mHandleHeight = mSelectHandleCenter.getMinimumHeight(); 3910 } else { 3911 mHandleHeight = 0; 3912 } 3913 } 3914 } 3915 3916 @Override 3917 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 3918 mAssistClickHandlers.clear(); 3919 3920 mode.setTitle(null); 3921 mode.setSubtitle(null); 3922 mode.setTitleOptionalHint(true); 3923 populateMenuWithItems(menu); 3924 3925 Callback customCallback = getCustomCallback(); 3926 if (customCallback != null) { 3927 if (!customCallback.onCreateActionMode(mode, menu)) { 3928 // The custom mode can choose to cancel the action mode, dismiss selection. 3929 Selection.setSelection((Spannable) mTextView.getText(), 3930 mTextView.getSelectionEnd()); 3931 return false; 3932 } 3933 } 3934 3935 if (mTextView.canProcessText()) { 3936 mProcessTextIntentActionsHandler.onInitializeMenu(menu); 3937 } 3938 3939 if (mHasSelection && !mTextView.hasTransientState()) { 3940 mTextView.setHasTransientState(true); 3941 } 3942 return true; 3943 } 3944 3945 private Callback getCustomCallback() { 3946 return mHasSelection 3947 ? mCustomSelectionActionModeCallback 3948 : mCustomInsertionActionModeCallback; 3949 } 3950 3951 private void populateMenuWithItems(Menu menu) { 3952 if (mTextView.canCut()) { 3953 menu.add(Menu.NONE, TextView.ID_CUT, MENU_ITEM_ORDER_CUT, 3954 com.android.internal.R.string.cut) 3955 .setAlphabeticShortcut('x') 3956 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 3957 } 3958 3959 if (mTextView.canCopy()) { 3960 menu.add(Menu.NONE, TextView.ID_COPY, MENU_ITEM_ORDER_COPY, 3961 com.android.internal.R.string.copy) 3962 .setAlphabeticShortcut('c') 3963 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 3964 } 3965 3966 if (mTextView.canPaste()) { 3967 menu.add(Menu.NONE, TextView.ID_PASTE, MENU_ITEM_ORDER_PASTE, 3968 com.android.internal.R.string.paste) 3969 .setAlphabeticShortcut('v') 3970 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 3971 } 3972 3973 if (mTextView.canShare()) { 3974 menu.add(Menu.NONE, TextView.ID_SHARE, MENU_ITEM_ORDER_SHARE, 3975 com.android.internal.R.string.share) 3976 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 3977 } 3978 3979 if (mTextView.canRequestAutofill()) { 3980 final String selected = mTextView.getSelectedText(); 3981 if (selected == null || selected.isEmpty()) { 3982 menu.add(Menu.NONE, TextView.ID_AUTOFILL, MENU_ITEM_ORDER_AUTOFILL, 3983 com.android.internal.R.string.autofill) 3984 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 3985 } 3986 } 3987 3988 if (mTextView.canPasteAsPlainText()) { 3989 menu.add( 3990 Menu.NONE, 3991 TextView.ID_PASTE_AS_PLAIN_TEXT, 3992 MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT, 3993 com.android.internal.R.string.paste_as_plain_text) 3994 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 3995 } 3996 3997 updateSelectAllItem(menu); 3998 updateReplaceItem(menu); 3999 updateAssistMenuItems(menu); 4000 } 4001 4002 @Override 4003 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 4004 updateSelectAllItem(menu); 4005 updateReplaceItem(menu); 4006 updateAssistMenuItems(menu); 4007 4008 Callback customCallback = getCustomCallback(); 4009 if (customCallback != null) { 4010 return customCallback.onPrepareActionMode(mode, menu); 4011 } 4012 return true; 4013 } 4014 4015 private void updateSelectAllItem(Menu menu) { 4016 boolean canSelectAll = mTextView.canSelectAllText(); 4017 boolean selectAllItemExists = menu.findItem(TextView.ID_SELECT_ALL) != null; 4018 if (canSelectAll && !selectAllItemExists) { 4019 menu.add(Menu.NONE, TextView.ID_SELECT_ALL, MENU_ITEM_ORDER_SELECT_ALL, 4020 com.android.internal.R.string.selectAll) 4021 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 4022 } else if (!canSelectAll && selectAllItemExists) { 4023 menu.removeItem(TextView.ID_SELECT_ALL); 4024 } 4025 } 4026 4027 private void updateReplaceItem(Menu menu) { 4028 boolean canReplace = mTextView.isSuggestionsEnabled() && shouldOfferToShowSuggestions(); 4029 boolean replaceItemExists = menu.findItem(TextView.ID_REPLACE) != null; 4030 if (canReplace && !replaceItemExists) { 4031 menu.add(Menu.NONE, TextView.ID_REPLACE, MENU_ITEM_ORDER_REPLACE, 4032 com.android.internal.R.string.replace) 4033 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 4034 } else if (!canReplace && replaceItemExists) { 4035 menu.removeItem(TextView.ID_REPLACE); 4036 } 4037 } 4038 4039 private void updateAssistMenuItems(Menu menu) { 4040 clearAssistMenuItems(menu); 4041 if (!shouldEnableAssistMenuItems()) { 4042 return; 4043 } 4044 final TextClassification textClassification = 4045 getSelectionActionModeHelper().getTextClassification(); 4046 if (textClassification == null) { 4047 return; 4048 } 4049 if (!textClassification.getActions().isEmpty()) { 4050 // Primary assist action (Always shown). 4051 final MenuItem item = addAssistMenuItem(menu, 4052 textClassification.getActions().get(0), TextView.ID_ASSIST, 4053 MENU_ITEM_ORDER_ASSIST, MenuItem.SHOW_AS_ACTION_ALWAYS); 4054 item.setIntent(textClassification.getIntent()); 4055 } else if (hasLegacyAssistItem(textClassification)) { 4056 // Legacy primary assist action (Always shown). 4057 final MenuItem item = menu.add( 4058 TextView.ID_ASSIST, TextView.ID_ASSIST, MENU_ITEM_ORDER_ASSIST, 4059 textClassification.getLabel()) 4060 .setIcon(textClassification.getIcon()) 4061 .setIntent(textClassification.getIntent()); 4062 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 4063 mAssistClickHandlers.put(item, TextClassification.createIntentOnClickListener( 4064 TextClassification.createPendingIntent(mTextView.getContext(), 4065 textClassification.getIntent()))); 4066 } 4067 final int count = textClassification.getActions().size(); 4068 for (int i = 1; i < count; i++) { 4069 // Secondary assist action (Never shown). 4070 addAssistMenuItem(menu, textClassification.getActions().get(i), Menu.NONE, 4071 MENU_ITEM_ORDER_SECONDARY_ASSIST_ACTIONS_START + i - 1, 4072 MenuItem.SHOW_AS_ACTION_NEVER); 4073 } 4074 } 4075 4076 private MenuItem addAssistMenuItem(Menu menu, RemoteAction action, int intemId, int order, 4077 int showAsAction) { 4078 final MenuItem item = menu.add(TextView.ID_ASSIST, intemId, order, action.getTitle()) 4079 .setContentDescription(action.getContentDescription()); 4080 if (action.shouldShowIcon()) { 4081 item.setIcon(action.getIcon().loadDrawable(mTextView.getContext())); 4082 } 4083 item.setShowAsAction(showAsAction); 4084 mAssistClickHandlers.put(item, 4085 TextClassification.createIntentOnClickListener(action.getActionIntent())); 4086 return item; 4087 } 4088 4089 private void clearAssistMenuItems(Menu menu) { 4090 int i = 0; 4091 while (i < menu.size()) { 4092 final MenuItem menuItem = menu.getItem(i); 4093 if (menuItem.getGroupId() == TextView.ID_ASSIST) { 4094 menu.removeItem(menuItem.getItemId()); 4095 continue; 4096 } 4097 i++; 4098 } 4099 } 4100 4101 private boolean hasLegacyAssistItem(TextClassification classification) { 4102 // Check whether we have the UI data and and action. 4103 return (classification.getIcon() != null || !TextUtils.isEmpty( 4104 classification.getLabel())) && (classification.getIntent() != null 4105 || classification.getOnClickListener() != null); 4106 } 4107 4108 private boolean onAssistMenuItemClicked(MenuItem assistMenuItem) { 4109 Preconditions.checkArgument(assistMenuItem.getGroupId() == TextView.ID_ASSIST); 4110 4111 final TextClassification textClassification = 4112 getSelectionActionModeHelper().getTextClassification(); 4113 if (!shouldEnableAssistMenuItems() || textClassification == null) { 4114 // No textClassification result to handle the click. Eat the click. 4115 return true; 4116 } 4117 4118 OnClickListener onClickListener = mAssistClickHandlers.get(assistMenuItem); 4119 if (onClickListener == null) { 4120 final Intent intent = assistMenuItem.getIntent(); 4121 if (intent != null) { 4122 onClickListener = TextClassification.createIntentOnClickListener( 4123 TextClassification.createPendingIntent(mTextView.getContext(), intent)); 4124 } 4125 } 4126 if (onClickListener != null) { 4127 onClickListener.onClick(mTextView); 4128 stopTextActionMode(); 4129 } 4130 // We tried our best. 4131 return true; 4132 } 4133 4134 private boolean shouldEnableAssistMenuItems() { 4135 return mTextView.isDeviceProvisioned() 4136 && TextClassificationManager.getSettings(mTextView.getContext()) 4137 .isSmartTextShareEnabled(); 4138 } 4139 4140 @Override 4141 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 4142 getSelectionActionModeHelper().onSelectionAction(item.getItemId()); 4143 4144 if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) { 4145 return true; 4146 } 4147 Callback customCallback = getCustomCallback(); 4148 if (customCallback != null && customCallback.onActionItemClicked(mode, item)) { 4149 return true; 4150 } 4151 if (item.getGroupId() == TextView.ID_ASSIST && onAssistMenuItemClicked(item)) { 4152 return true; 4153 } 4154 return mTextView.onTextContextMenuItem(item.getItemId()); 4155 } 4156 4157 @Override 4158 public void onDestroyActionMode(ActionMode mode) { 4159 // Clear mTextActionMode not to recursively destroy action mode by clearing selection. 4160 getSelectionActionModeHelper().onDestroyActionMode(); 4161 mTextActionMode = null; 4162 Callback customCallback = getCustomCallback(); 4163 if (customCallback != null) { 4164 customCallback.onDestroyActionMode(mode); 4165 } 4166 4167 if (!mPreserveSelection) { 4168 /* 4169 * Leave current selection when we tentatively destroy action mode for the 4170 * selection. If we're detaching from a window, we'll bring back the selection 4171 * mode when (if) we get reattached. 4172 */ 4173 Selection.setSelection((Spannable) mTextView.getText(), 4174 mTextView.getSelectionEnd()); 4175 } 4176 4177 if (mSelectionModifierCursorController != null) { 4178 mSelectionModifierCursorController.hide(); 4179 } 4180 4181 mAssistClickHandlers.clear(); 4182 mRequestingLinkActionMode = false; 4183 } 4184 4185 @Override 4186 public void onGetContentRect(ActionMode mode, View view, Rect outRect) { 4187 if (!view.equals(mTextView) || mTextView.getLayout() == null) { 4188 super.onGetContentRect(mode, view, outRect); 4189 return; 4190 } 4191 if (mTextView.getSelectionStart() != mTextView.getSelectionEnd()) { 4192 // We have a selection. 4193 mSelectionPath.reset(); 4194 mTextView.getLayout().getSelectionPath( 4195 mTextView.getSelectionStart(), mTextView.getSelectionEnd(), mSelectionPath); 4196 mSelectionPath.computeBounds(mSelectionBounds, true); 4197 mSelectionBounds.bottom += mHandleHeight; 4198 } else { 4199 // We have a cursor. 4200 Layout layout = mTextView.getLayout(); 4201 int line = layout.getLineForOffset(mTextView.getSelectionStart()); 4202 float primaryHorizontal = clampHorizontalPosition(null, 4203 layout.getPrimaryHorizontal(mTextView.getSelectionStart())); 4204 mSelectionBounds.set( 4205 primaryHorizontal, 4206 layout.getLineTop(line), 4207 primaryHorizontal, 4208 layout.getLineBottom(line) + mHandleHeight); 4209 } 4210 // Take TextView's padding and scroll into account. 4211 int textHorizontalOffset = mTextView.viewportToContentHorizontalOffset(); 4212 int textVerticalOffset = mTextView.viewportToContentVerticalOffset(); 4213 outRect.set( 4214 (int) Math.floor(mSelectionBounds.left + textHorizontalOffset), 4215 (int) Math.floor(mSelectionBounds.top + textVerticalOffset), 4216 (int) Math.ceil(mSelectionBounds.right + textHorizontalOffset), 4217 (int) Math.ceil(mSelectionBounds.bottom + textVerticalOffset)); 4218 } 4219 } 4220 4221 /** 4222 * A listener to call {@link InputMethodManager#updateCursorAnchorInfo(View, CursorAnchorInfo)} 4223 * while the input method is requesting the cursor/anchor position. Does nothing as long as 4224 * {@link InputMethodManager#isWatchingCursor(View)} returns false. 4225 */ 4226 private final class CursorAnchorInfoNotifier implements TextViewPositionListener { 4227 final CursorAnchorInfo.Builder mSelectionInfoBuilder = new CursorAnchorInfo.Builder(); 4228 final int[] mTmpIntOffset = new int[2]; 4229 final Matrix mViewToScreenMatrix = new Matrix(); 4230 4231 @Override 4232 public void updatePosition(int parentPositionX, int parentPositionY, 4233 boolean parentPositionChanged, boolean parentScrolled) { 4234 final InputMethodState ims = mInputMethodState; 4235 if (ims == null || ims.mBatchEditNesting > 0) { 4236 return; 4237 } 4238 final InputMethodManager imm = InputMethodManager.peekInstance(); 4239 if (null == imm) { 4240 return; 4241 } 4242 if (!imm.isActive(mTextView)) { 4243 return; 4244 } 4245 // Skip if the IME has not requested the cursor/anchor position. 4246 if (!imm.isCursorAnchorInfoEnabled()) { 4247 return; 4248 } 4249 Layout layout = mTextView.getLayout(); 4250 if (layout == null) { 4251 return; 4252 } 4253 4254 final CursorAnchorInfo.Builder builder = mSelectionInfoBuilder; 4255 builder.reset(); 4256 4257 final int selectionStart = mTextView.getSelectionStart(); 4258 builder.setSelectionRange(selectionStart, mTextView.getSelectionEnd()); 4259 4260 // Construct transformation matrix from view local coordinates to screen coordinates. 4261 mViewToScreenMatrix.set(mTextView.getMatrix()); 4262 mTextView.getLocationOnScreen(mTmpIntOffset); 4263 mViewToScreenMatrix.postTranslate(mTmpIntOffset[0], mTmpIntOffset[1]); 4264 builder.setMatrix(mViewToScreenMatrix); 4265 4266 final float viewportToContentHorizontalOffset = 4267 mTextView.viewportToContentHorizontalOffset(); 4268 final float viewportToContentVerticalOffset = 4269 mTextView.viewportToContentVerticalOffset(); 4270 4271 final CharSequence text = mTextView.getText(); 4272 if (text instanceof Spannable) { 4273 final Spannable sp = (Spannable) text; 4274 int composingTextStart = EditableInputConnection.getComposingSpanStart(sp); 4275 int composingTextEnd = EditableInputConnection.getComposingSpanEnd(sp); 4276 if (composingTextEnd < composingTextStart) { 4277 final int temp = composingTextEnd; 4278 composingTextEnd = composingTextStart; 4279 composingTextStart = temp; 4280 } 4281 final boolean hasComposingText = 4282 (0 <= composingTextStart) && (composingTextStart < composingTextEnd); 4283 if (hasComposingText) { 4284 final CharSequence composingText = text.subSequence(composingTextStart, 4285 composingTextEnd); 4286 builder.setComposingText(composingTextStart, composingText); 4287 mTextView.populateCharacterBounds(builder, composingTextStart, 4288 composingTextEnd, viewportToContentHorizontalOffset, 4289 viewportToContentVerticalOffset); 4290 } 4291 } 4292 4293 // Treat selectionStart as the insertion point. 4294 if (0 <= selectionStart) { 4295 final int offset = selectionStart; 4296 final int line = layout.getLineForOffset(offset); 4297 final float insertionMarkerX = layout.getPrimaryHorizontal(offset) 4298 + viewportToContentHorizontalOffset; 4299 final float insertionMarkerTop = layout.getLineTop(line) 4300 + viewportToContentVerticalOffset; 4301 final float insertionMarkerBaseline = layout.getLineBaseline(line) 4302 + viewportToContentVerticalOffset; 4303 final float insertionMarkerBottom = layout.getLineBottomWithoutSpacing(line) 4304 + viewportToContentVerticalOffset; 4305 final boolean isTopVisible = mTextView 4306 .isPositionVisible(insertionMarkerX, insertionMarkerTop); 4307 final boolean isBottomVisible = mTextView 4308 .isPositionVisible(insertionMarkerX, insertionMarkerBottom); 4309 int insertionMarkerFlags = 0; 4310 if (isTopVisible || isBottomVisible) { 4311 insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; 4312 } 4313 if (!isTopVisible || !isBottomVisible) { 4314 insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; 4315 } 4316 if (layout.isRtlCharAt(offset)) { 4317 insertionMarkerFlags |= CursorAnchorInfo.FLAG_IS_RTL; 4318 } 4319 builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop, 4320 insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags); 4321 } 4322 4323 imm.updateCursorAnchorInfo(mTextView, builder.build()); 4324 } 4325 } 4326 4327 private static class MagnifierMotionAnimator { 4328 private static final long DURATION = 100 /* miliseconds */; 4329 4330 // The magnifier being animated. 4331 private final Magnifier mMagnifier; 4332 // A value animator used to animate the magnifier. 4333 private final ValueAnimator mAnimator; 4334 4335 // Whether the magnifier is currently visible. 4336 private boolean mMagnifierIsShowing; 4337 // The coordinates of the magnifier when the currently running animation started. 4338 private float mAnimationStartX; 4339 private float mAnimationStartY; 4340 // The coordinates of the magnifier in the latest animation frame. 4341 private float mAnimationCurrentX; 4342 private float mAnimationCurrentY; 4343 // The latest coordinates the motion animator was asked to #show() the magnifier at. 4344 private float mLastX; 4345 private float mLastY; 4346 4347 private MagnifierMotionAnimator(final Magnifier magnifier) { 4348 mMagnifier = magnifier; 4349 // Prepare the animator used to run the motion animation. 4350 mAnimator = ValueAnimator.ofFloat(0, 1); 4351 mAnimator.setDuration(DURATION); 4352 mAnimator.setInterpolator(new LinearInterpolator()); 4353 mAnimator.addUpdateListener((animation) -> { 4354 // Interpolate to find the current position of the magnifier. 4355 mAnimationCurrentX = mAnimationStartX 4356 + (mLastX - mAnimationStartX) * animation.getAnimatedFraction(); 4357 mAnimationCurrentY = mAnimationStartY 4358 + (mLastY - mAnimationStartY) * animation.getAnimatedFraction(); 4359 mMagnifier.show(mAnimationCurrentX, mAnimationCurrentY); 4360 }); 4361 } 4362 4363 /** 4364 * Shows the magnifier at a new position. 4365 * If the y coordinate is different from the previous y coordinate 4366 * (probably corresponding to a line jump in the text), a short 4367 * animation is added to the jump. 4368 */ 4369 private void show(final float x, final float y) { 4370 final boolean startNewAnimation = mMagnifierIsShowing && y != mLastY; 4371 4372 if (startNewAnimation) { 4373 if (mAnimator.isRunning()) { 4374 mAnimator.cancel(); 4375 mAnimationStartX = mAnimationCurrentX; 4376 mAnimationStartY = mAnimationCurrentY; 4377 } else { 4378 mAnimationStartX = mLastX; 4379 mAnimationStartY = mLastY; 4380 } 4381 mAnimator.start(); 4382 } else { 4383 if (!mAnimator.isRunning()) { 4384 mMagnifier.show(x, y); 4385 } 4386 } 4387 mLastX = x; 4388 mLastY = y; 4389 mMagnifierIsShowing = true; 4390 } 4391 4392 /** 4393 * Updates the content of the magnifier. 4394 */ 4395 private void update() { 4396 mMagnifier.update(); 4397 } 4398 4399 /** 4400 * Dismisses the magnifier, or does nothing if it is already dismissed. 4401 */ 4402 private void dismiss() { 4403 mMagnifier.dismiss(); 4404 mAnimator.cancel(); 4405 mMagnifierIsShowing = false; 4406 } 4407 } 4408 4409 @VisibleForTesting 4410 public abstract class HandleView extends View implements TextViewPositionListener { 4411 protected Drawable mDrawable; 4412 protected Drawable mDrawableLtr; 4413 protected Drawable mDrawableRtl; 4414 private final PopupWindow mContainer; 4415 // Position with respect to the parent TextView 4416 private int mPositionX, mPositionY; 4417 private boolean mIsDragging; 4418 // Offset from touch position to mPosition 4419 private float mTouchToWindowOffsetX, mTouchToWindowOffsetY; 4420 protected int mHotspotX; 4421 protected int mHorizontalGravity; 4422 // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up 4423 private float mTouchOffsetY; 4424 // Where the touch position should be on the handle to ensure a maximum cursor visibility 4425 private float mIdealVerticalOffset; 4426 // Parent's (TextView) previous position in window 4427 private int mLastParentX, mLastParentY; 4428 // Parent's (TextView) previous position on screen 4429 private int mLastParentXOnScreen, mLastParentYOnScreen; 4430 // Previous text character offset 4431 protected int mPreviousOffset = -1; 4432 // Previous text character offset 4433 private boolean mPositionHasChanged = true; 4434 // Minimum touch target size for handles 4435 private int mMinSize; 4436 // Indicates the line of text that the handle is on. 4437 protected int mPrevLine = UNSET_LINE; 4438 // Indicates the line of text that the user was touching. This can differ from mPrevLine 4439 // when selecting text when the handles jump to the end / start of words which may be on 4440 // a different line. 4441 protected int mPreviousLineTouched = UNSET_LINE; 4442 4443 private HandleView(Drawable drawableLtr, Drawable drawableRtl, final int id) { 4444 super(mTextView.getContext()); 4445 setId(id); 4446 mContainer = new PopupWindow(mTextView.getContext(), null, 4447 com.android.internal.R.attr.textSelectHandleWindowStyle); 4448 mContainer.setSplitTouchEnabled(true); 4449 mContainer.setClippingEnabled(false); 4450 mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 4451 mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 4452 mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 4453 mContainer.setContentView(this); 4454 4455 mDrawableLtr = drawableLtr; 4456 mDrawableRtl = drawableRtl; 4457 mMinSize = mTextView.getContext().getResources().getDimensionPixelSize( 4458 com.android.internal.R.dimen.text_handle_min_size); 4459 4460 updateDrawable(); 4461 4462 final int handleHeight = getPreferredHeight(); 4463 mTouchOffsetY = -0.3f * handleHeight; 4464 mIdealVerticalOffset = 0.7f * handleHeight; 4465 } 4466 4467 public float getIdealVerticalOffset() { 4468 return mIdealVerticalOffset; 4469 } 4470 4471 protected void updateDrawable() { 4472 if (mIsDragging) { 4473 // Don't update drawable during dragging. 4474 return; 4475 } 4476 final Layout layout = mTextView.getLayout(); 4477 if (layout == null) { 4478 return; 4479 } 4480 final int offset = getCurrentCursorOffset(); 4481 final boolean isRtlCharAtOffset = isAtRtlRun(layout, offset); 4482 final Drawable oldDrawable = mDrawable; 4483 mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr; 4484 mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset); 4485 mHorizontalGravity = getHorizontalGravity(isRtlCharAtOffset); 4486 if (oldDrawable != mDrawable && isShowing()) { 4487 // Update popup window position. 4488 mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX 4489 - getHorizontalOffset() + getCursorOffset(); 4490 mPositionX += mTextView.viewportToContentHorizontalOffset(); 4491 mPositionHasChanged = true; 4492 updatePosition(mLastParentX, mLastParentY, false, false); 4493 postInvalidate(); 4494 } 4495 } 4496 4497 protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun); 4498 protected abstract int getHorizontalGravity(boolean isRtlRun); 4499 4500 // Touch-up filter: number of previous positions remembered 4501 private static final int HISTORY_SIZE = 5; 4502 private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150; 4503 private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350; 4504 private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE]; 4505 private final int[] mPreviousOffsets = new int[HISTORY_SIZE]; 4506 private int mPreviousOffsetIndex = 0; 4507 private int mNumberPreviousOffsets = 0; 4508 4509 private void startTouchUpFilter(int offset) { 4510 mNumberPreviousOffsets = 0; 4511 addPositionToTouchUpFilter(offset); 4512 } 4513 4514 private void addPositionToTouchUpFilter(int offset) { 4515 mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE; 4516 mPreviousOffsets[mPreviousOffsetIndex] = offset; 4517 mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis(); 4518 mNumberPreviousOffsets++; 4519 } 4520 4521 private void filterOnTouchUp(boolean fromTouchScreen) { 4522 final long now = SystemClock.uptimeMillis(); 4523 int i = 0; 4524 int index = mPreviousOffsetIndex; 4525 final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE); 4526 while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) { 4527 i++; 4528 index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE; 4529 } 4530 4531 if (i > 0 && i < iMax 4532 && (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) { 4533 positionAtCursorOffset(mPreviousOffsets[index], false, fromTouchScreen); 4534 } 4535 } 4536 4537 public boolean offsetHasBeenChanged() { 4538 return mNumberPreviousOffsets > 1; 4539 } 4540 4541 @Override 4542 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 4543 setMeasuredDimension(getPreferredWidth(), getPreferredHeight()); 4544 } 4545 4546 @Override 4547 public void invalidate() { 4548 super.invalidate(); 4549 if (isShowing()) { 4550 positionAtCursorOffset(getCurrentCursorOffset(), true, false); 4551 } 4552 }; 4553 4554 private int getPreferredWidth() { 4555 return Math.max(mDrawable.getIntrinsicWidth(), mMinSize); 4556 } 4557 4558 private int getPreferredHeight() { 4559 return Math.max(mDrawable.getIntrinsicHeight(), mMinSize); 4560 } 4561 4562 public void show() { 4563 if (isShowing()) return; 4564 4565 getPositionListener().addSubscriber(this, true /* local position may change */); 4566 4567 // Make sure the offset is always considered new, even when focusing at same position 4568 mPreviousOffset = -1; 4569 positionAtCursorOffset(getCurrentCursorOffset(), false, false); 4570 } 4571 4572 protected void dismiss() { 4573 mIsDragging = false; 4574 mContainer.dismiss(); 4575 onDetached(); 4576 } 4577 4578 public void hide() { 4579 dismiss(); 4580 4581 getPositionListener().removeSubscriber(this); 4582 } 4583 4584 public boolean isShowing() { 4585 return mContainer.isShowing(); 4586 } 4587 4588 private boolean shouldShow() { 4589 // A dragging handle should always be shown. 4590 if (mIsDragging) { 4591 return true; 4592 } 4593 4594 if (mTextView.isInBatchEditMode()) { 4595 return false; 4596 } 4597 4598 return mTextView.isPositionVisible( 4599 mPositionX + mHotspotX + getHorizontalOffset(), mPositionY); 4600 } 4601 4602 private void setVisible(final boolean visible) { 4603 mContainer.getContentView().setVisibility(visible ? VISIBLE : INVISIBLE); 4604 } 4605 4606 public abstract int getCurrentCursorOffset(); 4607 4608 protected abstract void updateSelection(int offset); 4609 4610 protected abstract void updatePosition(float x, float y, boolean fromTouchScreen); 4611 4612 @MagnifierHandleTrigger 4613 protected abstract int getMagnifierHandleTrigger(); 4614 4615 protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { 4616 return layout.isRtlCharAt(offset); 4617 } 4618 4619 @VisibleForTesting 4620 public float getHorizontal(@NonNull Layout layout, int offset) { 4621 return layout.getPrimaryHorizontal(offset); 4622 } 4623 4624 protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { 4625 return mTextView.getOffsetAtCoordinate(line, x); 4626 } 4627 4628 /** 4629 * @param offset Cursor offset. Must be in [-1, length]. 4630 * @param forceUpdatePosition whether to force update the position. This should be true 4631 * when If the parent has been scrolled, for example. 4632 * @param fromTouchScreen {@code true} if the cursor is moved with motion events from the 4633 * touch screen. 4634 */ 4635 protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition, 4636 boolean fromTouchScreen) { 4637 // A HandleView relies on the layout, which may be nulled by external methods 4638 Layout layout = mTextView.getLayout(); 4639 if (layout == null) { 4640 // Will update controllers' state, hiding them and stopping selection mode if needed 4641 prepareCursorControllers(); 4642 return; 4643 } 4644 layout = mTextView.getLayout(); 4645 4646 boolean offsetChanged = offset != mPreviousOffset; 4647 if (offsetChanged || forceUpdatePosition) { 4648 if (offsetChanged) { 4649 updateSelection(offset); 4650 if (fromTouchScreen && mHapticTextHandleEnabled) { 4651 mTextView.performHapticFeedback(HapticFeedbackConstants.TEXT_HANDLE_MOVE); 4652 } 4653 addPositionToTouchUpFilter(offset); 4654 } 4655 final int line = layout.getLineForOffset(offset); 4656 mPrevLine = line; 4657 4658 mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX 4659 - getHorizontalOffset() + getCursorOffset(); 4660 mPositionY = layout.getLineBottomWithoutSpacing(line); 4661 4662 // Take TextView's padding and scroll into account. 4663 mPositionX += mTextView.viewportToContentHorizontalOffset(); 4664 mPositionY += mTextView.viewportToContentVerticalOffset(); 4665 4666 mPreviousOffset = offset; 4667 mPositionHasChanged = true; 4668 } 4669 } 4670 4671 /** 4672 * Return the clamped horizontal position for the cursor. 4673 * 4674 * @param layout Text layout. 4675 * @param offset Character offset for the cursor. 4676 * @return The clamped horizontal position for the cursor. 4677 */ 4678 int getCursorHorizontalPosition(Layout layout, int offset) { 4679 return (int) (getHorizontal(layout, offset) - 0.5f); 4680 } 4681 4682 @Override 4683 public void updatePosition(int parentPositionX, int parentPositionY, 4684 boolean parentPositionChanged, boolean parentScrolled) { 4685 positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled, false); 4686 if (parentPositionChanged || mPositionHasChanged) { 4687 if (mIsDragging) { 4688 // Update touchToWindow offset in case of parent scrolling while dragging 4689 if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) { 4690 mTouchToWindowOffsetX += parentPositionX - mLastParentX; 4691 mTouchToWindowOffsetY += parentPositionY - mLastParentY; 4692 mLastParentX = parentPositionX; 4693 mLastParentY = parentPositionY; 4694 } 4695 4696 onHandleMoved(); 4697 } 4698 4699 if (shouldShow()) { 4700 // Transform to the window coordinates to follow the view tranformation. 4701 final int[] pts = { mPositionX + mHotspotX + getHorizontalOffset(), mPositionY}; 4702 mTextView.transformFromViewToWindowSpace(pts); 4703 pts[0] -= mHotspotX + getHorizontalOffset(); 4704 4705 if (isShowing()) { 4706 mContainer.update(pts[0], pts[1], -1, -1); 4707 } else { 4708 mContainer.showAtLocation(mTextView, Gravity.NO_GRAVITY, pts[0], pts[1]); 4709 } 4710 } else { 4711 if (isShowing()) { 4712 dismiss(); 4713 } 4714 } 4715 4716 mPositionHasChanged = false; 4717 } 4718 } 4719 4720 @Override 4721 protected void onDraw(Canvas c) { 4722 final int drawWidth = mDrawable.getIntrinsicWidth(); 4723 final int left = getHorizontalOffset(); 4724 4725 mDrawable.setBounds(left, 0, left + drawWidth, mDrawable.getIntrinsicHeight()); 4726 mDrawable.draw(c); 4727 } 4728 4729 private int getHorizontalOffset() { 4730 final int width = getPreferredWidth(); 4731 final int drawWidth = mDrawable.getIntrinsicWidth(); 4732 final int left; 4733 switch (mHorizontalGravity) { 4734 case Gravity.LEFT: 4735 left = 0; 4736 break; 4737 default: 4738 case Gravity.CENTER: 4739 left = (width - drawWidth) / 2; 4740 break; 4741 case Gravity.RIGHT: 4742 left = width - drawWidth; 4743 break; 4744 } 4745 return left; 4746 } 4747 4748 protected int getCursorOffset() { 4749 return 0; 4750 } 4751 4752 private boolean tooLargeTextForMagnifier() { 4753 final float magnifierContentHeight = Math.round( 4754 mMagnifierAnimator.mMagnifier.getHeight() 4755 / mMagnifierAnimator.mMagnifier.getZoom()); 4756 final Paint.FontMetrics fontMetrics = mTextView.getPaint().getFontMetrics(); 4757 final float glyphHeight = fontMetrics.descent - fontMetrics.ascent; 4758 return glyphHeight > magnifierContentHeight; 4759 } 4760 4761 /** 4762 * Computes the position where the magnifier should be shown, relative to 4763 * {@code mTextView}, and writes them to {@code showPosInView}. Also decides 4764 * whether the magnifier should be shown or dismissed after this touch event. 4765 * @return Whether the magnifier should be shown at the computed coordinates or dismissed. 4766 */ 4767 private boolean obtainMagnifierShowCoordinates(@NonNull final MotionEvent event, 4768 final PointF showPosInView) { 4769 4770 final int trigger = getMagnifierHandleTrigger(); 4771 final int offset; 4772 final int otherHandleOffset; 4773 switch (trigger) { 4774 case MagnifierHandleTrigger.INSERTION: 4775 offset = mTextView.getSelectionStart(); 4776 otherHandleOffset = -1; 4777 break; 4778 case MagnifierHandleTrigger.SELECTION_START: 4779 offset = mTextView.getSelectionStart(); 4780 otherHandleOffset = mTextView.getSelectionEnd(); 4781 break; 4782 case MagnifierHandleTrigger.SELECTION_END: 4783 offset = mTextView.getSelectionEnd(); 4784 otherHandleOffset = mTextView.getSelectionStart(); 4785 break; 4786 default: 4787 offset = -1; 4788 otherHandleOffset = -1; 4789 break; 4790 } 4791 4792 if (offset == -1) { 4793 return false; 4794 } 4795 4796 final Layout layout = mTextView.getLayout(); 4797 final int lineNumber = layout.getLineForOffset(offset); 4798 // Compute whether the selection handles are currently on the same line, and, 4799 // in this particular case, whether the selected text is right to left. 4800 final boolean sameLineSelection = otherHandleOffset != -1 4801 && lineNumber == layout.getLineForOffset(otherHandleOffset); 4802 final boolean rtl = sameLineSelection 4803 && (offset < otherHandleOffset) 4804 != (getHorizontal(mTextView.getLayout(), offset) 4805 < getHorizontal(mTextView.getLayout(), otherHandleOffset)); 4806 4807 // Horizontally move the magnifier smoothly, clamp inside the current line / selection. 4808 final int[] textViewLocationOnScreen = new int[2]; 4809 mTextView.getLocationOnScreen(textViewLocationOnScreen); 4810 final float touchXInView = event.getRawX() - textViewLocationOnScreen[0]; 4811 float leftBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); 4812 float rightBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); 4813 if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_END) ^ rtl)) { 4814 leftBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); 4815 } else { 4816 leftBound += mTextView.getLayout().getLineLeft(lineNumber); 4817 } 4818 if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_START) ^ rtl)) { 4819 rightBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); 4820 } else { 4821 rightBound += mTextView.getLayout().getLineRight(lineNumber); 4822 } 4823 final float contentWidth = Math.round(mMagnifierAnimator.mMagnifier.getWidth() 4824 / mMagnifierAnimator.mMagnifier.getZoom()); 4825 if (touchXInView < leftBound - contentWidth / 2 4826 || touchXInView > rightBound + contentWidth / 2) { 4827 // The touch is too far from the current line / selection, so hide the magnifier. 4828 return false; 4829 } 4830 showPosInView.x = Math.max(leftBound, Math.min(rightBound, touchXInView)); 4831 4832 // Vertically snap to middle of current line. 4833 showPosInView.y = (mTextView.getLayout().getLineTop(lineNumber) 4834 + mTextView.getLayout().getLineBottom(lineNumber)) / 2.0f 4835 + mTextView.getTotalPaddingTop() - mTextView.getScrollY(); 4836 4837 return true; 4838 } 4839 4840 private boolean handleOverlapsMagnifier() { 4841 final int handleY = mContainer.getDecorViewLayoutParams().y; 4842 final int magnifierBottomWhenAtWindowTop = 4843 mTextView.getRootWindowInsets().getSystemWindowInsetTop() 4844 + mMagnifierAnimator.mMagnifier.getHeight(); 4845 return handleY <= magnifierBottomWhenAtWindowTop; 4846 } 4847 4848 protected final void updateMagnifier(@NonNull final MotionEvent event) { 4849 if (mMagnifierAnimator == null) { 4850 return; 4851 } 4852 4853 final PointF showPosInView = new PointF(); 4854 final boolean shouldShow = !tooLargeTextForMagnifier() 4855 && obtainMagnifierShowCoordinates(event, showPosInView); 4856 if (shouldShow) { 4857 // Make the cursor visible and stop blinking. 4858 mRenderCursorRegardlessTiming = true; 4859 mTextView.invalidateCursorPath(); 4860 suspendBlink(); 4861 // Hide handle if it overlaps the magnifier. 4862 if (handleOverlapsMagnifier()) { 4863 setVisible(false); 4864 } else { 4865 setVisible(true); 4866 } 4867 4868 mMagnifierAnimator.show(showPosInView.x, showPosInView.y); 4869 } else { 4870 dismissMagnifier(); 4871 } 4872 } 4873 4874 protected final void dismissMagnifier() { 4875 if (mMagnifierAnimator != null) { 4876 mMagnifierAnimator.dismiss(); 4877 mRenderCursorRegardlessTiming = false; 4878 resumeBlink(); 4879 setVisible(true); 4880 } 4881 } 4882 4883 @Override 4884 public boolean onTouchEvent(MotionEvent ev) { 4885 updateFloatingToolbarVisibility(ev); 4886 4887 switch (ev.getActionMasked()) { 4888 case MotionEvent.ACTION_DOWN: { 4889 startTouchUpFilter(getCurrentCursorOffset()); 4890 4891 final PositionListener positionListener = getPositionListener(); 4892 mLastParentX = positionListener.getPositionX(); 4893 mLastParentY = positionListener.getPositionY(); 4894 mLastParentXOnScreen = positionListener.getPositionXOnScreen(); 4895 mLastParentYOnScreen = positionListener.getPositionYOnScreen(); 4896 4897 final float xInWindow = ev.getRawX() - mLastParentXOnScreen + mLastParentX; 4898 final float yInWindow = ev.getRawY() - mLastParentYOnScreen + mLastParentY; 4899 mTouchToWindowOffsetX = xInWindow - mPositionX; 4900 mTouchToWindowOffsetY = yInWindow - mPositionY; 4901 4902 mIsDragging = true; 4903 mPreviousLineTouched = UNSET_LINE; 4904 break; 4905 } 4906 4907 case MotionEvent.ACTION_MOVE: { 4908 final float xInWindow = ev.getRawX() - mLastParentXOnScreen + mLastParentX; 4909 final float yInWindow = ev.getRawY() - mLastParentYOnScreen + mLastParentY; 4910 4911 // Vertical hysteresis: vertical down movement tends to snap to ideal offset 4912 final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY; 4913 final float currentVerticalOffset = yInWindow - mPositionY - mLastParentY; 4914 float newVerticalOffset; 4915 if (previousVerticalOffset < mIdealVerticalOffset) { 4916 newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset); 4917 newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset); 4918 } else { 4919 newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset); 4920 newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset); 4921 } 4922 mTouchToWindowOffsetY = newVerticalOffset + mLastParentY; 4923 4924 final float newPosX = 4925 xInWindow - mTouchToWindowOffsetX + mHotspotX + getHorizontalOffset(); 4926 final float newPosY = yInWindow - mTouchToWindowOffsetY + mTouchOffsetY; 4927 4928 updatePosition(newPosX, newPosY, 4929 ev.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); 4930 break; 4931 } 4932 4933 case MotionEvent.ACTION_UP: 4934 filterOnTouchUp(ev.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); 4935 // Fall through. 4936 case MotionEvent.ACTION_CANCEL: 4937 mIsDragging = false; 4938 updateDrawable(); 4939 break; 4940 } 4941 return true; 4942 } 4943 4944 public boolean isDragging() { 4945 return mIsDragging; 4946 } 4947 4948 void onHandleMoved() {} 4949 4950 public void onDetached() {} 4951 } 4952 4953 private class InsertionHandleView extends HandleView { 4954 private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000; 4955 private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds 4956 4957 // Used to detect taps on the insertion handle, which will affect the insertion action mode 4958 private float mDownPositionX, mDownPositionY; 4959 private Runnable mHider; 4960 4961 public InsertionHandleView(Drawable drawable) { 4962 super(drawable, drawable, com.android.internal.R.id.insertion_handle); 4963 } 4964 4965 @Override 4966 public void show() { 4967 super.show(); 4968 4969 final long durationSinceCutOrCopy = 4970 SystemClock.uptimeMillis() - TextView.sLastCutCopyOrTextChangedTime; 4971 4972 // Cancel the single tap delayed runnable. 4973 if (mInsertionActionModeRunnable != null 4974 && ((mTapState == TAP_STATE_DOUBLE_TAP) 4975 || (mTapState == TAP_STATE_TRIPLE_CLICK) 4976 || isCursorInsideEasyCorrectionSpan())) { 4977 mTextView.removeCallbacks(mInsertionActionModeRunnable); 4978 } 4979 4980 // Prepare and schedule the single tap runnable to run exactly after the double tap 4981 // timeout has passed. 4982 if ((mTapState != TAP_STATE_DOUBLE_TAP) && (mTapState != TAP_STATE_TRIPLE_CLICK) 4983 && !isCursorInsideEasyCorrectionSpan() 4984 && (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION)) { 4985 if (mTextActionMode == null) { 4986 if (mInsertionActionModeRunnable == null) { 4987 mInsertionActionModeRunnable = new Runnable() { 4988 @Override 4989 public void run() { 4990 startInsertionActionMode(); 4991 } 4992 }; 4993 } 4994 mTextView.postDelayed( 4995 mInsertionActionModeRunnable, 4996 ViewConfiguration.getDoubleTapTimeout() + 1); 4997 } 4998 4999 } 5000 5001 hideAfterDelay(); 5002 } 5003 5004 private void hideAfterDelay() { 5005 if (mHider == null) { 5006 mHider = new Runnable() { 5007 public void run() { 5008 hide(); 5009 } 5010 }; 5011 } else { 5012 removeHiderCallback(); 5013 } 5014 mTextView.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT); 5015 } 5016 5017 private void removeHiderCallback() { 5018 if (mHider != null) { 5019 mTextView.removeCallbacks(mHider); 5020 } 5021 } 5022 5023 @Override 5024 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 5025 return drawable.getIntrinsicWidth() / 2; 5026 } 5027 5028 @Override 5029 protected int getHorizontalGravity(boolean isRtlRun) { 5030 return Gravity.CENTER_HORIZONTAL; 5031 } 5032 5033 @Override 5034 protected int getCursorOffset() { 5035 int offset = super.getCursorOffset(); 5036 if (mDrawableForCursor != null) { 5037 mDrawableForCursor.getPadding(mTempRect); 5038 offset += (mDrawableForCursor.getIntrinsicWidth() 5039 - mTempRect.left - mTempRect.right) / 2; 5040 } 5041 return offset; 5042 } 5043 5044 @Override 5045 int getCursorHorizontalPosition(Layout layout, int offset) { 5046 if (mDrawableForCursor != null) { 5047 final float horizontal = getHorizontal(layout, offset); 5048 return clampHorizontalPosition(mDrawableForCursor, horizontal) + mTempRect.left; 5049 } 5050 return super.getCursorHorizontalPosition(layout, offset); 5051 } 5052 5053 @Override 5054 public boolean onTouchEvent(MotionEvent ev) { 5055 final boolean result = super.onTouchEvent(ev); 5056 5057 switch (ev.getActionMasked()) { 5058 case MotionEvent.ACTION_DOWN: 5059 mDownPositionX = ev.getRawX(); 5060 mDownPositionY = ev.getRawY(); 5061 updateMagnifier(ev); 5062 break; 5063 5064 case MotionEvent.ACTION_MOVE: 5065 updateMagnifier(ev); 5066 break; 5067 5068 case MotionEvent.ACTION_UP: 5069 if (!offsetHasBeenChanged()) { 5070 final float deltaX = mDownPositionX - ev.getRawX(); 5071 final float deltaY = mDownPositionY - ev.getRawY(); 5072 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 5073 5074 final ViewConfiguration viewConfiguration = ViewConfiguration.get( 5075 mTextView.getContext()); 5076 final int touchSlop = viewConfiguration.getScaledTouchSlop(); 5077 5078 if (distanceSquared < touchSlop * touchSlop) { 5079 // Tapping on the handle toggles the insertion action mode. 5080 if (mTextActionMode != null) { 5081 stopTextActionMode(); 5082 } else { 5083 startInsertionActionMode(); 5084 } 5085 } 5086 } else { 5087 if (mTextActionMode != null) { 5088 mTextActionMode.invalidateContentRect(); 5089 } 5090 } 5091 // Fall through. 5092 case MotionEvent.ACTION_CANCEL: 5093 hideAfterDelay(); 5094 dismissMagnifier(); 5095 break; 5096 5097 default: 5098 break; 5099 } 5100 5101 return result; 5102 } 5103 5104 @Override 5105 public int getCurrentCursorOffset() { 5106 return mTextView.getSelectionStart(); 5107 } 5108 5109 @Override 5110 public void updateSelection(int offset) { 5111 Selection.setSelection((Spannable) mTextView.getText(), offset); 5112 } 5113 5114 @Override 5115 protected void updatePosition(float x, float y, boolean fromTouchScreen) { 5116 Layout layout = mTextView.getLayout(); 5117 int offset; 5118 if (layout != null) { 5119 if (mPreviousLineTouched == UNSET_LINE) { 5120 mPreviousLineTouched = mTextView.getLineAtCoordinate(y); 5121 } 5122 int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); 5123 offset = getOffsetAtCoordinate(layout, currLine, x); 5124 mPreviousLineTouched = currLine; 5125 } else { 5126 offset = -1; 5127 } 5128 positionAtCursorOffset(offset, false, fromTouchScreen); 5129 if (mTextActionMode != null) { 5130 invalidateActionMode(); 5131 } 5132 } 5133 5134 @Override 5135 void onHandleMoved() { 5136 super.onHandleMoved(); 5137 removeHiderCallback(); 5138 } 5139 5140 @Override 5141 public void onDetached() { 5142 super.onDetached(); 5143 removeHiderCallback(); 5144 } 5145 5146 @Override 5147 @MagnifierHandleTrigger 5148 protected int getMagnifierHandleTrigger() { 5149 return MagnifierHandleTrigger.INSERTION; 5150 } 5151 } 5152 5153 @Retention(RetentionPolicy.SOURCE) 5154 @IntDef(prefix = { "HANDLE_TYPE_" }, value = { 5155 HANDLE_TYPE_SELECTION_START, 5156 HANDLE_TYPE_SELECTION_END 5157 }) 5158 public @interface HandleType {} 5159 public static final int HANDLE_TYPE_SELECTION_START = 0; 5160 public static final int HANDLE_TYPE_SELECTION_END = 1; 5161 5162 /** For selection handles */ 5163 @VisibleForTesting 5164 public final class SelectionHandleView extends HandleView { 5165 // Indicates the handle type, selection start (HANDLE_TYPE_SELECTION_START) or selection 5166 // end (HANDLE_TYPE_SELECTION_END). 5167 @HandleType 5168 private final int mHandleType; 5169 // Indicates whether the cursor is making adjustments within a word. 5170 private boolean mInWord = false; 5171 // Difference between touch position and word boundary position. 5172 private float mTouchWordDelta; 5173 // X value of the previous updatePosition call. 5174 private float mPrevX; 5175 // Indicates if the handle has moved a boundary between LTR and RTL text. 5176 private boolean mLanguageDirectionChanged = false; 5177 // Distance from edge of horizontally scrolling text view 5178 // to use to switch to character mode. 5179 private final float mTextViewEdgeSlop; 5180 // Used to save text view location. 5181 private final int[] mTextViewLocation = new int[2]; 5182 5183 public SelectionHandleView(Drawable drawableLtr, Drawable drawableRtl, int id, 5184 @HandleType int handleType) { 5185 super(drawableLtr, drawableRtl, id); 5186 mHandleType = handleType; 5187 ViewConfiguration viewConfiguration = ViewConfiguration.get(mTextView.getContext()); 5188 mTextViewEdgeSlop = viewConfiguration.getScaledTouchSlop() * 4; 5189 } 5190 5191 private boolean isStartHandle() { 5192 return mHandleType == HANDLE_TYPE_SELECTION_START; 5193 } 5194 5195 @Override 5196 protected int getHotspotX(Drawable drawable, boolean isRtlRun) { 5197 if (isRtlRun == isStartHandle()) { 5198 return drawable.getIntrinsicWidth() / 4; 5199 } else { 5200 return (drawable.getIntrinsicWidth() * 3) / 4; 5201 } 5202 } 5203 5204 @Override 5205 protected int getHorizontalGravity(boolean isRtlRun) { 5206 return (isRtlRun == isStartHandle()) ? Gravity.LEFT : Gravity.RIGHT; 5207 } 5208 5209 @Override 5210 public int getCurrentCursorOffset() { 5211 return isStartHandle() ? mTextView.getSelectionStart() : mTextView.getSelectionEnd(); 5212 } 5213 5214 @Override 5215 protected void updateSelection(int offset) { 5216 if (isStartHandle()) { 5217 Selection.setSelection((Spannable) mTextView.getText(), offset, 5218 mTextView.getSelectionEnd()); 5219 } else { 5220 Selection.setSelection((Spannable) mTextView.getText(), 5221 mTextView.getSelectionStart(), offset); 5222 } 5223 updateDrawable(); 5224 if (mTextActionMode != null) { 5225 invalidateActionMode(); 5226 } 5227 } 5228 5229 @Override 5230 protected void updatePosition(float x, float y, boolean fromTouchScreen) { 5231 final Layout layout = mTextView.getLayout(); 5232 if (layout == null) { 5233 // HandleView will deal appropriately in positionAtCursorOffset when 5234 // layout is null. 5235 positionAndAdjustForCrossingHandles(mTextView.getOffsetForPosition(x, y), 5236 fromTouchScreen); 5237 return; 5238 } 5239 5240 if (mPreviousLineTouched == UNSET_LINE) { 5241 mPreviousLineTouched = mTextView.getLineAtCoordinate(y); 5242 } 5243 5244 boolean positionCursor = false; 5245 final int anotherHandleOffset = 5246 isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); 5247 int currLine = getCurrentLineAdjustedForSlop(layout, mPreviousLineTouched, y); 5248 int initialOffset = getOffsetAtCoordinate(layout, currLine, x); 5249 5250 if (isStartHandle() && initialOffset >= anotherHandleOffset 5251 || !isStartHandle() && initialOffset <= anotherHandleOffset) { 5252 // Handles have crossed, bound it to the first selected line and 5253 // adjust by word / char as normal. 5254 currLine = layout.getLineForOffset(anotherHandleOffset); 5255 initialOffset = getOffsetAtCoordinate(layout, currLine, x); 5256 } 5257 5258 int offset = initialOffset; 5259 final int wordEnd = getWordEnd(offset); 5260 final int wordStart = getWordStart(offset); 5261 5262 if (mPrevX == UNSET_X_VALUE) { 5263 mPrevX = x; 5264 } 5265 5266 final int currentOffset = getCurrentCursorOffset(); 5267 final boolean rtlAtCurrentOffset = isAtRtlRun(layout, currentOffset); 5268 final boolean atRtl = isAtRtlRun(layout, offset); 5269 final boolean isLvlBoundary = layout.isLevelBoundary(offset); 5270 5271 // We can't determine if the user is expanding or shrinking the selection if they're 5272 // on a bi-di boundary, so until they've moved past the boundary we'll just place 5273 // the cursor at the current position. 5274 if (isLvlBoundary || (rtlAtCurrentOffset && !atRtl) || (!rtlAtCurrentOffset && atRtl)) { 5275 // We're on a boundary or this is the first direction change -- just update 5276 // to the current position. 5277 mLanguageDirectionChanged = true; 5278 mTouchWordDelta = 0.0f; 5279 positionAndAdjustForCrossingHandles(offset, fromTouchScreen); 5280 return; 5281 } else if (mLanguageDirectionChanged && !isLvlBoundary) { 5282 // We've just moved past the boundary so update the position. After this we can 5283 // figure out if the user is expanding or shrinking to go by word or character. 5284 positionAndAdjustForCrossingHandles(offset, fromTouchScreen); 5285 mTouchWordDelta = 0.0f; 5286 mLanguageDirectionChanged = false; 5287 return; 5288 } 5289 5290 boolean isExpanding; 5291 final float xDiff = x - mPrevX; 5292 if (isStartHandle()) { 5293 isExpanding = currLine < mPreviousLineTouched; 5294 } else { 5295 isExpanding = currLine > mPreviousLineTouched; 5296 } 5297 if (atRtl == isStartHandle()) { 5298 isExpanding |= xDiff > 0; 5299 } else { 5300 isExpanding |= xDiff < 0; 5301 } 5302 5303 if (mTextView.getHorizontallyScrolling()) { 5304 if (positionNearEdgeOfScrollingView(x, atRtl) 5305 && ((isStartHandle() && mTextView.getScrollX() != 0) 5306 || (!isStartHandle() 5307 && mTextView.canScrollHorizontally(atRtl ? -1 : 1))) 5308 && ((isExpanding && ((isStartHandle() && offset < currentOffset) 5309 || (!isStartHandle() && offset > currentOffset))) 5310 || !isExpanding)) { 5311 // If we're expanding ensure that the offset is actually expanding compared to 5312 // the current offset, if the handle snapped to the word, the finger position 5313 // may be out of sync and we don't want the selection to jump back. 5314 mTouchWordDelta = 0.0f; 5315 final int nextOffset = (atRtl == isStartHandle()) 5316 ? layout.getOffsetToRightOf(mPreviousOffset) 5317 : layout.getOffsetToLeftOf(mPreviousOffset); 5318 positionAndAdjustForCrossingHandles(nextOffset, fromTouchScreen); 5319 return; 5320 } 5321 } 5322 5323 if (isExpanding) { 5324 // User is increasing the selection. 5325 int wordBoundary = isStartHandle() ? wordStart : wordEnd; 5326 final boolean snapToWord = (!mInWord 5327 || (isStartHandle() ? currLine < mPrevLine : currLine > mPrevLine)) 5328 && atRtl == isAtRtlRun(layout, wordBoundary); 5329 if (snapToWord) { 5330 // Sometimes words can be broken across lines (Chinese, hyphenation). 5331 // We still snap to the word boundary but we only use the letters on the 5332 // current line to determine if the user is far enough into the word to snap. 5333 if (layout.getLineForOffset(wordBoundary) != currLine) { 5334 wordBoundary = isStartHandle() 5335 ? layout.getLineStart(currLine) : layout.getLineEnd(currLine); 5336 } 5337 final int offsetThresholdToSnap = isStartHandle() 5338 ? wordEnd - ((wordEnd - wordBoundary) / 2) 5339 : wordStart + ((wordBoundary - wordStart) / 2); 5340 if (isStartHandle() 5341 && (offset <= offsetThresholdToSnap || currLine < mPrevLine)) { 5342 // User is far enough into the word or on a different line so we expand by 5343 // word. 5344 offset = wordStart; 5345 } else if (!isStartHandle() 5346 && (offset >= offsetThresholdToSnap || currLine > mPrevLine)) { 5347 // User is far enough into the word or on a different line so we expand by 5348 // word. 5349 offset = wordEnd; 5350 } else { 5351 offset = mPreviousOffset; 5352 } 5353 } 5354 if ((isStartHandle() && offset < initialOffset) 5355 || (!isStartHandle() && offset > initialOffset)) { 5356 final float adjustedX = getHorizontal(layout, offset); 5357 mTouchWordDelta = 5358 mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; 5359 } else { 5360 mTouchWordDelta = 0.0f; 5361 } 5362 positionCursor = true; 5363 } else { 5364 final int adjustedOffset = 5365 getOffsetAtCoordinate(layout, currLine, x - mTouchWordDelta); 5366 final boolean shrinking = isStartHandle() 5367 ? adjustedOffset > mPreviousOffset || currLine > mPrevLine 5368 : adjustedOffset < mPreviousOffset || currLine < mPrevLine; 5369 if (shrinking) { 5370 // User is shrinking the selection. 5371 if (currLine != mPrevLine) { 5372 // We're on a different line, so we'll snap to word boundaries. 5373 offset = isStartHandle() ? wordStart : wordEnd; 5374 if ((isStartHandle() && offset < initialOffset) 5375 || (!isStartHandle() && offset > initialOffset)) { 5376 final float adjustedX = getHorizontal(layout, offset); 5377 mTouchWordDelta = 5378 mTextView.convertToLocalHorizontalCoordinate(x) - adjustedX; 5379 } else { 5380 mTouchWordDelta = 0.0f; 5381 } 5382 } else { 5383 offset = adjustedOffset; 5384 } 5385 positionCursor = true; 5386 } else if ((isStartHandle() && adjustedOffset < mPreviousOffset) 5387 || (!isStartHandle() && adjustedOffset > mPreviousOffset)) { 5388 // Handle has jumped to the word boundary, and the user is moving 5389 // their finger towards the handle, the delta should be updated. 5390 mTouchWordDelta = mTextView.convertToLocalHorizontalCoordinate(x) 5391 - getHorizontal(layout, mPreviousOffset); 5392 } 5393 } 5394 5395 if (positionCursor) { 5396 mPreviousLineTouched = currLine; 5397 positionAndAdjustForCrossingHandles(offset, fromTouchScreen); 5398 } 5399 mPrevX = x; 5400 } 5401 5402 @Override 5403 protected void positionAtCursorOffset(int offset, boolean forceUpdatePosition, 5404 boolean fromTouchScreen) { 5405 super.positionAtCursorOffset(offset, forceUpdatePosition, fromTouchScreen); 5406 mInWord = (offset != -1) && !getWordIteratorWithText().isBoundary(offset); 5407 } 5408 5409 @Override 5410 public boolean onTouchEvent(MotionEvent event) { 5411 boolean superResult = super.onTouchEvent(event); 5412 5413 switch (event.getActionMasked()) { 5414 case MotionEvent.ACTION_DOWN: 5415 // Reset the touch word offset and x value when the user 5416 // re-engages the handle. 5417 mTouchWordDelta = 0.0f; 5418 mPrevX = UNSET_X_VALUE; 5419 updateMagnifier(event); 5420 break; 5421 5422 case MotionEvent.ACTION_MOVE: 5423 updateMagnifier(event); 5424 break; 5425 5426 case MotionEvent.ACTION_UP: 5427 case MotionEvent.ACTION_CANCEL: 5428 dismissMagnifier(); 5429 break; 5430 } 5431 5432 return superResult; 5433 } 5434 5435 private void positionAndAdjustForCrossingHandles(int offset, boolean fromTouchScreen) { 5436 final int anotherHandleOffset = 5437 isStartHandle() ? mTextView.getSelectionEnd() : mTextView.getSelectionStart(); 5438 if ((isStartHandle() && offset >= anotherHandleOffset) 5439 || (!isStartHandle() && offset <= anotherHandleOffset)) { 5440 mTouchWordDelta = 0.0f; 5441 final Layout layout = mTextView.getLayout(); 5442 if (layout != null && offset != anotherHandleOffset) { 5443 final float horiz = getHorizontal(layout, offset); 5444 final float anotherHandleHoriz = getHorizontal(layout, anotherHandleOffset, 5445 !isStartHandle()); 5446 final float currentHoriz = getHorizontal(layout, mPreviousOffset); 5447 if (currentHoriz < anotherHandleHoriz && horiz < anotherHandleHoriz 5448 || currentHoriz > anotherHandleHoriz && horiz > anotherHandleHoriz) { 5449 // This handle passes another one as it crossed a direction boundary. 5450 // Don't minimize the selection, but keep the handle at the run boundary. 5451 final int currentOffset = getCurrentCursorOffset(); 5452 final int offsetToGetRunRange = isStartHandle() 5453 ? currentOffset : Math.max(currentOffset - 1, 0); 5454 final long range = layout.getRunRange(offsetToGetRunRange); 5455 if (isStartHandle()) { 5456 offset = TextUtils.unpackRangeStartFromLong(range); 5457 } else { 5458 offset = TextUtils.unpackRangeEndFromLong(range); 5459 } 5460 positionAtCursorOffset(offset, false, fromTouchScreen); 5461 return; 5462 } 5463 } 5464 // Handles can not cross and selection is at least one character. 5465 offset = getNextCursorOffset(anotherHandleOffset, !isStartHandle()); 5466 } 5467 positionAtCursorOffset(offset, false, fromTouchScreen); 5468 } 5469 5470 private boolean positionNearEdgeOfScrollingView(float x, boolean atRtl) { 5471 mTextView.getLocationOnScreen(mTextViewLocation); 5472 boolean nearEdge; 5473 if (atRtl == isStartHandle()) { 5474 int rightEdge = mTextViewLocation[0] + mTextView.getWidth() 5475 - mTextView.getPaddingRight(); 5476 nearEdge = x > rightEdge - mTextViewEdgeSlop; 5477 } else { 5478 int leftEdge = mTextViewLocation[0] + mTextView.getPaddingLeft(); 5479 nearEdge = x < leftEdge + mTextViewEdgeSlop; 5480 } 5481 return nearEdge; 5482 } 5483 5484 @Override 5485 protected boolean isAtRtlRun(@NonNull Layout layout, int offset) { 5486 final int offsetToCheck = isStartHandle() ? offset : Math.max(offset - 1, 0); 5487 return layout.isRtlCharAt(offsetToCheck); 5488 } 5489 5490 @Override 5491 public float getHorizontal(@NonNull Layout layout, int offset) { 5492 return getHorizontal(layout, offset, isStartHandle()); 5493 } 5494 5495 private float getHorizontal(@NonNull Layout layout, int offset, boolean startHandle) { 5496 final int line = layout.getLineForOffset(offset); 5497 final int offsetToCheck = startHandle ? offset : Math.max(offset - 1, 0); 5498 final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); 5499 final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; 5500 return (isRtlChar == isRtlParagraph) 5501 ? layout.getPrimaryHorizontal(offset) : layout.getSecondaryHorizontal(offset); 5502 } 5503 5504 @Override 5505 protected int getOffsetAtCoordinate(@NonNull Layout layout, int line, float x) { 5506 final float localX = mTextView.convertToLocalHorizontalCoordinate(x); 5507 final int primaryOffset = layout.getOffsetForHorizontal(line, localX, true); 5508 if (!layout.isLevelBoundary(primaryOffset)) { 5509 return primaryOffset; 5510 } 5511 final int secondaryOffset = layout.getOffsetForHorizontal(line, localX, false); 5512 final int currentOffset = getCurrentCursorOffset(); 5513 final int primaryDiff = Math.abs(primaryOffset - currentOffset); 5514 final int secondaryDiff = Math.abs(secondaryOffset - currentOffset); 5515 if (primaryDiff < secondaryDiff) { 5516 return primaryOffset; 5517 } else if (primaryDiff > secondaryDiff) { 5518 return secondaryOffset; 5519 } else { 5520 final int offsetToCheck = isStartHandle() 5521 ? currentOffset : Math.max(currentOffset - 1, 0); 5522 final boolean isRtlChar = layout.isRtlCharAt(offsetToCheck); 5523 final boolean isRtlParagraph = layout.getParagraphDirection(line) == -1; 5524 return isRtlChar == isRtlParagraph ? primaryOffset : secondaryOffset; 5525 } 5526 } 5527 5528 @MagnifierHandleTrigger 5529 protected int getMagnifierHandleTrigger() { 5530 return isStartHandle() 5531 ? MagnifierHandleTrigger.SELECTION_START 5532 : MagnifierHandleTrigger.SELECTION_END; 5533 } 5534 } 5535 5536 private int getCurrentLineAdjustedForSlop(Layout layout, int prevLine, float y) { 5537 final int trueLine = mTextView.getLineAtCoordinate(y); 5538 if (layout == null || prevLine > layout.getLineCount() 5539 || layout.getLineCount() <= 0 || prevLine < 0) { 5540 // Invalid parameters, just return whatever line is at y. 5541 return trueLine; 5542 } 5543 5544 if (Math.abs(trueLine - prevLine) >= 2) { 5545 // Only stick to lines if we're within a line of the previous selection. 5546 return trueLine; 5547 } 5548 5549 final float verticalOffset = mTextView.viewportToContentVerticalOffset(); 5550 final int lineCount = layout.getLineCount(); 5551 final float slop = mTextView.getLineHeight() * LINE_SLOP_MULTIPLIER_FOR_HANDLEVIEWS; 5552 5553 final float firstLineTop = layout.getLineTop(0) + verticalOffset; 5554 final float prevLineTop = layout.getLineTop(prevLine) + verticalOffset; 5555 final float yTopBound = Math.max(prevLineTop - slop, firstLineTop + slop); 5556 5557 final float lastLineBottom = layout.getLineBottom(lineCount - 1) + verticalOffset; 5558 final float prevLineBottom = layout.getLineBottom(prevLine) + verticalOffset; 5559 final float yBottomBound = Math.min(prevLineBottom + slop, lastLineBottom - slop); 5560 5561 // Determine if we've moved lines based on y position and previous line. 5562 int currLine; 5563 if (y <= yTopBound) { 5564 currLine = Math.max(prevLine - 1, 0); 5565 } else if (y >= yBottomBound) { 5566 currLine = Math.min(prevLine + 1, lineCount - 1); 5567 } else { 5568 currLine = prevLine; 5569 } 5570 return currLine; 5571 } 5572 5573 /** 5574 * A CursorController instance can be used to control a cursor in the text. 5575 */ 5576 private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener { 5577 /** 5578 * Makes the cursor controller visible on screen. 5579 * See also {@link #hide()}. 5580 */ 5581 public void show(); 5582 5583 /** 5584 * Hide the cursor controller from screen. 5585 * See also {@link #show()}. 5586 */ 5587 public void hide(); 5588 5589 /** 5590 * Called when the view is detached from window. Perform house keeping task, such as 5591 * stopping Runnable thread that would otherwise keep a reference on the context, thus 5592 * preventing the activity from being recycled. 5593 */ 5594 public void onDetached(); 5595 5596 public boolean isCursorBeingModified(); 5597 5598 public boolean isActive(); 5599 } 5600 5601 private class InsertionPointCursorController implements CursorController { 5602 private InsertionHandleView mHandle; 5603 5604 public void show() { 5605 getHandle().show(); 5606 5607 if (mSelectionModifierCursorController != null) { 5608 mSelectionModifierCursorController.hide(); 5609 } 5610 } 5611 5612 public void hide() { 5613 if (mHandle != null) { 5614 mHandle.hide(); 5615 } 5616 } 5617 5618 public void onTouchModeChanged(boolean isInTouchMode) { 5619 if (!isInTouchMode) { 5620 hide(); 5621 } 5622 } 5623 5624 private InsertionHandleView getHandle() { 5625 if (mSelectHandleCenter == null) { 5626 mSelectHandleCenter = mTextView.getContext().getDrawable( 5627 mTextView.mTextSelectHandleRes); 5628 } 5629 if (mHandle == null) { 5630 mHandle = new InsertionHandleView(mSelectHandleCenter); 5631 } 5632 return mHandle; 5633 } 5634 5635 @Override 5636 public void onDetached() { 5637 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 5638 observer.removeOnTouchModeChangeListener(this); 5639 5640 if (mHandle != null) mHandle.onDetached(); 5641 } 5642 5643 @Override 5644 public boolean isCursorBeingModified() { 5645 return mHandle != null && mHandle.isDragging(); 5646 } 5647 5648 @Override 5649 public boolean isActive() { 5650 return mHandle != null && mHandle.isShowing(); 5651 } 5652 5653 public void invalidateHandle() { 5654 if (mHandle != null) { 5655 mHandle.invalidate(); 5656 } 5657 } 5658 } 5659 5660 class SelectionModifierCursorController implements CursorController { 5661 // The cursor controller handles, lazily created when shown. 5662 private SelectionHandleView mStartHandle; 5663 private SelectionHandleView mEndHandle; 5664 // The offsets of that last touch down event. Remembered to start selection there. 5665 private int mMinTouchOffset, mMaxTouchOffset; 5666 5667 private float mDownPositionX, mDownPositionY; 5668 private boolean mGestureStayedInTapRegion; 5669 5670 // Where the user first starts the drag motion. 5671 private int mStartOffset = -1; 5672 5673 private boolean mHaventMovedEnoughToStartDrag; 5674 // The line that a selection happened most recently with the drag accelerator. 5675 private int mLineSelectionIsOn = -1; 5676 // Whether the drag accelerator has selected past the initial line. 5677 private boolean mSwitchedLines = false; 5678 5679 // Indicates the drag accelerator mode that the user is currently using. 5680 private int mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; 5681 // Drag accelerator is inactive. 5682 private static final int DRAG_ACCELERATOR_MODE_INACTIVE = 0; 5683 // Character based selection by dragging. Only for mouse. 5684 private static final int DRAG_ACCELERATOR_MODE_CHARACTER = 1; 5685 // Word based selection by dragging. Enabled after long pressing or double tapping. 5686 private static final int DRAG_ACCELERATOR_MODE_WORD = 2; 5687 // Paragraph based selection by dragging. Enabled after mouse triple click. 5688 private static final int DRAG_ACCELERATOR_MODE_PARAGRAPH = 3; 5689 5690 SelectionModifierCursorController() { 5691 resetTouchOffsets(); 5692 } 5693 5694 public void show() { 5695 if (mTextView.isInBatchEditMode()) { 5696 return; 5697 } 5698 initDrawables(); 5699 initHandles(); 5700 } 5701 5702 private void initDrawables() { 5703 if (mSelectHandleLeft == null) { 5704 mSelectHandleLeft = mTextView.getContext().getDrawable( 5705 mTextView.mTextSelectHandleLeftRes); 5706 } 5707 if (mSelectHandleRight == null) { 5708 mSelectHandleRight = mTextView.getContext().getDrawable( 5709 mTextView.mTextSelectHandleRightRes); 5710 } 5711 } 5712 5713 private void initHandles() { 5714 // Lazy object creation has to be done before updatePosition() is called. 5715 if (mStartHandle == null) { 5716 mStartHandle = new SelectionHandleView(mSelectHandleLeft, mSelectHandleRight, 5717 com.android.internal.R.id.selection_start_handle, 5718 HANDLE_TYPE_SELECTION_START); 5719 } 5720 if (mEndHandle == null) { 5721 mEndHandle = new SelectionHandleView(mSelectHandleRight, mSelectHandleLeft, 5722 com.android.internal.R.id.selection_end_handle, 5723 HANDLE_TYPE_SELECTION_END); 5724 } 5725 5726 mStartHandle.show(); 5727 mEndHandle.show(); 5728 5729 hideInsertionPointCursorController(); 5730 } 5731 5732 public void hide() { 5733 if (mStartHandle != null) mStartHandle.hide(); 5734 if (mEndHandle != null) mEndHandle.hide(); 5735 } 5736 5737 public void enterDrag(int dragAcceleratorMode) { 5738 // Just need to init the handles / hide insertion cursor. 5739 show(); 5740 mDragAcceleratorMode = dragAcceleratorMode; 5741 // Start location of selection. 5742 mStartOffset = mTextView.getOffsetForPosition(mLastDownPositionX, 5743 mLastDownPositionY); 5744 mLineSelectionIsOn = mTextView.getLineAtCoordinate(mLastDownPositionY); 5745 // Don't show the handles until user has lifted finger. 5746 hide(); 5747 5748 // This stops scrolling parents from intercepting the touch event, allowing 5749 // the user to continue dragging across the screen to select text; TextView will 5750 // scroll as necessary. 5751 mTextView.getParent().requestDisallowInterceptTouchEvent(true); 5752 mTextView.cancelLongPress(); 5753 } 5754 5755 public void onTouchEvent(MotionEvent event) { 5756 // This is done even when the View does not have focus, so that long presses can start 5757 // selection and tap can move cursor from this tap position. 5758 final float eventX = event.getX(); 5759 final float eventY = event.getY(); 5760 final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); 5761 switch (event.getActionMasked()) { 5762 case MotionEvent.ACTION_DOWN: 5763 if (extractedTextModeWillBeStarted()) { 5764 // Prevent duplicating the selection handles until the mode starts. 5765 hide(); 5766 } else { 5767 // Remember finger down position, to be able to start selection from there. 5768 mMinTouchOffset = mMaxTouchOffset = mTextView.getOffsetForPosition( 5769 eventX, eventY); 5770 5771 // Double tap detection 5772 if (mGestureStayedInTapRegion) { 5773 if (mTapState == TAP_STATE_DOUBLE_TAP 5774 || mTapState == TAP_STATE_TRIPLE_CLICK) { 5775 final float deltaX = eventX - mDownPositionX; 5776 final float deltaY = eventY - mDownPositionY; 5777 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 5778 5779 ViewConfiguration viewConfiguration = ViewConfiguration.get( 5780 mTextView.getContext()); 5781 int doubleTapSlop = viewConfiguration.getScaledDoubleTapSlop(); 5782 boolean stayedInArea = 5783 distanceSquared < doubleTapSlop * doubleTapSlop; 5784 5785 if (stayedInArea && (isMouse || isPositionOnText(eventX, eventY))) { 5786 if (mTapState == TAP_STATE_DOUBLE_TAP) { 5787 selectCurrentWordAndStartDrag(); 5788 } else if (mTapState == TAP_STATE_TRIPLE_CLICK) { 5789 selectCurrentParagraphAndStartDrag(); 5790 } 5791 mDiscardNextActionUp = true; 5792 } 5793 } 5794 } 5795 5796 mDownPositionX = eventX; 5797 mDownPositionY = eventY; 5798 mGestureStayedInTapRegion = true; 5799 mHaventMovedEnoughToStartDrag = true; 5800 } 5801 break; 5802 5803 case MotionEvent.ACTION_POINTER_DOWN: 5804 case MotionEvent.ACTION_POINTER_UP: 5805 // Handle multi-point gestures. Keep min and max offset positions. 5806 // Only activated for devices that correctly handle multi-touch. 5807 if (mTextView.getContext().getPackageManager().hasSystemFeature( 5808 PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) { 5809 updateMinAndMaxOffsets(event); 5810 } 5811 break; 5812 5813 case MotionEvent.ACTION_MOVE: 5814 final ViewConfiguration viewConfig = ViewConfiguration.get( 5815 mTextView.getContext()); 5816 final int touchSlop = viewConfig.getScaledTouchSlop(); 5817 5818 if (mGestureStayedInTapRegion || mHaventMovedEnoughToStartDrag) { 5819 final float deltaX = eventX - mDownPositionX; 5820 final float deltaY = eventY - mDownPositionY; 5821 final float distanceSquared = deltaX * deltaX + deltaY * deltaY; 5822 5823 if (mGestureStayedInTapRegion) { 5824 int doubleTapTouchSlop = viewConfig.getScaledDoubleTapTouchSlop(); 5825 mGestureStayedInTapRegion = 5826 distanceSquared <= doubleTapTouchSlop * doubleTapTouchSlop; 5827 } 5828 if (mHaventMovedEnoughToStartDrag) { 5829 // We don't start dragging until the user has moved enough. 5830 mHaventMovedEnoughToStartDrag = 5831 distanceSquared <= touchSlop * touchSlop; 5832 } 5833 } 5834 5835 if (isMouse && !isDragAcceleratorActive()) { 5836 final int offset = mTextView.getOffsetForPosition(eventX, eventY); 5837 if (mTextView.hasSelection() 5838 && (!mHaventMovedEnoughToStartDrag || mStartOffset != offset) 5839 && offset >= mTextView.getSelectionStart() 5840 && offset <= mTextView.getSelectionEnd()) { 5841 startDragAndDrop(); 5842 break; 5843 } 5844 5845 if (mStartOffset != offset) { 5846 // Start character based drag accelerator. 5847 stopTextActionMode(); 5848 enterDrag(DRAG_ACCELERATOR_MODE_CHARACTER); 5849 mDiscardNextActionUp = true; 5850 mHaventMovedEnoughToStartDrag = false; 5851 } 5852 } 5853 5854 if (mStartHandle != null && mStartHandle.isShowing()) { 5855 // Don't do the drag if the handles are showing already. 5856 break; 5857 } 5858 5859 updateSelection(event); 5860 break; 5861 5862 case MotionEvent.ACTION_UP: 5863 if (!isDragAcceleratorActive()) { 5864 break; 5865 } 5866 updateSelection(event); 5867 5868 // No longer dragging to select text, let the parent intercept events. 5869 mTextView.getParent().requestDisallowInterceptTouchEvent(false); 5870 5871 // No longer the first dragging motion, reset. 5872 resetDragAcceleratorState(); 5873 5874 if (mTextView.hasSelection()) { 5875 // Drag selection should not be adjusted by the text classifier. 5876 startSelectionActionModeAsync(mHaventMovedEnoughToStartDrag); 5877 } 5878 break; 5879 } 5880 } 5881 5882 private void updateSelection(MotionEvent event) { 5883 if (mTextView.getLayout() != null) { 5884 switch (mDragAcceleratorMode) { 5885 case DRAG_ACCELERATOR_MODE_CHARACTER: 5886 updateCharacterBasedSelection(event); 5887 break; 5888 case DRAG_ACCELERATOR_MODE_WORD: 5889 updateWordBasedSelection(event); 5890 break; 5891 case DRAG_ACCELERATOR_MODE_PARAGRAPH: 5892 updateParagraphBasedSelection(event); 5893 break; 5894 } 5895 } 5896 } 5897 5898 /** 5899 * If the TextView allows text selection, selects the current paragraph and starts a drag. 5900 * 5901 * @return true if the drag was started. 5902 */ 5903 private boolean selectCurrentParagraphAndStartDrag() { 5904 if (mInsertionActionModeRunnable != null) { 5905 mTextView.removeCallbacks(mInsertionActionModeRunnable); 5906 } 5907 stopTextActionMode(); 5908 if (!selectCurrentParagraph()) { 5909 return false; 5910 } 5911 enterDrag(SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_PARAGRAPH); 5912 return true; 5913 } 5914 5915 private void updateCharacterBasedSelection(MotionEvent event) { 5916 final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); 5917 updateSelectionInternal(mStartOffset, offset, 5918 event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); 5919 } 5920 5921 private void updateWordBasedSelection(MotionEvent event) { 5922 if (mHaventMovedEnoughToStartDrag) { 5923 return; 5924 } 5925 final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); 5926 final ViewConfiguration viewConfig = ViewConfiguration.get( 5927 mTextView.getContext()); 5928 final float eventX = event.getX(); 5929 final float eventY = event.getY(); 5930 final int currLine; 5931 if (isMouse) { 5932 // No need to offset the y coordinate for mouse input. 5933 currLine = mTextView.getLineAtCoordinate(eventY); 5934 } else { 5935 float y = eventY; 5936 if (mSwitchedLines) { 5937 // Offset the finger by the same vertical offset as the handles. 5938 // This improves visibility of the content being selected by 5939 // shifting the finger below the content, this is applied once 5940 // the user has switched lines. 5941 final int touchSlop = viewConfig.getScaledTouchSlop(); 5942 final float fingerOffset = (mStartHandle != null) 5943 ? mStartHandle.getIdealVerticalOffset() 5944 : touchSlop; 5945 y = eventY - fingerOffset; 5946 } 5947 5948 currLine = getCurrentLineAdjustedForSlop(mTextView.getLayout(), mLineSelectionIsOn, 5949 y); 5950 if (!mSwitchedLines && currLine != mLineSelectionIsOn) { 5951 // Break early here, we want to offset the finger position from 5952 // the selection highlight, once the user moved their finger 5953 // to a different line we should apply the offset and *not* switch 5954 // lines until recomputing the position with the finger offset. 5955 mSwitchedLines = true; 5956 return; 5957 } 5958 } 5959 5960 int startOffset; 5961 int offset = mTextView.getOffsetAtCoordinate(currLine, eventX); 5962 // Snap to word boundaries. 5963 if (mStartOffset < offset) { 5964 // Expanding with end handle. 5965 offset = getWordEnd(offset); 5966 startOffset = getWordStart(mStartOffset); 5967 } else { 5968 // Expanding with start handle. 5969 offset = getWordStart(offset); 5970 startOffset = getWordEnd(mStartOffset); 5971 if (startOffset == offset) { 5972 offset = getNextCursorOffset(offset, false); 5973 } 5974 } 5975 mLineSelectionIsOn = currLine; 5976 updateSelectionInternal(startOffset, offset, 5977 event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); 5978 } 5979 5980 private void updateParagraphBasedSelection(MotionEvent event) { 5981 final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY()); 5982 5983 final int start = Math.min(offset, mStartOffset); 5984 final int end = Math.max(offset, mStartOffset); 5985 final long paragraphsRange = getParagraphsRange(start, end); 5986 final int selectionStart = TextUtils.unpackRangeStartFromLong(paragraphsRange); 5987 final int selectionEnd = TextUtils.unpackRangeEndFromLong(paragraphsRange); 5988 updateSelectionInternal(selectionStart, selectionEnd, 5989 event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)); 5990 } 5991 5992 private void updateSelectionInternal(int selectionStart, int selectionEnd, 5993 boolean fromTouchScreen) { 5994 final boolean performHapticFeedback = fromTouchScreen && mHapticTextHandleEnabled 5995 && ((mTextView.getSelectionStart() != selectionStart) 5996 || (mTextView.getSelectionEnd() != selectionEnd)); 5997 Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); 5998 if (performHapticFeedback) { 5999 mTextView.performHapticFeedback(HapticFeedbackConstants.TEXT_HANDLE_MOVE); 6000 } 6001 } 6002 6003 /** 6004 * @param event 6005 */ 6006 private void updateMinAndMaxOffsets(MotionEvent event) { 6007 int pointerCount = event.getPointerCount(); 6008 for (int index = 0; index < pointerCount; index++) { 6009 int offset = mTextView.getOffsetForPosition(event.getX(index), event.getY(index)); 6010 if (offset < mMinTouchOffset) mMinTouchOffset = offset; 6011 if (offset > mMaxTouchOffset) mMaxTouchOffset = offset; 6012 } 6013 } 6014 6015 public int getMinTouchOffset() { 6016 return mMinTouchOffset; 6017 } 6018 6019 public int getMaxTouchOffset() { 6020 return mMaxTouchOffset; 6021 } 6022 6023 public void resetTouchOffsets() { 6024 mMinTouchOffset = mMaxTouchOffset = -1; 6025 resetDragAcceleratorState(); 6026 } 6027 6028 private void resetDragAcceleratorState() { 6029 mStartOffset = -1; 6030 mDragAcceleratorMode = DRAG_ACCELERATOR_MODE_INACTIVE; 6031 mSwitchedLines = false; 6032 final int selectionStart = mTextView.getSelectionStart(); 6033 final int selectionEnd = mTextView.getSelectionEnd(); 6034 if (selectionStart > selectionEnd) { 6035 Selection.setSelection((Spannable) mTextView.getText(), 6036 selectionEnd, selectionStart); 6037 } 6038 } 6039 6040 /** 6041 * @return true iff this controller is currently used to move the selection start. 6042 */ 6043 public boolean isSelectionStartDragged() { 6044 return mStartHandle != null && mStartHandle.isDragging(); 6045 } 6046 6047 @Override 6048 public boolean isCursorBeingModified() { 6049 return isDragAcceleratorActive() || isSelectionStartDragged() 6050 || (mEndHandle != null && mEndHandle.isDragging()); 6051 } 6052 6053 /** 6054 * @return true if the user is selecting text using the drag accelerator. 6055 */ 6056 public boolean isDragAcceleratorActive() { 6057 return mDragAcceleratorMode != DRAG_ACCELERATOR_MODE_INACTIVE; 6058 } 6059 6060 public void onTouchModeChanged(boolean isInTouchMode) { 6061 if (!isInTouchMode) { 6062 hide(); 6063 } 6064 } 6065 6066 @Override 6067 public void onDetached() { 6068 final ViewTreeObserver observer = mTextView.getViewTreeObserver(); 6069 observer.removeOnTouchModeChangeListener(this); 6070 6071 if (mStartHandle != null) mStartHandle.onDetached(); 6072 if (mEndHandle != null) mEndHandle.onDetached(); 6073 } 6074 6075 @Override 6076 public boolean isActive() { 6077 return mStartHandle != null && mStartHandle.isShowing(); 6078 } 6079 6080 public void invalidateHandles() { 6081 if (mStartHandle != null) { 6082 mStartHandle.invalidate(); 6083 } 6084 if (mEndHandle != null) { 6085 mEndHandle.invalidate(); 6086 } 6087 } 6088 } 6089 6090 private class CorrectionHighlighter { 6091 private final Path mPath = new Path(); 6092 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 6093 private int mStart, mEnd; 6094 private long mFadingStartTime; 6095 private RectF mTempRectF; 6096 private static final int FADE_OUT_DURATION = 400; 6097 6098 public CorrectionHighlighter() { 6099 mPaint.setCompatibilityScaling( 6100 mTextView.getResources().getCompatibilityInfo().applicationScale); 6101 mPaint.setStyle(Paint.Style.FILL); 6102 } 6103 6104 public void highlight(CorrectionInfo info) { 6105 mStart = info.getOffset(); 6106 mEnd = mStart + info.getNewText().length(); 6107 mFadingStartTime = SystemClock.uptimeMillis(); 6108 6109 if (mStart < 0 || mEnd < 0) { 6110 stopAnimation(); 6111 } 6112 } 6113 6114 public void draw(Canvas canvas, int cursorOffsetVertical) { 6115 if (updatePath() && updatePaint()) { 6116 if (cursorOffsetVertical != 0) { 6117 canvas.translate(0, cursorOffsetVertical); 6118 } 6119 6120 canvas.drawPath(mPath, mPaint); 6121 6122 if (cursorOffsetVertical != 0) { 6123 canvas.translate(0, -cursorOffsetVertical); 6124 } 6125 invalidate(true); // TODO invalidate cursor region only 6126 } else { 6127 stopAnimation(); 6128 invalidate(false); // TODO invalidate cursor region only 6129 } 6130 } 6131 6132 private boolean updatePaint() { 6133 final long duration = SystemClock.uptimeMillis() - mFadingStartTime; 6134 if (duration > FADE_OUT_DURATION) return false; 6135 6136 final float coef = 1.0f - (float) duration / FADE_OUT_DURATION; 6137 final int highlightColorAlpha = Color.alpha(mTextView.mHighlightColor); 6138 final int color = (mTextView.mHighlightColor & 0x00FFFFFF) 6139 + ((int) (highlightColorAlpha * coef) << 24); 6140 mPaint.setColor(color); 6141 return true; 6142 } 6143 6144 private boolean updatePath() { 6145 final Layout layout = mTextView.getLayout(); 6146 if (layout == null) return false; 6147 6148 // Update in case text is edited while the animation is run 6149 final int length = mTextView.getText().length(); 6150 int start = Math.min(length, mStart); 6151 int end = Math.min(length, mEnd); 6152 6153 mPath.reset(); 6154 layout.getSelectionPath(start, end, mPath); 6155 return true; 6156 } 6157 6158 private void invalidate(boolean delayed) { 6159 if (mTextView.getLayout() == null) return; 6160 6161 if (mTempRectF == null) mTempRectF = new RectF(); 6162 mPath.computeBounds(mTempRectF, false); 6163 6164 int left = mTextView.getCompoundPaddingLeft(); 6165 int top = mTextView.getExtendedPaddingTop() + mTextView.getVerticalOffset(true); 6166 6167 if (delayed) { 6168 mTextView.postInvalidateOnAnimation( 6169 left + (int) mTempRectF.left, top + (int) mTempRectF.top, 6170 left + (int) mTempRectF.right, top + (int) mTempRectF.bottom); 6171 } else { 6172 mTextView.postInvalidate((int) mTempRectF.left, (int) mTempRectF.top, 6173 (int) mTempRectF.right, (int) mTempRectF.bottom); 6174 } 6175 } 6176 6177 private void stopAnimation() { 6178 Editor.this.mCorrectionHighlighter = null; 6179 } 6180 } 6181 6182 private static class ErrorPopup extends PopupWindow { 6183 private boolean mAbove = false; 6184 private final TextView mView; 6185 private int mPopupInlineErrorBackgroundId = 0; 6186 private int mPopupInlineErrorAboveBackgroundId = 0; 6187 6188 ErrorPopup(TextView v, int width, int height) { 6189 super(v, width, height); 6190 mView = v; 6191 // Make sure the TextView has a background set as it will be used the first time it is 6192 // shown and positioned. Initialized with below background, which should have 6193 // dimensions identical to the above version for this to work (and is more likely). 6194 mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, 6195 com.android.internal.R.styleable.Theme_errorMessageBackground); 6196 mView.setBackgroundResource(mPopupInlineErrorBackgroundId); 6197 } 6198 6199 void fixDirection(boolean above) { 6200 mAbove = above; 6201 6202 if (above) { 6203 mPopupInlineErrorAboveBackgroundId = 6204 getResourceId(mPopupInlineErrorAboveBackgroundId, 6205 com.android.internal.R.styleable.Theme_errorMessageAboveBackground); 6206 } else { 6207 mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId, 6208 com.android.internal.R.styleable.Theme_errorMessageBackground); 6209 } 6210 6211 mView.setBackgroundResource( 6212 above ? mPopupInlineErrorAboveBackgroundId : mPopupInlineErrorBackgroundId); 6213 } 6214 6215 private int getResourceId(int currentId, int index) { 6216 if (currentId == 0) { 6217 TypedArray styledAttributes = mView.getContext().obtainStyledAttributes( 6218 R.styleable.Theme); 6219 currentId = styledAttributes.getResourceId(index, 0); 6220 styledAttributes.recycle(); 6221 } 6222 return currentId; 6223 } 6224 6225 @Override 6226 public void update(int x, int y, int w, int h, boolean force) { 6227 super.update(x, y, w, h, force); 6228 6229 boolean above = isAboveAnchor(); 6230 if (above != mAbove) { 6231 fixDirection(above); 6232 } 6233 } 6234 } 6235 6236 static class InputContentType { 6237 int imeOptions = EditorInfo.IME_NULL; 6238 String privateImeOptions; 6239 CharSequence imeActionLabel; 6240 int imeActionId; 6241 Bundle extras; 6242 OnEditorActionListener onEditorActionListener; 6243 boolean enterDown; 6244 LocaleList imeHintLocales; 6245 } 6246 6247 static class InputMethodState { 6248 ExtractedTextRequest mExtractedTextRequest; 6249 final ExtractedText mExtractedText = new ExtractedText(); 6250 int mBatchEditNesting; 6251 boolean mCursorChanged; 6252 boolean mSelectionModeChanged; 6253 boolean mContentChanged; 6254 int mChangedStart, mChangedEnd, mChangedDelta; 6255 } 6256 6257 /** 6258 * @return True iff (start, end) is a valid range within the text. 6259 */ 6260 private static boolean isValidRange(CharSequence text, int start, int end) { 6261 return 0 <= start && start <= end && end <= text.length(); 6262 } 6263 6264 @VisibleForTesting 6265 public SuggestionsPopupWindow getSuggestionsPopupWindowForTesting() { 6266 return mSuggestionsPopupWindow; 6267 } 6268 6269 /** 6270 * An InputFilter that monitors text input to maintain undo history. It does not modify the 6271 * text being typed (and hence always returns null from the filter() method). 6272 * 6273 * TODO: Make this span aware. 6274 */ 6275 public static class UndoInputFilter implements InputFilter { 6276 private final Editor mEditor; 6277 6278 // Whether the current filter pass is directly caused by an end-user text edit. 6279 private boolean mIsUserEdit; 6280 6281 // Whether the text field is handling an IME composition. Must be parceled in case the user 6282 // rotates the screen during composition. 6283 private boolean mHasComposition; 6284 6285 // Whether the user is expanding or shortening the text 6286 private boolean mExpanding; 6287 6288 // Whether the previous edit operation was in the current batch edit. 6289 private boolean mPreviousOperationWasInSameBatchEdit; 6290 6291 public UndoInputFilter(Editor editor) { 6292 mEditor = editor; 6293 } 6294 6295 public void saveInstanceState(Parcel parcel) { 6296 parcel.writeInt(mIsUserEdit ? 1 : 0); 6297 parcel.writeInt(mHasComposition ? 1 : 0); 6298 parcel.writeInt(mExpanding ? 1 : 0); 6299 parcel.writeInt(mPreviousOperationWasInSameBatchEdit ? 1 : 0); 6300 } 6301 6302 public void restoreInstanceState(Parcel parcel) { 6303 mIsUserEdit = parcel.readInt() != 0; 6304 mHasComposition = parcel.readInt() != 0; 6305 mExpanding = parcel.readInt() != 0; 6306 mPreviousOperationWasInSameBatchEdit = parcel.readInt() != 0; 6307 } 6308 6309 /** 6310 * Signals that a user-triggered edit is starting. 6311 */ 6312 public void beginBatchEdit() { 6313 if (DEBUG_UNDO) Log.d(TAG, "beginBatchEdit"); 6314 mIsUserEdit = true; 6315 } 6316 6317 public void endBatchEdit() { 6318 if (DEBUG_UNDO) Log.d(TAG, "endBatchEdit"); 6319 mIsUserEdit = false; 6320 mPreviousOperationWasInSameBatchEdit = false; 6321 } 6322 6323 @Override 6324 public CharSequence filter(CharSequence source, int start, int end, 6325 Spanned dest, int dstart, int dend) { 6326 if (DEBUG_UNDO) { 6327 Log.d(TAG, "filter: source=" + source + " (" + start + "-" + end + ") " 6328 + "dest=" + dest + " (" + dstart + "-" + dend + ")"); 6329 } 6330 6331 // Check to see if this edit should be tracked for undo. 6332 if (!canUndoEdit(source, start, end, dest, dstart, dend)) { 6333 return null; 6334 } 6335 6336 final boolean hadComposition = mHasComposition; 6337 mHasComposition = isComposition(source); 6338 final boolean wasExpanding = mExpanding; 6339 boolean shouldCreateSeparateState = false; 6340 if ((end - start) != (dend - dstart)) { 6341 mExpanding = (end - start) > (dend - dstart); 6342 if (hadComposition && mExpanding != wasExpanding) { 6343 shouldCreateSeparateState = true; 6344 } 6345 } 6346 6347 // Handle edit. 6348 handleEdit(source, start, end, dest, dstart, dend, shouldCreateSeparateState); 6349 return null; 6350 } 6351 6352 void freezeLastEdit() { 6353 mEditor.mUndoManager.beginUpdate("Edit text"); 6354 EditOperation lastEdit = getLastEdit(); 6355 if (lastEdit != null) { 6356 lastEdit.mFrozen = true; 6357 } 6358 mEditor.mUndoManager.endUpdate(); 6359 } 6360 6361 @Retention(RetentionPolicy.SOURCE) 6362 @IntDef(prefix = { "MERGE_EDIT_MODE_" }, value = { 6363 MERGE_EDIT_MODE_FORCE_MERGE, 6364 MERGE_EDIT_MODE_NEVER_MERGE, 6365 MERGE_EDIT_MODE_NORMAL 6366 }) 6367 private @interface MergeMode {} 6368 private static final int MERGE_EDIT_MODE_FORCE_MERGE = 0; 6369 private static final int MERGE_EDIT_MODE_NEVER_MERGE = 1; 6370 /** Use {@link EditOperation#mergeWith} to merge */ 6371 private static final int MERGE_EDIT_MODE_NORMAL = 2; 6372 6373 private void handleEdit(CharSequence source, int start, int end, 6374 Spanned dest, int dstart, int dend, boolean shouldCreateSeparateState) { 6375 // An application may install a TextWatcher to provide additional modifications after 6376 // the initial input filters run (e.g. a credit card formatter that adds spaces to a 6377 // string). This results in multiple filter() calls for what the user considers to be 6378 // a single operation. Always undo the whole set of changes in one step. 6379 @MergeMode 6380 final int mergeMode; 6381 if (isInTextWatcher() || mPreviousOperationWasInSameBatchEdit) { 6382 mergeMode = MERGE_EDIT_MODE_FORCE_MERGE; 6383 } else if (shouldCreateSeparateState) { 6384 mergeMode = MERGE_EDIT_MODE_NEVER_MERGE; 6385 } else { 6386 mergeMode = MERGE_EDIT_MODE_NORMAL; 6387 } 6388 // Build a new operation with all the information from this edit. 6389 String newText = TextUtils.substring(source, start, end); 6390 String oldText = TextUtils.substring(dest, dstart, dend); 6391 EditOperation edit = new EditOperation(mEditor, oldText, dstart, newText, 6392 mHasComposition); 6393 if (mHasComposition && TextUtils.equals(edit.mNewText, edit.mOldText)) { 6394 return; 6395 } 6396 recordEdit(edit, mergeMode); 6397 } 6398 6399 private EditOperation getLastEdit() { 6400 final UndoManager um = mEditor.mUndoManager; 6401 return um.getLastOperation( 6402 EditOperation.class, mEditor.mUndoOwner, UndoManager.MERGE_MODE_UNIQUE); 6403 } 6404 /** 6405 * Fetches the last undo operation and checks to see if a new edit should be merged into it. 6406 * If forceMerge is true then the new edit is always merged. 6407 */ 6408 private void recordEdit(EditOperation edit, @MergeMode int mergeMode) { 6409 // Fetch the last edit operation and attempt to merge in the new edit. 6410 final UndoManager um = mEditor.mUndoManager; 6411 um.beginUpdate("Edit text"); 6412 EditOperation lastEdit = getLastEdit(); 6413 if (lastEdit == null) { 6414 // Add this as the first edit. 6415 if (DEBUG_UNDO) Log.d(TAG, "filter: adding first op " + edit); 6416 um.addOperation(edit, UndoManager.MERGE_MODE_NONE); 6417 } else if (mergeMode == MERGE_EDIT_MODE_FORCE_MERGE) { 6418 // Forced merges take priority because they could be the result of a non-user-edit 6419 // change and this case should not create a new undo operation. 6420 if (DEBUG_UNDO) Log.d(TAG, "filter: force merge " + edit); 6421 lastEdit.forceMergeWith(edit); 6422 } else if (!mIsUserEdit) { 6423 // An application directly modified the Editable outside of a text edit. Treat this 6424 // as a new change and don't attempt to merge. 6425 if (DEBUG_UNDO) Log.d(TAG, "non-user edit, new op " + edit); 6426 um.commitState(mEditor.mUndoOwner); 6427 um.addOperation(edit, UndoManager.MERGE_MODE_NONE); 6428 } else if (mergeMode == MERGE_EDIT_MODE_NORMAL && lastEdit.mergeWith(edit)) { 6429 // Merge succeeded, nothing else to do. 6430 if (DEBUG_UNDO) Log.d(TAG, "filter: merge succeeded, created " + lastEdit); 6431 } else { 6432 // Could not merge with the last edit, so commit the last edit and add this edit. 6433 if (DEBUG_UNDO) Log.d(TAG, "filter: merge failed, adding " + edit); 6434 um.commitState(mEditor.mUndoOwner); 6435 um.addOperation(edit, UndoManager.MERGE_MODE_NONE); 6436 } 6437 mPreviousOperationWasInSameBatchEdit = mIsUserEdit; 6438 um.endUpdate(); 6439 } 6440 6441 private boolean canUndoEdit(CharSequence source, int start, int end, 6442 Spanned dest, int dstart, int dend) { 6443 if (!mEditor.mAllowUndo) { 6444 if (DEBUG_UNDO) Log.d(TAG, "filter: undo is disabled"); 6445 return false; 6446 } 6447 6448 if (mEditor.mUndoManager.isInUndo()) { 6449 if (DEBUG_UNDO) Log.d(TAG, "filter: skipping, currently performing undo/redo"); 6450 return false; 6451 } 6452 6453 // Text filters run before input operations are applied. However, some input operations 6454 // are invalid and will throw exceptions when applied. This is common in tests. Don't 6455 // attempt to undo invalid operations. 6456 if (!isValidRange(source, start, end) || !isValidRange(dest, dstart, dend)) { 6457 if (DEBUG_UNDO) Log.d(TAG, "filter: invalid op"); 6458 return false; 6459 } 6460 6461 // Earlier filters can rewrite input to be a no-op, for example due to a length limit 6462 // on an input field. Skip no-op changes. 6463 if (start == end && dstart == dend) { 6464 if (DEBUG_UNDO) Log.d(TAG, "filter: skipping no-op"); 6465 return false; 6466 } 6467 6468 return true; 6469 } 6470 6471 private static boolean isComposition(CharSequence source) { 6472 if (!(source instanceof Spannable)) { 6473 return false; 6474 } 6475 // This is a composition edit if the source has a non-zero-length composing span. 6476 Spannable text = (Spannable) source; 6477 int composeBegin = EditableInputConnection.getComposingSpanStart(text); 6478 int composeEnd = EditableInputConnection.getComposingSpanEnd(text); 6479 return composeBegin < composeEnd; 6480 } 6481 6482 private boolean isInTextWatcher() { 6483 CharSequence text = mEditor.mTextView.getText(); 6484 return (text instanceof SpannableStringBuilder) 6485 && ((SpannableStringBuilder) text).getTextWatcherDepth() > 0; 6486 } 6487 } 6488 6489 /** 6490 * An operation to undo a single "edit" to a text view. 6491 */ 6492 public static class EditOperation extends UndoOperation<Editor> { 6493 private static final int TYPE_INSERT = 0; 6494 private static final int TYPE_DELETE = 1; 6495 private static final int TYPE_REPLACE = 2; 6496 6497 private int mType; 6498 private String mOldText; 6499 private String mNewText; 6500 private int mStart; 6501 6502 private int mOldCursorPos; 6503 private int mNewCursorPos; 6504 private boolean mFrozen; 6505 private boolean mIsComposition; 6506 6507 /** 6508 * Constructs an edit operation from a text input operation on editor that replaces the 6509 * oldText starting at dstart with newText. 6510 */ 6511 public EditOperation(Editor editor, String oldText, int dstart, String newText, 6512 boolean isComposition) { 6513 super(editor.mUndoOwner); 6514 mOldText = oldText; 6515 mNewText = newText; 6516 6517 // Determine the type of the edit. 6518 if (mNewText.length() > 0 && mOldText.length() == 0) { 6519 mType = TYPE_INSERT; 6520 } else if (mNewText.length() == 0 && mOldText.length() > 0) { 6521 mType = TYPE_DELETE; 6522 } else { 6523 mType = TYPE_REPLACE; 6524 } 6525 6526 mStart = dstart; 6527 // Store cursor data. 6528 mOldCursorPos = editor.mTextView.getSelectionStart(); 6529 mNewCursorPos = dstart + mNewText.length(); 6530 mIsComposition = isComposition; 6531 } 6532 6533 public EditOperation(Parcel src, ClassLoader loader) { 6534 super(src, loader); 6535 mType = src.readInt(); 6536 mOldText = src.readString(); 6537 mNewText = src.readString(); 6538 mStart = src.readInt(); 6539 mOldCursorPos = src.readInt(); 6540 mNewCursorPos = src.readInt(); 6541 mFrozen = src.readInt() == 1; 6542 mIsComposition = src.readInt() == 1; 6543 } 6544 6545 @Override 6546 public void writeToParcel(Parcel dest, int flags) { 6547 dest.writeInt(mType); 6548 dest.writeString(mOldText); 6549 dest.writeString(mNewText); 6550 dest.writeInt(mStart); 6551 dest.writeInt(mOldCursorPos); 6552 dest.writeInt(mNewCursorPos); 6553 dest.writeInt(mFrozen ? 1 : 0); 6554 dest.writeInt(mIsComposition ? 1 : 0); 6555 } 6556 6557 private int getNewTextEnd() { 6558 return mStart + mNewText.length(); 6559 } 6560 6561 private int getOldTextEnd() { 6562 return mStart + mOldText.length(); 6563 } 6564 6565 @Override 6566 public void commit() { 6567 } 6568 6569 @Override 6570 public void undo() { 6571 if (DEBUG_UNDO) Log.d(TAG, "undo"); 6572 // Remove the new text and insert the old. 6573 Editor editor = getOwnerData(); 6574 Editable text = (Editable) editor.mTextView.getText(); 6575 modifyText(text, mStart, getNewTextEnd(), mOldText, mStart, mOldCursorPos); 6576 } 6577 6578 @Override 6579 public void redo() { 6580 if (DEBUG_UNDO) Log.d(TAG, "redo"); 6581 // Remove the old text and insert the new. 6582 Editor editor = getOwnerData(); 6583 Editable text = (Editable) editor.mTextView.getText(); 6584 modifyText(text, mStart, getOldTextEnd(), mNewText, mStart, mNewCursorPos); 6585 } 6586 6587 /** 6588 * Attempts to merge this existing operation with a new edit. 6589 * @param edit The new edit operation. 6590 * @return If the merge succeeded, returns true. Otherwise returns false and leaves this 6591 * object unchanged. 6592 */ 6593 private boolean mergeWith(EditOperation edit) { 6594 if (DEBUG_UNDO) { 6595 Log.d(TAG, "mergeWith old " + this); 6596 Log.d(TAG, "mergeWith new " + edit); 6597 } 6598 6599 if (mFrozen) { 6600 return false; 6601 } 6602 6603 switch (mType) { 6604 case TYPE_INSERT: 6605 return mergeInsertWith(edit); 6606 case TYPE_DELETE: 6607 return mergeDeleteWith(edit); 6608 case TYPE_REPLACE: 6609 return mergeReplaceWith(edit); 6610 default: 6611 return false; 6612 } 6613 } 6614 6615 private boolean mergeInsertWith(EditOperation edit) { 6616 if (edit.mType == TYPE_INSERT) { 6617 // Merge insertions that are contiguous even when it's frozen. 6618 if (getNewTextEnd() != edit.mStart) { 6619 return false; 6620 } 6621 mNewText += edit.mNewText; 6622 mNewCursorPos = edit.mNewCursorPos; 6623 mFrozen = edit.mFrozen; 6624 mIsComposition = edit.mIsComposition; 6625 return true; 6626 } 6627 if (mIsComposition && edit.mType == TYPE_REPLACE 6628 && mStart <= edit.mStart && getNewTextEnd() >= edit.getOldTextEnd()) { 6629 // Merge insertion with replace as they can be single insertion. 6630 mNewText = mNewText.substring(0, edit.mStart - mStart) + edit.mNewText 6631 + mNewText.substring(edit.getOldTextEnd() - mStart, mNewText.length()); 6632 mNewCursorPos = edit.mNewCursorPos; 6633 mIsComposition = edit.mIsComposition; 6634 return true; 6635 } 6636 return false; 6637 } 6638 6639 // TODO: Support forward delete. 6640 private boolean mergeDeleteWith(EditOperation edit) { 6641 // Only merge continuous deletes. 6642 if (edit.mType != TYPE_DELETE) { 6643 return false; 6644 } 6645 // Only merge deletions that are contiguous. 6646 if (mStart != edit.getOldTextEnd()) { 6647 return false; 6648 } 6649 mStart = edit.mStart; 6650 mOldText = edit.mOldText + mOldText; 6651 mNewCursorPos = edit.mNewCursorPos; 6652 mIsComposition = edit.mIsComposition; 6653 return true; 6654 } 6655 6656 private boolean mergeReplaceWith(EditOperation edit) { 6657 if (edit.mType == TYPE_INSERT && getNewTextEnd() == edit.mStart) { 6658 // Merge with adjacent insert. 6659 mNewText += edit.mNewText; 6660 mNewCursorPos = edit.mNewCursorPos; 6661 return true; 6662 } 6663 if (!mIsComposition) { 6664 return false; 6665 } 6666 if (edit.mType == TYPE_DELETE && mStart <= edit.mStart 6667 && getNewTextEnd() >= edit.getOldTextEnd()) { 6668 // Merge with delete as they can be single operation. 6669 mNewText = mNewText.substring(0, edit.mStart - mStart) 6670 + mNewText.substring(edit.getOldTextEnd() - mStart, mNewText.length()); 6671 if (mNewText.isEmpty()) { 6672 mType = TYPE_DELETE; 6673 } 6674 mNewCursorPos = edit.mNewCursorPos; 6675 mIsComposition = edit.mIsComposition; 6676 return true; 6677 } 6678 if (edit.mType == TYPE_REPLACE && mStart == edit.mStart 6679 && TextUtils.equals(mNewText, edit.mOldText)) { 6680 // Merge with the replace that replaces the same region. 6681 mNewText = edit.mNewText; 6682 mNewCursorPos = edit.mNewCursorPos; 6683 mIsComposition = edit.mIsComposition; 6684 return true; 6685 } 6686 return false; 6687 } 6688 6689 /** 6690 * Forcibly creates a single merged edit operation by simulating the entire text 6691 * contents being replaced. 6692 */ 6693 public void forceMergeWith(EditOperation edit) { 6694 if (DEBUG_UNDO) Log.d(TAG, "forceMerge"); 6695 if (mergeWith(edit)) { 6696 return; 6697 } 6698 Editor editor = getOwnerData(); 6699 6700 // Copy the text of the current field. 6701 // NOTE: Using StringBuilder instead of SpannableStringBuilder would be somewhat faster, 6702 // but would require two parallel implementations of modifyText() because Editable and 6703 // StringBuilder do not share an interface for replace/delete/insert. 6704 Editable editable = (Editable) editor.mTextView.getText(); 6705 Editable originalText = new SpannableStringBuilder(editable.toString()); 6706 6707 // Roll back the last operation. 6708 modifyText(originalText, mStart, getNewTextEnd(), mOldText, mStart, mOldCursorPos); 6709 6710 // Clone the text again and apply the new operation. 6711 Editable finalText = new SpannableStringBuilder(editable.toString()); 6712 modifyText(finalText, edit.mStart, edit.getOldTextEnd(), 6713 edit.mNewText, edit.mStart, edit.mNewCursorPos); 6714 6715 // Convert this operation into a replace operation. 6716 mType = TYPE_REPLACE; 6717 mNewText = finalText.toString(); 6718 mOldText = originalText.toString(); 6719 mStart = 0; 6720 mNewCursorPos = edit.mNewCursorPos; 6721 mIsComposition = edit.mIsComposition; 6722 // mOldCursorPos is unchanged. 6723 } 6724 6725 private static void modifyText(Editable text, int deleteFrom, int deleteTo, 6726 CharSequence newText, int newTextInsertAt, int newCursorPos) { 6727 // Apply the edit if it is still valid. 6728 if (isValidRange(text, deleteFrom, deleteTo) 6729 && newTextInsertAt <= text.length() - (deleteTo - deleteFrom)) { 6730 if (deleteFrom != deleteTo) { 6731 text.delete(deleteFrom, deleteTo); 6732 } 6733 if (newText.length() != 0) { 6734 text.insert(newTextInsertAt, newText); 6735 } 6736 } 6737 // Restore the cursor position. If there wasn't an old cursor (newCursorPos == -1) then 6738 // don't explicitly set it and rely on SpannableStringBuilder to position it. 6739 // TODO: Select all the text that was undone. 6740 if (0 <= newCursorPos && newCursorPos <= text.length()) { 6741 Selection.setSelection(text, newCursorPos); 6742 } 6743 } 6744 6745 private String getTypeString() { 6746 switch (mType) { 6747 case TYPE_INSERT: 6748 return "insert"; 6749 case TYPE_DELETE: 6750 return "delete"; 6751 case TYPE_REPLACE: 6752 return "replace"; 6753 default: 6754 return ""; 6755 } 6756 } 6757 6758 @Override 6759 public String toString() { 6760 return "[mType=" + getTypeString() + ", " 6761 + "mOldText=" + mOldText + ", " 6762 + "mNewText=" + mNewText + ", " 6763 + "mStart=" + mStart + ", " 6764 + "mOldCursorPos=" + mOldCursorPos + ", " 6765 + "mNewCursorPos=" + mNewCursorPos + ", " 6766 + "mFrozen=" + mFrozen + ", " 6767 + "mIsComposition=" + mIsComposition + "]"; 6768 } 6769 6770 public static final Parcelable.ClassLoaderCreator<EditOperation> CREATOR = 6771 new Parcelable.ClassLoaderCreator<EditOperation>() { 6772 @Override 6773 public EditOperation createFromParcel(Parcel in) { 6774 return new EditOperation(in, null); 6775 } 6776 6777 @Override 6778 public EditOperation createFromParcel(Parcel in, ClassLoader loader) { 6779 return new EditOperation(in, loader); 6780 } 6781 6782 @Override 6783 public EditOperation[] newArray(int size) { 6784 return new EditOperation[size]; 6785 } 6786 }; 6787 } 6788 6789 /** 6790 * A helper for enabling and handling "PROCESS_TEXT" menu actions. 6791 * These allow external applications to plug into currently selected text. 6792 */ 6793 static final class ProcessTextIntentActionsHandler { 6794 6795 private final Editor mEditor; 6796 private final TextView mTextView; 6797 private final Context mContext; 6798 private final PackageManager mPackageManager; 6799 private final String mPackageName; 6800 private final SparseArray<Intent> mAccessibilityIntents = new SparseArray<>(); 6801 private final SparseArray<AccessibilityNodeInfo.AccessibilityAction> mAccessibilityActions = 6802 new SparseArray<>(); 6803 private final List<ResolveInfo> mSupportedActivities = new ArrayList<>(); 6804 6805 private ProcessTextIntentActionsHandler(Editor editor) { 6806 mEditor = Preconditions.checkNotNull(editor); 6807 mTextView = Preconditions.checkNotNull(mEditor.mTextView); 6808 mContext = Preconditions.checkNotNull(mTextView.getContext()); 6809 mPackageManager = Preconditions.checkNotNull(mContext.getPackageManager()); 6810 mPackageName = Preconditions.checkNotNull(mContext.getPackageName()); 6811 } 6812 6813 /** 6814 * Adds "PROCESS_TEXT" menu items to the specified menu. 6815 */ 6816 public void onInitializeMenu(Menu menu) { 6817 loadSupportedActivities(); 6818 final int size = mSupportedActivities.size(); 6819 for (int i = 0; i < size; i++) { 6820 final ResolveInfo resolveInfo = mSupportedActivities.get(i); 6821 menu.add(Menu.NONE, Menu.NONE, 6822 Editor.MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START + i, 6823 getLabel(resolveInfo)) 6824 .setIntent(createProcessTextIntentForResolveInfo(resolveInfo)) 6825 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 6826 } 6827 } 6828 6829 /** 6830 * Performs a "PROCESS_TEXT" action if there is one associated with the specified 6831 * menu item. 6832 * 6833 * @return True if the action was performed, false otherwise. 6834 */ 6835 public boolean performMenuItemAction(MenuItem item) { 6836 return fireIntent(item.getIntent()); 6837 } 6838 6839 /** 6840 * Initializes and caches "PROCESS_TEXT" accessibility actions. 6841 */ 6842 public void initializeAccessibilityActions() { 6843 mAccessibilityIntents.clear(); 6844 mAccessibilityActions.clear(); 6845 int i = 0; 6846 loadSupportedActivities(); 6847 for (ResolveInfo resolveInfo : mSupportedActivities) { 6848 int actionId = TextView.ACCESSIBILITY_ACTION_PROCESS_TEXT_START_ID + i++; 6849 mAccessibilityActions.put( 6850 actionId, 6851 new AccessibilityNodeInfo.AccessibilityAction( 6852 actionId, getLabel(resolveInfo))); 6853 mAccessibilityIntents.put( 6854 actionId, createProcessTextIntentForResolveInfo(resolveInfo)); 6855 } 6856 } 6857 6858 /** 6859 * Adds "PROCESS_TEXT" accessibility actions to the specified accessibility node info. 6860 * NOTE: This needs a prior call to {@link #initializeAccessibilityActions()} to make the 6861 * latest accessibility actions available for this call. 6862 */ 6863 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo nodeInfo) { 6864 for (int i = 0; i < mAccessibilityActions.size(); i++) { 6865 nodeInfo.addAction(mAccessibilityActions.valueAt(i)); 6866 } 6867 } 6868 6869 /** 6870 * Performs a "PROCESS_TEXT" action if there is one associated with the specified 6871 * accessibility action id. 6872 * 6873 * @return True if the action was performed, false otherwise. 6874 */ 6875 public boolean performAccessibilityAction(int actionId) { 6876 return fireIntent(mAccessibilityIntents.get(actionId)); 6877 } 6878 6879 private boolean fireIntent(Intent intent) { 6880 if (intent != null && Intent.ACTION_PROCESS_TEXT.equals(intent.getAction())) { 6881 String selectedText = mTextView.getSelectedText(); 6882 selectedText = TextUtils.trimToParcelableSize(selectedText); 6883 intent.putExtra(Intent.EXTRA_PROCESS_TEXT, selectedText); 6884 mEditor.mPreserveSelection = true; 6885 mTextView.startActivityForResult(intent, TextView.PROCESS_TEXT_REQUEST_CODE); 6886 return true; 6887 } 6888 return false; 6889 } 6890 6891 private void loadSupportedActivities() { 6892 mSupportedActivities.clear(); 6893 if (!mContext.canStartActivityForResult()) { 6894 return; 6895 } 6896 PackageManager packageManager = mTextView.getContext().getPackageManager(); 6897 List<ResolveInfo> unfiltered = 6898 packageManager.queryIntentActivities(createProcessTextIntent(), 0); 6899 for (ResolveInfo info : unfiltered) { 6900 if (isSupportedActivity(info)) { 6901 mSupportedActivities.add(info); 6902 } 6903 } 6904 } 6905 6906 private boolean isSupportedActivity(ResolveInfo info) { 6907 return mPackageName.equals(info.activityInfo.packageName) 6908 || info.activityInfo.exported 6909 && (info.activityInfo.permission == null 6910 || mContext.checkSelfPermission(info.activityInfo.permission) 6911 == PackageManager.PERMISSION_GRANTED); 6912 } 6913 6914 private Intent createProcessTextIntentForResolveInfo(ResolveInfo info) { 6915 return createProcessTextIntent() 6916 .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, !mTextView.isTextEditable()) 6917 .setClassName(info.activityInfo.packageName, info.activityInfo.name); 6918 } 6919 6920 private Intent createProcessTextIntent() { 6921 return new Intent() 6922 .setAction(Intent.ACTION_PROCESS_TEXT) 6923 .setType("text/plain"); 6924 } 6925 6926 private CharSequence getLabel(ResolveInfo resolveInfo) { 6927 return resolveInfo.loadLabel(mPackageManager); 6928 } 6929 } 6930} 6931