Search in sources :

Example 1 with SuggestionSpan

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

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

use of android.text.style.SuggestionSpan in project XobotOS by xamarin.

the class TextView 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) mText;
    SuggestionSpan[] suggestionSpans = spannable.getSpans(getSelectionStart(), 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) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 3 with SuggestionSpan

use of android.text.style.SuggestionSpan in project XobotOS by xamarin.

the class TextView method spanChange.

/**
     * Not private so it can be called from an inner class without going
     * through a thunk.
     */
void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) {
    // XXX Make the start and end move together if this ends up
    // spending too much time invalidating.
    boolean selChanged = false;
    int newSelStart = -1, newSelEnd = -1;
    final InputMethodState ims = mInputMethodState;
    if (what == Selection.SELECTION_END) {
        mHighlightPathBogus = true;
        selChanged = true;
        newSelEnd = newStart;
        if (!isFocused()) {
            mSelectionMoved = true;
        }
        if (oldStart >= 0 || newStart >= 0) {
            invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart);
            registerForPreDraw();
            makeBlink();
        }
    }
    if (what == Selection.SELECTION_START) {
        mHighlightPathBogus = true;
        selChanged = true;
        newSelStart = newStart;
        if (!isFocused()) {
            mSelectionMoved = true;
        }
        if (oldStart >= 0 || newStart >= 0) {
            int end = Selection.getSelectionEnd(buf);
            invalidateCursor(end, oldStart, newStart);
        }
    }
    if (selChanged) {
        if ((buf.getSpanFlags(what) & Spanned.SPAN_INTERMEDIATE) == 0) {
            if (newSelStart < 0) {
                newSelStart = Selection.getSelectionStart(buf);
            }
            if (newSelEnd < 0) {
                newSelEnd = Selection.getSelectionEnd(buf);
            }
            onSelectionChanged(newSelStart, newSelEnd);
        }
    }
    if (what instanceof UpdateAppearance || what instanceof ParagraphStyle || (what instanceof SuggestionSpan && (((SuggestionSpan) what).getFlags() & SuggestionSpan.FLAG_AUTO_CORRECTION) != 0)) {
        if (ims == null || ims.mBatchEditNesting == 0) {
            invalidate();
            mHighlightPathBogus = true;
            checkForResize();
        } else {
            ims.mContentChanged = true;
        }
    }
    if (MetaKeyKeyListener.isMetaTracker(buf, what)) {
        mHighlightPathBogus = true;
        if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) {
            ims.mSelectionModeChanged = true;
        }
        if (Selection.getSelectionStart(buf) >= 0) {
            if (ims == null || ims.mBatchEditNesting == 0) {
                invalidateCursor();
            } else {
                ims.mCursorChanged = true;
            }
        }
    }
    if (what instanceof ParcelableSpan) {
        // the current extract editor would be interested in it.
        if (ims != null && ims.mExtracting != null) {
            if (ims.mBatchEditNesting != 0) {
                if (oldStart >= 0) {
                    if (ims.mChangedStart > oldStart) {
                        ims.mChangedStart = oldStart;
                    }
                    if (ims.mChangedStart > oldEnd) {
                        ims.mChangedStart = oldEnd;
                    }
                }
                if (newStart >= 0) {
                    if (ims.mChangedStart > newStart) {
                        ims.mChangedStart = newStart;
                    }
                    if (ims.mChangedStart > newEnd) {
                        ims.mChangedStart = newEnd;
                    }
                }
            } else {
                if (DEBUG_EXTRACT)
                    Log.v(LOG_TAG, "Span change outside of batch: " + oldStart + "-" + oldEnd + "," + newStart + "-" + newEnd + what);
                ims.mContentChanged = true;
            }
        }
    }
    if (newStart < 0 && what instanceof SpellCheckSpan) {
        getSpellChecker().removeSpellCheckSpan((SpellCheckSpan) what);
    }
}
Also used : UpdateAppearance(android.text.style.UpdateAppearance) SpellCheckSpan(android.text.style.SpellCheckSpan) ParagraphStyle(android.text.style.ParagraphStyle) SuggestionSpan(android.text.style.SuggestionSpan) ParcelableSpan(android.text.ParcelableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 4 with SuggestionSpan

use of android.text.style.SuggestionSpan in project XobotOS by xamarin.

the class SpellChecker method createMisspelledSuggestionSpan.

private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo, SpellCheckSpan spellCheckSpan) {
    final int start = editable.getSpanStart(spellCheckSpan);
    final int end = editable.getSpanEnd(spellCheckSpan);
    // Other suggestion spans may exist on that region, with identical suggestions, filter
    // them out to avoid duplicates. First, filter suggestion spans on that exact region.
    SuggestionSpan[] suggestionSpans = editable.getSpans(start, end, SuggestionSpan.class);
    final int length = suggestionSpans.length;
    for (int i = 0; i < length; i++) {
        final int spanStart = editable.getSpanStart(suggestionSpans[i]);
        final int spanEnd = editable.getSpanEnd(suggestionSpans[i]);
        if (spanStart != start || spanEnd != end) {
            suggestionSpans[i] = null;
            break;
        }
    }
    final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
    String[] suggestions;
    if (suggestionsCount <= 0) {
        // A negative suggestion count is possible
        suggestions = ArrayUtils.emptyArray(String.class);
    } else {
        int numberOfSuggestions = 0;
        suggestions = new String[suggestionsCount];
        for (int i = 0; i < suggestionsCount; i++) {
            final String spellSuggestion = suggestionsInfo.getSuggestionAt(i);
            if (spellSuggestion == null)
                break;
            boolean suggestionFound = false;
            for (int j = 0; j < length && !suggestionFound; j++) {
                if (suggestionSpans[j] == null)
                    break;
                String[] suggests = suggestionSpans[j].getSuggestions();
                for (int k = 0; k < suggests.length; k++) {
                    if (spellSuggestion.equals(suggests[k])) {
                        // The suggestion is already provided by an other SuggestionSpan
                        suggestionFound = true;
                        break;
                    }
                }
            }
            if (!suggestionFound) {
                suggestions[numberOfSuggestions++] = spellSuggestion;
            }
        }
        if (numberOfSuggestions != suggestionsCount) {
            String[] newSuggestions = new String[numberOfSuggestions];
            System.arraycopy(suggestions, 0, newSuggestions, 0, numberOfSuggestions);
            suggestions = newSuggestions;
        }
    }
    SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions, SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
    editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    // TODO limit to the word rectangle region
    mTextView.invalidate();
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan)

Example 5 with SuggestionSpan

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

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