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