From bd7b160cfb05ee543e3cb6ddc7bd231b3f3aba0b Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 20 Jan 2012 13:18:06 +0900 Subject: [PATCH] Change more key specification type to String from CharSequence Change-Id: I5e910f8225a248fc6403771836dec4c0b7294caa --- .../com/android/inputmethod/keyboard/Key.java | 4 +- .../inputmethod/keyboard/MiniKeyboard.java | 14 ++- .../keyboard/internal/KeyStyles.java | 98 ++++++++++--------- .../keyboard/internal/MoreKeySpecParser.java | 26 +++-- .../keyboard/internal/KeyStylesTests.java | 7 +- 5 files changed, 73 insertions(+), 76 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index ebd61505d..6003d9723 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -101,7 +101,7 @@ public class Key { /** Text to output when pressed. This can be multiple characters, like ".com" */ public final CharSequence mOutputText; /** More keys */ - public final CharSequence[] mMoreKeys; + public final String[] mMoreKeys; /** More keys maximum column number */ public final int mMaxMoreKeysColumn; @@ -255,7 +255,7 @@ public class Key { // Update row to have current x coordinate. row.setXPos(keyXPos + keyWidth); - final CharSequence[] moreKeys = style.getTextArray(keyAttr, + final String[] moreKeys = style.getTextArray(keyAttr, R.styleable.Keyboard_Key_moreKeys); // In Arabic symbol layouts, we'd like to keep digits in more keys regardless of // config_digit_more_keys_enabled. diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java index 548b5ea85..974291373 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java @@ -34,7 +34,7 @@ public class MiniKeyboard extends Keyboard { } public static class Builder extends Keyboard.Builder { - private final CharSequence[] mMoreKeys; + private final String[] mMoreKeys; public static class MiniKeyboardParams extends Keyboard.Params { /* package */int mTopRowAdjustment; @@ -230,16 +230,14 @@ public class MiniKeyboard extends Keyboard { parentKey.mX + (mParams.mDefaultKeyWidth - width) / 2, view.getMeasuredWidth()); } - private static int getMaxKeyWidth(KeyboardView view, CharSequence[] moreKeys, - int minKeyWidth) { + private static int getMaxKeyWidth(KeyboardView view, String[] moreKeys, int minKeyWidth) { final int padding = (int) view.getContext().getResources() .getDimension(R.dimen.mini_keyboard_key_horizontal_padding); Paint paint = null; int maxWidth = minKeyWidth; - for (CharSequence moreKeySpec : moreKeys) { - final CharSequence label = MoreKeySpecParser.getLabel(moreKeySpec.toString()); - // If the label is single letter, minKeyWidth is enough to hold - // the label. + for (String moreKeySpec : moreKeys) { + final String label = MoreKeySpecParser.getLabel(moreKeySpec); + // If the label is single letter, minKeyWidth is enough to hold the label. if (label != null && label.length() > 1) { if (paint == null) { paint = new Paint(); @@ -258,7 +256,7 @@ public class MiniKeyboard extends Keyboard { public MiniKeyboard build() { final MiniKeyboardParams params = mParams; 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 Key key = new Key(mResources, params, moreKeySpec, params.getX(n, row), params.getY(row), params.mDefaultKeyWidth, params.mDefaultRowHeight); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java index 5dd8340fc..0f84c4563 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java @@ -37,20 +37,22 @@ public class KeyStyles { new HashMap(); private static final KeyStyle EMPTY_KEY_STYLE = new EmptyKeyStyle(); + private static final char ESCAPE_CHAR = '\\'; + 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 int getInt(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() { // Nothing to do. } @Override - public CharSequence[] getTextArray(TypedArray a, int index) { + public String[] getTextArray(TypedArray a, int index) { return parseTextArray(a, index); } @@ -69,64 +71,66 @@ public class KeyStyles { 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)) return null; 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; - if (size == 1) return new CharSequence[] { text }; - final StringBuilder sb = new StringBuilder(); - ArrayList list = null; - int start = 0; - for (int pos = 0; pos < size; pos++) { - final char c = text.charAt(pos); - if (c == ',') { - if (list == null) list = new ArrayList(); - if (sb.length() == 0) { - list.add(text.subSequence(start, pos)); - } else { - list.add(sb.toString()); - sb.setLength(0); - } - start = pos + 1; - continue; - } 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); + } + + /* package for test */ + static String[] parseCsvText(String text) { + final int size = text.length(); + if (size == 0) return null; + if (size == 1) return new String[] { text }; + final StringBuilder sb = new StringBuilder(); + ArrayList list = null; + int start = 0; + for (int pos = 0; pos < size; pos++) { + final char c = text.charAt(pos); + if (c == ',') { + if (list == null) list = new ArrayList(); + if (sb.length() == 0) { + list.add(text.substring(start, pos)); + } else { + list.add(sb.toString()); + sb.setLength(0); } + 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) }; - } else { - list.add(sb.length() > 0 ? sb : text.subSequence(start, size)); - return list.toArray(new CharSequence[list.size()]); - } + } + if (list == null) { + return new String[] { sb.length() > 0 ? sb.toString() : text.substring(start) }; + } else { + 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 mAttributes = new HashMap(); @Override - public CharSequence[] getTextArray(TypedArray a, int index) { + public String[] getTextArray(TypedArray a, int index) { return a.hasValue(index) - ? super.getTextArray(a, index) : (CharSequence[])mAttributes.get(index); + ? super.getTextArray(a, index) : (String[])mAttributes.get(index); } @Override diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java index c7d226d6e..e6b6db423 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java @@ -40,7 +40,7 @@ import java.util.ArrayList; public class MoreKeySpecParser { 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 PREFIX_AT = "@"; private static final String PREFIX_ICON = PREFIX_AT + "icon/"; @@ -71,14 +71,14 @@ public class MoreKeySpecParser { } private static String parseEscape(String text) { - if (text.indexOf(ESCAPE) < 0) { + if (text.indexOf(ESCAPE_CHAR) < 0) { return text; } final int length = text.length(); final StringBuilder sb = new StringBuilder(); for (int pos = 0; pos < length; 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)); } else { sb.append(c); @@ -88,7 +88,7 @@ public class MoreKeySpecParser { } 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); if (end == 0) { throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec); @@ -98,7 +98,7 @@ public class MoreKeySpecParser { final int length = moreKeySpec.length(); for (int pos = start; pos < length; pos++) { final char c = moreKeySpec.charAt(pos); - if (c == ESCAPE && pos + 1 < length) { + if (c == ESCAPE_CHAR && pos + 1 < length) { pos++; } else if (moreKeySpec.startsWith(LABEL_END, pos)) { return pos; @@ -207,21 +207,19 @@ public class MoreKeySpecParser { } }; - public static CharSequence[] filterOut(Resources res, CharSequence[] moreKeys, - CodeFilter filter) { + public static String[] filterOut(Resources res, String[] moreKeys, CodeFilter filter) { if (moreKeys == null || moreKeys.length < 1) { return null; } - if (moreKeys.length == 1 - && filter.shouldFilterOut(getCode(res, moreKeys[0].toString()))) { + if (moreKeys.length == 1 && filter.shouldFilterOut(getCode(res, moreKeys[0]))) { return null; } - ArrayList filtered = null; + ArrayList filtered = null; for (int i = 0; i < moreKeys.length; i++) { - final CharSequence moreKeySpec = moreKeys[i]; - if (filter.shouldFilterOut(getCode(res, moreKeySpec.toString()))) { + final String moreKeySpec = moreKeys[i]; + if (filter.shouldFilterOut(getCode(res, moreKeySpec))) { if (filtered == null) { - filtered = new ArrayList(); + filtered = new ArrayList(); for (int j = 0; j < i; j++) { filtered.add(moreKeys[j]); } @@ -236,6 +234,6 @@ public class MoreKeySpecParser { if (filtered.size() == 0) { return null; } - return filtered.toArray(new CharSequence[filtered.size()]); + return filtered.toArray(new String[filtered.size()]); } } diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeyStylesTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeyStylesTests.java index 4050a7123..cebe40958 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeyStylesTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeyStylesTests.java @@ -16,8 +16,6 @@ package com.android.inputmethod.keyboard.internal; -import com.android.inputmethod.keyboard.internal.KeyStyles.EmptyKeyStyle; - import android.test.AndroidTestCase; import android.text.TextUtils; @@ -26,9 +24,8 @@ public class KeyStylesTests extends AndroidTestCase { return message + " expected:<" + expected + "> but was:<" + actual + ">"; } - private static void assertTextArray(String message, CharSequence value, - CharSequence ... expected) { - final CharSequence actual[] = EmptyKeyStyle.parseCsvText(value); + private static void assertTextArray(String message, String value, String ... expected) { + final String actual[] = KeyStyles.parseCsvText(value); if (expected.length == 0) { assertNull(message, actual); return;