Refactor BaseKeyboard parsing code

Change-Id: I241125b5b15c9d6bbba456476f1f4fd3ef9db3f2
This commit is contained in:
Tadashi G. Takaoka 2010-10-21 17:20:51 +09:00
parent d329795d91
commit bf77bb7678

View File

@ -57,14 +57,6 @@ public class BaseKeyboard {
static final String TAG = "BaseKeyboard"; static final String TAG = "BaseKeyboard";
// Keyboard XML Tags
private static final String TAG_KEYBOARD = "Keyboard";
private static final String TAG_ROW = "Row";
private static final String TAG_KEY = "Key";
private static final String TAG_SPACER = "Spacer";
private static final String TAG_INCLUDE = "include";
private static final String TAG_MERGE = "merge";
public static final int EDGE_LEFT = 0x01; public static final int EDGE_LEFT = 0x01;
public static final int EDGE_RIGHT = 0x02; public static final int EDGE_RIGHT = 0x02;
public static final int EDGE_TOP = 0x04; public static final int EDGE_TOP = 0x04;
@ -161,16 +153,16 @@ public class BaseKeyboard {
this.parent = parent; this.parent = parent;
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.BaseKeyboard); R.styleable.BaseKeyboard);
defaultWidth = getDimensionOrFraction(a, defaultWidth = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_keyWidth, R.styleable.BaseKeyboard_keyWidth,
parent.mDisplayWidth, parent.mDefaultWidth); parent.mDisplayWidth, parent.mDefaultWidth);
defaultHeight = getDimensionOrFraction(a, defaultHeight = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_keyHeight, R.styleable.BaseKeyboard_keyHeight,
parent.mDisplayHeight, parent.mDefaultHeight); parent.mDisplayHeight, parent.mDefaultHeight);
defaultHorizontalGap = getDimensionOrFraction(a, defaultHorizontalGap = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_horizontalGap, R.styleable.BaseKeyboard_horizontalGap,
parent.mDisplayWidth, parent.mDefaultHorizontalGap); parent.mDisplayWidth, parent.mDefaultHorizontalGap);
verticalGap = getDimensionOrFraction(a, verticalGap = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_verticalGap, R.styleable.BaseKeyboard_verticalGap,
parent.mDisplayHeight, parent.mDefaultVerticalGap); parent.mDisplayHeight, parent.mDefaultVerticalGap);
a.recycle(); a.recycle();
@ -293,11 +285,14 @@ public class BaseKeyboard {
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.BaseKeyboard); R.styleable.BaseKeyboard);
height = getDimensionOrFraction(a, R.styleable.BaseKeyboard_keyHeight, height = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_keyHeight,
keyboard.mDisplayHeight, parent.defaultHeight); keyboard.mDisplayHeight, parent.defaultHeight);
gap = getDimensionOrFraction(a, R.styleable.BaseKeyboard_horizontalGap, gap = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_horizontalGap,
keyboard.mDisplayWidth, parent.defaultHorizontalGap); keyboard.mDisplayWidth, parent.defaultHorizontalGap);
width = getDimensionOrFraction(a, R.styleable.BaseKeyboard_keyWidth, width = BaseKeyboardParser.getDimensionOrFraction(a,
R.styleable.BaseKeyboard_keyWidth,
keyboard.mDisplayWidth, parent.defaultWidth) - gap; keyboard.mDisplayWidth, parent.defaultWidth) - gap;
a.recycle(); a.recycle();
a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.BaseKeyboard_Key); a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.BaseKeyboard_Key);
@ -388,9 +383,9 @@ public class BaseKeyboard {
* Detects if a point falls inside this key. * Detects if a point falls inside this key.
* @param x the x-coordinate of the point * @param x the x-coordinate of the point
* @param y the y-coordinate of the point * @param y the y-coordinate of the point
* @return whether or not the point falls inside the key. If the key is attached to an edge, * @return whether or not the point falls inside the key. If the key is attached to an
* it will assume that all points between the key and the edge are considered to be inside * edge, it will assume that all points between the key and the edge are considered to be
* the key. * inside the key.
*/ */
public boolean isInside(int x, int y) { public boolean isInside(int x, int y) {
boolean leftEdge = (edgeFlags & EDGE_LEFT) > 0; boolean leftEdge = (edgeFlags & EDGE_LEFT) > 0;
@ -476,11 +471,11 @@ public class BaseKeyboard {
mDisplayHeight = height; mDisplayHeight = height;
mDefaultHorizontalGap = 0; mDefaultHorizontalGap = 0;
mDefaultWidth = mDisplayWidth / 10; setKeyWidth(mDisplayWidth / 10);
mDefaultVerticalGap = 0; mDefaultVerticalGap = 0;
mDefaultHeight = mDefaultWidth; mDefaultHeight = mDefaultWidth;
mKeyboardMode = modeId; mKeyboardMode = modeId;
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId)); loadKeyboard(context, xmlLayoutResId);
} }
/** /**
@ -497,11 +492,11 @@ public class BaseKeyboard {
//Log.v(TAG, "keyboard's display metrics:" + dm); //Log.v(TAG, "keyboard's display metrics:" + dm);
mDefaultHorizontalGap = 0; mDefaultHorizontalGap = 0;
mDefaultWidth = mDisplayWidth / 10; setKeyWidth(mDisplayWidth / 10);
mDefaultVerticalGap = 0; mDefaultVerticalGap = 0;
mDefaultHeight = mDefaultWidth; mDefaultHeight = mDefaultWidth;
mKeyboardMode = modeId; mKeyboardMode = modeId;
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId)); loadKeyboard(context, xmlLayoutResId);
} }
/** /**
@ -590,6 +585,8 @@ public class BaseKeyboard {
protected void setKeyWidth(int width) { protected void setKeyWidth(int width) {
mDefaultWidth = width; mDefaultWidth = width;
final int threshold = (int) (width * SEARCH_DISTANCE);
mProximityThreshold = threshold * threshold;
} }
/** /**
@ -604,6 +601,18 @@ public class BaseKeyboard {
return mTotalWidth; return mTotalWidth;
} }
public int getKeyboardHeight() {
return mDisplayHeight;
}
public int getKeyboardWidth() {
return mDisplayWidth;
}
public int getKeyboardMode() {
return mKeyboardMode;
}
public boolean setShifted(boolean shiftState) { public boolean setShifted(boolean shiftState) {
for (final Key key : mShiftKeys) { for (final Key key : mShiftKeys) {
key.on = shiftState; key.on = shiftState;
@ -636,11 +645,12 @@ public class BaseKeyboard {
int count = 0; int count = 0;
for (int i = 0; i < mKeys.size(); i++) { for (int i = 0; i < mKeys.size(); i++) {
final Key key = mKeys.get(i); final Key key = mKeys.get(i);
if (key.squaredDistanceFrom(x, y) < mProximityThreshold || final int threshold = mProximityThreshold;
key.squaredDistanceFrom(x + mCellWidth - 1, y) < mProximityThreshold || if (key.squaredDistanceFrom(x, y) < threshold ||
key.squaredDistanceFrom(x + mCellWidth - 1, y) < threshold ||
key.squaredDistanceFrom(x + mCellWidth - 1, y + mCellHeight - 1) key.squaredDistanceFrom(x + mCellWidth - 1, y + mCellHeight - 1)
< mProximityThreshold || < threshold ||
key.squaredDistanceFrom(x, y + mCellHeight - 1) < mProximityThreshold) { key.squaredDistanceFrom(x, y + mCellHeight - 1) < threshold) {
indices[count++] = i; indices[count++] = i;
} }
} }
@ -670,26 +680,41 @@ public class BaseKeyboard {
} }
// TODO should be private // TODO should be private
protected Row createRowFromXml(Resources res, XmlResourceParser parser) { protected BaseKeyboard.Row createRowFromXml(Resources res, XmlResourceParser parser) {
return new Row(res, this, parser); return new BaseKeyboard.Row(res, this, parser);
} }
// TODO should be private // TODO should be private
protected Key createKeyFromXml(Resources res, Row parent, int x, int y, protected BaseKeyboard.Key createKeyFromXml(Resources res, Row parent, int x, int y,
XmlResourceParser parser) { XmlResourceParser parser) {
return new Key(res, parent, x, y, parser); return new BaseKeyboard.Key(res, parent, x, y, parser);
} }
private static class KeyboardParseState { private static class BaseKeyboardParser {
private final int mKeyboardMode; // Keyboard XML Tags
private static final String TAG_KEYBOARD = "Keyboard";
private static final String TAG_ROW = "Row";
private static final String TAG_KEY = "Key";
private static final String TAG_SPACER = "Spacer";
private static final String TAG_INCLUDE = "include";
private static final String TAG_MERGE = "merge";
private final BaseKeyboard mKeyboard;
private final List<Key> mKeys;
private final List<Key> mShiftKeys;
private final Resources mResources;
private int mCurrentX = 0; private int mCurrentX = 0;
private int mCurrentY = 0; private int mCurrentY = 0;
private int mMaxRowWidth = 0; private int mMaxRowWidth = 0;
private int mTotalHeight = 0; private int mTotalHeight = 0;
private Row mCurrentRow = null; private Row mCurrentRow = null;
public KeyboardParseState(int keyboardMode) { public BaseKeyboardParser(BaseKeyboard keyboard, Resources res) {
mKeyboardMode = keyboardMode; mKeyboard = keyboard;
mKeys = keyboard.getKeys();
mShiftKeys = keyboard.getShiftKeys();
mResources = res;
} }
public int getX() { public int getX() {
@ -708,7 +733,7 @@ public class BaseKeyboard {
public boolean startRow(Row row) { public boolean startRow(Row row) {
mCurrentX = 0; mCurrentX = 0;
mCurrentRow = row; mCurrentRow = row;
return row.mode == 0 || row.mode == mKeyboardMode; return row.mode == 0 || row.mode == mKeyboard.getKeyboardMode();
} }
public void skipRow() { public void skipRow() {
@ -743,23 +768,8 @@ public class BaseKeyboard {
public int getTotalHeight() { public int getTotalHeight() {
return mTotalHeight; return mTotalHeight;
} }
}
private void loadKeyboard(Context context, XmlResourceParser parser) { private void parseKeyboard(XmlResourceParser parser)
try {
KeyboardParseState state = new KeyboardParseState(mKeyboardMode);
parseKeyboard(context.getResources(), parser, state);
// mTotalWidth is the width of this keyboard which is maximum width of row.
mTotalWidth = state.getMaxRowWidth();
mTotalHeight = state.getTotalHeight();
} catch (XmlPullParserException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void parseKeyboard(Resources res, XmlResourceParser parser, KeyboardParseState state)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
Key key = null; Key key = null;
@ -768,40 +778,43 @@ public class BaseKeyboard {
if (event == XmlResourceParser.START_TAG) { if (event == XmlResourceParser.START_TAG) {
String tag = parser.getName(); String tag = parser.getName();
if (TAG_ROW.equals(tag)) { if (TAG_ROW.equals(tag)) {
// TODO createRowFromXml should not be called from BaseKeyboard constructor. // TODO createRowFromXml should not be called from
Row row = createRowFromXml(res, parser); // BaseKeyboard constructor.
if (!state.startRow(row)) Row row = mKeyboard.createRowFromXml(mResources, parser);
skipToEndOfRow(parser, state); if (!startRow(row))
skipToEndOfRow(parser);
} else if (TAG_KEY.equals(tag)) { } else if (TAG_KEY.equals(tag)) {
// TODO createKeyFromXml should not be called from BaseKeyboard constructor. // TODO createKeyFromXml should not be called from
key = createKeyFromXml(res, state.getRow(), state.getX(), state.getY(), // BaseKeyboard constructor.
key = mKeyboard.createKeyFromXml(mResources, getRow(), getX(), getY(),
parser); parser);
mKeys.add(key); mKeys.add(key);
if (key.codes[0] == KEYCODE_SHIFT) if (key.codes[0] == KEYCODE_SHIFT)
mShiftKeys.add(key); mShiftKeys.add(key);
} else if (TAG_SPACER.equals(tag)) { } else if (TAG_SPACER.equals(tag)) {
parseSpacerAttribute(res, parser, state); parseSpacerAttribute(parser);
} else if (TAG_KEYBOARD.equals(tag)) { } else if (TAG_KEYBOARD.equals(tag)) {
parseKeyboardAttributes(res, parser); parseKeyboardAttributes(parser);
} else if (TAG_INCLUDE.equals(tag)) { } else if (TAG_INCLUDE.equals(tag)) {
if (parser.getDepth() == 0) if (parser.getDepth() == 0)
throw new InflateException("<include /> cannot be the root element"); throw new InflateException("<include /> cannot be the root element");
parseInclude(res, parser, state); parseInclude(parser);
} else if (TAG_MERGE.equals(tag)) { } else if (TAG_MERGE.equals(tag)) {
throw new InflateException("<merge> must not be appeared in keyboard XML file"); throw new InflateException(
"<merge> must not be appeared in keyboard XML file");
} else { } else {
throw new InflateException("unknown start tag: " + tag); throw new InflateException("unknown start tag: " + tag);
} }
} else if (event == XmlResourceParser.END_TAG) { } else if (event == XmlResourceParser.END_TAG) {
String tag = parser.getName(); String tag = parser.getName();
if (TAG_KEY.equals(tag)) { if (TAG_KEY.equals(tag)) {
state.endKey(key); endKey(key);
} else if (TAG_ROW.equals(tag)) { } else if (TAG_ROW.equals(tag)) {
state.endRow(); endRow();
} else if (TAG_SPACER.equals(tag)) { } else if (TAG_SPACER.equals(tag)) {
; ;
} else if (TAG_KEYBOARD.equals(tag)) { } else if (TAG_KEYBOARD.equals(tag)) {
state.endKeyboard(mDefaultVerticalGap); endKeyboard(mKeyboard.getVerticalGap());
} else if (TAG_INCLUDE.equals(tag)) { } else if (TAG_INCLUDE.equals(tag)) {
; ;
} else if (TAG_MERGE.equals(tag)) { } else if (TAG_MERGE.equals(tag)) {
@ -813,32 +826,32 @@ public class BaseKeyboard {
} }
} }
private void parseSpacerAttribute(Resources res, XmlResourceParser parser, private void parseSpacerAttribute(XmlResourceParser parser) {
KeyboardParseState state) { TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.BaseKeyboard); R.styleable.BaseKeyboard);
int gap = getDimensionOrFraction(a, int gap = getDimensionOrFraction(a, R.styleable.BaseKeyboard_horizontalGap,
R.styleable.BaseKeyboard_horizontalGap, mDisplayWidth, 0); mKeyboard.getKeyboardWidth(), 0);
a.recycle(); a.recycle();
state.setSpacer(gap); setSpacer(gap);
} }
private void parseInclude(Resources res, XmlResourceParser parent, KeyboardParseState state) private void parseInclude(XmlResourceParser parent)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
final TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parent), final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parent),
R.styleable.BaseKeyboard_Include); R.styleable.BaseKeyboard_Include);
final int keyboardLayout = a.getResourceId( final int keyboardLayout = a.getResourceId(
R.styleable.BaseKeyboard_Include_keyboardLayout, 0); R.styleable.BaseKeyboard_Include_keyboardLayout, 0);
a.recycle(); a.recycle();
if (keyboardLayout == 0) if (keyboardLayout == 0)
throw new InflateException("<include /> must have keyboardLayout attribute"); throw new InflateException("<include /> must have keyboardLayout attribute");
final XmlResourceParser parser = res.getLayout(keyboardLayout); final XmlResourceParser parser = mResources.getLayout(keyboardLayout);
int event; int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) { while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) { if (event == XmlResourceParser.START_TAG) {
String name = parser.getName(); String name = parser.getName();
if (TAG_MERGE.equals(name)) { if (TAG_MERGE.equals(name)) {
parseKeyboard(res, parser, state); parseKeyboard(parser);
return; return;
} else { } else {
throw new InflateException( throw new InflateException(
@ -848,14 +861,14 @@ public class BaseKeyboard {
} }
} }
private void skipToEndOfRow(XmlResourceParser parser, KeyboardParseState state) private void skipToEndOfRow(XmlResourceParser parser)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
int event; int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) { while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG) { if (event == XmlResourceParser.END_TAG) {
String tag = parser.getName(); String tag = parser.getName();
if (TAG_ROW.equals(tag)) { if (TAG_ROW.equals(tag)) {
state.skipRow(); skipRow();
return; return;
} }
} }
@ -863,25 +876,26 @@ public class BaseKeyboard {
throw new InflateException("can not find </Row>"); throw new InflateException("can not find </Row>");
} }
private void parseKeyboardAttributes(Resources res, XmlResourceParser parser) { private void parseKeyboardAttributes(XmlResourceParser parser) {
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.BaseKeyboard); R.styleable.BaseKeyboard);
mDefaultWidth = getDimensionOrFraction(a, final int width = mKeyboard.getKeyboardWidth();
R.styleable.BaseKeyboard_keyWidth, mDisplayWidth, mDisplayWidth / 10); final int height = mKeyboard.getKeyboardHeight();
mDefaultHeight = getDimensionOrFraction(a, mKeyboard.setKeyWidth(getDimensionOrFraction(a,
R.styleable.BaseKeyboard_keyHeight, mDisplayHeight, 50); R.styleable.BaseKeyboard_keyWidth, width, width / 10));
mDefaultHorizontalGap = getDimensionOrFraction(a, mKeyboard.setKeyHeight(getDimensionOrFraction(a,
R.styleable.BaseKeyboard_horizontalGap, mDisplayWidth, 0); R.styleable.BaseKeyboard_keyHeight, height, 50));
mDefaultVerticalGap = getDimensionOrFraction(a, mKeyboard.setHorizontalGap(getDimensionOrFraction(a,
R.styleable.BaseKeyboard_verticalGap, mDisplayHeight, 0); R.styleable.BaseKeyboard_horizontalGap, width, 0));
mProximityThreshold = (int) (mDefaultWidth * SEARCH_DISTANCE); mKeyboard.setVerticalGap(getDimensionOrFraction(a,
mProximityThreshold = mProximityThreshold * mProximityThreshold; R.styleable.BaseKeyboard_verticalGap, height, 0));
a.recycle(); a.recycle();
} }
static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) { public static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) {
TypedValue value = a.peekValue(index); TypedValue value = a.peekValue(index);
if (value == null) return defValue; if (value == null)
return defValue;
if (value.type == TypedValue.TYPE_DIMENSION) { if (value.type == TypedValue.TYPE_DIMENSION) {
return a.getDimensionPixelOffset(index, defValue); return a.getDimensionPixelOffset(index, defValue);
} else if (value.type == TypedValue.TYPE_FRACTION) { } else if (value.type == TypedValue.TYPE_FRACTION) {
@ -890,6 +904,21 @@ public class BaseKeyboard {
} }
return defValue; return defValue;
} }
}
private void loadKeyboard(Context context, int xmlLayoutResId) {
try {
BaseKeyboardParser parser = new BaseKeyboardParser(this, context.getResources());
parser.parseKeyboard(context.getResources().getXml(xmlLayoutResId));
// mTotalWidth is the width of this keyboard which is maximum width of row.
mTotalWidth = parser.getMaxRowWidth();
mTotalHeight = parser.getTotalHeight();
} catch (XmlPullParserException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected static void setDefaultBounds(Drawable drawable) { protected static void setDefaultBounds(Drawable drawable) {
if (drawable != null) if (drawable != null)