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