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