am 530f5d7e: Merge "Support @icon/icon_name for more keys specification"

* commit '530f5d7e155011ff4fa520002c68acb78b195497':
  Support @icon/icon_name for more keys specification
This commit is contained in:
Tadashi G. Takaoka 2012-01-20 03:29:02 -08:00 committed by Android Git Automerger
commit d5937e08e5
8 changed files with 58 additions and 51 deletions

View file

@ -70,10 +70,8 @@
<string name="keylabel_for_symbols_percent">\u066a</string>
<string name="more_keys_for_comma">,</string>
<string name="more_keys_for_f1">,</string>
<!-- @icon/3 is iconSettingsKey -->
<string name="more_keys_for_f1_settings">\\,,\@icon/3|\@integer/key_settings</string>
<!-- @icon/7 is iconTabKey -->
<string name="more_keys_for_f1_navigate">\\,,\@icon/7|\@integer/key_tab</string>
<string name="more_keys_for_f1_settings">\\,,\@icon/settingsKey|\@integer/key_settings</string>
<string name="more_keys_for_f1_navigate">\\,,\@icon/tabKey|\@integer/key_tab</string>
<string name="more_keys_for_symbols_question">\?</string>
<string name="more_keys_for_symbols_semicolon">;</string>
<string name="more_keys_for_symbols_percent">%,‰</string>

View file

@ -248,12 +248,10 @@
<!-- This should be aligned with the KeyboardIcons.ICONS_TO_ATTRS_MAP -->
<enum name="iconShiftKey" value="1" />
<enum name="iconDeleteKey" value="2" />
<!-- This is also represented as "@icon/3" in keyboard layout XML. -->
<enum name="iconSettingsKey" value="3" />
<enum name="iconSpaceKey" value="4" />
<enum name="iconReturnKey" value="5" />
<enum name="iconSearchKey" value="6" />
<!-- This is also represented as "@icon/7" in keyboard layout XML. -->
<enum name="iconTabKey" value="7" />
<enum name="iconShortcutKey" value="8" />
<enum name="iconShortcutForLabel" value="9" />

View file

@ -91,10 +91,8 @@
<string name="keylabel_for_symbols_percent">%</string>
<string name="more_keys_for_comma"></string>
<string name="more_keys_for_f1"></string>
<!-- @icon/3 is iconSettingsKey -->
<string name="more_keys_for_f1_settings">\@icon/3|\@integer/key_settings</string>
<!-- @icon/7 is iconTabKey -->
<string name="more_keys_for_f1_navigate">\@icon/7|\@integer/key_tab</string>
<string name="more_keys_for_f1_settings">\@icon/settingsKey|\@integer/key_settings</string>
<string name="more_keys_for_f1_navigate">\@icon/tabKey|\@integer/key_tab</string>
<string name="more_keys_for_symbols_question">¿</string>
<string name="more_keys_for_symbols_semicolon"></string>
<string name="more_keys_for_symbols_percent"></string>

View file

@ -34,7 +34,7 @@
<key-style
latin:styleName="f2PopupStyle"
latin:keyLabelFlags="hasPopupHint"
latin:moreKeys="\@icon/3|\@integer/key_settings"
latin:moreKeys="\@icon/settingsKey|\@integer/key_settings"
latin:backgroundType="functional" />
</default>
</switch>

View file

