Merge "Change more key specification type to String from CharSequence"

This commit is contained in:
Tadashi G. Takaoka 2012-01-20 00:01:24 -08:00 committed by Android (Google) Code Review
commit 86641ef6e8
5 changed files with 73 additions and 76 deletions

View file

@ -101,7 +101,7 @@ public class Key {
/** Text to output when pressed. This can be multiple characters, like ".com" */ /** Text to output when pressed. This can be multiple characters, like ".com" */
public final CharSequence mOutputText; public final CharSequence mOutputText;
/** More keys */ /** More keys */
public final CharSequence[] mMoreKeys; public final String[] mMoreKeys;
/** More keys maximum column number */ /** More keys maximum column number */
public final int mMaxMoreKeysColumn; public final int mMaxMoreKeysColumn;
@ -255,7 +255,7 @@ public class Key {
// Update row to have current x coordinate. // Update row to have current x coordinate.
row.setXPos(keyXPos + keyWidth); row.setXPos(keyXPos + keyWidth);
final CharSequence[] moreKeys = style.getTextArray(keyAttr, final String[] moreKeys = style.getTextArray(keyAttr,
R.styleable.Keyboard_Key_moreKeys); R.styleable.Keyboard_Key_moreKeys);
// In Arabic symbol layouts, we'd like to keep digits in more keys regardless of // In Arabic symbol layouts, we'd like to keep digits in more keys regardless of
// config_digit_more_keys_enabled. // config_digit_more_keys_enabled.

View file

@ -34,7 +34,7 @@ public class MiniKeyboard extends Keyboard {
} }
public static class Builder extends Keyboard.Builder<Builder.MiniKeyboardParams> { public static class Builder extends Keyboard.Builder<Builder.MiniKeyboardParams> {
private final CharSequence[] mMoreKeys; private final String[] mMoreKeys;
public static class MiniKeyboardParams extends Keyboard.Params { public static class MiniKeyboardParams extends Keyboard.Params {
/* package */int mTopRowAdjustment; /* package */int mTopRowAdjustment;
@ -230,16 +230,14 @@ public class MiniKeyboard extends Keyboard {
parentKey.mX + (mParams.mDefaultKeyWidth - width) / 2, view.getMeasuredWidth()); parentKey.mX + (mParams.mDefaultKeyWidth - width) / 2, view.getMeasuredWidth());
} }
private static int getMaxKeyWidth(KeyboardView view, CharSequence[] moreKeys, private static int getMaxKeyWidth(KeyboardView view, String[] moreKeys, int minKeyWidth) {
int minKeyWidth) {
final int padding = (int) view.getContext().getResources() final int padding = (int) view.getContext().getResources()
.getDimension(R.dimen.mini_keyboard_key_horizontal_padding); .getDimension(R.dimen.mini_keyboard_key_horizontal_padding);
Paint paint = null; Paint paint = null;
int maxWidth = minKeyWidth; int maxWidth = minKeyWidth;
for (CharSequence moreKeySpec : moreKeys) { for (String moreKeySpec : moreKeys) {
final CharSequence label = MoreKeySpecParser.getLabel(moreKeySpec.toString()); final String label = MoreKeySpecParser.getLabel(moreKeySpec);
// If the label is single letter, minKeyWidth is enough to hold // If the label is single letter, minKeyWidth is enough to hold the label.
// the label.
if (label != null && label.length() > 1) { if (label != null && label.length() > 1) {
if (paint == null) { if (paint == null) {
paint = new Paint(); paint = new Paint();
@ -258,7 +256,7 @@ public class MiniKeyboard extends Keyboard {
public MiniKeyboard build() { public MiniKeyboard build() {
final MiniKeyboardParams params = mParams; final MiniKeyboardParams params = mParams;
for (int n = 0; n < mMoreKeys.length; n++) { for (int n = 0; n < mMoreKeys.length; n++) {
final String moreKeySpec = mMoreKeys[n].toString(); final String moreKeySpec = mMoreKeys[n];
final int row = n / params.mNumColumns; final int row = n / params.mNumColumns;
final Key key = new Key(mResources, params, moreKeySpec, params.getX(n, row), final Key key = new Key(mResources, params, moreKeySpec, params.getX(n, row),
params.getY(row), params.mDefaultKeyWidth, params.mDefaultRowHeight); params.getY(row), params.mDefaultKeyWidth, params.mDefaultRowHeight);

View file

@ -37,20 +37,22 @@ public class KeyStyles {
new HashMap<String, DeclaredKeyStyle>(); new HashMap<String, DeclaredKeyStyle>();
private static final KeyStyle EMPTY_KEY_STYLE = new EmptyKeyStyle(); private static final KeyStyle EMPTY_KEY_STYLE = new EmptyKeyStyle();
private static final char ESCAPE_CHAR = '\\';
public interface KeyStyle { public interface KeyStyle {
public CharSequence[] getTextArray(TypedArray a, int index); public String[] getTextArray(TypedArray a, int index);
public CharSequence getText(TypedArray a, int index); public CharSequence getText(TypedArray a, int index);
public int getInt(TypedArray a, int index, int defaultValue); public int getInt(TypedArray a, int index, int defaultValue);
public int getFlag(TypedArray a, int index, int defaultValue); public int getFlag(TypedArray a, int index, int defaultValue);
} }
/* package */ static class EmptyKeyStyle implements KeyStyle { private static class EmptyKeyStyle implements KeyStyle {
EmptyKeyStyle() { EmptyKeyStyle() {
// Nothing to do. // Nothing to do.
} }
@Override @Override
public CharSequence[] getTextArray(TypedArray a, int index) { public String[] getTextArray(TypedArray a, int index) {
return parseTextArray(a, index); return parseTextArray(a, index);
} }
@ -69,64 +71,66 @@ public class KeyStyles {
return a.getInt(index, defaultValue); return a.getInt(index, defaultValue);
} }
protected static CharSequence[] parseTextArray(TypedArray a, int index) { protected static String[] parseTextArray(TypedArray a, int index) {
if (!a.hasValue(index)) if (!a.hasValue(index))
return null; return null;
final CharSequence text = a.getText(index); final CharSequence text = a.getText(index);
return parseCsvText(text); return parseCsvText(text.toString());
} }
/* package */ static CharSequence[] parseCsvText(CharSequence text) { }
final int size = text.length();
if (size == 0) return null; /* package for test */
if (size == 1) return new CharSequence[] { text }; static String[] parseCsvText(String text) {
final StringBuilder sb = new StringBuilder(); final int size = text.length();
ArrayList<CharSequence> list = null; if (size == 0) return null;
int start = 0; if (size == 1) return new String[] { text };
for (int pos = 0; pos < size; pos++) { final StringBuilder sb = new StringBuilder();
final char c = text.charAt(pos); ArrayList<String> list = null;
if (c == ',') { int start = 0;
if (list == null) list = new ArrayList<CharSequence>(); for (int pos = 0; pos < size; pos++) {
if (sb.length() == 0) { final char c = text.charAt(pos);
list.add(text.subSequence(start, pos)); if (c == ',') {
} else { if (list == null) list = new ArrayList<String>();
list.add(sb.toString()); if (sb.length() == 0) {
sb.setLength(0); list.add(text.substring(start, pos));
} } else {
start = pos + 1; list.add(sb.toString());
continue; sb.setLength(0);
} else if (c == '\\') {
if (start == pos) {
// Skip escape character at the beginning of the value.
start++;
pos++;
} else {
if (start < pos && sb.length() == 0)
sb.append(text.subSequence(start, pos));
pos++;
if (pos < size)
sb.append(text.charAt(pos));
}
} else if (sb.length() > 0) {
sb.append(c);
} }
start = pos + 1;
continue;
} else if (c == ESCAPE_CHAR) {
if (start == pos) {
// Skip escape character at the beginning of the value.
start++;
pos++;
} else {
if (start < pos && sb.length() == 0)
sb.append(text.subSequence(start, pos));
pos++;
if (pos < size)
sb.append(text.charAt(pos));
}
} else if (sb.length() > 0) {
sb.append(c);
} }
if (list == null) { }
return new CharSequence[] { sb.length() > 0 ? sb : text.subSequence(start, size) }; if (list == null) {
} else { return new String[] { sb.length() > 0 ? sb.toString() : text.substring(start) };
list.add(sb.length() > 0 ? sb : text.subSequence(start, size)); } else {
return list.toArray(new CharSequence[list.size()]); list.add(sb.length() > 0 ? sb.toString() : text.substring(start));
} return list.toArray(new String[list.size()]);
} }
} }
/* package */ static class DeclaredKeyStyle extends EmptyKeyStyle { private static class DeclaredKeyStyle extends EmptyKeyStyle {
private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>(); private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>();
@Override @Override
public CharSequence[] getTextArray(TypedArray a, int index) { public String[] getTextArray(TypedArray a, int index) {
return a.hasValue(index) return a.hasValue(index)
? super.getTextArray(a, index) : (CharSequence[])mAttributes.get(index); ? super.getTextArray(a, index) : (String[])mAttributes.get(index);
} }
@Override @Override

View file

@ -40,7 +40,7 @@ import java.util.ArrayList;
public class MoreKeySpecParser { public class MoreKeySpecParser {
private static final String TAG = MoreKeySpecParser.class.getSimpleName(); private static final String TAG = MoreKeySpecParser.class.getSimpleName();
private static final char ESCAPE = '\\'; private static final char ESCAPE_CHAR = '\\';
private static final String LABEL_END = "|"; private static final String LABEL_END = "|";
private static final String PREFIX_AT = "@"; private static final String PREFIX_AT = "@";
private static final String PREFIX_ICON = PREFIX_AT + "icon/"; private static final String PREFIX_ICON = PREFIX_AT + "icon/";
@ -71,14 +71,14 @@ public class MoreKeySpecParser {
} }
private static String parseEscape(String text) { private static String parseEscape(String text) {
if (text.indexOf(ESCAPE) < 0) { if (text.indexOf(ESCAPE_CHAR) < 0) {
return text; return text;
} }
final int length = text.length(); final int length = text.length();
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (int pos = 0; pos < length; pos++) { for (int pos = 0; pos < length; pos++) {
final char c = text.charAt(pos); final char c = text.charAt(pos);
if (c == ESCAPE && pos + 1 < length) { if (c == ESCAPE_CHAR && pos + 1 < length) {
sb.append(text.charAt(++pos)); sb.append(text.charAt(++pos));
} else { } else {
sb.append(c); sb.append(c);
@ -88,7 +88,7 @@ public class MoreKeySpecParser {
} }
private static int indexOfLabelEnd(String moreKeySpec, int start) { private static int indexOfLabelEnd(String moreKeySpec, int start) {
if (moreKeySpec.indexOf(ESCAPE, start) < 0) { if (moreKeySpec.indexOf(ESCAPE_CHAR, start) < 0) {
final int end = moreKeySpec.indexOf(LABEL_END, start); final int end = moreKeySpec.indexOf(LABEL_END, start);
if (end == 0) { if (end == 0) {
throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec); throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec);
@ -98,7 +98,7 @@ public class MoreKeySpecParser {
final int length = moreKeySpec.length(); final int length = moreKeySpec.length();
for (int pos = start; pos < length; pos++) { for (int pos = start; pos < length; pos++) {
final char c = moreKeySpec.charAt(pos); final char c = moreKeySpec.charAt(pos);
if (c == ESCAPE && pos + 1 < length) { if (c == ESCAPE_CHAR && pos + 1 < length) {
pos++; pos++;
} else if (moreKeySpec.startsWith(LABEL_END, pos)) { } else if (moreKeySpec.startsWith(LABEL_END, pos)) {
return pos; return pos;
@ -207,21 +207,19 @@ public class MoreKeySpecParser {
} }
}; };
public static CharSequence[] filterOut(Resources res, CharSequence[] moreKeys, public static String[] filterOut(Resources res, String[] moreKeys, CodeFilter filter) {
CodeFilter filter) {
if (moreKeys == null || moreKeys.length < 1) { if (moreKeys == null || moreKeys.length < 1) {
return null; return null;
} }
if (moreKeys.length == 1 if (moreKeys.length == 1 && filter.shouldFilterOut(getCode(res, moreKeys[0]))) {
&& filter.shouldFilterOut(getCode(res, moreKeys[0].toString()))) {
return null; return null;
} }
ArrayList<CharSequence> filtered = null; ArrayList<String> filtered = null;
for (int i = 0; i < moreKeys.length; i++) { for (int i = 0; i < moreKeys.length; i++) {
final CharSequence moreKeySpec = moreKeys[i]; final String moreKeySpec = moreKeys[i];
if (filter.shouldFilterOut(getCode(res, moreKeySpec.toString()))) { if (filter.shouldFilterOut(getCode(res, moreKeySpec))) {
if (filtered == null) { if (filtered == null) {
filtered = new ArrayList<CharSequence>(); filtered = new ArrayList<String>();
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
filtered.add(moreKeys[j]); filtered.add(moreKeys[j]);
} }
@ -236,6 +234,6 @@ public class MoreKeySpecParser {
if (filtered.size() == 0) { if (filtered.size() == 0) {
return null; return null;
} }
return filtered.toArray(new CharSequence[filtered.size()]); return filtered.toArray(new String[filtered.size()]);
} }
} }

View file

@ -16,8 +16,6 @@
package com.android.inputmethod.keyboard.internal; package com.android.inputmethod.keyboard.internal;
import com.android.inputmethod.keyboard.internal.KeyStyles.EmptyKeyStyle;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.text.TextUtils; import android.text.TextUtils;
@ -26,9 +24,8 @@ public class KeyStylesTests extends AndroidTestCase {
return message + " expected:<" + expected + "> but was:<" + actual + ">"; return message + " expected:<" + expected + "> but was:<" + actual + ">";
} }
private static void assertTextArray(String message, CharSequence value, private static void assertTextArray(String message, String value, String ... expected) {
CharSequence ... expected) { final String actual[] = KeyStyles.parseCsvText(value);
final CharSequence actual[] = EmptyKeyStyle.parseCsvText(value);
if (expected.length == 0) { if (expected.length == 0) {
assertNull(message, actual); assertNull(message, actual);
return; return;