TextView.java revision 30401328c1026389171d454c934c15875c3f7ff0
1/*
2 * Copyright (C) 2006 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 com.android.internal.util.FastMath;
20import com.android.internal.widget.EditableInputConnection;
21
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.R;
25import android.content.ClipData;
26import android.content.ClipData.Item;
27import android.content.ClipboardManager;
28import android.content.Context;
29import android.content.pm.PackageManager;
30import android.content.res.ColorStateList;
31import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
34import android.graphics.Canvas;
35import android.graphics.Color;
36import android.graphics.Paint;
37import android.graphics.Path;
38import android.graphics.Rect;
39import android.graphics.RectF;
40import android.graphics.Typeface;
41import android.graphics.drawable.Drawable;
42import android.inputmethodservice.ExtractEditText;
43import android.net.Uri;
44import android.os.Bundle;
45import android.os.Handler;
46import android.os.Message;
47import android.os.Parcel;
48import android.os.Parcelable;
49import android.os.SystemClock;
50import android.text.BoringLayout;
51import android.text.DynamicLayout;
52import android.text.Editable;
53import android.text.GetChars;
54import android.text.GraphicsOperations;
55import android.text.InputFilter;
56import android.text.InputType;
57import android.text.Layout;
58import android.text.ParcelableSpan;
59import android.text.Selection;
60import android.text.SpanWatcher;
61import android.text.Spannable;
62import android.text.SpannableString;
63import android.text.SpannableStringBuilder;
64import android.text.Spanned;
65import android.text.SpannedString;
66import android.text.StaticLayout;
67import android.text.TextPaint;
68import android.text.TextUtils;
69import android.text.TextWatcher;
70import android.text.method.ArrowKeyMovementMethod;
71import android.text.method.DateKeyListener;
72import android.text.method.DateTimeKeyListener;
73import android.text.method.DialerKeyListener;
74import android.text.method.DigitsKeyListener;
75import android.text.method.KeyListener;
76import android.text.method.LinkMovementMethod;
77import android.text.method.MetaKeyKeyListener;
78import android.text.method.MovementMethod;
79import android.text.method.PasswordTransformationMethod;
80import android.text.method.SingleLineTransformationMethod;
81import android.text.method.TextKeyListener;
82import android.text.method.TimeKeyListener;
83import android.text.method.TransformationMethod;
84import android.text.method.WordIterator;
85import android.text.style.ClickableSpan;
86import android.text.style.ParagraphStyle;
87import android.text.style.SuggestionSpan;
88import android.text.style.TextAppearanceSpan;
89import android.text.style.URLSpan;
90import android.text.style.UnderlineSpan;
91import android.text.style.UpdateAppearance;
92import android.text.util.Linkify;
93import android.util.AttributeSet;
94import android.util.DisplayMetrics;
95import android.util.FloatMath;
96import android.util.Log;
97import android.util.TypedValue;
98import android.view.ActionMode;
99import android.view.ActionMode.Callback;
100import android.view.ContextMenu;
101import android.view.DragEvent;
102import android.view.Gravity;
103import android.view.HapticFeedbackConstants;
104import android.view.KeyCharacterMap;
105import android.view.KeyEvent;
106import android.view.LayoutInflater;
107import android.view.Menu;
108import android.view.MenuItem;
109import android.view.MotionEvent;
110import android.view.View;
111import android.view.ViewConfiguration;
112import android.view.ViewDebug;
113import android.view.ViewGroup;
114import android.view.ViewGroup.LayoutParams;
115import android.view.ViewParent;
116import android.view.ViewAncestor;
117import android.view.ViewTreeObserver;
118import android.view.WindowManager;
119import android.view.accessibility.AccessibilityEvent;
120import android.view.accessibility.AccessibilityManager;
121import android.view.animation.AnimationUtils;
122import android.view.inputmethod.BaseInputConnection;
123import android.view.inputmethod.CompletionInfo;
124import android.view.inputmethod.CorrectionInfo;
125import android.view.inputmethod.EditorInfo;
126import android.view.inputmethod.ExtractedText;
127import android.view.inputmethod.ExtractedTextRequest;
128import android.view.inputmethod.InputConnection;
129import android.view.inputmethod.InputMethodManager;
130import android.widget.RemoteViews.RemoteView;
131
132import java.io.IOException;
133import java.lang.ref.WeakReference;
134import java.text.BreakIterator;
135import java.util.ArrayList;
136
137/**
138 * Displays text to the user and optionally allows them to edit it.  A TextView
139 * is a complete text editor, however the basic class is configured to not
140 * allow editing; see {@link EditText} for a subclass that configures the text
141 * view for editing.
142 *
143 * <p>
144 * <b>XML attributes</b>
145 * <p>
146 * See {@link android.R.styleable#TextView TextView Attributes},
147 * {@link android.R.styleable#View View Attributes}
148 *
149 * @attr ref android.R.styleable#TextView_text
150 * @attr ref android.R.styleable#TextView_bufferType
151 * @attr ref android.R.styleable#TextView_hint
152 * @attr ref android.R.styleable#TextView_textColor
153 * @attr ref android.R.styleable#TextView_textColorHighlight
154 * @attr ref android.R.styleable#TextView_textColorHint
155 * @attr ref android.R.styleable#TextView_textAppearance
156 * @attr ref android.R.styleable#TextView_textColorLink
157 * @attr ref android.R.styleable#TextView_textSize
158 * @attr ref android.R.styleable#TextView_textScaleX
159 * @attr ref android.R.styleable#TextView_typeface
160 * @attr ref android.R.styleable#TextView_textStyle
161 * @attr ref android.R.styleable#TextView_cursorVisible
162 * @attr ref android.R.styleable#TextView_maxLines
163 * @attr ref android.R.styleable#TextView_maxHeight
164 * @attr ref android.R.styleable#TextView_lines
165 * @attr ref android.R.styleable#TextView_height
166 * @attr ref android.R.styleable#TextView_minLines
167 * @attr ref android.R.styleable#TextView_minHeight
168 * @attr ref android.R.styleable#TextView_maxEms
169 * @attr ref android.R.styleable#TextView_maxWidth
170 * @attr ref android.R.styleable#TextView_ems
171 * @attr ref android.R.styleable#TextView_width
172 * @attr ref android.R.styleable#TextView_minEms
173 * @attr ref android.R.styleable#TextView_minWidth
174 * @attr ref android.R.styleable#TextView_gravity
175 * @attr ref android.R.styleable#TextView_scrollHorizontally
176 * @attr ref android.R.styleable#TextView_password
177 * @attr ref android.R.styleable#TextView_singleLine
178 * @attr ref android.R.styleable#TextView_selectAllOnFocus
179 * @attr ref android.R.styleable#TextView_includeFontPadding
180 * @attr ref android.R.styleable#TextView_maxLength
181 * @attr ref android.R.styleable#TextView_shadowColor
182 * @attr ref android.R.styleable#TextView_shadowDx
183 * @attr ref android.R.styleable#TextView_shadowDy
184 * @attr ref android.R.styleable#TextView_shadowRadius
185 * @attr ref android.R.styleable#TextView_autoLink
186 * @attr ref android.R.styleable#TextView_linksClickable
187 * @attr ref android.R.styleable#TextView_numeric
188 * @attr ref android.R.styleable#TextView_digits
189 * @attr ref android.R.styleable#TextView_phoneNumber
190 * @attr ref android.R.styleable#TextView_inputMethod
191 * @attr ref android.R.styleable#TextView_capitalize
192 * @attr ref android.R.styleable#TextView_autoText
193 * @attr ref android.R.styleable#TextView_editable
194 * @attr ref android.R.styleable#TextView_freezesText
195 * @attr ref android.R.styleable#TextView_ellipsize
196 * @attr ref android.R.styleable#TextView_drawableTop
197 * @attr ref android.R.styleable#TextView_drawableBottom
198 * @attr ref android.R.styleable#TextView_drawableRight
199 * @attr ref android.R.styleable#TextView_drawableLeft
200 * @attr ref android.R.styleable#TextView_drawablePadding
201 * @attr ref android.R.styleable#TextView_lineSpacingExtra
202 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
203 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
204 * @attr ref android.R.styleable#TextView_inputType
205 * @attr ref android.R.styleable#TextView_imeOptions
206 * @attr ref android.R.styleable#TextView_privateImeOptions
207 * @attr ref android.R.styleable#TextView_imeActionLabel
208 * @attr ref android.R.styleable#TextView_imeActionId
209 * @attr ref android.R.styleable#TextView_editorExtras
210 */
211@RemoteView
212public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {
213    static final String LOG_TAG = "TextView";
214    static final boolean DEBUG_EXTRACT = false;
215
216    private static final int PRIORITY = 100;
217    private int mCurrentAlpha = 255;
218
219    final int[] mTempCoords = new int[2];
220    Rect mTempRect;
221
222    private ColorStateList mTextColor;
223    private int mCurTextColor;
224    private ColorStateList mHintTextColor;
225    private ColorStateList mLinkTextColor;
226    private int mCurHintTextColor;
227    private boolean mFreezesText;
228    private boolean mFrozenWithFocus;
229    private boolean mTemporaryDetach;
230    private boolean mDispatchTemporaryDetach;
231
232    private boolean mDiscardNextActionUp = false;
233    private boolean mIgnoreActionUpEvent = false;
234
235    private Editable.Factory mEditableFactory = Editable.Factory.getInstance();
236    private Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance();
237
238    private float mShadowRadius, mShadowDx, mShadowDy;
239
240    private static final int PREDRAW_NOT_REGISTERED = 0;
241    private static final int PREDRAW_PENDING = 1;
242    private static final int PREDRAW_DONE = 2;
243    private int mPreDrawState = PREDRAW_NOT_REGISTERED;
244
245    private TextUtils.TruncateAt mEllipsize = null;
246
247    // Enum for the "typeface" XML parameter.
248    // TODO: How can we get this from the XML instead of hardcoding it here?
249    private static final int SANS = 1;
250    private static final int SERIF = 2;
251    private static final int MONOSPACE = 3;
252
253    // Bitfield for the "numeric" XML parameter.
254    // TODO: How can we get this from the XML instead of hardcoding it here?
255    private static final int SIGNED = 2;
256    private static final int DECIMAL = 4;
257
258    class Drawables {
259        final Rect mCompoundRect = new Rect();
260        Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight;
261        int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight;
262        int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight;
263        int mDrawablePadding;
264    }
265    private Drawables mDrawables;
266
267    private CharSequence mError;
268    private boolean mErrorWasChanged;
269    private ErrorPopup mPopup;
270    /**
271     * This flag is set if the TextView tries to display an error before it
272     * is attached to the window (so its position is still unknown).
273     * It causes the error to be shown later, when onAttachedToWindow()
274     * is called.
275     */
276    private boolean mShowErrorAfterAttach;
277
278    private CharWrapper mCharWrapper = null;
279
280    private boolean mSelectionMoved = false;
281    private boolean mTouchFocusSelected = false;
282
283    private Marquee mMarquee;
284    private boolean mRestartMarquee;
285
286    private int mMarqueeRepeatLimit = 3;
287
288    class InputContentType {
289        int imeOptions = EditorInfo.IME_NULL;
290        String privateImeOptions;
291        CharSequence imeActionLabel;
292        int imeActionId;
293        Bundle extras;
294        OnEditorActionListener onEditorActionListener;
295        boolean enterDown;
296    }
297    InputContentType mInputContentType;
298
299    class InputMethodState {
300        Rect mCursorRectInWindow = new Rect();
301        RectF mTmpRectF = new RectF();
302        float[] mTmpOffset = new float[2];
303        ExtractedTextRequest mExtracting;
304        final ExtractedText mTmpExtracted = new ExtractedText();
305        int mBatchEditNesting;
306        boolean mCursorChanged;
307        boolean mSelectionModeChanged;
308        boolean mContentChanged;
309        int mChangedStart, mChangedEnd, mChangedDelta;
310    }
311    InputMethodState mInputMethodState;
312
313    private int mTextSelectHandleLeftRes;
314    private int mTextSelectHandleRightRes;
315    private int mTextSelectHandleRes;
316    private int mTextEditPasteWindowLayout, mTextEditSidePasteWindowLayout;
317    private int mTextEditNoPasteWindowLayout, mTextEditSideNoPasteWindowLayout;
318
319    private int mTextEditSuggestionsBottomWindowLayout, mTextEditSuggestionsTopWindowLayout;
320    private int mTextEditSuggestionItemLayout;
321    private SuggestionsPopupWindow mSuggestionsPopupWindow;
322    private SuggestionRangeSpan mSuggestionRangeSpan;
323
324    private int mCursorDrawableRes;
325    private final Drawable[] mCursorDrawable = new Drawable[2];
326    private int mCursorCount; // Actual current number of used mCursorDrawable: 0, 1 or 2
327
328    private Drawable mSelectHandleLeft;
329    private Drawable mSelectHandleRight;
330    private Drawable mSelectHandleCenter;
331
332    private int mLastDownPositionX, mLastDownPositionY;
333    private Callback mCustomSelectionActionModeCallback;
334
335    private final int mSquaredTouchSlopDistance;
336    // Set when this TextView gained focus with some text selected. Will start selection mode.
337    private boolean mCreatedWithASelection = false;
338
339    private WordIterator mWordIterator;
340
341    /*
342     * Kick-start the font cache for the zygote process (to pay the cost of
343     * initializing freetype for our default font only once).
344     */
345    static {
346        Paint p = new Paint();
347        p.setAntiAlias(true);
348        // We don't care about the result, just the side-effect of measuring.
349        p.measureText("H");
350    }
351
352    /**
353     * Interface definition for a callback to be invoked when an action is
354     * performed on the editor.
355     */
356    public interface OnEditorActionListener {
357        /**
358         * Called when an action is being performed.
359         *
360         * @param v The view that was clicked.
361         * @param actionId Identifier of the action.  This will be either the
362         * identifier you supplied, or {@link EditorInfo#IME_NULL
363         * EditorInfo.IME_NULL} if being called due to the enter key
364         * being pressed.
365         * @param event If triggered by an enter key, this is the event;
366         * otherwise, this is null.
367         * @return Return true if you have consumed the action, else false.
368         */
369        boolean onEditorAction(TextView v, int actionId, KeyEvent event);
370    }
371
372    public TextView(Context context) {
373        this(context, null);
374    }
375
376    public TextView(Context context,
377                    AttributeSet attrs) {
378        this(context, attrs, com.android.internal.R.attr.textViewStyle);
379    }
380
381    @SuppressWarnings("deprecation")
382    public TextView(Context context,
383                    AttributeSet attrs,
384                    int defStyle) {
385        super(context, attrs, defStyle);
386        mText = "";
387
388        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
389        mTextPaint.density = getResources().getDisplayMetrics().density;
390        mTextPaint.setCompatibilityScaling(
391                getResources().getCompatibilityInfo().applicationScale);
392
393        // If we get the paint from the skin, we should set it to left, since
394        // the layout always wants it to be left.
395        // mTextPaint.setTextAlign(Paint.Align.LEFT);
396
397        mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
398        mHighlightPaint.setCompatibilityScaling(
399                getResources().getCompatibilityInfo().applicationScale);
400
401        mMovement = getDefaultMovementMethod();
402        mTransformation = null;
403
404        TypedArray a =
405            context.obtainStyledAttributes(
406                attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
407
408        int textColorHighlight = 0;
409        ColorStateList textColor = null;
410        ColorStateList textColorHint = null;
411        ColorStateList textColorLink = null;
412        int textSize = 15;
413        int typefaceIndex = -1;
414        int styleIndex = -1;
415
416        /*
417         * Look the appearance up without checking first if it exists because
418         * almost every TextView has one and it greatly simplifies the logic
419         * to be able to parse the appearance first and then let specific tags
420         * for this View override it.
421         */
422        TypedArray appearance = null;
423        int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
424        if (ap != -1) {
425            appearance = context.obtainStyledAttributes(ap,
426                                com.android.internal.R.styleable.
427                                TextAppearance);
428        }
429        if (appearance != null) {
430            int n = appearance.getIndexCount();
431            for (int i = 0; i < n; i++) {
432                int attr = appearance.getIndex(i);
433
434                switch (attr) {
435                case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
436                    textColorHighlight = appearance.getColor(attr, textColorHighlight);
437                    break;
438
439                case com.android.internal.R.styleable.TextAppearance_textColor:
440                    textColor = appearance.getColorStateList(attr);
441                    break;
442
443                case com.android.internal.R.styleable.TextAppearance_textColorHint:
444                    textColorHint = appearance.getColorStateList(attr);
445                    break;
446
447                case com.android.internal.R.styleable.TextAppearance_textColorLink:
448                    textColorLink = appearance.getColorStateList(attr);
449                    break;
450
451                case com.android.internal.R.styleable.TextAppearance_textSize:
452                    textSize = appearance.getDimensionPixelSize(attr, textSize);
453                    break;
454
455                case com.android.internal.R.styleable.TextAppearance_typeface:
456                    typefaceIndex = appearance.getInt(attr, -1);
457                    break;
458
459                case com.android.internal.R.styleable.TextAppearance_textStyle:
460                    styleIndex = appearance.getInt(attr, -1);
461                    break;
462                }
463            }
464
465            appearance.recycle();
466        }
467
468        boolean editable = getDefaultEditable();
469        CharSequence inputMethod = null;
470        int numeric = 0;
471        CharSequence digits = null;
472        boolean phone = false;
473        boolean autotext = false;
474        int autocap = -1;
475        int buffertype = 0;
476        boolean selectallonfocus = false;
477        Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
478            drawableBottom = null;
479        int drawablePadding = 0;
480        int ellipsize = -1;
481        boolean singleLine = false;
482        int maxlength = -1;
483        CharSequence text = "";
484        CharSequence hint = null;
485        int shadowcolor = 0;
486        float dx = 0, dy = 0, r = 0;
487        boolean password = false;
488        int inputType = EditorInfo.TYPE_NULL;
489
490        int n = a.getIndexCount();
491        for (int i = 0; i < n; i++) {
492            int attr = a.getIndex(i);
493
494            switch (attr) {
495            case com.android.internal.R.styleable.TextView_editable:
496                editable = a.getBoolean(attr, editable);
497                break;
498
499            case com.android.internal.R.styleable.TextView_inputMethod:
500                inputMethod = a.getText(attr);
501                break;
502
503            case com.android.internal.R.styleable.TextView_numeric:
504                numeric = a.getInt(attr, numeric);
505                break;
506
507            case com.android.internal.R.styleable.TextView_digits:
508                digits = a.getText(attr);
509                break;
510
511            case com.android.internal.R.styleable.TextView_phoneNumber:
512                phone = a.getBoolean(attr, phone);
513                break;
514
515            case com.android.internal.R.styleable.TextView_autoText:
516                autotext = a.getBoolean(attr, autotext);
517                break;
518
519            case com.android.internal.R.styleable.TextView_capitalize:
520                autocap = a.getInt(attr, autocap);
521                break;
522
523            case com.android.internal.R.styleable.TextView_bufferType:
524                buffertype = a.getInt(attr, buffertype);
525                break;
526
527            case com.android.internal.R.styleable.TextView_selectAllOnFocus:
528                selectallonfocus = a.getBoolean(attr, selectallonfocus);
529                break;
530
531            case com.android.internal.R.styleable.TextView_autoLink:
532                mAutoLinkMask = a.getInt(attr, 0);
533                break;
534
535            case com.android.internal.R.styleable.TextView_linksClickable:
536                mLinksClickable = a.getBoolean(attr, true);
537                break;
538
539            case com.android.internal.R.styleable.TextView_drawableLeft:
540                drawableLeft = a.getDrawable(attr);
541                break;
542
543            case com.android.internal.R.styleable.TextView_drawableTop:
544                drawableTop = a.getDrawable(attr);
545                break;
546
547            case com.android.internal.R.styleable.TextView_drawableRight:
548                drawableRight = a.getDrawable(attr);
549                break;
550
551            case com.android.internal.R.styleable.TextView_drawableBottom:
552                drawableBottom = a.getDrawable(attr);
553                break;
554
555            case com.android.internal.R.styleable.TextView_drawablePadding:
556                drawablePadding = a.getDimensionPixelSize(attr, drawablePadding);
557                break;
558
559            case com.android.internal.R.styleable.TextView_maxLines:
560                setMaxLines(a.getInt(attr, -1));
561                break;
562
563            case com.android.internal.R.styleable.TextView_maxHeight:
564                setMaxHeight(a.getDimensionPixelSize(attr, -1));
565                break;
566
567            case com.android.internal.R.styleable.TextView_lines:
568                setLines(a.getInt(attr, -1));
569                break;
570
571            case com.android.internal.R.styleable.TextView_height:
572                setHeight(a.getDimensionPixelSize(attr, -1));
573                break;
574
575            case com.android.internal.R.styleable.TextView_minLines:
576                setMinLines(a.getInt(attr, -1));
577                break;
578
579            case com.android.internal.R.styleable.TextView_minHeight:
580                setMinHeight(a.getDimensionPixelSize(attr, -1));
581                break;
582
583            case com.android.internal.R.styleable.TextView_maxEms:
584                setMaxEms(a.getInt(attr, -1));
585                break;
586
587            case com.android.internal.R.styleable.TextView_maxWidth:
588                setMaxWidth(a.getDimensionPixelSize(attr, -1));
589                break;
590
591            case com.android.internal.R.styleable.TextView_ems:
592                setEms(a.getInt(attr, -1));
593                break;
594
595            case com.android.internal.R.styleable.TextView_width:
596                setWidth(a.getDimensionPixelSize(attr, -1));
597                break;
598
599            case com.android.internal.R.styleable.TextView_minEms:
600                setMinEms(a.getInt(attr, -1));
601                break;
602
603            case com.android.internal.R.styleable.TextView_minWidth:
604                setMinWidth(a.getDimensionPixelSize(attr, -1));
605                break;
606
607            case com.android.internal.R.styleable.TextView_gravity:
608                setGravity(a.getInt(attr, -1));
609                break;
610
611            case com.android.internal.R.styleable.TextView_hint:
612                hint = a.getText(attr);
613                break;
614
615            case com.android.internal.R.styleable.TextView_text:
616                text = a.getText(attr);
617                break;
618
619            case com.android.internal.R.styleable.TextView_scrollHorizontally:
620                if (a.getBoolean(attr, false)) {
621                    setHorizontallyScrolling(true);
622                }
623                break;
624
625            case com.android.internal.R.styleable.TextView_singleLine:
626                singleLine = a.getBoolean(attr, singleLine);
627                break;
628
629            case com.android.internal.R.styleable.TextView_ellipsize:
630                ellipsize = a.getInt(attr, ellipsize);
631                break;
632
633            case com.android.internal.R.styleable.TextView_marqueeRepeatLimit:
634                setMarqueeRepeatLimit(a.getInt(attr, mMarqueeRepeatLimit));
635                break;
636
637            case com.android.internal.R.styleable.TextView_includeFontPadding:
638                if (!a.getBoolean(attr, true)) {
639                    setIncludeFontPadding(false);
640                }
641                break;
642
643            case com.android.internal.R.styleable.TextView_cursorVisible:
644                if (!a.getBoolean(attr, true)) {
645                    setCursorVisible(false);
646                }
647                break;
648
649            case com.android.internal.R.styleable.TextView_maxLength:
650                maxlength = a.getInt(attr, -1);
651                break;
652
653            case com.android.internal.R.styleable.TextView_textScaleX:
654                setTextScaleX(a.getFloat(attr, 1.0f));
655                break;
656
657            case com.android.internal.R.styleable.TextView_freezesText:
658                mFreezesText = a.getBoolean(attr, false);
659                break;
660
661            case com.android.internal.R.styleable.TextView_shadowColor:
662                shadowcolor = a.getInt(attr, 0);
663                break;
664
665            case com.android.internal.R.styleable.TextView_shadowDx:
666                dx = a.getFloat(attr, 0);
667                break;
668
669            case com.android.internal.R.styleable.TextView_shadowDy:
670                dy = a.getFloat(attr, 0);
671                break;
672
673            case com.android.internal.R.styleable.TextView_shadowRadius:
674                r = a.getFloat(attr, 0);
675                break;
676
677            case com.android.internal.R.styleable.TextView_enabled:
678                setEnabled(a.getBoolean(attr, isEnabled()));
679                break;
680
681            case com.android.internal.R.styleable.TextView_textColorHighlight:
682                textColorHighlight = a.getColor(attr, textColorHighlight);
683                break;
684
685            case com.android.internal.R.styleable.TextView_textColor:
686                textColor = a.getColorStateList(attr);
687                break;
688
689            case com.android.internal.R.styleable.TextView_textColorHint:
690                textColorHint = a.getColorStateList(attr);
691                break;
692
693            case com.android.internal.R.styleable.TextView_textColorLink:
694                textColorLink = a.getColorStateList(attr);
695                break;
696
697            case com.android.internal.R.styleable.TextView_textSize:
698                textSize = a.getDimensionPixelSize(attr, textSize);
699                break;
700
701            case com.android.internal.R.styleable.TextView_typeface:
702                typefaceIndex = a.getInt(attr, typefaceIndex);
703                break;
704
705            case com.android.internal.R.styleable.TextView_textStyle:
706                styleIndex = a.getInt(attr, styleIndex);
707                break;
708
709            case com.android.internal.R.styleable.TextView_password:
710                password = a.getBoolean(attr, password);
711                break;
712
713            case com.android.internal.R.styleable.TextView_lineSpacingExtra:
714                mSpacingAdd = a.getDimensionPixelSize(attr, (int) mSpacingAdd);
715                break;
716
717            case com.android.internal.R.styleable.TextView_lineSpacingMultiplier:
718                mSpacingMult = a.getFloat(attr, mSpacingMult);
719                break;
720
721            case com.android.internal.R.styleable.TextView_inputType:
722                inputType = a.getInt(attr, mInputType);
723                break;
724
725            case com.android.internal.R.styleable.TextView_imeOptions:
726                if (mInputContentType == null) {
727                    mInputContentType = new InputContentType();
728                }
729                mInputContentType.imeOptions = a.getInt(attr,
730                        mInputContentType.imeOptions);
731                break;
732
733            case com.android.internal.R.styleable.TextView_imeActionLabel:
734                if (mInputContentType == null) {
735                    mInputContentType = new InputContentType();
736                }
737                mInputContentType.imeActionLabel = a.getText(attr);
738                break;
739
740            case com.android.internal.R.styleable.TextView_imeActionId:
741                if (mInputContentType == null) {
742                    mInputContentType = new InputContentType();
743                }
744                mInputContentType.imeActionId = a.getInt(attr,
745                        mInputContentType.imeActionId);
746                break;
747
748            case com.android.internal.R.styleable.TextView_privateImeOptions:
749                setPrivateImeOptions(a.getString(attr));
750                break;
751
752            case com.android.internal.R.styleable.TextView_editorExtras:
753                try {
754                    setInputExtras(a.getResourceId(attr, 0));
755                } catch (XmlPullParserException e) {
756                    Log.w(LOG_TAG, "Failure reading input extras", e);
757                } catch (IOException e) {
758                    Log.w(LOG_TAG, "Failure reading input extras", e);
759                }
760                break;
761
762            case com.android.internal.R.styleable.TextView_textCursorDrawable:
763                mCursorDrawableRes = a.getResourceId(attr, 0);
764                break;
765
766            case com.android.internal.R.styleable.TextView_textSelectHandleLeft:
767                mTextSelectHandleLeftRes = a.getResourceId(attr, 0);
768                break;
769
770            case com.android.internal.R.styleable.TextView_textSelectHandleRight:
771                mTextSelectHandleRightRes = a.getResourceId(attr, 0);
772                break;
773
774            case com.android.internal.R.styleable.TextView_textSelectHandle:
775                mTextSelectHandleRes = a.getResourceId(attr, 0);
776                break;
777
778            case com.android.internal.R.styleable.TextView_textEditPasteWindowLayout:
779                mTextEditPasteWindowLayout = a.getResourceId(attr, 0);
780                break;
781
782            case com.android.internal.R.styleable.TextView_textEditNoPasteWindowLayout:
783                mTextEditNoPasteWindowLayout = a.getResourceId(attr, 0);
784                break;
785
786            case com.android.internal.R.styleable.TextView_textEditSidePasteWindowLayout:
787                mTextEditSidePasteWindowLayout = a.getResourceId(attr, 0);
788                break;
789
790            case com.android.internal.R.styleable.TextView_textEditSideNoPasteWindowLayout:
791                mTextEditSideNoPasteWindowLayout = a.getResourceId(attr, 0);
792                break;
793
794            case com.android.internal.R.styleable.TextView_textEditSuggestionsBottomWindowLayout:
795                mTextEditSuggestionsBottomWindowLayout = a.getResourceId(attr, 0);
796                break;
797
798            case com.android.internal.R.styleable.TextView_textEditSuggestionsTopWindowLayout:
799                mTextEditSuggestionsTopWindowLayout = a.getResourceId(attr, 0);
800                break;
801
802            case com.android.internal.R.styleable.TextView_textEditSuggestionItemLayout:
803                mTextEditSuggestionItemLayout = a.getResourceId(attr, 0);
804                break;
805
806            case com.android.internal.R.styleable.TextView_textIsSelectable:
807                mTextIsSelectable = a.getBoolean(attr, false);
808                break;
809            }
810        }
811        a.recycle();
812
813        BufferType bufferType = BufferType.EDITABLE;
814
815        final int variation =
816                inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
817        final boolean passwordInputType = variation
818                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
819        final boolean webPasswordInputType = variation
820                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD);
821        final boolean numberPasswordInputType = variation
822                == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
823
824        if (inputMethod != null) {
825            Class<?> c;
826
827            try {
828                c = Class.forName(inputMethod.toString());
829            } catch (ClassNotFoundException ex) {
830                throw new RuntimeException(ex);
831            }
832
833            try {
834                mInput = (KeyListener) c.newInstance();
835            } catch (InstantiationException ex) {
836                throw new RuntimeException(ex);
837            } catch (IllegalAccessException ex) {
838                throw new RuntimeException(ex);
839            }
840            try {
841                mInputType = inputType != EditorInfo.TYPE_NULL
842                        ? inputType
843                        : mInput.getInputType();
844            } catch (IncompatibleClassChangeError e) {
845                mInputType = EditorInfo.TYPE_CLASS_TEXT;
846            }
847        } else if (digits != null) {
848            mInput = DigitsKeyListener.getInstance(digits.toString());
849            // If no input type was specified, we will default to generic
850            // text, since we can't tell the IME about the set of digits
851            // that was selected.
852            mInputType = inputType != EditorInfo.TYPE_NULL
853                    ? inputType : EditorInfo.TYPE_CLASS_TEXT;
854        } else if (inputType != EditorInfo.TYPE_NULL) {
855            setInputType(inputType, true);
856            // If set, the input type overrides what was set using the deprecated singleLine flag.
857            singleLine = !isMultilineInputType(inputType);
858        } else if (phone) {
859            mInput = DialerKeyListener.getInstance();
860            mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE;
861        } else if (numeric != 0) {
862            mInput = DigitsKeyListener.getInstance((numeric & SIGNED) != 0,
863                                                   (numeric & DECIMAL) != 0);
864            inputType = EditorInfo.TYPE_CLASS_NUMBER;
865            if ((numeric & SIGNED) != 0) {
866                inputType |= EditorInfo.TYPE_NUMBER_FLAG_SIGNED;
867            }
868            if ((numeric & DECIMAL) != 0) {
869                inputType |= EditorInfo.TYPE_NUMBER_FLAG_DECIMAL;
870            }
871            mInputType = inputType;
872        } else if (autotext || autocap != -1) {
873            TextKeyListener.Capitalize cap;
874
875            inputType = EditorInfo.TYPE_CLASS_TEXT;
876
877            switch (autocap) {
878            case 1:
879                cap = TextKeyListener.Capitalize.SENTENCES;
880                inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES;
881                break;
882
883            case 2:
884                cap = TextKeyListener.Capitalize.WORDS;
885                inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
886                break;
887
888            case 3:
889                cap = TextKeyListener.Capitalize.CHARACTERS;
890                inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS;
891                break;
892
893            default:
894                cap = TextKeyListener.Capitalize.NONE;
895                break;
896            }
897
898            mInput = TextKeyListener.getInstance(autotext, cap);
899            mInputType = inputType;
900        } else if (mTextIsSelectable) {
901            // Prevent text changes from keyboard.
902            mInputType = EditorInfo.TYPE_NULL;
903            mInput = null;
904            bufferType = BufferType.SPANNABLE;
905            // Required to request focus while in touch mode.
906            setFocusableInTouchMode(true);
907            // So that selection can be changed using arrow keys and touch is handled.
908            setMovementMethod(ArrowKeyMovementMethod.getInstance());
909        } else if (editable) {
910            mInput = TextKeyListener.getInstance();
911            mInputType = EditorInfo.TYPE_CLASS_TEXT;
912        } else {
913            mInput = null;
914
915            switch (buffertype) {
916                case 0:
917                    bufferType = BufferType.NORMAL;
918                    break;
919                case 1:
920                    bufferType = BufferType.SPANNABLE;
921                    break;
922                case 2:
923                    bufferType = BufferType.EDITABLE;
924                    break;
925            }
926        }
927
928        // mInputType has been set from inputType, possibly modified by mInputMethod.
929        // Specialize mInputType to [web]password if we have a text class and the original input
930        // type was a password.
931        if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
932            if (password || passwordInputType) {
933                mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
934                        | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD;
935            }
936            if (webPasswordInputType) {
937                mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
938                        | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD;
939            }
940        } else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) {
941            if (numberPasswordInputType) {
942                mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
943                        | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD;
944            }
945        }
946
947        if (selectallonfocus) {
948            mSelectAllOnFocus = true;
949
950            if (bufferType == BufferType.NORMAL)
951                bufferType = BufferType.SPANNABLE;
952        }
953
954        setCompoundDrawablesWithIntrinsicBounds(
955            drawableLeft, drawableTop, drawableRight, drawableBottom);
956        setCompoundDrawablePadding(drawablePadding);
957
958        // Same as setSingleLine(), but make sure the transformation method and the maximum number
959        // of lines of height are unchanged for multi-line TextViews.
960        setInputTypeSingleLine(singleLine);
961        applySingleLine(singleLine, singleLine, singleLine);
962
963        if (singleLine && mInput == null && ellipsize < 0) {
964                ellipsize = 3; // END
965        }
966
967        switch (ellipsize) {
968            case 1:
969                setEllipsize(TextUtils.TruncateAt.START);
970                break;
971            case 2:
972                setEllipsize(TextUtils.TruncateAt.MIDDLE);
973                break;
974            case 3:
975                setEllipsize(TextUtils.TruncateAt.END);
976                break;
977            case 4:
978                setHorizontalFadingEdgeEnabled(true);
979                setEllipsize(TextUtils.TruncateAt.MARQUEE);
980                break;
981        }
982
983        setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000));
984        setHintTextColor(textColorHint);
985        setLinkTextColor(textColorLink);
986        if (textColorHighlight != 0) {
987            setHighlightColor(textColorHighlight);
988        }
989        setRawTextSize(textSize);
990
991        if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) {
992            setTransformationMethod(PasswordTransformationMethod.getInstance());
993            typefaceIndex = MONOSPACE;
994        } else if ((mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION))
995                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
996            typefaceIndex = MONOSPACE;
997        }
998
999        setTypefaceByIndex(typefaceIndex, styleIndex);
1000
1001        if (shadowcolor != 0) {
1002            setShadowLayer(r, dx, dy, shadowcolor);
1003        }
1004
1005        if (maxlength >= 0) {
1006            setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });
1007        } else {
1008            setFilters(NO_FILTERS);
1009        }
1010
1011        setText(text, bufferType);
1012        if (hint != null) setHint(hint);
1013
1014        /*
1015         * Views are not normally focusable unless specified to be.
1016         * However, TextViews that have input or movement methods *are*
1017         * focusable by default.
1018         */
1019        a = context.obtainStyledAttributes(attrs,
1020                                           com.android.internal.R.styleable.View,
1021                                           defStyle, 0);
1022
1023        boolean focusable = mMovement != null || mInput != null;
1024        boolean clickable = focusable;
1025        boolean longClickable = focusable;
1026
1027        n = a.getIndexCount();
1028        for (int i = 0; i < n; i++) {
1029            int attr = a.getIndex(i);
1030
1031            switch (attr) {
1032            case com.android.internal.R.styleable.View_focusable:
1033                focusable = a.getBoolean(attr, focusable);
1034                break;
1035
1036            case com.android.internal.R.styleable.View_clickable:
1037                clickable = a.getBoolean(attr, clickable);
1038                break;
1039
1040            case com.android.internal.R.styleable.View_longClickable:
1041                longClickable = a.getBoolean(attr, longClickable);
1042                break;
1043            }
1044        }
1045        a.recycle();
1046
1047        setFocusable(focusable);
1048        setClickable(clickable);
1049        setLongClickable(longClickable);
1050
1051        prepareCursorControllers();
1052
1053        final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
1054        final int touchSlop = viewConfiguration.getScaledTouchSlop();
1055        mSquaredTouchSlopDistance = touchSlop * touchSlop;
1056    }
1057
1058    private void setTypefaceByIndex(int typefaceIndex, int styleIndex) {
1059        Typeface tf = null;
1060        switch (typefaceIndex) {
1061            case SANS:
1062                tf = Typeface.SANS_SERIF;
1063                break;
1064
1065            case SERIF:
1066                tf = Typeface.SERIF;
1067                break;
1068
1069            case MONOSPACE:
1070                tf = Typeface.MONOSPACE;
1071                break;
1072        }
1073
1074        setTypeface(tf, styleIndex);
1075    }
1076
1077    @Override
1078    public void setEnabled(boolean enabled) {
1079        if (enabled == isEnabled()) {
1080            return;
1081        }
1082
1083        if (!enabled) {
1084            // Hide the soft input if the currently active TextView is disabled
1085            InputMethodManager imm = InputMethodManager.peekInstance();
1086            if (imm != null && imm.isActive(this)) {
1087                imm.hideSoftInputFromWindow(getWindowToken(), 0);
1088            }
1089        }
1090        super.setEnabled(enabled);
1091    }
1092
1093    /**
1094     * Sets the typeface and style in which the text should be displayed,
1095     * and turns on the fake bold and italic bits in the Paint if the
1096     * Typeface that you provided does not have all the bits in the
1097     * style that you specified.
1098     *
1099     * @attr ref android.R.styleable#TextView_typeface
1100     * @attr ref android.R.styleable#TextView_textStyle
1101     */
1102    public void setTypeface(Typeface tf, int style) {
1103        if (style > 0) {
1104            if (tf == null) {
1105                tf = Typeface.defaultFromStyle(style);
1106            } else {
1107                tf = Typeface.create(tf, style);
1108            }
1109
1110            setTypeface(tf);
1111            // now compute what (if any) algorithmic styling is needed
1112            int typefaceStyle = tf != null ? tf.getStyle() : 0;
1113            int need = style & ~typefaceStyle;
1114            mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
1115            mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
1116        } else {
1117            mTextPaint.setFakeBoldText(false);
1118            mTextPaint.setTextSkewX(0);
1119            setTypeface(tf);
1120        }
1121    }
1122
1123    /**
1124     * Subclasses override this to specify that they have a KeyListener
1125     * by default even if not specifically called for in the XML options.
1126     */
1127    protected boolean getDefaultEditable() {
1128        return false;
1129    }
1130
1131    /**
1132     * Subclasses override this to specify a default movement method.
1133     */
1134    protected MovementMethod getDefaultMovementMethod() {
1135        return null;
1136    }
1137
1138    /**
1139     * Return the text the TextView is displaying. If setText() was called with
1140     * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast
1141     * the return value from this method to Spannable or Editable, respectively.
1142     *
1143     * Note: The content of the return value should not be modified. If you want
1144     * a modifiable one, you should make your own copy first.
1145     */
1146    @ViewDebug.CapturedViewProperty
1147    public CharSequence getText() {
1148        return mText;
1149    }
1150
1151    /**
1152     * Returns the length, in characters, of the text managed by this TextView
1153     */
1154    public int length() {
1155        return mText.length();
1156    }
1157
1158    /**
1159     * Return the text the TextView is displaying as an Editable object.  If
1160     * the text is not editable, null is returned.
1161     *
1162     * @see #getText
1163     */
1164    public Editable getEditableText() {
1165        return (mText instanceof Editable) ? (Editable)mText : null;
1166    }
1167
1168    /**
1169     * @return the height of one standard line in pixels.  Note that markup
1170     * within the text can cause individual lines to be taller or shorter
1171     * than this height, and the layout may contain additional first-
1172     * or last-line padding.
1173     */
1174    public int getLineHeight() {
1175        return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd);
1176    }
1177
1178    /**
1179     * @return the Layout that is currently being used to display the text.
1180     * This can be null if the text or width has recently changes.
1181     */
1182    public final Layout getLayout() {
1183        return mLayout;
1184    }
1185
1186    /**
1187     * @return the current key listener for this TextView.
1188     * This will frequently be null for non-EditText TextViews.
1189     */
1190    public final KeyListener getKeyListener() {
1191        return mInput;
1192    }
1193
1194    /**
1195     * Sets the key listener to be used with this TextView.  This can be null
1196     * to disallow user input.  Note that this method has significant and
1197     * subtle interactions with soft keyboards and other input method:
1198     * see {@link KeyListener#getInputType() KeyListener.getContentType()}
1199     * for important details.  Calling this method will replace the current
1200     * content type of the text view with the content type returned by the
1201     * key listener.
1202     * <p>
1203     * Be warned that if you want a TextView with a key listener or movement
1204     * method not to be focusable, or if you want a TextView without a
1205     * key listener or movement method to be focusable, you must call
1206     * {@link #setFocusable} again after calling this to get the focusability
1207     * back the way you want it.
1208     *
1209     * @attr ref android.R.styleable#TextView_numeric
1210     * @attr ref android.R.styleable#TextView_digits
1211     * @attr ref android.R.styleable#TextView_phoneNumber
1212     * @attr ref android.R.styleable#TextView_inputMethod
1213     * @attr ref android.R.styleable#TextView_capitalize
1214     * @attr ref android.R.styleable#TextView_autoText
1215     */
1216    public void setKeyListener(KeyListener input) {
1217        setKeyListenerOnly(input);
1218        fixFocusableAndClickableSettings();
1219
1220        if (input != null) {
1221            try {
1222                mInputType = mInput.getInputType();
1223            } catch (IncompatibleClassChangeError e) {
1224                mInputType = EditorInfo.TYPE_CLASS_TEXT;
1225            }
1226            // Change inputType, without affecting transformation.
1227            // No need to applySingleLine since mSingleLine is unchanged.
1228            setInputTypeSingleLine(mSingleLine);
1229        } else {
1230            mInputType = EditorInfo.TYPE_NULL;
1231        }
1232
1233        InputMethodManager imm = InputMethodManager.peekInstance();
1234        if (imm != null) imm.restartInput(this);
1235    }
1236
1237    private void setKeyListenerOnly(KeyListener input) {
1238        mInput = input;
1239        if (mInput != null && !(mText instanceof Editable))
1240            setText(mText);
1241
1242        setFilters((Editable) mText, mFilters);
1243    }
1244
1245    /**
1246     * @return the movement method being used for this TextView.
1247     * This will frequently be null for non-EditText TextViews.
1248     */
1249    public final MovementMethod getMovementMethod() {
1250        return mMovement;
1251    }
1252
1253    /**
1254     * Sets the movement method (arrow key handler) to be used for
1255     * this TextView.  This can be null to disallow using the arrow keys
1256     * to move the cursor or scroll the view.
1257     * <p>
1258     * Be warned that if you want a TextView with a key listener or movement
1259     * method not to be focusable, or if you want a TextView without a
1260     * key listener or movement method to be focusable, you must call
1261     * {@link #setFocusable} again after calling this to get the focusability
1262     * back the way you want it.
1263     */
1264    public final void setMovementMethod(MovementMethod movement) {
1265        mMovement = movement;
1266
1267        if (mMovement != null && !(mText instanceof Spannable))
1268            setText(mText);
1269
1270        fixFocusableAndClickableSettings();
1271
1272        // SelectionModifierCursorController depends on textCanBeSelected, which depends on mMovement
1273        prepareCursorControllers();
1274    }
1275
1276    private void fixFocusableAndClickableSettings() {
1277        if ((mMovement != null) || mInput != null) {
1278            setFocusable(true);
1279            setClickable(true);
1280            setLongClickable(true);
1281        } else {
1282            setFocusable(false);
1283            setClickable(false);
1284            setLongClickable(false);
1285        }
1286    }
1287
1288    /**
1289     * @return the current transformation method for this TextView.
1290     * This will frequently be null except for single-line and password
1291     * fields.
1292     */
1293    public final TransformationMethod getTransformationMethod() {
1294        return mTransformation;
1295    }
1296
1297    /**
1298     * Sets the transformation that is applied to the text that this
1299     * TextView is displaying.
1300     *
1301     * @attr ref android.R.styleable#TextView_password
1302     * @attr ref android.R.styleable#TextView_singleLine
1303     */
1304    public final void setTransformationMethod(TransformationMethod method) {
1305        if (method == mTransformation) {
1306            // Avoid the setText() below if the transformation is
1307            // the same.
1308            return;
1309        }
1310        if (mTransformation != null) {
1311            if (mText instanceof Spannable) {
1312                ((Spannable) mText).removeSpan(mTransformation);
1313            }
1314        }
1315
1316        mTransformation = method;
1317
1318        setText(mText);
1319    }
1320
1321    /**
1322     * Returns the top padding of the view, plus space for the top
1323     * Drawable if any.
1324     */
1325    public int getCompoundPaddingTop() {
1326        final Drawables dr = mDrawables;
1327        if (dr == null || dr.mDrawableTop == null) {
1328            return mPaddingTop;
1329        } else {
1330            return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop;
1331        }
1332    }
1333
1334    /**
1335     * Returns the bottom padding of the view, plus space for the bottom
1336     * Drawable if any.
1337     */
1338    public int getCompoundPaddingBottom() {
1339        final Drawables dr = mDrawables;
1340        if (dr == null || dr.mDrawableBottom == null) {
1341            return mPaddingBottom;
1342        } else {
1343            return mPaddingBottom + dr.mDrawablePadding + dr.mDrawableSizeBottom;
1344        }
1345    }
1346
1347    /**
1348     * Returns the left padding of the view, plus space for the left
1349     * Drawable if any.
1350     */
1351    public int getCompoundPaddingLeft() {
1352        final Drawables dr = mDrawables;
1353        if (dr == null || dr.mDrawableLeft == null) {
1354            return mPaddingLeft;
1355        } else {
1356            return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft;
1357        }
1358    }
1359
1360    /**
1361     * Returns the right padding of the view, plus space for the right
1362     * Drawable if any.
1363     */
1364    public int getCompoundPaddingRight() {
1365        final Drawables dr = mDrawables;
1366        if (dr == null || dr.mDrawableRight == null) {
1367            return mPaddingRight;
1368        } else {
1369            return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight;
1370        }
1371    }
1372
1373    /**
1374     * Returns the extended top padding of the view, including both the
1375     * top Drawable if any and any extra space to keep more than maxLines
1376     * of text from showing.  It is only valid to call this after measuring.
1377     */
1378    public int getExtendedPaddingTop() {
1379        if (mMaxMode != LINES) {
1380            return getCompoundPaddingTop();
1381        }
1382
1383        if (mLayout.getLineCount() <= mMaximum) {
1384            return getCompoundPaddingTop();
1385        }
1386
1387        int top = getCompoundPaddingTop();
1388        int bottom = getCompoundPaddingBottom();
1389        int viewht = getHeight() - top - bottom;
1390        int layoutht = mLayout.getLineTop(mMaximum);
1391
1392        if (layoutht >= viewht) {
1393            return top;
1394        }
1395
1396        final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1397        if (gravity == Gravity.TOP) {
1398            return top;
1399        } else if (gravity == Gravity.BOTTOM) {
1400            return top + viewht - layoutht;
1401        } else { // (gravity == Gravity.CENTER_VERTICAL)
1402            return top + (viewht - layoutht) / 2;
1403        }
1404    }
1405
1406    /**
1407     * Returns the extended bottom padding of the view, including both the
1408     * bottom Drawable if any and any extra space to keep more than maxLines
1409     * of text from showing.  It is only valid to call this after measuring.
1410     */
1411    public int getExtendedPaddingBottom() {
1412        if (mMaxMode != LINES) {
1413            return getCompoundPaddingBottom();
1414        }
1415
1416        if (mLayout.getLineCount() <= mMaximum) {
1417            return getCompoundPaddingBottom();
1418        }
1419
1420        int top = getCompoundPaddingTop();
1421        int bottom = getCompoundPaddingBottom();
1422        int viewht = getHeight() - top - bottom;
1423        int layoutht = mLayout.getLineTop(mMaximum);
1424
1425        if (layoutht >= viewht) {
1426            return bottom;
1427        }
1428
1429        final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1430        if (gravity == Gravity.TOP) {
1431            return bottom + viewht - layoutht;
1432        } else if (gravity == Gravity.BOTTOM) {
1433            return bottom;
1434        } else { // (gravity == Gravity.CENTER_VERTICAL)
1435            return bottom + (viewht - layoutht) / 2;
1436        }
1437    }
1438
1439    /**
1440     * Returns the total left padding of the view, including the left
1441     * Drawable if any.
1442     */
1443    public int getTotalPaddingLeft() {
1444        return getCompoundPaddingLeft();
1445    }
1446
1447    /**
1448     * Returns the total right padding of the view, including the right
1449     * Drawable if any.
1450     */
1451    public int getTotalPaddingRight() {
1452        return getCompoundPaddingRight();
1453    }
1454
1455    /**
1456     * Returns the total top padding of the view, including the top
1457     * Drawable if any, the extra space to keep more than maxLines
1458     * from showing, and the vertical offset for gravity, if any.
1459     */
1460    public int getTotalPaddingTop() {
1461        return getExtendedPaddingTop() + getVerticalOffset(true);
1462    }
1463
1464    /**
1465     * Returns the total bottom padding of the view, including the bottom
1466     * Drawable if any, the extra space to keep more than maxLines
1467     * from showing, and the vertical offset for gravity, if any.
1468     */
1469    public int getTotalPaddingBottom() {
1470        return getExtendedPaddingBottom() + getBottomVerticalOffset(true);
1471    }
1472
1473    /**
1474     * Sets the Drawables (if any) to appear to the left of, above,
1475     * to the right of, and below the text.  Use null if you do not
1476     * want a Drawable there.  The Drawables must already have had
1477     * {@link Drawable#setBounds} called.
1478     *
1479     * @attr ref android.R.styleable#TextView_drawableLeft
1480     * @attr ref android.R.styleable#TextView_drawableTop
1481     * @attr ref android.R.styleable#TextView_drawableRight
1482     * @attr ref android.R.styleable#TextView_drawableBottom
1483     */
1484    public void setCompoundDrawables(Drawable left, Drawable top,
1485                                     Drawable right, Drawable bottom) {
1486        Drawables dr = mDrawables;
1487
1488        final boolean drawables = left != null || top != null
1489                || right != null || bottom != null;
1490
1491        if (!drawables) {
1492            // Clearing drawables...  can we free the data structure?
1493            if (dr != null) {
1494                if (dr.mDrawablePadding == 0) {
1495                    mDrawables = null;
1496                } else {
1497                    // We need to retain the last set padding, so just clear
1498                    // out all of the fields in the existing structure.
1499                    if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null);
1500                    dr.mDrawableLeft = null;
1501                    if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null);
1502                    dr.mDrawableTop = null;
1503                    if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null);
1504                    dr.mDrawableRight = null;
1505                    if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null);
1506                    dr.mDrawableBottom = null;
1507                    dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
1508                    dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
1509                    dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
1510                    dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
1511                }
1512            }
1513        } else {
1514            if (dr == null) {
1515                mDrawables = dr = new Drawables();
1516            }
1517
1518            if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) {
1519                dr.mDrawableLeft.setCallback(null);
1520            }
1521            dr.mDrawableLeft = left;
1522
1523            if (dr.mDrawableTop != top && dr.mDrawableTop != null) {
1524                dr.mDrawableTop.setCallback(null);
1525            }
1526            dr.mDrawableTop = top;
1527
1528            if (dr.mDrawableRight != right && dr.mDrawableRight != null) {
1529                dr.mDrawableRight.setCallback(null);
1530            }
1531            dr.mDrawableRight = right;
1532
1533            if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) {
1534                dr.mDrawableBottom.setCallback(null);
1535            }
1536            dr.mDrawableBottom = bottom;
1537
1538            final Rect compoundRect = dr.mCompoundRect;
1539            int[] state;
1540
1541            state = getDrawableState();
1542
1543            if (left != null) {
1544                left.setState(state);
1545                left.copyBounds(compoundRect);
1546                left.setCallback(this);
1547                dr.mDrawableSizeLeft = compoundRect.width();
1548                dr.mDrawableHeightLeft = compoundRect.height();
1549            } else {
1550                dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
1551            }
1552
1553            if (right != null) {
1554                right.setState(state);
1555                right.copyBounds(compoundRect);
1556                right.setCallback(this);
1557                dr.mDrawableSizeRight = compoundRect.width();
1558                dr.mDrawableHeightRight = compoundRect.height();
1559            } else {
1560                dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
1561            }
1562
1563            if (top != null) {
1564                top.setState(state);
1565                top.copyBounds(compoundRect);
1566                top.setCallback(this);
1567                dr.mDrawableSizeTop = compoundRect.height();
1568                dr.mDrawableWidthTop = compoundRect.width();
1569            } else {
1570                dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
1571            }
1572
1573            if (bottom != null) {
1574                bottom.setState(state);
1575                bottom.copyBounds(compoundRect);
1576                bottom.setCallback(this);
1577                dr.mDrawableSizeBottom = compoundRect.height();
1578                dr.mDrawableWidthBottom = compoundRect.width();
1579            } else {
1580                dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
1581            }
1582        }
1583
1584        invalidate();
1585        requestLayout();
1586    }
1587
1588    /**
1589     * Sets the Drawables (if any) to appear to the left of, above,
1590     * to the right of, and below the text.  Use 0 if you do not
1591     * want a Drawable there. The Drawables' bounds will be set to
1592     * their intrinsic bounds.
1593     *
1594     * @param left Resource identifier of the left Drawable.
1595     * @param top Resource identifier of the top Drawable.
1596     * @param right Resource identifier of the right Drawable.
1597     * @param bottom Resource identifier of the bottom Drawable.
1598     *
1599     * @attr ref android.R.styleable#TextView_drawableLeft
1600     * @attr ref android.R.styleable#TextView_drawableTop
1601     * @attr ref android.R.styleable#TextView_drawableRight
1602     * @attr ref android.R.styleable#TextView_drawableBottom
1603     */
1604    public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
1605        final Resources resources = getContext().getResources();
1606        setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null,
1607                top != 0 ? resources.getDrawable(top) : null,
1608                right != 0 ? resources.getDrawable(right) : null,
1609                bottom != 0 ? resources.getDrawable(bottom) : null);
1610    }
1611
1612    /**
1613     * Sets the Drawables (if any) to appear to the left of, above,
1614     * to the right of, and below the text.  Use null if you do not
1615     * want a Drawable there. The Drawables' bounds will be set to
1616     * their intrinsic bounds.
1617     *
1618     * @attr ref android.R.styleable#TextView_drawableLeft
1619     * @attr ref android.R.styleable#TextView_drawableTop
1620     * @attr ref android.R.styleable#TextView_drawableRight
1621     * @attr ref android.R.styleable#TextView_drawableBottom
1622     */
1623    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top,
1624            Drawable right, Drawable bottom) {
1625
1626        if (left != null) {
1627            left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
1628        }
1629        if (right != null) {
1630            right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
1631        }
1632        if (top != null) {
1633            top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
1634        }
1635        if (bottom != null) {
1636            bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
1637        }
1638        setCompoundDrawables(left, top, right, bottom);
1639    }
1640
1641    /**
1642     * Returns drawables for the left, top, right, and bottom borders.
1643     */
1644    public Drawable[] getCompoundDrawables() {
1645        final Drawables dr = mDrawables;
1646        if (dr != null) {
1647            return new Drawable[] {
1648                dr.mDrawableLeft, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom
1649            };
1650        } else {
1651            return new Drawable[] { null, null, null, null };
1652        }
1653    }
1654
1655    /**
1656     * Sets the size of the padding between the compound drawables and
1657     * the text.
1658     *
1659     * @attr ref android.R.styleable#TextView_drawablePadding
1660     */
1661    public void setCompoundDrawablePadding(int pad) {
1662        Drawables dr = mDrawables;
1663        if (pad == 0) {
1664            if (dr != null) {
1665                dr.mDrawablePadding = pad;
1666            }
1667        } else {
1668            if (dr == null) {
1669                mDrawables = dr = new Drawables();
1670            }
1671            dr.mDrawablePadding = pad;
1672        }
1673
1674        invalidate();
1675        requestLayout();
1676    }
1677
1678    /**
1679     * Returns the padding between the compound drawables and the text.
1680     */
1681    public int getCompoundDrawablePadding() {
1682        final Drawables dr = mDrawables;
1683        return dr != null ? dr.mDrawablePadding : 0;
1684    }
1685
1686    @Override
1687    public void setPadding(int left, int top, int right, int bottom) {
1688        if (left != mPaddingLeft ||
1689            right != mPaddingRight ||
1690            top != mPaddingTop ||
1691            bottom != mPaddingBottom) {
1692            nullLayouts();
1693        }
1694
1695        // the super call will requestLayout()
1696        super.setPadding(left, top, right, bottom);
1697        invalidate();
1698    }
1699
1700    /**
1701     * Gets the autolink mask of the text.  See {@link
1702     * android.text.util.Linkify#ALL Linkify.ALL} and peers for
1703     * possible values.
1704     *
1705     * @attr ref android.R.styleable#TextView_autoLink
1706     */
1707    public final int getAutoLinkMask() {
1708        return mAutoLinkMask;
1709    }
1710
1711    /**
1712     * Sets the text color, size, style, hint color, and highlight color
1713     * from the specified TextAppearance resource.
1714     */
1715    public void setTextAppearance(Context context, int resid) {
1716        TypedArray appearance =
1717            context.obtainStyledAttributes(resid,
1718                                           com.android.internal.R.styleable.TextAppearance);
1719
1720        int color;
1721        ColorStateList colors;
1722        int ts;
1723
1724        color = appearance.getColor(com.android.internal.R.styleable.TextAppearance_textColorHighlight, 0);
1725        if (color != 0) {
1726            setHighlightColor(color);
1727        }
1728
1729        colors = appearance.getColorStateList(com.android.internal.R.styleable.
1730                                              TextAppearance_textColor);
1731        if (colors != null) {
1732            setTextColor(colors);
1733        }
1734
1735        ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
1736                                              TextAppearance_textSize, 0);
1737        if (ts != 0) {
1738            setRawTextSize(ts);
1739        }
1740
1741        colors = appearance.getColorStateList(com.android.internal.R.styleable.
1742                                              TextAppearance_textColorHint);
1743        if (colors != null) {
1744            setHintTextColor(colors);
1745        }
1746
1747        colors = appearance.getColorStateList(com.android.internal.R.styleable.
1748                                              TextAppearance_textColorLink);
1749        if (colors != null) {
1750            setLinkTextColor(colors);
1751        }
1752
1753        int typefaceIndex, styleIndex;
1754
1755        typefaceIndex = appearance.getInt(com.android.internal.R.styleable.
1756                                          TextAppearance_typeface, -1);
1757        styleIndex = appearance.getInt(com.android.internal.R.styleable.
1758                                       TextAppearance_textStyle, -1);
1759
1760        setTypefaceByIndex(typefaceIndex, styleIndex);
1761
1762        appearance.recycle();
1763    }
1764
1765    /**
1766     * @return the size (in pixels) of the default text size in this TextView.
1767     */
1768    public float getTextSize() {
1769        return mTextPaint.getTextSize();
1770    }
1771
1772    /**
1773     * Set the default text size to the given value, interpreted as "scaled
1774     * pixel" units.  This size is adjusted based on the current density and
1775     * user font size preference.
1776     *
1777     * @param size The scaled pixel size.
1778     *
1779     * @attr ref android.R.styleable#TextView_textSize
1780     */
1781    @android.view.RemotableViewMethod
1782    public void setTextSize(float size) {
1783        setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
1784    }
1785
1786    /**
1787     * Set the default text size to a given unit and value.  See {@link
1788     * TypedValue} for the possible dimension units.
1789     *
1790     * @param unit The desired dimension unit.
1791     * @param size The desired size in the given units.
1792     *
1793     * @attr ref android.R.styleable#TextView_textSize
1794     */
1795    public void setTextSize(int unit, float size) {
1796        Context c = getContext();
1797        Resources r;
1798
1799        if (c == null)
1800            r = Resources.getSystem();
1801        else
1802            r = c.getResources();
1803
1804        setRawTextSize(TypedValue.applyDimension(
1805            unit, size, r.getDisplayMetrics()));
1806    }
1807
1808    private void setRawTextSize(float size) {
1809        if (size != mTextPaint.getTextSize()) {
1810            mTextPaint.setTextSize(size);
1811
1812            if (mLayout != null) {
1813                nullLayouts();
1814                requestLayout();
1815                invalidate();
1816            }
1817        }
1818    }
1819
1820    /**
1821     * @return the extent by which text is currently being stretched
1822     * horizontally.  This will usually be 1.
1823     */
1824    public float getTextScaleX() {
1825        return mTextPaint.getTextScaleX();
1826    }
1827
1828    /**
1829     * Sets the extent by which text should be stretched horizontally.
1830     *
1831     * @attr ref android.R.styleable#TextView_textScaleX
1832     */
1833    @android.view.RemotableViewMethod
1834    public void setTextScaleX(float size) {
1835        if (size != mTextPaint.getTextScaleX()) {
1836            mUserSetTextScaleX = true;
1837            mTextPaint.setTextScaleX(size);
1838
1839            if (mLayout != null) {
1840                nullLayouts();
1841                requestLayout();
1842                invalidate();
1843            }
1844        }
1845    }
1846
1847    /**
1848     * Sets the typeface and style in which the text should be displayed.
1849     * Note that not all Typeface families actually have bold and italic
1850     * variants, so you may need to use
1851     * {@link #setTypeface(Typeface, int)} to get the appearance
1852     * that you actually want.
1853     *
1854     * @attr ref android.R.styleable#TextView_typeface
1855     * @attr ref android.R.styleable#TextView_textStyle
1856     */
1857    public void setTypeface(Typeface tf) {
1858        if (mTextPaint.getTypeface() != tf) {
1859            mTextPaint.setTypeface(tf);
1860
1861            if (mLayout != null) {
1862                nullLayouts();
1863                requestLayout();
1864                invalidate();
1865            }
1866        }
1867    }
1868
1869    /**
1870     * @return the current typeface and style in which the text is being
1871     * displayed.
1872     */
1873    public Typeface getTypeface() {
1874        return mTextPaint.getTypeface();
1875    }
1876
1877    /**
1878     * Sets the text color for all the states (normal, selected,
1879     * focused) to be this color.
1880     *
1881     * @attr ref android.R.styleable#TextView_textColor
1882     */
1883    @android.view.RemotableViewMethod
1884    public void setTextColor(int color) {
1885        mTextColor = ColorStateList.valueOf(color);
1886        updateTextColors();
1887    }
1888
1889    /**
1890     * Sets the text color.
1891     *
1892     * @attr ref android.R.styleable#TextView_textColor
1893     */
1894    public void setTextColor(ColorStateList colors) {
1895        if (colors == null) {
1896            throw new NullPointerException();
1897        }
1898
1899        mTextColor = colors;
1900        updateTextColors();
1901    }
1902
1903    /**
1904     * Return the set of text colors.
1905     *
1906     * @return Returns the set of text colors.
1907     */
1908    public final ColorStateList getTextColors() {
1909        return mTextColor;
1910    }
1911
1912    /**
1913     * <p>Return the current color selected for normal text.</p>
1914     *
1915     * @return Returns the current text color.
1916     */
1917    public final int getCurrentTextColor() {
1918        return mCurTextColor;
1919    }
1920
1921    /**
1922     * Sets the color used to display the selection highlight.
1923     *
1924     * @attr ref android.R.styleable#TextView_textColorHighlight
1925     */
1926    @android.view.RemotableViewMethod
1927    public void setHighlightColor(int color) {
1928        if (mHighlightColor != color) {
1929            mHighlightColor = color;
1930            invalidate();
1931        }
1932    }
1933
1934    /**
1935     * Gives the text a shadow of the specified radius and color, the specified
1936     * distance from its normal position.
1937     *
1938     * @attr ref android.R.styleable#TextView_shadowColor
1939     * @attr ref android.R.styleable#TextView_shadowDx
1940     * @attr ref android.R.styleable#TextView_shadowDy
1941     * @attr ref android.R.styleable#TextView_shadowRadius
1942     */
1943    public void setShadowLayer(float radius, float dx, float dy, int color) {
1944        mTextPaint.setShadowLayer(radius, dx, dy, color);
1945
1946        mShadowRadius = radius;
1947        mShadowDx = dx;
1948        mShadowDy = dy;
1949
1950        invalidate();
1951    }
1952
1953    /**
1954     * @return the base paint used for the text.  Please use this only to
1955     * consult the Paint's properties and not to change them.
1956     */
1957    public TextPaint getPaint() {
1958        return mTextPaint;
1959    }
1960
1961    /**
1962     * Sets the autolink mask of the text.  See {@link
1963     * android.text.util.Linkify#ALL Linkify.ALL} and peers for
1964     * possible values.
1965     *
1966     * @attr ref android.R.styleable#TextView_autoLink
1967     */
1968    @android.view.RemotableViewMethod
1969    public final void setAutoLinkMask(int mask) {
1970        mAutoLinkMask = mask;
1971    }
1972
1973    /**
1974     * Sets whether the movement method will automatically be set to
1975     * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been
1976     * set to nonzero and links are detected in {@link #setText}.
1977     * The default is true.
1978     *
1979     * @attr ref android.R.styleable#TextView_linksClickable
1980     */
1981    @android.view.RemotableViewMethod
1982    public final void setLinksClickable(boolean whether) {
1983        mLinksClickable = whether;
1984    }
1985
1986    /**
1987     * Returns whether the movement method will automatically be set to
1988     * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been
1989     * set to nonzero and links are detected in {@link #setText}.
1990     * The default is true.
1991     *
1992     * @attr ref android.R.styleable#TextView_linksClickable
1993     */
1994    public final boolean getLinksClickable() {
1995        return mLinksClickable;
1996    }
1997
1998    /**
1999     * Returns the list of URLSpans attached to the text
2000     * (by {@link Linkify} or otherwise) if any.  You can call
2001     * {@link URLSpan#getURL} on them to find where they link to
2002     * or use {@link Spanned#getSpanStart} and {@link Spanned#getSpanEnd}
2003     * to find the region of the text they are attached to.
2004     */
2005    public URLSpan[] getUrls() {
2006        if (mText instanceof Spanned) {
2007            return ((Spanned) mText).getSpans(0, mText.length(), URLSpan.class);
2008        } else {
2009            return new URLSpan[0];
2010        }
2011    }
2012
2013    /**
2014     * Sets the color of the hint text.
2015     *
2016     * @attr ref android.R.styleable#TextView_textColorHint
2017     */
2018    @android.view.RemotableViewMethod
2019    public final void setHintTextColor(int color) {
2020        mHintTextColor = ColorStateList.valueOf(color);
2021        updateTextColors();
2022    }
2023
2024    /**
2025     * Sets the color of the hint text.
2026     *
2027     * @attr ref android.R.styleable#TextView_textColorHint
2028     */
2029    public final void setHintTextColor(ColorStateList colors) {
2030        mHintTextColor = colors;
2031        updateTextColors();
2032    }
2033
2034    /**
2035     * <p>Return the color used to paint the hint text.</p>
2036     *
2037     * @return Returns the list of hint text colors.
2038     */
2039    public final ColorStateList getHintTextColors() {
2040        return mHintTextColor;
2041    }
2042
2043    /**
2044     * <p>Return the current color selected to paint the hint text.</p>
2045     *
2046     * @return Returns the current hint text color.
2047     */
2048    public final int getCurrentHintTextColor() {
2049        return mHintTextColor != null ? mCurHintTextColor : mCurTextColor;
2050    }
2051
2052    /**
2053     * Sets the color of links in the text.
2054     *
2055     * @attr ref android.R.styleable#TextView_textColorLink
2056     */
2057    @android.view.RemotableViewMethod
2058    public final void setLinkTextColor(int color) {
2059        mLinkTextColor = ColorStateList.valueOf(color);
2060        updateTextColors();
2061    }
2062
2063    /**
2064     * Sets the color of links in the text.
2065     *
2066     * @attr ref android.R.styleable#TextView_textColorLink
2067     */
2068    public final void setLinkTextColor(ColorStateList colors) {
2069        mLinkTextColor = colors;
2070        updateTextColors();
2071    }
2072
2073    /**
2074     * <p>Returns the color used to paint links in the text.</p>
2075     *
2076     * @return Returns the list of link text colors.
2077     */
2078    public final ColorStateList getLinkTextColors() {
2079        return mLinkTextColor;
2080    }
2081
2082    /**
2083     * Sets the horizontal alignment of the text and the
2084     * vertical gravity that will be used when there is extra space
2085     * in the TextView beyond what is required for the text itself.
2086     *
2087     * @see android.view.Gravity
2088     * @attr ref android.R.styleable#TextView_gravity
2089     */
2090    public void setGravity(int gravity) {
2091        if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == 0) {
2092            gravity |= Gravity.LEFT;
2093        }
2094        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
2095            gravity |= Gravity.TOP;
2096        }
2097
2098        boolean newLayout = false;
2099
2100        if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) !=
2101            (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK)) {
2102            newLayout = true;
2103        }
2104
2105        if (gravity != mGravity) {
2106            invalidate();
2107        }
2108
2109        mGravity = gravity;
2110
2111        if (mLayout != null && newLayout) {
2112            // XXX this is heavy-handed because no actual content changes.
2113            int want = mLayout.getWidth();
2114            int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();
2115
2116            makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
2117                          mRight - mLeft - getCompoundPaddingLeft() -
2118                          getCompoundPaddingRight(), true);
2119        }
2120    }
2121
2122    /**
2123     * Returns the horizontal and vertical alignment of this TextView.
2124     *
2125     * @see android.view.Gravity
2126     * @attr ref android.R.styleable#TextView_gravity
2127     */
2128    public int getGravity() {
2129        return mGravity;
2130    }
2131
2132    /**
2133     * @return the flags on the Paint being used to display the text.
2134     * @see Paint#getFlags
2135     */
2136    public int getPaintFlags() {
2137        return mTextPaint.getFlags();
2138    }
2139
2140    /**
2141     * Sets flags on the Paint being used to display the text and
2142     * reflows the text if they are different from the old flags.
2143     * @see Paint#setFlags
2144     */
2145    @android.view.RemotableViewMethod
2146    public void setPaintFlags(int flags) {
2147        if (mTextPaint.getFlags() != flags) {
2148            mTextPaint.setFlags(flags);
2149
2150            if (mLayout != null) {
2151                nullLayouts();
2152                requestLayout();
2153                invalidate();
2154            }
2155        }
2156    }
2157
2158    /**
2159     * Sets whether the text should be allowed to be wider than the
2160     * View is.  If false, it will be wrapped to the width of the View.
2161     *
2162     * @attr ref android.R.styleable#TextView_scrollHorizontally
2163     */
2164    public void setHorizontallyScrolling(boolean whether) {
2165        mHorizontallyScrolling = whether;
2166
2167        if (mLayout != null) {
2168            nullLayouts();
2169            requestLayout();
2170            invalidate();
2171        }
2172    }
2173
2174    /**
2175     * Makes the TextView at least this many lines tall.
2176     *
2177     * Setting this value overrides any other (minimum) height setting. A single line TextView will
2178     * set this value to 1.
2179     *
2180     * @attr ref android.R.styleable#TextView_minLines
2181     */
2182    @android.view.RemotableViewMethod
2183    public void setMinLines(int minlines) {
2184        mMinimum = minlines;
2185        mMinMode = LINES;
2186
2187        requestLayout();
2188        invalidate();
2189    }
2190
2191    /**
2192     * Makes the TextView at least this many pixels tall.
2193     *
2194     * Setting this value overrides any other (minimum) number of lines setting.
2195     *
2196     * @attr ref android.R.styleable#TextView_minHeight
2197     */
2198    @android.view.RemotableViewMethod
2199    public void setMinHeight(int minHeight) {
2200        mMinimum = minHeight;
2201        mMinMode = PIXELS;
2202
2203        requestLayout();
2204        invalidate();
2205    }
2206
2207    /**
2208     * Makes the TextView at most this many lines tall.
2209     *
2210     * Setting this value overrides any other (maximum) height setting.
2211     *
2212     * @attr ref android.R.styleable#TextView_maxLines
2213     */
2214    @android.view.RemotableViewMethod
2215    public void setMaxLines(int maxlines) {
2216        mMaximum = maxlines;
2217        mMaxMode = LINES;
2218
2219        requestLayout();
2220        invalidate();
2221    }
2222
2223    /**
2224     * Makes the TextView at most this many pixels tall.  This option is mutually exclusive with the
2225     * {@link #setMaxLines(int)} method.
2226     *
2227     * Setting this value overrides any other (maximum) number of lines setting.
2228     *
2229     * @attr ref android.R.styleable#TextView_maxHeight
2230     */
2231    @android.view.RemotableViewMethod
2232    public void setMaxHeight(int maxHeight) {
2233        mMaximum = maxHeight;
2234        mMaxMode = PIXELS;
2235
2236        requestLayout();
2237        invalidate();
2238    }
2239
2240    /**
2241     * Makes the TextView exactly this many lines tall.
2242     *
2243     * Note that setting this value overrides any other (minimum / maximum) number of lines or
2244     * height setting. A single line TextView will set this value to 1.
2245     *
2246     * @attr ref android.R.styleable#TextView_lines
2247     */
2248    @android.view.RemotableViewMethod
2249    public void setLines(int lines) {
2250        mMaximum = mMinimum = lines;
2251        mMaxMode = mMinMode = LINES;
2252
2253        requestLayout();
2254        invalidate();
2255    }
2256
2257    /**
2258     * Makes the TextView exactly this many pixels tall.
2259     * You could do the same thing by specifying this number in the
2260     * LayoutParams.
2261     *
2262     * Note that setting this value overrides any other (minimum / maximum) number of lines or
2263     * height setting.
2264     *
2265     * @attr ref android.R.styleable#TextView_height
2266     */
2267    @android.view.RemotableViewMethod
2268    public void setHeight(int pixels) {
2269        mMaximum = mMinimum = pixels;
2270        mMaxMode = mMinMode = PIXELS;
2271
2272        requestLayout();
2273        invalidate();
2274    }
2275
2276    /**
2277     * Makes the TextView at least this many ems wide
2278     *
2279     * @attr ref android.R.styleable#TextView_minEms
2280     */
2281    @android.view.RemotableViewMethod
2282    public void setMinEms(int minems) {
2283        mMinWidth = minems;
2284        mMinWidthMode = EMS;
2285
2286        requestLayout();
2287        invalidate();
2288    }
2289
2290    /**
2291     * Makes the TextView at least this many pixels wide
2292     *
2293     * @attr ref android.R.styleable#TextView_minWidth
2294     */
2295    @android.view.RemotableViewMethod
2296    public void setMinWidth(int minpixels) {
2297        mMinWidth = minpixels;
2298        mMinWidthMode = PIXELS;
2299
2300        requestLayout();
2301        invalidate();
2302    }
2303
2304    /**
2305     * Makes the TextView at most this many ems wide
2306     *
2307     * @attr ref android.R.styleable#TextView_maxEms
2308     */
2309    @android.view.RemotableViewMethod
2310    public void setMaxEms(int maxems) {
2311        mMaxWidth = maxems;
2312        mMaxWidthMode = EMS;
2313
2314        requestLayout();
2315        invalidate();
2316    }
2317
2318    /**
2319     * Makes the TextView at most this many pixels wide
2320     *
2321     * @attr ref android.R.styleable#TextView_maxWidth
2322     */
2323    @android.view.RemotableViewMethod
2324    public void setMaxWidth(int maxpixels) {
2325        mMaxWidth = maxpixels;
2326        mMaxWidthMode = PIXELS;
2327
2328        requestLayout();
2329        invalidate();
2330    }
2331
2332    /**
2333     * Makes the TextView exactly this many ems wide
2334     *
2335     * @attr ref android.R.styleable#TextView_ems
2336     */
2337    @android.view.RemotableViewMethod
2338    public void setEms(int ems) {
2339        mMaxWidth = mMinWidth = ems;
2340        mMaxWidthMode = mMinWidthMode = EMS;
2341
2342        requestLayout();
2343        invalidate();
2344    }
2345
2346    /**
2347     * Makes the TextView exactly this many pixels wide.
2348     * You could do the same thing by specifying this number in the
2349     * LayoutParams.
2350     *
2351     * @attr ref android.R.styleable#TextView_width
2352     */
2353    @android.view.RemotableViewMethod
2354    public void setWidth(int pixels) {
2355        mMaxWidth = mMinWidth = pixels;
2356        mMaxWidthMode = mMinWidthMode = PIXELS;
2357
2358        requestLayout();
2359        invalidate();
2360    }
2361
2362
2363    /**
2364     * Sets line spacing for this TextView.  Each line will have its height
2365     * multiplied by <code>mult</code> and have <code>add</code> added to it.
2366     *
2367     * @attr ref android.R.styleable#TextView_lineSpacingExtra
2368     * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
2369     */
2370    public void setLineSpacing(float add, float mult) {
2371        mSpacingMult = mult;
2372        mSpacingAdd = add;
2373
2374        if (mLayout != null) {
2375            nullLayouts();
2376            requestLayout();
2377            invalidate();
2378        }
2379    }
2380
2381    /**
2382     * Convenience method: Append the specified text to the TextView's
2383     * display buffer, upgrading it to BufferType.EDITABLE if it was
2384     * not already editable.
2385     */
2386    public final void append(CharSequence text) {
2387        append(text, 0, text.length());
2388    }
2389
2390    /**
2391     * Convenience method: Append the specified text slice to the TextView's
2392     * display buffer, upgrading it to BufferType.EDITABLE if it was
2393     * not already editable.
2394     */
2395    public void append(CharSequence text, int start, int end) {
2396        if (!(mText instanceof Editable)) {
2397            setText(mText, BufferType.EDITABLE);
2398        }
2399
2400        ((Editable) mText).append(text, start, end);
2401    }
2402
2403    private void updateTextColors() {
2404        boolean inval = false;
2405        int color = mTextColor.getColorForState(getDrawableState(), 0);
2406        if (color != mCurTextColor) {
2407            mCurTextColor = color;
2408            inval = true;
2409        }
2410        if (mLinkTextColor != null) {
2411            color = mLinkTextColor.getColorForState(getDrawableState(), 0);
2412            if (color != mTextPaint.linkColor) {
2413                mTextPaint.linkColor = color;
2414                inval = true;
2415            }
2416        }
2417        if (mHintTextColor != null) {
2418            color = mHintTextColor.getColorForState(getDrawableState(), 0);
2419            if (color != mCurHintTextColor && mText.length() == 0) {
2420                mCurHintTextColor = color;
2421                inval = true;
2422            }
2423        }
2424        if (inval) {
2425            invalidate();
2426        }
2427    }
2428
2429    @Override
2430    protected void drawableStateChanged() {
2431        super.drawableStateChanged();
2432        if (mTextColor != null && mTextColor.isStateful()
2433                || (mHintTextColor != null && mHintTextColor.isStateful())
2434                || (mLinkTextColor != null && mLinkTextColor.isStateful())) {
2435            updateTextColors();
2436        }
2437
2438        final Drawables dr = mDrawables;
2439        if (dr != null) {
2440            int[] state = getDrawableState();
2441            if (dr.mDrawableTop != null && dr.mDrawableTop.isStateful()) {
2442                dr.mDrawableTop.setState(state);
2443            }
2444            if (dr.mDrawableBottom != null && dr.mDrawableBottom.isStateful()) {
2445                dr.mDrawableBottom.setState(state);
2446            }
2447            if (dr.mDrawableLeft != null && dr.mDrawableLeft.isStateful()) {
2448                dr.mDrawableLeft.setState(state);
2449            }
2450            if (dr.mDrawableRight != null && dr.mDrawableRight.isStateful()) {
2451                dr.mDrawableRight.setState(state);
2452            }
2453        }
2454    }
2455
2456    /**
2457     * User interface state that is stored by TextView for implementing
2458     * {@link View#onSaveInstanceState}.
2459     */
2460    public static class SavedState extends BaseSavedState {
2461        int selStart;
2462        int selEnd;
2463        CharSequence text;
2464        boolean frozenWithFocus;
2465        CharSequence error;
2466
2467        SavedState(Parcelable superState) {
2468            super(superState);
2469        }
2470
2471        @Override
2472        public void writeToParcel(Parcel out, int flags) {
2473            super.writeToParcel(out, flags);
2474            out.writeInt(selStart);
2475            out.writeInt(selEnd);
2476            out.writeInt(frozenWithFocus ? 1 : 0);
2477            TextUtils.writeToParcel(text, out, flags);
2478
2479            if (error == null) {
2480                out.writeInt(0);
2481            } else {
2482                out.writeInt(1);
2483                TextUtils.writeToParcel(error, out, flags);
2484            }
2485        }
2486
2487        @Override
2488        public String toString() {
2489            String str = "TextView.SavedState{"
2490                    + Integer.toHexString(System.identityHashCode(this))
2491                    + " start=" + selStart + " end=" + selEnd;
2492            if (text != null) {
2493                str += " text=" + text;
2494            }
2495            return str + "}";
2496        }
2497
2498        @SuppressWarnings("hiding")
2499        public static final Parcelable.Creator<SavedState> CREATOR
2500                = new Parcelable.Creator<SavedState>() {
2501            public SavedState createFromParcel(Parcel in) {
2502                return new SavedState(in);
2503            }
2504
2505            public SavedState[] newArray(int size) {
2506                return new SavedState[size];
2507            }
2508        };
2509
2510        private SavedState(Parcel in) {
2511            super(in);
2512            selStart = in.readInt();
2513            selEnd = in.readInt();
2514            frozenWithFocus = (in.readInt() != 0);
2515            text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
2516
2517            if (in.readInt() != 0) {
2518                error = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
2519            }
2520        }
2521    }
2522
2523    @Override
2524    public Parcelable onSaveInstanceState() {
2525        Parcelable superState = super.onSaveInstanceState();
2526
2527        // Save state if we are forced to
2528        boolean save = mFreezesText;
2529        int start = 0;
2530        int end = 0;
2531
2532        if (mText != null) {
2533            start = getSelectionStart();
2534            end = getSelectionEnd();
2535            if (start >= 0 || end >= 0) {
2536                // Or save state if there is a selection
2537                save = true;
2538            }
2539        }
2540
2541        if (save) {
2542            SavedState ss = new SavedState(superState);
2543            // XXX Should also save the current scroll position!
2544            ss.selStart = start;
2545            ss.selEnd = end;
2546
2547            if (mText instanceof Spanned) {
2548                /*
2549                 * Calling setText() strips off any ChangeWatchers;
2550                 * strip them now to avoid leaking references.
2551                 * But do it to a copy so that if there are any
2552                 * further changes to the text of this view, it
2553                 * won't get into an inconsistent state.
2554                 */
2555
2556                Spannable sp = new SpannableString(mText);
2557
2558                for (ChangeWatcher cw :
2559                     sp.getSpans(0, sp.length(), ChangeWatcher.class)) {
2560                    sp.removeSpan(cw);
2561                }
2562
2563                ss.text = sp;
2564            } else {
2565                ss.text = mText.toString();
2566            }
2567
2568            if (isFocused() && start >= 0 && end >= 0) {
2569                ss.frozenWithFocus = true;
2570            }
2571
2572            ss.error = mError;
2573
2574            return ss;
2575        }
2576
2577        return superState;
2578    }
2579
2580    @Override
2581    public void onRestoreInstanceState(Parcelable state) {
2582        if (!(state instanceof SavedState)) {
2583            super.onRestoreInstanceState(state);
2584            return;
2585        }
2586
2587        SavedState ss = (SavedState)state;
2588        super.onRestoreInstanceState(ss.getSuperState());
2589
2590        // XXX restore buffer type too, as well as lots of other stuff
2591        if (ss.text != null) {
2592            setText(ss.text);
2593        }
2594
2595        if (ss.selStart >= 0 && ss.selEnd >= 0) {
2596            if (mText instanceof Spannable) {
2597                int len = mText.length();
2598
2599                if (ss.selStart > len || ss.selEnd > len) {
2600                    String restored = "";
2601
2602                    if (ss.text != null) {
2603                        restored = "(restored) ";
2604                    }
2605
2606                    Log.e(LOG_TAG, "Saved cursor position " + ss.selStart +
2607                          "/" + ss.selEnd + " out of range for " + restored +
2608                          "text " + mText);
2609                } else {
2610                    Selection.setSelection((Spannable) mText, ss.selStart,
2611                                           ss.selEnd);
2612
2613                    if (ss.frozenWithFocus) {
2614                        mFrozenWithFocus = true;
2615                    }
2616                }
2617            }
2618        }
2619
2620        if (ss.error != null) {
2621            final CharSequence error = ss.error;
2622            // Display the error later, after the first layout pass
2623            post(new Runnable() {
2624                public void run() {
2625                    setError(error);
2626                }
2627            });
2628        }
2629    }
2630
2631    /**
2632     * Control whether this text view saves its entire text contents when
2633     * freezing to an icicle, in addition to dynamic state such as cursor
2634     * position.  By default this is false, not saving the text.  Set to true
2635     * if the text in the text view is not being saved somewhere else in
2636     * persistent storage (such as in a content provider) so that if the
2637     * view is later thawed the user will not lose their data.
2638     *
2639     * @param freezesText Controls whether a frozen icicle should include the
2640     * entire text data: true to include it, false to not.
2641     *
2642     * @attr ref android.R.styleable#TextView_freezesText
2643     */
2644    @android.view.RemotableViewMethod
2645    public void setFreezesText(boolean freezesText) {
2646        mFreezesText = freezesText;
2647    }
2648
2649    /**
2650     * Return whether this text view is including its entire text contents
2651     * in frozen icicles.
2652     *
2653     * @return Returns true if text is included, false if it isn't.
2654     *
2655     * @see #setFreezesText
2656     */
2657    public boolean getFreezesText() {
2658        return mFreezesText;
2659    }
2660
2661    ///////////////////////////////////////////////////////////////////////////
2662
2663    /**
2664     * Sets the Factory used to create new Editables.
2665     */
2666    public final void setEditableFactory(Editable.Factory factory) {
2667        mEditableFactory = factory;
2668        setText(mText);
2669    }
2670
2671    /**
2672     * Sets the Factory used to create new Spannables.
2673     */
2674    public final void setSpannableFactory(Spannable.Factory factory) {
2675        mSpannableFactory = factory;
2676        setText(mText);
2677    }
2678
2679    /**
2680     * Sets the string value of the TextView. TextView <em>does not</em> accept
2681     * HTML-like formatting, which you can do with text strings in XML resource files.
2682     * To style your strings, attach android.text.style.* objects to a
2683     * {@link android.text.SpannableString SpannableString}, or see the
2684     * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources">
2685     * Available Resource Types</a> documentation for an example of setting
2686     * formatted text in the XML resource file.
2687     *
2688     * @attr ref android.R.styleable#TextView_text
2689     */
2690    @android.view.RemotableViewMethod
2691    public final void setText(CharSequence text) {
2692        setText(text, mBufferType);
2693    }
2694
2695    /**
2696     * Like {@link #setText(CharSequence)},
2697     * except that the cursor position (if any) is retained in the new text.
2698     *
2699     * @param text The new text to place in the text view.
2700     *
2701     * @see #setText(CharSequence)
2702     */
2703    @android.view.RemotableViewMethod
2704    public final void setTextKeepState(CharSequence text) {
2705        setTextKeepState(text, mBufferType);
2706    }
2707
2708    /**
2709     * Sets the text that this TextView is to display (see
2710     * {@link #setText(CharSequence)}) and also sets whether it is stored
2711     * in a styleable/spannable buffer and whether it is editable.
2712     *
2713     * @attr ref android.R.styleable#TextView_text
2714     * @attr ref android.R.styleable#TextView_bufferType
2715     */
2716    public void setText(CharSequence text, BufferType type) {
2717        setText(text, type, true, 0);
2718
2719        if (mCharWrapper != null) {
2720            mCharWrapper.mChars = null;
2721        }
2722    }
2723
2724    private void setText(CharSequence text, BufferType type,
2725                         boolean notifyBefore, int oldlen) {
2726        if (text == null) {
2727            text = "";
2728        }
2729
2730        if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f);
2731
2732        if (text instanceof Spanned &&
2733            ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
2734            setHorizontalFadingEdgeEnabled(true);
2735            setEllipsize(TextUtils.TruncateAt.MARQUEE);
2736        }
2737
2738        int n = mFilters.length;
2739        for (int i = 0; i < n; i++) {
2740            CharSequence out = mFilters[i].filter(text, 0, text.length(),
2741                                                  EMPTY_SPANNED, 0, 0);
2742            if (out != null) {
2743                text = out;
2744            }
2745        }
2746
2747        if (notifyBefore) {
2748            if (mText != null) {
2749                oldlen = mText.length();
2750                sendBeforeTextChanged(mText, 0, oldlen, text.length());
2751            } else {
2752                sendBeforeTextChanged("", 0, 0, text.length());
2753            }
2754        }
2755
2756        boolean needEditableForNotification = false;
2757
2758        if (mListeners != null && mListeners.size() != 0) {
2759            needEditableForNotification = true;
2760        }
2761
2762        if (type == BufferType.EDITABLE || mInput != null ||
2763            needEditableForNotification) {
2764            Editable t = mEditableFactory.newEditable(text);
2765            text = t;
2766            setFilters(t, mFilters);
2767            InputMethodManager imm = InputMethodManager.peekInstance();
2768            if (imm != null) imm.restartInput(this);
2769        } else if (type == BufferType.SPANNABLE || mMovement != null) {
2770            text = mSpannableFactory.newSpannable(text);
2771        } else if (!(text instanceof CharWrapper)) {
2772            text = TextUtils.stringOrSpannedString(text);
2773        }
2774
2775        if (mAutoLinkMask != 0) {
2776            Spannable s2;
2777
2778            if (type == BufferType.EDITABLE || text instanceof Spannable) {
2779                s2 = (Spannable) text;
2780            } else {
2781                s2 = mSpannableFactory.newSpannable(text);
2782            }
2783
2784            if (Linkify.addLinks(s2, mAutoLinkMask)) {
2785                text = s2;
2786                type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;
2787
2788                /*
2789                 * We must go ahead and set the text before changing the
2790                 * movement method, because setMovementMethod() may call
2791                 * setText() again to try to upgrade the buffer type.
2792                 */
2793                mText = text;
2794
2795                // Do not change the movement method for text that support text selection as it
2796                // would prevent an arbitrary cursor displacement.
2797                final boolean hasTextSelection = this instanceof EditText || mTextIsSelectable;
2798                if (mLinksClickable && !hasTextSelection) {
2799                    setMovementMethod(LinkMovementMethod.getInstance());
2800                }
2801            }
2802        }
2803
2804        mBufferType = type;
2805        mText = text;
2806
2807        if (mTransformation == null)
2808            mTransformed = text;
2809        else
2810            mTransformed = mTransformation.getTransformation(text, this);
2811
2812        final int textLength = text.length();
2813
2814        if (text instanceof Spannable) {
2815            Spannable sp = (Spannable) text;
2816
2817            // Remove any ChangeWatchers that might have come
2818            // from other TextViews.
2819            final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
2820            final int count = watchers.length;
2821            for (int i = 0; i < count; i++)
2822                sp.removeSpan(watchers[i]);
2823
2824            if (mChangeWatcher == null)
2825                mChangeWatcher = new ChangeWatcher();
2826
2827            sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE |
2828                       (PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));
2829
2830            if (mInput != null) {
2831                sp.setSpan(mInput, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
2832            }
2833
2834            if (mTransformation != null) {
2835                sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
2836
2837            }
2838
2839            if (mMovement != null) {
2840                mMovement.initialize(this, (Spannable) text);
2841
2842                /*
2843                 * Initializing the movement method will have set the
2844                 * selection, so reset mSelectionMoved to keep that from
2845                 * interfering with the normal on-focus selection-setting.
2846                 */
2847                mSelectionMoved = false;
2848            }
2849        }
2850
2851        if (mLayout != null) {
2852            checkForRelayout();
2853        }
2854
2855        sendOnTextChanged(text, 0, oldlen, textLength);
2856        onTextChanged(text, 0, oldlen, textLength);
2857
2858        if (needEditableForNotification) {
2859            sendAfterTextChanged((Editable) text);
2860        }
2861
2862        // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
2863        prepareCursorControllers();
2864    }
2865
2866    /**
2867     * Sets the TextView to display the specified slice of the specified
2868     * char array.  You must promise that you will not change the contents
2869     * of the array except for right before another call to setText(),
2870     * since the TextView has no way to know that the text
2871     * has changed and that it needs to invalidate and re-layout.
2872     */
2873    public final void setText(char[] text, int start, int len) {
2874        int oldlen = 0;
2875
2876        if (start < 0 || len < 0 || start + len > text.length) {
2877            throw new IndexOutOfBoundsException(start + ", " + len);
2878        }
2879
2880        /*
2881         * We must do the before-notification here ourselves because if
2882         * the old text is a CharWrapper we destroy it before calling
2883         * into the normal path.
2884         */
2885        if (mText != null) {
2886            oldlen = mText.length();
2887            sendBeforeTextChanged(mText, 0, oldlen, len);
2888        } else {
2889            sendBeforeTextChanged("", 0, 0, len);
2890        }
2891
2892        if (mCharWrapper == null) {
2893            mCharWrapper = new CharWrapper(text, start, len);
2894        } else {
2895            mCharWrapper.set(text, start, len);
2896        }
2897
2898        setText(mCharWrapper, mBufferType, false, oldlen);
2899    }
2900
2901    private static class CharWrapper
2902            implements CharSequence, GetChars, GraphicsOperations {
2903        private char[] mChars;
2904        private int mStart, mLength;
2905
2906        public CharWrapper(char[] chars, int start, int len) {
2907            mChars = chars;
2908            mStart = start;
2909            mLength = len;
2910        }
2911
2912        /* package */ void set(char[] chars, int start, int len) {
2913            mChars = chars;
2914            mStart = start;
2915            mLength = len;
2916        }
2917
2918        public int length() {
2919            return mLength;
2920        }
2921
2922        public char charAt(int off) {
2923            return mChars[off + mStart];
2924        }
2925
2926        @Override
2927        public String toString() {
2928            return new String(mChars, mStart, mLength);
2929        }
2930
2931        public CharSequence subSequence(int start, int end) {
2932            if (start < 0 || end < 0 || start > mLength || end > mLength) {
2933                throw new IndexOutOfBoundsException(start + ", " + end);
2934            }
2935
2936            return new String(mChars, start + mStart, end - start);
2937        }
2938
2939        public void getChars(int start, int end, char[] buf, int off) {
2940            if (start < 0 || end < 0 || start > mLength || end > mLength) {
2941                throw new IndexOutOfBoundsException(start + ", " + end);
2942            }
2943
2944            System.arraycopy(mChars, start + mStart, buf, off, end - start);
2945        }
2946
2947        public void drawText(Canvas c, int start, int end,
2948                             float x, float y, Paint p) {
2949            c.drawText(mChars, start + mStart, end - start, x, y, p);
2950        }
2951
2952        public void drawTextRun(Canvas c, int start, int end,
2953                int contextStart, int contextEnd, float x, float y, int flags, Paint p) {
2954            int count = end - start;
2955            int contextCount = contextEnd - contextStart;
2956            c.drawTextRun(mChars, start + mStart, count, contextStart + mStart,
2957                    contextCount, x, y, flags, p);
2958        }
2959
2960        public float measureText(int start, int end, Paint p) {
2961            return p.measureText(mChars, start + mStart, end - start);
2962        }
2963
2964        public int getTextWidths(int start, int end, float[] widths, Paint p) {
2965            return p.getTextWidths(mChars, start + mStart, end - start, widths);
2966        }
2967
2968        public float getTextRunAdvances(int start, int end, int contextStart,
2969                int contextEnd, int flags, float[] advances, int advancesIndex,
2970                Paint p) {
2971            int count = end - start;
2972            int contextCount = contextEnd - contextStart;
2973            return p.getTextRunAdvances(mChars, start + mStart, count,
2974                    contextStart + mStart, contextCount, flags, advances,
2975                    advancesIndex);
2976        }
2977
2978        public float getTextRunAdvances(int start, int end, int contextStart,
2979                int contextEnd, int flags, float[] advances, int advancesIndex,
2980                Paint p, int reserved) {
2981            int count = end - start;
2982            int contextCount = contextEnd - contextStart;
2983            return p.getTextRunAdvances(mChars, start + mStart, count,
2984                    contextStart + mStart, contextCount, flags, advances,
2985                    advancesIndex, reserved);
2986        }
2987
2988        public int getTextRunCursor(int contextStart, int contextEnd, int flags,
2989                int offset, int cursorOpt, Paint p) {
2990            int contextCount = contextEnd - contextStart;
2991            return p.getTextRunCursor(mChars, contextStart + mStart,
2992                    contextCount, flags, offset + mStart, cursorOpt);
2993        }
2994    }
2995
2996    /**
2997     * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)},
2998     * except that the cursor position (if any) is retained in the new text.
2999     *
3000     * @see #setText(CharSequence, android.widget.TextView.BufferType)
3001     */
3002    public final void setTextKeepState(CharSequence text, BufferType type) {
3003        int start = getSelectionStart();
3004        int end = getSelectionEnd();
3005        int len = text.length();
3006
3007        setText(text, type);
3008
3009        if (start >= 0 || end >= 0) {
3010            if (mText instanceof Spannable) {
3011                Selection.setSelection((Spannable) mText,
3012                                       Math.max(0, Math.min(start, len)),
3013                                       Math.max(0, Math.min(end, len)));
3014            }
3015        }
3016    }
3017
3018    @android.view.RemotableViewMethod
3019    public final void setText(int resid) {
3020        setText(getContext().getResources().getText(resid));
3021    }
3022
3023    public final void setText(int resid, BufferType type) {
3024        setText(getContext().getResources().getText(resid), type);
3025    }
3026
3027    /**
3028     * Sets the text to be displayed when the text of the TextView is empty.
3029     * Null means to use the normal empty text. The hint does not currently
3030     * participate in determining the size of the view.
3031     *
3032     * @attr ref android.R.styleable#TextView_hint
3033     */
3034    @android.view.RemotableViewMethod
3035    public final void setHint(CharSequence hint) {
3036        mHint = TextUtils.stringOrSpannedString(hint);
3037
3038        if (mLayout != null) {
3039            checkForRelayout();
3040        }
3041
3042        if (mText.length() == 0) {
3043            invalidate();
3044        }
3045    }
3046
3047    /**
3048     * Sets the text to be displayed when the text of the TextView is empty,
3049     * from a resource.
3050     *
3051     * @attr ref android.R.styleable#TextView_hint
3052     */
3053    @android.view.RemotableViewMethod
3054    public final void setHint(int resid) {
3055        setHint(getContext().getResources().getText(resid));
3056    }
3057
3058    /**
3059     * Returns the hint that is displayed when the text of the TextView
3060     * is empty.
3061     *
3062     * @attr ref android.R.styleable#TextView_hint
3063     */
3064    @ViewDebug.CapturedViewProperty
3065    public CharSequence getHint() {
3066        return mHint;
3067    }
3068
3069    private boolean isMultilineInputType(int type) {
3070        return (type & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)) ==
3071            (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
3072    }
3073
3074    /**
3075     * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This
3076     * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)},
3077     * to match the given content type.  If the given content type is {@link EditorInfo#TYPE_NULL}
3078     * then a soft keyboard will not be displayed for this text view.
3079     *
3080     * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be
3081     * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input
3082     * type.
3083     *
3084     * @see #getInputType()
3085     * @see #setRawInputType(int)
3086     * @see android.text.InputType
3087     * @attr ref android.R.styleable#TextView_inputType
3088     */
3089    public void setInputType(int type) {
3090        final boolean wasPassword = isPasswordInputType(mInputType);
3091        final boolean wasVisiblePassword = isVisiblePasswordInputType(mInputType);
3092        setInputType(type, false);
3093        final boolean isPassword = isPasswordInputType(type);
3094        final boolean isVisiblePassword = isVisiblePasswordInputType(type);
3095        boolean forceUpdate = false;
3096        if (isPassword) {
3097            setTransformationMethod(PasswordTransformationMethod.getInstance());
3098            setTypefaceByIndex(MONOSPACE, 0);
3099        } else if (isVisiblePassword) {
3100            if (mTransformation == PasswordTransformationMethod.getInstance()) {
3101                forceUpdate = true;
3102            }
3103            setTypefaceByIndex(MONOSPACE, 0);
3104        } else if (wasPassword || wasVisiblePassword) {
3105            // not in password mode, clean up typeface and transformation
3106            setTypefaceByIndex(-1, -1);
3107            if (mTransformation == PasswordTransformationMethod.getInstance()) {
3108                forceUpdate = true;
3109            }
3110        }
3111
3112        boolean singleLine = !isMultilineInputType(type);
3113
3114        // We need to update the single line mode if it has changed or we
3115        // were previously in password mode.
3116        if (mSingleLine != singleLine || forceUpdate) {
3117            // Change single line mode, but only change the transformation if
3118            // we are not in password mode.
3119            applySingleLine(singleLine, !isPassword, true);
3120        }
3121
3122        InputMethodManager imm = InputMethodManager.peekInstance();
3123        if (imm != null) imm.restartInput(this);
3124    }
3125
3126    /**
3127     * It would be better to rely on the input type for everything. A password inputType should have
3128     * a password transformation. We should hence use isPasswordInputType instead of this method.
3129     *
3130     * We should:
3131     * - Call setInputType in setKeyListener instead of changing the input type directly (which
3132     * would install the correct transformation).
3133     * - Refuse the installation of a non-password transformation in setTransformation if the input
3134     * type is password.
3135     *
3136     * However, this is like this for legacy reasons and we cannot break existing apps. This method
3137     * is useful since it matches what the user can see (obfuscated text or not).
3138     *
3139     * @return true if the current transformation method is of the password type.
3140     */
3141    private boolean hasPasswordTransformationMethod() {
3142        return mTransformation instanceof PasswordTransformationMethod;
3143    }
3144
3145    private boolean isPasswordInputType(int inputType) {
3146        final int variation =
3147                inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
3148        return variation
3149                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)
3150                || variation
3151                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD)
3152                || variation
3153                == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
3154    }
3155
3156    private boolean isVisiblePasswordInputType(int inputType) {
3157        final int variation =
3158                inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
3159        return variation
3160                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
3161    }
3162
3163    /**
3164     * Directly change the content type integer of the text view, without
3165     * modifying any other state.
3166     * @see #setInputType(int)
3167     * @see android.text.InputType
3168     * @attr ref android.R.styleable#TextView_inputType
3169     */
3170    public void setRawInputType(int type) {
3171        mInputType = type;
3172    }
3173
3174    private void setInputType(int type, boolean direct) {
3175        final int cls = type & EditorInfo.TYPE_MASK_CLASS;
3176        KeyListener input;
3177        if (cls == EditorInfo.TYPE_CLASS_TEXT) {
3178            boolean autotext = (type & EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0;
3179            TextKeyListener.Capitalize cap;
3180            if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) {
3181                cap = TextKeyListener.Capitalize.CHARACTERS;
3182            } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS) != 0) {
3183                cap = TextKeyListener.Capitalize.WORDS;
3184            } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) {
3185                cap = TextKeyListener.Capitalize.SENTENCES;
3186            } else {
3187                cap = TextKeyListener.Capitalize.NONE;
3188            }
3189            input = TextKeyListener.getInstance(autotext, cap);
3190        } else if (cls == EditorInfo.TYPE_CLASS_NUMBER) {
3191            input = DigitsKeyListener.getInstance(
3192                    (type & EditorInfo.TYPE_NUMBER_FLAG_SIGNED) != 0,
3193                    (type & EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) != 0);
3194        } else if (cls == EditorInfo.TYPE_CLASS_DATETIME) {
3195            switch (type & EditorInfo.TYPE_MASK_VARIATION) {
3196                case EditorInfo.TYPE_DATETIME_VARIATION_DATE:
3197                    input = DateKeyListener.getInstance();
3198                    break;
3199                case EditorInfo.TYPE_DATETIME_VARIATION_TIME:
3200                    input = TimeKeyListener.getInstance();
3201                    break;
3202                default:
3203                    input = DateTimeKeyListener.getInstance();
3204                    break;
3205            }
3206        } else if (cls == EditorInfo.TYPE_CLASS_PHONE) {
3207            input = DialerKeyListener.getInstance();
3208        } else {
3209            input = TextKeyListener.getInstance();
3210        }
3211        setRawInputType(type);
3212        if (direct) mInput = input;
3213        else {
3214            setKeyListenerOnly(input);
3215        }
3216    }
3217
3218    /**
3219     * Get the type of the content.
3220     *
3221     * @see #setInputType(int)
3222     * @see android.text.InputType
3223     */
3224    public int getInputType() {
3225        return mInputType;
3226    }
3227
3228    /**
3229     * Change the editor type integer associated with the text view, which
3230     * will be reported to an IME with {@link EditorInfo#imeOptions} when it
3231     * has focus.
3232     * @see #getImeOptions
3233     * @see android.view.inputmethod.EditorInfo
3234     * @attr ref android.R.styleable#TextView_imeOptions
3235     */
3236    public void setImeOptions(int imeOptions) {
3237        if (mInputContentType == null) {
3238            mInputContentType = new InputContentType();
3239        }
3240        mInputContentType.imeOptions = imeOptions;
3241    }
3242
3243    /**
3244     * Get the type of the IME editor.
3245     *
3246     * @see #setImeOptions(int)
3247     * @see android.view.inputmethod.EditorInfo
3248     */
3249    public int getImeOptions() {
3250        return mInputContentType != null
3251                ? mInputContentType.imeOptions : EditorInfo.IME_NULL;
3252    }
3253
3254    /**
3255     * Change the custom IME action associated with the text view, which
3256     * will be reported to an IME with {@link EditorInfo#actionLabel}
3257     * and {@link EditorInfo#actionId} when it has focus.
3258     * @see #getImeActionLabel
3259     * @see #getImeActionId
3260     * @see android.view.inputmethod.EditorInfo
3261     * @attr ref android.R.styleable#TextView_imeActionLabel
3262     * @attr ref android.R.styleable#TextView_imeActionId
3263     */
3264    public void setImeActionLabel(CharSequence label, int actionId) {
3265        if (mInputContentType == null) {
3266            mInputContentType = new InputContentType();
3267        }
3268        mInputContentType.imeActionLabel = label;
3269        mInputContentType.imeActionId = actionId;
3270    }
3271
3272    /**
3273     * Get the IME action label previous set with {@link #setImeActionLabel}.
3274     *
3275     * @see #setImeActionLabel
3276     * @see android.view.inputmethod.EditorInfo
3277     */
3278    public CharSequence getImeActionLabel() {
3279        return mInputContentType != null
3280                ? mInputContentType.imeActionLabel : null;
3281    }
3282
3283    /**
3284     * Get the IME action ID previous set with {@link #setImeActionLabel}.
3285     *
3286     * @see #setImeActionLabel
3287     * @see android.view.inputmethod.EditorInfo
3288     */
3289    public int getImeActionId() {
3290        return mInputContentType != null
3291                ? mInputContentType.imeActionId : 0;
3292    }
3293
3294    /**
3295     * Set a special listener to be called when an action is performed
3296     * on the text view.  This will be called when the enter key is pressed,
3297     * or when an action supplied to the IME is selected by the user.  Setting
3298     * this means that the normal hard key event will not insert a newline
3299     * into the text view, even if it is multi-line; holding down the ALT
3300     * modifier will, however, allow the user to insert a newline character.
3301     */
3302    public void setOnEditorActionListener(OnEditorActionListener l) {
3303        if (mInputContentType == null) {
3304            mInputContentType = new InputContentType();
3305        }
3306        mInputContentType.onEditorActionListener = l;
3307    }
3308
3309    /**
3310     * Called when an attached input method calls
3311     * {@link InputConnection#performEditorAction(int)
3312     * InputConnection.performEditorAction()}
3313     * for this text view.  The default implementation will call your action
3314     * listener supplied to {@link #setOnEditorActionListener}, or perform
3315     * a standard operation for {@link EditorInfo#IME_ACTION_NEXT
3316     * EditorInfo.IME_ACTION_NEXT}, {@link EditorInfo#IME_ACTION_PREVIOUS
3317     * EditorInfo.IME_ACTION_PREVIOUS}, or {@link EditorInfo#IME_ACTION_DONE
3318     * EditorInfo.IME_ACTION_DONE}.
3319     *
3320     * <p>For backwards compatibility, if no IME options have been set and the
3321     * text view would not normally advance focus on enter, then
3322     * the NEXT and DONE actions received here will be turned into an enter
3323     * key down/up pair to go through the normal key handling.
3324     *
3325     * @param actionCode The code of the action being performed.
3326     *
3327     * @see #setOnEditorActionListener
3328     */
3329    public void onEditorAction(int actionCode) {
3330        final InputContentType ict = mInputContentType;
3331        if (ict != null) {
3332            if (ict.onEditorActionListener != null) {
3333                if (ict.onEditorActionListener.onEditorAction(this,
3334                        actionCode, null)) {
3335                    return;
3336                }
3337            }
3338
3339            // This is the handling for some default action.
3340            // Note that for backwards compatibility we don't do this
3341            // default handling if explicit ime options have not been given,
3342            // instead turning this into the normal enter key codes that an
3343            // app may be expecting.
3344            if (actionCode == EditorInfo.IME_ACTION_NEXT) {
3345                View v = focusSearch(FOCUS_DOWN);
3346                if (v != null) {
3347                    if (!v.requestFocus(FOCUS_DOWN)) {
3348                        throw new IllegalStateException("focus search returned a view " +
3349                                "that wasn't able to take focus!");
3350                    }
3351                }
3352                return;
3353
3354            } else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) {
3355                View v = focusSearch(FOCUS_UP);
3356                if (v != null) {
3357                    if (!v.requestFocus(FOCUS_UP)) {
3358                        throw new IllegalStateException("focus search returned a view " +
3359                                "that wasn't able to take focus!");
3360                    }
3361                }
3362                return;
3363
3364            } else if (actionCode == EditorInfo.IME_ACTION_DONE) {
3365                InputMethodManager imm = InputMethodManager.peekInstance();
3366                if (imm != null && imm.isActive(this)) {
3367                    imm.hideSoftInputFromWindow(getWindowToken(), 0);
3368                }
3369                return;
3370            }
3371        }
3372
3373        Handler h = getHandler();
3374        if (h != null) {
3375            long eventTime = SystemClock.uptimeMillis();
3376            h.sendMessage(h.obtainMessage(ViewAncestor.DISPATCH_KEY_FROM_IME,
3377                    new KeyEvent(eventTime, eventTime,
3378                    KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0,
3379                    KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
3380                    KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
3381                    | KeyEvent.FLAG_EDITOR_ACTION)));
3382            h.sendMessage(h.obtainMessage(ViewAncestor.DISPATCH_KEY_FROM_IME,
3383                    new KeyEvent(SystemClock.uptimeMillis(), eventTime,
3384                    KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0,
3385                    KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
3386                    KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
3387                    | KeyEvent.FLAG_EDITOR_ACTION)));
3388        }
3389    }
3390
3391    /**
3392     * Set the private content type of the text, which is the
3393     * {@link EditorInfo#privateImeOptions EditorInfo.privateImeOptions}
3394     * field that will be filled in when creating an input connection.
3395     *
3396     * @see #getPrivateImeOptions()
3397     * @see EditorInfo#privateImeOptions
3398     * @attr ref android.R.styleable#TextView_privateImeOptions
3399     */
3400    public void setPrivateImeOptions(String type) {
3401        if (mInputContentType == null) mInputContentType = new InputContentType();
3402        mInputContentType.privateImeOptions = type;
3403    }
3404
3405    /**
3406     * Get the private type of the content.
3407     *
3408     * @see #setPrivateImeOptions(String)
3409     * @see EditorInfo#privateImeOptions
3410     */
3411    public String getPrivateImeOptions() {
3412        return mInputContentType != null
3413                ? mInputContentType.privateImeOptions : null;
3414    }
3415
3416    /**
3417     * Set the extra input data of the text, which is the
3418     * {@link EditorInfo#extras TextBoxAttribute.extras}
3419     * Bundle that will be filled in when creating an input connection.  The
3420     * given integer is the resource ID of an XML resource holding an
3421     * {@link android.R.styleable#InputExtras &lt;input-extras&gt;} XML tree.
3422     *
3423     * @see #getInputExtras(boolean)
3424     * @see EditorInfo#extras
3425     * @attr ref android.R.styleable#TextView_editorExtras
3426     */
3427    public void setInputExtras(int xmlResId)
3428            throws XmlPullParserException, IOException {
3429        XmlResourceParser parser = getResources().getXml(xmlResId);
3430        if (mInputContentType == null) mInputContentType = new InputContentType();
3431        mInputContentType.extras = new Bundle();
3432        getResources().parseBundleExtras(parser, mInputContentType.extras);
3433    }
3434
3435    /**
3436     * Retrieve the input extras currently associated with the text view, which
3437     * can be viewed as well as modified.
3438     *
3439     * @param create If true, the extras will be created if they don't already
3440     * exist.  Otherwise, null will be returned if none have been created.
3441     * @see #setInputExtras(int)
3442     * @see EditorInfo#extras
3443     * @attr ref android.R.styleable#TextView_editorExtras
3444     */
3445    public Bundle getInputExtras(boolean create) {
3446        if (mInputContentType == null) {
3447            if (!create) return null;
3448            mInputContentType = new InputContentType();
3449        }
3450        if (mInputContentType.extras == null) {
3451            if (!create) return null;
3452            mInputContentType.extras = new Bundle();
3453        }
3454        return mInputContentType.extras;
3455    }
3456
3457    /**
3458     * Returns the error message that was set to be displayed with
3459     * {@link #setError}, or <code>null</code> if no error was set
3460     * or if it the error was cleared by the widget after user input.
3461     */
3462    public CharSequence getError() {
3463        return mError;
3464    }
3465
3466    /**
3467     * Sets the right-hand compound drawable of the TextView to the "error"
3468     * icon and sets an error message that will be displayed in a popup when
3469     * the TextView has focus.  The icon and error message will be reset to
3470     * null when any key events cause changes to the TextView's text.  If the
3471     * <code>error</code> is <code>null</code>, the error message and icon
3472     * will be cleared.
3473     */
3474    @android.view.RemotableViewMethod
3475    public void setError(CharSequence error) {
3476        if (error == null) {
3477            setError(null, null);
3478        } else {
3479            Drawable dr = getContext().getResources().
3480                getDrawable(com.android.internal.R.drawable.indicator_input_error);
3481
3482            dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
3483            setError(error, dr);
3484        }
3485    }
3486
3487    /**
3488     * Sets the right-hand compound drawable of the TextView to the specified
3489     * icon and sets an error message that will be displayed in a popup when
3490     * the TextView has focus.  The icon and error message will be reset to
3491     * null when any key events cause changes to the TextView's text.  The
3492     * drawable must already have had {@link Drawable#setBounds} set on it.
3493     * If the <code>error</code> is <code>null</code>, the error message will
3494     * be cleared (and you should provide a <code>null</code> icon as well).
3495     */
3496    public void setError(CharSequence error, Drawable icon) {
3497        error = TextUtils.stringOrSpannedString(error);
3498
3499        mError = error;
3500        mErrorWasChanged = true;
3501        final Drawables dr = mDrawables;
3502        if (dr != null) {
3503            setCompoundDrawables(dr.mDrawableLeft, dr.mDrawableTop, icon, dr.mDrawableBottom);
3504        } else {
3505            setCompoundDrawables(null, null, icon, null);
3506        }
3507
3508        if (error == null) {
3509            if (mPopup != null) {
3510                if (mPopup.isShowing()) {
3511                    mPopup.dismiss();
3512                }
3513
3514                mPopup = null;
3515            }
3516        } else {
3517            if (isFocused()) {
3518                showError();
3519            }
3520        }
3521    }
3522
3523    private void showError() {
3524        if (getWindowToken() == null) {
3525            mShowErrorAfterAttach = true;
3526            return;
3527        }
3528
3529        if (mPopup == null) {
3530            LayoutInflater inflater = LayoutInflater.from(getContext());
3531            final TextView err = (TextView) inflater.inflate(
3532                    com.android.internal.R.layout.textview_hint, null);
3533
3534            final float scale = getResources().getDisplayMetrics().density;
3535            mPopup = new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f));
3536            mPopup.setFocusable(false);
3537            // The user is entering text, so the input method is needed.  We
3538            // don't want the popup to be displayed on top of it.
3539            mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
3540        }
3541
3542        TextView tv = (TextView) mPopup.getContentView();
3543        chooseSize(mPopup, mError, tv);
3544        tv.setText(mError);
3545
3546        mPopup.showAsDropDown(this, getErrorX(), getErrorY());
3547        mPopup.fixDirection(mPopup.isAboveAnchor());
3548    }
3549
3550    private static class ErrorPopup extends PopupWindow {
3551        private boolean mAbove = false;
3552        private final TextView mView;
3553        private int mPopupInlineErrorBackgroundId = 0;
3554        private int mPopupInlineErrorAboveBackgroundId = 0;
3555
3556        ErrorPopup(TextView v, int width, int height) {
3557            super(v, width, height);
3558            mView = v;
3559            // Make sure the TextView has a background set as it will be used the first time it is
3560            // shown and positionned. Initialized with below background, which should have
3561            // dimensions identical to the above version for this to work (and is more likely).
3562            mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
3563                    com.android.internal.R.styleable.Theme_errorMessageBackground);
3564            mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
3565        }
3566
3567        void fixDirection(boolean above) {
3568            mAbove = above;
3569
3570            if (above) {
3571                mPopupInlineErrorAboveBackgroundId =
3572                    getResourceId(mPopupInlineErrorAboveBackgroundId,
3573                            com.android.internal.R.styleable.Theme_errorMessageAboveBackground);
3574            } else {
3575                mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
3576                        com.android.internal.R.styleable.Theme_errorMessageBackground);
3577            }
3578
3579            mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId :
3580                mPopupInlineErrorBackgroundId);
3581        }
3582
3583        private int getResourceId(int currentId, int index) {
3584            if (currentId == 0) {
3585                TypedArray styledAttributes = mView.getContext().obtainStyledAttributes(
3586                        R.styleable.Theme);
3587                currentId = styledAttributes.getResourceId(index, 0);
3588                styledAttributes.recycle();
3589            }
3590            return currentId;
3591        }
3592
3593        @Override
3594        public void update(int x, int y, int w, int h, boolean force) {
3595            super.update(x, y, w, h, force);
3596
3597            boolean above = isAboveAnchor();
3598            if (above != mAbove) {
3599                fixDirection(above);
3600            }
3601        }
3602    }
3603
3604    /**
3605     * Returns the Y offset to make the pointy top of the error point
3606     * at the middle of the error icon.
3607     */
3608    private int getErrorX() {
3609        /*
3610         * The "25" is the distance between the point and the right edge
3611         * of the background
3612         */
3613        final float scale = getResources().getDisplayMetrics().density;
3614
3615        final Drawables dr = mDrawables;
3616        return getWidth() - mPopup.getWidth() - getPaddingRight() -
3617                (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f);
3618    }
3619
3620    /**
3621     * Returns the Y offset to make the pointy top of the error point
3622     * at the bottom of the error icon.
3623     */
3624    private int getErrorY() {
3625        /*
3626         * Compound, not extended, because the icon is not clipped
3627         * if the text height is smaller.
3628         */
3629        final int compoundPaddingTop = getCompoundPaddingTop();
3630        int vspace = mBottom - mTop - getCompoundPaddingBottom() - compoundPaddingTop;
3631
3632        final Drawables dr = mDrawables;
3633        int icontop = compoundPaddingTop +
3634                (vspace - (dr != null ? dr.mDrawableHeightRight : 0)) / 2;
3635
3636        /*
3637         * The "2" is the distance between the point and the top edge
3638         * of the background.
3639         */
3640        final float scale = getResources().getDisplayMetrics().density;
3641        return icontop + (dr != null ? dr.mDrawableHeightRight : 0) - getHeight() -
3642                (int) (2 * scale + 0.5f);
3643    }
3644
3645    private void hideError() {
3646        if (mPopup != null) {
3647            if (mPopup.isShowing()) {
3648                mPopup.dismiss();
3649            }
3650        }
3651
3652        mShowErrorAfterAttach = false;
3653    }
3654
3655    private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
3656        int wid = tv.getPaddingLeft() + tv.getPaddingRight();
3657        int ht = tv.getPaddingTop() + tv.getPaddingBottom();
3658
3659        /*
3660         * Figure out how big the text would be if we laid it out to the
3661         * full width of this view minus the border.
3662         */
3663        int cap = getWidth() - wid;
3664        if (cap < 0) {
3665            cap = 200; // We must not be measured yet -- setFrame() will fix it.
3666        }
3667
3668        Layout l = new StaticLayout(text, tv.getPaint(), cap,
3669                                    Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
3670        float max = 0;
3671        for (int i = 0; i < l.getLineCount(); i++) {
3672            max = Math.max(max, l.getLineWidth(i));
3673        }
3674
3675        /*
3676         * Now set the popup size to be big enough for the text plus the border.
3677         */
3678        pop.setWidth(wid + (int) Math.ceil(max));
3679        pop.setHeight(ht + l.getHeight());
3680    }
3681
3682
3683    @Override
3684    protected boolean setFrame(int l, int t, int r, int b) {
3685        boolean result = super.setFrame(l, t, r, b);
3686
3687        if (mPopup != null) {
3688            TextView tv = (TextView) mPopup.getContentView();
3689            chooseSize(mPopup, mError, tv);
3690            mPopup.update(this, getErrorX(), getErrorY(),
3691                          mPopup.getWidth(), mPopup.getHeight());
3692        }
3693
3694        restartMarqueeIfNeeded();
3695
3696        return result;
3697    }
3698
3699    private void restartMarqueeIfNeeded() {
3700        if (mRestartMarquee && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
3701            mRestartMarquee = false;
3702            startMarquee();
3703        }
3704    }
3705
3706    /**
3707     * Sets the list of input filters that will be used if the buffer is
3708     * Editable.  Has no effect otherwise.
3709     *
3710     * @attr ref android.R.styleable#TextView_maxLength
3711     */
3712    public void setFilters(InputFilter[] filters) {
3713        if (filters == null) {
3714            throw new IllegalArgumentException();
3715        }
3716
3717        mFilters = filters;
3718
3719        if (mText instanceof Editable) {
3720            setFilters((Editable) mText, filters);
3721        }
3722    }
3723
3724    /**
3725     * Sets the list of input filters on the specified Editable,
3726     * and includes mInput in the list if it is an InputFilter.
3727     */
3728    private void setFilters(Editable e, InputFilter[] filters) {
3729        if (mInput instanceof InputFilter) {
3730            InputFilter[] nf = new InputFilter[filters.length + 1];
3731
3732            System.arraycopy(filters, 0, nf, 0, filters.length);
3733            nf[filters.length] = (InputFilter) mInput;
3734
3735            e.setFilters(nf);
3736        } else {
3737            e.setFilters(filters);
3738        }
3739    }
3740
3741    /**
3742     * Returns the current list of input filters.
3743     */
3744    public InputFilter[] getFilters() {
3745        return mFilters;
3746    }
3747
3748    /////////////////////////////////////////////////////////////////////////
3749
3750    private int getVerticalOffset(boolean forceNormal) {
3751        int voffset = 0;
3752        final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
3753
3754        Layout l = mLayout;
3755        if (!forceNormal && mText.length() == 0 && mHintLayout != null) {
3756            l = mHintLayout;
3757        }
3758
3759        if (gravity != Gravity.TOP) {
3760            int boxht;
3761
3762            if (l == mHintLayout) {
3763                boxht = getMeasuredHeight() - getCompoundPaddingTop() -
3764                        getCompoundPaddingBottom();
3765            } else {
3766                boxht = getMeasuredHeight() - getExtendedPaddingTop() -
3767                        getExtendedPaddingBottom();
3768            }
3769            int textht = l.getHeight();
3770
3771            if (textht < boxht) {
3772                if (gravity == Gravity.BOTTOM)
3773                    voffset = boxht - textht;
3774                else // (gravity == Gravity.CENTER_VERTICAL)
3775                    voffset = (boxht - textht) >> 1;
3776            }
3777        }
3778        return voffset;
3779    }
3780
3781    private int getBottomVerticalOffset(boolean forceNormal) {
3782        int voffset = 0;
3783        final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
3784
3785        Layout l = mLayout;
3786        if (!forceNormal && mText.length() == 0 && mHintLayout != null) {
3787            l = mHintLayout;
3788        }
3789
3790        if (gravity != Gravity.BOTTOM) {
3791            int boxht;
3792
3793            if (l == mHintLayout) {
3794                boxht = getMeasuredHeight() - getCompoundPaddingTop() -
3795                        getCompoundPaddingBottom();
3796            } else {
3797                boxht = getMeasuredHeight() - getExtendedPaddingTop() -
3798                        getExtendedPaddingBottom();
3799            }
3800            int textht = l.getHeight();
3801
3802            if (textht < boxht) {
3803                if (gravity == Gravity.TOP)
3804                    voffset = boxht - textht;
3805                else // (gravity == Gravity.CENTER_VERTICAL)
3806                    voffset = (boxht - textht) >> 1;
3807            }
3808        }
3809        return voffset;
3810    }
3811
3812    private void invalidateCursorPath() {
3813        if (mHighlightPathBogus) {
3814            invalidateCursor();
3815        } else {
3816            final int horizontalPadding = getCompoundPaddingLeft();
3817            final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true);
3818
3819            if (mCursorCount == 0) {
3820                synchronized (sTempRect) {
3821                    /*
3822                     * The reason for this concern about the thickness of the
3823                     * cursor and doing the floor/ceil on the coordinates is that
3824                     * some EditTexts (notably textfields in the Browser) have
3825                     * anti-aliased text where not all the characters are
3826                     * necessarily at integer-multiple locations.  This should
3827                     * make sure the entire cursor gets invalidated instead of
3828                     * sometimes missing half a pixel.
3829                     */
3830                    float thick = FloatMath.ceil(mTextPaint.getStrokeWidth());
3831                    if (thick < 1.0f) {
3832                        thick = 1.0f;
3833                    }
3834
3835                    thick /= 2.0f;
3836
3837                    mHighlightPath.computeBounds(sTempRect, false);
3838
3839                    invalidate((int) FloatMath.floor(horizontalPadding + sTempRect.left - thick),
3840                            (int) FloatMath.floor(verticalPadding + sTempRect.top - thick),
3841                            (int) FloatMath.ceil(horizontalPadding + sTempRect.right + thick),
3842                            (int) FloatMath.ceil(verticalPadding + sTempRect.bottom + thick));
3843                }
3844            } else {
3845                for (int i = 0; i < mCursorCount; i++) {
3846                    Rect bounds = mCursorDrawable[i].getBounds();
3847                    invalidate(bounds.left + horizontalPadding, bounds.top + verticalPadding,
3848                            bounds.right + horizontalPadding, bounds.bottom + verticalPadding);
3849                }
3850            }
3851        }
3852    }
3853
3854    private void invalidateCursor() {
3855        int where = getSelectionEnd();
3856
3857        invalidateCursor(where, where, where);
3858    }
3859
3860    private void invalidateCursor(int a, int b, int c) {
3861        if (mLayout == null) {
3862            invalidate();
3863        } else {
3864            if (a >= 0 || b >= 0 || c >= 0) {
3865                int first = Math.min(Math.min(a, b), c);
3866                int last = Math.max(Math.max(a, b), c);
3867
3868                int line = mLayout.getLineForOffset(first);
3869                int top = mLayout.getLineTop(line);
3870
3871                // This is ridiculous, but the descent from the line above
3872                // can hang down into the line we really want to redraw,
3873                // so we have to invalidate part of the line above to make
3874                // sure everything that needs to be redrawn really is.
3875                // (But not the whole line above, because that would cause
3876                // the same problem with the descenders on the line above it!)
3877                if (line > 0) {
3878                    top -= mLayout.getLineDescent(line - 1);
3879                }
3880
3881                int line2;
3882
3883                if (first == last)
3884                    line2 = line;
3885                else
3886                    line2 = mLayout.getLineForOffset(last);
3887
3888                int bottom = mLayout.getLineTop(line2 + 1);
3889
3890                final int horizontalPadding = getCompoundPaddingLeft();
3891                final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true);
3892
3893                // If used, the cursor drawables can have an arbitrary dimension that can go beyond
3894                // the invalidated lines specified above.
3895                for (int i = 0; i < mCursorCount; i++) {
3896                    Rect bounds = mCursorDrawable[i].getBounds();
3897                    top = Math.min(top, bounds.top);
3898                    bottom = Math.max(bottom, bounds.bottom);
3899                    // Horizontal bounds are already full width, no need to update
3900                }
3901
3902                invalidate(horizontalPadding + mScrollX, top + verticalPadding,
3903                        horizontalPadding + mScrollX + getWidth() -
3904                        getCompoundPaddingLeft() - getCompoundPaddingRight(),
3905                        bottom + verticalPadding);
3906            }
3907        }
3908    }
3909
3910    private void registerForPreDraw() {
3911        final ViewTreeObserver observer = getViewTreeObserver();
3912
3913        if (mPreDrawState == PREDRAW_NOT_REGISTERED) {
3914            observer.addOnPreDrawListener(this);
3915            mPreDrawState = PREDRAW_PENDING;
3916        } else if (mPreDrawState == PREDRAW_DONE) {
3917            mPreDrawState = PREDRAW_PENDING;
3918        }
3919
3920        // else state is PREDRAW_PENDING, so keep waiting.
3921    }
3922
3923    /**
3924     * {@inheritDoc}
3925     */
3926    public boolean onPreDraw() {
3927        if (mPreDrawState != PREDRAW_PENDING) {
3928            return true;
3929        }
3930
3931        if (mLayout == null) {
3932            assumeLayout();
3933        }
3934
3935        boolean changed = false;
3936
3937        if (mMovement != null) {
3938            /* This code also provides auto-scrolling when a cursor is moved using a
3939             * CursorController (insertion point or selection limits).
3940             * For selection, ensure start or end is visible depending on controller's state.
3941             */
3942            int curs = getSelectionEnd();
3943            // Do not create the controller if it is not already created.
3944            if (mSelectionModifierCursorController != null &&
3945                    mSelectionModifierCursorController.isSelectionStartDragged()) {
3946                curs = getSelectionStart();
3947            }
3948
3949            /*
3950             * TODO: This should really only keep the end in view if
3951             * it already was before the text changed.  I'm not sure
3952             * of a good way to tell from here if it was.
3953             */
3954            if (curs < 0 &&
3955                  (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
3956                curs = mText.length();
3957            }
3958
3959            if (curs >= 0) {
3960                changed = bringPointIntoView(curs);
3961            }
3962        } else {
3963            changed = bringTextIntoView();
3964        }
3965
3966        // This has to be checked here since:
3967        // - onFocusChanged cannot start it when focus is given to a view with selected text (after
3968        //   a screen rotation) since layout is not yet initialized at that point.
3969        if (mCreatedWithASelection) {
3970            startSelectionActionMode();
3971            mCreatedWithASelection = false;
3972        }
3973
3974        // Phone specific code (there is no ExtractEditText on tablets).
3975        // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can
3976        // not be set. Do the test here instead.
3977        if (this instanceof ExtractEditText && hasSelection()) {
3978            startSelectionActionMode();
3979        }
3980
3981        mPreDrawState = PREDRAW_DONE;
3982        return !changed;
3983    }
3984
3985    @Override
3986    protected void onAttachedToWindow() {
3987        super.onAttachedToWindow();
3988
3989        mTemporaryDetach = false;
3990
3991        if (mShowErrorAfterAttach) {
3992            showError();
3993            mShowErrorAfterAttach = false;
3994        }
3995
3996        final ViewTreeObserver observer = getViewTreeObserver();
3997        // No need to create the controller.
3998        // The get method will add the listener on controller creation.
3999        if (mInsertionPointCursorController != null) {
4000            observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
4001        }
4002        if (mSelectionModifierCursorController != null) {
4003            observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
4004        }
4005    }
4006
4007    @Override
4008    protected void onDetachedFromWindow() {
4009        super.onDetachedFromWindow();
4010
4011        final ViewTreeObserver observer = getViewTreeObserver();
4012        if (mPreDrawState != PREDRAW_NOT_REGISTERED) {
4013            observer.removeOnPreDrawListener(this);
4014            mPreDrawState = PREDRAW_NOT_REGISTERED;
4015        }
4016
4017        if (mError != null) {
4018            hideError();
4019        }
4020
4021        if (mBlink != null) {
4022            mBlink.removeCallbacks(mBlink);
4023        }
4024
4025        if (mInsertionPointCursorController != null) {
4026            mInsertionPointCursorController.onDetached();
4027        }
4028
4029        if (mSelectionModifierCursorController != null) {
4030            mSelectionModifierCursorController.onDetached();
4031        }
4032
4033        hideControllers();
4034    }
4035
4036    @Override
4037    protected boolean isPaddingOffsetRequired() {
4038        return mShadowRadius != 0 || mDrawables != null;
4039    }
4040
4041    @Override
4042    protected int getLeftPaddingOffset() {
4043        return getCompoundPaddingLeft() - mPaddingLeft +
4044                (int) Math.min(0, mShadowDx - mShadowRadius);
4045    }
4046
4047    @Override
4048    protected int getTopPaddingOffset() {
4049        return (int) Math.min(0, mShadowDy - mShadowRadius);
4050    }
4051
4052    @Override
4053    protected int getBottomPaddingOffset() {
4054        return (int) Math.max(0, mShadowDy + mShadowRadius);
4055    }
4056
4057    @Override
4058    protected int getRightPaddingOffset() {
4059        return -(getCompoundPaddingRight() - mPaddingRight) +
4060                (int) Math.max(0, mShadowDx + mShadowRadius);
4061    }
4062
4063    @Override
4064    protected boolean verifyDrawable(Drawable who) {
4065        final boolean verified = super.verifyDrawable(who);
4066        if (!verified && mDrawables != null) {
4067            return who == mDrawables.mDrawableLeft || who == mDrawables.mDrawableTop ||
4068                    who == mDrawables.mDrawableRight || who == mDrawables.mDrawableBottom;
4069        }
4070        return verified;
4071    }
4072
4073    @Override
4074    public void jumpDrawablesToCurrentState() {
4075        super.jumpDrawablesToCurrentState();
4076        if (mDrawables != null) {
4077            if (mDrawables.mDrawableLeft != null) {
4078                mDrawables.mDrawableLeft.jumpToCurrentState();
4079            }
4080            if (mDrawables.mDrawableTop != null) {
4081                mDrawables.mDrawableTop.jumpToCurrentState();
4082            }
4083            if (mDrawables.mDrawableRight != null) {
4084                mDrawables.mDrawableRight.jumpToCurrentState();
4085            }
4086            if (mDrawables.mDrawableBottom != null) {
4087                mDrawables.mDrawableBottom.jumpToCurrentState();
4088            }
4089        }
4090    }
4091
4092    @Override
4093    public void invalidateDrawable(Drawable drawable) {
4094        if (verifyDrawable(drawable)) {
4095            final Rect dirty = drawable.getBounds();
4096            int scrollX = mScrollX;
4097            int scrollY = mScrollY;
4098
4099            // IMPORTANT: The coordinates below are based on the coordinates computed
4100            // for each compound drawable in onDraw(). Make sure to update each section
4101            // accordingly.
4102            final TextView.Drawables drawables = mDrawables;
4103            if (drawables != null) {
4104                if (drawable == drawables.mDrawableLeft) {
4105                    final int compoundPaddingTop = getCompoundPaddingTop();
4106                    final int compoundPaddingBottom = getCompoundPaddingBottom();
4107                    final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
4108
4109                    scrollX += mPaddingLeft;
4110                    scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2;
4111                } else if (drawable == drawables.mDrawableRight) {
4112                    final int compoundPaddingTop = getCompoundPaddingTop();
4113                    final int compoundPaddingBottom = getCompoundPaddingBottom();
4114                    final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
4115
4116                    scrollX += (mRight - mLeft - mPaddingRight - drawables.mDrawableSizeRight);
4117                    scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2;
4118                } else if (drawable == drawables.mDrawableTop) {
4119                    final int compoundPaddingLeft = getCompoundPaddingLeft();
4120                    final int compoundPaddingRight = getCompoundPaddingRight();
4121                    final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
4122
4123                    scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2;
4124                    scrollY += mPaddingTop;
4125                } else if (drawable == drawables.mDrawableBottom) {
4126                    final int compoundPaddingLeft = getCompoundPaddingLeft();
4127                    final int compoundPaddingRight = getCompoundPaddingRight();
4128                    final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
4129
4130                    scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2;
4131                    scrollY += (mBottom - mTop - mPaddingBottom - drawables.mDrawableSizeBottom);
4132                }
4133            }
4134
4135            invalidate(dirty.left + scrollX, dirty.top + scrollY,
4136                    dirty.right + scrollX, dirty.bottom + scrollY);
4137        }
4138    }
4139
4140    @Override
4141    protected boolean onSetAlpha(int alpha) {
4142        // Alpha is supported if and only if the drawing can be done in one pass.
4143        // TODO text with spans with a background color currently do not respect this alpha.
4144        if (getBackground() == null) {
4145            mCurrentAlpha = alpha;
4146            final Drawables dr = mDrawables;
4147            if (dr != null) {
4148                if (dr.mDrawableLeft != null) dr.mDrawableLeft.mutate().setAlpha(alpha);
4149                if (dr.mDrawableTop != null) dr.mDrawableTop.mutate().setAlpha(alpha);
4150                if (dr.mDrawableRight != null) dr.mDrawableRight.mutate().setAlpha(alpha);
4151                if (dr.mDrawableBottom != null) dr.mDrawableBottom.mutate().setAlpha(alpha);
4152            }
4153            return true;
4154        }
4155
4156        mCurrentAlpha = 255;
4157        return false;
4158    }
4159
4160    /**
4161     * When a TextView is used to display a useful piece of information to the user (such as a
4162     * contact's address), it should be made selectable, so that the user can select and copy this
4163     * content.
4164     *
4165     * Use {@link #setTextIsSelectable(boolean)} or the
4166     * {@link android.R.styleable#TextView_textIsSelectable} XML attribute to make this TextView
4167     * selectable (text is not selectable by default).
4168     *
4169     * Note that the content of an EditText is always selectable.
4170     *
4171     * @return True if the text displayed in this TextView can be selected by the user.
4172     *
4173     * @attr ref android.R.styleable#TextView_textIsSelectable
4174     */
4175    public boolean isTextSelectable() {
4176        return mTextIsSelectable;
4177    }
4178
4179    /**
4180     * Sets whether or not (default) the content of this view is selectable by the user.
4181     *
4182     * Note that this methods affect the {@link #setFocusable(boolean)},
4183     * {@link #setFocusableInTouchMode(boolean)} {@link #setClickable(boolean)} and
4184     * {@link #setLongClickable(boolean)} states and you may want to restore these if they were
4185     * customized.
4186     *
4187     * See {@link #isTextSelectable} for details.
4188     *
4189     * @param selectable Whether or not the content of this TextView should be selectable.
4190     */
4191    public void setTextIsSelectable(boolean selectable) {
4192        if (mTextIsSelectable == selectable) return;
4193
4194        mTextIsSelectable = selectable;
4195
4196        setFocusableInTouchMode(selectable);
4197        setFocusable(selectable);
4198        setClickable(selectable);
4199        setLongClickable(selectable);
4200
4201        // mInputType is already EditorInfo.TYPE_NULL and mInput is null;
4202
4203        setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
4204        setText(getText(), selectable ? BufferType.SPANNABLE : BufferType.NORMAL);
4205
4206        // Called by setText above, but safer in case of future code changes
4207        prepareCursorControllers();
4208    }
4209
4210    @Override
4211    protected int[] onCreateDrawableState(int extraSpace) {
4212        final int[] drawableState;
4213
4214        if (mSingleLine) {
4215            drawableState = super.onCreateDrawableState(extraSpace);
4216        } else {
4217            drawableState = super.onCreateDrawableState(extraSpace + 1);
4218            mergeDrawableStates(drawableState, MULTILINE_STATE_SET);
4219        }
4220
4221        if (mTextIsSelectable) {
4222            // Disable pressed state, which was introduced when TextView was made clickable.
4223            // Prevents text color change.
4224            // setClickable(false) would have a similar effect, but it also disables focus changes
4225            // and long press actions, which are both needed by text selection.
4226            final int length = drawableState.length;
4227            for (int i = 0; i < length; i++) {
4228                if (drawableState[i] == R.attr.state_pressed) {
4229                    final int[] nonPressedState = new int[length - 1];
4230                    System.arraycopy(drawableState, 0, nonPressedState, 0, i);
4231                    System.arraycopy(drawableState, i + 1, nonPressedState, i, length - i - 1);
4232                    return nonPressedState;
4233                }
4234            }
4235        }
4236
4237        return drawableState;
4238    }
4239
4240    @Override
4241    protected void onDraw(Canvas canvas) {
4242        if (mPreDrawState == PREDRAW_DONE) {
4243            final ViewTreeObserver observer = getViewTreeObserver();
4244            observer.removeOnPreDrawListener(this);
4245            mPreDrawState = PREDRAW_NOT_REGISTERED;
4246        }
4247
4248        if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return;
4249
4250        restartMarqueeIfNeeded();
4251
4252        // Draw the background for this view
4253        super.onDraw(canvas);
4254
4255        final int compoundPaddingLeft = getCompoundPaddingLeft();
4256        final int compoundPaddingTop = getCompoundPaddingTop();
4257        final int compoundPaddingRight = getCompoundPaddingRight();
4258        final int compoundPaddingBottom = getCompoundPaddingBottom();
4259        final int scrollX = mScrollX;
4260        final int scrollY = mScrollY;
4261        final int right = mRight;
4262        final int left = mLeft;
4263        final int bottom = mBottom;
4264        final int top = mTop;
4265
4266        final Drawables dr = mDrawables;
4267        if (dr != null) {
4268            /*
4269             * Compound, not extended, because the icon is not clipped
4270             * if the text height is smaller.
4271             */
4272
4273            int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
4274            int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;
4275
4276            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
4277            // Make sure to update invalidateDrawable() when changing this code.
4278            if (dr.mDrawableLeft != null) {
4279                canvas.save();
4280                canvas.translate(scrollX + mPaddingLeft,
4281                                 scrollY + compoundPaddingTop +
4282                                 (vspace - dr.mDrawableHeightLeft) / 2);
4283                dr.mDrawableLeft.draw(canvas);
4284                canvas.restore();
4285            }
4286
4287            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
4288            // Make sure to update invalidateDrawable() when changing this code.
4289            if (dr.mDrawableRight != null) {
4290                canvas.save();
4291                canvas.translate(scrollX + right - left - mPaddingRight - dr.mDrawableSizeRight,
4292                         scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2);
4293                dr.mDrawableRight.draw(canvas);
4294                canvas.restore();
4295            }
4296
4297            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
4298            // Make sure to update invalidateDrawable() when changing this code.
4299            if (dr.mDrawableTop != null) {
4300                canvas.save();
4301                canvas.translate(scrollX + compoundPaddingLeft + (hspace - dr.mDrawableWidthTop) / 2,
4302                        scrollY + mPaddingTop);
4303                dr.mDrawableTop.draw(canvas);
4304                canvas.restore();
4305            }
4306
4307            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
4308            // Make sure to update invalidateDrawable() when changing this code.
4309            if (dr.mDrawableBottom != null) {
4310                canvas.save();
4311                canvas.translate(scrollX + compoundPaddingLeft +
4312                        (hspace - dr.mDrawableWidthBottom) / 2,
4313                         scrollY + bottom - top - mPaddingBottom - dr.mDrawableSizeBottom);
4314                dr.mDrawableBottom.draw(canvas);
4315                canvas.restore();
4316            }
4317        }
4318
4319        int color = mCurTextColor;
4320
4321        if (mLayout == null) {
4322            assumeLayout();
4323        }
4324
4325        Layout layout = mLayout;
4326        int cursorcolor = color;
4327
4328        if (mHint != null && mText.length() == 0) {
4329            if (mHintTextColor != null) {
4330                color = mCurHintTextColor;
4331            }
4332
4333            layout = mHintLayout;
4334        }
4335
4336        mTextPaint.setColor(color);
4337        if (mCurrentAlpha != 255) {
4338            // If set, the alpha will override the color's alpha. Multiply the alphas.
4339            mTextPaint.setAlpha((mCurrentAlpha * Color.alpha(color)) / 255);
4340        }
4341        mTextPaint.drawableState = getDrawableState();
4342
4343        canvas.save();
4344        /*  Would be faster if we didn't have to do this. Can we chop the
4345            (displayable) text so that we don't need to do this ever?
4346        */
4347
4348        int extendedPaddingTop = getExtendedPaddingTop();
4349        int extendedPaddingBottom = getExtendedPaddingBottom();
4350
4351        float clipLeft = compoundPaddingLeft + scrollX;
4352        float clipTop = extendedPaddingTop + scrollY;
4353        float clipRight = right - left - compoundPaddingRight + scrollX;
4354        float clipBottom = bottom - top - extendedPaddingBottom + scrollY;
4355
4356        if (mShadowRadius != 0) {
4357            clipLeft += Math.min(0, mShadowDx - mShadowRadius);
4358            clipRight += Math.max(0, mShadowDx + mShadowRadius);
4359
4360            clipTop += Math.min(0, mShadowDy - mShadowRadius);
4361            clipBottom += Math.max(0, mShadowDy + mShadowRadius);
4362        }
4363
4364        canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
4365
4366        int voffsetText = 0;
4367        int voffsetCursor = 0;
4368
4369        // translate in by our padding
4370        {
4371            /* shortcircuit calling getVerticaOffset() */
4372            if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
4373                voffsetText = getVerticalOffset(false);
4374                voffsetCursor = getVerticalOffset(true);
4375            }
4376            canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText);
4377        }
4378
4379        if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
4380            if (!mSingleLine && getLineCount() == 1 && canMarquee() &&
4381                    (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) {
4382                canvas.translate(mLayout.getLineRight(0) - (mRight - mLeft -
4383                        getCompoundPaddingLeft() - getCompoundPaddingRight()), 0.0f);
4384            }
4385
4386            if (mMarquee != null && mMarquee.isRunning()) {
4387                canvas.translate(-mMarquee.mScroll, 0.0f);
4388            }
4389        }
4390
4391        Path highlight = null;
4392        int selStart = -1, selEnd = -1;
4393        boolean drawCursor = false;
4394
4395        //  If there is no movement method, then there can be no selection.
4396        //  Check that first and attempt to skip everything having to do with
4397        //  the cursor.
4398        //  XXX This is not strictly true -- a program could set the
4399        //  selection manually if it really wanted to.
4400        if (mMovement != null && (isFocused() || isPressed())) {
4401            selStart = getSelectionStart();
4402            selEnd = getSelectionEnd();
4403
4404            if ((isCursorVisible() || mTextIsSelectable) && selStart >= 0 && isEnabled()) {
4405                if (mHighlightPath == null)
4406                    mHighlightPath = new Path();
4407
4408                if (selStart == selEnd) {
4409                    if (!mTextIsSelectable &&
4410                            (SystemClock.uptimeMillis() - mShowCursor) % (2 * BLINK) < BLINK) {
4411                        if (mHighlightPathBogus) {
4412                            mHighlightPath.reset();
4413                            mLayout.getCursorPath(selStart, mHighlightPath, mText);
4414                            updateCursorsPositions();
4415                            mHighlightPathBogus = false;
4416                        }
4417
4418                        // XXX should pass to skin instead of drawing directly
4419                        mHighlightPaint.setColor(cursorcolor);
4420                        if (mCurrentAlpha != 255) {
4421                            mHighlightPaint.setAlpha(
4422                                    (mCurrentAlpha * Color.alpha(cursorcolor)) / 255);
4423                        }
4424                        mHighlightPaint.setStyle(Paint.Style.STROKE);
4425                        highlight = mHighlightPath;
4426                        drawCursor = mCursorCount > 0;
4427                    }
4428                } else {
4429                    if (mHighlightPathBogus) {
4430                        mHighlightPath.reset();
4431                        mLayout.getSelectionPath(selStart, selEnd, mHighlightPath);
4432                        mHighlightPathBogus = false;
4433                    }
4434
4435                    // XXX should pass to skin instead of drawing directly
4436                    mHighlightPaint.setColor(mHighlightColor);
4437                    if (mCurrentAlpha != 255) {
4438                        mHighlightPaint.setAlpha(
4439                                (mCurrentAlpha * Color.alpha(mHighlightColor)) / 255);
4440                    }
4441                    mHighlightPaint.setStyle(Paint.Style.FILL);
4442
4443                    highlight = mHighlightPath;
4444                }
4445            }
4446        }
4447
4448        /*  Comment out until we decide what to do about animations
4449        boolean isLinearTextOn = false;
4450        if (currentTransformation != null) {
4451            isLinearTextOn = mTextPaint.isLinearTextOn();
4452            Matrix m = currentTransformation.getMatrix();
4453            if (!m.isIdentity()) {
4454                // mTextPaint.setLinearTextOn(true);
4455            }
4456        }
4457        */
4458
4459        final InputMethodState ims = mInputMethodState;
4460        final int cursorOffsetVertical = voffsetCursor - voffsetText;
4461        if (ims != null && ims.mBatchEditNesting == 0) {
4462            InputMethodManager imm = InputMethodManager.peekInstance();
4463            if (imm != null) {
4464                if (imm.isActive(this)) {
4465                    boolean reported = false;
4466                    if (ims.mContentChanged || ims.mSelectionModeChanged) {
4467                        // We are in extract mode and the content has changed
4468                        // in some way... just report complete new text to the
4469                        // input method.
4470                        reported = reportExtractedText();
4471                    }
4472                    if (!reported && highlight != null) {
4473                        int candStart = -1;
4474                        int candEnd = -1;
4475                        if (mText instanceof Spannable) {
4476                            Spannable sp = (Spannable)mText;
4477                            candStart = EditableInputConnection.getComposingSpanStart(sp);
4478                            candEnd = EditableInputConnection.getComposingSpanEnd(sp);
4479                        }
4480                        imm.updateSelection(this, selStart, selEnd, candStart, candEnd);
4481                    }
4482                }
4483
4484                if (imm.isWatchingCursor(this) && highlight != null) {
4485                    highlight.computeBounds(ims.mTmpRectF, true);
4486                    ims.mTmpOffset[0] = ims.mTmpOffset[1] = 0;
4487
4488                    canvas.getMatrix().mapPoints(ims.mTmpOffset);
4489                    ims.mTmpRectF.offset(ims.mTmpOffset[0], ims.mTmpOffset[1]);
4490
4491                    ims.mTmpRectF.offset(0, cursorOffsetVertical);
4492
4493                    ims.mCursorRectInWindow.set((int)(ims.mTmpRectF.left + 0.5),
4494                            (int)(ims.mTmpRectF.top + 0.5),
4495                            (int)(ims.mTmpRectF.right + 0.5),
4496                            (int)(ims.mTmpRectF.bottom + 0.5));
4497
4498                    imm.updateCursor(this,
4499                            ims.mCursorRectInWindow.left, ims.mCursorRectInWindow.top,
4500                            ims.mCursorRectInWindow.right, ims.mCursorRectInWindow.bottom);
4501                }
4502            }
4503        }
4504
4505        if (mCorrectionHighlighter != null) {
4506            mCorrectionHighlighter.draw(canvas, cursorOffsetVertical);
4507        }
4508
4509        if (drawCursor) {
4510            drawCursor(canvas, cursorOffsetVertical);
4511            // Rely on the drawable entirely, do not draw the cursor line.
4512            // Has to be done after the IMM related code above which relies on the highlight.
4513            highlight = null;
4514        }
4515
4516        layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical);
4517
4518        if (mMarquee != null && mMarquee.shouldDrawGhost()) {
4519            canvas.translate((int) mMarquee.getGhostOffset(), 0.0f);
4520            layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical);
4521        }
4522
4523        /*  Comment out until we decide what to do about animations
4524        if (currentTransformation != null) {
4525            mTextPaint.setLinearTextOn(isLinearTextOn);
4526        }
4527        */
4528
4529        canvas.restore();
4530    }
4531
4532    private void updateCursorsPositions() {
4533        if (mCursorDrawableRes == 0) {
4534            mCursorCount = 0;
4535            return;
4536        }
4537
4538        final int offset = getSelectionStart();
4539        final int line = mLayout.getLineForOffset(offset);
4540        final int top = mLayout.getLineTop(line);
4541        final int bottom = mLayout.getLineTop(line + 1);
4542
4543        mCursorCount = mLayout.isLevelBoundary(offset) ? 2 : 1;
4544
4545        int middle = bottom;
4546        if (mCursorCount == 2) {
4547            // Similar to what is done in {@link Layout.#getCursorPath(int, Path, CharSequence)}
4548            middle = (top + bottom) >> 1;
4549        }
4550
4551        updateCursorPosition(0, top, middle, mLayout.getPrimaryHorizontal(offset));
4552
4553        if (mCursorCount == 2) {
4554            updateCursorPosition(1, middle, bottom, mLayout.getSecondaryHorizontal(offset));
4555        }
4556    }
4557
4558    private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) {
4559        if (mCursorDrawable[cursorIndex] == null)
4560            mCursorDrawable[cursorIndex] = mContext.getResources().getDrawable(mCursorDrawableRes);
4561
4562        if (mTempRect == null) mTempRect = new Rect();
4563
4564        mCursorDrawable[cursorIndex].getPadding(mTempRect);
4565        final int width = mCursorDrawable[cursorIndex].getIntrinsicWidth();
4566        horizontal = Math.max(0.5f, horizontal - 0.5f);
4567        final int left = (int) (horizontal) - mTempRect.left;
4568        mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width,
4569                bottom + mTempRect.bottom);
4570    }
4571
4572    private void drawCursor(Canvas canvas, int cursorOffsetVertical) {
4573        final boolean translate = cursorOffsetVertical != 0;
4574        if (translate) canvas.translate(0, cursorOffsetVertical);
4575        for (int i = 0; i < mCursorCount; i++) {
4576            mCursorDrawable[i].draw(canvas);
4577        }
4578        if (translate) canvas.translate(0, -cursorOffsetVertical);
4579    }
4580
4581    @Override
4582    public void getFocusedRect(Rect r) {
4583        if (mLayout == null) {
4584            super.getFocusedRect(r);
4585            return;
4586        }
4587
4588        int sel = getSelectionEnd();
4589        if (sel < 0) {
4590            super.getFocusedRect(r);
4591            return;
4592        }
4593
4594        int line = mLayout.getLineForOffset(sel);
4595        r.top = mLayout.getLineTop(line);
4596        r.bottom = mLayout.getLineBottom(line);
4597
4598        r.left = (int) mLayout.getPrimaryHorizontal(sel);
4599        r.right = r.left + 1;
4600
4601        // Adjust for padding and gravity.
4602        int paddingLeft = getCompoundPaddingLeft();
4603        int paddingTop = getExtendedPaddingTop();
4604        if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
4605            paddingTop += getVerticalOffset(false);
4606        }
4607        r.offset(paddingLeft, paddingTop);
4608    }
4609
4610    /**
4611     * Return the number of lines of text, or 0 if the internal Layout has not
4612     * been built.
4613     */
4614    public int getLineCount() {
4615        return mLayout != null ? mLayout.getLineCount() : 0;
4616    }
4617
4618    /**
4619     * Return the baseline for the specified line (0...getLineCount() - 1)
4620     * If bounds is not null, return the top, left, right, bottom extents
4621     * of the specified line in it. If the internal Layout has not been built,
4622     * return 0 and set bounds to (0, 0, 0, 0)
4623     * @param line which line to examine (0..getLineCount() - 1)
4624     * @param bounds Optional. If not null, it returns the extent of the line
4625     * @return the Y-coordinate of the baseline
4626     */
4627    public int getLineBounds(int line, Rect bounds) {
4628        if (mLayout == null) {
4629            if (bounds != null) {
4630                bounds.set(0, 0, 0, 0);
4631            }
4632            return 0;
4633        }
4634        else {
4635            int baseline = mLayout.getLineBounds(line, bounds);
4636
4637            int voffset = getExtendedPaddingTop();
4638            if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
4639                voffset += getVerticalOffset(true);
4640            }
4641            if (bounds != null) {
4642                bounds.offset(getCompoundPaddingLeft(), voffset);
4643            }
4644            return baseline + voffset;
4645        }
4646    }
4647
4648    @Override
4649    public int getBaseline() {
4650        if (mLayout == null) {
4651            return super.getBaseline();
4652        }
4653
4654        int voffset = 0;
4655        if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
4656            voffset = getVerticalOffset(true);
4657        }
4658
4659        return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0);
4660    }
4661
4662    @Override
4663    public boolean onKeyDown(int keyCode, KeyEvent event) {
4664        int which = doKeyDown(keyCode, event, null);
4665        if (which == 0) {
4666            // Go through default dispatching.
4667            return super.onKeyDown(keyCode, event);
4668        }
4669
4670        return true;
4671    }
4672
4673    @Override
4674    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4675        KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN);
4676
4677        int which = doKeyDown(keyCode, down, event);
4678        if (which == 0) {
4679            // Go through default dispatching.
4680            return super.onKeyMultiple(keyCode, repeatCount, event);
4681        }
4682        if (which == -1) {
4683            // Consumed the whole thing.
4684            return true;
4685        }
4686
4687        repeatCount--;
4688
4689        // We are going to dispatch the remaining events to either the input
4690        // or movement method.  To do this, we will just send a repeated stream
4691        // of down and up events until we have done the complete repeatCount.
4692        // It would be nice if those interfaces had an onKeyMultiple() method,
4693        // but adding that is a more complicated change.
4694        KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
4695        if (which == 1) {
4696            mInput.onKeyUp(this, (Editable)mText, keyCode, up);
4697            while (--repeatCount > 0) {
4698                mInput.onKeyDown(this, (Editable)mText, keyCode, down);
4699                mInput.onKeyUp(this, (Editable)mText, keyCode, up);
4700            }
4701            hideErrorIfUnchanged();
4702
4703        } else if (which == 2) {
4704            mMovement.onKeyUp(this, (Spannable)mText, keyCode, up);
4705            while (--repeatCount > 0) {
4706                mMovement.onKeyDown(this, (Spannable)mText, keyCode, down);
4707                mMovement.onKeyUp(this, (Spannable)mText, keyCode, up);
4708            }
4709        }
4710
4711        return true;
4712    }
4713
4714    /**
4715     * Returns true if pressing ENTER in this field advances focus instead
4716     * of inserting the character.  This is true mostly in single-line fields,
4717     * but also in mail addresses and subjects which will display on multiple
4718     * lines but where it doesn't make sense to insert newlines.
4719     */
4720    private boolean shouldAdvanceFocusOnEnter() {
4721        if (mInput == null) {
4722            return false;
4723        }
4724
4725        if (mSingleLine) {
4726            return true;
4727        }
4728
4729        if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
4730            int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION;
4731            if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
4732                    || variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT) {
4733                return true;
4734            }
4735        }
4736
4737        return false;
4738    }
4739
4740    /**
4741     * Returns true if pressing TAB in this field advances focus instead
4742     * of inserting the character.  Insert tabs only in multi-line editors.
4743     */
4744    private boolean shouldAdvanceFocusOnTab() {
4745        if (mInput != null && !mSingleLine) {
4746            if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
4747                int variation = mInputType & EditorInfo.TYPE_MASK_VARIATION;
4748                if (variation == EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE
4749                        || variation == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) {
4750                    return false;
4751                }
4752            }
4753        }
4754        return true;
4755    }
4756
4757    private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) {
4758        if (!isEnabled()) {
4759            return 0;
4760        }
4761
4762        switch (keyCode) {
4763            case KeyEvent.KEYCODE_ENTER:
4764                mEnterKeyIsDown = true;
4765                if (event.hasNoModifiers()) {
4766                    // When mInputContentType is set, we know that we are
4767                    // running in a "modern" cupcake environment, so don't need
4768                    // to worry about the application trying to capture
4769                    // enter key events.
4770                    if (mInputContentType != null) {
4771                        // If there is an action listener, given them a
4772                        // chance to consume the event.
4773                        if (mInputContentType.onEditorActionListener != null &&
4774                                mInputContentType.onEditorActionListener.onEditorAction(
4775                                this, EditorInfo.IME_NULL, event)) {
4776                            mInputContentType.enterDown = true;
4777                            // We are consuming the enter key for them.
4778                            return -1;
4779                        }
4780                    }
4781
4782                    // If our editor should move focus when enter is pressed, or
4783                    // this is a generated event from an IME action button, then
4784                    // don't let it be inserted into the text.
4785                    if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0
4786                            || shouldAdvanceFocusOnEnter()) {
4787                        if (mOnClickListener != null) {
4788                            return 0;
4789                        }
4790                        return -1;
4791                    }
4792                }
4793                break;
4794
4795            case KeyEvent.KEYCODE_DPAD_CENTER:
4796                mDPadCenterIsDown = true;
4797                if (event.hasNoModifiers()) {
4798                    if (shouldAdvanceFocusOnEnter()) {
4799                        return 0;
4800                    }
4801                }
4802                break;
4803
4804            case KeyEvent.KEYCODE_TAB:
4805                if (event.hasNoModifiers() || event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
4806                    if (shouldAdvanceFocusOnTab()) {
4807                        return 0;
4808                    }
4809                }
4810                break;
4811
4812                // Has to be done on key down (and not on key up) to correctly be intercepted.
4813            case KeyEvent.KEYCODE_BACK:
4814                if (mSelectionActionMode != null) {
4815                    stopSelectionActionMode();
4816                    return -1;
4817                }
4818                break;
4819        }
4820
4821        if (mInput != null) {
4822            resetErrorChangedFlag();
4823
4824            boolean doDown = true;
4825            if (otherEvent != null) {
4826                try {
4827                    beginBatchEdit();
4828                    final boolean handled = mInput.onKeyOther(this, (Editable) mText, otherEvent);
4829                    hideErrorIfUnchanged();
4830                    doDown = false;
4831                    if (handled) {
4832                        return -1;
4833                    }
4834                } catch (AbstractMethodError e) {
4835                    // onKeyOther was added after 1.0, so if it isn't
4836                    // implemented we need to try to dispatch as a regular down.
4837                } finally {
4838                    endBatchEdit();
4839                }
4840            }
4841
4842            if (doDown) {
4843                beginBatchEdit();
4844                final boolean handled = mInput.onKeyDown(this, (Editable) mText, keyCode, event);
4845                endBatchEdit();
4846                hideErrorIfUnchanged();
4847                if (handled) return 1;
4848            }
4849        }
4850
4851        // bug 650865: sometimes we get a key event before a layout.
4852        // don't try to move around if we don't know the layout.
4853
4854        if (mMovement != null && mLayout != null) {
4855            boolean doDown = true;
4856            if (otherEvent != null) {
4857                try {
4858                    boolean handled = mMovement.onKeyOther(this, (Spannable) mText,
4859                            otherEvent);
4860                    doDown = false;
4861                    if (handled) {
4862                        return -1;
4863                    }
4864                } catch (AbstractMethodError e) {
4865                    // onKeyOther was added after 1.0, so if it isn't
4866                    // implemented we need to try to dispatch as a regular down.
4867                }
4868            }
4869            if (doDown) {
4870                if (mMovement.onKeyDown(this, (Spannable)mText, keyCode, event))
4871                    return 2;
4872            }
4873        }
4874
4875        return 0;
4876    }
4877
4878    /**
4879     * Resets the mErrorWasChanged flag, so that future calls to {@link #setError(CharSequence)}
4880     * can be recorded.
4881     * @hide
4882     */
4883    public void resetErrorChangedFlag() {
4884        /*
4885         * Keep track of what the error was before doing the input
4886         * so that if an input filter changed the error, we leave
4887         * that error showing.  Otherwise, we take down whatever
4888         * error was showing when the user types something.
4889         */
4890        mErrorWasChanged = false;
4891    }
4892
4893    /**
4894     * @hide
4895     */
4896    public void hideErrorIfUnchanged() {
4897        if (mError != null && !mErrorWasChanged) {
4898            setError(null, null);
4899        }
4900    }
4901
4902    @Override
4903    public boolean onKeyUp(int keyCode, KeyEvent event) {
4904        if (!isEnabled()) {
4905            return super.onKeyUp(keyCode, event);
4906        }
4907
4908        switch (keyCode) {
4909            case KeyEvent.KEYCODE_DPAD_CENTER:
4910                mDPadCenterIsDown = false;
4911                if (event.hasNoModifiers()) {
4912                    /*
4913                     * If there is a click listener, just call through to
4914                     * super, which will invoke it.
4915                     *
4916                     * If there isn't a click listener, try to show the soft
4917                     * input method.  (It will also
4918                     * call performClick(), but that won't do anything in
4919                     * this case.)
4920                     */
4921                    if (mOnClickListener == null) {
4922                        if (mMovement != null && mText instanceof Editable
4923                                && mLayout != null && onCheckIsTextEditor()) {
4924                            InputMethodManager imm = InputMethodManager.peekInstance();
4925                            if (imm != null) imm.showSoftInput(this, 0);
4926                        }
4927                    }
4928                }
4929                return super.onKeyUp(keyCode, event);
4930
4931            case KeyEvent.KEYCODE_ENTER:
4932                mEnterKeyIsDown = false;
4933                if (event.hasNoModifiers()) {
4934                    if (mInputContentType != null
4935                            && mInputContentType.onEditorActionListener != null
4936                            && mInputContentType.enterDown) {
4937                        mInputContentType.enterDown = false;
4938                        if (mInputContentType.onEditorActionListener.onEditorAction(
4939                                this, EditorInfo.IME_NULL, event)) {
4940                            return true;
4941                        }
4942                    }
4943
4944                    if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0
4945                            || shouldAdvanceFocusOnEnter()) {
4946                        /*
4947                         * If there is a click listener, just call through to
4948                         * super, which will invoke it.
4949                         *
4950                         * If there isn't a click listener, try to advance focus,
4951                         * but still call through to super, which will reset the
4952                         * pressed state and longpress state.  (It will also
4953                         * call performClick(), but that won't do anything in
4954                         * this case.)
4955                         */
4956                        if (mOnClickListener == null) {
4957                            View v = focusSearch(FOCUS_DOWN);
4958
4959                            if (v != null) {
4960                                if (!v.requestFocus(FOCUS_DOWN)) {
4961                                    throw new IllegalStateException(
4962                                            "focus search returned a view " +
4963                                            "that wasn't able to take focus!");
4964                                }
4965
4966                                /*
4967                                 * Return true because we handled the key; super
4968                                 * will return false because there was no click
4969                                 * listener.
4970                                 */
4971                                super.onKeyUp(keyCode, event);
4972                                return true;
4973                            } else if ((event.getFlags()
4974                                    & KeyEvent.FLAG_EDITOR_ACTION) != 0) {
4975                                // No target for next focus, but make sure the IME
4976                                // if this came from it.
4977                                InputMethodManager imm = InputMethodManager.peekInstance();
4978                                if (imm != null && imm.isActive(this)) {
4979                                    imm.hideSoftInputFromWindow(getWindowToken(), 0);
4980                                }
4981                            }
4982                        }
4983                    }
4984                    return super.onKeyUp(keyCode, event);
4985                }
4986                break;
4987        }
4988
4989        if (mInput != null)
4990            if (mInput.onKeyUp(this, (Editable) mText, keyCode, event))
4991                return true;
4992
4993        if (mMovement != null && mLayout != null)
4994            if (mMovement.onKeyUp(this, (Spannable) mText, keyCode, event))
4995                return true;
4996
4997        return super.onKeyUp(keyCode, event);
4998    }
4999
5000    @Override public boolean onCheckIsTextEditor() {
5001        return mInputType != EditorInfo.TYPE_NULL;
5002    }
5003
5004    @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5005        if (onCheckIsTextEditor() && isEnabled()) {
5006            if (mInputMethodState == null) {
5007                mInputMethodState = new InputMethodState();
5008            }
5009            outAttrs.inputType = mInputType;
5010            if (mInputContentType != null) {
5011                outAttrs.imeOptions = mInputContentType.imeOptions;
5012                outAttrs.privateImeOptions = mInputContentType.privateImeOptions;
5013                outAttrs.actionLabel = mInputContentType.imeActionLabel;
5014                outAttrs.actionId = mInputContentType.imeActionId;
5015                outAttrs.extras = mInputContentType.extras;
5016            } else {
5017                outAttrs.imeOptions = EditorInfo.IME_NULL;
5018            }
5019            if (focusSearch(FOCUS_DOWN) != null) {
5020                outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT;
5021            }
5022            if (focusSearch(FOCUS_UP) != null) {
5023                outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS;
5024            }
5025            if ((outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION)
5026                    == EditorInfo.IME_ACTION_UNSPECIFIED) {
5027                if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) {
5028                    // An action has not been set, but the enter key will move to
5029                    // the next focus, so set the action to that.
5030                    outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
5031                } else {
5032                    // An action has not been set, and there is no focus to move
5033                    // to, so let's just supply a "done" action.
5034                    outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
5035                }
5036                if (!shouldAdvanceFocusOnEnter()) {
5037                    outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION;
5038                }
5039            }
5040            if (isMultilineInputType(outAttrs.inputType)) {
5041                // Multi-line text editors should always show an enter key.
5042                outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION;
5043            }
5044            outAttrs.hintText = mHint;
5045            if (mText instanceof Editable) {
5046                InputConnection ic = new EditableInputConnection(this);
5047                outAttrs.initialSelStart = getSelectionStart();
5048                outAttrs.initialSelEnd = getSelectionEnd();
5049                outAttrs.initialCapsMode = ic.getCursorCapsMode(mInputType);
5050                return ic;
5051            }
5052        }
5053        return null;
5054    }
5055
5056    /**
5057     * If this TextView contains editable content, extract a portion of it
5058     * based on the information in <var>request</var> in to <var>outText</var>.
5059     * @return Returns true if the text was successfully extracted, else false.
5060     */
5061    public boolean extractText(ExtractedTextRequest request,
5062            ExtractedText outText) {
5063        return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN,
5064                EXTRACT_UNKNOWN, outText);
5065    }
5066
5067    static final int EXTRACT_NOTHING = -2;
5068    static final int EXTRACT_UNKNOWN = -1;
5069
5070    boolean extractTextInternal(ExtractedTextRequest request,
5071            int partialStartOffset, int partialEndOffset, int delta,
5072            ExtractedText outText) {
5073        final CharSequence content = mText;
5074        if (content != null) {
5075            if (partialStartOffset != EXTRACT_NOTHING) {
5076                final int N = content.length();
5077                if (partialStartOffset < 0) {
5078                    outText.partialStartOffset = outText.partialEndOffset = -1;
5079                    partialStartOffset = 0;
5080                    partialEndOffset = N;
5081                } else {
5082                    // Now use the delta to determine the actual amount of text
5083                    // we need.
5084                    partialEndOffset += delta;
5085                    // Adjust offsets to ensure we contain full spans.
5086                    if (content instanceof Spanned) {
5087                        Spanned spanned = (Spanned)content;
5088                        Object[] spans = spanned.getSpans(partialStartOffset,
5089                                partialEndOffset, ParcelableSpan.class);
5090                        int i = spans.length;
5091                        while (i > 0) {
5092                            i--;
5093                            int j = spanned.getSpanStart(spans[i]);
5094                            if (j < partialStartOffset) partialStartOffset = j;
5095                            j = spanned.getSpanEnd(spans[i]);
5096                            if (j > partialEndOffset) partialEndOffset = j;
5097                        }
5098                    }
5099                    outText.partialStartOffset = partialStartOffset;
5100                    outText.partialEndOffset = partialEndOffset - delta;
5101
5102                    if (partialStartOffset > N) {
5103                        partialStartOffset = N;
5104                    } else if (partialStartOffset < 0) {
5105                        partialStartOffset = 0;
5106                    }
5107                    if (partialEndOffset > N) {
5108                        partialEndOffset = N;
5109                    } else if (partialEndOffset < 0) {
5110                        partialEndOffset = 0;
5111                    }
5112                }
5113                if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) {
5114                    outText.text = content.subSequence(partialStartOffset,
5115                            partialEndOffset);
5116                } else {
5117                    outText.text = TextUtils.substring(content, partialStartOffset,
5118                            partialEndOffset);
5119                }
5120            } else {
5121                outText.partialStartOffset = 0;
5122                outText.partialEndOffset = 0;
5123                outText.text = "";
5124            }
5125            outText.flags = 0;
5126            if (MetaKeyKeyListener.getMetaState(mText, MetaKeyKeyListener.META_SELECTING) != 0) {
5127                outText.flags |= ExtractedText.FLAG_SELECTING;
5128            }
5129            if (mSingleLine) {
5130                outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
5131            }
5132            outText.startOffset = 0;
5133            outText.selectionStart = getSelectionStart();
5134            outText.selectionEnd = getSelectionEnd();
5135            return true;
5136        }
5137        return false;
5138    }
5139
5140    boolean reportExtractedText() {
5141        final InputMethodState ims = mInputMethodState;
5142        if (ims != null) {
5143            final boolean contentChanged = ims.mContentChanged;
5144            if (contentChanged || ims.mSelectionModeChanged) {
5145                ims.mContentChanged = false;
5146                ims.mSelectionModeChanged = false;
5147                final ExtractedTextRequest req = mInputMethodState.mExtracting;
5148                if (req != null) {
5149                    InputMethodManager imm = InputMethodManager.peekInstance();
5150                    if (imm != null) {
5151                        if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Retrieving extracted start="
5152                                + ims.mChangedStart + " end=" + ims.mChangedEnd
5153                                + " delta=" + ims.mChangedDelta);
5154                        if (ims.mChangedStart < 0 && !contentChanged) {
5155                            ims.mChangedStart = EXTRACT_NOTHING;
5156                        }
5157                        if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd,
5158                                ims.mChangedDelta, ims.mTmpExtracted)) {
5159                            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Reporting extracted start="
5160                                    + ims.mTmpExtracted.partialStartOffset
5161                                    + " end=" + ims.mTmpExtracted.partialEndOffset
5162                                    + ": " + ims.mTmpExtracted.text);
5163                            imm.updateExtractedText(this, req.token,
5164                                    mInputMethodState.mTmpExtracted);
5165                            ims.mChangedStart = EXTRACT_UNKNOWN;
5166                            ims.mChangedEnd = EXTRACT_UNKNOWN;
5167                            ims.mChangedDelta = 0;
5168                            ims.mContentChanged = false;
5169                            return true;
5170                        }
5171                    }
5172                }
5173            }
5174        }
5175        return false;
5176    }
5177
5178    /**
5179     * This is used to remove all style-impacting spans from text before new
5180     * extracted text is being replaced into it, so that we don't have any
5181     * lingering spans applied during the replace.
5182     */
5183    static void removeParcelableSpans(Spannable spannable, int start, int end) {
5184        Object[] spans = spannable.getSpans(start, end, ParcelableSpan.class);
5185        int i = spans.length;
5186        while (i > 0) {
5187            i--;
5188            spannable.removeSpan(spans[i]);
5189        }
5190    }
5191
5192    /**
5193     * Apply to this text view the given extracted text, as previously
5194     * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}.
5195     */
5196    public void setExtractedText(ExtractedText text) {
5197        Editable content = getEditableText();
5198        if (text.text != null) {
5199            if (content == null) {
5200                setText(text.text, TextView.BufferType.EDITABLE);
5201            } else if (text.partialStartOffset < 0) {
5202                removeParcelableSpans(content, 0, content.length());
5203                content.replace(0, content.length(), text.text);
5204            } else {
5205                final int N = content.length();
5206                int start = text.partialStartOffset;
5207                if (start > N) start = N;
5208                int end = text.partialEndOffset;
5209                if (end > N) end = N;
5210                removeParcelableSpans(content, start, end);
5211                content.replace(start, end, text.text);
5212            }
5213        }
5214
5215        // Now set the selection position...  make sure it is in range, to
5216        // avoid crashes.  If this is a partial update, it is possible that
5217        // the underlying text may have changed, causing us problems here.
5218        // Also we just don't want to trust clients to do the right thing.
5219        Spannable sp = (Spannable)getText();
5220        final int N = sp.length();
5221        int start = text.selectionStart;
5222        if (start < 0) start = 0;
5223        else if (start > N) start = N;
5224        int end = text.selectionEnd;
5225        if (end < 0) end = 0;
5226        else if (end > N) end = N;
5227        Selection.setSelection(sp, start, end);
5228
5229        // Finally, update the selection mode.
5230        if ((text.flags&ExtractedText.FLAG_SELECTING) != 0) {
5231            MetaKeyKeyListener.startSelecting(this, sp);
5232        } else {
5233            MetaKeyKeyListener.stopSelecting(this, sp);
5234        }
5235    }
5236
5237    /**
5238     * @hide
5239     */
5240    public void setExtracting(ExtractedTextRequest req) {
5241        if (mInputMethodState != null) {
5242            mInputMethodState.mExtracting = req;
5243        }
5244        // This stops a possible text selection mode. Maybe not intended.
5245        hideControllers();
5246    }
5247
5248    /**
5249     * Called by the framework in response to a text completion from
5250     * the current input method, provided by it calling
5251     * {@link InputConnection#commitCompletion
5252     * InputConnection.commitCompletion()}.  The default implementation does
5253     * nothing; text views that are supporting auto-completion should override
5254     * this to do their desired behavior.
5255     *
5256     * @param text The auto complete text the user has selected.
5257     */
5258    public void onCommitCompletion(CompletionInfo text) {
5259        // intentionally empty
5260    }
5261
5262    /**
5263     * Called by the framework in response to a text auto-correction (such as fixing a typo using a
5264     * a dictionnary) from the current input method, provided by it calling
5265     * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default
5266     * implementation flashes the background of the corrected word to provide feedback to the user.
5267     *
5268     * @param info The auto correct info about the text that was corrected.
5269     */
5270    public void onCommitCorrection(CorrectionInfo info) {
5271        if (mCorrectionHighlighter == null) {
5272            mCorrectionHighlighter = new CorrectionHighlighter();
5273        } else {
5274            mCorrectionHighlighter.invalidate(false);
5275        }
5276
5277        mCorrectionHighlighter.highlight(info);
5278    }
5279
5280    private class CorrectionHighlighter {
5281        private final Path mPath = new Path();
5282        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
5283        private int mStart, mEnd;
5284        private long mFadingStartTime;
5285        private final static int FADE_OUT_DURATION = 400;
5286
5287        public CorrectionHighlighter() {
5288            mPaint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale);
5289            mPaint.setStyle(Paint.Style.FILL);
5290        }
5291
5292        public void highlight(CorrectionInfo info) {
5293            mStart = info.getOffset();
5294            mEnd = mStart + info.getNewText().length();
5295            mFadingStartTime = SystemClock.uptimeMillis();
5296
5297            if (mStart < 0 || mEnd < 0) {
5298                stopAnimation();
5299            }
5300        }
5301
5302        public void draw(Canvas canvas, int cursorOffsetVertical) {
5303            if (updatePath() && updatePaint()) {
5304                if (cursorOffsetVertical != 0) {
5305                    canvas.translate(0, cursorOffsetVertical);
5306                }
5307
5308                canvas.drawPath(mPath, mPaint);
5309
5310                if (cursorOffsetVertical != 0) {
5311                    canvas.translate(0, -cursorOffsetVertical);
5312                }
5313                invalidate(true);
5314            } else {
5315                stopAnimation();
5316                invalidate(false);
5317            }
5318        }
5319
5320        private boolean updatePaint() {
5321            final long duration = SystemClock.uptimeMillis() - mFadingStartTime;
5322            if (duration > FADE_OUT_DURATION) return false;
5323
5324            final float coef = 1.0f - (float) duration / FADE_OUT_DURATION;
5325            final int highlightColorAlpha = Color.alpha(mHighlightColor);
5326            final int color = (mHighlightColor & 0x00FFFFFF) +
5327                    ((int) (highlightColorAlpha * coef) << 24);
5328            mPaint.setColor(color);
5329            return true;
5330        }
5331
5332        private boolean updatePath() {
5333            final Layout layout = TextView.this.mLayout;
5334            if (layout == null) return false;
5335
5336            // Update in case text is edited while the animation is run
5337            final int length = mText.length();
5338            int start = Math.min(length, mStart);
5339            int end = Math.min(length, mEnd);
5340
5341            mPath.reset();
5342            TextView.this.mLayout.getSelectionPath(start, end, mPath);
5343            return true;
5344        }
5345
5346        private void invalidate(boolean delayed) {
5347            if (TextView.this.mLayout == null) return;
5348
5349            synchronized (sTempRect) {
5350                mPath.computeBounds(sTempRect, false);
5351
5352                int left = getCompoundPaddingLeft();
5353                int top = getExtendedPaddingTop() + getVerticalOffset(true);
5354
5355                if (delayed) {
5356                    TextView.this.postInvalidateDelayed(16, // 60 Hz update
5357                            left + (int) sTempRect.left, top + (int) sTempRect.top,
5358                            left + (int) sTempRect.right, top + (int) sTempRect.bottom);
5359                } else {
5360                    TextView.this.postInvalidate((int) sTempRect.left, (int) sTempRect.top,
5361                            (int) sTempRect.right, (int) sTempRect.bottom);
5362                }
5363            }
5364        }
5365
5366        private void stopAnimation() {
5367            TextView.this.mCorrectionHighlighter = null;
5368        }
5369    }
5370
5371    public void beginBatchEdit() {
5372        mInBatchEditControllers = true;
5373        final InputMethodState ims = mInputMethodState;
5374        if (ims != null) {
5375            int nesting = ++ims.mBatchEditNesting;
5376            if (nesting == 1) {
5377                ims.mCursorChanged = false;
5378                ims.mChangedDelta = 0;
5379                if (ims.mContentChanged) {
5380                    // We already have a pending change from somewhere else,
5381                    // so turn this into a full update.
5382                    ims.mChangedStart = 0;
5383                    ims.mChangedEnd = mText.length();
5384                } else {
5385                    ims.mChangedStart = EXTRACT_UNKNOWN;
5386                    ims.mChangedEnd = EXTRACT_UNKNOWN;
5387                    ims.mContentChanged = false;
5388                }
5389                onBeginBatchEdit();
5390            }
5391        }
5392    }
5393
5394    public void endBatchEdit() {
5395        mInBatchEditControllers = false;
5396        final InputMethodState ims = mInputMethodState;
5397        if (ims != null) {
5398            int nesting = --ims.mBatchEditNesting;
5399            if (nesting == 0) {
5400                finishBatchEdit(ims);
5401            }
5402        }
5403    }
5404
5405    void ensureEndedBatchEdit() {
5406        final InputMethodState ims = mInputMethodState;
5407        if (ims != null && ims.mBatchEditNesting != 0) {
5408            ims.mBatchEditNesting = 0;
5409            finishBatchEdit(ims);
5410        }
5411    }
5412
5413    void finishBatchEdit(final InputMethodState ims) {
5414        onEndBatchEdit();
5415
5416        if (ims.mContentChanged || ims.mSelectionModeChanged) {
5417            updateAfterEdit();
5418            reportExtractedText();
5419        } else if (ims.mCursorChanged) {
5420            // Cheezy way to get us to report the current cursor location.
5421            invalidateCursor();
5422        }
5423    }
5424
5425    void updateAfterEdit() {
5426        invalidate();
5427        int curs = getSelectionStart();
5428
5429        if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
5430            registerForPreDraw();
5431        }
5432
5433        if (curs >= 0) {
5434            mHighlightPathBogus = true;
5435            makeBlink();
5436        }
5437
5438        checkForResize();
5439    }
5440
5441    /**
5442     * Called by the framework in response to a request to begin a batch
5443     * of edit operations through a call to link {@link #beginBatchEdit()}.
5444     */
5445    public void onBeginBatchEdit() {
5446        // intentionally empty
5447    }
5448
5449    /**
5450     * Called by the framework in response to a request to end a batch
5451     * of edit operations through a call to link {@link #endBatchEdit}.
5452     */
5453    public void onEndBatchEdit() {
5454        // intentionally empty
5455    }
5456
5457    /**
5458     * Called by the framework in response to a private command from the
5459     * current method, provided by it calling
5460     * {@link InputConnection#performPrivateCommand
5461     * InputConnection.performPrivateCommand()}.
5462     *
5463     * @param action The action name of the command.
5464     * @param data Any additional data for the command.  This may be null.
5465     * @return Return true if you handled the command, else false.
5466     */
5467    public boolean onPrivateIMECommand(String action, Bundle data) {
5468        return false;
5469    }
5470
5471    private void nullLayouts() {
5472        if (mLayout instanceof BoringLayout && mSavedLayout == null) {
5473            mSavedLayout = (BoringLayout) mLayout;
5474        }
5475        if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) {
5476            mSavedHintLayout = (BoringLayout) mHintLayout;
5477        }
5478
5479        mLayout = mHintLayout = null;
5480
5481        // Since it depends on the value of mLayout
5482        prepareCursorControllers();
5483    }
5484
5485    /**
5486     * Make a new Layout based on the already-measured size of the view,
5487     * on the assumption that it was measured correctly at some point.
5488     */
5489    private void assumeLayout() {
5490        int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
5491
5492        if (width < 1) {
5493            width = 0;
5494        }
5495
5496        int physicalWidth = width;
5497
5498        if (mHorizontallyScrolling) {
5499            width = VERY_WIDE;
5500        }
5501
5502        makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING,
5503                      physicalWidth, false);
5504    }
5505
5506    /**
5507     * The width passed in is now the desired layout width,
5508     * not the full view width with padding.
5509     * {@hide}
5510     */
5511    protected void makeNewLayout(int w, int hintWidth,
5512                                 BoringLayout.Metrics boring,
5513                                 BoringLayout.Metrics hintBoring,
5514                                 int ellipsisWidth, boolean bringIntoView) {
5515        stopMarquee();
5516
5517        mHighlightPathBogus = true;
5518
5519        if (w < 0) {
5520            w = 0;
5521        }
5522        if (hintWidth < 0) {
5523            hintWidth = 0;
5524        }
5525
5526        Layout.Alignment alignment;
5527        switch (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
5528            case Gravity.CENTER_HORIZONTAL:
5529                alignment = Layout.Alignment.ALIGN_CENTER;
5530                break;
5531
5532            case Gravity.RIGHT:
5533                // Note, Layout resolves ALIGN_OPPOSITE to left or
5534                // right based on the paragraph direction.
5535                alignment = Layout.Alignment.ALIGN_OPPOSITE;
5536                break;
5537
5538            default:
5539                alignment = Layout.Alignment.ALIGN_NORMAL;
5540        }
5541
5542        boolean shouldEllipsize = mEllipsize != null && mInput == null;
5543
5544        if (mText instanceof Spannable) {
5545            mLayout = new DynamicLayout(mText, mTransformed, mTextPaint, w,
5546                    alignment, mSpacingMult,
5547                    mSpacingAdd, mIncludePad, mInput == null ? mEllipsize : null,
5548                    ellipsisWidth);
5549        } else {
5550            if (boring == UNKNOWN_BORING) {
5551                boring = BoringLayout.isBoring(mTransformed, mTextPaint, mBoring);
5552                if (boring != null) {
5553                    mBoring = boring;
5554                }
5555            }
5556
5557            if (boring != null) {
5558                if (boring.width <= w &&
5559                    (mEllipsize == null || boring.width <= ellipsisWidth)) {
5560                    if (mSavedLayout != null) {
5561                        mLayout = mSavedLayout.
5562                                replaceOrMake(mTransformed, mTextPaint,
5563                                w, alignment, mSpacingMult, mSpacingAdd,
5564                                boring, mIncludePad);
5565                    } else {
5566                        mLayout = BoringLayout.make(mTransformed, mTextPaint,
5567                                w, alignment, mSpacingMult, mSpacingAdd,
5568                                boring, mIncludePad);
5569                    }
5570
5571                    mSavedLayout = (BoringLayout) mLayout;
5572                } else if (shouldEllipsize && boring.width <= w) {
5573                    if (mSavedLayout != null) {
5574                        mLayout = mSavedLayout.
5575                                replaceOrMake(mTransformed, mTextPaint,
5576                                w, alignment, mSpacingMult, mSpacingAdd,
5577                                boring, mIncludePad, mEllipsize,
5578                                ellipsisWidth);
5579                    } else {
5580                        mLayout = BoringLayout.make(mTransformed, mTextPaint,
5581                                w, alignment, mSpacingMult, mSpacingAdd,
5582                                boring, mIncludePad, mEllipsize,
5583                                ellipsisWidth);
5584                    }
5585                } else if (shouldEllipsize) {
5586                    mLayout = new StaticLayout(mTransformed,
5587                                0, mTransformed.length(),
5588                                mTextPaint, w, alignment, mSpacingMult,
5589                                mSpacingAdd, mIncludePad, mEllipsize,
5590                                ellipsisWidth);
5591                } else {
5592                    mLayout = new StaticLayout(mTransformed, mTextPaint,
5593                            w, alignment, mSpacingMult, mSpacingAdd,
5594                            mIncludePad);
5595                }
5596            } else if (shouldEllipsize) {
5597                mLayout = new StaticLayout(mTransformed,
5598                            0, mTransformed.length(),
5599                            mTextPaint, w, alignment, mSpacingMult,
5600                            mSpacingAdd, mIncludePad, mEllipsize,
5601                            ellipsisWidth);
5602            } else {
5603                mLayout = new StaticLayout(mTransformed, mTextPaint,
5604                        w, alignment, mSpacingMult, mSpacingAdd,
5605                        mIncludePad);
5606            }
5607        }
5608
5609        shouldEllipsize = mEllipsize != null;
5610        mHintLayout = null;
5611
5612        if (mHint != null) {
5613            if (shouldEllipsize) hintWidth = w;
5614
5615            if (hintBoring == UNKNOWN_BORING) {
5616                hintBoring = BoringLayout.isBoring(mHint, mTextPaint,
5617                                                   mHintBoring);
5618                if (hintBoring != null) {
5619                    mHintBoring = hintBoring;
5620                }
5621            }
5622
5623            if (hintBoring != null) {
5624                if (hintBoring.width <= hintWidth &&
5625                    (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) {
5626                    if (mSavedHintLayout != null) {
5627                        mHintLayout = mSavedHintLayout.
5628                                replaceOrMake(mHint, mTextPaint,
5629                                hintWidth, alignment, mSpacingMult, mSpacingAdd,
5630                                hintBoring, mIncludePad);
5631                    } else {
5632                        mHintLayout = BoringLayout.make(mHint, mTextPaint,
5633                                hintWidth, alignment, mSpacingMult, mSpacingAdd,
5634                                hintBoring, mIncludePad);
5635                    }
5636
5637                    mSavedHintLayout = (BoringLayout) mHintLayout;
5638                } else if (shouldEllipsize && hintBoring.width <= hintWidth) {
5639                    if (mSavedHintLayout != null) {
5640                        mHintLayout = mSavedHintLayout.
5641                                replaceOrMake(mHint, mTextPaint,
5642                                hintWidth, alignment, mSpacingMult, mSpacingAdd,
5643                                hintBoring, mIncludePad, mEllipsize,
5644                                ellipsisWidth);
5645                    } else {
5646                        mHintLayout = BoringLayout.make(mHint, mTextPaint,
5647                                hintWidth, alignment, mSpacingMult, mSpacingAdd,
5648                                hintBoring, mIncludePad, mEllipsize,
5649                                ellipsisWidth);
5650                    }
5651                } else if (shouldEllipsize) {
5652                    mHintLayout = new StaticLayout(mHint,
5653                                0, mHint.length(),
5654                                mTextPaint, hintWidth, alignment, mSpacingMult,
5655                                mSpacingAdd, mIncludePad, mEllipsize,
5656                                ellipsisWidth);
5657                } else {
5658                    mHintLayout = new StaticLayout(mHint, mTextPaint,
5659                            hintWidth, alignment, mSpacingMult, mSpacingAdd,
5660                            mIncludePad);
5661                }
5662            } else if (shouldEllipsize) {
5663                mHintLayout = new StaticLayout(mHint,
5664                            0, mHint.length(),
5665                            mTextPaint, hintWidth, alignment, mSpacingMult,
5666                            mSpacingAdd, mIncludePad, mEllipsize,
5667                            ellipsisWidth);
5668            } else {
5669                mHintLayout = new StaticLayout(mHint, mTextPaint,
5670                        hintWidth, alignment, mSpacingMult, mSpacingAdd,
5671                        mIncludePad);
5672            }
5673        }
5674
5675        if (bringIntoView) {
5676            registerForPreDraw();
5677        }
5678
5679        if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
5680            if (!compressText(ellipsisWidth)) {
5681                final int height = mLayoutParams.height;
5682                // If the size of the view does not depend on the size of the text, try to
5683                // start the marquee immediately
5684                if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) {
5685                    startMarquee();
5686                } else {
5687                    // Defer the start of the marquee until we know our width (see setFrame())
5688                    mRestartMarquee = true;
5689                }
5690            }
5691        }
5692
5693        // CursorControllers need a non-null mLayout
5694        prepareCursorControllers();
5695    }
5696
5697    private boolean compressText(float width) {
5698        if (isHardwareAccelerated()) return false;
5699
5700        // Only compress the text if it hasn't been compressed by the previous pass
5701        if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX &&
5702                mTextPaint.getTextScaleX() == 1.0f) {
5703            final float textWidth = mLayout.getLineWidth(0);
5704            final float overflow = (textWidth + 1.0f - width) / width;
5705            if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) {
5706                mTextPaint.setTextScaleX(1.0f - overflow - 0.005f);
5707                post(new Runnable() {
5708                    public void run() {
5709                        requestLayout();
5710                    }
5711                });
5712                return true;
5713            }
5714        }
5715
5716        return false;
5717    }
5718
5719    private static int desired(Layout layout) {
5720        int n = layout.getLineCount();
5721        CharSequence text = layout.getText();
5722        float max = 0;
5723
5724        // if any line was wrapped, we can't use it.
5725        // but it's ok for the last line not to have a newline
5726
5727        for (int i = 0; i < n - 1; i++) {
5728            if (text.charAt(layout.getLineEnd(i) - 1) != '\n')
5729                return -1;
5730        }
5731
5732        for (int i = 0; i < n; i++) {
5733            max = Math.max(max, layout.getLineWidth(i));
5734        }
5735
5736        return (int) FloatMath.ceil(max);
5737    }
5738
5739    /**
5740     * Set whether the TextView includes extra top and bottom padding to make
5741     * room for accents that go above the normal ascent and descent.
5742     * The default is true.
5743     *
5744     * @attr ref android.R.styleable#TextView_includeFontPadding
5745     */
5746    public void setIncludeFontPadding(boolean includepad) {
5747        mIncludePad = includepad;
5748
5749        if (mLayout != null) {
5750            nullLayouts();
5751            requestLayout();
5752            invalidate();
5753        }
5754    }
5755
5756    private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics();
5757
5758    @Override
5759    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
5760        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
5761        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
5762        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
5763        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
5764
5765        int width;
5766        int height;
5767
5768        BoringLayout.Metrics boring = UNKNOWN_BORING;
5769        BoringLayout.Metrics hintBoring = UNKNOWN_BORING;
5770
5771        int des = -1;
5772        boolean fromexisting = false;
5773
5774        if (widthMode == MeasureSpec.EXACTLY) {
5775            // Parent has told us how big to be. So be it.
5776            width = widthSize;
5777        } else {
5778            if (mLayout != null && mEllipsize == null) {
5779                des = desired(mLayout);
5780            }
5781
5782            if (des < 0) {
5783                boring = BoringLayout.isBoring(mTransformed, mTextPaint, mBoring);
5784                if (boring != null) {
5785                    mBoring = boring;
5786                }
5787            } else {
5788                fromexisting = true;
5789            }
5790
5791            if (boring == null || boring == UNKNOWN_BORING) {
5792                if (des < 0) {
5793                    des = (int) FloatMath.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint));
5794                }
5795
5796                width = des;
5797            } else {
5798                width = boring.width;
5799            }
5800
5801            final Drawables dr = mDrawables;
5802            if (dr != null) {
5803                width = Math.max(width, dr.mDrawableWidthTop);
5804                width = Math.max(width, dr.mDrawableWidthBottom);
5805            }
5806
5807            if (mHint != null) {
5808                int hintDes = -1;
5809                int hintWidth;
5810
5811                if (mHintLayout != null && mEllipsize == null) {
5812                    hintDes = desired(mHintLayout);
5813                }
5814
5815                if (hintDes < 0) {
5816                    hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mHintBoring);
5817                    if (hintBoring != null) {
5818                        mHintBoring = hintBoring;
5819                    }
5820                }
5821
5822                if (hintBoring == null || hintBoring == UNKNOWN_BORING) {
5823                    if (hintDes < 0) {
5824                        hintDes = (int) FloatMath.ceil(
5825                                Layout.getDesiredWidth(mHint, mTextPaint));
5826                    }
5827
5828                    hintWidth = hintDes;
5829                } else {
5830                    hintWidth = hintBoring.width;
5831                }
5832
5833                if (hintWidth > width) {
5834                    width = hintWidth;
5835                }
5836            }
5837
5838            width += getCompoundPaddingLeft() + getCompoundPaddingRight();
5839
5840            if (mMaxWidthMode == EMS) {
5841                width = Math.min(width, mMaxWidth * getLineHeight());
5842            } else {
5843                width = Math.min(width, mMaxWidth);
5844            }
5845
5846            if (mMinWidthMode == EMS) {
5847                width = Math.max(width, mMinWidth * getLineHeight());
5848            } else {
5849                width = Math.max(width, mMinWidth);
5850            }
5851
5852            // Check against our minimum width
5853            width = Math.max(width, getSuggestedMinimumWidth());
5854
5855            if (widthMode == MeasureSpec.AT_MOST) {
5856                width = Math.min(widthSize, width);
5857            }
5858        }
5859
5860        int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight();
5861        int unpaddedWidth = want;
5862
5863        if (mHorizontallyScrolling) want = VERY_WIDE;
5864
5865        int hintWant = want;
5866        int hintWidth = mHintLayout == null ? hintWant : mHintLayout.getWidth();
5867
5868        if (mLayout == null) {
5869            makeNewLayout(want, hintWant, boring, hintBoring,
5870                          width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false);
5871        } else if ((mLayout.getWidth() != want) || (hintWidth != hintWant) ||
5872                   (mLayout.getEllipsizedWidth() !=
5873                        width - getCompoundPaddingLeft() - getCompoundPaddingRight())) {
5874            if (mHint == null && mEllipsize == null &&
5875                    want > mLayout.getWidth() &&
5876                    (mLayout instanceof BoringLayout ||
5877                            (fromexisting && des >= 0 && des <= want))) {
5878                mLayout.increaseWidthTo(want);
5879            } else {
5880                makeNewLayout(want, hintWant, boring, hintBoring,
5881                              width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false);
5882            }
5883        } else {
5884            // Width has not changed.
5885        }
5886
5887        if (heightMode == MeasureSpec.EXACTLY) {
5888            // Parent has told us how big to be. So be it.
5889            height = heightSize;
5890            mDesiredHeightAtMeasure = -1;
5891        } else {
5892            int desired = getDesiredHeight();
5893
5894            height = desired;
5895            mDesiredHeightAtMeasure = desired;
5896
5897            if (heightMode == MeasureSpec.AT_MOST) {
5898                height = Math.min(desired, heightSize);
5899            }
5900        }
5901
5902        int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom();
5903        if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) {
5904            unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum));
5905        }
5906
5907        /*
5908         * We didn't let makeNewLayout() register to bring the cursor into view,
5909         * so do it here if there is any possibility that it is needed.
5910         */
5911        if (mMovement != null ||
5912            mLayout.getWidth() > unpaddedWidth ||
5913            mLayout.getHeight() > unpaddedHeight) {
5914            registerForPreDraw();
5915        } else {
5916            scrollTo(0, 0);
5917        }
5918
5919        setMeasuredDimension(width, height);
5920    }
5921
5922    private int getDesiredHeight() {
5923        return Math.max(
5924                getDesiredHeight(mLayout, true),
5925                getDesiredHeight(mHintLayout, mEllipsize != null));
5926    }
5927
5928    private int getDesiredHeight(Layout layout, boolean cap) {
5929        if (layout == null) {
5930            return 0;
5931        }
5932
5933        int linecount = layout.getLineCount();
5934        int pad = getCompoundPaddingTop() + getCompoundPaddingBottom();
5935        int desired = layout.getLineTop(linecount);
5936
5937        final Drawables dr = mDrawables;
5938        if (dr != null) {
5939            desired = Math.max(desired, dr.mDrawableHeightLeft);
5940            desired = Math.max(desired, dr.mDrawableHeightRight);
5941        }
5942
5943        desired += pad;
5944        layout.setMaximumVisibleLineCount(0);
5945
5946        if (mMaxMode == LINES) {
5947            /*
5948             * Don't cap the hint to a certain number of lines.
5949             * (Do cap it, though, if we have a maximum pixel height.)
5950             */
5951            if (cap) {
5952                if (linecount > mMaximum) {
5953                    layout.setMaximumVisibleLineCount(mMaximum);
5954                    desired = layout.getLineTop(mMaximum);
5955
5956                    if (dr != null) {
5957                        desired = Math.max(desired, dr.mDrawableHeightLeft);
5958                        desired = Math.max(desired, dr.mDrawableHeightRight);
5959                    }
5960
5961                    desired += pad;
5962                    linecount = mMaximum;
5963                }
5964            }
5965        } else {
5966            desired = Math.min(desired, mMaximum);
5967        }
5968
5969        if (mMinMode == LINES) {
5970            if (linecount < mMinimum) {
5971                desired += getLineHeight() * (mMinimum - linecount);
5972            }
5973        } else {
5974            desired = Math.max(desired, mMinimum);
5975        }
5976
5977        // Check against our minimum height
5978        desired = Math.max(desired, getSuggestedMinimumHeight());
5979
5980        return desired;
5981    }
5982
5983    /**
5984     * Check whether a change to the existing text layout requires a
5985     * new view layout.
5986     */
5987    private void checkForResize() {
5988        boolean sizeChanged = false;
5989
5990        if (mLayout != null) {
5991            // Check if our width changed
5992            if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) {
5993                sizeChanged = true;
5994                invalidate();
5995            }
5996
5997            // Check if our height changed
5998            if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) {
5999                int desiredHeight = getDesiredHeight();
6000
6001                if (desiredHeight != this.getHeight()) {
6002                    sizeChanged = true;
6003                }
6004            } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) {
6005                if (mDesiredHeightAtMeasure >= 0) {
6006                    int desiredHeight = getDesiredHeight();
6007
6008                    if (desiredHeight != mDesiredHeightAtMeasure) {
6009                        sizeChanged = true;
6010                    }
6011                }
6012            }
6013        }
6014
6015        if (sizeChanged) {
6016            requestLayout();
6017            // caller will have already invalidated
6018        }
6019    }
6020
6021    /**
6022     * Check whether entirely new text requires a new view layout
6023     * or merely a new text layout.
6024     */
6025    private void checkForRelayout() {
6026        // If we have a fixed width, we can just swap in a new text layout
6027        // if the text height stays the same or if the view height is fixed.
6028
6029        if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||
6030                (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&
6031                (mHint == null || mHintLayout != null) &&
6032                (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {
6033            // Static width, so try making a new text layout.
6034
6035            int oldht = mLayout.getHeight();
6036            int want = mLayout.getWidth();
6037            int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();
6038
6039            /*
6040             * No need to bring the text into view, since the size is not
6041             * changing (unless we do the requestLayout(), in which case it
6042             * will happen at measure).
6043             */
6044            makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
6045                          mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),
6046                          false);
6047
6048            if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {
6049                // In a fixed-height view, so use our new text layout.
6050                if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&
6051                    mLayoutParams.height != LayoutParams.MATCH_PARENT) {
6052                    invalidate();
6053                    return;
6054                }
6055
6056                // Dynamic height, but height has stayed the same,
6057                // so use our new text layout.
6058                if (mLayout.getHeight() == oldht &&
6059                    (mHintLayout == null || mHintLayout.getHeight() == oldht)) {
6060                    invalidate();
6061                    return;
6062                }
6063            }
6064
6065            // We lose: the height has changed and we have a dynamic height.
6066            // Request a new view layout using our new text layout.
6067            requestLayout();
6068            invalidate();
6069        } else {
6070            // Dynamic width, so we have no choice but to request a new
6071            // view layout with a new text layout.
6072
6073            nullLayouts();
6074            requestLayout();
6075            invalidate();
6076        }
6077    }
6078
6079    /**
6080     * Returns true if anything changed.
6081     */
6082    private boolean bringTextIntoView() {
6083        int line = 0;
6084        if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
6085            line = mLayout.getLineCount() - 1;
6086        }
6087
6088        Layout.Alignment a = mLayout.getParagraphAlignment(line);
6089        int dir = mLayout.getParagraphDirection(line);
6090        int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
6091        int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
6092        int ht = mLayout.getHeight();
6093
6094        int scrollx, scrolly;
6095
6096        if (a == Layout.Alignment.ALIGN_CENTER) {
6097            /*
6098             * Keep centered if possible, or, if it is too wide to fit,
6099             * keep leading edge in view.
6100             */
6101
6102            int left = (int) FloatMath.floor(mLayout.getLineLeft(line));
6103            int right = (int) FloatMath.ceil(mLayout.getLineRight(line));
6104
6105            if (right - left < hspace) {
6106                scrollx = (right + left) / 2 - hspace / 2;
6107            } else {
6108                if (dir < 0) {
6109                    scrollx = right - hspace;
6110                } else {
6111                    scrollx = left;
6112                }
6113            }
6114        } else if (a == Layout.Alignment.ALIGN_NORMAL) {
6115            /*
6116             * Keep leading edge in view.
6117             */
6118
6119            if (dir < 0) {
6120                int right = (int) FloatMath.ceil(mLayout.getLineRight(line));
6121                scrollx = right - hspace;
6122            } else {
6123                scrollx = (int) FloatMath.floor(mLayout.getLineLeft(line));
6124            }
6125        } else /* a == Layout.Alignment.ALIGN_OPPOSITE */ {
6126            /*
6127             * Keep trailing edge in view.
6128             */
6129
6130            if (dir < 0) {
6131                scrollx = (int) FloatMath.floor(mLayout.getLineLeft(line));
6132            } else {
6133                int right = (int) FloatMath.ceil(mLayout.getLineRight(line));
6134                scrollx = right - hspace;
6135            }
6136        }
6137
6138        if (ht < vspace) {
6139            scrolly = 0;
6140        } else {
6141            if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
6142                scrolly = ht - vspace;
6143            } else {
6144                scrolly = 0;
6145            }
6146        }
6147
6148        if (scrollx != mScrollX || scrolly != mScrollY) {
6149            scrollTo(scrollx, scrolly);
6150            return true;
6151        } else {
6152            return false;
6153        }
6154    }
6155
6156    /**
6157     * Move the point, specified by the offset, into the view if it is needed.
6158     * This has to be called after layout. Returns true if anything changed.
6159     */
6160    public boolean bringPointIntoView(int offset) {
6161        boolean changed = false;
6162
6163        int line = mLayout.getLineForOffset(offset);
6164
6165        // FIXME: Is it okay to truncate this, or should we round?
6166        final int x = (int)mLayout.getPrimaryHorizontal(offset);
6167        final int top = mLayout.getLineTop(line);
6168        final int bottom = mLayout.getLineTop(line + 1);
6169
6170        int left = (int) FloatMath.floor(mLayout.getLineLeft(line));
6171        int right = (int) FloatMath.ceil(mLayout.getLineRight(line));
6172        int ht = mLayout.getHeight();
6173
6174        int grav;
6175
6176        switch (mLayout.getParagraphAlignment(line)) {
6177            case ALIGN_NORMAL:
6178                grav = 1;
6179                break;
6180
6181            case ALIGN_OPPOSITE:
6182                grav = -1;
6183                break;
6184
6185            default:
6186                grav = 0;
6187        }
6188
6189        grav *= mLayout.getParagraphDirection(line);
6190
6191        int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
6192        int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
6193
6194        int hslack = (bottom - top) / 2;
6195        int vslack = hslack;
6196
6197        if (vslack > vspace / 4)
6198            vslack = vspace / 4;
6199        if (hslack > hspace / 4)
6200            hslack = hspace / 4;
6201
6202        int hs = mScrollX;
6203        int vs = mScrollY;
6204
6205        if (top - vs < vslack)
6206            vs = top - vslack;
6207        if (bottom - vs > vspace - vslack)
6208            vs = bottom - (vspace - vslack);
6209        if (ht - vs < vspace)
6210            vs = ht - vspace;
6211        if (0 - vs > 0)
6212            vs = 0;
6213
6214        if (grav != 0) {
6215            if (x - hs < hslack) {
6216                hs = x - hslack;
6217            }
6218            if (x - hs > hspace - hslack) {
6219                hs = x - (hspace - hslack);
6220            }
6221        }
6222
6223        if (grav < 0) {
6224            if (left - hs > 0)
6225                hs = left;
6226            if (right - hs < hspace)
6227                hs = right - hspace;
6228        } else if (grav > 0) {
6229            if (right - hs < hspace)
6230                hs = right - hspace;
6231            if (left - hs > 0)
6232                hs = left;
6233        } else /* grav == 0 */ {
6234            if (right - left <= hspace) {
6235                /*
6236                 * If the entire text fits, center it exactly.
6237                 */
6238                hs = left - (hspace - (right - left)) / 2;
6239            } else if (x > right - hslack) {
6240                /*
6241                 * If we are near the right edge, keep the right edge
6242                 * at the edge of the view.
6243                 */
6244                hs = right - hspace;
6245            } else if (x < left + hslack) {
6246                /*
6247                 * If we are near the left edge, keep the left edge
6248                 * at the edge of the view.
6249                 */
6250                hs = left;
6251            } else if (left > hs) {
6252                /*
6253                 * Is there whitespace visible at the left?  Fix it if so.
6254                 */
6255                hs = left;
6256            } else if (right < hs + hspace) {
6257                /*
6258                 * Is there whitespace visible at the right?  Fix it if so.
6259                 */
6260                hs = right - hspace;
6261            } else {
6262                /*
6263                 * Otherwise, float as needed.
6264                 */
6265                if (x - hs < hslack) {
6266                    hs = x - hslack;
6267                }
6268                if (x - hs > hspace - hslack) {
6269                    hs = x - (hspace - hslack);
6270                }
6271            }
6272        }
6273
6274        if (hs != mScrollX || vs != mScrollY) {
6275            if (mScroller == null) {
6276                scrollTo(hs, vs);
6277            } else {
6278                long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
6279                int dx = hs - mScrollX;
6280                int dy = vs - mScrollY;
6281
6282                if (duration > ANIMATED_SCROLL_GAP) {
6283                    mScroller.startScroll(mScrollX, mScrollY, dx, dy);
6284                    awakenScrollBars(mScroller.getDuration());
6285                    invalidate();
6286                } else {
6287                    if (!mScroller.isFinished()) {
6288                        mScroller.abortAnimation();
6289                    }
6290
6291                    scrollBy(dx, dy);
6292                }
6293
6294                mLastScroll = AnimationUtils.currentAnimationTimeMillis();
6295            }
6296
6297            changed = true;
6298        }
6299
6300        if (isFocused()) {
6301            // This offsets because getInterestingRect() is in terms of viewport coordinates, but
6302            // requestRectangleOnScreen() is in terms of content coordinates.
6303
6304            if (mTempRect == null) mTempRect = new Rect();
6305            mTempRect.set(x, top, x + 1, bottom);
6306            getInterestingRect(mTempRect, line);
6307            mTempRect.offset(mScrollX, mScrollY);
6308
6309            if (requestRectangleOnScreen(mTempRect)) {
6310                changed = true;
6311            }
6312        }
6313
6314        return changed;
6315    }
6316
6317    /**
6318     * Move the cursor, if needed, so that it is at an offset that is visible
6319     * to the user.  This will not move the cursor if it represents more than
6320     * one character (a selection range).  This will only work if the
6321     * TextView contains spannable text; otherwise it will do nothing.
6322     *
6323     * @return True if the cursor was actually moved, false otherwise.
6324     */
6325    public boolean moveCursorToVisibleOffset() {
6326        if (!(mText instanceof Spannable)) {
6327            return false;
6328        }
6329        int start = getSelectionStart();
6330        int end = getSelectionEnd();
6331        if (start != end) {
6332            return false;
6333        }
6334
6335        // First: make sure the line is visible on screen:
6336
6337        int line = mLayout.getLineForOffset(start);
6338
6339        final int top = mLayout.getLineTop(line);
6340        final int bottom = mLayout.getLineTop(line + 1);
6341        final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
6342        int vslack = (bottom - top) / 2;
6343        if (vslack > vspace / 4)
6344            vslack = vspace / 4;
6345        final int vs = mScrollY;
6346
6347        if (top < (vs+vslack)) {
6348            line = mLayout.getLineForVertical(vs+vslack+(bottom-top));
6349        } else if (bottom > (vspace+vs-vslack)) {
6350            line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top));
6351        }
6352
6353        // Next: make sure the character is visible on screen:
6354
6355        final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
6356        final int hs = mScrollX;
6357        final int leftChar = mLayout.getOffsetForHorizontal(line, hs);
6358        final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs);
6359
6360        // line might contain bidirectional text
6361        final int lowChar = leftChar < rightChar ? leftChar : rightChar;
6362        final int highChar = leftChar > rightChar ? leftChar : rightChar;
6363
6364        int newStart = start;
6365        if (newStart < lowChar) {
6366            newStart = lowChar;
6367        } else if (newStart > highChar) {
6368            newStart = highChar;
6369        }
6370
6371        if (newStart != start) {
6372            Selection.setSelection((Spannable)mText, newStart);
6373            return true;
6374        }
6375
6376        return false;
6377    }
6378
6379    @Override
6380    public void computeScroll() {
6381        if (mScroller != null) {
6382            if (mScroller.computeScrollOffset()) {
6383                mScrollX = mScroller.getCurrX();
6384                mScrollY = mScroller.getCurrY();
6385                invalidateParentCaches();
6386                postInvalidate();  // So we draw again
6387            }
6388        }
6389    }
6390
6391    private void getInterestingRect(Rect r, int line) {
6392        convertFromViewportToContentCoordinates(r);
6393
6394        // Rectangle can can be expanded on first and last line to take
6395        // padding into account.
6396        // TODO Take left/right padding into account too?
6397        if (line == 0) r.top -= getExtendedPaddingTop();
6398        if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom();
6399    }
6400
6401    private void convertFromViewportToContentCoordinates(Rect r) {
6402        final int horizontalOffset = viewportToContentHorizontalOffset();
6403        r.left += horizontalOffset;
6404        r.right += horizontalOffset;
6405
6406        final int verticalOffset = viewportToContentVerticalOffset();
6407        r.top += verticalOffset;
6408        r.bottom += verticalOffset;
6409    }
6410
6411    private int viewportToContentHorizontalOffset() {
6412        return getCompoundPaddingLeft() - mScrollX;
6413    }
6414
6415    private int viewportToContentVerticalOffset() {
6416        int offset = getExtendedPaddingTop() - mScrollY;
6417        if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
6418            offset += getVerticalOffset(false);
6419        }
6420        return offset;
6421    }
6422
6423    @Override
6424    public void debug(int depth) {
6425        super.debug(depth);
6426
6427        String output = debugIndent(depth);
6428        output += "frame={" + mLeft + ", " + mTop + ", " + mRight
6429                + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
6430                + "} ";
6431
6432        if (mText != null) {
6433
6434            output += "mText=\"" + mText + "\" ";
6435            if (mLayout != null) {
6436                output += "mLayout width=" + mLayout.getWidth()
6437                        + " height=" + mLayout.getHeight();
6438            }
6439        } else {
6440            output += "mText=NULL";
6441        }
6442        Log.d(VIEW_LOG_TAG, output);
6443    }
6444
6445    /**
6446     * Convenience for {@link Selection#getSelectionStart}.
6447     */
6448    @ViewDebug.ExportedProperty(category = "text")
6449    public int getSelectionStart() {
6450        return Selection.getSelectionStart(getText());
6451    }
6452
6453    /**
6454     * Convenience for {@link Selection#getSelectionEnd}.
6455     */
6456    @ViewDebug.ExportedProperty(category = "text")
6457    public int getSelectionEnd() {
6458        return Selection.getSelectionEnd(getText());
6459    }
6460
6461    /**
6462     * Return true iff there is a selection inside this text view.
6463     */
6464    public boolean hasSelection() {
6465        final int selectionStart = getSelectionStart();
6466        final int selectionEnd = getSelectionEnd();
6467
6468        return selectionStart >= 0 && selectionStart != selectionEnd;
6469    }
6470
6471    /**
6472     * Sets the properties of this field (lines, horizontally scrolling,
6473     * transformation method) to be for a single-line input.
6474     *
6475     * @attr ref android.R.styleable#TextView_singleLine
6476     */
6477    public void setSingleLine() {
6478        setSingleLine(true);
6479    }
6480
6481    /**
6482     * If true, sets the properties of this field (number of lines, horizontally scrolling,
6483     * transformation method) to be for a single-line input; if false, restores these to the default
6484     * conditions.
6485     *
6486     * Note that the default conditions are not necessarily those that were in effect prior this
6487     * method, and you may want to reset these properties to your custom values.
6488     *
6489     * @attr ref android.R.styleable#TextView_singleLine
6490     */
6491    @android.view.RemotableViewMethod
6492    public void setSingleLine(boolean singleLine) {
6493        // Could be used, but may break backward compatibility.
6494        // if (mSingleLine == singleLine) return;
6495        setInputTypeSingleLine(singleLine);
6496        applySingleLine(singleLine, true, true);
6497    }
6498
6499    /**
6500     * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType.
6501     * @param singleLine
6502     */
6503    private void setInputTypeSingleLine(boolean singleLine) {
6504        if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
6505            if (singleLine) {
6506                mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
6507            } else {
6508                mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
6509            }
6510        }
6511    }
6512
6513    private void applySingleLine(boolean singleLine, boolean applyTransformation,
6514            boolean changeMaxLines) {
6515        mSingleLine = singleLine;
6516        if (singleLine) {
6517            setLines(1);
6518            setHorizontallyScrolling(true);
6519            if (applyTransformation) {
6520                setTransformationMethod(SingleLineTransformationMethod.getInstance());
6521            }
6522        } else {
6523            if (changeMaxLines) {
6524                setMaxLines(Integer.MAX_VALUE);
6525            }
6526            setHorizontallyScrolling(false);
6527            if (applyTransformation) {
6528                setTransformationMethod(null);
6529            }
6530        }
6531    }
6532
6533    /**
6534     * Causes words in the text that are longer than the view is wide
6535     * to be ellipsized instead of broken in the middle.  You may also
6536     * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling}
6537     * to constrain the text to a single line.  Use <code>null</code>
6538     * to turn off ellipsizing.
6539     *
6540     * @attr ref android.R.styleable#TextView_ellipsize
6541     */
6542    public void setEllipsize(TextUtils.TruncateAt where) {
6543        mEllipsize = where;
6544
6545        if (mLayout != null) {
6546            nullLayouts();
6547            requestLayout();
6548            invalidate();
6549        }
6550    }
6551
6552    /**
6553     * Sets how many times to repeat the marquee animation. Only applied if the
6554     * TextView has marquee enabled. Set to -1 to repeat indefinitely.
6555     *
6556     * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
6557     */
6558    public void setMarqueeRepeatLimit(int marqueeLimit) {
6559        mMarqueeRepeatLimit = marqueeLimit;
6560    }
6561
6562    /**
6563     * Returns where, if anywhere, words that are longer than the view
6564     * is wide should be ellipsized.
6565     */
6566    @ViewDebug.ExportedProperty
6567    public TextUtils.TruncateAt getEllipsize() {
6568        return mEllipsize;
6569    }
6570
6571    /**
6572     * Set the TextView so that when it takes focus, all the text is
6573     * selected.
6574     *
6575     * @attr ref android.R.styleable#TextView_selectAllOnFocus
6576     */
6577    @android.view.RemotableViewMethod
6578    public void setSelectAllOnFocus(boolean selectAllOnFocus) {
6579        mSelectAllOnFocus = selectAllOnFocus;
6580
6581        if (selectAllOnFocus && !(mText instanceof Spannable)) {
6582            setText(mText, BufferType.SPANNABLE);
6583        }
6584    }
6585
6586    /**
6587     * Set whether the cursor is visible.  The default is true.
6588     *
6589     * @attr ref android.R.styleable#TextView_cursorVisible
6590     */
6591    @android.view.RemotableViewMethod
6592    public void setCursorVisible(boolean visible) {
6593        if (mCursorVisible != visible) {
6594            mCursorVisible = visible;
6595            invalidate();
6596
6597            makeBlink();
6598
6599            // InsertionPointCursorController depends on mCursorVisible
6600            prepareCursorControllers();
6601        }
6602    }
6603
6604    private boolean isCursorVisible() {
6605        return mCursorVisible && isTextEditable();
6606    }
6607
6608    private boolean canMarquee() {
6609        int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight());
6610        return width > 0 && mLayout.getLineWidth(0) > width;
6611    }
6612
6613    private void startMarquee() {
6614        // Do not ellipsize EditText
6615        if (mInput != null) return;
6616
6617        if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) {
6618            return;
6619        }
6620
6621        if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) &&
6622                getLineCount() == 1 && canMarquee()) {
6623
6624            if (mMarquee == null) mMarquee = new Marquee(this);
6625            mMarquee.start(mMarqueeRepeatLimit);
6626        }
6627    }
6628
6629    private void stopMarquee() {
6630        if (mMarquee != null && !mMarquee.isStopped()) {
6631            mMarquee.stop();
6632        }
6633    }
6634
6635    private void startStopMarquee(boolean start) {
6636        if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
6637            if (start) {
6638                startMarquee();
6639            } else {
6640                stopMarquee();
6641            }
6642        }
6643    }
6644
6645    private static final class Marquee extends Handler {
6646        // TODO: Add an option to configure this
6647        private static final float MARQUEE_DELTA_MAX = 0.07f;
6648        private static final int MARQUEE_DELAY = 1200;
6649        private static final int MARQUEE_RESTART_DELAY = 1200;
6650        private static final int MARQUEE_RESOLUTION = 1000 / 30;
6651        private static final int MARQUEE_PIXELS_PER_SECOND = 30;
6652
6653        private static final byte MARQUEE_STOPPED = 0x0;
6654        private static final byte MARQUEE_STARTING = 0x1;
6655        private static final byte MARQUEE_RUNNING = 0x2;
6656
6657        private static final int MESSAGE_START = 0x1;
6658        private static final int MESSAGE_TICK = 0x2;
6659        private static final int MESSAGE_RESTART = 0x3;
6660
6661        private final WeakReference<TextView> mView;
6662
6663        private byte mStatus = MARQUEE_STOPPED;
6664        private final float mScrollUnit;
6665        private float mMaxScroll;
6666        float mMaxFadeScroll;
6667        private float mGhostStart;
6668        private float mGhostOffset;
6669        private float mFadeStop;
6670        private int mRepeatLimit;
6671
6672        float mScroll;
6673
6674        Marquee(TextView v) {
6675            final float density = v.getContext().getResources().getDisplayMetrics().density;
6676            mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION;
6677            mView = new WeakReference<TextView>(v);
6678        }
6679
6680        @Override
6681        public void handleMessage(Message msg) {
6682            switch (msg.what) {
6683                case MESSAGE_START:
6684                    mStatus = MARQUEE_RUNNING;
6685                    tick();
6686                    break;
6687                case MESSAGE_TICK:
6688                    tick();
6689                    break;
6690                case MESSAGE_RESTART:
6691                    if (mStatus == MARQUEE_RUNNING) {
6692                        if (mRepeatLimit >= 0) {
6693                            mRepeatLimit--;
6694                        }
6695                        start(mRepeatLimit);
6696                    }
6697                    break;
6698            }
6699        }
6700
6701        void tick() {
6702            if (mStatus != MARQUEE_RUNNING) {
6703                return;
6704            }
6705
6706            removeMessages(MESSAGE_TICK);
6707
6708            final TextView textView = mView.get();
6709            if (textView != null && (textView.isFocused() || textView.isSelected())) {
6710                mScroll += mScrollUnit;
6711                if (mScroll > mMaxScroll) {
6712                    mScroll = mMaxScroll;
6713                    sendEmptyMessageDelayed(MESSAGE_RESTART, MARQUEE_RESTART_DELAY);
6714                } else {
6715                    sendEmptyMessageDelayed(MESSAGE_TICK, MARQUEE_RESOLUTION);
6716                }
6717                textView.invalidate();
6718            }
6719        }
6720
6721        void stop() {
6722            mStatus = MARQUEE_STOPPED;
6723            removeMessages(MESSAGE_START);
6724            removeMessages(MESSAGE_RESTART);
6725            removeMessages(MESSAGE_TICK);
6726            resetScroll();
6727        }
6728
6729        private void resetScroll() {
6730            mScroll = 0.0f;
6731            final TextView textView = mView.get();
6732            if (textView != null) textView.invalidate();
6733        }
6734
6735        void start(int repeatLimit) {
6736            if (repeatLimit == 0) {
6737                stop();
6738                return;
6739            }
6740            mRepeatLimit = repeatLimit;
6741            final TextView textView = mView.get();
6742            if (textView != null && textView.mLayout != null) {
6743                mStatus = MARQUEE_STARTING;
6744                mScroll = 0.0f;
6745                final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() -
6746                        textView.getCompoundPaddingRight();
6747                final float lineWidth = textView.mLayout.getLineWidth(0);
6748                final float gap = textWidth / 3.0f;
6749                mGhostStart = lineWidth - textWidth + gap;
6750                mMaxScroll = mGhostStart + textWidth;
6751                mGhostOffset = lineWidth + gap;
6752                mFadeStop = lineWidth + textWidth / 6.0f;
6753                mMaxFadeScroll = mGhostStart + lineWidth + lineWidth;
6754
6755                textView.invalidate();
6756                sendEmptyMessageDelayed(MESSAGE_START, MARQUEE_DELAY);
6757            }
6758        }
6759
6760        float getGhostOffset() {
6761            return mGhostOffset;
6762        }
6763
6764        boolean shouldDrawLeftFade() {
6765            return mScroll <= mFadeStop;
6766        }
6767
6768        boolean shouldDrawGhost() {
6769            return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart;
6770        }
6771
6772        boolean isRunning() {
6773            return mStatus == MARQUEE_RUNNING;
6774        }
6775
6776        boolean isStopped() {
6777            return mStatus == MARQUEE_STOPPED;
6778        }
6779    }
6780
6781    /**
6782     * This method is called when the text is changed, in case any subclasses
6783     * would like to know.
6784     *
6785     * Within <code>text</code>, the <code>lengthAfter</code> characters
6786     * beginning at <code>start</code> have just replaced old text that had
6787     * length <code>lengthBefore</code>. It is an error to attempt to make
6788     * changes to <code>text</code> from this callback.
6789     *
6790     * @param text The text the TextView is displaying
6791     * @param start The offset of the start of the range of the text that was
6792     * modified
6793     * @param lengthBefore The length of the former text that has been replaced
6794     * @param lengthAfter The length of the replacement modified text
6795     */
6796    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
6797        // intentionally empty
6798    }
6799
6800    /**
6801     * This method is called when the selection has changed, in case any
6802     * subclasses would like to know.
6803     *
6804     * @param selStart The new selection start location.
6805     * @param selEnd The new selection end location.
6806     */
6807    protected void onSelectionChanged(int selStart, int selEnd) {
6808        // intentionally empty
6809    }
6810
6811    /**
6812     * Adds a TextWatcher to the list of those whose methods are called
6813     * whenever this TextView's text changes.
6814     * <p>
6815     * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously
6816     * not called after {@link #setText} calls.  Now, doing {@link #setText}
6817     * if there are any text changed listeners forces the buffer type to
6818     * Editable if it would not otherwise be and does call this method.
6819     */
6820    public void addTextChangedListener(TextWatcher watcher) {
6821        if (mListeners == null) {
6822            mListeners = new ArrayList<TextWatcher>();
6823        }
6824
6825        mListeners.add(watcher);
6826    }
6827
6828    /**
6829     * Removes the specified TextWatcher from the list of those whose
6830     * methods are called
6831     * whenever this TextView's text changes.
6832     */
6833    public void removeTextChangedListener(TextWatcher watcher) {
6834        if (mListeners != null) {
6835            int i = mListeners.indexOf(watcher);
6836
6837            if (i >= 0) {
6838                mListeners.remove(i);
6839            }
6840        }
6841    }
6842
6843    private void sendBeforeTextChanged(CharSequence text, int start, int before,
6844                                   int after) {
6845        if (mListeners != null) {
6846            final ArrayList<TextWatcher> list = mListeners;
6847            final int count = list.size();
6848            for (int i = 0; i < count; i++) {
6849                list.get(i).beforeTextChanged(text, start, before, after);
6850            }
6851        }
6852    }
6853
6854    /**
6855     * Not private so it can be called from an inner class without going
6856     * through a thunk.
6857     */
6858    void sendOnTextChanged(CharSequence text, int start, int before,
6859                                   int after) {
6860        if (mListeners != null) {
6861            final ArrayList<TextWatcher> list = mListeners;
6862            final int count = list.size();
6863            for (int i = 0; i < count; i++) {
6864                list.get(i).onTextChanged(text, start, before, after);
6865            }
6866        }
6867    }
6868
6869    /**
6870     * Not private so it can be called from an inner class without going
6871     * through a thunk.
6872     */
6873    void sendAfterTextChanged(Editable text) {
6874        if (mListeners != null) {
6875            final ArrayList<TextWatcher> list = mListeners;
6876            final int count = list.size();
6877            for (int i = 0; i < count; i++) {
6878                list.get(i).afterTextChanged(text);
6879            }
6880        }
6881    }
6882
6883    /**
6884     * Not private so it can be called from an inner class without going
6885     * through a thunk.
6886     */
6887    void handleTextChanged(CharSequence buffer, int start, int before, int after) {
6888        final InputMethodState ims = mInputMethodState;
6889        if (ims == null || ims.mBatchEditNesting == 0) {
6890            updateAfterEdit();
6891        }
6892        if (ims != null) {
6893            ims.mContentChanged = true;
6894            if (ims.mChangedStart < 0) {
6895                ims.mChangedStart = start;
6896                ims.mChangedEnd = start+before;
6897            } else {
6898                ims.mChangedStart = Math.min(ims.mChangedStart, start);
6899                ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta);
6900            }
6901            ims.mChangedDelta += after-before;
6902        }
6903
6904        sendOnTextChanged(buffer, start, before, after);
6905        onTextChanged(buffer, start, before, after);
6906
6907        // Hide the controllers if the amount of content changed
6908        if (before != after) {
6909            hideControllers();
6910        }
6911    }
6912
6913    /**
6914     * Not private so it can be called from an inner class without going
6915     * through a thunk.
6916     */
6917    void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) {
6918        // XXX Make the start and end move together if this ends up
6919        // spending too much time invalidating.
6920
6921        boolean selChanged = false;
6922        int newSelStart=-1, newSelEnd=-1;
6923
6924        final InputMethodState ims = mInputMethodState;
6925
6926        if (what == Selection.SELECTION_END) {
6927            mHighlightPathBogus = true;
6928            selChanged = true;
6929            newSelEnd = newStart;
6930
6931            if (!isFocused()) {
6932                mSelectionMoved = true;
6933            }
6934
6935            if (oldStart >= 0 || newStart >= 0) {
6936                invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart);
6937                registerForPreDraw();
6938                makeBlink();
6939            }
6940        }
6941
6942        if (what == Selection.SELECTION_START) {
6943            mHighlightPathBogus = true;
6944            selChanged = true;
6945            newSelStart = newStart;
6946
6947            if (!isFocused()) {
6948                mSelectionMoved = true;
6949            }
6950
6951            if (oldStart >= 0 || newStart >= 0) {
6952                int end = Selection.getSelectionEnd(buf);
6953                invalidateCursor(end, oldStart, newStart);
6954            }
6955        }
6956
6957        if (selChanged) {
6958            if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) {
6959                if (newSelStart < 0) {
6960                    newSelStart = Selection.getSelectionStart(buf);
6961                }
6962                if (newSelEnd < 0) {
6963                    newSelEnd = Selection.getSelectionEnd(buf);
6964                }
6965                onSelectionChanged(newSelStart, newSelEnd);
6966            }
6967        }
6968
6969        if (what instanceof UpdateAppearance ||
6970            what instanceof ParagraphStyle) {
6971            if (ims == null || ims.mBatchEditNesting == 0) {
6972                invalidate();
6973                mHighlightPathBogus = true;
6974                checkForResize();
6975            } else {
6976                ims.mContentChanged = true;
6977            }
6978        }
6979
6980        if (MetaKeyKeyListener.isMetaTracker(buf, what)) {
6981            mHighlightPathBogus = true;
6982            if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) {
6983                ims.mSelectionModeChanged = true;
6984            }
6985
6986            if (Selection.getSelectionStart(buf) >= 0) {
6987                if (ims == null || ims.mBatchEditNesting == 0) {
6988                    invalidateCursor();
6989                } else {
6990                    ims.mCursorChanged = true;
6991                }
6992            }
6993        }
6994
6995        if (what instanceof ParcelableSpan) {
6996            // If this is a span that can be sent to a remote process,
6997            // the current extract editor would be interested in it.
6998            if (ims != null && ims.mExtracting != null) {
6999                if (ims.mBatchEditNesting != 0) {
7000                    if (oldStart >= 0) {
7001                        if (ims.mChangedStart > oldStart) {
7002                            ims.mChangedStart = oldStart;
7003                        }
7004                        if (ims.mChangedStart > oldEnd) {
7005                            ims.mChangedStart = oldEnd;
7006                        }
7007                    }
7008                    if (newStart >= 0) {
7009                        if (ims.mChangedStart > newStart) {
7010                            ims.mChangedStart = newStart;
7011                        }
7012                        if (ims.mChangedStart > newEnd) {
7013                            ims.mChangedStart = newEnd;
7014                        }
7015                    }
7016                } else {
7017                    if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: "
7018                            + oldStart + "-" + oldEnd + ","
7019                            + newStart + "-" + newEnd + what);
7020                    ims.mContentChanged = true;
7021                }
7022            }
7023        }
7024    }
7025
7026    private class ChangeWatcher
7027    implements TextWatcher, SpanWatcher {
7028
7029        private CharSequence mBeforeText;
7030
7031        public void beforeTextChanged(CharSequence buffer, int start,
7032                                      int before, int after) {
7033            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start
7034                    + " before=" + before + " after=" + after + ": " + buffer);
7035
7036            if (AccessibilityManager.getInstance(mContext).isEnabled()
7037                    && !isPasswordInputType(mInputType)
7038                    && !hasPasswordTransformationMethod()) {
7039                mBeforeText = buffer.toString();
7040            }
7041
7042            TextView.this.sendBeforeTextChanged(buffer, start, before, after);
7043        }
7044
7045        public void onTextChanged(CharSequence buffer, int start,
7046                                  int before, int after) {
7047            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start
7048                    + " before=" + before + " after=" + after + ": " + buffer);
7049            TextView.this.handleTextChanged(buffer, start, before, after);
7050
7051            if (AccessibilityManager.getInstance(mContext).isEnabled() &&
7052                    (isFocused() || isSelected() &&
7053                    isShown())) {
7054                sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after);
7055                mBeforeText = null;
7056            }
7057        }
7058
7059        public void afterTextChanged(Editable buffer) {
7060            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer);
7061            TextView.this.sendAfterTextChanged(buffer);
7062
7063            if (MetaKeyKeyListener.getMetaState(buffer,
7064                                 MetaKeyKeyListener.META_SELECTING) != 0) {
7065                MetaKeyKeyListener.stopSelecting(TextView.this, buffer);
7066            }
7067        }
7068
7069        public void onSpanChanged(Spannable buf,
7070                                  Object what, int s, int e, int st, int en) {
7071            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e
7072                    + " st=" + st + " en=" + en + " what=" + what + ": " + buf);
7073            TextView.this.spanChange(buf, what, s, st, e, en);
7074        }
7075
7076        public void onSpanAdded(Spannable buf, Object what, int s, int e) {
7077            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e
7078                    + " what=" + what + ": " + buf);
7079            TextView.this.spanChange(buf, what, -1, s, -1, e);
7080        }
7081
7082        public void onSpanRemoved(Spannable buf, Object what, int s, int e) {
7083            if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e
7084                    + " what=" + what + ": " + buf);
7085            TextView.this.spanChange(buf, what, s, -1, e, -1);
7086        }
7087    }
7088
7089    /**
7090     * @hide
7091     */
7092    @Override
7093    public void dispatchFinishTemporaryDetach() {
7094        mDispatchTemporaryDetach = true;
7095        super.dispatchFinishTemporaryDetach();
7096        mDispatchTemporaryDetach = false;
7097    }
7098
7099    @Override
7100    public void onStartTemporaryDetach() {
7101        super.onStartTemporaryDetach();
7102        // Only track when onStartTemporaryDetach() is called directly,
7103        // usually because this instance is an editable field in a list
7104        if (!mDispatchTemporaryDetach) mTemporaryDetach = true;
7105
7106        // Because of View recycling in ListView, there is no easy way to know when a TextView with
7107        // selection becomes visible again. Until a better solution is found, stop text selection
7108        // mode (if any) as soon as this TextView is recycled.
7109        stopSelectionActionMode();
7110    }
7111
7112    @Override
7113    public void onFinishTemporaryDetach() {
7114        super.onFinishTemporaryDetach();
7115        // Only track when onStartTemporaryDetach() is called directly,
7116        // usually because this instance is an editable field in a list
7117        if (!mDispatchTemporaryDetach) mTemporaryDetach = false;
7118    }
7119
7120    @Override
7121    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
7122        if (mTemporaryDetach) {
7123            // If we are temporarily in the detach state, then do nothing.
7124            super.onFocusChanged(focused, direction, previouslyFocusedRect);
7125            return;
7126        }
7127
7128        mShowCursor = SystemClock.uptimeMillis();
7129
7130        ensureEndedBatchEdit();
7131
7132        if (focused) {
7133            int selStart = getSelectionStart();
7134            int selEnd = getSelectionEnd();
7135
7136            // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
7137            // mode for these, unless there was a specific selection already started.
7138            final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 &&
7139                    selEnd == mText.length();
7140            mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted;
7141
7142            if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
7143                // If a tap was used to give focus to that view, move cursor at tap position.
7144                // Has to be done before onTakeFocus, which can be overloaded.
7145                final int lastTapPosition = getLastTapPosition();
7146                if (lastTapPosition >= 0) {
7147                    Selection.setSelection((Spannable) mText, lastTapPosition);
7148                }
7149
7150                if (mMovement != null) {
7151                    mMovement.onTakeFocus(this, (Spannable) mText, direction);
7152                }
7153
7154                // The DecorView does not have focus when the 'Done' ExtractEditText button is
7155                // pressed. Since it is the ViewAncestor's mView, it requests focus before
7156                // ExtractEditText clears focus, which gives focus to the ExtractEditText.
7157                // This special case ensure that we keep current selection in that case.
7158                // It would be better to know why the DecorView does not have focus at that time.
7159                if (((this instanceof ExtractEditText) || mSelectionMoved) &&
7160                        selStart >= 0 && selEnd >= 0) {
7161                    /*
7162                     * Someone intentionally set the selection, so let them
7163                     * do whatever it is that they wanted to do instead of
7164                     * the default on-focus behavior.  We reset the selection
7165                     * here instead of just skipping the onTakeFocus() call
7166                     * because some movement methods do something other than
7167                     * just setting the selection in theirs and we still
7168                     * need to go through that path.
7169                     */
7170                    Selection.setSelection((Spannable) mText, selStart, selEnd);
7171                }
7172
7173                if (mSelectAllOnFocus) {
7174                    selectAll();
7175                }
7176
7177                mTouchFocusSelected = true;
7178            }
7179
7180            mFrozenWithFocus = false;
7181            mSelectionMoved = false;
7182
7183            if (mText instanceof Spannable) {
7184                Spannable sp = (Spannable) mText;
7185                MetaKeyKeyListener.resetMetaState(sp);
7186            }
7187
7188            makeBlink();
7189
7190            if (mError != null) {
7191                showError();
7192            }
7193        } else {
7194            if (mError != null) {
7195                hideError();
7196            }
7197            // Don't leave us in the middle of a batch edit.
7198            onEndBatchEdit();
7199
7200            if (this instanceof ExtractEditText) {
7201                // terminateTextSelectionMode removes selection, which we want to keep when
7202                // ExtractEditText goes out of focus.
7203                final int selStart = getSelectionStart();
7204                final int selEnd = getSelectionEnd();
7205                hideControllers();
7206                Selection.setSelection((Spannable) mText, selStart, selEnd);
7207            } else {
7208                hideControllers();
7209            }
7210
7211            // No need to create the controller
7212            if (mSelectionModifierCursorController != null) {
7213                mSelectionModifierCursorController.resetTouchOffsets();
7214            }
7215        }
7216
7217        startStopMarquee(focused);
7218
7219        if (mTransformation != null) {
7220            mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect);
7221        }
7222
7223        super.onFocusChanged(focused, direction, previouslyFocusedRect);
7224    }
7225
7226    private int getLastTapPosition() {
7227        // No need to create the controller at that point, no last tap position saved
7228        if (mSelectionModifierCursorController != null) {
7229            int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset();
7230            if (lastTapPosition >= 0) {
7231                // Safety check, should not be possible.
7232                if (lastTapPosition > mText.length()) {
7233                    Log.e(LOG_TAG, "Invalid tap focus position (" + lastTapPosition + " vs "
7234                            + mText.length() + ")");
7235                    lastTapPosition = mText.length();
7236                }
7237                return lastTapPosition;
7238            }
7239        }
7240
7241        return -1;
7242    }
7243
7244    @Override
7245    public void onWindowFocusChanged(boolean hasWindowFocus) {
7246        super.onWindowFocusChanged(hasWindowFocus);
7247
7248        if (hasWindowFocus) {
7249            if (mBlink != null) {
7250                mBlink.uncancel();
7251                makeBlink();
7252            }
7253        } else {
7254            if (mBlink != null) {
7255                mBlink.cancel();
7256            }
7257            // Don't leave us in the middle of a batch edit.
7258            onEndBatchEdit();
7259            if (mInputContentType != null) {
7260                mInputContentType.enterDown = false;
7261            }
7262            hideControllers();
7263            removeAllSuggestionSpans();
7264        }
7265
7266        startStopMarquee(hasWindowFocus);
7267    }
7268
7269    private void removeAllSuggestionSpans() {
7270        if (mText instanceof Editable) {
7271            Editable editable = ((Editable) mText);
7272            SuggestionSpan[] spans = editable.getSpans(0, mText.length(), SuggestionSpan.class);
7273            final int length = spans.length;
7274            for (int i = 0; i < length; i++) {
7275                editable.removeSpan(spans[i]);
7276            }
7277        }
7278    }
7279
7280    @Override
7281    protected void onVisibilityChanged(View changedView, int visibility) {
7282        super.onVisibilityChanged(changedView, visibility);
7283        if (visibility != VISIBLE) {
7284            hideControllers();
7285        }
7286    }
7287
7288    /**
7289     * Use {@link BaseInputConnection#removeComposingSpans
7290     * BaseInputConnection.removeComposingSpans()} to remove any IME composing
7291     * state from this text view.
7292     */
7293    public void clearComposingText() {
7294        if (mText instanceof Spannable) {
7295            BaseInputConnection.removeComposingSpans((Spannable)mText);
7296        }
7297    }
7298
7299    @Override
7300    public void setSelected(boolean selected) {
7301        boolean wasSelected = isSelected();
7302
7303        super.setSelected(selected);
7304
7305        if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
7306            if (selected) {
7307                startMarquee();
7308            } else {
7309                stopMarquee();
7310            }
7311        }
7312    }
7313
7314    @Override
7315    public boolean onTouchEvent(MotionEvent event) {
7316        final int action = event.getActionMasked();
7317
7318        if (hasInsertionController()) {
7319            getInsertionController().onTouchEvent(event);
7320        }
7321        if (hasSelectionController()) {
7322            getSelectionController().onTouchEvent(event);
7323        }
7324
7325        if (action == MotionEvent.ACTION_DOWN) {
7326            mLastDownPositionX = (int) event.getX();
7327            mLastDownPositionY = (int) event.getY();
7328
7329            // Reset this state; it will be re-set if super.onTouchEvent
7330            // causes focus to move to the view.
7331            mTouchFocusSelected = false;
7332            mIgnoreActionUpEvent = false;
7333        }
7334
7335        final boolean superResult = super.onTouchEvent(event);
7336
7337        /*
7338         * Don't handle the release after a long press, because it will
7339         * move the selection away from whatever the menu action was
7340         * trying to affect.
7341         */
7342        if (mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
7343            mDiscardNextActionUp = false;
7344            return superResult;
7345        }
7346
7347        final boolean touchIsFinished = action == MotionEvent.ACTION_UP && !mIgnoreActionUpEvent &&
7348                isFocused();
7349
7350        if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
7351                && mText instanceof Spannable && mLayout != null) {
7352            boolean handled = false;
7353
7354            if (mMovement != null) {
7355                handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
7356            }
7357
7358            if (mLinksClickable && mAutoLinkMask != 0 && mTextIsSelectable && touchIsFinished) {
7359                // The LinkMovementMethod which should handle taps on links has not been installed
7360                // to support text selection. We reproduce its behavior here to open links.
7361                ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
7362                        getSelectionEnd(), ClickableSpan.class);
7363
7364                if (links.length != 0) {
7365                    links[0].onClick(this);
7366                    handled = true;
7367                }
7368            }
7369
7370            if ((isTextEditable() || mTextIsSelectable) && touchIsFinished) {
7371                // Show the IME, except when selecting in read-only text.
7372                if (!mTextIsSelectable) {
7373                    final InputMethodManager imm = InputMethodManager.peekInstance();
7374                    handled |= imm != null && imm.showSoftInput(this, 0);
7375                }
7376
7377                boolean selectAllGotFocus = mSelectAllOnFocus && didTouchFocusSelect();
7378                if (!selectAllGotFocus && hasSelection()) {
7379                    startSelectionActionMode();
7380                } else {
7381                    stopSelectionActionMode();
7382                    hideSuggestions();
7383                    if (hasInsertionController() && !selectAllGotFocus && mText.length() > 0) {
7384                        getInsertionController().show();
7385                    }
7386                }
7387            }
7388
7389            if (handled) {
7390                return true;
7391            }
7392        }
7393
7394        return superResult;
7395    }
7396
7397    @Override
7398    public boolean onGenericMotionEvent(MotionEvent event) {
7399        if (mMovement != null && mText instanceof Spannable && mLayout != null) {
7400            try {
7401                if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) {
7402                    return true;
7403                }
7404            } catch (AbstractMethodError ex) {
7405                // onGenericMotionEvent was added to the MovementMethod interface in API 12.
7406                // Ignore its absence in case third party applications implemented the
7407                // interface directly.
7408            }
7409        }
7410        return super.onGenericMotionEvent(event);
7411    }
7412
7413    private void prepareCursorControllers() {
7414        boolean windowSupportsHandles = false;
7415
7416        ViewGroup.LayoutParams params = getRootView().getLayoutParams();
7417        if (params instanceof WindowManager.LayoutParams) {
7418            WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
7419            windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
7420                    || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
7421        }
7422
7423        mInsertionControllerEnabled = windowSupportsHandles && isCursorVisible() && mLayout != null;
7424        mSelectionControllerEnabled = windowSupportsHandles && textCanBeSelected() &&
7425                mLayout != null;
7426
7427        if (!mInsertionControllerEnabled) {
7428            hideInsertionPointCursorController();
7429            if (mInsertionPointCursorController != null) {
7430                mInsertionPointCursorController.onDetached();
7431                mInsertionPointCursorController = null;
7432            }
7433        }
7434
7435        if (!mSelectionControllerEnabled) {
7436            stopSelectionActionMode();
7437            if (mSelectionModifierCursorController != null) {
7438                mSelectionModifierCursorController.onDetached();
7439                mSelectionModifierCursorController = null;
7440            }
7441        }
7442    }
7443
7444    /**
7445     * @return True iff this TextView contains a text that can be edited, or if this is
7446     * a selectable TextView.
7447     */
7448    private boolean isTextEditable() {
7449        return mText instanceof Editable && onCheckIsTextEditor() && isEnabled();
7450    }
7451
7452    /**
7453     * Returns true, only while processing a touch gesture, if the initial
7454     * touch down event caused focus to move to the text view and as a result
7455     * its selection changed.  Only valid while processing the touch gesture
7456     * of interest.
7457     */
7458    public boolean didTouchFocusSelect() {
7459        return mTouchFocusSelected;
7460    }
7461
7462    @Override
7463    public void cancelLongPress() {
7464        super.cancelLongPress();
7465        mIgnoreActionUpEvent = true;
7466    }
7467
7468    @Override
7469    public boolean onTrackballEvent(MotionEvent event) {
7470        if (mMovement != null && mText instanceof Spannable &&
7471            mLayout != null) {
7472            if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) {
7473                return true;
7474            }
7475        }
7476
7477        return super.onTrackballEvent(event);
7478    }
7479
7480    public void setScroller(Scroller s) {
7481        mScroller = s;
7482    }
7483
7484    private static class Blink extends Handler implements Runnable {
7485        private final WeakReference<TextView> mView;
7486        private boolean mCancelled;
7487
7488        public Blink(TextView v) {
7489            mView = new WeakReference<TextView>(v);
7490        }
7491
7492        public void run() {
7493            if (mCancelled) {
7494                return;
7495            }
7496
7497            removeCallbacks(Blink.this);
7498
7499            TextView tv = mView.get();
7500
7501            if (tv != null && tv.shouldBlink()) {
7502                if (tv.mLayout != null) {
7503                    tv.invalidateCursorPath();
7504                }
7505
7506                postAtTime(this, SystemClock.uptimeMillis() + BLINK);
7507            }
7508        }
7509
7510        void cancel() {
7511            if (!mCancelled) {
7512                removeCallbacks(Blink.this);
7513                mCancelled = true;
7514            }
7515        }
7516
7517        void uncancel() {
7518            mCancelled = false;
7519        }
7520    }
7521
7522    /**
7523     * @return True when the TextView isFocused and has a valid zero-length selection (cursor).
7524     */
7525    private boolean shouldBlink() {
7526        if (!isFocused()) return false;
7527
7528        final int start = getSelectionStart();
7529        if (start < 0) return false;
7530
7531        final int end = getSelectionEnd();
7532        if (end < 0) return false;
7533
7534        return start == end;
7535    }
7536
7537    private void makeBlink() {
7538        if (isCursorVisible()) {
7539            if (shouldBlink()) {
7540                mShowCursor = SystemClock.uptimeMillis();
7541                if (mBlink == null) mBlink = new Blink(this);
7542                mBlink.removeCallbacks(mBlink);
7543                mBlink.postAtTime(mBlink, mShowCursor + BLINK);
7544            }
7545        } else {
7546            if (mBlink != null) mBlink.removeCallbacks(mBlink);
7547        }
7548    }
7549
7550    @Override
7551    protected float getLeftFadingEdgeStrength() {
7552        if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f;
7553        if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
7554            if (mMarquee != null && !mMarquee.isStopped()) {
7555                final Marquee marquee = mMarquee;
7556                if (marquee.shouldDrawLeftFade()) {
7557                    return marquee.mScroll / getHorizontalFadingEdgeLength();
7558                } else {
7559                    return 0.0f;
7560                }
7561            } else if (getLineCount() == 1) {
7562                switch (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
7563                    case Gravity.LEFT:
7564                        return 0.0f;
7565                    case Gravity.RIGHT:
7566                        return (mLayout.getLineRight(0) - (mRight - mLeft) -
7567                                getCompoundPaddingLeft() - getCompoundPaddingRight() -
7568                                mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength();
7569                    case Gravity.CENTER_HORIZONTAL:
7570                        return 0.0f;
7571                }
7572            }
7573        }
7574        return super.getLeftFadingEdgeStrength();
7575    }
7576
7577    @Override
7578    protected float getRightFadingEdgeStrength() {
7579        if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return 0.0f;
7580        if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
7581            if (mMarquee != null && !mMarquee.isStopped()) {
7582                final Marquee marquee = mMarquee;
7583                return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength();
7584            } else if (getLineCount() == 1) {
7585                switch (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
7586                    case Gravity.LEFT:
7587                        final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() -
7588                                getCompoundPaddingRight();
7589                        final float lineWidth = mLayout.getLineWidth(0);
7590                        return (lineWidth - textWidth) / getHorizontalFadingEdgeLength();
7591                    case Gravity.RIGHT:
7592                        return 0.0f;
7593                    case Gravity.CENTER_HORIZONTAL:
7594                    case Gravity.FILL_HORIZONTAL:
7595                        return (mLayout.getLineWidth(0) - ((mRight - mLeft) -
7596                                getCompoundPaddingLeft() - getCompoundPaddingRight())) /
7597                                getHorizontalFadingEdgeLength();
7598                }
7599            }
7600        }
7601        return super.getRightFadingEdgeStrength();
7602    }
7603
7604    @Override
7605    protected int computeHorizontalScrollRange() {
7606        if (mLayout != null) {
7607            return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ?
7608                    (int) mLayout.getLineWidth(0) : mLayout.getWidth();
7609        }
7610
7611        return super.computeHorizontalScrollRange();
7612    }
7613
7614    @Override
7615    protected int computeVerticalScrollRange() {
7616        if (mLayout != null)
7617            return mLayout.getHeight();
7618
7619        return super.computeVerticalScrollRange();
7620    }
7621
7622    @Override
7623    protected int computeVerticalScrollExtent() {
7624        return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom();
7625    }
7626
7627    public enum BufferType {
7628        NORMAL, SPANNABLE, EDITABLE,
7629    }
7630
7631    /**
7632     * Returns the TextView_textColor attribute from the
7633     * Resources.StyledAttributes, if set, or the TextAppearance_textColor
7634     * from the TextView_textAppearance attribute, if TextView_textColor
7635     * was not set directly.
7636     */
7637    public static ColorStateList getTextColors(Context context, TypedArray attrs) {
7638        ColorStateList colors;
7639        colors = attrs.getColorStateList(com.android.internal.R.styleable.
7640                                         TextView_textColor);
7641
7642        if (colors == null) {
7643            int ap = attrs.getResourceId(com.android.internal.R.styleable.
7644                                         TextView_textAppearance, -1);
7645            if (ap != -1) {
7646                TypedArray appearance;
7647                appearance = context.obtainStyledAttributes(ap,
7648                                            com.android.internal.R.styleable.TextAppearance);
7649                colors = appearance.getColorStateList(com.android.internal.R.styleable.
7650                                                  TextAppearance_textColor);
7651                appearance.recycle();
7652            }
7653        }
7654
7655        return colors;
7656    }
7657
7658    /**
7659     * Returns the default color from the TextView_textColor attribute
7660     * from the AttributeSet, if set, or the default color from the
7661     * TextAppearance_textColor from the TextView_textAppearance attribute,
7662     * if TextView_textColor was not set directly.
7663     */
7664    public static int getTextColor(Context context,
7665                                   TypedArray attrs,
7666                                   int def) {
7667        ColorStateList colors = getTextColors(context, attrs);
7668
7669        if (colors == null) {
7670            return def;
7671        } else {
7672            return colors.getDefaultColor();
7673        }
7674    }
7675
7676    @Override
7677    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7678        final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK;
7679        if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) {
7680            switch (keyCode) {
7681            case KeyEvent.KEYCODE_A:
7682                if (canSelectText()) {
7683                    return onTextContextMenuItem(ID_SELECT_ALL);
7684                }
7685                break;
7686            case KeyEvent.KEYCODE_X:
7687                if (canCut()) {
7688                    return onTextContextMenuItem(ID_CUT);
7689                }
7690                break;
7691            case KeyEvent.KEYCODE_C:
7692                if (canCopy()) {
7693                    return onTextContextMenuItem(ID_COPY);
7694                }
7695                break;
7696            case KeyEvent.KEYCODE_V:
7697                if (canPaste()) {
7698                    return onTextContextMenuItem(ID_PASTE);
7699                }
7700                break;
7701            }
7702        }
7703        return super.onKeyShortcut(keyCode, event);
7704    }
7705
7706    /**
7707     * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the
7708     * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have
7709     * a selection controller (see {@link #prepareCursorControllers()}), but this is not sufficient.
7710     */
7711    private boolean canSelectText() {
7712        return hasSelectionController() && mText.length() != 0;
7713    }
7714
7715    /**
7716     * Test based on the <i>intrinsic</i> charateristics of the TextView.
7717     * The text must be spannable and the movement method must allow for arbitary selection.
7718     *
7719     * See also {@link #canSelectText()}.
7720     */
7721    private boolean textCanBeSelected() {
7722        // prepareCursorController() relies on this method.
7723        // If you change this condition, make sure prepareCursorController is called anywhere
7724        // the value of this condition might be changed.
7725        return mText instanceof Spannable && mMovement != null && mMovement.canSelectArbitrarily();
7726    }
7727
7728    private boolean canCut() {
7729        if (hasPasswordTransformationMethod()) {
7730            return false;
7731        }
7732
7733        if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mInput != null) {
7734            return true;
7735        }
7736
7737        return false;
7738    }
7739
7740    private boolean canCopy() {
7741        if (hasPasswordTransformationMethod()) {
7742            return false;
7743        }
7744
7745        if (mText.length() > 0 && hasSelection()) {
7746            return true;
7747        }
7748
7749        return false;
7750    }
7751
7752    private boolean canPaste() {
7753        return (mText instanceof Editable &&
7754                mInput != null &&
7755                getSelectionStart() >= 0 &&
7756                getSelectionEnd() >= 0 &&
7757                ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)).
7758                hasPrimaryClip());
7759    }
7760
7761    private boolean isWordCharacter(int c, int type) {
7762        return (c == '\'' || c == '"' ||
7763                type == Character.UPPERCASE_LETTER ||
7764                type == Character.LOWERCASE_LETTER ||
7765                type == Character.TITLECASE_LETTER ||
7766                type == Character.MODIFIER_LETTER ||
7767                type == Character.OTHER_LETTER || // Should handle asian characters
7768                type == Character.DECIMAL_DIGIT_NUMBER);
7769    }
7770
7771    private static long packRangeInLong(int start, int end) {
7772        return (((long) start) << 32) | end;
7773    }
7774
7775    private static int extractRangeStartFromLong(long range) {
7776        return (int) (range >>> 32);
7777    }
7778
7779    private static int extractRangeEndFromLong(long range) {
7780        return (int) (range & 0x00000000FFFFFFFFL);
7781    }
7782
7783    private boolean selectAll() {
7784        final int length = mText.length();
7785        Selection.setSelection((Spannable) mText, 0, length);
7786        return length > 0;
7787    }
7788
7789    /**
7790     * Adjusts selection to the word under last touch offset.
7791     * Return true if the operation was successfully performed.
7792     */
7793    private boolean selectCurrentWord() {
7794        if (!canSelectText()) {
7795            return false;
7796        }
7797
7798        if (hasPasswordTransformationMethod()) {
7799            // Always select all on a password field.
7800            // Cut/copy menu entries are not available for passwords, but being able to select all
7801            // is however useful to delete or paste to replace the entire content.
7802            return selectAll();
7803        }
7804
7805        int klass = mInputType & InputType.TYPE_MASK_CLASS;
7806        int variation = mInputType & InputType.TYPE_MASK_VARIATION;
7807
7808        // Specific text field types: select the entire text for these
7809        if (klass == InputType.TYPE_CLASS_NUMBER ||
7810                klass == InputType.TYPE_CLASS_PHONE ||
7811                klass == InputType.TYPE_CLASS_DATETIME ||
7812                variation == InputType.TYPE_TEXT_VARIATION_URI ||
7813                variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS ||
7814                variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS ||
7815                variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
7816            return selectAll();
7817        }
7818
7819        long lastTouchOffsets = getLastTouchOffsets();
7820        final int minOffset = extractRangeStartFromLong(lastTouchOffsets);
7821        final int maxOffset = extractRangeEndFromLong(lastTouchOffsets);
7822
7823        int selectionStart, selectionEnd;
7824
7825        // If a URLSpan (web address, email, phone...) is found at that position, select it.
7826        URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class);
7827        if (urlSpans.length == 1) {
7828            URLSpan url = urlSpans[0];
7829            selectionStart = ((Spanned) mText).getSpanStart(url);
7830            selectionEnd = ((Spanned) mText).getSpanEnd(url);
7831        } else {
7832            if (mWordIterator == null) {
7833                mWordIterator = new WordIterator();
7834            }
7835            // WordIerator handles text changes, this is a no-op if text in unchanged.
7836            mWordIterator.setCharSequence(mText);
7837
7838            selectionStart = mWordIterator.getBeginning(minOffset);
7839            if (selectionStart == BreakIterator.DONE) return false;
7840
7841            selectionEnd = mWordIterator.getEnd(maxOffset);
7842            if (selectionEnd == BreakIterator.DONE) return false;
7843        }
7844
7845        Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);
7846        return true;
7847    }
7848
7849    private long getLastTouchOffsets() {
7850        int minOffset, maxOffset;
7851
7852        if (mContextMenuTriggeredByKey) {
7853            minOffset = getSelectionStart();
7854            maxOffset = getSelectionEnd();
7855        } else {
7856            SelectionModifierCursorController selectionController = getSelectionController();
7857            minOffset = selectionController.getMinTouchOffset();
7858            maxOffset = selectionController.getMaxTouchOffset();
7859        }
7860
7861        return packRangeInLong(minOffset, maxOffset);
7862    }
7863
7864    @Override
7865    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
7866        super.onPopulateAccessibilityEvent(event);
7867
7868        final boolean isPassword = hasPasswordTransformationMethod();
7869        if (!isPassword) {
7870            CharSequence text = getText();
7871            if (TextUtils.isEmpty(text)) {
7872                text = getHint();
7873            }
7874            if (!TextUtils.isEmpty(text)) {
7875                event.getText().add(text);
7876            }
7877        }
7878    }
7879
7880    @Override
7881    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
7882        super.onInitializeAccessibilityEvent(event);
7883
7884        final boolean isPassword = hasPasswordTransformationMethod();
7885        event.setPassword(isPassword);
7886    }
7887
7888    void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText,
7889            int fromIndex, int removedCount, int addedCount) {
7890        AccessibilityEvent event =
7891            AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
7892        event.setFromIndex(fromIndex);
7893        event.setRemovedCount(removedCount);
7894        event.setAddedCount(addedCount);
7895        event.setBeforeText(beforeText);
7896        sendAccessibilityEventUnchecked(event);
7897    }
7898
7899    @Override
7900    protected void onCreateContextMenu(ContextMenu menu) {
7901        super.onCreateContextMenu(menu);
7902        boolean added = false;
7903        mContextMenuTriggeredByKey = mDPadCenterIsDown || mEnterKeyIsDown;
7904        // Problem with context menu on long press: the menu appears while the key in down and when
7905        // the key is released, the view does not receive the key_up event.
7906        // We need two layers of flags: mDPadCenterIsDown and mEnterKeyIsDown are set in key down/up
7907        // events. We cannot simply clear these flags in onTextContextMenuItem since
7908        // it may not be called (if the user/ discards the context menu with the back key).
7909        // We clear these flags here and mContextMenuTriggeredByKey saves that state so that it is
7910        // available in onTextContextMenuItem.
7911        mDPadCenterIsDown = mEnterKeyIsDown = false;
7912
7913        MenuHandler handler = new MenuHandler();
7914
7915        if (mText instanceof Spanned && hasSelectionController()) {
7916            long lastTouchOffset = getLastTouchOffsets();
7917            final int selStart = extractRangeStartFromLong(lastTouchOffset);
7918            final int selEnd = extractRangeEndFromLong(lastTouchOffset);
7919
7920            URLSpan[] urls = ((Spanned) mText).getSpans(selStart, selEnd, URLSpan.class);
7921            if (urls.length > 0) {
7922                menu.add(0, ID_COPY_URL, 0, com.android.internal.R.string.copyUrl).
7923                        setOnMenuItemClickListener(handler);
7924
7925                added = true;
7926            }
7927        }
7928
7929        // The context menu is not empty, which will prevent the selection mode from starting.
7930        // Add a entry to start it in the context menu.
7931        // TODO Does not handle the case where a subclass does not call super.thisMethod or
7932        // populates the menu AFTER this call.
7933        if (menu.size() > 0) {
7934            menu.add(0, ID_SELECTION_MODE, 0, com.android.internal.R.string.selectTextMode).
7935                    setOnMenuItemClickListener(handler);
7936            added = true;
7937        }
7938
7939        if (added) {
7940            menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle);
7941        }
7942    }
7943
7944    /**
7945     * Returns whether this text view is a current input method target.  The
7946     * default implementation just checks with {@link InputMethodManager}.
7947     */
7948    public boolean isInputMethodTarget() {
7949        InputMethodManager imm = InputMethodManager.peekInstance();
7950        return imm != null && imm.isActive(this);
7951    }
7952
7953    // Selection context mode
7954    private static final int ID_SELECT_ALL = android.R.id.selectAll;
7955    private static final int ID_CUT = android.R.id.cut;
7956    private static final int ID_COPY = android.R.id.copy;
7957    private static final int ID_PASTE = android.R.id.paste;
7958    // Context menu entries
7959    private static final int ID_COPY_URL = android.R.id.copyUrl;
7960    private static final int ID_SELECTION_MODE = android.R.id.selectTextMode;
7961
7962    private class MenuHandler implements MenuItem.OnMenuItemClickListener {
7963        public boolean onMenuItemClick(MenuItem item) {
7964            return onTextContextMenuItem(item.getItemId());
7965        }
7966    }
7967
7968    /**
7969     * Called when a context menu option for the text view is selected.  Currently
7970     * this will be {@link android.R.id#copyUrl}, {@link android.R.id#selectTextMode},
7971     * {@link android.R.id#selectAll}, {@link android.R.id#paste}, {@link android.R.id#cut}
7972     * or {@link android.R.id#copy}.
7973     *
7974     * @return true if the context menu item action was performed.
7975     */
7976    public boolean onTextContextMenuItem(int id) {
7977        int min = 0;
7978        int max = mText.length();
7979
7980        if (isFocused()) {
7981            final int selStart = getSelectionStart();
7982            final int selEnd = getSelectionEnd();
7983
7984            min = Math.max(0, Math.min(selStart, selEnd));
7985            max = Math.max(0, Math.max(selStart, selEnd));
7986        }
7987
7988        switch (id) {
7989            case ID_COPY_URL:
7990                URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
7991                if (urls.length >= 1) {
7992                    ClipData clip = null;
7993                    for (int i=0; i<urls.length; i++) {
7994                        Uri uri = Uri.parse(urls[0].getURL());
7995                        if (clip == null) {
7996                            clip = ClipData.newRawUri(null, uri);
7997                        } else {
7998                            clip.addItem(new ClipData.Item(uri));
7999                        }
8000                    }
8001                    if (clip != null) {
8002                        setPrimaryClip(clip);
8003                    }
8004                }
8005                stopSelectionActionMode();
8006                return true;
8007
8008            case ID_SELECTION_MODE:
8009                if (mSelectionActionMode != null) {
8010                    // Selection mode is already started, simply change selected part.
8011                    selectCurrentWord();
8012                } else {
8013                    startSelectionActionMode();
8014                }
8015                return true;
8016
8017            case ID_SELECT_ALL:
8018                // This does not enter text selection mode. Text is highlighted, so that it can be
8019                // bulk edited, like selectAllOnFocus does. Returns true even if text is empty.
8020                selectAll();
8021                return true;
8022
8023            case ID_PASTE:
8024                paste(min, max);
8025                return true;
8026
8027            case ID_CUT:
8028                setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max)));
8029                ((Editable) mText).delete(min, max);
8030                stopSelectionActionMode();
8031                return true;
8032
8033            case ID_COPY:
8034                setPrimaryClip(ClipData.newPlainText(null, mTransformed.subSequence(min, max)));
8035                stopSelectionActionMode();
8036                return true;
8037        }
8038        return false;
8039    }
8040
8041    /**
8042     * Prepare text so that there are not zero or two spaces at beginning and end of region defined
8043     * by [min, max] when replacing this region by paste.
8044     * Note that if there were two spaces (or more) at that position before, they are kept. We just
8045     * make sure we do not add an extra one from the paste content.
8046     */
8047    private long prepareSpacesAroundPaste(int min, int max, CharSequence paste) {
8048        if (paste.length() > 0) {
8049            if (min > 0) {
8050                final char charBefore = mTransformed.charAt(min - 1);
8051                final char charAfter = paste.charAt(0);
8052
8053                if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) {
8054                    // Two spaces at beginning of paste: remove one
8055                    final int originalLength = mText.length();
8056                    ((Editable) mText).delete(min - 1, min);
8057                    // Due to filters, there is no guarantee that exactly one character was
8058                    // removed: count instead.
8059                    final int delta = mText.length() - originalLength;
8060                    min += delta;
8061                    max += delta;
8062                } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' &&
8063                        !Character.isSpaceChar(charAfter) && charAfter != '\n') {
8064                    // No space at beginning of paste: add one
8065                    final int originalLength = mText.length();
8066                    ((Editable) mText).replace(min, min, " ");
8067                    // Taking possible filters into account as above.
8068                    final int delta = mText.length() - originalLength;
8069                    min += delta;
8070                    max += delta;
8071                }
8072            }
8073
8074            if (max < mText.length()) {
8075                final char charBefore = paste.charAt(paste.length() - 1);
8076                final char charAfter = mTransformed.charAt(max);
8077
8078                if (Character.isSpaceChar(charBefore) && Character.isSpaceChar(charAfter)) {
8079                    // Two spaces at end of paste: remove one
8080                    ((Editable) mText).delete(max, max + 1);
8081                } else if (!Character.isSpaceChar(charBefore) && charBefore != '\n' &&
8082                        !Character.isSpaceChar(charAfter) && charAfter != '\n') {
8083                    // No space at end of paste: add one
8084                    ((Editable) mText).replace(max, max, " ");
8085                }
8086            }
8087        }
8088
8089        return packRangeInLong(min, max);
8090    }
8091
8092    private DragShadowBuilder getTextThumbnailBuilder(CharSequence text) {
8093        TextView shadowView = (TextView) inflate(mContext,
8094                com.android.internal.R.layout.text_drag_thumbnail, null);
8095
8096        if (shadowView == null) {
8097            throw new IllegalArgumentException("Unable to inflate text drag thumbnail");
8098        }
8099
8100        if (text.length() > DRAG_SHADOW_MAX_TEXT_LENGTH) {
8101            text = text.subSequence(0, DRAG_SHADOW_MAX_TEXT_LENGTH);
8102        }
8103        shadowView.setText(text);
8104        shadowView.setTextColor(getTextColors());
8105
8106        shadowView.setTextAppearance(mContext, R.styleable.Theme_textAppearanceLarge);
8107        shadowView.setGravity(Gravity.CENTER);
8108
8109        shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
8110                ViewGroup.LayoutParams.WRAP_CONTENT));
8111
8112        final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
8113        shadowView.measure(size, size);
8114
8115        shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight());
8116        shadowView.invalidate();
8117        return new DragShadowBuilder(shadowView);
8118    }
8119
8120    private static class DragLocalState {
8121        public TextView sourceTextView;
8122        public int start, end;
8123
8124        public DragLocalState(TextView sourceTextView, int start, int end) {
8125            this.sourceTextView = sourceTextView;
8126            this.start = start;
8127            this.end = end;
8128        }
8129    }
8130
8131    @Override
8132    public boolean performLongClick() {
8133        if (super.performLongClick()) {
8134            mDiscardNextActionUp = true;
8135            return true;
8136        }
8137
8138        boolean handled = false;
8139
8140        // Long press in empty space moves cursor and shows the Paste affordance if available.
8141        if (!isPositionOnText(mLastDownPositionX, mLastDownPositionY) &&
8142                mInsertionControllerEnabled) {
8143            final int offset = getOffset(mLastDownPositionX, mLastDownPositionY);
8144            stopSelectionActionMode();
8145            Selection.setSelection((Spannable) mText, offset);
8146            getInsertionController().showWithPaste();
8147            handled = true;
8148        }
8149
8150        if (!handled && mSelectionActionMode != null) {
8151            if (touchPositionIsInSelection()) {
8152                // Start a drag
8153                final int start = getSelectionStart();
8154                final int end = getSelectionEnd();
8155                CharSequence selectedText = mTransformed.subSequence(start, end);
8156                ClipData data = ClipData.newPlainText(null, selectedText);
8157                DragLocalState localState = new DragLocalState(this, start, end);
8158                startDrag(data, getTextThumbnailBuilder(selectedText), localState, 0);
8159                stopSelectionActionMode();
8160            } else {
8161                selectCurrentWord();
8162            }
8163            handled = true;
8164        }
8165
8166        // Start a new selection
8167        handled |= !handled && startSelectionActionMode();
8168
8169        if (handled) {
8170            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
8171            mDiscardNextActionUp = true;
8172        }
8173
8174        return handled;
8175    }
8176
8177    private boolean touchPositionIsInSelection() {
8178        int selectionStart = getSelectionStart();
8179        int selectionEnd = getSelectionEnd();
8180
8181        if (selectionStart == selectionEnd) {
8182            return false;
8183        }
8184
8185        if (selectionStart > selectionEnd) {
8186            int tmp = selectionStart;
8187            selectionStart = selectionEnd;
8188            selectionEnd = tmp;
8189            Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);
8190        }
8191
8192        SelectionModifierCursorController selectionController = getSelectionController();
8193        int minOffset = selectionController.getMinTouchOffset();
8194        int maxOffset = selectionController.getMaxTouchOffset();
8195
8196        return ((minOffset >= selectionStart) && (maxOffset < selectionEnd));
8197    }
8198
8199    private static class SuggestionRangeSpan extends UnderlineSpan {
8200        // TODO themable, would be nice to make it a child class of TextAppearanceSpan, but
8201        // there is no way to have underline and TextAppearanceSpan.
8202    }
8203
8204    private class SuggestionsPopupWindow implements OnClickListener {
8205        private static final int MAX_NUMBER_SUGGESTIONS = 5;
8206        private static final int NO_SUGGESTIONS = -1;
8207        private final PopupWindow mContainer;
8208        private final ViewGroup[] mSuggestionViews = new ViewGroup[2];
8209        private final int[] mSuggestionViewLayouts = new int[] {
8210                mTextEditSuggestionsBottomWindowLayout, mTextEditSuggestionsTopWindowLayout};
8211        private WordIterator mWordIterator;
8212        private TextAppearanceSpan[] mHighlightSpans = new TextAppearanceSpan[0];
8213
8214        public SuggestionsPopupWindow() {
8215            mContainer = new PopupWindow(TextView.this.mContext, null,
8216                    com.android.internal.R.attr.textSuggestionsWindowStyle);
8217            mContainer.setSplitTouchEnabled(true);
8218            mContainer.setClippingEnabled(false);
8219            mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
8220
8221            mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
8222            mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
8223        }
8224
8225        private class SuggestionInfo {
8226            int suggestionStart, suggestionEnd; // range of suggestion item with replacement text
8227            int spanStart, spanEnd; // range in TextView where text should be inserted
8228        }
8229
8230        private ViewGroup getViewGroup(boolean under) {
8231            final int viewIndex = under ? 0 : 1;
8232            ViewGroup viewGroup = mSuggestionViews[viewIndex];
8233
8234            if (viewGroup == null) {
8235                final int layout = mSuggestionViewLayouts[viewIndex];
8236                LayoutInflater inflater = (LayoutInflater) TextView.this.mContext.
8237                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
8238
8239                if (inflater == null) {
8240                    throw new IllegalArgumentException(
8241                            "Unable to create TextEdit suggestion window inflater");
8242                }
8243
8244                View view = inflater.inflate(layout, null);
8245
8246                if (! (view instanceof ViewGroup)) {
8247                    throw new IllegalArgumentException(
8248                            "Inflated TextEdit suggestion window is not a ViewGroup: " + view);
8249                }
8250
8251                viewGroup = (ViewGroup) view;
8252
8253                // Inflate the suggestion items once and for all.
8254                for (int i = 0; i < MAX_NUMBER_SUGGESTIONS; i++) {
8255                    View childView = inflater.inflate(mTextEditSuggestionItemLayout, viewGroup,
8256                            false);
8257
8258                    if (! (childView instanceof TextView)) {
8259                        throw new IllegalArgumentException(
8260                               "Inflated TextEdit suggestion item is not a TextView: " + childView);
8261                    }
8262
8263                    childView.setTag(new SuggestionInfo());
8264                    viewGroup.addView(childView);
8265                    childView.setOnClickListener(this);
8266                }
8267
8268                mSuggestionViews[viewIndex] = viewGroup;
8269            }
8270
8271            return viewGroup;
8272        }
8273
8274        public void show() {
8275            if (!(mText instanceof Editable)) return;
8276
8277            final int pos = TextView.this.getSelectionStart();
8278            Spannable spannable = (Spannable)TextView.this.mText;
8279            SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class);
8280            final int nbSpans = suggestionSpans.length;
8281
8282            ViewGroup viewGroup = getViewGroup(true);
8283            mContainer.setContentView(viewGroup);
8284
8285            int totalNbSuggestions = 0;
8286            int spanUnionStart = mText.length();
8287            int spanUnionEnd = 0;
8288
8289            for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) {
8290                SuggestionSpan suggestionSpan = suggestionSpans[spanIndex];
8291                final int spanStart = spannable.getSpanStart(suggestionSpan);
8292                final int spanEnd = spannable.getSpanEnd(suggestionSpan);
8293                spanUnionStart = Math.min(spanStart, spanUnionStart);
8294                spanUnionEnd = Math.max(spanEnd, spanUnionEnd);
8295
8296                String[] suggestions = suggestionSpan.getSuggestions();
8297                int nbSuggestions = suggestions.length;
8298                for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
8299                    TextView textView = (TextView) viewGroup.getChildAt(totalNbSuggestions);
8300                    textView.setText(suggestions[suggestionIndex]);
8301                    SuggestionInfo suggestionInfo = (SuggestionInfo) textView.getTag();
8302                    suggestionInfo.spanStart = spanStart;
8303                    suggestionInfo.spanEnd = spanEnd;
8304
8305                    totalNbSuggestions++;
8306                    if (totalNbSuggestions == MAX_NUMBER_SUGGESTIONS) {
8307                        // Also end outer for loop
8308                        spanIndex = nbSpans;
8309                        break;
8310                    }
8311                }
8312            }
8313
8314            if (totalNbSuggestions == 0) {
8315                // TODO Replace by final text, use a dedicated layout, add a fade out timer...
8316                TextView textView = (TextView) viewGroup.getChildAt(0);
8317                textView.setText("No suggestions available");
8318                SuggestionInfo suggestionInfo = (SuggestionInfo) textView.getTag();
8319                suggestionInfo.spanStart = NO_SUGGESTIONS;
8320                totalNbSuggestions++;
8321            } else {
8322                if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan();
8323                ((Editable) mText).setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd,
8324                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8325
8326                for (int i = 0; i < totalNbSuggestions; i++) {
8327                    final TextView textView = (TextView) viewGroup.getChildAt(i);
8328                    highlightTextDifferences(textView, spanUnionStart, spanUnionEnd);
8329                }
8330            }
8331
8332            for (int i = 0; i < MAX_NUMBER_SUGGESTIONS; i++) {
8333                viewGroup.getChildAt(i).setVisibility(i < totalNbSuggestions ? VISIBLE : GONE);
8334            }
8335
8336            final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
8337            viewGroup.measure(size, size);
8338
8339            positionAtCursor();
8340        }
8341
8342        private long[] getWordLimits(CharSequence text) {
8343            if (mWordIterator == null) mWordIterator = new WordIterator(); // TODO locale
8344            mWordIterator.setCharSequence(text);
8345
8346            // First pass will simply count the number of words to be able to create an array
8347            // Not too expensive since previous break positions are cached by the BreakIterator
8348            int nbWords = 0;
8349            int position = mWordIterator.following(0);
8350            while (position != BreakIterator.DONE) {
8351                nbWords++;
8352                position = mWordIterator.following(position);
8353            }
8354
8355            int index = 0;
8356            long[] result = new long[nbWords];
8357
8358            position = mWordIterator.following(0);
8359            while (position != BreakIterator.DONE) {
8360                int wordStart = mWordIterator.getBeginning(position);
8361                result[index++] = packRangeInLong(wordStart, position);
8362                position = mWordIterator.following(position);
8363            }
8364
8365            return result;
8366        }
8367
8368        private TextAppearanceSpan highlightSpan(int index) {
8369            final int length = mHighlightSpans.length;
8370            if (index < length) {
8371                return mHighlightSpans[index];
8372            }
8373
8374            // Assumes indexes are requested in sequence: simply append one more item
8375            TextAppearanceSpan[] newArray = new TextAppearanceSpan[length + 1];
8376            System.arraycopy(mHighlightSpans, 0, newArray, 0, length);
8377            TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mContext,
8378                    android.R.style.TextAppearance_SuggestionHighlight);
8379            newArray[length] = highlightSpan;
8380            mHighlightSpans = newArray;
8381            return highlightSpan;
8382        }
8383
8384        private void highlightTextDifferences(TextView textView, int unionStart, int unionEnd) {
8385            SuggestionInfo suggestionInfo = (SuggestionInfo) textView.getTag();
8386            final int spanStart = suggestionInfo.spanStart;
8387            final int spanEnd = suggestionInfo.spanEnd;
8388
8389            // Remove all text formating by converting to Strings
8390            final String text = textView.getText().toString();
8391            final String sourceText = mText.subSequence(spanStart, spanEnd).toString();
8392
8393            long[] sourceWordLimits = getWordLimits(sourceText);
8394            long[] wordLimits = getWordLimits(text);
8395
8396            SpannableStringBuilder ssb = new SpannableStringBuilder();
8397            // span [spanStart, spanEnd] is included in union [spanUnionStart, int spanUnionEnd]
8398            // The final result is made of 3 parts: the text before, between and after the span
8399            // This is the text before, provided for context
8400            ssb.append(mText.subSequence(unionStart, spanStart).toString());
8401
8402            // shift is used to offset spans positions wrt span's beginning
8403            final int shift = spanStart - unionStart;
8404            suggestionInfo.suggestionStart = shift;
8405            suggestionInfo.suggestionEnd = shift + text.length();
8406
8407            // This is the actual suggestion text, which will be highlighted by the following code
8408            ssb.append(text);
8409
8410            String[] words = new String[wordLimits.length];
8411            for (int i = 0; i < wordLimits.length; i++) {
8412                int wordStart = extractRangeStartFromLong(wordLimits[i]);
8413                int wordEnd = extractRangeEndFromLong(wordLimits[i]);
8414                words[i] = text.substring(wordStart, wordEnd);
8415            }
8416
8417            // Highlighted word algorithm is bases on word matching between source and text
8418            // Matching words are found from left to right. TODO: change for RTL languages
8419            // Characters between matching words are highlighted
8420            int previousCommonWordIndex = -1;
8421            int nbHighlightSpans = 0;
8422            for (int i = 0; i < sourceWordLimits.length; i++) {
8423                int wordStart = extractRangeStartFromLong(sourceWordLimits[i]);
8424                int wordEnd = extractRangeEndFromLong(sourceWordLimits[i]);
8425                String sourceWord = sourceText.substring(wordStart, wordEnd);
8426
8427                for (int j = previousCommonWordIndex + 1; j < words.length; j++) {
8428                    if (sourceWord.equals(words[j])) {
8429                        if (j != previousCommonWordIndex + 1) {
8430                            int firstDifferentPosition = previousCommonWordIndex < 0 ? 0 :
8431                                extractRangeEndFromLong(wordLimits[previousCommonWordIndex]);
8432                            int lastDifferentPosition = extractRangeStartFromLong(wordLimits[j]);
8433                            ssb.setSpan(highlightSpan(nbHighlightSpans++),
8434                                    shift + firstDifferentPosition, shift + lastDifferentPosition,
8435                                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8436                        } else {
8437                            // Compare characters between words
8438                            int previousSourceWordEnd = i == 0 ? 0 :
8439                                extractRangeEndFromLong(sourceWordLimits[i - 1]);
8440                            int sourceWordStart = extractRangeStartFromLong(sourceWordLimits[i]);
8441                            String sourceSpaces = sourceText.substring(previousSourceWordEnd,
8442                                    sourceWordStart);
8443
8444                            int previousWordEnd = j == 0 ? 0 :
8445                                extractRangeEndFromLong(wordLimits[j - 1]);
8446                            int currentWordStart = extractRangeStartFromLong(wordLimits[j]);
8447                            String textSpaces = text.substring(previousWordEnd, currentWordStart);
8448
8449                            if (!sourceSpaces.equals(textSpaces)) {
8450                                ssb.setSpan(highlightSpan(nbHighlightSpans++),
8451                                        shift + previousWordEnd, shift + currentWordStart,
8452                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8453                            }
8454                        }
8455                        previousCommonWordIndex = j;
8456                        break;
8457                    }
8458                }
8459            }
8460
8461            // Finally, compare ends of Strings
8462            if (previousCommonWordIndex < words.length - 1) {
8463                int firstDifferentPosition = previousCommonWordIndex < 0 ? 0 :
8464                    extractRangeEndFromLong(wordLimits[previousCommonWordIndex]);
8465                int lastDifferentPosition = textView.length();
8466                ssb.setSpan(highlightSpan(nbHighlightSpans++),
8467                        shift + firstDifferentPosition, shift + lastDifferentPosition,
8468                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8469            } else {
8470                int lastSourceWordEnd = sourceWordLimits.length == 0 ? 0 :
8471                    extractRangeEndFromLong(sourceWordLimits[sourceWordLimits.length - 1]);
8472                String sourceSpaces = sourceText.substring(lastSourceWordEnd, sourceText.length());
8473
8474                int lastCommonTextWordEnd = previousCommonWordIndex < 0 ? 0 :
8475                    extractRangeEndFromLong(wordLimits[previousCommonWordIndex]);
8476                String textSpaces = text.substring(lastCommonTextWordEnd, textView.length());
8477
8478                if (!sourceSpaces.equals(textSpaces) && textSpaces.length() > 0) {
8479                    ssb.setSpan(highlightSpan(nbHighlightSpans++),
8480                            shift + lastCommonTextWordEnd, shift + textView.length(),
8481                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
8482                }
8483            }
8484
8485            // Final part, text after the current suggestion range.
8486            ssb.append(mText.subSequence(spanEnd, unionEnd).toString());
8487            textView.setText(ssb);
8488        }
8489
8490        public void hide() {
8491            if ((mText instanceof Editable) && mSuggestionRangeSpan != null) {
8492                ((Editable) mText).removeSpan(mSuggestionRangeSpan);
8493            }
8494            mContainer.dismiss();
8495        }
8496
8497        @Override
8498        public void onClick(View view) {
8499            if (view instanceof TextView) {
8500                TextView textView = (TextView) view;
8501                SuggestionInfo suggestionInfo = (SuggestionInfo) textView.getTag();
8502                final int spanStart = suggestionInfo.spanStart;
8503                final int spanEnd = suggestionInfo.spanEnd;
8504                if (spanStart != NO_SUGGESTIONS) {
8505                    final int suggestionStart = suggestionInfo.suggestionStart;
8506                    final int suggestionEnd = suggestionInfo.suggestionEnd;
8507                    final String suggestion = textView.getText().subSequence(
8508                            suggestionStart, suggestionEnd).toString();
8509                    ((Editable) mText).replace(spanStart, spanEnd, suggestion);
8510                }
8511            }
8512            hide();
8513        }
8514
8515        void positionAtCursor() {
8516            View contentView = mContainer.getContentView();
8517            int width = contentView.getMeasuredWidth();
8518            int height = contentView.getMeasuredHeight();
8519            final int offset = TextView.this.getSelectionStart();
8520            final int line = mLayout.getLineForOffset(offset);
8521            final int lineBottom = mLayout.getLineBottom(line);
8522            float primaryHorizontal = mLayout.getPrimaryHorizontal(offset);
8523
8524            final Rect bounds = sCursorControllerTempRect;
8525            bounds.left = (int) (primaryHorizontal - width / 2.0f);
8526            bounds.top = lineBottom;
8527
8528            bounds.right = bounds.left + width;
8529            bounds.bottom = bounds.top + height;
8530
8531            convertFromViewportToContentCoordinates(bounds);
8532
8533            final int[] coords = mTempCoords;
8534            TextView.this.getLocationInWindow(coords);
8535            coords[0] += bounds.left;
8536            coords[1] += bounds.top;
8537
8538            final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
8539            final int screenHeight = displayMetrics.heightPixels;
8540
8541            // Vertical clipping
8542            if (coords[1] + height > screenHeight) {
8543                // Try to position above current line instead
8544                // TODO use top layout instead, reverse suggestion order,
8545                // try full screen vertical down if it still does not fit. TBD with designers.
8546
8547                // Update dimensions from new view
8548                contentView = mContainer.getContentView();
8549                width = contentView.getMeasuredWidth();
8550                height = contentView.getMeasuredHeight();
8551
8552                final int lineTop = mLayout.getLineTop(line);
8553                final int lineHeight = lineBottom - lineTop;
8554                coords[1] -= height + lineHeight;
8555            }
8556
8557            // Horizontal clipping
8558            coords[0] = Math.max(0, coords[0]);
8559            coords[0] = Math.min(displayMetrics.widthPixels - width, coords[0]);
8560
8561            mContainer.showAtLocation(TextView.this, Gravity.NO_GRAVITY, coords[0], coords[1]);
8562        }
8563    }
8564
8565    void showSuggestions() {
8566        if (mSuggestionsPopupWindow == null) {
8567            mSuggestionsPopupWindow = new SuggestionsPopupWindow();
8568        }
8569        hideControllers();
8570        mSuggestionsPopupWindow.show();
8571    }
8572
8573    void hideSuggestions() {
8574        if (mSuggestionsPopupWindow != null) {
8575            mSuggestionsPopupWindow.hide();
8576        }
8577    }
8578
8579    /**
8580     * If provided, this ActionMode.Callback will be used to create the ActionMode when text
8581     * selection is initiated in this View.
8582     *
8583     * The standard implementation populates the menu with a subset of Select All, Cut, Copy and
8584     * Paste actions, depending on what this View supports.
8585     *
8586     * A custom implementation can add new entries in the default menu in its
8587     * {@link ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The default actions
8588     * can also be removed from the menu using {@link Menu#removeItem(int)} and passing
8589     * {@link android.R.id#selectAll}, {@link android.R.id#cut}, {@link android.R.id#copy} or
8590     * {@link android.R.id#paste} ids as parameters.
8591     *
8592     * Returning false from {@link ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will
8593     * prevent the action mode from being started.
8594     *
8595     * Action click events should be handled by the custom implementation of
8596     * {@link ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}.
8597     *
8598     * Note that text selection mode is not started when a TextView receives focus and the
8599     * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in
8600     * that case, to allow for quick replacement.
8601     */
8602    public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {
8603        mCustomSelectionActionModeCallback = actionModeCallback;
8604    }
8605
8606    /**
8607     * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null.
8608     *
8609     * @return The current custom selection callback.
8610     */
8611    public ActionMode.Callback getCustomSelectionActionModeCallback() {
8612        return mCustomSelectionActionModeCallback;
8613    }
8614
8615    /**
8616     *
8617     * @return true if the selection mode was actually started.
8618     */
8619    private boolean startSelectionActionMode() {
8620        if (mSelectionActionMode != null) {
8621            // Selection action mode is already started
8622            return false;
8623        }
8624
8625        if (!canSelectText() || !requestFocus()) {
8626            Log.w(LOG_TAG, "TextView does not support text selection. Action mode cancelled.");
8627            return false;
8628        }
8629
8630        boolean currentWordSelected = selectCurrentWord();
8631        if (!currentWordSelected) {
8632            // No word found under cursor or text selection not permitted.
8633            return false;
8634        }
8635
8636        ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
8637        mSelectionActionMode = startActionMode(actionModeCallback);
8638        final boolean selectionStarted = mSelectionActionMode != null;
8639
8640        if (selectionStarted && !mTextIsSelectable) {
8641            // Show the IME to be able to replace text, except when selecting non editable text.
8642            final InputMethodManager imm = InputMethodManager.peekInstance();
8643            if (imm != null) imm.showSoftInput(this, 0, null);
8644        }
8645
8646        return selectionStarted;
8647    }
8648
8649    private void stopSelectionActionMode() {
8650        if (mSelectionActionMode != null) {
8651            // This will hide the mSelectionModifierCursorController
8652            mSelectionActionMode.finish();
8653        }
8654    }
8655
8656    /**
8657     * Paste clipboard content between min and max positions.
8658     */
8659    private void paste(int min, int max) {
8660        ClipboardManager clipboard =
8661            (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
8662        ClipData clip = clipboard.getPrimaryClip();
8663        if (clip != null) {
8664            boolean didFirst = false;
8665            for (int i=0; i<clip.getItemCount(); i++) {
8666                CharSequence paste = clip.getItemAt(i).coerceToText(getContext());
8667                if (paste != null) {
8668                    if (!didFirst) {
8669                        long minMax = prepareSpacesAroundPaste(min, max, paste);
8670                        min = extractRangeStartFromLong(minMax);
8671                        max = extractRangeEndFromLong(minMax);
8672                        Selection.setSelection((Spannable) mText, max);
8673                        ((Editable) mText).replace(min, max, paste);
8674                        didFirst = true;
8675                    } else {
8676                        ((Editable) mText).insert(getSelectionEnd(), "\n");
8677                        ((Editable) mText).insert(getSelectionEnd(), paste);
8678                    }
8679                }
8680            }
8681            stopSelectionActionMode();
8682            sLastCutOrCopyTime = 0;
8683        }
8684    }
8685
8686    private void setPrimaryClip(ClipData clip) {
8687        ClipboardManager clipboard = (ClipboardManager) getContext().
8688                getSystemService(Context.CLIPBOARD_SERVICE);
8689        clipboard.setPrimaryClip(clip);
8690        sLastCutOrCopyTime = SystemClock.uptimeMillis();
8691    }
8692
8693    /**
8694     * An ActionMode Callback class that is used to provide actions while in text selection mode.
8695     *
8696     * The default callback provides a subset of Select All, Cut, Copy and Paste actions, depending
8697     * on which of these this TextView supports.
8698     */
8699    private class SelectionActionModeCallback implements ActionMode.Callback {
8700
8701        @Override
8702        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
8703            TypedArray styledAttributes = mContext.obtainStyledAttributes(R.styleable.Theme);
8704
8705            mode.setTitle(mContext.getString(com.android.internal.R.string.textSelectionCABTitle));
8706            mode.setSubtitle(null);
8707
8708            menu.add(0, ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll).
8709                    setAlphabeticShortcut('a').
8710                    setShowAsAction(
8711                            MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
8712
8713            if (canCut()) {
8714                menu.add(0, ID_CUT, 0, com.android.internal.R.string.cut).
8715                    setIcon(styledAttributes.getResourceId(
8716                            R.styleable.Theme_actionModeCutDrawable, 0)).
8717                    setAlphabeticShortcut('x').
8718                    setShowAsAction(
8719                            MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
8720            }
8721
8722            if (canCopy()) {
8723                menu.add(0, ID_COPY, 0, com.android.internal.R.string.copy).
8724                    setIcon(styledAttributes.getResourceId(
8725                            R.styleable.Theme_actionModeCopyDrawable, 0)).
8726                    setAlphabeticShortcut('c').
8727                    setShowAsAction(
8728                            MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
8729            }
8730
8731            if (canPaste()) {
8732                menu.add(0, ID_PASTE, 0, com.android.internal.R.string.paste).
8733                        setIcon(styledAttributes.getResourceId(
8734                                R.styleable.Theme_actionModePasteDrawable, 0)).
8735                        setAlphabeticShortcut('v').
8736                        setShowAsAction(
8737                                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
8738            }
8739
8740            styledAttributes.recycle();
8741
8742            if (mCustomSelectionActionModeCallback != null) {
8743                if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) {
8744                    // The custom mode can choose to cancel the action mode
8745                    return false;
8746                }
8747            }
8748
8749            if (menu.hasVisibleItems() || mode.getCustomView() != null) {
8750                getSelectionController().show();
8751                return true;
8752            } else {
8753                return false;
8754            }
8755        }
8756
8757        @Override
8758        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
8759            if (mCustomSelectionActionModeCallback != null) {
8760                return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu);
8761            }
8762            return true;
8763        }
8764
8765        @Override
8766        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
8767            if (mCustomSelectionActionModeCallback != null &&
8768                 mCustomSelectionActionModeCallback.onActionItemClicked(mode, item)) {
8769                return true;
8770            }
8771            return onTextContextMenuItem(item.getItemId());
8772        }
8773
8774        @Override
8775        public void onDestroyActionMode(ActionMode mode) {
8776            if (mCustomSelectionActionModeCallback != null) {
8777                mCustomSelectionActionModeCallback.onDestroyActionMode(mode);
8778            }
8779            Selection.setSelection((Spannable) mText, getSelectionEnd());
8780
8781            if (mSelectionModifierCursorController != null) {
8782                mSelectionModifierCursorController.hide();
8783            }
8784
8785            mSelectionActionMode = null;
8786        }
8787    }
8788
8789    private class PastePopupWindow implements OnClickListener {
8790        private final PopupWindow mContainer;
8791        private final View[] mPasteViews = new View[4];
8792        private final int[] mPasteViewLayouts = new int[] {
8793                mTextEditPasteWindowLayout,  mTextEditNoPasteWindowLayout,
8794                mTextEditSidePasteWindowLayout, mTextEditSideNoPasteWindowLayout };
8795
8796        public PastePopupWindow() {
8797            mContainer = new PopupWindow(TextView.this.mContext, null,
8798                    com.android.internal.R.attr.textSelectHandleWindowStyle);
8799            mContainer.setSplitTouchEnabled(true);
8800            mContainer.setClippingEnabled(false);
8801            mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
8802
8803            mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
8804            mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
8805        }
8806
8807        private int viewIndex(boolean onTop) {
8808            return (onTop ? 0 : 1<<1) + (canPaste() ? 0 : 1<<0);
8809        }
8810
8811        private void updateContent(boolean onTop) {
8812            final int viewIndex = viewIndex(onTop);
8813            View view = mPasteViews[viewIndex];
8814
8815            if (view == null) {
8816                final int layout = mPasteViewLayouts[viewIndex];
8817                LayoutInflater inflater = (LayoutInflater)TextView.this.mContext.
8818                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
8819                if (inflater != null) {
8820                    view = inflater.inflate(layout, null);
8821                }
8822
8823                if (view == null) {
8824                    throw new IllegalArgumentException("Unable to inflate TextEdit paste window");
8825                }
8826
8827                final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
8828                view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
8829                        ViewGroup.LayoutParams.WRAP_CONTENT));
8830                view.measure(size, size);
8831
8832                view.setOnClickListener(this);
8833
8834                mPasteViews[viewIndex] = view;
8835            }
8836
8837            mContainer.setContentView(view);
8838        }
8839
8840        public void show() {
8841            updateContent(true);
8842            positionAtCursor();
8843        }
8844
8845        public void hide() {
8846            mContainer.dismiss();
8847        }
8848
8849        public boolean isShowing() {
8850            return mContainer.isShowing();
8851        }
8852
8853        @Override
8854        public void onClick(View v) {
8855            if (canPaste()) {
8856                paste(getSelectionStart(), getSelectionEnd());
8857            }
8858            hide();
8859        }
8860
8861        void positionAtCursor() {
8862            View contentView = mContainer.getContentView();
8863            int width = contentView.getMeasuredWidth();
8864            int height = contentView.getMeasuredHeight();
8865            final int offset = TextView.this.getSelectionStart();
8866            final int line = mLayout.getLineForOffset(offset);
8867            final int lineTop = mLayout.getLineTop(line);
8868            float primaryHorizontal = mLayout.getPrimaryHorizontal(offset);
8869
8870            final Rect bounds = sCursorControllerTempRect;
8871            bounds.left = (int) (primaryHorizontal - width / 2.0f);
8872            bounds.top = lineTop - height;
8873
8874            bounds.right = bounds.left + width;
8875            bounds.bottom = bounds.top + height;
8876
8877            convertFromViewportToContentCoordinates(bounds);
8878
8879            final int[] coords = mTempCoords;
8880            TextView.this.getLocationInWindow(coords);
8881            coords[0] += bounds.left;
8882            coords[1] += bounds.top;
8883
8884            final int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
8885            if (coords[1] < 0) {
8886                updateContent(false);
8887                // Update dimensions from new view
8888                contentView = mContainer.getContentView();
8889                width = contentView.getMeasuredWidth();
8890                height = contentView.getMeasuredHeight();
8891
8892                // Vertical clipping, move under edited line and to the side of insertion cursor
8893                // TODO bottom clipping in case there is no system bar
8894                coords[1] += height;
8895                final int lineBottom = mLayout.getLineBottom(line);
8896                final int lineHeight = lineBottom - lineTop;
8897                coords[1] += lineHeight;
8898
8899                // Move to right hand side of insertion cursor by default. TODO RTL text.
8900                final Drawable handle = mContext.getResources().getDrawable(mTextSelectHandleRes);
8901                final int handleHalfWidth = handle.getIntrinsicWidth() / 2;
8902
8903                if (primaryHorizontal + handleHalfWidth + width < screenWidth) {
8904                    coords[0] += handleHalfWidth + width / 2;
8905                } else {
8906                    coords[0] -= handleHalfWidth + width / 2;
8907                }
8908            } else {
8909                // Horizontal clipping
8910                coords[0] = Math.max(0, coords[0]);
8911                coords[0] = Math.min(screenWidth - width, coords[0]);
8912            }
8913
8914            mContainer.showAtLocation(TextView.this, Gravity.NO_GRAVITY, coords[0], coords[1]);
8915        }
8916    }
8917
8918    private abstract class HandleView extends View implements ViewTreeObserver.OnPreDrawListener {
8919        protected Drawable mDrawable;
8920        private final PopupWindow mContainer;
8921        // Position with respect to the parent TextView
8922        private int mPositionX, mPositionY;
8923        private boolean mIsDragging;
8924        // Offset from touch position to mPosition
8925        private float mTouchToWindowOffsetX, mTouchToWindowOffsetY;
8926        protected float mHotspotX;
8927        // Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up
8928        private float mTouchOffsetY;
8929        // Where the touch position should be on the handle to ensure a maximum cursor visibility
8930        private float mIdealVerticalOffset;
8931        // Parent's (TextView) previous position in window
8932        private int mLastParentX, mLastParentY;
8933        // PopupWindow container absolute position with respect to the enclosing window
8934        private int mContainerPositionX, mContainerPositionY;
8935        // Visible or not (scrolled off screen), whether or not this handle should be visible
8936        private boolean mIsActive = false;
8937
8938        public HandleView() {
8939            super(TextView.this.mContext);
8940            mContainer = new PopupWindow(TextView.this.mContext, null,
8941                    com.android.internal.R.attr.textSelectHandleWindowStyle);
8942            mContainer.setSplitTouchEnabled(true);
8943            mContainer.setClippingEnabled(false);
8944            mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
8945            mContainer.setContentView(this);
8946
8947            initDrawable();
8948
8949            final int handleHeight = mDrawable.getIntrinsicHeight();
8950            mTouchOffsetY = -0.3f * handleHeight;
8951            mIdealVerticalOffset = 0.7f * handleHeight;
8952        }
8953
8954        protected abstract void initDrawable();
8955
8956        // Touch-up filter: number of previous positions remembered
8957        private static final int HISTORY_SIZE = 5;
8958        private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150;
8959        private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350;
8960        private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE];
8961        private final int[] mPreviousOffsets = new int[HISTORY_SIZE];
8962        private int mPreviousOffsetIndex = 0;
8963        private int mNumberPreviousOffsets = 0;
8964
8965        private void startTouchUpFilter(int offset) {
8966            mNumberPreviousOffsets = 0;
8967            addPositionToTouchUpFilter(offset);
8968        }
8969
8970        private void addPositionToTouchUpFilter(int offset) {
8971            if (mNumberPreviousOffsets > 0 &&
8972                    mPreviousOffsets[mPreviousOffsetIndex] == offset) {
8973                // Make sure only actual changes of position are recorded.
8974                return;
8975            }
8976
8977            mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE;
8978            mPreviousOffsets[mPreviousOffsetIndex] = offset;
8979            mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis();
8980            mNumberPreviousOffsets++;
8981        }
8982
8983        private void filterOnTouchUp() {
8984            final long now = SystemClock.uptimeMillis();
8985            int i = 0;
8986            int index = mPreviousOffsetIndex;
8987            final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE);
8988            while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) {
8989                i++;
8990                index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE;
8991            }
8992
8993            if (i > 0 && i < iMax &&
8994                    (now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) {
8995                updateOffset(mPreviousOffsets[index]);
8996            }
8997        }
8998
8999        @Override
9000        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
9001            setMeasuredDimension(mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
9002        }
9003
9004        public void show() {
9005            if (isShowing()) {
9006                mContainer.update(mContainerPositionX, mContainerPositionY,
9007                        mRight - mLeft, mBottom - mTop);
9008            } else {
9009                mContainer.showAtLocation(TextView.this, 0,
9010                        mContainerPositionX, mContainerPositionY);
9011
9012                mIsActive = true;
9013
9014                ViewTreeObserver vto = TextView.this.getViewTreeObserver();
9015                vto.addOnPreDrawListener(this);
9016            }
9017        }
9018
9019        protected void dismiss() {
9020            mIsDragging = false;
9021            mContainer.dismiss();
9022        }
9023
9024        public void hide() {
9025            dismiss();
9026
9027            mIsActive = false;
9028
9029            ViewTreeObserver vto = TextView.this.getViewTreeObserver();
9030            vto.removeOnPreDrawListener(this);
9031        }
9032
9033        public boolean isShowing() {
9034            return mContainer.isShowing();
9035        }
9036
9037        private boolean isPositionVisible() {
9038            // Always show a dragging handle.
9039            if (mIsDragging) {
9040                return true;
9041            }
9042
9043            if (isInBatchEditMode()) {
9044                return false;
9045            }
9046
9047            final int extendedPaddingTop = getExtendedPaddingTop();
9048            final int extendedPaddingBottom = getExtendedPaddingBottom();
9049            final int compoundPaddingLeft = getCompoundPaddingLeft();
9050            final int compoundPaddingRight = getCompoundPaddingRight();
9051
9052            final TextView textView = TextView.this;
9053
9054            if (mTempRect == null) mTempRect = new Rect();
9055            final Rect clip = mTempRect;
9056            clip.left = compoundPaddingLeft;
9057            clip.top = extendedPaddingTop;
9058            clip.right = textView.getWidth() - compoundPaddingRight;
9059            clip.bottom = textView.getHeight() - extendedPaddingBottom;
9060
9061            final ViewParent parent = textView.getParent();
9062            if (parent == null || !parent.getChildVisibleRect(textView, clip, null)) {
9063                return false;
9064            }
9065
9066            final int[] coords = mTempCoords;
9067            textView.getLocationInWindow(coords);
9068            final int posX = coords[0] + mPositionX + (int) mHotspotX;
9069            final int posY = coords[1] + mPositionY;
9070
9071            // Offset by 1 to take into account 0.5 and int rounding around getPrimaryHorizontal.
9072            return posX >= clip.left - 1 && posX <= clip.right + 1 &&
9073                    posY >= clip.top && posY <= clip.bottom;
9074        }
9075
9076        public abstract int getCurrentCursorOffset();
9077
9078        public abstract void updateOffset(int offset);
9079
9080        public abstract void updatePosition(int x, int y);
9081
9082        protected void positionAtCursorOffset(int offset) {
9083            addPositionToTouchUpFilter(offset);
9084            final int line = mLayout.getLineForOffset(offset);
9085            final int lineBottom = mLayout.getLineBottom(line);
9086
9087            mPositionX = (int) (mLayout.getPrimaryHorizontal(offset) - 0.5f - mHotspotX);
9088            mPositionY = lineBottom;
9089
9090            // Take TextView's padding into account.
9091            mPositionX += viewportToContentHorizontalOffset();
9092            mPositionY += viewportToContentVerticalOffset();
9093        }
9094
9095        protected boolean updateContainerPosition() {
9096            positionAtCursorOffset(getCurrentCursorOffset());
9097
9098            final int previousContainerPositionX = mContainerPositionX;
9099            final int previousContainerPositionY = mContainerPositionY;
9100
9101            TextView.this.getLocationInWindow(mTempCoords);
9102            mContainerPositionX = mTempCoords[0] + mPositionX;
9103            mContainerPositionY = mTempCoords[1] + mPositionY;
9104
9105            return (previousContainerPositionX != mContainerPositionX ||
9106                    previousContainerPositionY != mContainerPositionY);
9107        }
9108
9109        public boolean onPreDraw() {
9110            if (updateContainerPosition()) {
9111                if (mIsDragging) {
9112                    if (mTempCoords[0] != mLastParentX || mTempCoords[1] != mLastParentY) {
9113                        mTouchToWindowOffsetX += mTempCoords[0] - mLastParentX;
9114                        mTouchToWindowOffsetY += mTempCoords[1] - mLastParentY;
9115                        mLastParentX = mTempCoords[0];
9116                        mLastParentY = mTempCoords[1];
9117                    }
9118                }
9119
9120                onHandleMoved();
9121
9122                if (isPositionVisible()) {
9123                    mContainer.update(mContainerPositionX, mContainerPositionY,
9124                            mRight - mLeft, mBottom - mTop);
9125
9126                    if (mIsActive && !isShowing()) {
9127                        show();
9128                    }
9129                } else {
9130                    if (isShowing()) {
9131                        dismiss();
9132                    }
9133                }
9134            }
9135            return true;
9136        }
9137
9138        @Override
9139        protected void onDraw(Canvas c) {
9140            mDrawable.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9141            mDrawable.draw(c);
9142        }
9143
9144        @Override
9145        public boolean onTouchEvent(MotionEvent ev) {
9146            switch (ev.getActionMasked()) {
9147                case MotionEvent.ACTION_DOWN: {
9148                    startTouchUpFilter(getCurrentCursorOffset());
9149                    mTouchToWindowOffsetX = ev.getRawX() - mPositionX;
9150                    mTouchToWindowOffsetY = ev.getRawY() - mPositionY;
9151
9152                    final int[] coords = mTempCoords;
9153                    TextView.this.getLocationInWindow(coords);
9154                    mLastParentX = coords[0];
9155                    mLastParentY = coords[1];
9156                    mIsDragging = true;
9157                    break;
9158                }
9159
9160                case MotionEvent.ACTION_MOVE: {
9161                    final float rawX = ev.getRawX();
9162                    final float rawY = ev.getRawY();
9163
9164                    // Vertical hysteresis: vertical down movement tends to snap to ideal offset
9165                    final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY;
9166                    final float currentVerticalOffset = rawY - mPositionY - mLastParentY;
9167                    float newVerticalOffset;
9168                    if (previousVerticalOffset < mIdealVerticalOffset) {
9169                        newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset);
9170                        newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset);
9171                    } else {
9172                        newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset);
9173                        newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset);
9174                    }
9175                    mTouchToWindowOffsetY = newVerticalOffset + mLastParentY;
9176
9177                    final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX;
9178                    final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY;
9179
9180                    updatePosition(Math.round(newPosX), Math.round(newPosY));
9181                    break;
9182                }
9183
9184                case MotionEvent.ACTION_UP:
9185                    filterOnTouchUp();
9186                    mIsDragging = false;
9187                    break;
9188
9189                case MotionEvent.ACTION_CANCEL:
9190                    mIsDragging = false;
9191                    break;
9192            }
9193            return true;
9194        }
9195
9196        public boolean isDragging() {
9197            return mIsDragging;
9198        }
9199
9200        void onHandleMoved() {
9201            // Does nothing by default
9202        }
9203
9204        public void onDetached() {
9205            // Should be overriden to clean possible Runnable
9206        }
9207    }
9208
9209    private class InsertionHandleView extends HandleView {
9210        private static final int DELAY_BEFORE_FADE_OUT = 4000;
9211        private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds
9212
9213        // Used to detect taps on the insertion handle, which will affect the PastePopupWindow
9214        private long mTouchTimer;
9215        private float mDownPositionX, mDownPositionY;
9216        private PastePopupWindow mPastePopupWindow;
9217        private Runnable mHider;
9218        private Runnable mPastePopupShower;
9219
9220        @Override
9221        public void show() {
9222            super.show();
9223            hideDelayed();
9224            hidePastePopupWindow();
9225        }
9226
9227        public void show(int delayBeforePaste) {
9228            show();
9229
9230            final long durationSinceCutOrCopy = SystemClock.uptimeMillis() - sLastCutOrCopyTime;
9231            if (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION) {
9232                delayBeforePaste = 0;
9233            }
9234            if (delayBeforePaste == 0 || canPaste()) {
9235                if (mPastePopupShower == null) {
9236                    mPastePopupShower = new Runnable() {
9237                        public void run() {
9238                            showPastePopupWindow();
9239                        }
9240                    };
9241                }
9242                TextView.this.postDelayed(mPastePopupShower, delayBeforePaste);
9243            }
9244        }
9245
9246        @Override
9247        protected void dismiss() {
9248            super.dismiss();
9249            onDetached();
9250        }
9251
9252        private void hideDelayed() {
9253            removeHiderCallback();
9254            if (mHider == null) {
9255                mHider = new Runnable() {
9256                    public void run() {
9257                        hide();
9258                    }
9259                };
9260            }
9261            TextView.this.postDelayed(mHider, DELAY_BEFORE_FADE_OUT);
9262        }
9263
9264        private void removeHiderCallback() {
9265            if (mHider != null) {
9266                TextView.this.removeCallbacks(mHider);
9267            }
9268        }
9269
9270        @Override
9271        protected void initDrawable() {
9272            if (mSelectHandleCenter == null) {
9273                mSelectHandleCenter = mContext.getResources().getDrawable(
9274                        mTextSelectHandleRes);
9275            }
9276            mDrawable = mSelectHandleCenter;
9277            mHotspotX = mDrawable.getIntrinsicWidth() / 2.0f;
9278        }
9279
9280        @Override
9281        public boolean onTouchEvent(MotionEvent ev) {
9282            final boolean result = super.onTouchEvent(ev);
9283
9284            switch (ev.getActionMasked()) {
9285                case MotionEvent.ACTION_DOWN:
9286                    mDownPositionX = ev.getRawX();
9287                    mDownPositionY = ev.getRawY();
9288                    mTouchTimer = SystemClock.uptimeMillis();
9289                    break;
9290
9291                case MotionEvent.ACTION_UP:
9292                    long delay = SystemClock.uptimeMillis() - mTouchTimer;
9293                    if (delay < ViewConfiguration.getTapTimeout()) {
9294                        final float deltaX = mDownPositionX - ev.getRawX();
9295                        final float deltaY = mDownPositionY - ev.getRawY();
9296                        final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
9297                        if (distanceSquared < mSquaredTouchSlopDistance) {
9298                            if (mPastePopupWindow != null && mPastePopupWindow.isShowing()) {
9299                                // Tapping on the handle dismisses the displayed paste view,
9300                                mPastePopupWindow.hide();
9301                            } else {
9302                                show(0);
9303                            }
9304                        }
9305                    }
9306                    hideDelayed();
9307                    break;
9308
9309                case MotionEvent.ACTION_CANCEL:
9310                    hideDelayed();
9311                    break;
9312
9313                default:
9314                    break;
9315            }
9316
9317            return result;
9318        }
9319
9320        @Override
9321        public int getCurrentCursorOffset() {
9322            return TextView.this.getSelectionStart();
9323        }
9324
9325        @Override
9326        public void updateOffset(int offset) {
9327            Selection.setSelection((Spannable) mText, offset);
9328        }
9329
9330        @Override
9331        public void updatePosition(int x, int y) {
9332            updateOffset(getOffset(x, y));
9333        }
9334
9335        void showPastePopupWindow() {
9336            if (mPastePopupWindow == null) {
9337                mPastePopupWindow = new PastePopupWindow();
9338            }
9339            mPastePopupWindow.show();
9340        }
9341
9342        @Override
9343        void onHandleMoved() {
9344            removeHiderCallback();
9345            hidePastePopupWindow();
9346        }
9347
9348        void hidePastePopupWindow() {
9349            if (mPastePopupShower != null) {
9350                TextView.this.removeCallbacks(mPastePopupShower);
9351            }
9352            if (mPastePopupWindow != null) {
9353                mPastePopupWindow.hide();
9354            }
9355        }
9356
9357        @Override
9358        public void onDetached() {
9359            removeHiderCallback();
9360            hidePastePopupWindow();
9361        }
9362    }
9363
9364    private class SelectionStartHandleView extends HandleView {
9365        @Override
9366        protected void initDrawable() {
9367            if (mSelectHandleLeft == null) {
9368                mSelectHandleLeft = mContext.getResources().getDrawable(
9369                        mTextSelectHandleLeftRes);
9370            }
9371            mDrawable = mSelectHandleLeft;
9372            mHotspotX = mDrawable.getIntrinsicWidth() * 3.0f / 4.0f;
9373        }
9374
9375        @Override
9376        public int getCurrentCursorOffset() {
9377            return TextView.this.getSelectionStart();
9378        }
9379
9380        @Override
9381        public void updateOffset(int offset) {
9382            Selection.setSelection((Spannable) mText, offset, getSelectionEnd());
9383        }
9384
9385        @Override
9386        public void updatePosition(int x, int y) {
9387            final int selectionStart = getSelectionStart();
9388            final int selectionEnd = getSelectionEnd();
9389
9390            int offset = getOffset(x, y);
9391
9392            // No need to redraw when the offset is unchanged
9393            if (offset == selectionStart) return;
9394            // Handles can not cross and selection is at least one character
9395            if (offset >= selectionEnd) offset = selectionEnd - 1;
9396
9397            Selection.setSelection((Spannable) mText, offset, selectionEnd);
9398        }
9399    }
9400
9401    private class SelectionEndHandleView extends HandleView {
9402        @Override
9403        protected void initDrawable() {
9404            if (mSelectHandleRight == null) {
9405                mSelectHandleRight = mContext.getResources().getDrawable(
9406                        mTextSelectHandleRightRes);
9407            }
9408            mDrawable = mSelectHandleRight;
9409            mHotspotX = mDrawable.getIntrinsicWidth() / 4.0f;
9410        }
9411
9412        @Override
9413        public int getCurrentCursorOffset() {
9414            return TextView.this.getSelectionEnd();
9415        }
9416
9417        @Override
9418        public void updateOffset(int offset) {
9419            Selection.setSelection((Spannable) mText, getSelectionStart(), offset);
9420        }
9421
9422        @Override
9423        public void updatePosition(int x, int y) {
9424            final int selectionStart = getSelectionStart();
9425            final int selectionEnd = getSelectionEnd();
9426
9427            int offset = getOffset(x, y);
9428
9429            // No need to redraw when the offset is unchanged
9430            if (offset == selectionEnd) return;
9431            // Handles can not cross and selection is at least one character
9432            if (offset <= selectionStart) offset = selectionStart + 1;
9433
9434            Selection.setSelection((Spannable) mText, selectionStart, offset);
9435        }
9436    }
9437
9438    /**
9439     * A CursorController instance can be used to control a cursor in the text.
9440     * It is not used outside of {@link TextView}.
9441     * @hide
9442     */
9443    private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener {
9444        /**
9445         * Makes the cursor controller visible on screen. Will be drawn by {@link #draw(Canvas)}.
9446         * See also {@link #hide()}.
9447         */
9448        public void show();
9449
9450        /**
9451         * Hide the cursor controller from screen.
9452         * See also {@link #show()}.
9453         */
9454        public void hide();
9455
9456        /**
9457         * This method is called by {@link #onTouchEvent(MotionEvent)} and gives the controller
9458         * a chance to become active and/or visible.
9459         * @param event The touch event
9460         */
9461        public boolean onTouchEvent(MotionEvent event);
9462
9463        /**
9464         * Called when the view is detached from window. Perform house keeping task, such as
9465         * stopping Runnable thread that would otherwise keep a reference on the context, thus
9466         * preventing the activity from being recycled.
9467         */
9468        public void onDetached();
9469    }
9470
9471    private class InsertionPointCursorController implements CursorController {
9472        private static final int DELAY_BEFORE_PASTE = 2000;
9473
9474        private InsertionHandleView mHandle;
9475
9476        public void show() {
9477            ((InsertionHandleView) getHandle()).show(DELAY_BEFORE_PASTE);
9478        }
9479
9480        public void showWithPaste() {
9481            ((InsertionHandleView) getHandle()).show(0);
9482        }
9483
9484        public void hide() {
9485            if (mHandle != null) {
9486                mHandle.hide();
9487            }
9488        }
9489
9490        public boolean onTouchEvent(MotionEvent ev) {
9491            return false;
9492        }
9493
9494        public void onTouchModeChanged(boolean isInTouchMode) {
9495            if (!isInTouchMode) {
9496                hide();
9497            }
9498        }
9499
9500        private HandleView getHandle() {
9501            if (mHandle == null) {
9502                mHandle = new InsertionHandleView();
9503            }
9504            return mHandle;
9505        }
9506
9507        @Override
9508        public void onDetached() {
9509            final ViewTreeObserver observer = getViewTreeObserver();
9510            observer.removeOnTouchModeChangeListener(this);
9511
9512            if (mHandle != null) mHandle.onDetached();
9513        }
9514    }
9515
9516    private class SelectionModifierCursorController implements CursorController {
9517        // The cursor controller handles, lazily created when shown.
9518        private SelectionStartHandleView mStartHandle;
9519        private SelectionEndHandleView mEndHandle;
9520        // The offsets of that last touch down event. Remembered to start selection there.
9521        private int mMinTouchOffset, mMaxTouchOffset;
9522
9523        // Double tap detection
9524        private long mPreviousTapUpTime = 0;
9525        private int mPreviousTapPositionX, mPreviousTapPositionY;
9526
9527        SelectionModifierCursorController() {
9528            resetTouchOffsets();
9529        }
9530
9531        public void show() {
9532            if (isInBatchEditMode()) {
9533                return;
9534            }
9535
9536            // Lazy object creation has to be done before updatePosition() is called.
9537            if (mStartHandle == null) mStartHandle = new SelectionStartHandleView();
9538            if (mEndHandle == null) mEndHandle = new SelectionEndHandleView();
9539
9540            mStartHandle.show();
9541            mEndHandle.show();
9542
9543            hideInsertionPointCursorController();
9544            hideSuggestions();
9545        }
9546
9547        public void hide() {
9548            if (mStartHandle != null) mStartHandle.hide();
9549            if (mEndHandle != null) mEndHandle.hide();
9550        }
9551
9552        public boolean onTouchEvent(MotionEvent event) {
9553            // This is done even when the View does not have focus, so that long presses can start
9554            // selection and tap can move cursor from this tap position.
9555            if (isTextEditable() || mTextIsSelectable) {
9556                switch (event.getActionMasked()) {
9557                    case MotionEvent.ACTION_DOWN:
9558                        final int x = (int) event.getX();
9559                        final int y = (int) event.getY();
9560
9561                        // Remember finger down position, to be able to start selection from there
9562                        mMinTouchOffset = mMaxTouchOffset = getOffset(x, y);
9563
9564                        // Double tap detection
9565                        long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime;
9566                        if (duration <= ViewConfiguration.getDoubleTapTimeout() &&
9567                                isPositionOnText(x, y)) {
9568                            final int deltaX = x - mPreviousTapPositionX;
9569                            final int deltaY = y - mPreviousTapPositionY;
9570                            final int distanceSquared = deltaX * deltaX + deltaY * deltaY;
9571                            if (distanceSquared < mSquaredTouchSlopDistance) {
9572                                showSuggestions();
9573                                mDiscardNextActionUp = true;
9574                            }
9575                        }
9576
9577                        mPreviousTapPositionX = x;
9578                        mPreviousTapPositionY = y;
9579
9580                        break;
9581
9582                    case MotionEvent.ACTION_POINTER_DOWN:
9583                    case MotionEvent.ACTION_POINTER_UP:
9584                        // Handle multi-point gestures. Keep min and max offset positions.
9585                        // Only activated for devices that correctly handle multi-touch.
9586                        if (mContext.getPackageManager().hasSystemFeature(
9587                                PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) {
9588                            updateMinAndMaxOffsets(event);
9589                        }
9590                        break;
9591
9592                    case MotionEvent.ACTION_UP:
9593                        mPreviousTapUpTime = SystemClock.uptimeMillis();
9594                        break;
9595                }
9596            }
9597            return false;
9598        }
9599
9600        /**
9601         * @param event
9602         */
9603        private void updateMinAndMaxOffsets(MotionEvent event) {
9604            int pointerCount = event.getPointerCount();
9605            for (int index = 0; index < pointerCount; index++) {
9606                final int x = (int) event.getX(index);
9607                final int y = (int) event.getY(index);
9608                int offset = getOffset(x, y);
9609                if (offset < mMinTouchOffset) mMinTouchOffset = offset;
9610                if (offset > mMaxTouchOffset) mMaxTouchOffset = offset;
9611            }
9612        }
9613
9614        public int getMinTouchOffset() {
9615            return mMinTouchOffset;
9616        }
9617
9618        public int getMaxTouchOffset() {
9619            return mMaxTouchOffset;
9620        }
9621
9622        public void resetTouchOffsets() {
9623            mMinTouchOffset = mMaxTouchOffset = -1;
9624        }
9625
9626        /**
9627         * @return true iff this controller is currently used to move the selection start.
9628         */
9629        public boolean isSelectionStartDragged() {
9630            return mStartHandle != null && mStartHandle.isDragging();
9631        }
9632
9633        public void onTouchModeChanged(boolean isInTouchMode) {
9634            if (!isInTouchMode) {
9635                hide();
9636            }
9637        }
9638
9639        @Override
9640        public void onDetached() {
9641            final ViewTreeObserver observer = getViewTreeObserver();
9642            observer.removeOnTouchModeChangeListener(this);
9643
9644            if (mStartHandle != null) mStartHandle.onDetached();
9645            if (mEndHandle != null) mEndHandle.onDetached();
9646        }
9647    }
9648
9649    private void hideInsertionPointCursorController() {
9650        // No need to create the controller to hide it.
9651        if (mInsertionPointCursorController != null) {
9652            mInsertionPointCursorController.hide();
9653        }
9654    }
9655
9656    /**
9657     * Hides the insertion controller and stops text selection mode, hiding the selection controller
9658     */
9659    private void hideControllers() {
9660        hideInsertionPointCursorController();
9661        stopSelectionActionMode();
9662        hideSuggestions();
9663    }
9664
9665    /**
9666     * Get the offset character closest to the specified absolute position.
9667     *
9668     * @param x The horizontal absolute position of a point on screen
9669     * @param y The vertical absolute position of a point on screen
9670     * @return the character offset for the character whose position is closest to the specified
9671     *  position. Returns -1 if there is no layout.
9672     *
9673     * @hide
9674     */
9675    public int getOffset(int x, int y) {
9676        if (getLayout() == null) return -1;
9677        final int line = getLineAtCoordinate(y);
9678        final int offset = getOffsetAtCoordinate(line, x);
9679        return offset;
9680    }
9681
9682    private int convertToLocalHorizontalCoordinate(int x) {
9683        x -= getTotalPaddingLeft();
9684        // Clamp the position to inside of the view.
9685        x = Math.max(0, x);
9686        x = Math.min(getWidth() - getTotalPaddingRight() - 1, x);
9687        x += getScrollX();
9688        return x;
9689    }
9690
9691    private int getLineAtCoordinate(int y) {
9692        y -= getTotalPaddingTop();
9693        // Clamp the position to inside of the view.
9694        y = Math.max(0, y);
9695        y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y);
9696        y += getScrollY();
9697        return getLayout().getLineForVertical(y);
9698    }
9699
9700    private int getOffsetAtCoordinate(int line, int x) {
9701        x = convertToLocalHorizontalCoordinate(x);
9702        return getLayout().getOffsetForHorizontal(line, x);
9703    }
9704
9705    /** Returns true if the screen coordinates position (x,y) corresponds to a character displayed
9706     * in the view. Returns false when the position is in the empty space of left/right of text.
9707     */
9708    private boolean isPositionOnText(int x, int y) {
9709        if (getLayout() == null) return false;
9710
9711        final int line = getLineAtCoordinate(y);
9712        x = convertToLocalHorizontalCoordinate(x);
9713
9714        if (x < getLayout().getLineLeft(line)) return false;
9715        if (x > getLayout().getLineRight(line)) return false;
9716        return true;
9717    }
9718
9719    @Override
9720    public boolean onDragEvent(DragEvent event) {
9721        switch (event.getAction()) {
9722            case DragEvent.ACTION_DRAG_STARTED:
9723                return hasInsertionController();
9724
9725            case DragEvent.ACTION_DRAG_ENTERED:
9726                TextView.this.requestFocus();
9727                return true;
9728
9729            case DragEvent.ACTION_DRAG_LOCATION:
9730                final int offset = getOffset((int) event.getX(), (int) event.getY());
9731                Selection.setSelection((Spannable)mText, offset);
9732                return true;
9733
9734            case DragEvent.ACTION_DROP:
9735                onDrop(event);
9736                return true;
9737
9738            case DragEvent.ACTION_DRAG_ENDED:
9739            case DragEvent.ACTION_DRAG_EXITED:
9740            default:
9741                return true;
9742        }
9743    }
9744
9745    private void onDrop(DragEvent event) {
9746        StringBuilder content = new StringBuilder("");
9747        ClipData clipData = event.getClipData();
9748        final int itemCount = clipData.getItemCount();
9749        for (int i=0; i < itemCount; i++) {
9750            Item item = clipData.getItemAt(i);
9751            content.append(item.coerceToText(TextView.this.mContext));
9752        }
9753
9754        final int offset = getOffset((int) event.getX(), (int) event.getY());
9755
9756        Object localState = event.getLocalState();
9757        DragLocalState dragLocalState = null;
9758        if (localState instanceof DragLocalState) {
9759            dragLocalState = (DragLocalState) localState;
9760        }
9761        boolean dragDropIntoItself = dragLocalState != null &&
9762                dragLocalState.sourceTextView == this;
9763
9764        if (dragDropIntoItself) {
9765            if (offset >= dragLocalState.start && offset < dragLocalState.end) {
9766                // A drop inside the original selection discards the drop.
9767                return;
9768            }
9769        }
9770
9771        final int originalLength = mText.length();
9772        long minMax = prepareSpacesAroundPaste(offset, offset, content);
9773        int min = extractRangeStartFromLong(minMax);
9774        int max = extractRangeEndFromLong(minMax);
9775
9776        Selection.setSelection((Spannable) mText, max);
9777        ((Editable) mText).replace(min, max, content);
9778
9779        if (dragDropIntoItself) {
9780            int dragSourceStart = dragLocalState.start;
9781            int dragSourceEnd = dragLocalState.end;
9782            if (max <= dragSourceStart) {
9783                // Inserting text before selection has shifted positions
9784                final int shift = mText.length() - originalLength;
9785                dragSourceStart += shift;
9786                dragSourceEnd += shift;
9787            }
9788
9789            // Delete original selection
9790            ((Editable) mText).delete(dragSourceStart, dragSourceEnd);
9791
9792            // Make sure we do not leave two adjacent spaces.
9793            if ((dragSourceStart == 0 ||
9794                    Character.isSpaceChar(mTransformed.charAt(dragSourceStart - 1))) &&
9795                    (dragSourceStart == mText.length() ||
9796                    Character.isSpaceChar(mTransformed.charAt(dragSourceStart)))) {
9797                final int pos = dragSourceStart == mText.length() ?
9798                        dragSourceStart - 1 : dragSourceStart;
9799                ((Editable) mText).delete(pos, pos + 1);
9800            }
9801        }
9802    }
9803
9804    /**
9805     * @return True if this view supports insertion handles.
9806     */
9807    boolean hasInsertionController() {
9808        return mInsertionControllerEnabled;
9809    }
9810
9811    /**
9812     * @return True if this view supports selection handles.
9813     */
9814    boolean hasSelectionController() {
9815        return mSelectionControllerEnabled;
9816    }
9817
9818    InsertionPointCursorController getInsertionController() {
9819        if (!mInsertionControllerEnabled) {
9820            return null;
9821        }
9822
9823        if (mInsertionPointCursorController == null) {
9824            mInsertionPointCursorController = new InsertionPointCursorController();
9825
9826            final ViewTreeObserver observer = getViewTreeObserver();
9827            observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
9828        }
9829
9830        return mInsertionPointCursorController;
9831    }
9832
9833    SelectionModifierCursorController getSelectionController() {
9834        if (!mSelectionControllerEnabled) {
9835            return null;
9836        }
9837
9838        if (mSelectionModifierCursorController == null) {
9839            mSelectionModifierCursorController = new SelectionModifierCursorController();
9840
9841            final ViewTreeObserver observer = getViewTreeObserver();
9842            observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
9843        }
9844
9845        return mSelectionModifierCursorController;
9846    }
9847
9848    boolean isInBatchEditMode() {
9849        final InputMethodState ims = mInputMethodState;
9850        if (ims != null) {
9851            return ims.mBatchEditNesting > 0;
9852        }
9853        return mInBatchEditControllers;
9854    }
9855
9856    @ViewDebug.ExportedProperty(category = "text")
9857    private CharSequence            mText;
9858    private CharSequence            mTransformed;
9859    private BufferType              mBufferType = BufferType.NORMAL;
9860
9861    private int                     mInputType = EditorInfo.TYPE_NULL;
9862    private CharSequence            mHint;
9863    private Layout                  mHintLayout;
9864
9865    private KeyListener             mInput;
9866
9867    private MovementMethod          mMovement;
9868    private TransformationMethod    mTransformation;
9869    private ChangeWatcher           mChangeWatcher;
9870
9871    private ArrayList<TextWatcher>  mListeners = null;
9872
9873    // display attributes
9874    private final TextPaint         mTextPaint;
9875    private boolean                 mUserSetTextScaleX;
9876    private final Paint             mHighlightPaint;
9877    private int                     mHighlightColor = 0xCC475925;
9878    /**
9879     * This is temporarily visible to fix bug 3085564 in webView. Do not rely on
9880     * this field being protected. Will be restored as private when lineHeight
9881     * feature request 3215097 is implemented
9882     * @hide
9883     */
9884    protected Layout                mLayout;
9885
9886    private long                    mShowCursor;
9887    private Blink                   mBlink;
9888    private boolean                 mCursorVisible = true;
9889
9890    // Cursor Controllers.
9891    private InsertionPointCursorController mInsertionPointCursorController;
9892    private SelectionModifierCursorController mSelectionModifierCursorController;
9893    private ActionMode              mSelectionActionMode;
9894    private boolean                 mInsertionControllerEnabled;
9895    private boolean                 mSelectionControllerEnabled;
9896    private boolean                 mInBatchEditControllers;
9897
9898    // These are needed to desambiguate a long click. If the long click comes from ones of these, we
9899    // select from the current cursor position. Otherwise, select from long pressed position.
9900    private boolean                 mDPadCenterIsDown = false;
9901    private boolean                 mEnterKeyIsDown = false;
9902    private boolean                 mContextMenuTriggeredByKey = false;
9903    // Created once and shared by different CursorController helper methods.
9904    // Only one cursor controller is active at any time which prevent race conditions.
9905    private static Rect             sCursorControllerTempRect = new Rect();
9906
9907    private boolean                 mSelectAllOnFocus = false;
9908
9909    private int                     mGravity = Gravity.TOP | Gravity.LEFT;
9910    private boolean                 mHorizontallyScrolling;
9911
9912    private int                     mAutoLinkMask;
9913    private boolean                 mLinksClickable = true;
9914
9915    private float                   mSpacingMult = 1;
9916    private float                   mSpacingAdd = 0;
9917    private boolean                 mTextIsSelectable = false;
9918
9919    private static final int        LINES = 1;
9920    private static final int        EMS = LINES;
9921    private static final int        PIXELS = 2;
9922
9923    private int                     mMaximum = Integer.MAX_VALUE;
9924    private int                     mMaxMode = LINES;
9925    private int                     mMinimum = 0;
9926    private int                     mMinMode = LINES;
9927
9928    private int                     mMaxWidth = Integer.MAX_VALUE;
9929    private int                     mMaxWidthMode = PIXELS;
9930    private int                     mMinWidth = 0;
9931    private int                     mMinWidthMode = PIXELS;
9932
9933    private boolean                 mSingleLine;
9934    private int                     mDesiredHeightAtMeasure = -1;
9935    private boolean                 mIncludePad = true;
9936
9937    // tmp primitives, so we don't alloc them on each draw
9938    private Path                    mHighlightPath;
9939    private boolean                 mHighlightPathBogus = true;
9940    private static final RectF      sTempRect = new RectF();
9941
9942    // XXX should be much larger
9943    private static final int        VERY_WIDE = 16384;
9944
9945    private static final int        BLINK = 500;
9946
9947    private static final int ANIMATED_SCROLL_GAP = 250;
9948    private long mLastScroll;
9949    private Scroller mScroller = null;
9950
9951    private BoringLayout.Metrics mBoring;
9952    private BoringLayout.Metrics mHintBoring;
9953
9954    private BoringLayout mSavedLayout, mSavedHintLayout;
9955
9956    private static final InputFilter[] NO_FILTERS = new InputFilter[0];
9957    private InputFilter[] mFilters = NO_FILTERS;
9958    private static final Spanned EMPTY_SPANNED = new SpannedString("");
9959    private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20;
9960    // System wide time for last cut or copy action.
9961    private static long sLastCutOrCopyTime;
9962    // Used to highlight a word when it is corrected by the IME
9963    private CorrectionHighlighter mCorrectionHighlighter;
9964    // New state used to change background based on whether this TextView is multiline.
9965    private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline };
9966}
9967