aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2012-02-01 22:56:54 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2012-02-01 22:56:54 -0800
commit8ca325f437a4dd6484e14ac66415b792195dbd40 (patch)
treec8f5f7639903bd26de8f17ca11db20f058dee4c5 /java/src
parent51fd1632f59bd9aaeb5c98ff031f1618e8c31c59 (diff)
parente01d272603f3643ce613e61dd3204379f4f4fb73 (diff)
downloadlatinime-8ca325f437a4dd6484e14ac66415b792195dbd40.tar.gz
latinime-8ca325f437a4dd6484e14ac66415b792195dbd40.tar.xz
latinime-8ca325f437a4dd6484e14ac66415b792195dbd40.zip
Merge "Make KeySpecParser and CSV parser code point aware"
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/keyboard/Key.java19
-rw-r--r--java/src/com/android/inputmethod/keyboard/KeyboardView.java3
-rw-r--r--java/src/com/android/inputmethod/keyboard/MiniKeyboard.java4
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java (renamed from java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java)15
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsValues.java10
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java23
6 files changed, 46 insertions, 28 deletions
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/MoreKeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
index abebfec01..519637bf4 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpecParser.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
@@ -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;
}
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<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));