From c8383eda8bb05e25b22fe6d66100248f7d0e33fa Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Sat, 12 Oct 2013 16:41:37 +0900 Subject: [PATCH] Fix wrong commitText parameter in sendKeyCodePoint() Small optimization on generating a String instance from a single code point too. bug: 11181913 Change-Id: I0f905e4dc6ec7841092bb4d3d940daf3b2303f5b --- java/src/com/android/inputmethod/latin/LatinIME.java | 8 ++++---- .../android/inputmethod/latin/utils/StringUtils.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index dc96cd750..8654f3b63 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -99,6 +99,7 @@ import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.LatinImeLoggerUtils; import com.android.inputmethod.latin.utils.RecapitalizeStatus; import com.android.inputmethod.latin.utils.StaticInnerHandlerWrapper; +import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask; import com.android.inputmethod.latin.utils.TextRange; import com.android.inputmethod.latin.utils.UserHistoryForgettingCurveUtils; @@ -1586,8 +1587,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // relying on this behavior so we continue to support it for older apps. sendDownUpKeyEvent(KeyEvent.KEYCODE_ENTER); } else { - final String text = new String(new int[] { code }, 0, 1); - mConnection.commitText(text, text.length()); + mConnection.commitText(StringUtils.newSingleCodePointString(code), 1); } } @@ -2335,11 +2335,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (mWordComposer.isComposingWord()) { // May have changed since we stored wasComposing if (currentSettings.mCorrectionEnabled) { final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR - : new String(new int[] { primaryCode }, 0, 1); + : StringUtils.newSingleCodePointString(primaryCode); commitCurrentAutoCorrection(separator); didAutoCorrect = true; } else { - commitTyped(new String(new int[]{primaryCode}, 0, 1)); + commitTyped(StringUtils.newSingleCodePointString(primaryCode)); } } diff --git a/java/src/com/android/inputmethod/latin/utils/StringUtils.java b/java/src/com/android/inputmethod/latin/utils/StringUtils.java index 121aecf0f..a36548392 100644 --- a/java/src/com/android/inputmethod/latin/utils/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/StringUtils.java @@ -48,6 +48,16 @@ public final class StringUtils { return text.codePointCount(0, text.length()); } + public static String newSingleCodePointString(int codePoint) { + if (Character.charCount(codePoint) == 1) { + // Optimization: avoid creating an temporary array for characters that are + // represented by a single char value + return String.valueOf((char) codePoint); + } + // For surrogate pair + return new String(Character.toChars(codePoint)); + } + public static boolean containsInArray(final String text, final String[] array) { for (final String element : array) { if (text.equals(element)) return true;