From 1d6e647ac31eab38f30b8ed468551d1e3a07c705 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 23 Jan 2014 16:10:26 +0900 Subject: [PATCH] [IL69.1] Move a static method to a utility class. This is a followup to IL69. Change-Id: Iba154517aa7f75b9f16aa1b808c0da8d84be837f --- .../inputmethod/keyboard/Keyboard.java | 17 ++++++++++++++ .../android/inputmethod/latin/LatinIME.java | 23 +++++-------------- .../AndroidWordLevelSpellCheckerSession.java | 14 +++++++---- .../latin/utils/CoordinateUtils.java | 11 ++++++++- .../inputmethod/latin/WordComposerTests.java | 7 +++--- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index bc1383aff..4fd3bac2f 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -23,6 +23,7 @@ import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.keyboard.internal.KeyboardParams; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.utils.CollectionUtils; +import com.android.inputmethod.latin.utils.CoordinateUtils; /** * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard @@ -217,4 +218,20 @@ public class Keyboard { final int adjustedY = Math.max(0, Math.min(y, mOccupiedHeight - 1)); return mProximityInfo.getNearestKeys(adjustedX, adjustedY); } + + public int[] getCoordinates(final int[] codePoints) { + final int length = codePoints.length; + final int[] coordinates = CoordinateUtils.newCoordinateArray(length); + for (int i = 0; i < length; ++i) { + final Key key = getKey(codePoints[i]); + if (null != key) { + CoordinateUtils.setXYInArray(coordinates, i, + key.getX() + key.getWidth() / 2, key.getY() + key.getHeight() / 2); + } else { + CoordinateUtils.setXYInArray(coordinates, i, + Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); + } + } + return coordinates; + } } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index a4253bb3b..b094100c6 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -59,7 +59,6 @@ import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.compat.InputMethodServiceCompatUtils; import com.android.inputmethod.dictionarypack.DictionaryPackConstants; -import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardId; @@ -1206,23 +1205,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen * @return x,y coordinates for this keyboard, as a flattened array. */ public int[] getCoordinatesForCurrentKeyboard(final int[] codePoints) { - return getCoordinatesForKeyboard(codePoints, mKeyboardSwitcher.getKeyboard()); - } - - public static int[] getCoordinatesForKeyboard(final int[] codePoints, final Keyboard keyboard) { - final int length = codePoints.length; - final int[] coordinates = CoordinateUtils.newCoordinateArray(length); - Key key; - for (int i = 0; i < length; ++i) { - if (keyboard != null && (key = keyboard.getKey(codePoints[i])) != null) { - CoordinateUtils.setXYInArray(coordinates, i, - key.getX() + key.getWidth() / 2, key.getY() + key.getHeight() / 2); - } else { - CoordinateUtils.setXYInArray(coordinates, i, - Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); - } + final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); + if (null == keyboard) { + return CoordinateUtils.newCoordinateArray(codePoints.length, + Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); + } else { + return keyboard.getCoordinates(codePoints); } - return coordinates; } // Callback for the {@link SuggestionStripView}, to call when the "add to dictionary" hint is diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index 826e36d86..3947019ca 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -28,12 +28,13 @@ import android.view.textservice.SuggestionsInfo; import android.view.textservice.TextInfo; import com.android.inputmethod.compat.SuggestionsInfoCompatUtils; +import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Dictionary; -import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.spellcheck.AndroidSpellCheckerService.SuggestionsGatherer; +import com.android.inputmethod.latin.utils.CoordinateUtils; import com.android.inputmethod.latin.utils.LocaleUtils; import com.android.inputmethod.latin.utils.StringUtils; @@ -314,9 +315,14 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { } final WordComposer composer = new WordComposer(); final int[] codePoints = StringUtils.toCodePointArray(text); - composer.setComposingWord(codePoints, - LatinIME.getCoordinatesForKeyboard(codePoints, dictInfo.mKeyboard), - null /* previousWord */); + final int[] coordinates; + if (null == dictInfo.mKeyboard) { + coordinates = CoordinateUtils.newCoordinateArray(codePoints.length, + Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); + } else { + coordinates = dictInfo.mKeyboard.getCoordinates(codePoints); + } + composer.setComposingWord(codePoints, coordinates, null /* previousWord */); // TODO: make a spell checker option to block offensive words or not final ArrayList suggestions = dictInfo.mDictionary.getSuggestions(composer, prevWord, diff --git a/java/src/com/android/inputmethod/latin/utils/CoordinateUtils.java b/java/src/com/android/inputmethod/latin/utils/CoordinateUtils.java index 91a63501a..c29ecbd6f 100644 --- a/java/src/com/android/inputmethod/latin/utils/CoordinateUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/CoordinateUtils.java @@ -50,7 +50,16 @@ public final class CoordinateUtils { } public static int[] newCoordinateArray(final int arraySize) { - return new int[ELEMENT_SIZE * arraySize]; + return new int[ELEMENT_SIZE * arraySize]; + } + + public static int[] newCoordinateArray(final int arraySize, + final int defaultX, final int defaultY) { + final int[] result = new int[ELEMENT_SIZE * arraySize]; + for (int i = 0; i < arraySize; ++i) { + setXYInArray(result, i, defaultX, defaultY); + } + return result; } public static int xFromArray(final int[] coordsArray, final int index) { diff --git a/tests/src/com/android/inputmethod/latin/WordComposerTests.java b/tests/src/com/android/inputmethod/latin/WordComposerTests.java index 2fe331bc3..62eb63ccd 100644 --- a/tests/src/com/android/inputmethod/latin/WordComposerTests.java +++ b/tests/src/com/android/inputmethod/latin/WordComposerTests.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; +import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.utils.StringUtils; /** @@ -37,7 +38,7 @@ public class WordComposerTests extends AndroidTestCase { final String STR_WITHIN_BMP = "abcdef"; final int[] CODEPOINTS_WITHIN_BMP = StringUtils.toCodePointArray(STR_WITHIN_BMP); final int[] COORDINATES_WITHIN_BMP = - LatinIME.getCoordinatesForKeyboard(CODEPOINTS_WITHIN_BMP, null); + Keyboard.getCoordinatesWithoutKeyboard(CODEPOINTS_WITHIN_BMP); final String PREVWORD = "prevword"; wc.setComposingWord(CODEPOINTS_WITHIN_BMP, COORDINATES_WITHIN_BMP, PREVWORD); assertEquals(wc.size(), @@ -69,8 +70,8 @@ public class WordComposerTests extends AndroidTestCase { final String STR_WITH_SUPPLEMENTARY_CHAR = "abcde\uD861\uDED7fgh"; final int[] CODEPOINTS_WITH_SUPPLEMENTARY_CHAR = StringUtils.toCodePointArray(STR_WITH_SUPPLEMENTARY_CHAR); - final int[] COORDINATES_WITH_SUPPLEMENTARY_CHAR = LatinIME.getCoordinatesForKeyboard( - CODEPOINTS_WITH_SUPPLEMENTARY_CHAR, null); + final int[] COORDINATES_WITH_SUPPLEMENTARY_CHAR = Keyboard.getCoordinatesWithoutKeyboard( + CODEPOINTS_WITH_SUPPLEMENTARY_CHAR); wc.setComposingWord(CODEPOINTS_WITH_SUPPLEMENTARY_CHAR, COORDINATES_WITH_SUPPLEMENTARY_CHAR, null /* previousWord */); assertEquals(wc.size(), CODEPOINTS_WITH_SUPPLEMENTARY_CHAR.length);