@ -166,7 +166,12 @@ public class Key {
}
private static Drawable getIcon(Keyboard.Params params, String moreKeySpec) {
return params.mIconsSet.getIconByIconId(MoreKeySpecParser.getIconId(moreKeySpec));
final int iconAttrId = MoreKeySpecParser.getIconAttrId(moreKeySpec);
if (iconAttrId == KeyboardIconsSet.ICON_UNDEFINED) {
return null;
} else {
return params.mIconsSet.getIconByAttrId(iconAttrId);
}
}
/**
@ -277,12 +282,15 @@ 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.getIconByIconId(style.getInt(keyAttr,
final int previewIconAttrId = KeyboardIconsSet.getIconAttrId(style.getInt(keyAttr,
R.styleable.Keyboard_Key_keyIconPreview, KeyboardIconsSet.ICON_UNDEFINED));
mIcon = iconsSet.getIconByIconId(style.getInt(keyAttr,
mPreviewIcon = iconsSet.getIconByAttrId(previewIconAttrId);
final int iconAttrId = KeyboardIconsSet.getIconAttrId(style.getInt(keyAttr,
R.styleable.Keyboard_Key_keyIcon, KeyboardIconsSet.ICON_UNDEFINED));
mDisabledIcon = iconsSet.getIconByIconId(style.getInt(keyAttr,
mIcon = iconsSet.getIconByAttrId(iconAttrId);
final int disabledIconAttrId = KeyboardIconsSet.getIconAttrId(style.getInt(keyAttr,
R.styleable.Keyboard_Key_keyIconDisabled, KeyboardIconsSet.ICON_UNDEFINED));
mDisabledIcon = iconsSet.getIconByAttrId(disabledIconAttrId);
mHintLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLabel);
mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel);

View file

@ -31,34 +31,36 @@ public class KeyboardIconsSet {
private static final String TAG = KeyboardIconsSet.class.getSimpleName();
public static final int ICON_UNDEFINED = 0;
private static final int ATTR_UNDEFINED = 0;
private final Map<Integer, Drawable> mIcons = new HashMap<Integer, Drawable>();
// 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 Map<String, Integer> NAME_TO_ATTRS_MAP = new HashMap<String, Integer>();
private static final Collection<Integer> VALID_ATTRS;
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);
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);
VALID_ATTRS = ICONS_TO_ATTRS_MAP.values();
}
private static void addIconIdMap(int iconId, int attrId) {
private static void addIconIdMap(int iconId, String name, Integer attrId) {
ICONS_TO_ATTRS_MAP.put(iconId, attrId);
NAME_TO_ATTRS_MAP.put(name, attrId);
}
public void loadIcons(final TypedArray keyboardAttrs) {
@ -76,18 +78,29 @@ public class KeyboardIconsSet {
}
}
public Drawable getIconByIconId(final Integer iconId) {
public static int getIconAttrId(final Integer iconId) {
if (iconId == ICON_UNDEFINED) {
return null;
return ATTR_UNDEFINED;
}
final Integer attrId = ICONS_TO_ATTRS_MAP.get(iconId);
if (attrId == null) {
throw new IllegalArgumentException("icon id is out of range: " + iconId);
}
return getIconByAttrId(attrId);
return attrId;
}
public static int getIconAttrId(final String iconName) {
final Integer attrId = NAME_TO_ATTRS_MAP.get(iconName);
if (attrId == null) {
throw new IllegalArgumentException("unknown icon name: " + iconName);
}
return attrId;
}
public Drawable getIconByAttrId(final Integer attrId) {
if (attrId == ATTR_UNDEFINED) {
return null;
}
if (!VALID_ATTRS.contains(attrId)) {
throw new IllegalArgumentException("unknown icon attribute id: " + attrId);
}

View file

@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard.internal;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.latin.R;
@ -32,15 +31,13 @@ import java.util.ArrayList;
* Each "more key" specification is one of the following:
* - A single letter (Letter)
* - Label optionally followed by keyOutputText or code (keyLabel|keyOutputText).
* - Icon followed by keyOutputText or code (@icon/icon_number|@integer/key_code)
* - Icon followed by keyOutputText or code (@icon/icon_name|@integer/key_code)
* Special character, comma ',' backslash '\', and bar '|' can be escaped by '\'
* character.
* Note that the character '@' and '\' are also parsed by XML parser and CSV parser as well.
* See {@link KeyboardIconsSet} about icon_number.
*/
public class MoreKeySpecParser {
private static final String TAG = MoreKeySpecParser.class.getSimpleName();
private static final char LABEL_END = '|';
private static final String PREFIX_ICON = Utils.PREFIX_AT + "icon" + Utils.SUFFIX_SLASH;
private static final String PREFIX_CODE = Utils.PREFIX_AT + "integer" + Utils.SUFFIX_SLASH;
@ -167,16 +164,11 @@ public class MoreKeySpecParser {
return Keyboard.CODE_OUTPUT_TEXT;
}
public static int getIconId(String moreKeySpec) {
public static int getIconAttrId(String moreKeySpec) {
if (hasIcon(moreKeySpec)) {
final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length() + 1);
final String iconId = moreKeySpec.substring(PREFIX_ICON.length(), end);
try {
return Integer.valueOf(iconId);
} catch (NumberFormatException e) {
Log.w(TAG, "illegal icon id specified: " + iconId);
return KeyboardIconsSet.ICON_UNDEFINED;
}
final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length());
final String name = moreKeySpec.substring(PREFIX_ICON.length(), end);
return KeyboardIconsSet.getIconAttrId(name);
}
return KeyboardIconsSet.ICON_UNDEFINED;
}

View file

@ -25,11 +25,11 @@ import com.android.inputmethod.latin.R;
public class MoreKeySpecParserTests extends AndroidTestCase {
private Resources mRes;
private static final int ICON_SETTINGS_KEY = 5;
private static final int ICON_SETTINGS_KEY = R.styleable.Keyboard_iconSettingsKey;
private static final int ICON_UNDEFINED = KeyboardIconsSet.ICON_UNDEFINED;
private static final String CODE_SETTINGS = "@integer/key_settings";
private static final String ICON_SETTINGS = "@icon/" + ICON_SETTINGS_KEY;
private static final String ICON_SETTINGS = "@icon/settingsKey";
private static final String CODE_NON_EXISTING = "@integer/non_existing";
private static final String ICON_NON_EXISTING = "@icon/non_existing";
@ -53,7 +53,7 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
String actualOutputText = MoreKeySpecParser.getOutputText(moreKeySpec);
assertEquals(message + ": ouptputText:", expectedOutputText, actualOutputText);
int actualIcon = MoreKeySpecParser.getIconId(moreKeySpec);
int actualIcon = MoreKeySpecParser.getIconAttrId(moreKeySpec);
assertEquals(message + ": icon:", expectedIcon, actualIcon);
int actualCode = MoreKeySpecParser.getCode(mRes, moreKeySpec);
@ -201,7 +201,7 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
null, null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);
assertParserError("Icon without code", ICON_SETTINGS,
null, null, ICON_SETTINGS_KEY, Keyboard.CODE_UNSPECIFIED);
assertParser("Non existing icon", ICON_NON_EXISTING + "|abc",
assertParserError("Non existing icon", ICON_NON_EXISTING + "|abc",
null, "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParserError("Non existing code", "abc|" + CODE_NON_EXISTING,
"abc", null, ICON_UNDEFINED, Keyboard.CODE_UNSPECIFIED);