Search in sources :

Example 76 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class TextView method removeSuggestionSpans.

/**
     * Removes the suggestion spans.
     */
CharSequence removeSuggestionSpans(CharSequence text) {
    if (text instanceof Spanned) {
        Spannable spannable;
        if (text instanceof Spannable) {
            spannable = (Spannable) text;
        } else {
            spannable = new SpannableString(text);
            text = spannable;
        }
        SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
        for (int i = 0; i < spans.length; i++) {
            spannable.removeSpan(spans[i]);
        }
    }
    return text;
}
Also used : SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 77 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class EditableInputConnection method commitText.

@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
    if (mTextView == null) {
        return super.commitText(text, newCursorPosition);
    }
    if (text instanceof Spanned) {
        Spanned spanned = ((Spanned) text);
        SuggestionSpan[] spans = spanned.getSpans(0, text.length(), SuggestionSpan.class);
        mIMM.registerSuggestionSpansForNotification(spans);
    }
    mTextView.resetErrorChangedFlag();
    boolean success = super.commitText(text, newCursorPosition);
    mTextView.hideErrorIfUnchanged();
    return success;
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) Spanned(android.text.Spanned)

Example 78 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class Editor method isCursorInsideEasyCorrectionSpan.

/**
     * @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with
     * {@link SuggestionSpan#FLAG_EASY_CORRECT} set.
     */
private boolean isCursorInsideEasyCorrectionSpan() {
    Spannable spannable = (Spannable) mTextView.getText();
    SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(), mTextView.getSelectionEnd(), SuggestionSpan.class);
    for (int i = 0; i < suggestionSpans.length; i++) {
        if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) {
            return true;
        }
    }
    return false;
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) Paint(android.graphics.Paint)

Example 79 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class Editor method findEquivalentSuggestionSpan.

@Nullable
private SuggestionSpan findEquivalentSuggestionSpan(@NonNull SuggestionSpanInfo suggestionSpanInfo) {
    final Editable editable = (Editable) mTextView.getText();
    if (editable.getSpanStart(suggestionSpanInfo.mSuggestionSpan) >= 0) {
        // Exactly same span is found.
        return suggestionSpanInfo.mSuggestionSpan;
    }
    // Suggestion span couldn't be found. Try to find a suggestion span that has the same
    // contents.
    final SuggestionSpan[] suggestionSpans = editable.getSpans(suggestionSpanInfo.mSpanStart, suggestionSpanInfo.mSpanEnd, SuggestionSpan.class);
    for (final SuggestionSpan suggestionSpan : suggestionSpans) {
        final int start = editable.getSpanStart(suggestionSpan);
        if (start != suggestionSpanInfo.mSpanStart) {
            continue;
        }
        final int end = editable.getSpanEnd(suggestionSpan);
        if (end != suggestionSpanInfo.mSpanEnd) {
            continue;
        }
        if (suggestionSpan.equals(suggestionSpanInfo.mSuggestionSpan)) {
            return suggestionSpan;
        }
    }
    return null;
}
Also used : Editable(android.text.Editable) SuggestionSpan(android.text.style.SuggestionSpan) Paint(android.graphics.Paint) Nullable(android.annotation.Nullable)

Example 80 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class Editor method shouldOfferToShowSuggestions.

/**
     * @return <code>true</code> if it's reasonable to offer to show suggestions depending on
     * the current cursor position or selection range. This method is consistent with the
     * method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}.
     */
private boolean shouldOfferToShowSuggestions() {
    CharSequence text = mTextView.getText();
    if (!(text instanceof Spannable))
        return false;
    final Spannable spannable = (Spannable) text;
    final int selectionStart = mTextView.getSelectionStart();
    final int selectionEnd = mTextView.getSelectionEnd();
    final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd, SuggestionSpan.class);
    if (suggestionSpans.length == 0) {
        return false;
    }
    if (selectionStart == selectionEnd) {
        // Spans overlap the cursor.
        for (int i = 0; i < suggestionSpans.length; i++) {
            if (suggestionSpans[i].getSuggestions().length > 0) {
                return true;
            }
        }
        return false;
    }
    int minSpanStart = mTextView.getText().length();
    int maxSpanEnd = 0;
    int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length();
    int unionOfSpansCoveringSelectionStartEnd = 0;
    boolean hasValidSuggestions = false;
    for (int i = 0; i < suggestionSpans.length; i++) {
        final int spanStart = spannable.getSpanStart(suggestionSpans[i]);
        final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]);
        minSpanStart = Math.min(minSpanStart, spanStart);
        maxSpanEnd = Math.max(maxSpanEnd, spanEnd);
        if (selectionStart < spanStart || selectionStart > spanEnd) {
            // The span doesn't cover the current selection start point.
            continue;
        }
        hasValidSuggestions = hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0;
        unionOfSpansCoveringSelectionStartStart = Math.min(unionOfSpansCoveringSelectionStartStart, spanStart);
        unionOfSpansCoveringSelectionStartEnd = Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd);
    }
    if (!hasValidSuggestions) {
        return false;
    }
    if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) {
        // No spans cover the selection start point.
        return false;
    }
    if (minSpanStart < unionOfSpansCoveringSelectionStartStart || maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) {
        // to show suggestions as it's confusing.
        return false;
    }
    return true;
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) Paint(android.graphics.Paint)

Aggregations

SuggestionSpan (android.text.style.SuggestionSpan)114 Paint (android.graphics.Paint)43 SmallTest (android.test.suitebuilder.annotation.SmallTest)35 Spannable (android.text.Spannable)29 Spanned (android.text.Spanned)24 Editable (android.text.Editable)21 TextPaint (android.text.TextPaint)21 SpannableString (android.text.SpannableString)14 View (android.view.View)10 SpannableStringBuilder (android.text.SpannableStringBuilder)7 SpellCheckSpan (android.text.style.SpellCheckSpan)7 Nullable (android.annotation.Nullable)5 TypedArray (android.content.res.TypedArray)5 Espresso.onView (android.support.test.espresso.Espresso.onView)5 NoMatchingViewException (android.support.test.espresso.NoMatchingViewException)5 ViewAssertion (android.support.test.espresso.ViewAssertion)5 RootMatchers.withDecorView (android.support.test.espresso.matcher.RootMatchers.withDecorView)5 BackgroundColorSpan (android.text.style.BackgroundColorSpan)5 ForegroundColorSpan (android.text.style.ForegroundColorSpan)5 ImageSpan (android.text.style.ImageSpan)5