android.view.textservice.TextInfo Java Examples

The following examples show how to use android.view.textservice.TextInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextInfoCompatUtilsTests.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
public void testGetCharSequence() {
    final SpannableString text = new SpannableString(TEST_TEXT);
    text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END,
            TEST_STYLE_SPAN_FLAGS);
    text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END,
            TEST_URL_SPAN_FLAGS);

    final TextInfo textInfo = TextInfoCompatUtils.newInstance(text,
            TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE,
            TEST_SEQUENCE_NUMBER);
    final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START,
            TEST_CHAR_SEQUENCE_END);
    final CharSequence actualCharSequence =
            TextInfoCompatUtils.getCharSequenceOrString(textInfo);

    // This should be valid even if TextInfo#getCharSequence is not supported.
    assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence));

    if (TextInfoCompatUtils.isCharSequenceSupported()) {
        // This is valid only if TextInfo#getCharSequence is supported.
        assertTrue("should be Spanned", actualCharSequence instanceof Spanned);
        assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence)));
    }
}
 
Example #2
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 6 votes vote down vote up
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos, suggestionsLimit);
    if (retval == null || retval.length != textInfos.length) {
        return retval;
    }
    for (int i = 0; i < retval.length; ++i) {
        final SentenceSuggestionsInfo tempSsi =
                fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
        if (tempSsi != null) {
            retval[i] = tempSsi;
        }
    }
    return retval;
}
 
Example #3
Source File: SentenceLevelAdapter.java    From Android-Keyboard with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #4
Source File: SentenceLevelAdapter.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos, suggestionsLimit);
    if (retval == null || retval.length != textInfos.length) {
        return retval;
    }
    for (int i = 0; i < retval.length; ++i) {
        final SentenceSuggestionsInfo tempSsi =
                fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
        if (tempSsi != null) {
            retval[i] = tempSsi;
        }
    }
    return retval;
}
 
Example #6
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos, suggestionsLimit);
    if (retval == null || retval.length != textInfos.length) {
        return retval;
    }
    for (int i = 0; i < retval.length; ++i) {
        final SentenceSuggestionsInfo tempSsi =
                fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
        if (tempSsi != null) {
            retval[i] = tempSsi;
        }
    }
    return retval;
}
 
Example #7
Source File: SentenceLevelAdapter.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example #8
Source File: SentenceLevelAdapter.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #9
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos, suggestionsLimit);
    if (retval == null || retval.length != textInfos.length) {
        return retval;
    }
    for (int i = 0; i < retval.length; ++i) {
        final SentenceSuggestionsInfo tempSsi =
                fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
        if (tempSsi != null) {
            retval[i] = tempSsi;
        }
    }
    return retval;
}
 
Example #10
Source File: SpellCheckerSessionBridge.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Queries the input text against the SpellCheckerSession.
 * @param text Text to be queried.
 */
@CalledByNative
private void requestTextCheck(String text) {
    // SpellCheckerSession thinks that any word ending with a period is a typo.
    // We trim the period off before sending the text for spellchecking in order to avoid
    // unnecessary red underlines when the user ends a sentence with a period.
    // Filed as an Android bug here: https://code.google.com/p/android/issues/detail?id=183294
    if (text.endsWith(".")) {
        text = text.substring(0, text.length() - 1);
    }
    mStartMs = SystemClock.elapsedRealtime();
    mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(text)}, 0);
}
 
Example #11
Source File: SpellCheckerSessionBridge.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Queries the input text against the SpellCheckerSession.
 * @param text Text to be queried.
 */
@CalledByNative
private void requestTextCheck(String text) {
    // SpellCheckerSession thinks that any word ending with a period is a typo.
    // We trim the period off before sending the text for spellchecking in order to avoid
    // unnecessary red underlines when the user ends a sentence with a period.
    // Filed as an Android bug here: https://code.google.com/p/android/issues/detail?id=183294
    if (text.endsWith(".")) {
        text = text.substring(0, text.length() - 1);
    }
    mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(text)}, 0);
}
 
Example #12
Source File: TextInfoCompatUtils.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@UsedForTesting
public static TextInfo newInstance(CharSequence charSequence, int start, int end, int cookie,
        int sequenceNumber) {
    if (TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE != null) {
        return (TextInfo) CompatUtils.newInstance(TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE,
                charSequence, start, end, cookie, sequenceNumber);
    }
    return new TextInfo(charSequence.subSequence(start, end).toString(), cookie,
            sequenceNumber);
}
 
