am 8ca325f4: Merge "Make KeySpecParser and CSV parser code point aware"

* commit '8ca325f437a4dd6484e14ac66415b792195dbd40':
  Make KeySpecParser and CSV parser code point aware
This commit is contained in:
Tadashi G. Takaoka 2012-02-01 22:58:51 -08:00 committed by Android Git Automerger
commit d661845c0b
8 changed files with 121 additions and 37 deletions

View file

@ -28,8 +28,9 @@ import android.util.Xml;
import com.android.inputmethod.keyboard.internal.KeyStyles;
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.MoreKeySpecParser;
import com.android.inputmethod.keyboard.internal.KeySpecParser;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.XmlParseUtils;
import org.xmlpull.v1.XmlPullParser;
@ -128,7 +129,7 @@ public class Key {
private boolean mEnabled = true;
private static Drawable getIcon(Keyboard.Params params, String moreKeySpec) {
final int iconAttrId = MoreKeySpecParser.getIconAttrId(moreKeySpec);
final int iconAttrId = KeySpecParser.getIconAttrId(moreKeySpec);
if (iconAttrId == KeyboardIconsSet.ICON_UNDEFINED) {
return null;
} else {
@ -141,9 +142,9 @@ public class Key {
*/
public Key(Resources res, Keyboard.Params params, String moreKeySpec,
int x, int y, int width, int height) {
this(params, MoreKeySpecParser.getLabel(moreKeySpec), null, getIcon(params, moreKeySpec),
MoreKeySpecParser.getCode(res, moreKeySpec),
MoreKeySpecParser.getOutputText(moreKeySpec),
this(params, KeySpecParser.getLabel(moreKeySpec), null, getIcon(params, moreKeySpec),
KeySpecParser.getCode(res, moreKeySpec),
KeySpecParser.getOutputText(moreKeySpec),
x, y, width, height);
}
@ -245,7 +246,7 @@ public class Key {
int actionFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyActionFlags, 0);
final String[] additionalMoreKeys = style.getStringArray(
keyAttr, R.styleable.Keyboard_Key_additionalMoreKeys);
final String[] moreKeys = MoreKeySpecParser.insertAddtionalMoreKeys(style.getStringArray(
final String[] moreKeys = KeySpecParser.insertAddtionalMoreKeys(style.getStringArray(
keyAttr, R.styleable.Keyboard_Key_moreKeys), additionalMoreKeys);
if (moreKeys != null) {
actionFlags |= ACTION_FLAGS_ENABLE_LONG_PRESS;
@ -270,7 +271,7 @@ public class Key {
// Choose the first letter of the label as primary code if not specified.
if (code == Keyboard.CODE_UNSPECIFIED && TextUtils.isEmpty(outputText)
&& !TextUtils.isEmpty(mLabel)) {
if (mLabel.codePointCount(0, mLabel.length()) == 1) {
if (Utils.codePointCount(mLabel) == 1) {
// Use the first letter of the hint label if shiftedLetterActivated flag is
// specified.
if (hasShiftedLetterHint() && isShiftedLetterActivated()
@ -308,7 +309,7 @@ public class Key {
if (!Keyboard.isLetterCode(code) || preserveCase) return code;
final String text = new String(new int[] { code } , 0, 1);
final String casedText = adjustCaseOfStringForKeyboardId(text, preserveCase, id);
return casedText.codePointCount(0, casedText.length()) == 1
return Utils.codePointCount(casedText) == 1
? casedText.codePointAt(0) : Keyboard.CODE_UNSPECIFIED;
}
@ -380,7 +381,7 @@ public class Key {
@Override
public String toString() {
String top = Keyboard.printableCode(mCode);
if (mLabel != null && mLabel.codePointCount(0, mLabel.length()) != 1) {
if (Utils.codePointCount(mLabel) != 1) {
top += "/\"" + mLabel + '"';
}
return String.format("%s %d,%d", top, mX, mY);

View file

@ -41,6 +41,7 @@ import com.android.inputmethod.compat.FrameLayoutCompatUtils;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
import com.android.inputmethod.latin.Utils;
import java.util.HashMap;
@ -851,7 +852,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
if (key.mLabel != null) {
// TODO Should take care of temporaryShiftLabel here.
previewText.setCompoundDrawables(null, null, null, null);
if (key.mLabel.length() > 1) {
if (Utils.codePointCount(key.mLabel) > 1) {
previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, params.mKeyLetterSize);
previewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {

View file

@ -18,7 +18,7 @@ package com.android.inputmethod.keyboard;
import android.graphics.Paint;
import com.android.inputmethod.keyboard.internal.MoreKeySpecParser;
import com.android.inputmethod.keyboard.internal.KeySpecParser;
import com.android.inputmethod.latin.R;
public class MiniKeyboard extends Keyboard {
@ -235,7 +235,7 @@ public class MiniKeyboard extends Keyboard {
Paint paint = null;
int maxWidth = minKeyWidth;
for (String moreKeySpec : moreKeys) {
final String label = MoreKeySpecParser.getLabel(moreKeySpec);
final String label = KeySpecParser.getLabel(moreKeySpec);
// If the label is single letter, minKeyWidth is enough to hold the label.
if (label != null && label.length() > 1) {
if (paint == null) {

View file

@ -39,14 +39,14 @@ import java.util.Arrays;
* Note that the character '@' and '\' are also parsed by XML parser and CSV parser as well.
* See {@link KeyboardIconsSet} about icon_number.
*/
public class MoreKeySpecParser {
public class KeySpecParser {
private static final boolean DEBUG = LatinImeLogger.sDBG;
private static final char LABEL_END = '|';
private static final String PREFIX_ICON = Utils.PREFIX_AT + "icon" + Utils.SUFFIX_SLASH;
private static final String PREFIX_CODE = Utils.PREFIX_AT + "integer" + Utils.SUFFIX_SLASH;
private static final String ADDITIONAL_MORE_KEY_MARKER = "%";
private MoreKeySpecParser() {
private KeySpecParser() {
// Intentional empty constructor for utility class.
}
@ -79,7 +79,9 @@ public class MoreKeySpecParser {
for (int pos = 0; pos < length; pos++) {
final char c = text.charAt(pos);
if (c == Utils.ESCAPE_CHAR && pos + 1 < length) {
sb.append(text.charAt(++pos));
// Skip escape char
pos++;
sb.append(text.charAt(pos));
} else {
sb.append(c);
}
@ -99,6 +101,7 @@ public class MoreKeySpecParser {
for (int pos = start; pos < length; pos++) {
final char c = moreKeySpec.charAt(pos);
if (c == Utils.ESCAPE_CHAR && pos + 1 < length) {
// Skip escape char
pos++;
} else if (c == LABEL_END) {
return pos;
@ -142,7 +145,7 @@ public class MoreKeySpecParser {
throw new MoreKeySpecParserError("Empty label: " + moreKeySpec);
}
// Code is automatically generated for one letter label. See {@link getCode()}.
return (label.length() == 1) ? null : label;
return (Utils.codePointCount(label) == 1) ? null : label;
}
public static int getCode(Resources res, String moreKeySpec) {
@ -162,8 +165,8 @@ public class MoreKeySpecParser {
}
final String label = getLabel(moreKeySpec);
// Code is automatically generated for one letter label.
if (label != null && label.length() == 1) {
return label.charAt(0);
if (Utils.codePointCount(label) == 1) {
return label.codePointAt(0);
}
return Keyboard.CODE_OUTPUT_TEXT;
}

View file

@ -25,7 +25,7 @@ import android.view.inputmethod.EditorInfo;
import com.android.inputmethod.compat.InputTypeCompatUtils;
import com.android.inputmethod.compat.VibratorCompatWrapper;
import com.android.inputmethod.keyboard.internal.MoreKeySpecParser;
import com.android.inputmethod.keyboard.internal.KeySpecParser;
import java.util.Arrays;
import java.util.Locale;
@ -158,7 +158,7 @@ public class SettingsValues {
final StringBuilder sb = new StringBuilder();
if (puncs != null) {
for (final String puncSpec : puncs) {
sb.append(MoreKeySpecParser.getLabel(puncSpec));
sb.append(KeySpecParser.getLabel(puncSpec));
}
}
return sb.toString();
@ -168,7 +168,7 @@ public class SettingsValues {
final SuggestedWords.Builder builder = new SuggestedWords.Builder();
if (puncs != null) {
for (final String puncSpec : puncs) {
builder.addWord(MoreKeySpecParser.getLabel(puncSpec));
builder.addWord(KeySpecParser.getLabel(puncSpec));
}
}
return builder.setIsPunctuationSuggestions().build();
@ -178,11 +178,11 @@ public class SettingsValues {
final SuggestedWords.Builder builder = new SuggestedWords.Builder();
if (puncs != null) {
for (final String puncSpec : puncs) {
final String outputText = MoreKeySpecParser.getOutputText(puncSpec);
final String outputText = KeySpecParser.getOutputText(puncSpec);
if (outputText != null) {
builder.addWord(outputText);
} else {
builder.addWord(MoreKeySpecParser.getLabel(puncSpec));
builder.addWord(KeySpecParser.getLabel(puncSpec));
}
}
}

View file

@ -800,6 +800,13 @@ public class Utils {
}
}
public static int codePointCount(String text) {
if (TextUtils.isEmpty(text)) return 0;
return text.codePointCount(0, text.length());
}
// TODO: Move these methods to KeySpecParser.
public static int getResourceId(Resources res, String name, int packageNameResId) {
String packageName = res.getResourcePackageName(packageNameResId);
int resId = res.getIdentifier(name, null, packageName);
@ -858,13 +865,15 @@ public class Utils {
return size;
}
private static int COMMA = ',';
public static String[] parseCsvString(String rawText, Resources res, int packageNameResId) {
final String text = resolveStringResource(rawText, res, packageNameResId);
final int size = text.length();
if (size == 0) {
return null;
}
if (size == 1) {
if (codePointCount(text) == 1) {
return new String[] { text };
}
@ -873,7 +882,7 @@ public class Utils {
int start = 0;
for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos);
if (c == ',') {
if (c == COMMA) {
if (list == null) {
list = new ArrayList<String>();
}
@ -883,17 +892,21 @@ public class Utils {
list.add(sb.toString());
sb.setLength(0);
}
// Skip comma
start = pos + 1;
continue;
} else if (c == ESCAPE_CHAR) {
}
// Skip escaped sequence.
if (c == ESCAPE_CHAR) {
if (start == pos) {
// Skip escape character at the beginning of the value.
// Skip escaping comma at the beginning of the text.
start++;
pos++;
} else {
if (start < pos && sb.length() == 0) {
sb.append(text.subSequence(start, pos));
sb.append(text.substring(start, pos));
}
// Skip comma
pos++;
if (pos < size) {
sb.append(text.charAt(pos));

View file

@ -25,7 +25,7 @@ import com.android.inputmethod.latin.tests.R;
import java.util.Arrays;
public class KeyStylesTests extends AndroidTestCase {
public class CsvParserTests extends AndroidTestCase {
private Resources mTestResources;
@Override
@ -64,42 +64,77 @@ public class KeyStylesTests extends AndroidTestCase {
}
}
// \U001d11e: MUSICAL SYMBOL G CLEF
private static final String PAIR1 = "\ud834\udd1e";
// \U001d122: MUSICAL SYMBOL F CLEF
private static final String PAIR2 = "\ud834\udd22";
// \U002f8a6: CJK COMPATIBILITY IDEOGRAPH-2F8A6; variant character of \u6148.
private static final String PAIR3 = "\ud87e\udca6";
private static final String SURROGATE1 = PAIR1 + PAIR2;
private static final String SURROGATE2 = PAIR1 + PAIR2 + PAIR3;
public void testParseCsvTextZero() {
assertTextArray("Empty string", "");
}
public void testParseCsvTextSingle() {
assertTextArray("Single char", "a", "a");
assertTextArray("Surrogate pair", PAIR1, PAIR1);
assertTextArray("Space", " ", " ");
assertTextArray("Single label", "abc", "abc");
assertTextArray("Single srrogate pairs label", SURROGATE2, SURROGATE2);
assertTextArray("Spaces", " ", " ");
assertTextArray("Spaces in label", "a b c", "a b c");
assertTextArray("Spaces at beginning of label", " abc", " abc");
assertTextArray("Spaces at end of label", "abc ", "abc ");
assertTextArray("Label surrounded by spaces", " abc ", " abc ");
assertTextArray("Surrogate pair surrounded by space",
" " + PAIR1 + " ",
" " + PAIR1 + " ");
assertTextArray("Surrogate pair within characters",
"ab" + PAIR2 + "cd",
"ab" + PAIR2 + "cd");
assertTextArray("Surrogate pairs within characters",
"ab" + SURROGATE1 + "cd",
"ab" + SURROGATE1 + "cd");
assertTextArray("Incomplete resource reference 1", "string", "string");
assertTextArray("Incomplete resource reference 2", "@strin", "@strin");
assertTextArray("Incomplete resource reference 3", "@" + SURROGATE2, "@" + SURROGATE2);
}
public void testParseCsvTextSingleEscaped() {
assertTextArray("Escaped char", "\\a", "a");
assertTextArray("Escaped surrogate pair", "\\" + PAIR1, PAIR1);
assertTextArray("Escaped comma", "\\,", ",");
assertTextArray("Escaped escape", "\\\\", "\\");
assertTextArray("Escaped label", "a\\bc", "abc");
assertTextArray("Escaped surrogate", "a\\" + PAIR1 + "c", "a" + PAIR1 + "c");
assertTextArray("Escaped label at beginning", "\\abc", "abc");
assertTextArray("Escaped surrogate at beginning", "\\" + SURROGATE2, SURROGATE2);
assertTextArray("Escaped label with comma", "a\\,c", "a,c");
assertTextArray("Escaped surrogate with comma", PAIR1 + "\\," + PAIR2, PAIR1 + "," + PAIR2);
assertTextArray("Escaped label with comma at beginning", "\\,bc", ",bc");
assertTextArray("Escaped surrogate with comma at beginning",
"\\," + SURROGATE1, "," + SURROGATE1);
assertTextArray("Escaped label with successive", "\\,\\\\bc", ",\\bc");
assertTextArray("Escaped surrogate with successive",
"\\,\\\\" + SURROGATE1, ",\\" + SURROGATE1);
assertTextArray("Escaped label with escape", "a\\\\c", "a\\c");
assertTextArray("Escaped surrogate with escape",
PAIR1 + "\\\\" + PAIR2, PAIR1 + "\\" + PAIR2);
assertTextArray("Escaped @string", "\\@string/empty_string", "@string/empty_string");
}
public void testParseCsvTextMulti() {
assertTextArray("Multiple chars", "a,b,c", "a", "b", "c");
assertTextArray("Multiple surrogates", PAIR1 + "," + PAIR2 + "," + PAIR3,
PAIR1, PAIR2, PAIR3);
assertTextArray("Multiple chars surrounded by spaces", " a , b , c ", " a ", " b ", " c ");
assertTextArray("Multiple labels", "abc,def,ghi", "abc", "def", "ghi");
assertTextArray("Multiple surrogated", SURROGATE1 + "," + SURROGATE2,
SURROGATE1, SURROGATE2);
assertTextArray("Multiple labels surrounded by spaces", " abc , def , ghi ",
" abc ", " def ", " ghi ");
}

View file

@ -24,7 +24,7 @@ import com.android.inputmethod.latin.R;
import java.util.Arrays;
public class MoreKeySpecParserTests extends AndroidTestCase {
public class KeySpecParserTests extends AndroidTestCase {
private Resources mRes;
private static final int ICON_SETTINGS_KEY = R.styleable.Keyboard_iconSettingsKey;
@ -49,16 +49,16 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
private void assertParser(String message, String moreKeySpec, String expectedLabel,
String expectedOutputText, int expectedIcon, int expectedCode) {
String actualLabel = MoreKeySpecParser.getLabel(moreKeySpec);
String actualLabel = KeySpecParser.getLabel(moreKeySpec);
assertEquals(message + ": label:", expectedLabel, actualLabel);
String actualOutputText = MoreKeySpecParser.getOutputText(moreKeySpec);
String actualOutputText = KeySpecParser.getOutputText(moreKeySpec);
assertEquals(message + ": ouptputText:", expectedOutputText, actualOutputText);
int actualIcon = MoreKeySpecParser.getIconAttrId(moreKeySpec);
int actualIcon = KeySpecParser.getIconAttrId(moreKeySpec);
assertEquals(message + ": icon:", expectedIcon, actualIcon);
int actualCode = MoreKeySpecParser.getCode(mRes, moreKeySpec);
int actualCode = KeySpecParser.getCode(mRes, moreKeySpec);
assertEquals(message + ": codes value:", expectedCode, actualCode);
}
@ -73,9 +73,22 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
}
}
// \U001d11e: MUSICAL SYMBOL G CLEF
private static final String PAIR1 = "\ud834\udd1e";
private static final int CODE1 = PAIR1.codePointAt(0);
// \U001d122: MUSICAL SYMBOL F CLEF
private static final String PAIR2 = "\ud834\udd22";
private static final int CODE2 = PAIR2.codePointAt(0);
// \U002f8a6: CJK COMPATIBILITY IDEOGRAPH-2F8A6; variant character of \u6148.
private static final String PAIR3 = "\ud87e\udca6";
private static final String SURROGATE1 = PAIR1 + PAIR2;
private static final String SURROGATE2 = PAIR1 + PAIR2 + PAIR3;
public void testSingleLetter() {
assertParser("Single letter", "a",
"a", null, ICON_UNDEFINED, 'a');
assertParser("Single surrogate", PAIR1,
PAIR1, null, ICON_UNDEFINED, CODE1);
assertParser("Single escaped bar", "\\|",
"|", null, ICON_UNDEFINED, '|');
assertParser("Single escaped escape", "\\\\",
@ -86,20 +99,31 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
",", null, ICON_UNDEFINED, ',');
assertParser("Single escaped letter", "\\a",
"a", null, ICON_UNDEFINED, 'a');
assertParser("Single escaped surrogate", "\\" + PAIR2,
PAIR2, null, ICON_UNDEFINED, CODE2);
assertParser("Single at", "@",
"@", null, ICON_UNDEFINED, '@');
assertParser("Single escaped at", "\\@",
"@", null, ICON_UNDEFINED, '@');
assertParser("Single letter with outputText", "a|abc",
"a", "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with surrogate outputText", "a|" + SURROGATE1,
"a", SURROGATE1, ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single surrogate with outputText", PAIR3 + "|abc",
PAIR3, "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with escaped outputText", "a|a\\|c",
"a", "a|c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with escaped surrogate outputText",
"a|" + PAIR1 + "\\|" + PAIR2,
"a", PAIR1 + "|" + PAIR2, ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with comma outputText", "a|a,b",
"a", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with escaped comma outputText", "a|a\\,b",
"a", "a,b", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with outputText starts with at", "a|@bc",
"a", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with surrogate outputText starts with at", "a|@" + SURROGATE2,
"a", "@" + SURROGATE2, ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with outputText contains at", "a|a@c",
"a", "a@c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Single letter with escaped at outputText", "a|\\@bc",
@ -115,8 +139,13 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
public void testLabel() {
assertParser("Simple label", "abc",
"abc", "abc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Simple surrogate label", SURROGATE1,
SURROGATE1, SURROGATE1, ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label with escaped bar", "a\\|c",
"a|c", "a|c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Surrogate label with escaped bar", PAIR1 + "\\|" + PAIR2,
PAIR1 + "|" + PAIR2, PAIR1 + "|" + PAIR2,
ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label with escaped escape", "a\\\\c",
"a\\c", "a\\c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label with comma", "a,c",
@ -125,6 +154,8 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
"a,c", "a,c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label starts with at", "@bc",
"@bc", "@bc", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Surrogate label starts with at", "@" + SURROGATE1,
"@" + SURROGATE1, "@" + SURROGATE1, ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label contains at", "a@c",
"a@c", "a@c", ICON_UNDEFINED, Keyboard.CODE_OUTPUT_TEXT);
assertParser("Label with escaped at", "\\@bc",
@ -220,9 +251,9 @@ public class MoreKeySpecParserTests extends AndroidTestCase {
null, null, ICON_SETTINGS_KEY, mCodeSettings);
}
private void assertMoreKeys(String message, String[] moreKeys, String[] additionalMoreKeys,
String[] expected) {
final String[] actual = MoreKeySpecParser.insertAddtionalMoreKeys(
private static void assertMoreKeys(String message, String[] moreKeys,
String[] additionalMoreKeys, String[] expected) {
final String[] actual = KeySpecParser.insertAddtionalMoreKeys(
moreKeys, additionalMoreKeys);
if (expected == null && actual == null) {
return;