Search in sources :

Example 6 with TextInfo

use of android.view.textservice.TextInfo 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)

Example 7 with TextInfo

use of android.view.textservice.TextInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class AndroidSpellCheckerSession method onGetSuggestionsMultiple.

@Override
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) {
    long ident = Binder.clearCallingIdentity();
    try {
        final int length = textInfos.length;
        final SuggestionsInfo[] retval = new SuggestionsInfo[length];
        for (int i = 0; i < length; ++i) {
            final CharSequence prevWord;
            if (sequentialWords && i > 0) {
                final TextInfo prevTextInfo = textInfos[i - 1];
                final CharSequence prevWordCandidate = TextInfoCompatUtils.getCharSequenceOrString(prevTextInfo);
                // Note that an empty string would be used to indicate the initial word
                // in the future.
                prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate;
            } else {
                prevWord = null;
            }
            final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(prevWord));
            final TextInfo textInfo = textInfos[i];
            retval[i] = onGetSuggestionsInternal(textInfo, ngramContext, suggestionsLimit);
            retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence());
        }
        return retval;
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
Also used : TextInfo(android.view.textservice.TextInfo) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SuggestionsInfo(android.view.textservice.SuggestionsInfo) NgramContext(com.android.inputmethod.latin.NgramContext)

Example 8 with TextInfo

use of android.view.textservice.TextInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SentenceLevelAdapter method getSplitWords.

public SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) {
    final WordIterator wordIterator = mWordIterator;
    final CharSequence originalText = TextInfoCompatUtils.getCharSequenceOrString(originalTextInfo);
    final int cookie = originalTextInfo.getCookie();
    final int start = -1;
    final int end = originalText.length();
    final ArrayList<SentenceWordItem> wordItems = new ArrayList<>();
    int wordStart = wordIterator.getBeginningOfNextWord(originalText, start);
    int wordEnd = wordIterator.getEndOfWord(originalText, wordStart);
    while (wordStart <= end && wordEnd != -1 && wordStart != -1) {
        if (wordEnd >= start && wordEnd > wordStart) {
            final TextInfo ti = TextInfoCompatUtils.newInstance(originalText, wordStart, wordEnd, cookie, originalText.subSequence(wordStart, wordEnd).hashCode());
            wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd));
        }
        wordStart = wordIterator.getBeginningOfNextWord(originalText, wordEnd);
        if (wordStart == -1) {
            break;
        }
        wordEnd = wordIterator.getEndOfWord(originalText, wordStart);
    }
    return new SentenceTextInfoParams(originalTextInfo, wordItems);
}
Also used : ArrayList(java.util.ArrayList) TextInfo(android.view.textservice.TextInfo)

Example 9 with TextInfo

use of android.view.textservice.TextInfo in project android_frameworks_base by AOSPA.

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

use of android.view.textservice.TextInfo in project android_frameworks_base by DirtyUnicorns.

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)

Aggregations

TextInfo (android.view.textservice.TextInfo)10 Editable (android.text.Editable)7 SpellCheckSpan (android.text.style.SpellCheckSpan)7 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 SentenceSuggestionsInfo (android.view.textservice.SentenceSuggestionsInfo)1 SuggestionsInfo (android.view.textservice.SuggestionsInfo)1 NgramContext (com.android.inputmethod.latin.NgramContext)1 ArrayList (java.util.ArrayList)1