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..6ff206657646b317227bcd8dd75ebd34e6b71d24 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java |
@@ -0,0 +1,110 @@ |
+// 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 mIsActiveRequest; |
+ private boolean mIsPendingRequest; |
+ private SpellCheckerSession mSpellCheckerSession; |
+ private final long mNativeSpellCheckerSessionBridge; |
+ private String mActiveText; |
+ private String mPendingText; |
+ |
+ public SpellCheckerSessionBridge(long nativeSpellCheckerSessionBridge) { |
+ mNativeSpellCheckerSessionBridge = nativeSpellCheckerSessionBridge; |
+ Context context = ApplicationStatus.getApplicationContext(); |
+ final TextServicesManager textServicesManager = |
+ (TextServicesManager) context.getSystemService( |
+ Context.TEXT_SERVICES_MANAGER_SERVICE); |
+ mSpellCheckerSession = |
+ textServicesManager.newSpellCheckerSession(null, Locale.ENGLISH, this, true); |
+ } |
+ |
+ @CalledByNative |
+ private static SpellCheckerSessionBridge create(long nativeSpellCheckerSessionBridge) { |
+ return new SpellCheckerSessionBridge(nativeSpellCheckerSessionBridge); |
+ } |
+ |
+ @CalledByNative |
+ private void checkSpelling(String text) { |
+ // The listener can't handle multiple events in rapid sequence |
+ // Save incoming request text to run at the end of the currently active request |
+ // If multiple requests arrive during one active request, only the most |
+ // recent request will run (the others get overwritten). |
+ if (mIsActiveRequest) { |
+ System.out.println("DYLANKING Setting Pending Text To: " + text); |
please use gerrit instead
2015/08/11 16:43:46
Remove the log.
dylanking
2015/08/12 00:16:26
Don't know how that snuck through. Deleted.
|
+ mIsPendingRequest = true; |
+ mPendingText = text; |
+ return; |
+ } |
+ mIsActiveRequest = true; |
+ mActiveText = text; |
+ mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(mActiveText)}, 5); |
please use gerrit instead
2015/08/11 16:43:46
What is 5?
dylanking
2015/08/12 00:16:26
The second parameter to this function is the maxim
|
+ } |
+ |
+ @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(mNativeSpellCheckerSessionBridge, mActiveText, |
+ convertListToArray(offsets), convertListToArray(lengths)); |
+ |
+ // If any requests arrived during the execution of this request, run the |
+ // most recent request. |
+ if (mIsPendingRequest) { |
+ mIsPendingRequest = false; |
please use gerrit instead
2015/08/11 16:43:46
You're saving pending text here, but identifier an
dylanking
2015/08/12 00:16:26
Great point. I've created an inner class to encap
|
+ mActiveText = mPendingText; |
+ mSpellCheckerSession.getSentenceSuggestions( |
+ new TextInfo[] {new TextInfo(mActiveText)}, 5); |
+ } else { |
+ mIsActiveRequest = false; |
+ } |
+ } |
+ |
+ private int[] convertListToArray(ArrayList<Integer> toConvert) { |
please use gerrit instead
2015/08/11 16:43:46
Do any of these work?
https://docs.oracle.com/jav
dylanking
2015/08/12 00:16:26
I think those will convert the list of Integer *ob
|
+ 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 nativeSpellCheckerSessionBridge, String text, int[] offsets, int[] lengths); |
+} |