From 4ef27c035892888df82d9c9f32ac12ed48c8308f Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 3 Oct 2014 11:39:21 +0900 Subject: [PATCH] [ML13] Fix the locale passing in ProximityInfo The locale is used to determine additional proximity characters. This is dependent on the dictionary language, but was passed as a function of the layout, which is wrong and would have given bad suggestions in multi-lingual mode. Ideally, additional proximity characters should be inserted in the dictionary header, but for now it's a rather simple change to get it from the dictionary's locale instead of the proximity info locale. Also, that allows us to remove completely the locale parameter from proximity info, which is a much needed change. This change has zero effect on unit tests and on regression tests. Bug: 11230254 Change-Id: If95157155db7dccd1f00b8ba55ccb3600283f9e4 --- .../inputmethod/keyboard/Keyboard.java | 7 ++--- .../inputmethod/keyboard/ProximityInfo.java | 28 +++++++------------ ...oid_inputmethod_keyboard_ProximityInfo.cpp | 6 ++-- .../layout/additional_proximity_chars.cpp | 2 +- .../core/layout/additional_proximity_chars.h | 27 ++++++++++++------ .../suggest/core/layout/proximity_info.cpp | 21 +++++--------- .../src/suggest/core/layout/proximity_info.h | 14 ++++------ .../core/layout/proximity_info_state.cpp | 4 +-- .../core/layout/proximity_info_state.h | 3 +- .../core/layout/proximity_info_utils.h | 11 ++++---- .../core/session/dic_traverse_session.cpp | 8 ++++-- 11 files changed, 64 insertions(+), 67 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 85dfea4e7..d35c8fae1 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -108,10 +108,9 @@ public class Keyboard { mAltCodeKeysWhileTyping = Collections.unmodifiableList(params.mAltCodeKeysWhileTyping); mIconsSet = params.mIconsSet; - mProximityInfo = new ProximityInfo(params.mId.mLocale.toString(), - params.GRID_WIDTH, params.GRID_HEIGHT, mOccupiedWidth, mOccupiedHeight, - mMostCommonKeyWidth, mMostCommonKeyHeight, mSortedKeys, - params.mTouchPositionCorrection); + mProximityInfo = new ProximityInfo(params.GRID_WIDTH, params.GRID_HEIGHT, + mOccupiedWidth, mOccupiedHeight, mMostCommonKeyWidth, mMostCommonKeyHeight, + mSortedKeys, params.mTouchPositionCorrection); mProximityCharsCorrectionEnabled = params.mProximityCharsCorrectionEnabled; } diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index c19cd671a..9c5abcd71 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -52,18 +52,11 @@ public class ProximityInfo { private final int mMostCommonKeyHeight; private final List mSortedKeys; private final List[] mGridNeighbors; - private final String mLocaleStr; @SuppressWarnings("unchecked") - ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight, - final int minWidth, final int height, final int mostCommonKeyWidth, - final int mostCommonKeyHeight, final List sortedKeys, + ProximityInfo(final int gridWidth, final int gridHeight, final int minWidth, final int height, + final int mostCommonKeyWidth, final int mostCommonKeyHeight, final List sortedKeys, final TouchPositionCorrection touchPositionCorrection) { - if (TextUtils.isEmpty(localeStr)) { - mLocaleStr = ""; - } else { - mLocaleStr = localeStr; - } mGridWidth = gridWidth; mGridHeight = gridHeight; mGridSize = mGridWidth * mGridHeight; @@ -89,11 +82,10 @@ public class ProximityInfo { } // TODO: Stop passing proximityCharsArray - private static native long setProximityInfoNative(String locale, - int displayWidth, int displayHeight, int gridWidth, int gridHeight, - int mostCommonKeyWidth, int mostCommonKeyHeight, int[] proximityCharsArray, - int keyCount, int[] keyXCoordinates, int[] keyYCoordinates, int[] keyWidths, - int[] keyHeights, int[] keyCharCodes, float[] sweetSpotCenterXs, + private static native long setProximityInfoNative(int displayWidth, int displayHeight, + int gridWidth, int gridHeight, int mostCommonKeyWidth, int mostCommonKeyHeight, + int[] proximityCharsArray, int keyCount, int[] keyXCoordinates, int[] keyYCoordinates, + int[] keyWidths, int[] keyHeights, int[] keyCharCodes, float[] sweetSpotCenterXs, float[] sweetSpotCenterYs, float[] sweetSpotRadii); private static native void releaseProximityInfoNative(long nativeProximityInfo); @@ -221,10 +213,10 @@ public class ProximityInfo { } // TODO: Stop passing proximityCharsArray - return setProximityInfoNative(mLocaleStr, mKeyboardMinWidth, mKeyboardHeight, - mGridWidth, mGridHeight, mMostCommonKeyWidth, mMostCommonKeyHeight, - proximityCharsArray, keyCount, keyXCoordinates, keyYCoordinates, keyWidths, - keyHeights, keyCharCodes, sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii); + return setProximityInfoNative(mKeyboardMinWidth, mKeyboardHeight, mGridWidth, mGridHeight, + mMostCommonKeyWidth, mMostCommonKeyHeight, proximityCharsArray, keyCount, + keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes, + sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii); } public long getNativeProximityInfo() { diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index f88d37ec9..80419b335 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp @@ -25,13 +25,13 @@ namespace latinime { -static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jclass clazz, jstring localeJStr, +static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jclass clazz, jint displayWidth, jint displayHeight, jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jint mostCommonkeyHeight, jintArray proximityChars, jint keyCount, jintArray keyXCoordinates, jintArray keyYCoordinates, jintArray keyWidths, jintArray keyHeights, jintArray keyCharCodes, jfloatArray sweetSpotCenterXs, jfloatArray sweetSpotCenterYs, jfloatArray sweetSpotRadii) { - ProximityInfo *proximityInfo = new ProximityInfo(env, localeJStr, displayWidth, displayHeight, + ProximityInfo *proximityInfo = new ProximityInfo(env, displayWidth, displayHeight, gridWidth, gridHeight, mostCommonkeyWidth, mostCommonkeyHeight, proximityChars, keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes, sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii); @@ -46,7 +46,7 @@ static void latinime_Keyboard_release(JNIEnv *env, jclass clazz, jlong proximity static const JNINativeMethod sMethods[] = { { const_cast("setProximityInfoNative"), - const_cast("(Ljava/lang/String;IIIIII[II[I[I[I[I[I[F[F[F)J"), + const_cast("(IIIIII[II[I[I[I[I[I[F[F[F)J"), reinterpret_cast(latinime_Keyboard_setProximityInfo) }, { diff --git a/native/jni/src/suggest/core/layout/additional_proximity_chars.cpp b/native/jni/src/suggest/core/layout/additional_proximity_chars.cpp index 34b8b37b0..8b39f7da5 100644 --- a/native/jni/src/suggest/core/layout/additional_proximity_chars.cpp +++ b/native/jni/src/suggest/core/layout/additional_proximity_chars.cpp @@ -19,7 +19,7 @@ namespace latinime { // TODO: Stop using hardcoded additional proximity characters. // TODO: Have proximity character informations in each language's binary dictionary. -const char *AdditionalProximityChars::LOCALE_EN_US = "en"; +const int AdditionalProximityChars::LOCALE_EN_US[LOCALE_EN_US_SIZE] = { 'e', 'n' }; const int AdditionalProximityChars::EN_US_ADDITIONAL_A[EN_US_ADDITIONAL_A_SIZE] = { 'e', 'i', 'o', 'u' diff --git a/native/jni/src/suggest/core/layout/additional_proximity_chars.h b/native/jni/src/suggest/core/layout/additional_proximity_chars.h index a88fd6cea..2260be9bd 100644 --- a/native/jni/src/suggest/core/layout/additional_proximity_chars.h +++ b/native/jni/src/suggest/core/layout/additional_proximity_chars.h @@ -18,6 +18,7 @@ #define LATINIME_ADDITIONAL_PROXIMITY_CHARS_H #include +#include #include "defines.h" @@ -26,7 +27,8 @@ namespace latinime { class AdditionalProximityChars { private: DISALLOW_IMPLICIT_CONSTRUCTORS(AdditionalProximityChars); - static const char *LOCALE_EN_US; + static const int LOCALE_EN_US_SIZE = 2; + static const int LOCALE_EN_US[LOCALE_EN_US_SIZE]; static const int EN_US_ADDITIONAL_A_SIZE = 4; static const int EN_US_ADDITIONAL_A[]; static const int EN_US_ADDITIONAL_E_SIZE = 4; @@ -38,15 +40,22 @@ class AdditionalProximityChars { static const int EN_US_ADDITIONAL_U_SIZE = 4; static const int EN_US_ADDITIONAL_U[]; - AK_FORCE_INLINE static bool isEnLocale(const char *localeStr) { - const size_t LOCALE_EN_US_SIZE = strlen(LOCALE_EN_US); - return localeStr && strlen(localeStr) >= LOCALE_EN_US_SIZE - && strncmp(localeStr, LOCALE_EN_US, LOCALE_EN_US_SIZE) == 0; + AK_FORCE_INLINE static bool isEnLocale(const std::vector *locale) { + const int NCHARS = NELEMS(LOCALE_EN_US); + if (locale->size() < NCHARS) { + return false; + } + for (int i = 0; i < NCHARS; ++i) { + if ((*locale)[i] != LOCALE_EN_US[i]) { + return false; + } + } + return true; } public: - static int getAdditionalCharsSize(const char *const localeStr, const int c) { - if (!isEnLocale(localeStr)) { + static int getAdditionalCharsSize(const std::vector *locale, const int c) { + if (!isEnLocale(locale)) { return 0; } switch (c) { @@ -65,8 +74,8 @@ class AdditionalProximityChars { } } - static const int *getAdditionalChars(const char *const localeStr, const int c) { - if (!isEnLocale(localeStr)) { + static const int *getAdditionalChars(const std::vector *locale, const int c) { + if (!isEnLocale(locale)) { return 0; } switch (c) { diff --git a/native/jni/src/suggest/core/layout/proximity_info.cpp b/native/jni/src/suggest/core/layout/proximity_info.cpp index 4c75a188e..933a5e145 100644 --- a/native/jni/src/suggest/core/layout/proximity_info.cpp +++ b/native/jni/src/suggest/core/layout/proximity_info.cpp @@ -49,13 +49,13 @@ static AK_FORCE_INLINE void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloa } } -ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, - const int keyboardWidth, const int keyboardHeight, const int gridWidth, - const int gridHeight, const int mostCommonKeyWidth, const int mostCommonKeyHeight, - const jintArray proximityChars, const int keyCount, const jintArray keyXCoordinates, - const jintArray keyYCoordinates, const jintArray keyWidths, const jintArray keyHeights, - const jintArray keyCharCodes, const jfloatArray sweetSpotCenterXs, - const jfloatArray sweetSpotCenterYs, const jfloatArray sweetSpotRadii) +ProximityInfo::ProximityInfo(JNIEnv *env, const int keyboardWidth, const int keyboardHeight, + const int gridWidth, const int gridHeight, const int mostCommonKeyWidth, + const int mostCommonKeyHeight, const jintArray proximityChars, const int keyCount, + const jintArray keyXCoordinates, const jintArray keyYCoordinates, + const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes, + const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs, + const jfloatArray sweetSpotRadii) : GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), MOST_COMMON_KEY_WIDTH(mostCommonKeyWidth), MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth), NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE(1.0f + @@ -82,13 +82,6 @@ ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, if (DEBUG_PROXIMITY_INFO) { AKLOGI("Create proximity info array %d", proximityCharsLength); } - const jsize localeCStrUtf8Length = env->GetStringUTFLength(localeJStr); - if (localeCStrUtf8Length >= MAX_LOCALE_STRING_LENGTH) { - AKLOGI("Locale string length too long: length=%d", localeCStrUtf8Length); - ASSERT(false); - } - memset(mLocaleStr, 0, sizeof(mLocaleStr)); - env->GetStringUTFRegion(localeJStr, 0, env->GetStringLength(localeJStr), mLocaleStr); safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityCharsLength, mProximityCharsArray); safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates); diff --git a/native/jni/src/suggest/core/layout/proximity_info.h b/native/jni/src/suggest/core/layout/proximity_info.h index d4e453736..f7c907697 100644 --- a/native/jni/src/suggest/core/layout/proximity_info.h +++ b/native/jni/src/suggest/core/layout/proximity_info.h @@ -18,6 +18,7 @@ #define LATINIME_PROXIMITY_INFO_H #include +#include #include "defines.h" #include "jni.h" @@ -27,9 +28,9 @@ namespace latinime { class ProximityInfo { public: - ProximityInfo(JNIEnv *env, const jstring localeJStr, - const int keyboardWidth, const int keyboardHeight, const int gridWidth, - const int gridHeight, const int mostCommonKeyWidth, const int mostCommonKeyHeight, + ProximityInfo(JNIEnv *env, const int keyboardWidth, const int keyboardHeight, + const int gridWidth, const int gridHeight, + const int mostCommonKeyWidth, const int mostCommonKeyHeight, const jintArray proximityChars, const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates, const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes, const jfloatArray sweetSpotCenterXs, @@ -71,11 +72,11 @@ class ProximityInfo { AK_FORCE_INLINE void initializeProximities(const int *const inputCodes, const int *const inputXCoordinates, const int *const inputYCoordinates, - const int inputSize, int *allInputCodes) const { + const int inputSize, int *allInputCodes, const std::vector *locale) const { ProximityInfoUtils::initializeProximities(inputCodes, inputXCoordinates, inputYCoordinates, inputSize, mKeyXCoordinates, mKeyYCoordinates, mKeyWidths, mKeyHeights, mProximityCharsArray, CELL_HEIGHT, CELL_WIDTH, GRID_WIDTH, MOST_COMMON_KEY_WIDTH, - KEY_COUNT, mLocaleStr, &mLowerCodePointToKeyMap, allInputCodes); + KEY_COUNT, locale, &mLowerCodePointToKeyMap, allInputCodes); } AK_FORCE_INLINE int getKeyIndexOf(const int c) const { @@ -103,9 +104,6 @@ class ProximityInfo { const int KEYBOARD_HEIGHT; const float KEYBOARD_HYPOTENUSE; const bool HAS_TOUCH_POSITION_CORRECTION_DATA; - // Assuming locale strings such as en_US, sr-Latn etc. - static const int MAX_LOCALE_STRING_LENGTH = 10; - char mLocaleStr[MAX_LOCALE_STRING_LENGTH]; int *mProximityCharsArray; int mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; int mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; diff --git a/native/jni/src/suggest/core/layout/proximity_info_state.cpp b/native/jni/src/suggest/core/layout/proximity_info_state.cpp index 91469e26d..d43a0026a 100644 --- a/native/jni/src/suggest/core/layout/proximity_info_state.cpp +++ b/native/jni/src/suggest/core/layout/proximity_info_state.cpp @@ -42,7 +42,7 @@ int ProximityInfoState::getPrimaryOriginalCodePointAt(const int index) const { void ProximityInfoState::initInputParams(const int pointerId, const float maxPointToKeyLength, const ProximityInfo *proximityInfo, const int *const inputCodes, const int inputSize, const int *const xCoordinates, const int *const yCoordinates, const int *const times, - const int *const pointerIds, const bool isGeometric) { + const int *const pointerIds, const bool isGeometric, const std::vector *locale) { ASSERT(isGeometric || (inputSize < MAX_WORD_LENGTH)); mIsContinuousSuggestionPossible = (mHasBeenUpdatedByGeometricInput != isGeometric) ? false : ProximityInfoStateUtils::checkAndReturnIsContinuousSuggestionPossible( @@ -66,7 +66,7 @@ void ProximityInfoState::initInputParams(const int pointerId, const float maxPoi if (!isGeometric && pointerId == 0) { mProximityInfo->initializeProximities(inputCodes, xCoordinates, yCoordinates, - inputSize, mInputProximities); + inputSize, mInputProximities, locale); } /////////////////////// diff --git a/native/jni/src/suggest/core/layout/proximity_info_state.h b/native/jni/src/suggest/core/layout/proximity_info_state.h index e6180fe17..a2d663544 100644 --- a/native/jni/src/suggest/core/layout/proximity_info_state.h +++ b/native/jni/src/suggest/core/layout/proximity_info_state.h @@ -37,7 +37,8 @@ class ProximityInfoState { void initInputParams(const int pointerId, const float maxPointToKeyLength, const ProximityInfo *proximityInfo, const int *const inputCodes, const int inputSize, const int *xCoordinates, const int *yCoordinates, - const int *const times, const int *const pointerIds, const bool isGeometric); + const int *const times, const int *const pointerIds, const bool isGeometric, + const std::vector *locale); ///////////////////////////////////////// // Defined here // diff --git a/native/jni/src/suggest/core/layout/proximity_info_utils.h b/native/jni/src/suggest/core/layout/proximity_info_utils.h index 178aada2d..79d0615b8 100644 --- a/native/jni/src/suggest/core/layout/proximity_info_utils.h +++ b/native/jni/src/suggest/core/layout/proximity_info_utils.h @@ -19,6 +19,7 @@ #include #include +#include #include "defines.h" #include "suggest/core/layout/additional_proximity_chars.h" @@ -51,7 +52,7 @@ class ProximityInfoUtils { const int *const keyYCoordinates, const int *const keyWidths, const int *keyHeights, const int *const proximityCharsArray, const int cellHeight, const int cellWidth, const int gridWidth, const int mostCommonKeyWidth, const int keyCount, - const char *const localeStr, + const std::vector *locale, const std::unordered_map *const codeToKeyMap, int *inputProximities) { // Initialize // - mInputCodes @@ -64,7 +65,7 @@ class ProximityInfoUtils { int *proximities = &inputProximities[i * MAX_PROXIMITY_CHARS_SIZE]; calculateProximities(keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, proximityCharsArray, cellHeight, cellWidth, gridWidth, mostCommonKeyWidth, - keyCount, x, y, primaryKey, localeStr, codeToKeyMap, proximities); + keyCount, x, y, primaryKey, locale, codeToKeyMap, proximities); } if (DEBUG_PROXIMITY_CHARS) { @@ -143,7 +144,7 @@ class ProximityInfoUtils { const int *const keyYCoordinates, const int *const keyWidths, const int *keyHeights, const int *const proximityCharsArray, const int cellHeight, const int cellWidth, const int gridWidth, const int mostCommonKeyWidth, const int keyCount, - const int x, const int y, const int primaryKey, const char *const localeStr, + const int x, const int y, const int primaryKey, const std::vector *locale, const std::unordered_map *const codeToKeyMap, int *proximities) { const int mostCommonKeyWidthSquare = mostCommonKeyWidth * mostCommonKeyWidth; int insertPos = 0; @@ -177,7 +178,7 @@ class ProximityInfoUtils { } } const int additionalProximitySize = - AdditionalProximityChars::getAdditionalCharsSize(localeStr, primaryKey); + AdditionalProximityChars::getAdditionalCharsSize(locale, primaryKey); if (additionalProximitySize > 0) { proximities[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE; if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { @@ -188,7 +189,7 @@ class ProximityInfoUtils { } const int *additionalProximityChars = - AdditionalProximityChars::getAdditionalChars(localeStr, primaryKey); + AdditionalProximityChars::getAdditionalChars(locale, primaryKey); for (int j = 0; j < additionalProximitySize; ++j) { const int ac = additionalProximityChars[j]; int k = 0; diff --git a/native/jni/src/suggest/core/session/dic_traverse_session.cpp b/native/jni/src/suggest/core/session/dic_traverse_session.cpp index 4d7505a55..b4d01d0f0 100644 --- a/native/jni/src/suggest/core/session/dic_traverse_session.cpp +++ b/native/jni/src/suggest/core/session/dic_traverse_session.cpp @@ -69,8 +69,12 @@ void DicTraverseSession::initializeProximityInfoStates(const int *const inputCod for (int i = 0; i < maxPointerCount; ++i) { mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(), inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds, - maxPointerCount == MAX_POINTER_COUNT_G - /* TODO: this is a hack. fix proximity info state */); + // Right now the line below is trying to figure out whether this is a gesture by + // looking at the pointer count and assuming whatever is above the cutoff is + // a gesture and whatever is below is type. This is hacky and incorrect, we + // should pass the correct information instead. + maxPointerCount == MAX_POINTER_COUNT_G, + getDictionaryStructurePolicy()->getHeaderStructurePolicy()->getLocale()); mInputSize += mProximityInfoStates[i].size(); } }