Search in sources :

Example 6 with SuggestionSpan

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

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)

Example 7 with SuggestionSpan

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

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)

Example 8 with SuggestionSpan

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

the class TextView method removeAdjacentSuggestionSpans.

void removeAdjacentSuggestionSpans(final int pos) {
    if (!(mText instanceof Editable))
        return;
    final Editable text = (Editable) mText;
    final SuggestionSpan[] spans = text.getSpans(pos, pos, SuggestionSpan.class);
    final int length = spans.length;
    for (int i = 0; i < length; i++) {
        final int spanStart = text.getSpanStart(spans[i]);
        final int spanEnd = text.getSpanEnd(spans[i]);
        if (spanEnd == pos || spanStart == pos) {
            if (SpellChecker.haveWordBoundariesChanged(text, pos, pos, spanStart, spanEnd)) {
                text.removeSpan(spans[i]);
            }
        }
    }
}
Also used : Editable(android.text.Editable) SuggestionSpan(android.text.style.SuggestionSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 9 with SuggestionSpan

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

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

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

the class SuggestionsPopupWindowTest method testOnTextContextMenuItem.

@SmallTest
public void testOnTextContextMenuItem() {
    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);
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    textView.post(() -> textView.onTextContextMenuItem(TextView.ID_REPLACE));
    getInstrumentation().waitForIdleSync();
    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