下面列出了怎么用android.view.textservice.TextInfo的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
@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;
}
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);
}
@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;
}
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);
}
@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;
}
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);
}
@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;
}
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)));
}
}
/**
* 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;
}
@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);
}
}
@Override
public void onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {
try {
mListener.onGetSentenceSuggestions(
mSession.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit));
} catch (RemoteException e) {
}
}
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);
}
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
long ident = Binder.clearCallingIdentity();
try {
return onGetSuggestionsInternal(textInfo, suggestionsLimit);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/**
* 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;
}
@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);
}
}
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
long ident = Binder.clearCallingIdentity();
try {
return onGetSuggestionsInternal(textInfo, suggestionsLimit);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/**
* 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;
}
@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);
}
}
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
long ident = Binder.clearCallingIdentity();
try {
return onGetSuggestionsInternal(textInfo, suggestionsLimit);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/**
* 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;
}
@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);
}
}
@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);
}
/**
* 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);
}
/**
* 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);
}
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
long ident = Binder.clearCallingIdentity();
try {
return onGetSuggestionsInternal(textInfo, suggestionsLimit);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/**
* 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;
}
@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);
}
}
@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);
}
/**
* 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;
}