Search in sources :

Example 96 with SuggestionSpan

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

the class SuggestionsPopupWindowTest method testSuggestionItems.

@SmallTest
public void testSuggestionItems() {
    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);
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("DEF");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Select an item.
    clickSuggestionsPopupItem("DEF");
    assertSuggestionsPopupIsNotDisplayed();
    onView(withId(R.id.textview)).check(matches(withText("abc DEF ghi")));
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("def");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Delete
    clickSuggestionsPopupItem(getActivity().getString(com.android.internal.R.string.delete));
    assertSuggestionsPopupIsNotDisplayed();
    onView(withId(R.id.textview)).check(matches(withText("abc ghi")));
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 97 with SuggestionSpan

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

the class SuggestionsPopupWindowTest method testMisspelled.

@SmallTest
public void testMisspelled() {
    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_MISSPELLED);
    setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("DEF");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.addToDictionary));
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Click "Add to dictionary".
    clickSuggestionsPopupItem(getActivity().getString(com.android.internal.R.string.addToDictionary));
// TODO: Check if add to dictionary dialog is displayed.
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 98 with SuggestionSpan

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

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

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

the class Editor method replaceWithSuggestion.

private void replaceWithSuggestion(@NonNull final SuggestionInfo suggestionInfo) {
    final SuggestionSpan targetSuggestionSpan = findEquivalentSuggestionSpan(suggestionInfo.mSuggestionSpanInfo);
    if (targetSuggestionSpan == null) {
        // Span has been removed
        return;
    }
    final Editable editable = (Editable) mTextView.getText();
    final int spanStart = editable.getSpanStart(targetSuggestionSpan);
    final int spanEnd = editable.getSpanEnd(targetSuggestionSpan);
    if (spanStart < 0 || spanEnd <= spanStart) {
        // Span has been removed
        return;
    }
    final String originalText = TextUtils.substring(editable, spanStart, spanEnd);
    // SuggestionSpans are removed by replace: save them before
    SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd, SuggestionSpan.class);
    final int length = suggestionSpans.length;
    int[] suggestionSpansStarts = new int[length];
    int[] suggestionSpansEnds = new int[length];
    int[] suggestionSpansFlags = new int[length];
    for (int i = 0; i < length; i++) {
        final SuggestionSpan suggestionSpan = suggestionSpans[i];
        suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan);
        suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan);
        suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan);
        // Remove potential misspelled flags
        int suggestionSpanFlags = suggestionSpan.getFlags();
        if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) != 0) {
            suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED;
            suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
            suggestionSpan.setFlags(suggestionSpanFlags);
        }
    }
    // Notify source IME of the suggestion pick. Do this before swapping texts.
    targetSuggestionSpan.notifySelection(mTextView.getContext(), originalText, suggestionInfo.mSuggestionIndex);
    // Swap text content between actual text and Suggestion span
    final int suggestionStart = suggestionInfo.mSuggestionStart;
    final int suggestionEnd = suggestionInfo.mSuggestionEnd;
    final String suggestion = suggestionInfo.mText.subSequence(suggestionStart, suggestionEnd).toString();
    mTextView.replaceText_internal(spanStart, spanEnd, suggestion);
    String[] suggestions = targetSuggestionSpan.getSuggestions();
    suggestions[suggestionInfo.mSuggestionIndex] = originalText;
    // Restore previous SuggestionSpans
    final int lengthDelta = suggestion.length() - (spanEnd - spanStart);
    for (int i = 0; i < length; i++) {
        // way to assign them a valid range after replacement
        if (suggestionSpansStarts[i] <= spanStart && suggestionSpansEnds[i] >= spanEnd) {
            mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i], suggestionSpansEnds[i] + lengthDelta, suggestionSpansFlags[i]);
        }
    }
    // Move cursor at the end of the replaced word
    final int newCursorPosition = spanEnd + lengthDelta;
    mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition);
}
Also used : Editable(android.text.Editable) SuggestionSpan(android.text.style.SuggestionSpan) Paint(android.graphics.Paint)

Example 100 with SuggestionSpan

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

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)

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