Support sentence level spell check in sample code of the spell checker
Bug: 6136149 Change-Id: I1519258dd3ce95ad01e50a75f802469737bef3c4
This commit is contained in:
@@ -16,20 +16,31 @@
|
||||
|
||||
package com.example.android.samplespellcheckerservice;
|
||||
|
||||
import android.os.Build;
|
||||
import android.service.textservice.SpellCheckerService;
|
||||
import android.util.Log;
|
||||
import android.view.textservice.SentenceSuggestionsInfo;
|
||||
import android.view.textservice.SuggestionsInfo;
|
||||
import android.view.textservice.TextInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SampleSpellCheckerService extends SpellCheckerService {
|
||||
private static final String TAG = SampleSpellCheckerService.class.getSimpleName();
|
||||
private static final boolean DBG = true;
|
||||
|
||||
@Override
|
||||
public Session createSession() {
|
||||
return new AndroidSpellCheckerSession();
|
||||
}
|
||||
|
||||
private static class AndroidSpellCheckerSession extends Session {
|
||||
|
||||
private boolean isSentenceSpellCheckApiSupported() {
|
||||
// Note that the sentence level spell check APIs work on JB or later.
|
||||
return Build.VERSION.SDK_INT >= 16;
|
||||
}
|
||||
|
||||
private String mLocale;
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@@ -51,5 +62,60 @@ public class SampleSpellCheckerService extends SpellCheckerService {
|
||||
return new SuggestionsInfo(flags,
|
||||
new String[] {"aaa", "bbb", "Candidate for " + input, mLocale});
|
||||
}
|
||||
|
||||
@Override
|
||||
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(
|
||||
TextInfo[] textInfos, int suggestionsLimit) {
|
||||
if (!isSentenceSpellCheckApiSupported()) {
|
||||
Log.e(TAG, "Sentence spell check is not supported on this platform, "
|
||||
+ "but accidentially called.");
|
||||
return null;
|
||||
}
|
||||
final ArrayList<SentenceSuggestionsInfo> retval =
|
||||
new ArrayList<SentenceSuggestionsInfo>();
|
||||
for (int i = 0; i < textInfos.length; ++i) {
|
||||
final TextInfo ti = textInfos[i];
|
||||
if (DBG) {
|
||||
Log.d(TAG, "onGetSentenceSuggestionsMultiple: " + ti.getText());
|
||||
}
|
||||
final String input = ti.getText();
|
||||
final int length = input.length();
|
||||
final SuggestionsInfo[] sis;
|
||||
final int[] lengths;
|
||||
final int[] offsets;
|
||||
if (input.equalsIgnoreCase("I wold like to here form you")) {
|
||||
// Return sentence level suggestion for this fixed input
|
||||
final int flags0 = SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO;
|
||||
final int flags1 = SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS
|
||||
| SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO;
|
||||
final int flags2 = flags1;
|
||||
final SuggestionsInfo si0 = new SuggestionsInfo(
|
||||
flags0, new String[] { "would" });
|
||||
final SuggestionsInfo si1 = new SuggestionsInfo(
|
||||
flags1, new String[] { "hear" });
|
||||
final SuggestionsInfo si2 = new SuggestionsInfo(
|
||||
flags2, new String[] { "from" });
|
||||
sis = new SuggestionsInfo[] {si0, si1, si2};
|
||||
offsets = new int[] { 2, 15, 20 };
|
||||
lengths = new int[] { 4, 4, 4 };
|
||||
} else {
|
||||
// Just a fake logic:
|
||||
// length <= 3 for short words that we assume are in the fake dictionary
|
||||
// length > 20 for too long words that we assume can't be recognized
|
||||
// (such as CJK words)
|
||||
final int flags = length <= 3 ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY
|
||||
: length <= 20 ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0;
|
||||
final SuggestionsInfo si = new SuggestionsInfo(flags,
|
||||
new String[] {"aaa", "bbb", "Candidate for " + input, mLocale});
|
||||
sis = new SuggestionsInfo[] { si };
|
||||
offsets = new int[] { 0 };
|
||||
lengths = new int[] { ti.getText().length() };
|
||||
}
|
||||
final SentenceSuggestionsInfo ssi =
|
||||
new SentenceSuggestionsInfo(sis, lengths, offsets);
|
||||
retval.add(ssi);
|
||||
}
|
||||
return retval.toArray(new SentenceSuggestionsInfo[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user