Search in sources :

Example 86 with SuggestionSpan

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

the class SuggestionsPopupWindowTest method testInsertionActionMode.

@SmallTest
public void testInsertionActionMode() {
    final String text = "abc def ghi";
    onView(withId(R.id.textview)).perform(click());
    onView(withId(R.id.textview)).perform(replaceText(text));
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), new String[] { "DEF", "Def" }, SuggestionSpan.FLAG_AUTO_CORRECTION);
    setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
    onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
    onHandleView(com.android.internal.R.id.insertion_handle).perform(click());
    sleepForFloatingToolbarPopup();
    assertFloatingToolbarContainsItem(getActivity().getString(com.android.internal.R.string.replace));
    clickFloatingToolbarItem(getActivity().getString(com.android.internal.R.string.replace));
    assertSuggestionsPopupIsDisplayed();
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 87 with SuggestionSpan

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

the class SpellChecker method onGetSuggestionsInternal.

private SpellCheckSpan onGetSuggestionsInternal(SuggestionsInfo suggestionsInfo, int offset, int length) {
    if (suggestionsInfo == null || suggestionsInfo.getCookie() != mCookie) {
        return null;
    }
    final Editable editable = (Editable) mTextView.getText();
    final int sequenceNumber = suggestionsInfo.getSequence();
    for (int k = 0; k < mLength; ++k) {
        if (sequenceNumber == mIds[k]) {
            final int attributes = suggestionsInfo.getSuggestionsAttributes();
            final boolean isInDictionary = ((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
            final boolean looksLikeTypo = ((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
            final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
            // checker that will probably be in dictionary.
            if (!isInDictionary && looksLikeTypo) {
                createMisspelledSuggestionSpan(editable, suggestionsInfo, spellCheckSpan, offset, length);
            } else {
                // Valid word -- isInDictionary || !looksLikeTypo
                if (mIsSentenceSpellCheckSupported) {
                    // Allow the spell checker to remove existing misspelled span by
                    // overwriting the span over the same place
                    final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
                    final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
                    final int start;
                    final int end;
                    if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
                        start = spellCheckSpanStart + offset;
                        end = start + length;
                    } else {
                        start = spellCheckSpanStart;
                        end = spellCheckSpanEnd;
                    }
                    if (spellCheckSpanStart >= 0 && spellCheckSpanEnd > spellCheckSpanStart && end > start) {
                        final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
                        final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
                        if (tempSuggestionSpan != null) {
                            if (DBG) {
                                Log.i(TAG, "Remove existing misspelled span. " + editable.subSequence(start, end));
                            }
                            editable.removeSpan(tempSuggestionSpan);
                            mSuggestionSpanCache.remove(key);
                        }
                    }
                }
            }
            return spellCheckSpan;
        }
    }
    return null;
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) Editable(android.text.Editable) SuggestionSpan(android.text.style.SuggestionSpan)

Example 88 with SuggestionSpan

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

the class SpellChecker method createMisspelledSuggestionSpan.

private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo, SpellCheckSpan spellCheckSpan, int offset, int length) {
    final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
    final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
    if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
        // span was removed in the meantime
        return;
    final int start;
    final int end;
    if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
        start = spellCheckSpanStart + offset;
        end = start + length;
    } else {
        start = spellCheckSpanStart;
        end = spellCheckSpanEnd;
    }
    final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
    String[] suggestions;
    if (suggestionsCount > 0) {
        suggestions = new String[suggestionsCount];
        for (int i = 0; i < suggestionsCount; i++) {
            suggestions[i] = suggestionsInfo.getSuggestionAt(i);
        }
    } else {
        suggestions = ArrayUtils.emptyArray(String.class);
    }
    SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions, SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
    // to share the logic of word level spell checker and sentence level spell checker
    if (mIsSentenceSpellCheckSupported) {
        final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
        final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
        if (tempSuggestionSpan != null) {
            if (DBG) {
                Log.i(TAG, "Cached span on the same position is cleard. " + editable.subSequence(start, end));
            }
            editable.removeSpan(tempSuggestionSpan);
        }
        mSuggestionSpanCache.put(key, suggestionSpan);
    }
    editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    mTextView.invalidateRegion(start, end, false);
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan)

Example 89 with SuggestionSpan

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

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 90 with SuggestionSpan

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

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)

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