Save and restore Row attributes using stack

Change-Id: I5cb402060946a711384ee53fa1caaeac86aa10cc
main
Tadashi G. Takaoka 2013-08-19 16:07:58 +09:00
parent 221929c643
commit bcd173dfa1
3 changed files with 78 additions and 60 deletions

View File

@ -440,9 +440,6 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
attr, R.styleable.Keyboard_Include); attr, R.styleable.Keyboard_Include);
final TypedArray keyAttr = mResources.obtainAttributes(attr, R.styleable.Keyboard_Key); final TypedArray keyAttr = mResources.obtainAttributes(attr, R.styleable.Keyboard_Key);
int keyboardLayout = 0; int keyboardLayout = 0;
float savedDefaultKeyWidth = 0;
int savedDefaultKeyLabelFlags = 0;
int savedDefaultBackgroundType = Key.BACKGROUND_TYPE_NORMAL;
try { try {
XmlParseUtils.checkAttributeExists( XmlParseUtils.checkAttributeExists(
keyboardAttr, R.styleable.Keyboard_Include_keyboardLayout, "keyboardLayout", keyboardAttr, R.styleable.Keyboard_Include_keyboardLayout, "keyboardLayout",
@ -450,24 +447,10 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
keyboardLayout = keyboardAttr.getResourceId( keyboardLayout = keyboardAttr.getResourceId(
R.styleable.Keyboard_Include_keyboardLayout, 0); R.styleable.Keyboard_Include_keyboardLayout, 0);
if (row != null) { if (row != null) {
if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) { // Override current x coordinate.
// Override current x coordinate. row.setXPos(row.getKeyX(keyAttr));
row.setXPos(row.getKeyX(keyAttr)); // Push current Row attributes and update with new attributes.
} row.pushRowAttributes(keyAttr);
// TODO: Remove this if-clause and do the same as backgroundType below.
savedDefaultKeyWidth = row.getDefaultKeyWidth();
if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyWidth)) {
// Override default key width.
row.setDefaultKeyWidth(row.getKeyWidth(keyAttr));
}
savedDefaultKeyLabelFlags = row.getDefaultKeyLabelFlags();
// Bitwise-or default keyLabelFlag if exists.
row.setDefaultKeyLabelFlags(keyAttr.getInt(
R.styleable.Keyboard_Key_keyLabelFlags, 0) | savedDefaultKeyLabelFlags);
savedDefaultBackgroundType = row.getDefaultBackgroundType();
// Override default backgroundType if exists.
row.setDefaultBackgroundType(keyAttr.getInt(
R.styleable.Keyboard_Key_backgroundType, savedDefaultBackgroundType));
} }
} finally { } finally {
keyboardAttr.recycle(); keyboardAttr.recycle();
@ -484,10 +467,8 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
parseMerge(parserForInclude, row, skip); parseMerge(parserForInclude, row, skip);
} finally { } finally {
if (row != null) { if (row != null) {
// Restore default keyWidth, keyLabelFlags, and backgroundType. // Restore Row attributes.
row.setDefaultKeyWidth(savedDefaultKeyWidth); row.popRowAttributes();
row.setDefaultKeyLabelFlags(savedDefaultKeyLabelFlags);
row.setDefaultBackgroundType(savedDefaultBackgroundType);
} }
parserForInclude.close(); parserForInclude.close();
} }

View File

