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