Search in sources :

Example 61 with Spannable

use of android.text.Spannable in project XobotOS by xamarin.

the class TextView method onFocusChanged.

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    if (mTemporaryDetach) {
        // If we are temporarily in the detach state, then do nothing.
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        return;
    }
    mShowCursor = SystemClock.uptimeMillis();
    ensureEndedBatchEdit();
    if (focused) {
        int selStart = getSelectionStart();
        int selEnd = getSelectionEnd();
        // SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
        // mode for these, unless there was a specific selection already started.
        final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 && selEnd == mText.length();
        mCreatedWithASelection = mFrozenWithFocus && hasSelection() && !isFocusHighlighted;
        if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
            // If a tap was used to give focus to that view, move cursor at tap position.
            // Has to be done before onTakeFocus, which can be overloaded.
            final int lastTapPosition = getLastTapPosition();
            if (lastTapPosition >= 0) {
                Selection.setSelection((Spannable) mText, lastTapPosition);
            }
            if (mMovement != null) {
                mMovement.onTakeFocus(this, (Spannable) mText, direction);
            }
            // It would be better to know why the DecorView does not have focus at that time.
            if (((this instanceof ExtractEditText) || mSelectionMoved) && selStart >= 0 && selEnd >= 0) {
                /*
                     * Someone intentionally set the selection, so let them
                     * do whatever it is that they wanted to do instead of
                     * the default on-focus behavior.  We reset the selection
                     * here instead of just skipping the onTakeFocus() call
                     * because some movement methods do something other than
                     * just setting the selection in theirs and we still
                     * need to go through that path.
                     */
                Selection.setSelection((Spannable) mText, selStart, selEnd);
            }
            if (mSelectAllOnFocus) {
                selectAll();
            }
            mTouchFocusSelected = true;
        }
        mFrozenWithFocus = false;
        mSelectionMoved = false;
        if (mText instanceof Spannable) {
            Spannable sp = (Spannable) mText;
            MetaKeyKeyListener.resetMetaState(sp);
        }
        makeBlink();
        if (mError != null) {
            showError();
        }
    } else {
        if (mError != null) {
            hideError();
        }
        // Don't leave us in the middle of a batch edit.
        onEndBatchEdit();
        if (this instanceof ExtractEditText) {
            // terminateTextSelectionMode removes selection, which we want to keep when
            // ExtractEditText goes out of focus.
            final int selStart = getSelectionStart();
            final int selEnd = getSelectionEnd();
            hideControllers();
            Selection.setSelection((Spannable) mText, selStart, selEnd);
        } else {
            hideControllers();
            downgradeEasyCorrectionSpans();
        }
        // No need to create the controller
        if (mSelectionModifierCursorController != null) {
            mSelectionModifierCursorController.resetTouchOffsets();
        }
    }
    startStopMarquee(focused);
    if (mTransformation != null) {
        mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect);
    }
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
Also used : ExtractEditText(android.inputmethodservice.ExtractEditText) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 62 with Spannable

use of android.text.Spannable in project android_frameworks_base by ResurrectionRemix.

the class TextView method setText.

