diff options
author | 2011-12-19 01:11:09 +0900 | |
---|---|---|
committer | 2011-12-19 01:11:09 +0900 | |
commit | 42fcb2de641c4cd5d57f34889c8752401e35dcc8 (patch) | |
tree | d0ae470e137c219fe14a1ed1c10e41c3d00fa9df /java/src | |
parent | 4e1dab8cfaad891fe041ed8d71893186c05cef71 (diff) | |
download | latinime-42fcb2de641c4cd5d57f34889c8752401e35dcc8.tar.gz latinime-42fcb2de641c4cd5d57f34889c8752401e35dcc8.tar.xz latinime-42fcb2de641c4cd5d57f34889c8752401e35dcc8.zip |
Get rid of public reference to KeyboardIconsSet.ICON_* constants
As a consequence,
* Add Key.iconDisabledKey has been introduced and Key.getIcon honors
the enabled state of the key.
* The attribute id of disabled icon for shortcut key,
Keyboard_iconShortcutKeyDisabled, is renamed to
Keyboard_iconDisbledShortcutKey
* KeyboardIconsSet has getIconByIconId and getIconByAttrId methods
instead of getIcon.
Bug: 5778201
Change-Id: Ica93b073b9a04acd18ead7a33b60e3c6d813e7a1
Diffstat (limited to 'java/src')
5 files changed, 73 insertions, 82 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index b7d8c4a3e..e1e74fc9a 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -72,9 +72,11 @@ public class Key { private static final int LABEL_FLAGS_WITH_ICON_RIGHT = 0x2000; private static final int LABEL_FLAGS_AUTO_X_SCALE = 0x4000; - // TODO: This should be public final + // TODO: These icon references could be int (icon attribute id) /** Icon to display instead of a label. Icon takes precedence over a label */ private Drawable mIcon; + /** Icon for disabled state */ + private Drawable mDisabledIcon; /** Preview version of the icon, for the preview popup */ public final Drawable mPreviewIcon; @@ -164,7 +166,7 @@ public class Key { } private static Drawable getIcon(Keyboard.Params params, String moreKeySpec) { - return params.mIconsSet.getIcon(MoreKeySpecParser.getIconId(moreKeySpec)); + return params.mIconsSet.getIconByIconId(MoreKeySpecParser.getIconId(moreKeySpec)); } /** @@ -198,6 +200,7 @@ public class Key { mCode = code; mAltCode = Keyboard.CODE_DUMMY; mIcon = icon; + mDisabledIcon = null; mPreviewIcon = null; // Horizontal gap is divided equally to both sides of the key. mX = x + mHorizontalGap / 2; @@ -274,10 +277,12 @@ public class Key { R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0); mVisualInsetsRight = (int) Keyboard.Builder.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsRight, params.mBaseWidth, 0); - mPreviewIcon = iconsSet.getIcon(style.getInt(keyAttr, + mPreviewIcon = iconsSet.getIconByIconId(style.getInt(keyAttr, R.styleable.Keyboard_Key_keyIconPreview, KeyboardIconsSet.ICON_UNDEFINED)); - mIcon = iconsSet.getIcon(style.getInt(keyAttr, R.styleable.Keyboard_Key_keyIcon, - KeyboardIconsSet.ICON_UNDEFINED)); + mIcon = iconsSet.getIconByIconId(style.getInt(keyAttr, + R.styleable.Keyboard_Key_keyIcon, KeyboardIconsSet.ICON_UNDEFINED)); + mDisabledIcon = iconsSet.getIconByIconId(style.getInt(keyAttr, + R.styleable.Keyboard_Key_keyIconDisabled, KeyboardIconsSet.ICON_UNDEFINED)); mHintLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLabel); mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel); @@ -459,7 +464,7 @@ public class Key { } public Drawable getIcon() { - return mIcon; + return mEnabled ? mIcon : mDisabledIcon; } // TODO: Get rid of this method. diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 444360d9a..9f4b8811d 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -192,9 +192,10 @@ public class Keyboard { // To represent "shift locked" state. The highlight is handled by background image that // might be a StateListDrawable. key.setHighlightOn(newShiftLockState); - final int iconId = newShiftLockState ? KeyboardIconsSet.ICON_SHIFT_KEY_SHIFTED - : KeyboardIconsSet.ICON_SHIFT_KEY; - key.setIcon(mIconsSet.getIcon(iconId)); + final int attrId = newShiftLockState + ? R.styleable.Keyboard_iconShiftKeyShifted + : R.styleable.Keyboard_iconShiftKey; + key.setIcon(mIconsSet.getIconByAttrId(attrId)); } mShiftState.setShiftLocked(newShiftLockState); } @@ -208,9 +209,10 @@ public class Keyboard { void setShifted(boolean newShiftState) { if (!mShiftState.isShiftLocked()) { for (final Key key : mShiftKeys) { - final int iconId = newShiftState ? KeyboardIconsSet.ICON_SHIFT_KEY_SHIFTED - : KeyboardIconsSet.ICON_SHIFT_KEY; - key.setIcon(mIconsSet.getIcon(iconId)); + final int attrId = newShiftState + ? R.styleable.Keyboard_iconShiftKeyShifted + : R.styleable.Keyboard_iconShiftKey; + key.setIcon(mIconsSet.getIconByAttrId(attrId)); } } mShiftState.setShifted(newShiftState); diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index aefa50b22..3433cd455 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -47,7 +47,6 @@ import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.deprecated.VoiceProxy; import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy; import com.android.inputmethod.keyboard.PointerTracker.TimerProxy; -import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; @@ -357,7 +356,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke mMoreKeysPanelCache.clear(); mSpaceKey = keyboard.getKey(Keyboard.CODE_SPACE); - mSpaceIcon = keyboard.mIconsSet.getIcon(KeyboardIconsSet.ICON_SPACE_KEY); + mSpaceIcon = keyboard.mIconsSet.getIconByAttrId(R.styleable.Keyboard_iconSpaceKey); final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap; mSpacebarTextSize = keyHeight * mSpacebarTextRatio; mSpacebarLocale = keyboard.mId.mLocale; @@ -750,9 +749,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke final Key shortcutKey = keyboard.getKey(Keyboard.CODE_SHORTCUT); if (shortcutKey == null) return; shortcutKey.setEnabled(available); - final int iconId = available ? KeyboardIconsSet.ICON_SHORTCUT_KEY - : KeyboardIconsSet.ICON_SHORTCUT_KEY_DISABLED; - shortcutKey.setIcon(keyboard.mIconsSet.getIcon(iconId)); invalidateKey(shortcutKey); } diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java index 9b99dc42f..5dd8340fc 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java @@ -161,6 +161,7 @@ public class KeyStyles { readTextArray(keyAttr, R.styleable.Keyboard_Key_moreKeys); readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags); readInt(keyAttr, R.styleable.Keyboard_Key_keyIcon); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIconDisabled); readInt(keyAttr, R.styleable.Keyboard_Key_keyIconPreview); readInt(keyAttr, R.styleable.Keyboard_Key_maxMoreKeysColumn); readInt(keyAttr, R.styleable.Keyboard_Key_backgroundType); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java index c8a75a92a..6313a61b5 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -23,88 +23,75 @@ import android.util.Log; import com.android.inputmethod.latin.R; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + public class KeyboardIconsSet { private static final String TAG = KeyboardIconsSet.class.getSimpleName(); public static final int ICON_UNDEFINED = 0; - // TODO: Make all enums private - // This should be aligned with Keyboard.keyIcon enum. - public static final int ICON_SHIFT_KEY = 1; - private static final int ICON_DELETE_KEY = 2; - // This is also represented as "@icon/3" in keyboard layout XML. - private static final int ICON_SETTINGS_KEY = 3; - public static final int ICON_SPACE_KEY = 4; - private static final int ICON_RETURN_KEY = 5; - private static final int ICON_SEARCH_KEY = 6; - // This is also represented as "@icon/7" in keyboard layout XML. - private static final int ICON_TAB_KEY = 7; - public static final int ICON_SHORTCUT_KEY = 8; - private static final int ICON_SHORTCUT_FOR_LABEL = 9; - public static final int ICON_SHORTCUT_KEY_DISABLED = 10; - private static final int ICON_SPACE_KEY_FOR_NUMBER_LAYOUT = 11; - public static final int ICON_SHIFT_KEY_SHIFTED = 12; - // This should be aligned with Keyboard.keyIconPreview enum. - private static final int ICON_PREVIEW_TAB_KEY = 13; + private final Map<Integer, Drawable> mIcons = new HashMap<Integer, Drawable>(); - private static final int ICON_LAST = 13; + // The key value should be aligned with the enum value of Keyboard.icon*. + private static final Map<Integer, Integer> ICONS_TO_ATTRS_MAP = new HashMap<Integer, Integer>(); + private static final Collection<Integer> VALID_ATTRS; - private final Drawable mIcons[] = new Drawable[ICON_LAST + 1]; + static { + addIconIdMap(1, R.styleable.Keyboard_iconShiftKey); + addIconIdMap(2, R.styleable.Keyboard_iconDeleteKey); + // This is also represented as "@icon/3" in keyboard layout XML. + addIconIdMap(3, R.styleable.Keyboard_iconSettingsKey); + addIconIdMap(4, R.styleable.Keyboard_iconSpaceKey); + addIconIdMap(5, R.styleable.Keyboard_iconReturnKey); + addIconIdMap(6, R.styleable.Keyboard_iconSearchKey); + // This is also represented as "@icon/7" in keyboard layout XML. + addIconIdMap(7, R.styleable.Keyboard_iconTabKey); + addIconIdMap(8, R.styleable.Keyboard_iconShortcutKey); + addIconIdMap(9, R.styleable.Keyboard_iconShortcutForLabel); + addIconIdMap(10, R.styleable.Keyboard_iconSpaceKeyForNumberLayout); + addIconIdMap(11, R.styleable.Keyboard_iconShiftKeyShifted); + addIconIdMap(12, R.styleable.Keyboard_iconDisabledShortcutKey); + addIconIdMap(13, R.styleable.Keyboard_iconPreviewTabKey); + VALID_ATTRS = ICONS_TO_ATTRS_MAP.values(); + } - private static final int getIconId(final int attrIndex) { - switch (attrIndex) { - case R.styleable.Keyboard_iconShiftKey: - return ICON_SHIFT_KEY; - case R.styleable.Keyboard_iconDeleteKey: - return ICON_DELETE_KEY; - case R.styleable.Keyboard_iconSettingsKey: - return ICON_SETTINGS_KEY; - case R.styleable.Keyboard_iconSpaceKey: - return ICON_SPACE_KEY; - case R.styleable.Keyboard_iconReturnKey: - return ICON_RETURN_KEY; - case R.styleable.Keyboard_iconSearchKey: - return ICON_SEARCH_KEY; - case R.styleable.Keyboard_iconTabKey: - return ICON_TAB_KEY; - case R.styleable.Keyboard_iconShortcutKey: - return ICON_SHORTCUT_KEY; - case R.styleable.Keyboard_iconShortcutForLabel: - return ICON_SHORTCUT_FOR_LABEL; - case R.styleable.Keyboard_iconShortcutKeyDisabled: - return ICON_SHORTCUT_KEY_DISABLED; - case R.styleable.Keyboard_iconSpaceKeyForNumberLayout: - return ICON_SPACE_KEY_FOR_NUMBER_LAYOUT; - case R.styleable.Keyboard_iconShiftKeyShifted: - return ICON_SHIFT_KEY_SHIFTED; - case R.styleable.Keyboard_iconPreviewTabKey: - return ICON_PREVIEW_TAB_KEY; - default: - return ICON_UNDEFINED; - } + private static void addIconIdMap(int iconId, int attrId) { + ICONS_TO_ATTRS_MAP.put(iconId, attrId); } public void loadIcons(final TypedArray keyboardAttrs) { - final int count = keyboardAttrs.getIndexCount(); - for (int i = 0; i < count; i++) { - final int attrIndex = keyboardAttrs.getIndex(i); - final int iconId = getIconId(attrIndex); - if (iconId != ICON_UNDEFINED) { - try { - mIcons[iconId] = setDefaultBounds(keyboardAttrs.getDrawable(attrIndex)); - } catch (Resources.NotFoundException e) { - Log.w(TAG, "Drawable resource for icon #" + iconId + " not found"); - } + for (final Integer attrId : VALID_ATTRS) { + try { + final Drawable icon = keyboardAttrs.getDrawable(attrId); + if (icon == null) continue; + setDefaultBounds(icon); + mIcons.put(attrId, icon); + } catch (Resources.NotFoundException e) { + Log.w(TAG, "Drawable resource for icon #" + + keyboardAttrs.getResources().getResourceEntryName(attrId) + + " not found"); } } } - public Drawable getIcon(final int iconId) { - if (iconId == ICON_UNDEFINED) + public Drawable getIconByIconId(final Integer iconId) { + if (iconId == ICON_UNDEFINED) { return null; - if (iconId < 0 || iconId >= mIcons.length) + } + final Integer attrId = ICONS_TO_ATTRS_MAP.get(iconId); + if (attrId == null) { throw new IllegalArgumentException("icon id is out of range: " + iconId); - return mIcons[iconId]; + } + return getIconByAttrId(attrId); + } + + public Drawable getIconByAttrId(final Integer attrId) { + if (!VALID_ATTRS.contains(attrId)) { + throw new IllegalArgumentException("unknown icon attribute id: " + attrId); + } + return mIcons.get(attrId); } private static Drawable setDefaultBounds(final Drawable icon) { |