Search in sources :

Example 6 with SuggestionsInfo

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

the class SpellChecker method onGetSentenceSuggestions.

@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
    final Editable editable = (Editable) mTextView.getText();
    for (int i = 0; i < results.length; ++i) {
        final SentenceSuggestionsInfo ssi = results[i];
        if (ssi == null) {
            continue;
        }
        SpellCheckSpan spellCheckSpan = null;
        for (int j = 0; j < ssi.getSuggestionsCount(); ++j) {
            final SuggestionsInfo suggestionsInfo = ssi.getSuggestionsInfoAt(j);
            if (suggestionsInfo == null) {
                continue;
            }
            final int offset = ssi.getOffsetAt(j);
            final int length = ssi.getLengthAt(j);
            final SpellCheckSpan scs = onGetSuggestionsInternal(suggestionsInfo, offset, length);
            if (spellCheckSpan == null && scs != null) {
                // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
                // SentenceSuggestionsInfo. Removal is deferred after this loop.
                spellCheckSpan = scs;
            }
        }
        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) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SuggestionsInfo(android.view.textservice.SuggestionsInfo)

Example 7 with SuggestionsInfo

use of android.view.textservice.SuggestionsInfo in project android_frameworks_base by crdroidandroid.

the class SpellChecker method onGetSentenceSuggestions.

@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
    final Editable editable = (Editable) mTextView.getText();
    for (int i = 0; i < results.length; ++i) {
        final SentenceSuggestionsInfo ssi = results[i];
        if (ssi == null) {
            continue;
        }
        SpellCheckSpan spellCheckSpan = null;
        for (int j = 0; j < ssi.getSuggestionsCount(); ++j) {
            final SuggestionsInfo suggestionsInfo = ssi.getSuggestionsInfoAt(j);
            if (suggestionsInfo == null) {
                continue;
            }
            final int offset = ssi.getOffsetAt(j);
            final int length = ssi.getLengthAt(j);
            final SpellCheckSpan scs = onGetSuggestionsInternal(suggestionsInfo, offset, length);
            if (spellCheckSpan == null && scs != null) {
                // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
                // SentenceSuggestionsInfo. Removal is deferred after this loop.
                spellCheckSpan = scs;
            }
        }
        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) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SuggestionsInfo(android.view.textservice.SuggestionsInfo)

Example 8 with SuggestionsInfo

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

the class SentenceLevelAdapter method reconstructSuggestions.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static SentenceSuggestionsInfo reconstructSuggestions(SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (originalTextInfoParams == null) {
        return null;
    }
    final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
    final int originalSequence = originalTextInfoParams.mOriginalTextInfo.getSequence();
    final int querySize = originalTextInfoParams.mSize;
    final int[] offsets = new int[querySize];
    final int[] lengths = new int[querySize];
    final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
    for (int i = 0; i < querySize; ++i) {
        final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
        SuggestionsInfo result = null;
        for (int j = 0; j < results.length; ++j) {
            final SuggestionsInfo cur = results[j];
            if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
                result = cur;
                result.setCookieAndSequence(originalCookie, originalSequence);
                break;
            }
        }
        offsets[i] = item.mStart;
        lengths[i] = item.mLength;
        reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
Also used : SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SuggestionsInfo(android.view.textservice.SuggestionsInfo) TargetApi(android.annotation.TargetApi)

Example 9 with SuggestionsInfo

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

the class AndroidSpellCheckerSession method fixWronglyInvalidatedWordWithSingleQuote.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(TextInfo ti, SentenceSuggestionsInfo ssi) {
    final CharSequence typedText = TextInfoCompatUtils.getCharSequenceOrString(ti);
    if (!typedText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
        return null;
    }
    final int N = ssi.getSuggestionsCount();
    final ArrayList<Integer> additionalOffsets = new ArrayList<>();
    final ArrayList<Integer> additionalLengths = new ArrayList<>();
    final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = new ArrayList<>();
    CharSequence currentWord = null;
    for (int i = 0; i < N; ++i) {
        final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
        final int flags = si.getSuggestionsAttributes();
        if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) {
            continue;
        }
        final int offset = ssi.getOffsetAt(i);
        final int length = ssi.getLengthAt(i);
        final CharSequence subText = typedText.subSequence(offset, offset + length);
        final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(currentWord));
        currentWord = subText;
        if (!subText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
            continue;
        }
        // Split preserving spans.
        final CharSequence[] splitTexts = SpannableStringUtils.split(subText, AndroidSpellCheckerService.SINGLE_QUOTE, true);
        if (splitTexts == null || splitTexts.length <= 1) {
            continue;
        }
        final int splitNum = splitTexts.length;
        for (int j = 0; j < splitNum; ++j) {
            final CharSequence splitText = splitTexts[j];
            if (TextUtils.isEmpty(splitText)) {
                continue;
            }
            if (mSuggestionsCache.getSuggestionsFromCache(splitText.toString()) == null) {
                continue;
            }
            final int newLength = splitText.length();
            // Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO
            final int newFlags = 0;
            final SuggestionsInfo newSi = new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY);
            newSi.setCookieAndSequence(si.getCookie(), si.getSequence());
            if (DBG) {
                Log.d(TAG, "Override and remove old span over: " + splitText + ", " + offset + "," + newLength);
            }
            additionalOffsets.add(offset);
            additionalLengths.add(newLength);
            additionalSuggestionsInfos.add(newSi);
        }
    }
    final int additionalSize = additionalOffsets.size();
    if (additionalSize <= 0) {
        return null;
    }
    final int suggestionsSize = N + additionalSize;
    final int[] newOffsets = new int[suggestionsSize];
    final int[] newLengths = new int[suggestionsSize];
    final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize];
    int i;
    for (i = 0; i < N; ++i) {
        newOffsets[i] = ssi.getOffsetAt(i);
        newLengths[i] = ssi.getLengthAt(i);
        newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i);
    }
    for (; i < suggestionsSize; ++i) {
        newOffsets[i] = additionalOffsets.get(i - N);
        newLengths[i] = additionalLengths.get(i - N);
        newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N);
    }
    return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths);
}
Also used : ArrayList(java.util.ArrayList) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SentenceSuggestionsInfo(android.view.textservice.SentenceSuggestionsInfo) SuggestionsInfo(android.view.textservice.SuggestionsInfo) NgramContext(com.android.inputmethod.latin.NgramContext) TargetApi(android.annotation.TargetApi)

Example 10 with SuggestionsInfo

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

Aggregations

SuggestionsInfo (android.view.textservice.SuggestionsInfo)11 SentenceSuggestionsInfo (android.view.textservice.SentenceSuggestionsInfo)9 Editable (android.text.Editable)7 SpellCheckSpan (android.text.style.SpellCheckSpan)7 TargetApi (android.annotation.TargetApi)2 NgramContext (com.android.inputmethod.latin.NgramContext)2 TextInfo (android.view.textservice.TextInfo)1 Keyboard (com.android.inputmethod.keyboard.Keyboard)1 WordComposer (com.android.inputmethod.latin.WordComposer)1 SuggestionResults (com.android.inputmethod.latin.utils.SuggestionResults)1 ArrayList (java.util.ArrayList)1