Search in sources :

Example 36 with SuggestionSpan

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

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)

Example 37 with SuggestionSpan

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

the class SuggestionsPopupWindowTest method testEasyCorrect.

@SmallTest
public void testEasyCorrect() {
    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_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
    setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
    onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
    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")));
    onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
    assertSuggestionsPopupIsNotDisplayed();
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("def");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 38 with SuggestionSpan

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

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

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

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

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

the class ViewPropertyAlphaActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_properties);
    getWindow().getDecorView().postDelayed(new Runnable() {

        @Override
        public void run() {
            startAnim(R.id.button);
            startAnim(R.id.textview);
            startAnim(R.id.spantext);
            startAnim(R.id.edittext);
            startAnim(R.id.selectedtext);
            startAnim(R.id.textviewbackground);
            startAnim(R.id.layout);
            startAnim(R.id.imageview);
            startAnim(myViewAlphaDefault);
            startAnim(myViewAlphaHandled);
            EditText selectedText = (EditText) findViewById(R.id.selectedtext);
            selectedText.setSelection(3, 8);
        }
    }, 2000);
    Button invalidator = (Button) findViewById(R.id.invalidateButton);
    invalidator.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.textview).invalidate();
            findViewById(R.id.spantext).invalidate();
        }
    });
    TextView textView = (TextView) findViewById(R.id.spantext);
    if (textView != null) {
        SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
        text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(text);
    }
    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    myViewAlphaDefault = new MyView(this, false);
    myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaDefault);
    myViewAlphaHandled = new MyView(this, true);
    myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaHandled);
}
Also used : EditText(android.widget.EditText) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextView(android.widget.TextView) View(android.view.View) UnderlineSpan(android.text.style.UnderlineSpan) Button(android.widget.Button) TextView(android.widget.TextView) SuggestionSpan(android.text.style.SuggestionSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) LinearLayout(android.widget.LinearLayout) ImageSpan(android.text.style.ImageSpan)

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