From 09f8b126e532ca2ac6bbe00c0d78bf03e44b78a2 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 25 Jan 2012 19:43:13 +0900 Subject: [PATCH] Add Key preserveCase enum to keyLabelOptions attribute To support auto generate key depending keyboard element id, the KeysCache class is introduced to hold whole keys and reuse. Change-Id: Icb81b5f1c1b3aaa31968dcdb93aa0a856e737f78 --- java/res/values/attrs.xml | 3 + java/res/xml/keyboard_set.xml | 3 +- .../com/android/inputmethod/keyboard/Key.java | 76 ++++++++++++++----- .../inputmethod/keyboard/Keyboard.java | 9 ++- .../inputmethod/keyboard/KeyboardSet.java | 45 ++++++++--- 5 files changed, 104 insertions(+), 32 deletions(-) diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml index 5824d1736..70fc7f86e 100644 --- a/java/res/values/attrs.xml +++ b/java/res/values/attrs.xml @@ -242,6 +242,9 @@ + + diff --git a/java/res/xml/keyboard_set.xml b/java/res/xml/keyboard_set.xml index 03eb77897..27ef316fe 100644 --- a/java/res/xml/keyboard_set.xml +++ b/java/res/xml/keyboard_set.xml @@ -23,7 +23,8 @@ latin:keyboardLocale="en_GB,en_US"> + latin:elementKeyboard="@xml/kbd_qwerty" + latin:elementAutoGenerate="true" /> mShiftLockKeys = new HashSet(); public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet(); + public KeyboardSet.KeysCache mKeysCache; + public int mMostCommonKeyHeight = 0; public int mMostCommonKeyWidth = 0; @@ -361,7 +363,8 @@ public class Keyboard { clearHistogram(); } - public void onAddKey(Key key) { + public void onAddKey(Key newKey) { + final Key key = (mKeysCache != null) ? mKeysCache.get(newKey) : newKey; mKeys.add(key); updateHistogram(key); if (key.mCode == Keyboard.CODE_SHIFT) { @@ -688,6 +691,10 @@ public class Keyboard { params.mTouchPositionCorrection.load(data); } + public void setAutoGenerate(KeyboardSet.KeysCache keysCache) { + mParams.mKeysCache = keysCache; + } + public Builder load(int xmlId, KeyboardId id) { mParams.mId = id; final XmlResourceParser parser = mResources.getXml(xmlId); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java index c7f964aaa..cacb8a324 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java @@ -57,6 +57,25 @@ public class KeyboardSet { private final Context mContext; private final Params mParams; + private final KeysCache mKeysCache = new KeysCache(); + + public static class KeysCache { + private final Map mMap; + + public KeysCache() { + mMap = new HashMap(); + } + + public Key get(Key key) { + final Key existingKey = mMap.get(key); + if (existingKey != null) { + // Reuse the existing element that equals to "key" without adding "key" to the map. + return existingKey; + } + mMap.put(key, key); + return key; + } + } static class KeyboardElement { final int mElementId; @@ -99,15 +118,15 @@ public class KeyboardSet { } public Keyboard getMainKeyboard() { - return getKeyboard(false, false); + return getKeyboard(false, false, false); } public Keyboard getSymbolsKeyboard() { - return getKeyboard(true, false); + return getKeyboard(true, false, false); } public Keyboard getSymbolsShiftedKeyboard() { - final Keyboard keyboard = getKeyboard(true, true); + final Keyboard keyboard = getKeyboard(true, false, true); // TODO: Remove this logic once we introduce initial keyboard shift state attribute. // Symbol shift keyboard may have a shift key that has a caps lock style indicator (a.k.a. // sticky shift key). To show or dismiss the indicator, we need to call setShiftLocked() @@ -116,22 +135,23 @@ public class KeyboardSet { return keyboard; } - private Keyboard getKeyboard(boolean isSymbols, boolean isShift) { - final int elementId = KeyboardSet.getElementId(mParams.mMode, isSymbols, isShift); + private Keyboard getKeyboard(boolean isSymbols, boolean isShiftLock, boolean isShift) { + final int elementId = KeyboardSet.getElementId( + mParams.mMode, isSymbols, isShiftLock, isShift); final KeyboardElement keyboardElement = mParams.mElementKeyboards.get(elementId); // TODO: If keyboardElement.mAutoGenerate is true, the keyboard will be auto generated // based on keyboardElement.mKayoutId Keyboard XML definition. final KeyboardId id = KeyboardSet.getKeyboardId(elementId, isSymbols, mParams); - final Keyboard keyboard = getKeyboard(mContext, keyboardElement.mLayoutId, id); + final Keyboard keyboard = getKeyboard(mContext, keyboardElement, id); return keyboard; } public KeyboardId getMainKeyboardId() { - final int elementId = KeyboardSet.getElementId(mParams.mMode, false, false); + final int elementId = KeyboardSet.getElementId(mParams.mMode, false, false, false); return KeyboardSet.getKeyboardId(elementId, false, mParams); } - private Keyboard getKeyboard(Context context, int xmlId, KeyboardId id) { + private Keyboard getKeyboard(Context context, KeyboardElement element, KeyboardId id) { final Resources res = context.getResources(); final SoftReference ref = sKeyboardCache.get(id); Keyboard keyboard = (ref == null) ? null : ref.get(); @@ -140,7 +160,10 @@ public class KeyboardSet { try { final Keyboard.Builder builder = new Keyboard.Builder(context, new Keyboard.Params()); - builder.load(xmlId, id); + if (element.mAutoGenerate) { + builder.setAutoGenerate(mKeysCache); + } + builder.load(element.mLayoutId, id); builder.setTouchPositionCorrectionEnabled(mParams.mTouchPositionCorrectionEnabled); keyboard = builder.build(); } finally { @@ -162,7 +185,8 @@ public class KeyboardSet { return keyboard; } - private static int getElementId(int mode, boolean isSymbols, boolean isShift) { + private static int getElementId(int mode, boolean isSymbols, boolean isShiftLock, + boolean isShift) { switch (mode) { case KeyboardId.MODE_PHONE: return (isSymbols && isShift) @@ -174,6 +198,7 @@ public class KeyboardSet { return isShift ? KeyboardId.ELEMENT_SYMBOLS_SHIFTED : KeyboardId.ELEMENT_SYMBOLS; } + // TODO: Consult isShiftLock and isShift to determine the element. return KeyboardId.ELEMENT_ALPHABET; } }