am f70bcf3d: Refactor Key constructors

* commit 'f70bcf3d323b13b60c0567c69768ed986647f86a':
  Refactor Key constructors
main
Tadashi G. Takaoka 2014-02-09 20:13:36 -08:00 committed by Android Git Automerger
commit 20083cce07
5 changed files with 60 additions and 57 deletions

View File

@ -22,13 +22,11 @@ import static com.android.inputmethod.latin.Constants.CODE_SHIFT;
import static com.android.inputmethod.latin.Constants.CODE_SWITCH_ALPHA_SYMBOL; import static com.android.inputmethod.latin.Constants.CODE_SWITCH_ALPHA_SYMBOL;
import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED;
import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Xml;
import com.android.inputmethod.keyboard.internal.KeyDrawParams; import com.android.inputmethod.keyboard.internal.KeyDrawParams;
import com.android.inputmethod.keyboard.internal.KeySpecParser; import com.android.inputmethod.keyboard.internal.KeySpecParser;
@ -42,9 +40,6 @@ import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
@ -188,22 +183,15 @@ public class Key implements Comparable<Key> {
private boolean mEnabled = true; private boolean mEnabled = true;
/** /**
* This constructor is being used only for keys in more keys keyboard. * Constructor for a key on <code>MoreKeyKeyboard</code>, on <code>MoreSuggestions</code>,
* and in a <GridRows/>.
*/ */
public Key(final KeyboardParams params, final MoreKeySpec moreKeySpec, final int x, final int y, public Key(final String label, final int iconId, final int code, final String outputText,
final int width, final int height, final int labelFlags) { final String hintLabel, final int labelFlags, final int backgroundType, final int x,
this(params, moreKeySpec.mLabel, null, moreKeySpec.mIconId, moreKeySpec.mCode, final int y, final int width, final int height, final int horizontalGap,
moreKeySpec.mOutputText, x, y, width, height, labelFlags, BACKGROUND_TYPE_NORMAL); final int verticalGap) {
} mHeight = height - verticalGap;
mWidth = width - horizontalGap;
/**
* This constructor is being used only for key in popup suggestions pane.
*/
public Key(final KeyboardParams params, final String label, final String hintLabel,
final int iconId, final int code, final String outputText, final int x, final int y,
final int width, final int height, final int labelFlags, final int backgroundType) {
mHeight = height - params.mVerticalGap;
mWidth = width - params.mHorizontalGap;
mHintLabel = hintLabel; mHintLabel = hintLabel;
mLabelFlags = labelFlags; mLabelFlags = labelFlags;
mBackgroundType = backgroundType; mBackgroundType = backgroundType;
@ -218,7 +206,7 @@ public class Key implements Comparable<Key> {
mEnabled = (code != CODE_UNSPECIFIED); mEnabled = (code != CODE_UNSPECIFIED);
mIconId = iconId; mIconId = iconId;
// Horizontal gap is divided equally to both sides of the key. // Horizontal gap is divided equally to both sides of the key.
mX = x + params.mHorizontalGap / 2; mX = x + horizontalGap / 2;
mY = y; mY = y;
mHitBox.set(x, y, x + width + 1, y + height); mHitBox.set(x, y, x + width + 1, y + height);
mKeyVisualAttributes = null; mKeyVisualAttributes = null;
@ -227,25 +215,22 @@ public class Key implements Comparable<Key> {
} }
/** /**
* Create a key with the given top-left coordinate and extract its attributes from the XML * Create a key with the given top-left coordinate and extract its attributes from a key
* parser. * specification string, Key attribute array, key style, and etc.
* @param res resources associated with the caller's context *
* @param keySpec the key specification.
* @param keyAttr the Key XML attributes array.
* @param keyStyle the {@link KeyStyle} of this key.
* @param params the keyboard building parameters. * @param params the keyboard building parameters.
* @param row the row that this key belongs to. row's x-coordinate will be the right edge of * @param row the row that this key belongs to. row's x-coordinate will be the right edge of
* this key. * this key.
* @param parser the XML parser containing the attributes for this key
* @throws XmlPullParserException
*/ */
public Key(final Resources res, final KeyboardParams params, final KeyboardRow row, public Key(final String keySpec, final TypedArray keyAttr, final KeyStyle style,
final XmlPullParser parser) throws XmlPullParserException { final KeyboardParams params, final KeyboardRow row) {
final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap; final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
final int rowHeight = row.getRowHeight(); final int rowHeight = row.getRowHeight();
mHeight = rowHeight - params.mVerticalGap; mHeight = rowHeight - params.mVerticalGap;
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_Key);
final KeyStyle style = params.mKeyStyles.getKeyStyle(keyAttr, parser);
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();
@ -315,13 +300,6 @@ public class Key implements Comparable<Key> {
} }
mActionFlags = actionFlags; mActionFlags = actionFlags;
final String keySpec = style.getString(keyAttr, R.styleable.Keyboard_Key_keySpec);
// Note: {@link Spacer} has an empty keySpec.
// TODO: Create a Key constructor that parses only key geometries and share it with Spacer.
if (TextUtils.isEmpty(keySpec) && !(this instanceof Spacer)) {
throw new RuntimeException("Empty keySpec found in " + getClass().getName());
}
mIconId = KeySpecParser.getIconId(keySpec); mIconId = KeySpecParser.getIconId(keySpec);
final int disabledIconId = KeySpecParser.getIconId(style.getString(keyAttr, final int disabledIconId = KeySpecParser.getIconId(style.getString(keyAttr,
R.styleable.Keyboard_Key_keyIconDisabled)); R.styleable.Keyboard_Key_keyIconDisabled));
@ -382,12 +360,11 @@ public class Key implements Comparable<Key> {
mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode, mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode,
disabledIconId, previewIconId, visualInsetsLeft, visualInsetsRight); disabledIconId, previewIconId, visualInsetsLeft, visualInsetsRight);
mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr); mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
keyAttr.recycle();
mHashCode = computeHashCode(this); mHashCode = computeHashCode(this);
} }
/** /**
* Copy constructor. * Copy constructor for DynamicGridKeyboard.GridKey.
* *
* @param key the original key. * @param key the original key.
*/ */
@ -939,9 +916,9 @@ public class Key implements Comparable<Key> {
} }
public static class Spacer extends Key { public static class Spacer extends Key {
public Spacer(final Resources res, final KeyboardParams params, final KeyboardRow row, public Spacer(final TypedArray keyAttr, final KeyStyle keyStyle,
final XmlPullParser parser) throws XmlPullParserException { final KeyboardParams params, final KeyboardRow row) {
super(res, params, row, parser); super(null /* keySpec */, keyAttr, keyStyle, params, row);
} }
/** /**
@ -949,8 +926,9 @@ public class Key implements Comparable<Key> {
*/ */
protected Spacer(final KeyboardParams params, final int x, final int y, final int width, protected Spacer(final KeyboardParams params, final int x, final int y, final int width,
final int height) { final int height) {
super(params, null, null, ICON_UNDEFINED, CODE_UNSPECIFIED, super(null /* label */, ICON_UNDEFINED, CODE_UNSPECIFIED, null /* outputText */,
null, x, y, width, height, 0, BACKGROUND_TYPE_EMPTY); null /* hintLabel */, 0 /* labelFlags */, BACKGROUND_TYPE_EMPTY, x, y, width,
height, params.mHorizontalGap, params.mVerticalGap);
} }
} }
} }

