From 4a019a9c96b3a628265ef49f5522f20aeb5856cf Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 16 Dec 2011 15:32:24 +0900 Subject: [PATCH] Use Set to group Keys in Keyboard instead of List In deriving various shift stated alphabet keyboards from base/main keyboard, almost all keys should be shared among variants. Grouping keys in Set instead of List is a essential refactor to have. Bug: 5002108 Bug: 5679585 Change-Id: Idd5644d1c45a3276a24b61c984619c03d4e4c54c --- .../com/android/inputmethod/keyboard/Key.java | 62 +++++++++++++++++-- .../inputmethod/keyboard/Keyboard.java | 17 +++-- .../inputmethod/keyboard/PointerTracker.java | 3 +- .../inputmethod/keyboard/ProximityInfo.java | 17 ++--- .../keyboard/internal/KeyboardParams.java | 6 +- 5 files changed, 79 insertions(+), 26 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 42d1fe1ac..028e3fda8 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -37,6 +37,7 @@ import com.android.inputmethod.latin.R; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -116,6 +117,8 @@ public class Key { private static final int ACTION_FLAGS_NO_KEY_PREVIEW = 0x02; private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04; + private final int mHashCode; + /** The current pressed state of this key */ private boolean mPressed; /** If this is a sticky key, is its highlight on? */ @@ -204,6 +207,8 @@ public class Key { mX = x + mHorizontalGap / 2; mY = y; mHitBox.set(x, y, x + width + 1, y + height); + + mHashCode = hashCode(this); } /** @@ -279,10 +284,6 @@ public class Key { KeyboardIconsSet.ICON_UNDEFINED)); final int shiftedIconId = style.getInt(keyAttr, R.styleable.Keyboard_Key_keyIconShifted, KeyboardIconsSet.ICON_UNDEFINED); - if (shiftedIconId != KeyboardIconsSet.ICON_UNDEFINED) { - final Drawable shiftedIcon = iconsSet.getIcon(shiftedIconId); - params.addShiftedIcon(this, shiftedIcon); - } mHintLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLabel); mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel); @@ -306,8 +307,61 @@ public class Key { } mAltCode = style.getInt(keyAttr, R.styleable.Keyboard_Key_altCode, Keyboard.CODE_DUMMY); + mHashCode = hashCode(this); keyAttr.recycle(); + + if (shiftedIconId != KeyboardIconsSet.ICON_UNDEFINED) { + final Drawable shiftedIcon = iconsSet.getIcon(shiftedIconId); + params.addShiftedIcon(this, shiftedIcon); + } + } + + private static int hashCode(Key key) { + return Arrays.hashCode(new Object[] { + key.mX, + key.mY, + key.mWidth, + key.mHeight, + key.mCode, + key.mLabel, + key.mHintLabel, + // Key can be distinguishable without the following members. + // key.mAltCode, + // key.mOutputText, + // key.mActionFlags, + // key.mLabelFlags, + // key.mIcon, + // key.mPreviewIcon, + // key.mBackgroundType, + // key.mHorizontalGap, + // key.mVerticalGap, + // key.mVisualInsetLeft, + // key.mVisualInsetRight, + // Arrays.hashCode(key.mMoreKeys), + // key.mMaxMoreKeysColumn, + }); + } + + private boolean equals(Key o) { + if (this == o) return true; + return o.mX == mX + && o.mY == mY + && o.mWidth == mWidth + && o.mHeight == mHeight + && o.mCode == mCode + && TextUtils.equals(o.mLabel, mLabel) + && TextUtils.equals(o.mHintLabel, mHintLabel); + } + + @Override + public int hashCode() { + return mHashCode; + } + + @Override + public boolean equals(Object o) { + return o instanceof Key && equals((Key)o); } public void markAsLeftEdge(KeyboardParams params) { diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 8a0887353..f21d4545e 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -26,7 +26,6 @@ import com.android.inputmethod.keyboard.internal.KeyboardShiftState; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; @@ -106,8 +105,8 @@ public class Keyboard { public final boolean mIsRtlKeyboard; /** List of keys and icons in this keyboard */ - public final List mKeys; - public final List mShiftKeys; + public final Set mKeys; + public final Set mShiftKeys; public final Set mShiftLockKeys; public final Map mShiftedIcons; public final Map mUnshiftedIcons; @@ -134,8 +133,8 @@ public class Keyboard { mTopPadding = params.mTopPadding; mVerticalGap = params.mVerticalGap; - mKeys = Collections.unmodifiableList(params.mKeys); - mShiftKeys = Collections.unmodifiableList(params.mShiftKeys); + mKeys = Collections.unmodifiableSet(params.mKeys); + mShiftKeys = Collections.unmodifiableSet(params.mShiftKeys); mShiftLockKeys = Collections.unmodifiableSet(params.mShiftLockKeys); mShiftedIcons = Collections.unmodifiableMap(params.mShiftedIcons); mUnshiftedIcons = Collections.unmodifiableMap(params.mUnshiftedIcons); @@ -170,12 +169,12 @@ public class Keyboard { } // TODO: Remove this method. - public boolean hasShiftLockKey() { + boolean hasShiftLockKey() { return !mShiftLockKeys.isEmpty(); } // TODO: Remove this method. - public void setShiftLocked(boolean newShiftLockState) { + void setShiftLocked(boolean newShiftLockState) { for (final Key key : mShiftLockKeys) { // To represent "shift locked" state. The highlight is handled by background image that // might be a StateListDrawable. @@ -191,7 +190,7 @@ public class Keyboard { } // TODO: Remove this method. - public void setShifted(boolean newShiftState) { + void setShifted(boolean newShiftState) { if (!mShiftState.isShiftLocked()) { for (final Key key : mShiftKeys) { key.setIcon(newShiftState ? mShiftedIcons.get(key) : mUnshiftedIcons.get(key)); @@ -206,7 +205,7 @@ public class Keyboard { } // TODO: Remove this method - public void setAutomaticTemporaryUpperCase() { + void setAutomaticTemporaryUpperCase() { mShiftState.setAutomaticTemporaryUpperCase(); } diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 3a07cdf4d..7c14b5888 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -29,6 +29,7 @@ import com.android.inputmethod.latin.R; import java.util.ArrayList; import java.util.List; +import java.util.Set; public class PointerTracker { private static final String TAG = PointerTracker.class.getSimpleName(); @@ -118,7 +119,7 @@ public class PointerTracker { private KeyboardActionListener mListener = EMPTY_LISTENER; private Keyboard mKeyboard; - private List mKeys; + private Set mKeys; private int mKeyQuarterWidthSquared; private final TextView mKeyPreviewText; diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index 7f7f9cb22..b6178d945 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -24,7 +24,7 @@ import com.android.inputmethod.latin.spellcheck.SpellCheckerProximityInfo; import java.util.Arrays; import java.util.Collections; -import java.util.List; +import java.util.Set; public class ProximityInfo { public static final int MAX_PROXIMITY_CHARS_SIZE = 16; @@ -44,7 +44,7 @@ public class ProximityInfo { private final Key[][] mGridNeighbors; ProximityInfo(int gridWidth, int gridHeight, int minWidth, int height, int keyWidth, - int keyHeight, List keys, TouchPositionCorrection touchPositionCorrection) { + int keyHeight, Set keys, TouchPositionCorrection touchPositionCorrection) { mGridWidth = gridWidth; mGridHeight = gridHeight; mGridSize = mGridWidth * mGridHeight; @@ -62,7 +62,7 @@ public class ProximityInfo { } public static ProximityInfo createDummyProximityInfo() { - return new ProximityInfo(1, 1, 1, 1, 1, 1, Collections.emptyList(), null); + return new ProximityInfo(1, 1, 1, 1, 1, 1, Collections.emptySet(), null); } public static ProximityInfo createSpellCheckerProximityInfo(final int[] proximity) { @@ -87,9 +87,9 @@ public class ProximityInfo { private native void releaseProximityInfoNative(long nativeProximityInfo); private final void setProximityInfo(Key[][] gridNeighborKeys, int keyboardWidth, - int keyboardHeight, List keys, + int keyboardHeight, Set keys, TouchPositionCorrection touchPositionCorrection) { - int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE]; + final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE]; Arrays.fill(proximityCharsArray, KeyDetector.NOT_A_CODE); for (int i = 0; i < mGridSize; ++i) { final int proximityCharsLength = gridNeighborKeys[i].length; @@ -118,8 +118,8 @@ public class ProximityInfo { calculateSweetSpotParams = false; } - for (int i = 0; i < keyCount; ++i) { - final Key key = keys.get(i); + int i = 0; + for (final Key key : keys) { keyXCoordinates[i] = key.mX; keyYCoordinates[i] = key.mY; keyWidths[i] = key.mWidth; @@ -142,6 +142,7 @@ public class ProximityInfo { hitBoxWidth * hitBoxWidth + hitBoxHeight * hitBoxHeight); } } + i++; } mNativeProximityInfo = setProximityInfoNative(MAX_PROXIMITY_CHARS_SIZE, @@ -166,7 +167,7 @@ public class ProximityInfo { } } - private void computeNearestNeighbors(int defaultWidth, List keys, + private void computeNearestNeighbors(int defaultWidth, Set keys, TouchPositionCorrection touchPositionCorrection) { final int thresholdBase = (int) (defaultWidth * SEARCH_DISTANCE); final int threshold = thresholdBase * thresholdBase; diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java index 64cd37c4b..5248016f1 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java @@ -23,10 +23,8 @@ import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.latin.LatinImeLogger; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -59,8 +57,8 @@ public class KeyboardParams { public int GRID_WIDTH; public int GRID_HEIGHT; - public final List mKeys = new ArrayList(); - public final List mShiftKeys = new ArrayList(); + public final Set mKeys = new HashSet(); + public final Set mShiftKeys = new HashSet(); public final Set mShiftLockKeys = new HashSet(); public final Map mShiftedIcons = new HashMap(); public final Map mUnshiftedIcons = new HashMap();