Example #13
Source File: AndroidWordLevelSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #14
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #15
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from
 * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's
 * using private variables.
 * The default implementation splits the input text to words and returns
 * {@link SentenceSuggestionsInfo} which contains suggestions for each word.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    SentenceLevelAdapter sentenceLevelAdapter;
    synchronized(this) {
        sentenceLevelAdapter = mSentenceLevelAdapter;
        if (sentenceLevelAdapter == null) {
            final String localeStr = getLocale();
            if (!TextUtils.isEmpty(localeStr)) {
                sentenceLevelAdapter = new SentenceLevelAdapter(mResources,
                        new Locale(localeStr));
                mSentenceLevelAdapter = sentenceLevelAdapter;
            }
        }
    }
    if (sentenceLevelAdapter == null) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                sentenceLevelAdapter.getSplitWords(textInfos[i]);
        final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
                textInfoParams.mItems;
        final int itemsSize = mItems.size();
        final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
        for (int j = 0; j < itemsSize; ++j) {
            splitTextInfos[j] = mItems.get(j).mTextInfo;
        }
        retval[i] = SentenceLevelAdapter.reconstructSuggestions(
                textInfoParams, onGetSuggestionsMultiple(
                        splitTextInfos, suggestionsLimit, true));
    }
    return retval;
}
 
Example #16
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from
 * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's
 * using private variables.
 * The default implementation splits the input text to words and returns
 * {@link SentenceSuggestionsInfo} which contains suggestions for each word.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    SentenceLevelAdapter sentenceLevelAdapter;
    synchronized(this) {
        sentenceLevelAdapter = mSentenceLevelAdapter;
        if (sentenceLevelAdapter == null) {
            final String localeStr = getLocale();
            if (!TextUtils.isEmpty(localeStr)) {
                sentenceLevelAdapter = new SentenceLevelAdapter(mResources,
                        new Locale(localeStr));
                mSentenceLevelAdapter = sentenceLevelAdapter;
            }
        }
    }
    if (sentenceLevelAdapter == null) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                sentenceLevelAdapter.getSplitWords(textInfos[i]);
        final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
                textInfoParams.mItems;
        final int itemsSize = mItems.size();
        final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
        for (int j = 0; j < itemsSize; ++j) {
            splitTextInfos[j] = mItems.get(j).mTextInfo;
        }
        retval[i] = SentenceLevelAdapter.reconstructSuggestions(
                textInfoParams, onGetSuggestionsMultiple(
                        splitTextInfos, suggestionsLimit, true));
    }
    return retval;
}
 
Example #17
Source File: AndroidWordLevelSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #18
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #19
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from
 * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's
 * using private variables.
 * The default implementation splits the input text to words and returns
 * {@link SentenceSuggestionsInfo} which contains suggestions for each word.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    SentenceLevelAdapter sentenceLevelAdapter;
    synchronized(this) {
        sentenceLevelAdapter = mSentenceLevelAdapter;
        if (sentenceLevelAdapter == null) {
            final String localeStr = getLocale();
            if (!TextUtils.isEmpty(localeStr)) {
                sentenceLevelAdapter = new SentenceLevelAdapter(mResources,
                        new Locale(localeStr));
                mSentenceLevelAdapter = sentenceLevelAdapter;
            }
        }
    }
    if (sentenceLevelAdapter == null) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                sentenceLevelAdapter.getSplitWords(textInfos[i]);
        final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
                textInfoParams.mItems;
        final int itemsSize = mItems.size();
        final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
        for (int j = 0; j < itemsSize; ++j) {
            splitTextInfos[j] = mItems.get(j).mTextInfo;
        }
        retval[i] = SentenceLevelAdapter.reconstructSuggestions(
                textInfoParams, onGetSuggestionsMultiple(
                        splitTextInfos, suggestionsLimit, true));
    }
    return retval;
}
 
Example #20
Source File: TextInfoCompatUtils.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@UsedForTesting
public static TextInfo newInstance(CharSequence charSequence, int start, int end, int cookie,
        int sequenceNumber) {
    if (TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE != null) {
        return (TextInfo) CompatUtils.newInstance(TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE,
                charSequence, start, end, cookie, sequenceNumber);
    }
    return new TextInfo(charSequence.subSequence(start, end).toString(), cookie,
            sequenceNumber);
}
 
Example #21
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #22
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * A batch process of onGetSuggestions.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @param sequentialWords true if textInfos can be treated as sequential words.
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit, boolean sequentialWords) {
    final int length = textInfos.length;
    final SuggestionsInfo[] retval = new SuggestionsInfo[length];
    for (int i = 0; i < length; ++i) {
        retval[i] = onGetSuggestions(textInfos[i], suggestionsLimit);
        retval[i].setCookieAndSequence(
                textInfos[i].getCookie(), textInfos[i].getSequence());
    }
    return retval;
}
 
Example #23
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {
    try {
        mListener.onGetSentenceSuggestions(
                mSession.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit));
    } catch (RemoteException e) {
    }
}
 