private void setText(CharSequence text, BufferType type, boolean notifyBefore, int oldlen) {
    if (text == null) {
        text = "";
    }
    // If suggestions are not enabled, remove the suggestion spans from the text
    if (!isSuggestionsEnabled()) {
        text = removeSuggestionSpans(text);
    }
    if (!mUserSetTextScaleX)
        mTextPaint.setTextScaleX(1.0f);
    if (text instanceof Spanned && ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
        if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) {
            setHorizontalFadingEdgeEnabled(true);
            mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
        } else {
            setHorizontalFadingEdgeEnabled(false);
            mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
        }
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
    }
    int n = mFilters.length;
    for (int i = 0; i < n; i++) {
        CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
        if (out != null) {
            text = out;
        }
    }
    if (notifyBefore) {
        if (mText != null) {
            oldlen = mText.length();
            sendBeforeTextChanged(mText, 0, oldlen, text.length());
        } else {
            sendBeforeTextChanged("", 0, 0, text.length());
        }
    }
    boolean needEditableForNotification = false;
    if (mListeners != null && mListeners.size() != 0) {
        needEditableForNotification = true;
    }
    if (type == BufferType.EDITABLE || getKeyListener() != null || needEditableForNotification) {
        createEditorIfNeeded();
        mEditor.forgetUndoRedo();
        Editable t = mEditableFactory.newEditable(text);
        text = t;
        setFilters(t, mFilters);
        InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null)
            imm.restartInput(this);
    } else if (type == BufferType.SPANNABLE || mMovement != null) {
        text = mSpannableFactory.newSpannable(text);
    } else if (!(text instanceof CharWrapper)) {
        text = TextUtils.stringOrSpannedString(text);
    }
    if (mAutoLinkMask != 0) {
        Spannable s2;
        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }
        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;
            /*
                 * We must go ahead and set the text before changing the
                 * movement method, because setMovementMethod() may call
                 * setText() again to try to upgrade the buffer type.
                 */
            mText = text;
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }
    mBufferType = type;
    mText = text;
    if (mTransformation == null) {
        mTransformed = text;
    } else {
        mTransformed = mTransformation.getTransformation(text, this);
    }
    final int textLength = text.length();
    if (text instanceof Spannable && !mAllowTransformationLengthChange) {
        Spannable sp = (Spannable) text;
        // Remove any ChangeWatchers that might have come from other TextViews.
        final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
        final int count = watchers.length;
        for (int i = 0; i < count; i++) {
            sp.removeSpan(watchers[i]);
        }
        if (mChangeWatcher == null)
            mChangeWatcher = new ChangeWatcher();
        sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));
        if (mEditor != null)
            mEditor.addSpanWatchers(sp);
        if (mTransformation != null) {
            sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        if (mMovement != null) {
            mMovement.initialize(this, (Spannable) text);
            /*
                 * Initializing the movement method will have set the
                 * selection, so reset mSelectionMoved to keep that from
                 * interfering with the normal on-focus selection-setting.
                 */
            if (mEditor != null)
                mEditor.mSelectionMoved = false;
        }
    }
    if (mLayout != null) {
        checkForRelayout();
    }
    sendOnTextChanged(text, 0, oldlen, textLength);
    onTextChanged(text, 0, oldlen, textLength);
    notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
    if (needEditableForNotification) {
        sendAfterTextChanged((Editable) text);
    }
    // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
    if (mEditor != null)
        mEditor.prepareCursorControllers();
}
Also used : Editable(android.text.Editable) InputMethodManager(android.view.inputmethod.InputMethodManager) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 63 with Spannable

use of android.text.Spannable in project android_frameworks_base by ResurrectionRemix.

the class TextView method setTextKeepState.

/**
     * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)},
     * except that the cursor position (if any) is retained in the new text.
     *
     * @see #setText(CharSequence, android.widget.TextView.BufferType)
     */
public final void setTextKeepState(CharSequence text, BufferType type) {
    int start = getSelectionStart();
    int end = getSelectionEnd();
    int len = text.length();
    setText(text, type);
    if (start >= 0 || end >= 0) {
        if (mText instanceof Spannable) {
            Selection.setSelection((Spannable) mText, Math.max(0, Math.min(start, len)), Math.max(0, Math.min(end, len)));
        }
    }
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 64 with Spannable

use of android.text.Spannable in project android_frameworks_base by ResurrectionRemix.

the class TextView method onRestoreInstanceState.

@Override
public void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
        super.onRestoreInstanceState(state);
        return;
    }
    SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.getSuperState());
    // XXX restore buffer type too, as well as lots of other stuff
    if (ss.text != null) {
        setText(ss.text);
    }
    if (ss.selStart >= 0 && ss.selEnd >= 0) {
        if (mText instanceof Spannable) {
            int len = mText.length();
            if (ss.selStart > len || ss.selEnd > len) {
                String restored = "";
                if (ss.text != null) {
                    restored = "(restored) ";
                }
                Log.e(LOG_TAG, "Saved cursor position " + ss.selStart + "/" + ss.selEnd + " out of range for " + restored + "text " + mText);
            } else {
                Selection.setSelection((Spannable) mText, ss.selStart, ss.selEnd);
                if (ss.frozenWithFocus) {
                    createEditorIfNeeded();
                    mEditor.mFrozenWithFocus = true;
                }
            }
        }
    }
    if (ss.error != null) {
        final CharSequence error = ss.error;
        // Display the error later, after the first layout pass
        post(new Runnable() {

            public void run() {
                if (mEditor == null || !mEditor.mErrorWasChanged) {
                    setError(error);
                }
            }
        });
    }
    if (ss.editorState != null) {
        createEditorIfNeeded();
        mEditor.restoreInstanceState(ss.editorState);
    }
}
Also used : SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 65 with Spannable

