Use unmodifiable list to return keys from Keyboard

Change-Id: I85a0b18d2d78632e9a9f074eda1de9225001f876
This commit is contained in:
Tadashi G. Takaoka 2014-04-21 14:13:51 -07:00
parent 58d4e610ac
commit 5326dcfb7d
6 changed files with 35 additions and 32 deletions

View file

@ -159,8 +159,7 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
// Add the virtual children of the root View. // Add the virtual children of the root View.
final Keyboard keyboard = mKeyboardView.getKeyboard(); final Keyboard keyboard = mKeyboardView.getKeyboard();
final Key[] keys = keyboard.getKeys(); for (final Key key : keyboard.getKeys()) {
for (Key key : keys) {
final int childVirtualViewId = generateVirtualViewIdForKey(key); final int childVirtualViewId = generateVirtualViewIdForKey(key);
rootInfo.addChild(mKeyboardView, childVirtualViewId); rootInfo.addChild(mKeyboardView, childVirtualViewId);
} }
@ -308,8 +307,7 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
} }
mVirtualViewIdToKey.clear(); mVirtualViewIdToKey.clear();
final Key[] keys = keyboard.getKeys(); for (final Key key : keyboard.getKeys()) {
for (Key key : keys) {
final int virtualViewId = generateVirtualViewIdForKey(key); final int virtualViewId = generateVirtualViewIdForKey(key);
mVirtualViewIdToKey.put(virtualViewId, key); mVirtualViewIdToKey.put(virtualViewId, key);
} }

View file

@ -58,9 +58,10 @@ import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -297,7 +298,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
private int getCategoryPageCount(final int categoryId) { private int getCategoryPageCount(final int categoryId) {
final Keyboard keyboard = mLayoutSet.getKeyboard(sCategoryElementId[categoryId]); final Keyboard keyboard = mLayoutSet.getKeyboard(sCategoryElementId[categoryId]);
return (keyboard.getKeys().length - 1) / mMaxPageKeyCount + 1; return (keyboard.getKeys().size() - 1) / mMaxPageKeyCount + 1;
} }
// Returns a pair of the category id and the category page id from the view pager's page // Returns a pair of the category id and the category page id from the view pager's page
@ -394,13 +395,13 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
} }
}; };
private static Key[][] sortKeysIntoPages(final Key[] inKeys, final int maxPageCount) { private static Key[][] sortKeysIntoPages(final List<Key> inKeys, final int maxPageCount) {
final Key[] keys = Arrays.copyOf(inKeys, inKeys.length); final ArrayList<Key> keys = CollectionUtils.newArrayList(inKeys);
Arrays.sort(keys, 0, keys.length, EMOJI_KEY_COMPARATOR); Collections.sort(keys, EMOJI_KEY_COMPARATOR);
final int pageCount = (keys.length - 1) / maxPageCount + 1; final int pageCount = (keys.size() - 1) / maxPageCount + 1;
final Key[][] retval = new Key[pageCount][maxPageCount]; final Key[][] retval = new Key[pageCount][maxPageCount];
for (int i = 0; i < keys.length; ++i) { for (int i = 0; i < keys.size(); ++i) {
retval[i / maxPageCount][i % maxPageCount] = keys[i]; retval[i / maxPageCount][i % maxPageCount] = keys.get(i);
} }
return retval; return retval;
} }

View file

