diff options
Diffstat (limited to 'java/src')
5 files changed, 87 insertions, 85 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 68858e5bf..0d78c39f2 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -16,6 +16,12 @@ package com.android.inputmethod.keyboard; +import static com.android.inputmethod.keyboard.Keyboard.CODE_OUTPUT_TEXT; +import static com.android.inputmethod.keyboard.Keyboard.CODE_SHIFT; +import static com.android.inputmethod.keyboard.Keyboard.CODE_SWITCH_ALPHA_SYMBOL; +import static com.android.inputmethod.keyboard.Keyboard.CODE_UNSPECIFIED; +import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UNDEFINED; + import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Rect; @@ -170,11 +176,11 @@ public class Key { mLabel = label; mOutputText = outputText; mCode = code; - mEnabled = (code != Keyboard.CODE_UNSPECIFIED); - mAltCode = Keyboard.CODE_UNSPECIFIED; + mEnabled = (code != CODE_UNSPECIFIED); + mAltCode = CODE_UNSPECIFIED; mIconId = iconId; - mDisabledIconId = KeyboardIconsSet.ICON_UNDEFINED; - mPreviewIconId = KeyboardIconsSet.ICON_UNDEFINED; + mDisabledIconId = ICON_UNDEFINED; + mPreviewIconId = ICON_UNDEFINED; // Horizontal gap is divided equally to both sides of the key. mX = x + mHorizontalGap / 2; mY = y; @@ -208,9 +214,9 @@ public class Key { if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) { String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle); style = keyStyles.getKeyStyle(styleName); - if (style == null) - throw new XmlParseUtils.ParseException( - "Unknown key style: " + styleName, parser); + if (style == null) { + throw new XmlParseUtils.ParseException("Unknown key style: " + styleName, parser); + } } else { style = keyStyles.getEmptyKeyStyle(); } @@ -235,12 +241,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); - mPreviewIconId = style.getInt(keyAttr, - R.styleable.Keyboard_Key_keyIconPreview, KeyboardIconsSet.ICON_UNDEFINED); - mIconId = style.getInt(keyAttr, - R.styleable.Keyboard_Key_keyIcon, KeyboardIconsSet.ICON_UNDEFINED); - mDisabledIconId = style.getInt(keyAttr, - R.styleable.Keyboard_Key_keyIconDisabled, KeyboardIconsSet.ICON_UNDEFINED); + mIconId = KeySpecParser.getIconId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyIcon)); + mDisabledIconId = KeySpecParser.getIconId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyIconDisabled)); + mPreviewIconId = KeySpecParser.getIconId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyIconPreview)); mLabelFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags) | row.getDefaultKeyLabelFlags(); @@ -272,8 +278,8 @@ public class Key { if ((mLabelFlags & LABEL_FLAGS_DISABLE_ADDITIONAL_MORE_KEYS) != 0) { additionalMoreKeys = null; } else { - additionalMoreKeys = style.getStringArray( - keyAttr, R.styleable.Keyboard_Key_additionalMoreKeys); + additionalMoreKeys = style.getStringArray(keyAttr, + R.styleable.Keyboard_Key_additionalMoreKeys); } moreKeys = KeySpecParser.insertAdditionalMoreKeys(moreKeys, additionalMoreKeys); if (moreKeys != null) { @@ -291,22 +297,21 @@ public class Key { if ((mLabelFlags & LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL) != 0) { mLabel = params.mId.mCustomActionLabel; } else { - mLabel = adjustCaseOfStringForKeyboardId(style.getString( - keyAttr, R.styleable.Keyboard_Key_keyLabel), preserveCase, params.mId); + mLabel = adjustCaseOfStringForKeyboardId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyLabel), preserveCase, params.mId); } if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) { mHintLabel = null; } else { - mHintLabel = adjustCaseOfStringForKeyboardId(style.getString( - keyAttr, R.styleable.Keyboard_Key_keyHintLabel), preserveCase, params.mId); + mHintLabel = adjustCaseOfStringForKeyboardId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyHintLabel), preserveCase, params.mId); } - String outputText = adjustCaseOfStringForKeyboardId(style.getString( - keyAttr, R.styleable.Keyboard_Key_keyOutputText), preserveCase, params.mId); - final int code = KeySpecParser.parseCode(style.getString( - keyAttr, R.styleable.Keyboard_Key_code), - params.mCodesSet, Keyboard.CODE_UNSPECIFIED); + String outputText = adjustCaseOfStringForKeyboardId(style.getString(keyAttr, + R.styleable.Keyboard_Key_keyOutputText), preserveCase, params.mId); + final int code = KeySpecParser.parseCode(style.getString(keyAttr, + R.styleable.Keyboard_Key_code), params.mCodesSet, CODE_UNSPECIFIED); // Choose the first letter of the label as primary code if not specified. - if (code == Keyboard.CODE_UNSPECIFIED && TextUtils.isEmpty(outputText) + if (code == CODE_UNSPECIFIED && TextUtils.isEmpty(outputText) && !TextUtils.isEmpty(mLabel)) { if (StringUtils.codePointCount(mLabel) == 1) { // Use the first letter of the hint label if shiftedLetterActivated flag is @@ -321,22 +326,21 @@ public class Key { // In some locale and case, the character might be represented by multiple code // points, such as upper case Eszett of German alphabet. outputText = mLabel; - mCode = Keyboard.CODE_OUTPUT_TEXT; + mCode = CODE_OUTPUT_TEXT; } - } else if (code == Keyboard.CODE_UNSPECIFIED && outputText != null) { + } else if (code == CODE_UNSPECIFIED && outputText != null) { if (StringUtils.codePointCount(outputText) == 1) { mCode = outputText.codePointAt(0); outputText = null; } else { - mCode = Keyboard.CODE_OUTPUT_TEXT; + mCode = CODE_OUTPUT_TEXT; } } else { mCode = adjustCaseOfCodeForKeyboardId(code, preserveCase, params.mId); } mOutputText = outputText; - mAltCode = adjustCaseOfCodeForKeyboardId(KeySpecParser.parseCode(style.getString( - keyAttr, R.styleable.Keyboard_Key_altCode), - params.mCodesSet, Keyboard.CODE_UNSPECIFIED), + mAltCode = adjustCaseOfCodeForKeyboardId(KeySpecParser.parseCode(style.getString(keyAttr, + R.styleable.Keyboard_Key_altCode), params.mCodesSet, CODE_UNSPECIFIED), preserveCase, params.mId); mHashCode = computeHashCode(this); @@ -353,7 +357,7 @@ public class Key { final String text = new String(new int[] { code } , 0, 1); final String casedText = adjustCaseOfStringForKeyboardId(text, preserveCase, id); return StringUtils.codePointCount(casedText) == 1 - ? casedText.codePointAt(0) : Keyboard.CODE_UNSPECIFIED; + ? casedText.codePointAt(0) : CODE_UNSPECIFIED; } private static String adjustCaseOfStringForKeyboardId(String text, boolean preserveCase, @@ -463,11 +467,11 @@ public class Key { } public boolean isShift() { - return mCode == Keyboard.CODE_SHIFT; + return mCode == CODE_SHIFT; } public boolean isModifier() { - return mCode == Keyboard.CODE_SHIFT || mCode == Keyboard.CODE_SWITCH_ALPHA_SYMBOL; + return mCode == CODE_SHIFT || mCode == CODE_SWITCH_ALPHA_SYMBOL; } public boolean isRepeatable() { @@ -586,7 +590,7 @@ public class Key { } public Drawable getPreviewIcon(KeyboardIconsSet iconSet) { - return mPreviewIconId != KeyboardIconsSet.ICON_UNDEFINED + return mPreviewIconId != ICON_UNDEFINED ? iconSet.getIconDrawable(mPreviewIconId) : iconSet.getIconDrawable(mIconId); } @@ -726,7 +730,7 @@ public class Key { * This constructor is being used only for divider in more keys keyboard. */ protected Spacer(Keyboard.Params params, int x, int y, int width, int height) { - super(params, null, null, KeyboardIconsSet.ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED, + super(params, null, null, ICON_UNDEFINED, CODE_UNSPECIFIED, null, x, y, width, height, 0); } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java index e24cfa0e7..97e360e0b 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java @@ -76,14 +76,7 @@ public class KeySpecParser { } private static boolean hasIcon(String moreKeySpec) { - if (moreKeySpec.regionMatches(true, 0, PREFIX_ICON, 0, PREFIX_ICON.length())) { - final int end = indexOfLabelEnd(moreKeySpec, 0); - if (end > 0) { - return true; - } - throw new KeySpecParserError("outputText or code not specified: " + moreKeySpec); - } - return false; + return moreKeySpec.regionMatches(true, 0, PREFIX_ICON, 0, PREFIX_ICON.length()); } private static boolean hasCode(String moreKeySpec) { @@ -219,10 +212,11 @@ public class KeySpecParser { } } - static int getIconId(String moreKeySpec) { - if (hasIcon(moreKeySpec)) { + public static int getIconId(String moreKeySpec) { + if (moreKeySpec != null && hasIcon(moreKeySpec)) { final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length()); - final String name = moreKeySpec.substring(PREFIX_ICON.length(), end); + final String name = (end < 0) ? moreKeySpec.substring(PREFIX_ICON.length()) + : moreKeySpec.substring(PREFIX_ICON.length(), end); return KeyboardIconsSet.getIconId(name); } return KeyboardIconsSet.ICON_UNDEFINED; diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java index 8e0b21607..922a44158 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java @@ -145,9 +145,9 @@ public class KeyStyles { readStringArray(keyAttr, R.styleable.Keyboard_Key_moreKeys); readStringArray(keyAttr, R.styleable.Keyboard_Key_additionalMoreKeys); 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); + readString(keyAttr, R.styleable.Keyboard_Key_keyIcon); + readString(keyAttr, R.styleable.Keyboard_Key_keyIconDisabled); + readString(keyAttr, R.styleable.Keyboard_Key_keyIconPreview); readInt(keyAttr, R.styleable.Keyboard_Key_maxMoreKeysColumn); readInt(keyAttr, R.styleable.Keyboard_Key_backgroundType); readFlag(keyAttr, R.styleable.Keyboard_Key_keyActionFlags); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java index 07636249f..a86a9577f 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -28,48 +28,52 @@ import java.util.HashMap; public class KeyboardIconsSet { private static final String TAG = KeyboardIconsSet.class.getSimpleName(); - // The value should be aligned with the enum value of Key.keyIcon. public static final int ICON_UNDEFINED = 0; - private static final int NUM_ICONS = 16; - - private final Drawable[] mIcons = new Drawable[NUM_ICONS + 1]; + private static final int ATTR_UNDEFINED = 0; private static final HashMap<Integer, Integer> ATTR_ID_TO_ICON_ID = new HashMap<Integer, Integer>(); + // Lower case icon name to icon id map. private static final HashMap<String, Integer> sLowerCaseNameToIdsMap = new HashMap<String, Integer>(); - private static final String[] ICON_NAMES = new String[NUM_ICONS + 1]; - private static final int ATTR_UNDEFINED = 0; - static { - // The key value should be aligned with the enum value of Key.keyIcon. - addIconIdMap(0, "undefined", ATTR_UNDEFINED); - addIconIdMap(1, "shiftKey", R.styleable.Keyboard_iconShiftKey); - addIconIdMap(2, "deleteKey", R.styleable.Keyboard_iconDeleteKey); - addIconIdMap(3, "settingsKey", R.styleable.Keyboard_iconSettingsKey); - addIconIdMap(4, "spaceKey", R.styleable.Keyboard_iconSpaceKey); - addIconIdMap(5, "returnKey", R.styleable.Keyboard_iconReturnKey); - addIconIdMap(6, "searchKey", R.styleable.Keyboard_iconSearchKey); - addIconIdMap(7, "tabKey", R.styleable.Keyboard_iconTabKey); - addIconIdMap(8, "shortcutKey", R.styleable.Keyboard_iconShortcutKey); - addIconIdMap(9, "shortcutForLabel", R.styleable.Keyboard_iconShortcutForLabel); - addIconIdMap(10, "spaceKeyForNumberLayout", - R.styleable.Keyboard_iconSpaceKeyForNumberLayout); - addIconIdMap(11, "shiftKeyShifted", R.styleable.Keyboard_iconShiftKeyShifted); - addIconIdMap(12, "disabledShortcurKey", R.styleable.Keyboard_iconDisabledShortcutKey); - addIconIdMap(13, "previewTabKey", R.styleable.Keyboard_iconPreviewTabKey); - addIconIdMap(14, "languageSwitchKey", R.styleable.Keyboard_iconLanguageSwitchKey); - addIconIdMap(15, "zwnjKey", R.styleable.Keyboard_iconZwnjKey); - addIconIdMap(16, "zwjKey", R.styleable.Keyboard_iconZwjKey); - } + private static final Object[] NAMES_AND_ATTR_IDS = { + "undefined", ATTR_UNDEFINED, + "shift_key", R.styleable.Keyboard_iconShiftKey, + "delete_key", R.styleable.Keyboard_iconDeleteKey, + "settings_key", R.styleable.Keyboard_iconSettingsKey, + "space_key", R.styleable.Keyboard_iconSpaceKey, + "enter_key", R.styleable.Keyboard_iconEnterKey, + "search_key", R.styleable.Keyboard_iconSearchKey, + "tab_key", R.styleable.Keyboard_iconTabKey, + "shortcut_key", R.styleable.Keyboard_iconShortcutKey, + "shortcut_for_label", R.styleable.Keyboard_iconShortcutForLabel, + "space_key_for_number_layout", R.styleable.Keyboard_iconSpaceKeyForNumberLayout, + "shift_key_shifted", R.styleable.Keyboard_iconShiftKeyShifted, + "shortcut_key_disabled", R.styleable.Keyboard_iconShortcutKeyDisabled, + "tab_key_preview", R.styleable.Keyboard_iconTabKeyPreview, + "language_switch_key", R.styleable.Keyboard_iconLanguageSwitchKey, + "zwnj_key", R.styleable.Keyboard_iconZwnjKey, + "zwj_key", R.styleable.Keyboard_iconZwjKey, + }; + + private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2; + private static final String[] ICON_NAMES = new String[NUM_ICONS]; + private final Drawable[] mIcons = new Drawable[NUM_ICONS]; - private static void addIconIdMap(int iconId, String name, int attrId) { - if (attrId != ATTR_UNDEFINED) { - ATTR_ID_TO_ICON_ID.put(attrId, iconId); + static { + int iconId = ICON_UNDEFINED; + for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) { + final String name = (String)NAMES_AND_ATTR_IDS[i]; + final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1]; + if (attrId != ATTR_UNDEFINED) { + ATTR_ID_TO_ICON_ID.put(attrId, iconId); + } + sLowerCaseNameToIdsMap.put(name, iconId); + ICON_NAMES[iconId] = name; + iconId++; } - sLowerCaseNameToIdsMap.put(name.toLowerCase(), iconId); - ICON_NAMES[iconId] = name; } public void loadIcons(final TypedArray keyboardAttrs) { @@ -95,7 +99,7 @@ public class KeyboardIconsSet { return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">"; } - public static int getIconId(final String name) { + static int getIconId(final String name) { Integer iconId = sLowerCaseNameToIdsMap.get(name); if (iconId == null) { iconId = sLowerCaseNameToIdsMap.get(name.toLowerCase()); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardLabelsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardLabelsSet.java index bd95848a9..1ba84e505 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardLabelsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardLabelsSet.java @@ -345,8 +345,8 @@ public final class KeyboardLabelsSet { // U+2663: "♣" BLACK CLUB SUIT /* 108 */ "\u266A,\u2665,\u2660,\u2666,\u2663", /* 109 */ "!fixedColumnOrder!2,!hasLabels!,!label/label_time_am,!label/label_time_pm", - /* 110 */ "!icon/settingsKey|!code/key_settings", - /* 111 */ "!icon/shortcutKey|!code/key_shortcut", + /* 110 */ "!icon/settings_key|!code/key_settings", + /* 111 */ "!icon/shortcut_key|!code/key_shortcut", /* 112 */ "!hasLabels!,!label/label_next_key|!code/key_action_next", /* 113 */ "!hasLabels!,!label/label_previous_key|!code/key_action_previous", // Label for "switch to more symbol" modifier key. Must be short to fit on key! |