use of android.text.Spannable in project android_frameworks_base by ResurrectionRemix.

the class TextView method moveCursorToVisibleOffset.

/**
     * Move the cursor, if needed, so that it is at an offset that is visible
     * to the user.  This will not move the cursor if it represents more than
     * one character (a selection range).  This will only work if the
     * TextView contains spannable text; otherwise it will do nothing.
     *
     * @return True if the cursor was actually moved, false otherwise.
     */
public boolean moveCursorToVisibleOffset() {
    if (!(mText instanceof Spannable)) {
        return false;
    }
    int start = getSelectionStart();
    int end = getSelectionEnd();
    if (start != end) {
        return false;
    }
    // First: make sure the line is visible on screen:
    int line = mLayout.getLineForOffset(start);
    final int top = mLayout.getLineTop(line);
    final int bottom = mLayout.getLineTop(line + 1);
    final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
    int vslack = (bottom - top) / 2;
    if (vslack > vspace / 4)
        vslack = vspace / 4;
    final int vs = mScrollY;
    if (top < (vs + vslack)) {
        line = mLayout.getLineForVertical(vs + vslack + (bottom - top));
    } else if (bottom > (vspace + vs - vslack)) {
        line = mLayout.getLineForVertical(vspace + vs - vslack - (bottom - top));
    }
    // Next: make sure the character is visible on screen:
    final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
    final int hs = mScrollX;
    final int leftChar = mLayout.getOffsetForHorizontal(line, hs);
    final int rightChar = mLayout.getOffsetForHorizontal(line, hspace + hs);
    // line might contain bidirectional text
    final int lowChar = leftChar < rightChar ? leftChar : rightChar;
    final int highChar = leftChar > rightChar ? leftChar : rightChar;
    int newStart = start;
    if (newStart < lowChar) {
        newStart = lowChar;
    } else if (newStart > highChar) {
        newStart = highChar;
    }
    if (newStart != start) {
        Selection.setSelection((Spannable) mText, newStart);
        return true;
    }
    return false;
}
Also used : Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

Spannable (android.text.Spannable)333 Paint (android.graphics.Paint)122 TextPaint (android.text.TextPaint)97 SpannableString (android.text.SpannableString)72 Editable (android.text.Editable)42 InputMethodManager (android.view.inputmethod.InputMethodManager)34 Spanned (android.text.Spanned)32 SuggestionSpan (android.text.style.SuggestionSpan)29 View (android.view.View)28 SpannableStringBuilder (android.text.SpannableStringBuilder)24 ForegroundColorSpan (android.text.style.ForegroundColorSpan)21 TextView (android.widget.TextView)17 ClickableSpan (android.text.style.ClickableSpan)14 StyleSpan (android.text.style.StyleSpan)14 Intent (android.content.Intent)13 KeyEvent (android.view.KeyEvent)13 URLSpan (android.text.style.URLSpan)12 Layout (android.text.Layout)11 Parcelable (android.os.Parcelable)8 SpannedString (android.text.SpannedString)8