From e01d272603f3643ce613e61dd3204379f4f4fb73 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 2 Feb 2012 15:41:11 +0900 Subject: Make KeySpecParser and CSV parser code point aware This change also renames MoreKeySpecParser to KeySpecParser Change-Id: I35733cdbb344f16b57ffa2cfe79055c089b4e409 --- java/src/com/android/inputmethod/keyboard/Key.java | 19 +- .../android/inputmethod/keyboard/KeyboardView.java | 3 +- .../android/inputmethod/keyboard/MiniKeyboard.java | 4 +- .../keyboard/internal/KeySpecParser.java | 259 +++++++++++++++++++++ .../keyboard/internal/MoreKeySpecParser.java | 256 -------------------- .../android/inputmethod/latin/SettingsValues.java | 10 +- java/src/com/android/inputmethod/latin/Utils.java | 23 +- 7 files changed, 296 insertions(+), 278 deletions(-) create mode 100644 java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java (limited to 'java/src') diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 5e58821d0..8f2efab29 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -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); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 2cbd132ca..c6fb75489 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -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 { diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java index 433bd0d75..4648da1c1 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java @@ -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) { diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java new file mode 100644 index 000000000..519637bf4 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java @@ -0,0 +1,259 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.content.res.Resources; +import android.text.TextUtils; + +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.Utils; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * String parser of moreKeys attribute of Key. + * The string is comma separated texts each of which represents one "more key". + * Each "more key" specification is one of the following: + * - A single letter (Letter) + * - Label optionally followed by keyOutputText or code (keyLabel|keyOutputText). + * - Icon followed by keyOutputText or code (@icon/icon_name|@integer/key_code) + * Special character, comma ',' backslash '\', and bar '|' can be escaped by '\' + * character. + * Note that the character '@' and '\' are also parsed by XML parser and CSV parser as well. + * See {@link KeyboardIconsSet} about icon_number. + */ +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 KeySpecParser() { + // Intentional empty constructor for utility class. + } + + private static boolean hasIcon(String moreKeySpec) { + if (moreKeySpec.startsWith(PREFIX_ICON)) { + final int end = indexOfLabelEnd(moreKeySpec, 0); + if (end > 0) { + return true; + } + throw new MoreKeySpecParserError("outputText or code not specified: " + moreKeySpec); + } + return false; + } + + private static boolean hasCode(String moreKeySpec) { + final int end = indexOfLabelEnd(moreKeySpec, 0); + if (end > 0 && end + 1 < moreKeySpec.length() + && moreKeySpec.substring(end + 1).startsWith(PREFIX_CODE)) { + return true; + } + return false; + } + + private static String parseEscape(String text) { + if (text.indexOf(Utils.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 == Utils.ESCAPE_CHAR && pos + 1 < length) { + // Skip escape char + pos++; + sb.append(text.charAt(pos)); + } else { + sb.append(c); + } + } + return sb.toString(); + } + + private static int indexOfLabelEnd(String moreKeySpec, int start) { + if (moreKeySpec.indexOf(Utils.ESCAPE_CHAR, start) < 0) { + final int end = moreKeySpec.indexOf(LABEL_END, start); + if (end == 0) { + throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec); + } + return end; + } + final int length = moreKeySpec.length(); + 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; + } + } + return -1; + } + + public static String getLabel(String moreKeySpec) { + if (hasIcon(moreKeySpec)) { + return null; + } + final int end = indexOfLabelEnd(moreKeySpec, 0); + final String label = (end > 0) ? parseEscape(moreKeySpec.substring(0, end)) + : parseEscape(moreKeySpec); + if (TextUtils.isEmpty(label)) { + throw new MoreKeySpecParserError("Empty label: " + moreKeySpec); + } + return label; + } + + public static String getOutputText(String moreKeySpec) { + if (hasCode(moreKeySpec)) { + return null; + } + final int end = indexOfLabelEnd(moreKeySpec, 0); + if (end > 0) { + if (indexOfLabelEnd(moreKeySpec, end + 1) >= 0) { + throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": " + + moreKeySpec); + } + final String outputText = parseEscape( + moreKeySpec.substring(end + /* LABEL_END */1)); + if (!TextUtils.isEmpty(outputText)) { + return outputText; + } + throw new MoreKeySpecParserError("Empty outputText: " + moreKeySpec); + } + final String label = getLabel(moreKeySpec); + if (label == null) { + throw new MoreKeySpecParserError("Empty label: " + moreKeySpec); + } + // Code is automatically generated for one letter label. See {@link getCode()}. + return (Utils.codePointCount(label) == 1) ? null : label; + } + + public static int getCode(Resources res, String moreKeySpec) { + if (hasCode(moreKeySpec)) { + final int end = indexOfLabelEnd(moreKeySpec, 0); + if (indexOfLabelEnd(moreKeySpec, end + 1) >= 0) { + throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": " + moreKeySpec); + } + final int resId = Utils.getResourceId(res, + moreKeySpec.substring(end + /* LABEL_END */1 + /* PREFIX_AT */1), + R.string.english_ime_name); + final int code = res.getInteger(resId); + return code; + } + if (indexOfLabelEnd(moreKeySpec, 0) > 0) { + return Keyboard.CODE_OUTPUT_TEXT; + } + final String label = getLabel(moreKeySpec); + // Code is automatically generated for one letter label. + if (Utils.codePointCount(label) == 1) { + return label.codePointAt(0); + } + return Keyboard.CODE_OUTPUT_TEXT; + } + + public static int getIconAttrId(String moreKeySpec) { + if (hasIcon(moreKeySpec)) { + final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length()); + final String name = moreKeySpec.substring(PREFIX_ICON.length(), end); + return KeyboardIconsSet.getIconAttrId(name); + } + return KeyboardIconsSet.ICON_UNDEFINED; + } + + public static String[] insertAddtionalMoreKeys(String[] moreKeys, String[] additionalMoreKeys) { + final int moreKeysCount = (moreKeys != null) ? moreKeys.length : 0; + final int additionalCount = (additionalMoreKeys != null) ? additionalMoreKeys.length : 0; + ArrayList out = null; + int additionalIndex = 0; + for (int moreKeyIndex = 0; moreKeyIndex < moreKeysCount; moreKeyIndex++) { + final String moreKeySpec = moreKeys[moreKeyIndex]; + if (moreKeySpec.equals(ADDITIONAL_MORE_KEY_MARKER)) { + if (additionalIndex < additionalCount) { + // Replace '%' marker with additional more key specification. + final String additionalMoreKey = additionalMoreKeys[additionalIndex]; + if (out != null) { + out.add(additionalMoreKey); + } else { + moreKeys[moreKeyIndex] = additionalMoreKey; + } + additionalIndex++; + } else { + // Filter out excessive '%' marker. + if (out == null) { + out = new ArrayList(moreKeyIndex); + for (int i = 0; i < moreKeyIndex; i++) { + out.add(moreKeys[i]); + } + } + } + } else { + if (out != null) { + out.add(moreKeySpec); + } + } + } + if (additionalCount > 0 && additionalIndex == 0) { + // No '%' marker is found in more keys. + // Insert all additional more keys to the head of more keys. + if (DEBUG && out != null) { + throw new RuntimeException("Internal logic error:" + + " moreKeys=" + Arrays.toString(moreKeys) + + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys)); + } + out = new ArrayList(additionalCount + moreKeysCount); + for (int i = additionalIndex; i < additionalCount; i++) { + out.add(additionalMoreKeys[i]); + } + for (int i = 0; i < moreKeysCount; i++) { + out.add(moreKeys[i]); + } + } else if (additionalIndex < additionalCount) { + // The number of '%' markers are less than additional more keys. + // Append remained additional more keys to the tail of more keys. + if (DEBUG && out != null) { + throw new RuntimeException("Internal logic error:" + + " moreKeys=" + Arrays.toString(moreKeys) + + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys)); + } + out = new ArrayList(moreKeysCount); + for (int i = 0; i < moreKeysCount; i++) { + out.add(moreKeys[i]); + } + for (int i = additionalIndex; i < additionalCount; i++) { + out.add(additionalMoreKeys[additionalIndex]); + } + } + if (out != null) { + return out.size() > 0 ? out.toArray(new String[out.size()]) : null; + } else { + return moreKeys; + } + } + + @SuppressWarnings("serial") + public static class MoreKeySpecParserError extends RuntimeException { + public MoreKeySpecParserError(String message) { + super(message); + } + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java deleted file mode 100644 index abebfec01..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.android.inputmethod.keyboard.internal; - -import android.content.res.Resources; -import android.text.TextUtils; - -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.Utils; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * String parser of moreKeys attribute of Key. - * The string is comma separated texts each of which represents one "more key". - * Each "more key" specification is one of the following: - * - A single letter (Letter) - * - Label optionally followed by keyOutputText or code (keyLabel|keyOutputText). - * - Icon followed by keyOutputText or code (@icon/icon_name|@integer/key_code) - * Special character, comma ',' backslash '\', and bar '|' can be escaped by '\' - * character. - * 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 { - 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() { - // Intentional empty constructor for utility class. - } - - private static boolean hasIcon(String moreKeySpec) { - if (moreKeySpec.startsWith(PREFIX_ICON)) { - final int end = indexOfLabelEnd(moreKeySpec, 0); - if (end > 0) { - return true; - } - throw new MoreKeySpecParserError("outputText or code not specified: " + moreKeySpec); - } - return false; - } - - private static boolean hasCode(String moreKeySpec) { - final int end = indexOfLabelEnd(moreKeySpec, 0); - if (end > 0 && end + 1 < moreKeySpec.length() - && moreKeySpec.substring(end + 1).startsWith(PREFIX_CODE)) { - return true; - } - return false; - } - - private static String parseEscape(String text) { - if (text.indexOf(Utils.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 == Utils.ESCAPE_CHAR && pos + 1 < length) { - sb.append(text.charAt(++pos)); - } else { - sb.append(c); - } - } - return sb.toString(); - } - - private static int indexOfLabelEnd(String moreKeySpec, int start) { - if (moreKeySpec.indexOf(Utils.ESCAPE_CHAR, start) < 0) { - final int end = moreKeySpec.indexOf(LABEL_END, start); - if (end == 0) { - throw new MoreKeySpecParserError(LABEL_END + " at " + start + ": " + moreKeySpec); - } - return end; - } - final int length = moreKeySpec.length(); - for (int pos = start; pos < length; pos++) { - final char c = moreKeySpec.charAt(pos); - if (c == Utils.ESCAPE_CHAR && pos + 1 < length) { - pos++; - } else if (c == LABEL_END) { - return pos; - } - } - return -1; - } - - public static String getLabel(String moreKeySpec) { - if (hasIcon(moreKeySpec)) { - return null; - } - final int end = indexOfLabelEnd(moreKeySpec, 0); - final String label = (end > 0) ? parseEscape(moreKeySpec.substring(0, end)) - : parseEscape(moreKeySpec); - if (TextUtils.isEmpty(label)) { - throw new MoreKeySpecParserError("Empty label: " + moreKeySpec); - } - return label; - } - - public static String getOutputText(String moreKeySpec) { - if (hasCode(moreKeySpec)) { - return null; - } - final int end = indexOfLabelEnd(moreKeySpec, 0); - if (end > 0) { - if (indexOfLabelEnd(moreKeySpec, end + 1) >= 0) { - throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": " - + moreKeySpec); - } - final String outputText = parseEscape( - moreKeySpec.substring(end + /* LABEL_END */1)); - if (!TextUtils.isEmpty(outputText)) { - return outputText; - } - throw new MoreKeySpecParserError("Empty outputText: " + moreKeySpec); - } - final String label = getLabel(moreKeySpec); - if (label == null) { - throw new MoreKeySpecParserError("Empty label: " + moreKeySpec); - } - // Code is automatically generated for one letter label. See {@link getCode()}. - return (label.length() == 1) ? null : label; - } - - public static int getCode(Resources res, String moreKeySpec) { - if (hasCode(moreKeySpec)) { - final int end = indexOfLabelEnd(moreKeySpec, 0); - if (indexOfLabelEnd(moreKeySpec, end + 1) >= 0) { - throw new MoreKeySpecParserError("Multiple " + LABEL_END + ": " + moreKeySpec); - } - final int resId = Utils.getResourceId(res, - moreKeySpec.substring(end + /* LABEL_END */1 + /* PREFIX_AT */1), - R.string.english_ime_name); - final int code = res.getInteger(resId); - return code; - } - if (indexOfLabelEnd(moreKeySpec, 0) > 0) { - return Keyboard.CODE_OUTPUT_TEXT; - } - final String label = getLabel(moreKeySpec); - // Code is automatically generated for one letter label. - if (label != null && label.length() == 1) { - return label.charAt(0); - } - return Keyboard.CODE_OUTPUT_TEXT; - } - - public static int getIconAttrId(String moreKeySpec) { - if (hasIcon(moreKeySpec)) { - final int end = moreKeySpec.indexOf(LABEL_END, PREFIX_ICON.length()); - final String name = moreKeySpec.substring(PREFIX_ICON.length(), end); - return KeyboardIconsSet.getIconAttrId(name); - } - return KeyboardIconsSet.ICON_UNDEFINED; - } - - public static String[] insertAddtionalMoreKeys(String[] moreKeys, String[] additionalMoreKeys) { - final int moreKeysCount = (moreKeys != null) ? moreKeys.length : 0; - final int additionalCount = (additionalMoreKeys != null) ? additionalMoreKeys.length : 0; - ArrayList out = null; - int additionalIndex = 0; - for (int moreKeyIndex = 0; moreKeyIndex < moreKeysCount; moreKeyIndex++) { - final String moreKeySpec = moreKeys[moreKeyIndex]; - if (moreKeySpec.equals(ADDITIONAL_MORE_KEY_MARKER)) { - if (additionalIndex < additionalCount) { - // Replace '%' marker with additional more key specification. - final String additionalMoreKey = additionalMoreKeys[additionalIndex]; - if (out != null) { - out.add(additionalMoreKey); - } else { - moreKeys[moreKeyIndex] = additionalMoreKey; - } - additionalIndex++; - } else { - // Filter out excessive '%' marker. - if (out == null) { - out = new ArrayList(moreKeyIndex); - for (int i = 0; i < moreKeyIndex; i++) { - out.add(moreKeys[i]); - } - } - } - } else { - if (out != null) { - out.add(moreKeySpec); - } - } - } - if (additionalCount > 0 && additionalIndex == 0) { - // No '%' marker is found in more keys. - // Insert all additional more keys to the head of more keys. - if (DEBUG && out != null) { - throw new RuntimeException("Internal logic error:" - + " moreKeys=" + Arrays.toString(moreKeys) - + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys)); - } - out = new ArrayList(additionalCount + moreKeysCount); - for (int i = additionalIndex; i < additionalCount; i++) { - out.add(additionalMoreKeys[i]); - } - for (int i = 0; i < moreKeysCount; i++) { - out.add(moreKeys[i]); - } - } else if (additionalIndex < additionalCount) { - // The number of '%' markers are less than additional more keys. - // Append remained additional more keys to the tail of more keys. - if (DEBUG && out != null) { - throw new RuntimeException("Internal logic error:" - + " moreKeys=" + Arrays.toString(moreKeys) - + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys)); - } - out = new ArrayList(moreKeysCount); - for (int i = 0; i < moreKeysCount; i++) { - out.add(moreKeys[i]); - } - for (int i = additionalIndex; i < additionalCount; i++) { - out.add(additionalMoreKeys[additionalIndex]); - } - } - if (out != null) { - return out.size() > 0 ? out.toArray(new String[out.size()]) : null; - } else { - return moreKeys; - } - } - - @SuppressWarnings("serial") - public static class MoreKeySpecParserError extends RuntimeException { - public MoreKeySpecParserError(String message) { - super(message); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index 5f9cb8df6..4f8caa883 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -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)); } } } diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index d1b808fc8..7b9e4e23d 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -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(); } @@ -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)); -- cgit v1.2.3-83-g751a