am b6e128cc: Merge "[IL69.1] Move a static method to a utility class."

* commit 'b6e128cc05fa0b4b1f79b6c7eb4d67f44833c7f4':
  [IL69.1] Move a static method to a utility class.
main
Jean Chalard 2014-01-23 19:07:26 -08:00 committed by Android Git Automerger
commit 341044034c
5 changed files with 47 additions and 25 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -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<SuggestedWordInfo> suggestions =
dictInfo.mDictionary.getSuggestions(composer, prevWord,

View File

@ -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) {

View File

@ -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);