Extend JNI interface to pass the key coordinate information

Change-Id: Iec0d1415d6abba43b775a014516b961b167597b7
main
Yusuke Nojima 2011-09-21 12:02:47 +09:00
parent a35ea4cba8
commit 0e1f656c1b
5 changed files with 166 additions and 9 deletions

View File

@ -65,7 +65,8 @@ public class ProximityInfo {
spellCheckerProximityInfo.mNativeProximityInfo = spellCheckerProximityInfo.mNativeProximityInfo =
spellCheckerProximityInfo.setProximityInfoNative( spellCheckerProximityInfo.setProximityInfoNative(
SpellCheckerProximityInfo.ROW_SIZE, SpellCheckerProximityInfo.ROW_SIZE,
480, 300, 10, 3, SpellCheckerProximityInfo.PROXIMITY); 480, 300, 10, 3, SpellCheckerProximityInfo.PROXIMITY,
0, null, null, null, null, null);
return spellCheckerProximityInfo; return spellCheckerProximityInfo;
} }
@ -74,7 +75,9 @@ public class ProximityInfo {
Utils.loadNativeLibrary(); Utils.loadNativeLibrary();
} }
private native int setProximityInfoNative(int maxProximityCharsSize, int displayWidth, private native int setProximityInfoNative(int maxProximityCharsSize, int displayWidth,
int displayHeight, int gridWidth, int gridHeight, int[] proximityCharsArray); int displayHeight, int gridWidth, int gridHeight, int[] proximityCharsArray,
int keyCount, int[] keyXCoordinates, int[] keyYCoordinates,
int[] keyWidths, int[] keyHeights, int[] keyCharCodes);
private native void releaseProximityInfoNative(int nativeProximityInfo); private native void releaseProximityInfoNative(int nativeProximityInfo);
private final void setProximityInfo(int[][] gridNeighborKeyIndexes, int keyboardWidth, private final void setProximityInfo(int[][] gridNeighborKeyIndexes, int keyboardWidth,
@ -88,8 +91,23 @@ public class ProximityInfo {
keys.get(gridNeighborKeyIndexes[i][j]).mCode; keys.get(gridNeighborKeyIndexes[i][j]).mCode;
} }
} }
final int keyCount = keys.size();
int[] keyXCoordinates = new int[keyCount];
int[] keyYCoordinates = new int[keyCount];
int[] keyWidths = new int[keyCount];
int[] keyHeights = new int[keyCount];
int[] keyCharCodes = new int[keyCount];
for (int i = 0; i < keyCount; ++i) {
final Key key = keys.get(i);
keyXCoordinates[i] = key.mX;
keyYCoordinates[i] = key.mY;
keyWidths[i] = key.mWidth;
keyHeights[i] = key.mHeight;
keyCharCodes[i] = key.mCode;
}
mNativeProximityInfo = setProximityInfoNative(MAX_PROXIMITY_CHARS_SIZE, mNativeProximityInfo = setProximityInfoNative(MAX_PROXIMITY_CHARS_SIZE,
keyboardWidth, keyboardHeight, mGridWidth, mGridHeight, proximityCharsArray); keyboardWidth, keyboardHeight, mGridWidth, mGridHeight, proximityCharsArray,
keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes);
} }
public int getNativeProximityInfo() { public int getNativeProximityInfo() {

View File

@ -30,10 +30,24 @@ namespace latinime {
static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth, jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth,
jint gridHeight, jintArray proximityCharsArray) { jint gridHeight, jintArray proximityCharsArray, jint keyCount,
jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, jintArray keyWidthArray,
jintArray keyHeightArray, jintArray keyCharCodeArray) {
jint* proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL); jint* proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL);
jint* keyXCoordinates = env->GetIntArrayElements(keyXCoordinateArray, NULL);
jint* keyYCoordinates = env->GetIntArrayElements(keyYCoordinateArray, NULL);
jint* keyWidths = env->GetIntArrayElements(keyWidthArray, NULL);
jint* keyHeights = env->GetIntArrayElements(keyHeightArray, NULL);
jint* keyCharCodes = env->GetIntArrayElements(keyCharCodeArray, NULL);
ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth, ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
displayHeight, gridWidth, gridHeight, (const uint32_t *)proximityChars); displayHeight, gridWidth, gridHeight, (const uint32_t *)proximityChars,
keyCount, (const int32_t *)keyXCoordinates, (const int32_t *)keyYCoordinates,
(const int32_t *)keyWidths, (const int32_t *)keyHeights, (const int32_t *)keyCharCodes);
env->ReleaseIntArrayElements(keyCharCodeArray, keyCharCodes, 0);
env->ReleaseIntArrayElements(keyHeightArray, keyHeights, 0);
env->ReleaseIntArrayElements(keyWidthArray, keyWidths, 0);
env->ReleaseIntArrayElements(keyYCoordinateArray, keyYCoordinates, 0);
env->ReleaseIntArrayElements(keyXCoordinateArray, keyXCoordinates, 0);
env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0); env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
return (jint)proximityInfo; return (jint)proximityInfo;
} }
@ -45,7 +59,7 @@ static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximit
} }
static JNINativeMethod sKeyboardMethods[] = { static JNINativeMethod sKeyboardMethods[] = {
{"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo}, {"setProximityInfoNative", "(IIIII[II[I[I[I[I[I)I", (void*)latinime_Keyboard_setProximityInfo},
{"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release} {"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release}
}; };

View File

@ -0,0 +1,61 @@
/*
**
** Copyright 2011, 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.
*/
#ifndef LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H
#define LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H
#define OUTER_SWEET_SPOT_RADIUS_RATIO 1.3
static const char* TOUCH_POSITION_CORRECTION_GROUPS[] = {
"qwertyuiop",
"a",
"sdfghjk",
"l",
"zxcvbnm",
};
// (center X) / (key width)
static const float RELATIVE_TOUCH_CENTER_X[] = {
0, // qwertyuiop
-0.26871, // a
0, // sdfghjk
0.028050, // l
0, // zxcvbnm
};
// (center Y) / (key height)
static const float RELATIVE_TOUCH_CENTER_Y[] = {
0.192088, // qwertyuiop
0.214100, // a
0.216640, // sdfghjk
0.233288, // l
0.286598, // zxcvbnm
};
// (sweet spot radius) / ((key width) + (key height))
static const float SWEET_SPOT_RADIUS[] = {
0.148955, // qwertyuiop
0.185437, // a
0.145522, // sdfghjk
0.156882, // l
0.144211, // zxcvbnm
};
#define CORRECTION_GROUP_COUNT \
((int)(sizeof(TOUCH_POSITION_CORRECTION_GROUPS) / sizeof(TOUCH_POSITION_CORRECTION_GROUPS[0])))
#endif // LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H

View File

@ -14,11 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define LOG_TAG "LatinIME: proximity_info.cpp" #define LOG_TAG "LatinIME: proximity_info.cpp"
#include "defines_touch_position_correction.h"
#include "dictionary.h" #include "dictionary.h"
#include "proximity_info.h" #include "proximity_info.h"
@ -26,17 +28,59 @@ namespace latinime {
ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
const int keyboardHeight, const int gridWidth, const int gridHeight, const int keyboardHeight, const int gridWidth, const int gridHeight,
const uint32_t *proximityCharsArray) const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
const int32_t *keyCharCodes)
: MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth), CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight) { CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)) {
const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
mProximityCharsArray = new uint32_t[len]; mProximityCharsArray = new uint32_t[len];
if (DEBUG_PROXIMITY_INFO) { if (DEBUG_PROXIMITY_INFO) {
LOGI("Create proximity info array %d", len); LOGI("Create proximity info array %d", len);
} }
memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0])); memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
if (KEY_COUNT > 0) {
if (DEBUG_PROXIMITY_INFO) {
LOGI("Create key coordinate array %d", keyCount);
}
memcpy(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
memcpy(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
memcpy(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
memcpy(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
memcpy(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
}
initializeCodeToGroup();
initializeCodeToKeyIndex();
}
// Build the reversed look up table from the char code to the index in its group.
// see TOUCH_POSITION_CORRECTION_GROUPS
void ProximityInfo::initializeCodeToGroup() {
memset(mCodeToGroup, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToGroup[0]));
for (int i = 0; i < CORRECTION_GROUP_COUNT; ++i) {
const char *group = TOUCH_POSITION_CORRECTION_GROUPS[i];
for (int j = 0; group[j]; ++j) {
const int code = group[j];
if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
mCodeToGroup[code] = i;
}
}
}
// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
void ProximityInfo::initializeCodeToKeyIndex() {
memset(mCodeToKeyIndex, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
for (int i = 0; i < KEY_COUNT; ++i) {
const int code = mKeyCharCodes[i];
if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
mCodeToKeyIndex[code] = i;
}
} }
ProximityInfo::~ProximityInfo() { ProximityInfo::~ProximityInfo() {
@ -162,4 +206,7 @@ bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
return true; return true;
} }
const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
const int ProximityInfo::MAX_GROUPED_CHAR_CODE;
} // namespace latinime } // namespace latinime

View File

@ -35,7 +35,9 @@ public:
ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
const int keybaordHeight, const int gridWidth, const int gridHeight, const int keybaordHeight, const int gridWidth, const int gridHeight,
const uint32_t *proximityCharsArray); const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
const int32_t *keyCharCodes);
~ProximityInfo(); ~ProximityInfo();
bool hasSpaceProximity(const int x, const int y) const; bool hasSpaceProximity(const int x, const int y) const;
void setInputParams(const int* inputCodes, const int inputLength); void setInputParams(const int* inputCodes, const int inputLength);
@ -51,7 +53,14 @@ public:
} }
private: private:
// The max number of the keys in one keyboard layout
static const int MAX_KEY_COUNT_IN_A_KEYBOARD = 64;
// The upper limit of the char code in TOUCH_POSITION_CORRECTION_GROUP
static const int MAX_GROUPED_CHAR_CODE = 127;
int getStartIndexFromCoordinates(const int x, const int y) const; int getStartIndexFromCoordinates(const int x, const int y) const;
void initializeCodeToGroup();
void initializeCodeToKeyIndex();
const int MAX_PROXIMITY_CHARS_SIZE; const int MAX_PROXIMITY_CHARS_SIZE;
const int KEYBOARD_WIDTH; const int KEYBOARD_WIDTH;
const int KEYBOARD_HEIGHT; const int KEYBOARD_HEIGHT;
@ -59,10 +68,18 @@ private:
const int GRID_HEIGHT; const int GRID_HEIGHT;
const int CELL_WIDTH; const int CELL_WIDTH;
const int CELL_HEIGHT; const int CELL_HEIGHT;
const int KEY_COUNT;
const int *mInputCodes; const int *mInputCodes;
uint32_t *mProximityCharsArray; uint32_t *mProximityCharsArray;
int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
int32_t mKeyCharCodes[MAX_KEY_COUNT_IN_A_KEYBOARD];
int mInputLength; int mInputLength;
unsigned short mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL]; unsigned short mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL];
int mCodeToGroup[MAX_GROUPED_CHAR_CODE + 1];
int mCodeToKeyIndex[MAX_GROUPED_CHAR_CODE + 1];
}; };
} // namespace latinime } // namespace latinime