Example #24
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) {
    final WordIterator wordIterator = mWordIterator;
    final CharSequence originalText = originalTextInfo.getText();
    final int cookie = originalTextInfo.getCookie();
    final int start = 0;
    final int end = originalText.length();
    final ArrayList<SentenceWordItem> wordItems = new ArrayList<SentenceWordItem>();
    wordIterator.setCharSequence(originalText, 0, originalText.length());
    int wordEnd = wordIterator.following(start);
    int wordStart = wordIterator.getBeginning(wordEnd);
    if (DBG) {
        Log.d(TAG, "iterator: break: ---- 1st word start = " + wordStart + ", end = "
                + wordEnd + "\n" + originalText);
    }
    while (wordStart <= end && wordEnd != BreakIterator.DONE
            && wordStart != BreakIterator.DONE) {
        if (wordEnd >= start && wordEnd > wordStart) {
            final CharSequence query = originalText.subSequence(wordStart, wordEnd);
            final TextInfo ti = new TextInfo(query, 0, query.length(), cookie,
                    query.hashCode());
            wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd));
            if (DBG) {
                Log.d(TAG, "Adapter: word (" + (wordItems.size() - 1) + ") " + query);
            }
        }
        wordEnd = wordIterator.following(wordEnd);
        if (wordEnd == BreakIterator.DONE) {
            break;
        }
        wordStart = wordIterator.getBeginning(wordEnd);
    }
    return new SentenceTextInfoParams(originalTextInfo, wordItems);
}
 
Example #25
Source File: AndroidWordLevelSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #26
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onGetSuggestionsMultiple(
        TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) {
    int pri = Process.getThreadPriority(Process.myTid());
    try {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        mListener.onGetSuggestions(
                mSession.onGetSuggestionsMultiple(
                        textInfos, suggestionsLimit, sequentialWords));
    } catch (RemoteException e) {
    } finally {
        Process.setThreadPriority(pri);
    }
}
 
Example #27
Source File: AndroidWordLevelSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #28
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from
 * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's
 * using private variables.
 * The default implementation splits the input text to words and returns
 * {@link SentenceSuggestionsInfo} which contains suggestions for each word.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    SentenceLevelAdapter sentenceLevelAdapter;
    synchronized(this) {
        sentenceLevelAdapter = mSentenceLevelAdapter;
        if (sentenceLevelAdapter == null) {
            final String localeStr = getLocale();
            if (!TextUtils.isEmpty(localeStr)) {
                sentenceLevelAdapter = new SentenceLevelAdapter(mResources,
                        new Locale(localeStr));
                mSentenceLevelAdapter = sentenceLevelAdapter;
            }
        }
    }
    if (sentenceLevelAdapter == null) {
        return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo();
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                sentenceLevelAdapter.getSplitWords(textInfos[i]);
        final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
                textInfoParams.mItems;
        final int itemsSize = mItems.size();
        final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
        for (int j = 0; j < itemsSize; ++j) {
            splitTextInfos[j] = mItems.get(j).mTextInfo;
        }
        retval[i] = SentenceLevelAdapter.reconstructSuggestions(
                textInfoParams, onGetSuggestionsMultiple(
                        splitTextInfos, suggestionsLimit, true));
    }
    return retval;
}
 
Example #29
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}
 
Example #30
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Get sentence suggestions for specified texts in an array of TextInfo.
 * The default implementation splits the input text to words and returns
 * {@link SentenceSuggestionsInfo} which contains suggestions for each word.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * When you override this method, make sure that suggestionsLimit is applied to suggestions
 * that share the same start position and length.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit) {
    if (textInfos == null || textInfos.length == 0) {
        return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
    }
    if (DBG) {
        Log.d(TAG, "onGetSentenceSuggestionsMultiple: + " + textInfos.length + ", "
                + suggestionsLimit);
    }
    if (mSentenceLevelAdapter == null) {
        synchronized(this) {
            if (mSentenceLevelAdapter == null) {
                final String localeStr = getLocale();
                if (!TextUtils.isEmpty(localeStr)) {
                    mSentenceLevelAdapter = new SentenceLevelAdapter(new Locale(localeStr));
                }
            }
        }
    }
    if (mSentenceLevelAdapter == null) {
        return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
    }
    final int infosSize = textInfos.length;
    final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
    for (int i = 0; i < infosSize; ++i) {
        final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
                mSentenceLevelAdapter.getSplitWords(textInfos[i]);
        final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
                textInfoParams.mItems;
        final int itemsSize = mItems.size();
        final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
        for (int j = 0; j < itemsSize; ++j) {
            splitTextInfos[j] = mItems.get(j).mTextInfo;
        }
        retval[i] = SentenceLevelAdapter.reconstructSuggestions(
                textInfoParams, onGetSuggestionsMultiple(
                        splitTextInfos, suggestionsLimit, true));
    }
    return retval;
}