am 2f83e9e5
: Can override key-style
* commit '2f83e9e53244651dded489f91879f5150fb9c39d': Can override key-style
This commit is contained in:
commit
28b275359a
2 changed files with 47 additions and 56 deletions
|
@ -33,12 +33,10 @@ import android.util.Xml;
|
||||||
|
|
||||||
import com.android.inputmethod.keyboard.internal.KeySpecParser;
|
import com.android.inputmethod.keyboard.internal.KeySpecParser;
|
||||||
import com.android.inputmethod.keyboard.internal.KeySpecParser.MoreKeySpec;
|
import com.android.inputmethod.keyboard.internal.KeySpecParser.MoreKeySpec;
|
||||||
import com.android.inputmethod.keyboard.internal.KeyStyles;
|
|
||||||
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
|
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
|
||||||
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
||||||
import com.android.inputmethod.latin.R;
|
import com.android.inputmethod.latin.R;
|
||||||
import com.android.inputmethod.latin.StringUtils;
|
import com.android.inputmethod.latin.StringUtils;
|
||||||
import com.android.inputmethod.latin.XmlParseUtils;
|
|
||||||
|
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
@ -201,7 +199,6 @@ public class Key {
|
||||||
*/
|
*/
|
||||||
public Key(Resources res, Keyboard.Params params, Keyboard.Builder.Row row,
|
public Key(Resources res, Keyboard.Params params, Keyboard.Builder.Row row,
|
||||||
XmlPullParser parser) throws XmlPullParserException {
|
XmlPullParser parser) throws XmlPullParserException {
|
||||||
final KeyStyles keyStyles = params.mKeyStyles;
|
|
||||||
final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
|
final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
|
||||||
final int keyHeight = row.mRowHeight;
|
final int keyHeight = row.mRowHeight;
|
||||||
mVerticalGap = params.mVerticalGap;
|
mVerticalGap = params.mVerticalGap;
|
||||||
|
@ -210,17 +207,7 @@ public class Key {
|
||||||
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
||||||
R.styleable.Keyboard_Key);
|
R.styleable.Keyboard_Key);
|
||||||
|
|
||||||
final KeyStyle style;
|
final KeyStyle style = params.mKeyStyles.getKeyStyle(keyAttr, parser);
|
||||||
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);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
style = keyStyles.getEmptyKeyStyle();
|
|
||||||
}
|
|
||||||
|
|
||||||
final float keyXPos = row.getKeyX(keyAttr);
|
final float keyXPos = row.getKeyX(keyAttr);
|
||||||
final float keyWidth = row.getKeyWidth(keyAttr, keyXPos);
|
final float keyWidth = row.getKeyWidth(keyAttr, keyXPos);
|
||||||
final int keyYPos = row.getKeyY();
|
final int keyYPos = row.getKeyY();
|
||||||
|
|
|
@ -32,24 +32,19 @@ public class KeyStyles {
|
||||||
private static final String TAG = KeyStyles.class.getSimpleName();
|
private static final String TAG = KeyStyles.class.getSimpleName();
|
||||||
private static final boolean DEBUG = false;
|
private static final boolean DEBUG = false;
|
||||||
|
|
||||||
private final HashMap<String, DeclaredKeyStyle> mStyles =
|
final HashMap<String, KeyStyle> mStyles = new HashMap<String, KeyStyle>();
|
||||||
new HashMap<String, DeclaredKeyStyle>();
|
|
||||||
|
|
||||||
private final KeyboardTextsSet mTextsSet;
|
final KeyboardTextsSet mTextsSet;
|
||||||
private final KeyStyle mEmptyKeyStyle;
|
private final KeyStyle mEmptyKeyStyle;
|
||||||
|
private static final String EMPTY_STYLE_NAME = "<empty>";
|
||||||
|
|
||||||
public KeyStyles(KeyboardTextsSet textsSet) {
|
public KeyStyles(KeyboardTextsSet textsSet) {
|
||||||
mTextsSet = textsSet;
|
mTextsSet = textsSet;
|
||||||
mEmptyKeyStyle = new EmptyKeyStyle(textsSet);
|
mEmptyKeyStyle = new EmptyKeyStyle();
|
||||||
|
mStyles.put(EMPTY_STYLE_NAME, mEmptyKeyStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static abstract class KeyStyle {
|
public abstract class KeyStyle {
|
||||||
protected final KeyboardTextsSet mTextsSet;
|
|
||||||
|
|
||||||
public KeyStyle(KeyboardTextsSet textsSet) {
|
|
||||||
mTextsSet = textsSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract String[] getStringArray(TypedArray a, int index);
|
public abstract String[] getStringArray(TypedArray a, int index);
|
||||||
public abstract String getString(TypedArray a, int index);
|
public abstract String getString(TypedArray a, int index);
|
||||||
public abstract int getInt(TypedArray a, int index, int defaultValue);
|
public abstract int getInt(TypedArray a, int index, int defaultValue);
|
||||||
|
@ -70,11 +65,7 @@ public class KeyStyles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class EmptyKeyStyle extends KeyStyle {
|
class EmptyKeyStyle extends KeyStyle {
|
||||||
public EmptyKeyStyle(KeyboardTextsSet textsSet) {
|
|
||||||
super(textsSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getStringArray(TypedArray a, int index) {
|
public String[] getStringArray(TypedArray a, int index) {
|
||||||
return parseStringArray(a, index);
|
return parseStringArray(a, index);
|
||||||
|
@ -96,11 +87,12 @@ public class KeyStyles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class DeclaredKeyStyle extends KeyStyle {
|
private class DeclaredKeyStyle extends KeyStyle {
|
||||||
|
private final String mParentStyleName;
|
||||||
private final HashMap<Integer, Object> mStyleAttributes = new HashMap<Integer, Object>();
|
private final HashMap<Integer, Object> mStyleAttributes = new HashMap<Integer, Object>();
|
||||||
|
|
||||||
public DeclaredKeyStyle(KeyboardTextsSet textsSet) {
|
public DeclaredKeyStyle(String parentStyleName) {
|
||||||
super(textsSet);
|
mParentStyleName = parentStyleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -108,7 +100,11 @@ public class KeyStyles {
|
||||||
if (a.hasValue(index)) {
|
if (a.hasValue(index)) {
|
||||||
return parseStringArray(a, index);
|
return parseStringArray(a, index);
|
||||||
}
|
}
|
||||||
return (String[])mStyleAttributes.get(index);
|
if (mStyleAttributes.containsKey(index)) {
|
||||||
|
return (String[])mStyleAttributes.get(index);
|
||||||
|
}
|
||||||
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
||||||
|
return parentStyle.getStringArray(a, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -116,7 +112,11 @@ public class KeyStyles {
|
||||||
if (a.hasValue(index)) {
|
if (a.hasValue(index)) {
|
||||||
return parseString(a, index);
|
return parseString(a, index);
|
||||||
}
|
}
|
||||||
return (String)mStyleAttributes.get(index);
|
if (mStyleAttributes.containsKey(index)) {
|
||||||
|
return (String)mStyleAttributes.get(index);
|
||||||
|
}
|
||||||
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
||||||
|
return parentStyle.getString(a, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -124,15 +124,21 @@ public class KeyStyles {
|
||||||
if (a.hasValue(index)) {
|
if (a.hasValue(index)) {
|
||||||
return a.getInt(index, defaultValue);
|
return a.getInt(index, defaultValue);
|
||||||
}
|
}
|
||||||
final Integer styleValue = (Integer)mStyleAttributes.get(index);
|
if (mStyleAttributes.containsKey(index)) {
|
||||||
return styleValue != null ? styleValue : defaultValue;
|
return (Integer)mStyleAttributes.get(index);
|
||||||
|
}
|
||||||
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
||||||
|
return parentStyle.getInt(a, index, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getFlag(TypedArray a, int index) {
|
public int getFlag(TypedArray a, int index) {
|
||||||
final int value = a.getInt(index, 0);
|
int value = a.getInt(index, 0);
|
||||||
final Integer styleValue = (Integer)mStyleAttributes.get(index);
|
if (mStyleAttributes.containsKey(index)) {
|
||||||
return (styleValue != null ? styleValue : 0) | value;
|
value |= (Integer)mStyleAttributes.get(index);
|
||||||
|
}
|
||||||
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
||||||
|
return value | parentStyle.getFlag(a, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void readKeyAttributes(TypedArray keyAttr) {
|
void readKeyAttributes(TypedArray keyAttr) {
|
||||||
|
@ -177,10 +183,6 @@ public class KeyStyles {
|
||||||
mStyleAttributes.put(index, parseStringArray(a, index));
|
mStyleAttributes.put(index, parseStringArray(a, index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void addParentStyleAttributes(DeclaredKeyStyle parentStyle) {
|
|
||||||
mStyleAttributes.putAll(parentStyle.mStyleAttributes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs,
|
public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs,
|
||||||
|
@ -195,26 +197,28 @@ public class KeyStyles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final DeclaredKeyStyle style = new DeclaredKeyStyle(mTextsSet);
|
String parentStyleName = EMPTY_STYLE_NAME;
|
||||||
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
|
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
|
||||||
final String parentStyle = keyStyleAttr.getString(
|
parentStyleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_parentStyle);
|
||||||
R.styleable.Keyboard_KeyStyle_parentStyle);
|
if (!mStyles.containsKey(parentStyleName)) {
|
||||||
final DeclaredKeyStyle parent = mStyles.get(parentStyle);
|
|
||||||
if (parent == null) {
|
|
||||||
throw new XmlParseUtils.ParseException(
|
throw new XmlParseUtils.ParseException(
|
||||||
"Unknown parentStyle " + parentStyle, parser);
|
"Unknown parentStyle " + parentStyleName, parser);
|
||||||
}
|
}
|
||||||
style.addParentStyleAttributes(parent);
|
|
||||||
}
|
}
|
||||||
|
final DeclaredKeyStyle style = new DeclaredKeyStyle(parentStyleName);
|
||||||
style.readKeyAttributes(keyAttrs);
|
style.readKeyAttributes(keyAttrs);
|
||||||
mStyles.put(styleName, style);
|
mStyles.put(styleName, style);
|
||||||
}
|
}
|
||||||
|
|
||||||
public KeyStyle getKeyStyle(String styleName) {
|
public KeyStyle getKeyStyle(TypedArray keyAttr, XmlPullParser parser)
|
||||||
|
throws XmlParseUtils.ParseException {
|
||||||
|
if (!keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) {
|
||||||
|
return mEmptyKeyStyle;
|
||||||
|
}
|
||||||
|
final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle);
|
||||||
|
if (!mStyles.containsKey(styleName)) {
|
||||||
|
throw new XmlParseUtils.ParseException("Unknown key style: " + styleName, parser);
|
||||||
|
}
|
||||||
return mStyles.get(styleName);
|
return mStyles.get(styleName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public KeyStyle getEmptyKeyStyle() {
|
|
||||||
return mEmptyKeyStyle;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue