Search in sources :

Example 56 with SuggestionSpan

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

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

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

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

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

the class Editor method downgradeEasyCorrectionSpans.

/**
     * Downgrades to simple suggestions all the easy correction spans that are not a spell check
     * span.
     */
private void downgradeEasyCorrectionSpans() {
    CharSequence text = mTextView.getText();
    if (text instanceof Spannable) {
        Spannable spannable = (Spannable) text;
        SuggestionSpan[] suggestionSpans = spannable.getSpans(0, spannable.length(), SuggestionSpan.class);
        for (int i = 0; i < suggestionSpans.length; i++) {
            int flags = suggestionSpans[i].getFlags();
            if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
                flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
                suggestionSpans[i].setFlags(flags);
            }
        }
    }
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) Paint(android.graphics.Paint)

Example 59 with SuggestionSpan

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

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

use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testSelectionActionMode.

@SmallTest
public void testSelectionActionMode() {
    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(longPressOnTextAtIndex(text.indexOf('e')));
    sleepForFloatingToolbarPopup();
    assertFloatingToolbarContainsItem(getActivity().getString(com.android.internal.R.string.replace));
    sleepForFloatingToolbarPopup();
    clickFloatingToolbarItem(getActivity().getString(com.android.internal.R.string.replace));
    assertSuggestionsPopupIsDisplayed();
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

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