diff options
Diffstat (limited to 'java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java')
-rw-r--r-- | java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java index 5cbb34119..61f98c8ff 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java @@ -29,33 +29,42 @@ import org.xmlpull.v1.XmlPullParserException; import java.util.Arrays; import java.util.HashMap; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public final class KeyStylesSet { private static final String TAG = KeyStylesSet.class.getSimpleName(); private static final boolean DEBUG = false; + @Nonnull private final HashMap<String, KeyStyle> mStyles = new HashMap<>(); + @Nonnull private final KeyboardTextsSet mTextsSet; + @Nonnull private final KeyStyle mEmptyKeyStyle; + @Nonnull private static final String EMPTY_STYLE_NAME = "<empty>"; - public KeyStylesSet(final KeyboardTextsSet textsSet) { + public KeyStylesSet(@Nonnull final KeyboardTextsSet textsSet) { mTextsSet = textsSet; mEmptyKeyStyle = new EmptyKeyStyle(textsSet); mStyles.put(EMPTY_STYLE_NAME, mEmptyKeyStyle); } private static final class EmptyKeyStyle extends KeyStyle { - EmptyKeyStyle(final KeyboardTextsSet textsSet) { + EmptyKeyStyle(@Nonnull final KeyboardTextsSet textsSet) { super(textsSet); } @Override + @Nullable public String[] getStringArray(final TypedArray a, final int index) { return parseStringArray(a, index); } @Override + @Nullable public String getString(final TypedArray a, final int index) { return parseString(a, index); } @@ -76,14 +85,16 @@ public final class KeyStylesSet { private final String mParentStyleName; private final SparseArray<Object> mStyleAttributes = new SparseArray<>(); - public DeclaredKeyStyle(final String parentStyleName, final KeyboardTextsSet textsSet, - final HashMap<String, KeyStyle> styles) { + public DeclaredKeyStyle(@Nonnull final String parentStyleName, + @Nonnull final KeyboardTextsSet textsSet, + @Nonnull final HashMap<String, KeyStyle> styles) { super(textsSet); mParentStyleName = parentStyleName; mStyles = styles; } @Override + @Nullable public String[] getStringArray(final TypedArray a, final int index) { if (a.hasValue(index)) { return parseStringArray(a, index); @@ -98,6 +109,7 @@ public final class KeyStylesSet { } @Override + @Nullable public String getString(final TypedArray a, final int index) { if (a.hasValue(index)) { return parseString(a, index); @@ -176,37 +188,43 @@ public final class KeyStylesSet { public void parseKeyStyleAttributes(final TypedArray keyStyleAttr, final TypedArray keyAttrs, final XmlPullParser parser) throws XmlPullParserException { final String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName); + if (styleName == null) { + throw new XmlParseUtils.ParseException( + KeyboardBuilder.TAG_KEY_STYLE + " has no styleName attribute", parser); + } if (DEBUG) { Log.d(TAG, String.format("<%s styleName=%s />", KeyboardBuilder.TAG_KEY_STYLE, styleName)); if (mStyles.containsKey(styleName)) { - Log.d(TAG, "key-style " + styleName + " is overridden at " + Log.d(TAG, KeyboardBuilder.TAG_KEY_STYLE + " " + styleName + " is overridden at " + parser.getPositionDescription()); } } - String parentStyleName = EMPTY_STYLE_NAME; - if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) { - parentStyleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_parentStyle); - if (!mStyles.containsKey(parentStyleName)) { - throw new XmlParseUtils.ParseException( - "Unknown parentStyle " + parentStyleName, parser); - } + final String parentStyleInAttr = keyStyleAttr.getString( + R.styleable.Keyboard_KeyStyle_parentStyle); + if (parentStyleInAttr != null && !mStyles.containsKey(parentStyleInAttr)) { + throw new XmlParseUtils.ParseException( + "Unknown parentStyle " + parentStyleInAttr, parser); } + final String parentStyleName = (parentStyleInAttr == null) ? EMPTY_STYLE_NAME + : parentStyleInAttr; final DeclaredKeyStyle style = new DeclaredKeyStyle(parentStyleName, mTextsSet, mStyles); style.readKeyAttributes(keyAttrs); mStyles.put(styleName, style); } + @Nonnull public KeyStyle getKeyStyle(final TypedArray keyAttr, final XmlPullParser parser) throws XmlParseUtils.ParseException { - if (!keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) { + final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle); + if (styleName == null) { return mEmptyKeyStyle; } - final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle); - if (!mStyles.containsKey(styleName)) { + final KeyStyle style = mStyles.get(styleName); + if (style == null) { throw new XmlParseUtils.ParseException("Unknown key style: " + styleName, parser); } - return mStyles.get(styleName); + return style; } } |