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