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