Support sentence level spell check in sample code of the spell checker
Bug: 6136149
Change-Id: I1519258dd3ce95ad01e50a75f802469737bef3c4
diff --git a/samples/SpellChecker/SampleSpellCheckerService/Android.mk b/samples/SpellChecker/SampleSpellCheckerService/Android.mk
index adf65d9..4f6b421 100755
--- a/samples/SpellChecker/SampleSpellCheckerService/Android.mk
+++ b/samples/SpellChecker/SampleSpellCheckerService/Android.mk
@@ -5,8 +5,9 @@
LOCAL_SRC_FILES := $(call all-subdir-java-files)
+# TODO: Change sdk version to 16
LOCAL_SDK_VERSION := current
-LOCAL_PACKAGE_NAME := SampleSpellChecker
+LOCAL_PACKAGE_NAME := SampleSpellCheckerService
include $(BUILD_PACKAGE)
diff --git a/samples/SpellChecker/SampleSpellCheckerService/res/xml/spellchecker.xml b/samples/SpellChecker/SampleSpellCheckerService/res/xml/spellchecker.xml
index f4601d2..b3a078a 100644
--- a/samples/SpellChecker/SampleSpellCheckerService/res/xml/spellchecker.xml
+++ b/samples/SpellChecker/SampleSpellCheckerService/res/xml/spellchecker.xml
@@ -22,7 +22,8 @@
<spell-checker xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/spellchecker_name"
- android:settingsActivity="com.example.android.samplespellcheckerservice.SpellCheckerSettingsActivity">
+ android:settingsActivity="com.example.android.samplespellcheckerservice.SpellCheckerSettingsActivity"
+ android:supportsSentenceSpellCheck="true">
<subtype
android:label="@string/subtype_generic"
android:subtypeLocale="en"
diff --git a/samples/SpellChecker/SampleSpellCheckerService/src/com/example/android/samplespellcheckerservice/SampleSpellCheckerService.java b/samples/SpellChecker/SampleSpellCheckerService/src/com/example/android/samplespellcheckerservice/SampleSpellCheckerService.java
index dc85587..8778cc0 100644
--- a/samples/SpellChecker/SampleSpellCheckerService/src/com/example/android/samplespellcheckerservice/SampleSpellCheckerService.java
+++ b/samples/SpellChecker/SampleSpellCheckerService/src/com/example/android/samplespellcheckerservice/SampleSpellCheckerService.java
@@ -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 @@
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]);
+ }
}
}