aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java
diff options
context:
space:
mode:
authorYohei Yukawa <yukawa@google.com>2014-07-20 00:27:27 +0900
committerYohei Yukawa <yukawa@google.com>2014-07-20 02:16:11 +0000
commit86f36003fd4397143bd37938dda029e5707634af (patch)
tree44464146de8c5fd1af2074feffb6366a2eacdc33 /java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java
parent22ba22f32dc9d59a0dccc8d1bca7aaee90e90b2a (diff)
downloadlatinime-86f36003fd4397143bd37938dda029e5707634af.tar.gz
latinime-86f36003fd4397143bd37938dda029e5707634af.tar.xz
latinime-86f36003fd4397143bd37938dda029e5707634af.zip
Use CharSequence for spell checker to keep spans preserved
This is a ground work to take per word locale information into consideration in the spell checker. This CL is supposed to change no user visible behavior. With this CL, the spell checker session is able to read span information if necessary. BUG: 16029304 Change-Id: Icb1e1ecdf40fe0445e14565b685b1b878b746210
Diffstat (limited to 'java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java')
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java43
1 files changed, 24 insertions, 19 deletions
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java
index 38d720664..14ab2dbbf 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java
@@ -24,7 +24,9 @@ import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
+import com.android.inputmethod.compat.TextInfoCompatUtils;
import com.android.inputmethod.latin.PrevWordsInfo;
+import com.android.inputmethod.latin.utils.StringUtils;
import java.util.ArrayList;
import java.util.Locale;
@@ -42,15 +44,15 @@ public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheck
private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(TextInfo ti,
SentenceSuggestionsInfo ssi) {
- final String typedText = ti.getText();
- if (!typedText.contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
+ 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<>();
- String currentWord = null;
+ CharSequence currentWord = null;
for (int i = 0; i < N; ++i) {
final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
final int flags = si.getSuggestionsAttributes();
@@ -59,32 +61,33 @@ public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheck
}
final int offset = ssi.getOffsetAt(i);
final int length = ssi.getLengthAt(i);
- final String subText = typedText.substring(offset, offset + length);
+ final CharSequence subText = typedText.subSequence(offset, offset + length);
final PrevWordsInfo prevWordsInfo =
new PrevWordsInfo(new PrevWordsInfo.WordInfo(currentWord));
currentWord = subText;
- if (!subText.contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
+ if (!subText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
continue;
}
- final String[] splitTexts =
- subText.split(AndroidSpellCheckerService.SINGLE_QUOTE, -1);
+ final CharSequence[] splitTexts = StringUtils.split(subText,
+ AndroidSpellCheckerService.SINGLE_QUOTE,
+ true /* preserveTrailingEmptySegments */ );
if (splitTexts == null || splitTexts.length <= 1) {
continue;
}
final int splitNum = splitTexts.length;
for (int j = 0; j < splitNum; ++j) {
- final String splitText = splitTexts[j];
+ final CharSequence splitText = splitTexts[j];
if (TextUtils.isEmpty(splitText)) {
continue;
}
- if (mSuggestionsCache.getSuggestionsFromCache(splitText, prevWordsInfo) == null) {
+ if (mSuggestionsCache.getSuggestionsFromCache(splitText.toString(), prevWordsInfo)
+ == 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);
+ 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 + ", "
@@ -194,20 +197,22 @@ public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheck
final int length = textInfos.length;
final SuggestionsInfo[] retval = new SuggestionsInfo[length];
for (int i = 0; i < length; ++i) {
- final String prevWord;
+ final CharSequence prevWord;
if (sequentialWords && i > 0) {
- final String prevWordCandidate = textInfos[i - 1].getText();
- // Note that an empty string would be used to indicate the initial word
- // in the future.
- prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate;
+ 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 PrevWordsInfo prevWordsInfo =
new PrevWordsInfo(new PrevWordsInfo.WordInfo(prevWord));
- retval[i] = onGetSuggestionsInternal(textInfos[i], prevWordsInfo, suggestionsLimit);
- retval[i].setCookieAndSequence(textInfos[i].getCookie(),
- textInfos[i].getSequence());
+ final TextInfo textInfo = textInfos[i];
+ retval[i] = onGetSuggestionsInternal(textInfo, prevWordsInfo, suggestionsLimit);
+ retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence());
}
return retval;
} finally {