View File

@ -343,8 +343,7 @@ public final class MoreKeysKeyboard extends Keyboard {
final int row = n / params.mNumColumns; final int row = n / params.mNumColumns;
final int x = params.getX(n, row); final int x = params.getX(n, row);
final int y = params.getY(row); final int y = params.getY(row);
final Key key = new Key(params, moreKeySpec, x, y, final Key key = moreKeySpec.buildKey(x, y, moreKeyFlags, params);
params.mDefaultKeyWidth, params.mDefaultRowHeight, moreKeyFlags);
params.markAsEdgeKey(key, row); params.markAsEdgeKey(key, row);
params.onAddKey(key); params.onAddKey(key);

View File

@ -21,6 +21,7 @@ import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
@ -37,6 +38,7 @@ import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.StringUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils; import com.android.inputmethod.latin.utils.XmlParseUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils.ParseException;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -455,11 +457,15 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (Build.VERSION.SDK_INT < supportedMinSdkVersion) { if (Build.VERSION.SDK_INT < supportedMinSdkVersion) {
continue; continue;
} }
final int labelFlags = row.getDefaultKeyLabelFlags();
final int backgroundType = row.getDefaultBackgroundType();
final int x = (int)row.getKeyX(null); final int x = (int)row.getKeyX(null);
final int y = row.getKeyY(); final int y = row.getKeyY();
final Key key = new Key(mParams, label, null /* hintLabel */, 0 /* iconId */, final int width = (int)keyWidth;
code, outputText, x, y, (int)keyWidth, row.getRowHeight(), final int height = row.getRowHeight();
row.getDefaultKeyLabelFlags(), row.getDefaultBackgroundType()); final Key key = new Key(label, KeyboardIconsSet.ICON_UNDEFINED, code, outputText,
null /* hintLabel */, labelFlags, backgroundType, x, y, width, height,
mParams.mHorizontalGap, mParams.mVerticalGap);
endKey(key); endKey(key);
row.advanceXPos(keyWidth); row.advanceXPos(keyWidth);
} }
@ -476,7 +482,15 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (DEBUG) startEndTag("<%s /> skipped", TAG_KEY); if (DEBUG) startEndTag("<%s /> skipped", TAG_KEY);
return; return;
} }
final Key key = new Key(mResources, mParams, row, parser); final TypedArray keyAttr = mResources.obtainAttributes(
Xml.asAttributeSet(parser), R.styleable.Keyboard_Key);
final KeyStyle keyStyle = mParams.mKeyStyles.getKeyStyle(keyAttr, parser);
final String keySpec = keyStyle.getString(keyAttr, R.styleable.Keyboard_Key_keySpec);
if (TextUtils.isEmpty(keySpec)) {
throw new ParseException("Empty keySpec", parser);
}
final Key key = new Key(keySpec, keyAttr, keyStyle, mParams, row);
keyAttr.recycle();
if (DEBUG) { if (DEBUG) {
startEndTag("<%s%s %s moreKeys=%s />", TAG_KEY, (key.isEnabled() ? "" : " disabled"), startEndTag("<%s%s %s moreKeys=%s />", TAG_KEY, (key.isEnabled() ? "" : " disabled"),
key, Arrays.toString(key.getMoreKeys())); key, Arrays.toString(key.getMoreKeys()));
@ -492,7 +506,11 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (DEBUG) startEndTag("<%s /> skipped", TAG_SPACER); if (DEBUG) startEndTag("<%s /> skipped", TAG_SPACER);
return; return;
} }
final Key.Spacer spacer = new Key.Spacer(mResources, mParams, row, parser); final TypedArray keyAttr = mResources.obtainAttributes(
Xml.asAttributeSet(parser), R.styleable.Keyboard_Key);
final KeyStyle keyStyle = mParams.mKeyStyles.getKeyStyle(keyAttr, parser);
final Key spacer = new Key.Spacer(keyAttr, keyStyle, mParams, row);
keyAttr.recycle();
if (DEBUG) startEndTag("<%s />", TAG_SPACER); if (DEBUG) startEndTag("<%s />", TAG_SPACER);
XmlParseUtils.checkEndTag(TAG_SPACER, parser); XmlParseUtils.checkEndTag(TAG_SPACER, parser);
endKey(spacer); endKey(spacer);

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.keyboard.internal;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
@ -67,6 +68,13 @@ public final class MoreKeySpec {
mIconId = KeySpecParser.getIconId(moreKeySpec); mIconId = KeySpecParser.getIconId(moreKeySpec);
} }
public Key buildKey(final int x, final int y, final int labelFlags,
final KeyboardParams params) {
return new Key(mLabel, mIconId, mCode, mOutputText, null /* hintLabel */, labelFlags,
Key.BACKGROUND_TYPE_NORMAL, x, y, params.mDefaultKeyWidth, params.mDefaultRowHeight,
params.mHorizontalGap, params.mVerticalGap);
}
@Override @Override
public int hashCode() { public int hashCode() {
int hashCode = 1; int hashCode = 1;

View File

@ -209,10 +209,10 @@ public final class MoreSuggestions extends Keyboard {
final String word = mSuggestedWords.getWord(index); final String word = mSuggestedWords.getWord(index);
final String info = mSuggestedWords.getDebugString(index); final String info = mSuggestedWords.getDebugString(index);
final int indexInMoreSuggestions = index + SUGGESTION_CODE_BASE; final int indexInMoreSuggestions = index + SUGGESTION_CODE_BASE;
final Key key = new Key( final Key key = new Key(word, KeyboardIconsSet.ICON_UNDEFINED,
params, word, info, KeyboardIconsSet.ICON_UNDEFINED, indexInMoreSuggestions, indexInMoreSuggestions, null /* outputText */, info, 0 /* labelFlags */,
null /* outputText */, x, y, width, params.mDefaultRowHeight, Key.BACKGROUND_TYPE_NORMAL, x, y, width, params.mDefaultRowHeight,
0 /* labelFlags */, Key.BACKGROUND_TYPE_NORMAL); params.mHorizontalGap, params.mVerticalGap);
params.markAsEdgeKey(key, index); params.markAsEdgeKey(key, index);
params.onAddKey(key); params.onAddKey(key);
final int columnNumber = params.getColumnNumber(index); final int columnNumber = params.getColumnNumber(index);