@ -23,10 +23,13 @@ import android.util.Xml;
import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayDeque;
/** /**
* Container for keys in the keyboard. All keys in a row are at the same Y-coordinate. * Container for keys in the keyboard. All keys in a row are at the same Y-coordinate.
* Some of the key size defaults can be overridden per row from what the {@link Keyboard} * Some of the key size defaults can be overridden per row from what the {@link Keyboard}
@ -40,33 +43,68 @@ public final class KeyboardRow {
private final KeyboardParams mParams; private final KeyboardParams mParams;
/** The height of this row. */ /** The height of this row. */
private final int mRowHeight; private final int mRowHeight;
/** Default width of a key in this row. */
private float mDefaultKeyWidth; private final ArrayDeque<RowAttributes> mRowAttributesStack = CollectionUtils.newArrayDeque();
/** Default keyLabelFlags in this row. */
private int mDefaultKeyLabelFlags; private static class RowAttributes {
/** Default backgroundType for this row */ /** Default width of a key in this row. */
private int mDefaultBackgroundType; public final float mDefaultKeyWidth;
/** Default keyLabelFlags in this row. */
public final int mDefaultKeyLabelFlags;
/** Default backgroundType for this row */
public final int mDefaultBackgroundType;
/**
* Parse and create key attributes. This constructor is used to parse Row tag.
*
* @param keyAttr an attributes array of Row tag.
* @param defaultKeyWidth a default key width.
* @param keyboardWidth the keyboard width that is required to calculate keyWidth attribute.
*/
public RowAttributes(final TypedArray keyAttr, final float defaultKeyWidth,
final int keyboardWidth) {
mDefaultKeyWidth = keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth,
keyboardWidth, keyboardWidth, defaultKeyWidth);
mDefaultKeyLabelFlags = keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0);
mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType,
Key.BACKGROUND_TYPE_NORMAL);
}
/**
* Parse and update key attributes using default attributes. This constructor is used
* to parse include tag.
*
* @param keyAttr an attributes array of include tag.
* @param defaultRowAttr default Row attributes.
* @param keyboardWidth the keyboard width that is required to calculate keyWidth attribute.
*/
public RowAttributes(final TypedArray keyAttr, final RowAttributes defaultRowAttr,
final int keyboardWidth) {
mDefaultKeyWidth = keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth,
keyboardWidth, keyboardWidth, defaultRowAttr.mDefaultKeyWidth);
mDefaultKeyLabelFlags = keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0)
| defaultRowAttr.mDefaultKeyLabelFlags;
mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType,
defaultRowAttr.mDefaultBackgroundType);
}
}
private final int mCurrentY; private final int mCurrentY;
// Will be updated by {@link Key}'s constructor. // Will be updated by {@link Key}'s constructor.
private float mCurrentX; private float mCurrentX;
public KeyboardRow(final Resources res, final KeyboardParams params, final XmlPullParser parser, public KeyboardRow(final Resources res, final KeyboardParams params,
final int y) { final XmlPullParser parser, final int y) {
mParams = params; mParams = params;
final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard); R.styleable.Keyboard);
mRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr, mRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_rowHeight, R.styleable.Keyboard_rowHeight, params.mBaseHeight, params.mDefaultRowHeight);
params.mBaseHeight, params.mDefaultRowHeight);
keyboardAttr.recycle(); keyboardAttr.recycle();
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_Key); R.styleable.Keyboard_Key);
mDefaultKeyWidth = keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth, mRowAttributesStack.push(new RowAttributes(
params.mBaseWidth, params.mBaseWidth, params.mDefaultKeyWidth); keyAttr, params.mDefaultKeyWidth, params.mBaseWidth));
mDefaultKeyLabelFlags = keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0);
mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType,
Key.BACKGROUND_TYPE_NORMAL);
keyAttr.recycle(); keyAttr.recycle();
mCurrentY = y; mCurrentY = y;
@ -77,28 +115,26 @@ public final class KeyboardRow {
return mRowHeight; return mRowHeight;
} }
public float getDefaultKeyWidth() { public void pushRowAttributes(final TypedArray keyAttr) {
return mDefaultKeyWidth; final RowAttributes newAttributes = new RowAttributes(
keyAttr, mRowAttributesStack.peek(), mParams.mBaseWidth);
mRowAttributesStack.push(newAttributes);
} }
public void setDefaultKeyWidth(final float defaultKeyWidth) { public void popRowAttributes() {
mDefaultKeyWidth = defaultKeyWidth; mRowAttributesStack.pop();
}
public float getDefaultKeyWidth() {
return mRowAttributesStack.peek().mDefaultKeyWidth;
} }
public int getDefaultKeyLabelFlags() { public int getDefaultKeyLabelFlags() {
return mDefaultKeyLabelFlags; return mRowAttributesStack.peek().mDefaultKeyLabelFlags;
}
public void setDefaultKeyLabelFlags(final int keyLabelFlags) {
mDefaultKeyLabelFlags = keyLabelFlags;
} }
public int getDefaultBackgroundType() { public int getDefaultBackgroundType() {
return mDefaultBackgroundType; return mRowAttributesStack.peek().mDefaultBackgroundType;
}
public void setDefaultBackgroundType(final int backgroundType) {
mDefaultBackgroundType = backgroundType;
} }
public void setXPos(final float keyXPos) { public void setXPos(final float keyXPos) {
@ -131,13 +167,9 @@ public final class KeyboardRow {
return Math.max(keyXPos + keyboardRightEdge, mCurrentX); return Math.max(keyXPos + keyboardRightEdge, mCurrentX);
} }
public float getKeyWidth(final TypedArray keyAttr) {
return getKeyWidth(keyAttr, mCurrentX);
}
public float getKeyWidth(final TypedArray keyAttr, final float keyXPos) { public float getKeyWidth(final TypedArray keyAttr, final float keyXPos) {
if (keyAttr == null) { if (keyAttr == null) {
return mDefaultKeyWidth; return getDefaultKeyWidth();
} }
final int widthType = ResourceUtils.getEnumValue(keyAttr, final int widthType = ResourceUtils.getEnumValue(keyAttr,
R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM); R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM);
@ -149,7 +181,7 @@ public final class KeyboardRow {
return keyboardRightEdge - keyXPos; return keyboardRightEdge - keyXPos;
default: // KEYWIDTH_NOT_ENUM default: // KEYWIDTH_NOT_ENUM
return keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth, return keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth,
mParams.mBaseWidth, mParams.mBaseWidth, mDefaultKeyWidth); mParams.mBaseWidth, mParams.mBaseWidth, getDefaultKeyWidth());
} }
} }
} }

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.latin.utils;
import android.util.SparseArray; import android.util.SparseArray;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -94,6 +95,10 @@ public final class CollectionUtils {
return new CopyOnWriteArrayList<E>(array); return new CopyOnWriteArrayList<E>(array);
} }
public static <E> ArrayDeque<E> newArrayDeque() {
return new ArrayDeque<E>();
}
public static <E> SparseArray<E> newSparseArray() { public static <E> SparseArray<E> newSparseArray() {
return new SparseArray<E>(); return new SparseArray<E>();
} }