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