Chromium Code Reviews| 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..77863f3b672a2640d5cb1d9853d1f47874ddb828 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/spellchecker/SpellCheckerSessionBridge.java |
| @@ -0,0 +1,90 @@ |
| +// 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.SpellCheckerSession.SpellCheckerSessionListener; |
| +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; |
| + |
| +/** |
| +* JNI interface for native SpellCheckerSessionBridge to use Android's spellchecker. |
| +*/ |
| +public class SpellCheckerSessionBridge implements SpellCheckerSessionListener { |
| + private final long mNativeSpellCheckerSessionBridge; |
| + private SpellCheckerSession mSpellCheckerSession; |
| + |
| + private SpellCheckerSessionBridge(long nativeSpellCheckerSessionBridge) { |
| + mNativeSpellCheckerSessionBridge = nativeSpellCheckerSessionBridge; |
| + |
| + Context context = ApplicationStatus.getApplicationContext(); |
| + final TextServicesManager textServicesManager = |
| + (TextServicesManager) context.getSystemService( |
| + Context.TEXT_SERVICES_MANAGER_SERVICE); |
| + |
| + // This combination of parameters will cause the spellchecker to be based off of |
| + // the system language specified in settings (the default locale). Note that this is not |
| + // the language specified in the adjacent "Spell checker" setting. However, if the user |
| + // has turned off spellchecker in their system settings, this function will return null. |
| + mSpellCheckerSession = textServicesManager.newSpellCheckerSession(null, null, this, true); |
| + } |
| + |
| + @CalledByNative |
| + private static SpellCheckerSessionBridge create(long nativeSpellCheckerSessionBridge) { |
| + return new SpellCheckerSessionBridge(nativeSpellCheckerSessionBridge); |
|
newt (away)
2015/08/13 20:40:31
If mSpellCheckerSession is null, then this method
dylanking
2015/08/14 02:57:02
I tried this and got some "non-static object canno
|
| + } |
| + |
| + @CalledByNative |
| + private void requestTextCheck(String text) { |
| + if (mSpellCheckerSession == null) { |
| + return; |
| + } |
| + mSpellCheckerSession.getSentenceSuggestions(new TextInfo[] {new TextInfo(text)}, 0); |
| + } |
| + |
| + @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() |
|
newt (away)
2015/08/13 20:40:31
Still weird indentation. If git cl format did this
dylanking
2015/08/14 02:57:03
Yeah I had changed it, but git cl format keeps cha
dylanking
2015/08/14 02:57:03
Yeah, I think it was git cl format because I fixed
|
| + & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) |
| + == 0; |
| + if (!isWordCorrect) { |
| + offsets.add(result.getOffsetAt(i)); |
| + lengths.add(result.getLengthAt(i)); |
| + } |
| + } |
| + } |
| + |
| + nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge, |
| + convertListToArray(offsets), convertListToArray(lengths)); |
| + } |
| + |
| + private int[] convertListToArray(ArrayList<Integer> list) { |
| + int[] array = new int[list.size()]; |
| + for (int index = 0; index < array.length; index++) { |
| + array[index] = list.get(index).intValue(); |
| + } |
| + return array; |
| + } |
| + |
| + @Override |
| + public void onGetSuggestions(SuggestionsInfo[] results) {} |
| + |
| + private native void nativeProcessSpellCheckResults( |
| + long nativeSpellCheckerSessionBridge, int[] offsets, int[] lengths); |
| +} |