@ -77,8 +77,8 @@ public class Keyboard {
/** Maximum column for more keys keyboard */ /** Maximum column for more keys keyboard */
public final int mMaxMoreKeysKeyboardColumn; public final int mMaxMoreKeysKeyboardColumn;
/** Array of keys and icons in this keyboard */ /** List of keys in this keyboard */
private final Key[] mKeys; private final List<Key> mKeys;
public final List<Key> mShiftKeys; public final List<Key> mShiftKeys;
public final List<Key> mAltCodeKeysWhileTyping; public final List<Key> mAltCodeKeysWhileTyping;
public final KeyboardIconsSet mIconsSet; public final KeyboardIconsSet mIconsSet;
@ -103,7 +103,7 @@ public class Keyboard {
mTopPadding = params.mTopPadding; mTopPadding = params.mTopPadding;
mVerticalGap = params.mVerticalGap; mVerticalGap = params.mVerticalGap;
mKeys = params.mKeys.toArray(new Key[params.mKeys.size()]); mKeys = Collections.unmodifiableList(CollectionUtils.newArrayList(params.mKeys));
mShiftKeys = Collections.unmodifiableList(params.mShiftKeys); mShiftKeys = Collections.unmodifiableList(params.mShiftKeys);
mAltCodeKeysWhileTyping = Collections.unmodifiableList(params.mAltCodeKeysWhileTyping); mAltCodeKeysWhileTyping = Collections.unmodifiableList(params.mAltCodeKeysWhileTyping);
mIconsSet = params.mIconsSet; mIconsSet = params.mIconsSet;
@ -154,7 +154,7 @@ public class Keyboard {
return mProximityInfo; return mProximityInfo;
} }
public Key[] getKeys() { public List<Key> getKeys() {
return mKeys; return mKeys;
} }

View file

@ -51,13 +51,13 @@ public class ProximityInfo {
private final int mKeyboardHeight; private final int mKeyboardHeight;
private final int mMostCommonKeyWidth; private final int mMostCommonKeyWidth;
private final int mMostCommonKeyHeight; private final int mMostCommonKeyHeight;
private final Key[] mKeys; private final List<Key> mKeys;
private final List<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,
final int minWidth, final int height, final int mostCommonKeyWidth, final int minWidth, final int height, final int mostCommonKeyWidth,
final int mostCommonKeyHeight, final Key[] keys, final int mostCommonKeyHeight, final List<Key> keys,
final TouchPositionCorrection touchPositionCorrection) { final TouchPositionCorrection touchPositionCorrection) {
if (TextUtils.isEmpty(localeStr)) { if (TextUtils.isEmpty(localeStr)) {
mLocaleStr = ""; mLocaleStr = "";
@ -103,7 +103,7 @@ public class ProximityInfo {
return key.getCode() >= Constants.CODE_SPACE; return key.getCode() >= Constants.CODE_SPACE;
} }
private static int getProximityInfoKeysCount(final Key[] keys) { private static int getProximityInfoKeysCount(final List<Key> keys) {
int count = 0; int count = 0;
for (final Key key : keys) { for (final Key key : keys) {
if (needsProximityInfo(key)) { if (needsProximityInfo(key)) {
@ -146,7 +146,7 @@ public class ProximityInfo {
} }
} }
final Key[] keys = mKeys; final List<Key> keys = mKeys;
final int keyCount = getProximityInfoKeysCount(keys); final int keyCount = getProximityInfoKeysCount(keys);
final int[] keyXCoordinates = new int[keyCount]; final int[] keyXCoordinates = new int[keyCount];
final int[] keyYCoordinates = new int[keyCount]; final int[] keyYCoordinates = new int[keyCount];
@ -157,8 +157,8 @@ public class ProximityInfo {
final float[] sweetSpotCenterYs; final float[] sweetSpotCenterYs;
final float[] sweetSpotRadii; final float[] sweetSpotRadii;
for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.length; keyIndex++) { for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
final Key key = keys[keyIndex]; final Key key = keys.get(keyIndex);
// Excluding from key coordinate arrays // Excluding from key coordinate arrays
if (!needsProximityInfo(key)) { if (!needsProximityInfo(key)) {
continue; continue;
@ -181,8 +181,8 @@ public class ProximityInfo {
final int rows = touchPositionCorrection.getRows(); final int rows = touchPositionCorrection.getRows();
final float defaultRadius = DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS final float defaultRadius = DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS
* (float)Math.hypot(mMostCommonKeyWidth, mMostCommonKeyHeight); * (float)Math.hypot(mMostCommonKeyWidth, mMostCommonKeyHeight);
for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.length; keyIndex++) { for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
final Key key = keys[keyIndex]; final Key key = keys.get(keyIndex);
// Excluding from touch position correction arrays // Excluding from touch position correction arrays
if (!needsProximityInfo(key)) { if (!needsProximityInfo(key)) {
continue; continue;
@ -244,7 +244,7 @@ public class ProximityInfo {
private void computeNearestNeighbors() { private void computeNearestNeighbors() {
final int defaultWidth = mMostCommonKeyWidth; final int defaultWidth = mMostCommonKeyWidth;
final int keyCount = mKeys.length; final int keyCount = mKeys.size();
final int gridSize = mGridNeighbors.length; final int gridSize = mGridNeighbors.length;
final int threshold = (int) (defaultWidth * SEARCH_DISTANCE); final int threshold = (int) (defaultWidth * SEARCH_DISTANCE);
final int thresholdSquared = threshold * threshold; final int thresholdSquared = threshold * threshold;

View file

@ -31,6 +31,7 @@ import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -51,7 +52,7 @@ public class DynamicGridKeyboard extends Keyboard {
private final ArrayDeque<GridKey> mGridKeys = CollectionUtils.newArrayDeque(); private final ArrayDeque<GridKey> mGridKeys = CollectionUtils.newArrayDeque();
private final ArrayDeque<Key> mPendingKeys = CollectionUtils.newArrayDeque(); private final ArrayDeque<Key> mPendingKeys = CollectionUtils.newArrayDeque();
private Key[] mCachedGridKeys; private List<Key> mCachedGridKeys;
public DynamicGridKeyboard(final SharedPreferences prefs, final Keyboard templateKeyboard, public DynamicGridKeyboard(final SharedPreferences prefs, final Keyboard templateKeyboard,
final int maxKeyCount, final int categoryId) { final int maxKeyCount, final int categoryId) {
@ -206,12 +207,14 @@ public class DynamicGridKeyboard extends Keyboard {
} }
@Override @Override
public Key[] getKeys() { public List<Key> getKeys() {
synchronized (mLock) { synchronized (mLock) {
if (mCachedGridKeys != null) { if (mCachedGridKeys != null) {
return mCachedGridKeys; return mCachedGridKeys;
} }
mCachedGridKeys = mGridKeys.toArray(new Key[mGridKeys.size()]); final ArrayList<Key> cachedKeys = CollectionUtils.newArrayList(mGridKeys.size());
cachedKeys.addAll(mGridKeys);
mCachedGridKeys = Collections.unmodifiableList(cachedKeys);
return mCachedGridKeys; return mCachedGridKeys;
} }
} }
@ -219,7 +222,7 @@ public class DynamicGridKeyboard extends Keyboard {
@Override @Override
public List<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 Arrays.asList(getKeys()); return getKeys();
} }
static final class GridKey extends Key { static final class GridKey extends Key {

View file

@ -26,6 +26,7 @@ import com.android.inputmethod.latin.utils.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
/** /**
* This class builds an actual keyboard for unit test. * This class builds an actual keyboard for unit test.
@ -43,7 +44,7 @@ public final class ActualKeyboardBuilder extends AbstractKeyboardBuilder<Key> {
} }
}; };
private static ArrayList<Key> filterOutSpacerAndSortKeys(final Key[] keys) { private static ArrayList<Key> filterOutSpacerAndSortKeys(final List<Key> keys) {
final ArrayList<Key> filteredKeys = CollectionUtils.newArrayList(); final ArrayList<Key> filteredKeys = CollectionUtils.newArrayList();
for (final Key key : keys) { for (final Key key : keys) {
if (key.isSpacer()) { if (key.isSpacer()) {
@ -57,10 +58,10 @@ public final class ActualKeyboardBuilder extends AbstractKeyboardBuilder<Key> {
/** /**
* Create the keyboard that consists of the array of rows of the actual keyboard's keys. * Create the keyboard that consists of the array of rows of the actual keyboard's keys.
* @param keys the array of keys of the actual keyboard. * @param keys the list of keys of the actual keyboard.
* @return the actual keyboard grouped with rows. * @return the actual keyboard grouped with rows.
*/ */
public static Key[][] buildKeyboard(final Key[] keys) { public static Key[][] buildKeyboard(final List<Key> keys) {
// Filter out spacer and sort keys from top-left to bottom-right order to prepare to // Filter out spacer and sort keys from top-left to bottom-right order to prepare to
// create rows. // create rows.
final ArrayList<Key> sortedKeys = filterOutSpacerAndSortKeys(keys); final ArrayList<Key> sortedKeys = filterOutSpacerAndSortKeys(keys);