Search in sources :

Example 16 with SpellCheckSpan

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

the class SpellChecker method spellCheck.

private void spellCheck() {
    if (mSpellCheckerSession == null)
        return;
    Editable editable = (Editable) mTextView.getText();
    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);
    TextInfo[] textInfos = new TextInfo[mLength];
    int textInfosCount = 0;
    for (int i = 0; i < mLength; i++) {
        final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
        if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress())
            continue;
        final int start = editable.getSpanStart(spellCheckSpan);
        final int end = editable.getSpanEnd(spellCheckSpan);
        // Do not check this word if the user is currently editing it
        final boolean isEditing;
        // Defer spell check when typing a word with an interior apostrophe.
        // TODO: a better solution to this would be to make the word
        // iterator locale-sensitive and include the apostrophe in
        // languages that use it (such as English).
        final boolean apostrophe = (selectionStart == end + 1 && editable.charAt(end) == '\'');
        if (mIsSentenceSpellCheckSupported) {
            // Allow the overlap of the cursor and the first boundary of the spell check span
            // no to skip the spell check of the following word because the
            // following word will never be spell-checked even if the user finishes composing
            isEditing = !apostrophe && (selectionEnd <= start || selectionStart > end);
        } else {
            isEditing = !apostrophe && (selectionEnd < start || selectionStart > end);
        }
        if (start >= 0 && end > start && isEditing) {
            spellCheckSpan.setSpellCheckInProgress(true);
            final TextInfo textInfo = new TextInfo(editable, start, end, mCookie, mIds[i]);
            textInfos[textInfosCount++] = textInfo;
            if (DBG) {
                Log.d(TAG, "create TextInfo: (" + i + "/" + mLength + ") text = " + textInfo.getSequence() + ", cookie = " + mCookie + ", seq = " + mIds[i] + ", sel start = " + selectionStart + ", sel end = " + selectionEnd + ", start = " + start + ", end = " + end);
            }
        }
    }
    if (textInfosCount > 0) {
        if (textInfosCount < textInfos.length) {
            TextInfo[] textInfosCopy = new TextInfo[textInfosCount];
            System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount);
            textInfos = textInfosCopy;
        }
        if (mIsSentenceSpellCheckSupported) {
            mSpellCheckerSession.getSentenceSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE);
        } else {
            mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE, false);
        }
    }
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) TextInfo(android.view.textservice.TextInfo) Editable(android.text.Editable)

Example 17 with SpellCheckSpan

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

the class SpellChecker method onGetSuggestions.

@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
    final Editable editable = (Editable) mTextView.getText();
    for (int i = 0; i < results.length; ++i) {
        final SpellCheckSpan spellCheckSpan = onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
        if (spellCheckSpan != null) {
            // onSpellCheckSpanRemoved will recycle this span in the pool
            editable.removeSpan(spellCheckSpan);
        }
    }
    scheduleNewSpellCheck();
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) Editable(android.text.Editable)

Example 18 with SpellCheckSpan

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

the class SpellChecker method onGetSuggestions.

@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
    final Editable editable = (Editable) mTextView.getText();
    for (int i = 0; i < results.length; ++i) {
        final SpellCheckSpan spellCheckSpan = onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
        if (spellCheckSpan != null) {
            // onSpellCheckSpanRemoved will recycle this span in the pool
            editable.removeSpan(spellCheckSpan);
        }
    }
    scheduleNewSpellCheck();
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) Editable(android.text.Editable)

Example 19 with SpellCheckSpan

use of android.text.style.SpellCheckSpan 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 20 with SpellCheckSpan

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

the class SpellChecker method spellCheck.

private void spellCheck() {
    if (mSpellCheckerSession == null)
        return;
    Editable editable = (Editable) mTextView.getText();
    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);
    TextInfo[] textInfos = new TextInfo[mLength];
    int textInfosCount = 0;
    for (int i = 0; i < mLength; i++) {
        final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
        if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress())
            continue;
        final int start = editable.getSpanStart(spellCheckSpan);
        final int end = editable.getSpanEnd(spellCheckSpan);
        // Do not check this word if the user is currently editing it
        final boolean isEditing;
        if (mIsSentenceSpellCheckSupported) {
            // Allow the overlap of the cursor and the first boundary of the spell check span
            // no to skip the spell check of the following word because the
            // following word will never be spell-checked even if the user finishes composing
            isEditing = selectionEnd <= start || selectionStart > end;
        } else {
            isEditing = selectionEnd < start || selectionStart > end;
        }
        if (start >= 0 && end > start && isEditing) {
            final String word = (editable instanceof SpannableStringBuilder) ? ((SpannableStringBuilder) editable).substring(start, end) : editable.subSequence(start, end).toString();
            spellCheckSpan.setSpellCheckInProgress(true);
            textInfos[textInfosCount++] = new TextInfo(word, mCookie, mIds[i]);
            if (DBG) {
                Log.d(TAG, "create TextInfo: (" + i + "/" + mLength + ")" + word + ", cookie = " + mCookie + ", seq = " + mIds[i] + ", sel start = " + selectionStart + ", sel end = " + selectionEnd + ", start = " + start + ", end = " + end);
            }
        }
    }
    if (textInfosCount > 0) {
        if (textInfosCount < textInfos.length) {
            TextInfo[] textInfosCopy = new TextInfo[textInfosCount];
            System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount);
            textInfos = textInfosCopy;
        }
        if (mIsSentenceSpellCheckSupported) {
            mSpellCheckerSession.getSentenceSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE);
        } else {
            mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE, false);
        }
    }
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) TextInfo(android.view.textservice.TextInfo) Editable(android.text.Editable) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpellCheckSpan (android.text.style.SpellCheckSpan)41 Editable (android.text.Editable)26 Paint (android.graphics.Paint)7 ParcelableSpan (android.text.ParcelableSpan)7 TextPaint (android.text.TextPaint)7 ParagraphStyle (android.text.style.ParagraphStyle)7 SuggestionSpan (android.text.style.SuggestionSpan)7 UpdateAppearance (android.text.style.UpdateAppearance)7 SuggestionsInfo (android.view.textservice.SuggestionsInfo)7 TextInfo (android.view.textservice.TextInfo)7 CharacterStyle (android.text.style.CharacterStyle)6 SentenceSuggestionsInfo (android.view.textservice.SentenceSuggestionsInfo)6 SpannableStringBuilder (android.text.SpannableStringBuilder)1