Use unmodifiable list to return the nearest keys

Change-Id: Ic9084b08fa9a1f25b7f4b98c627350583c401c12
main
Tadashi G. Takaoka 2014-04-21 13:47:23 -07:00
parent 064af5c0fc
commit 58d4e610ac
3 changed files with 26 additions and 17 deletions

View File

@ -211,10 +211,10 @@ public class Keyboard {
* Returns the array of the keys that are closest to the given point. * Returns the array of the keys that are closest to the given point.
* @param x the x-coordinate of the point * @param x the x-coordinate of the point
* @param y the y-coordinate of the point * @param y the y-coordinate of the point
* @return the array of the nearest keys to the given point. If the given * @return the list of the nearest keys to the given point. If the given
* point is out of range, then an array of size zero is returned. * point is out of range, then an array of size zero is returned.
*/ */
public Key[] getNearestKeys(final int x, final int y) { public List<Key> getNearestKeys(final int x, final int y) {
// Avoid dead pixels at edges of the keyboard // Avoid dead pixels at edges of the keyboard
final int adjustedX = Math.max(0, Math.min(x, mOccupiedWidth - 1)); final int adjustedX = Math.max(0, Math.min(x, mOccupiedWidth - 1));
final int adjustedY = Math.max(0, Math.min(y, mOccupiedHeight - 1)); final int adjustedY = Math.max(0, Math.min(y, mOccupiedHeight - 1));

View File

@ -22,9 +22,13 @@ import android.util.Log;
import com.android.inputmethod.keyboard.internal.TouchPositionCorrection; import com.android.inputmethod.keyboard.internal.TouchPositionCorrection;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.JniUtils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ProximityInfo { public class ProximityInfo {
private static final String TAG = ProximityInfo.class.getSimpleName(); private static final String TAG = ProximityInfo.class.getSimpleName();
@ -34,7 +38,7 @@ public class ProximityInfo {
public static final int MAX_PROXIMITY_CHARS_SIZE = 16; public static final int MAX_PROXIMITY_CHARS_SIZE = 16;
/** Number of key widths from current touch point to search for nearest keys. */ /** Number of key widths from current touch point to search for nearest keys. */
private static final float SEARCH_DISTANCE = 1.2f; private static final float SEARCH_DISTANCE = 1.2f;
private static final Key[] EMPTY_KEY_ARRAY = new Key[0]; private static final List<Key> EMPTY_KEY_LIST = Collections.emptyList();
private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f; private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f;
private final int mGridWidth; private final int mGridWidth;
@ -48,7 +52,7 @@ public class ProximityInfo {
private final int mMostCommonKeyWidth; private final int mMostCommonKeyWidth;
private final int mMostCommonKeyHeight; private final int mMostCommonKeyHeight;
private final Key[] mKeys; private final Key[] mKeys;
private final Key[][] mGridNeighbors; private final List<Key>[] mGridNeighbors;
private final String mLocaleStr; private final String mLocaleStr;
ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight, ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight,
@ -70,7 +74,7 @@ public class ProximityInfo {
mMostCommonKeyHeight = mostCommonKeyHeight; mMostCommonKeyHeight = mostCommonKeyHeight;
mMostCommonKeyWidth = mostCommonKeyWidth; mMostCommonKeyWidth = mostCommonKeyWidth;
mKeys = keys; mKeys = keys;
mGridNeighbors = new Key[mGridSize][]; mGridNeighbors = new List[mGridSize];
if (minWidth == 0 || height == 0) { if (minWidth == 0 || height == 0) {
// No proximity required. Keyboard might be more keys keyboard. // No proximity required. Keyboard might be more keys keyboard.
return; return;
@ -110,14 +114,14 @@ public class ProximityInfo {
} }
private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) { private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) {
final Key[][] gridNeighborKeys = mGridNeighbors; final List<Key>[] gridNeighborKeys = mGridNeighbors;
final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE]; final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE];
Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE); Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE);
for (int i = 0; i < mGridSize; ++i) { for (int i = 0; i < mGridSize; ++i) {
final int proximityCharsLength = gridNeighborKeys[i].length; final int proximityCharsLength = gridNeighborKeys[i].size();
int infoIndex = i * MAX_PROXIMITY_CHARS_SIZE; int infoIndex = i * MAX_PROXIMITY_CHARS_SIZE;
for (int j = 0; j < proximityCharsLength; ++j) { for (int j = 0; j < proximityCharsLength; ++j) {
final Key neighborKey = gridNeighborKeys[i][j]; final Key neighborKey = gridNeighborKeys[i].get(j);
// Excluding from proximityCharsArray // Excluding from proximityCharsArray
if (!needsProximityInfo(neighborKey)) { if (!needsProximityInfo(neighborKey)) {
continue; continue;
@ -353,9 +357,13 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
} }
for (int i = 0; i < gridSize; ++i) { for (int i = 0; i < gridSize; ++i) {
final int base = i * keyCount; final int indexStart = i * keyCount;
mGridNeighbors[i] = final int indexEnd = indexStart + neighborCountPerCell[i];
Arrays.copyOfRange(neighborsFlatBuffer, base, base + neighborCountPerCell[i]); final ArrayList<Key> neighbords = CollectionUtils.newArrayList(indexEnd - indexStart);
for (int index = indexStart; index < indexEnd; index++) {
neighbords.add(neighborsFlatBuffer[index]);
}
mGridNeighbors[i] = Collections.unmodifiableList(neighbords);
} }
} }
@ -369,7 +377,7 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
if (primaryKeyCode > Constants.CODE_SPACE) { if (primaryKeyCode > Constants.CODE_SPACE) {
dest[index++] = primaryKeyCode; dest[index++] = primaryKeyCode;
} }
final Key[] nearestKeys = getNearestKeys(x, y); final List<Key> nearestKeys = getNearestKeys(x, y);
for (Key key : nearestKeys) { for (Key key : nearestKeys) {
if (index >= destLength) { if (index >= destLength) {
break; break;
@ -385,9 +393,9 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
} }
} }
public Key[] getNearestKeys(final int x, final int y) { public List<Key> getNearestKeys(final int x, final int y) {
if (mGridNeighbors == null) { if (mGridNeighbors == null) {
return EMPTY_KEY_ARRAY; return EMPTY_KEY_LIST;
} }
if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) { if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) {
int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth); int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth);
@ -395,6 +403,6 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
return mGridNeighbors[index]; return mGridNeighbors[index];
} }
} }
return EMPTY_KEY_ARRAY; return EMPTY_KEY_LIST;
} }
} }

View File

@ -29,6 +29,7 @@ import com.android.inputmethod.latin.utils.JsonUtils;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -216,9 +217,9 @@ public class DynamicGridKeyboard extends Keyboard {
} }
@Override @Override
public Key[] getNearestKeys(final int x, final int y) { public List<Key> getNearestKeys(final int x, final int y) {
// TODO: Calculate the nearest key index in mGridKeys from x and y. // TODO: Calculate the nearest key index in mGridKeys from x and y.
return getKeys(); return Arrays.asList(getKeys());
} }
static final class GridKey extends Key { static final class GridKey extends Key {