Index: chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..337cf0a33a184a9c374576748bf427687d988f51 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java |
@@ -0,0 +1,93 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.spellchecker; |
+ |
+import android.content.Context; |
+import android.view.textservice.SentenceSuggestionsInfo; |
+import android.view.textservice.SpellCheckerSession; |
+import android.view.textservice.SuggestionsInfo; |
+import android.view.textservice.TextInfo; |
+import android.view.textservice.TextServicesManager; |
+ |
+import org.chromium.base.ApplicationStatus; |
+import org.chromium.base.annotations.CalledByNative; |
+ |
+import java.util.ArrayList; |
+import java.util.Locale; |
+ |
+/** |
+* JNI interface for spellcheck_platform_android to use Android's spellchecker. |
+*/ |
+public class SpellCheckerSessionBridge implements SpellCheckerSession.SpellCheckerSessionListener { |
+ |
+ private boolean mReady; |
+ private SpellCheckerSession mSpellCheckerSession; |
+ private final long mNativeSpellCheckMessageFilterPlatform; |
+ private String mText; |
+ |
+ public SpellCheckerSessionBridge(long nativeSpellCheckMessageFilterPlatform) { |
+ mNativeSpellCheckMessageFilterPlatform = nativeSpellCheckMessageFilterPlatform; |
+ mReady = true; |
+ Context context = ApplicationStatus.getApplicationContext(); |
+ final TextServicesManager textServicesManager = |
+ (TextServicesManager) context.getSystemService( |
+ Context.TEXT_SERVICES_MANAGER_SERVICE); |
+ mSpellCheckerSession = textServicesManager.newSpellCheckerSession(null, |
+ Locale.ENGLISH, SpellCheckerSessionBridge.this, true); |
+ } |
+ |
+ @CalledByNative |
+ private static SpellCheckerSessionBridge create(long nativeSpellCheckMessageFilterPlatform) { |
+ return new SpellCheckerSessionBridge(nativeSpellCheckMessageFilterPlatform); |
+ } |
+ |
+ @CalledByNative |
+ private void checkSpelling(String text) { |
+ if (!mReady) { // The listener can't handle multiple events in rapid sequence |
+ return; |
+ } |
+ mReady = false; |
+ mText = text; |
+ mSpellCheckerSession.getSentenceSuggestions(new TextInfo[]{ new TextInfo(text)}, 5); |
+ } |
+ |
+ @Override |
+ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { |
+ ArrayList<Integer> offsets = new ArrayList<Integer>(); |
+ ArrayList<Integer> lengths = new ArrayList<Integer>(); |
+ |
+ for (SentenceSuggestionsInfo result: results) { |
+ for (int i = 0; i < result.getSuggestionsCount(); i++) { |
+ SuggestionsInfo currentSuggestion = result.getSuggestionsInfoAt(i); |
+ boolean isWordCorrect = (currentSuggestion.getSuggestionsAttributes() |
+ & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == 0; |
+ if (!isWordCorrect) { |
+ offsets.add(result.getOffsetAt(i)); |
+ lengths.add(result.getLengthAt(i)); |
+ } |
+ } |
+ } |
+ |
+ nativeGetSpellcheckInfo(mNativeSpellCheckMessageFilterPlatform, |
+ mText, convertListToArray(offsets), convertListToArray(lengths)); |
+ mReady = true; |
+ } |
+ |
+ private int[] convertListToArray(ArrayList<Integer> toConvert) { |
+ int[] toReturn = new int[toConvert.size()]; |
+ int index = 0; |
+ for (Integer number : toConvert) { |
+ toReturn[index++] = number.intValue(); |
+ } |
+ return toReturn; |
+ } |
+ |
+ @Override |
+ public void onGetSuggestions(SuggestionsInfo[] results) { |
+ } |
+ |
+ private native void nativeGetSpellcheckInfo(long nativeSpellCheckMessageFilterPlatform, |
+ String text, int[] offsets, int[] lengths); |
+} |