aboutsummaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
Diffstat (limited to 'native')
-rw-r--r--native/jni/Android.mk1
-rw-r--r--native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp51
-rw-r--r--native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp49
-rw-r--r--native/jni/com_android_inputmethod_latin_DicTraverseSession.h46
-rw-r--r--native/jni/jni_common.cpp10
-rw-r--r--native/jni/jni_common.h27
-rw-r--r--native/jni/src/proximity_info.cpp58
-rw-r--r--native/jni/src/proximity_info.h13
8 files changed, 156 insertions, 99 deletions
diff --git a/native/jni/Android.mk b/native/jni/Android.mk
index 9f9958377..1b7301b19 100644
--- a/native/jni/Android.mk
+++ b/native/jni/Android.mk
@@ -36,6 +36,7 @@ LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-function
LATIN_IME_JNI_SRC_FILES := \
com_android_inputmethod_keyboard_ProximityInfo.cpp \
com_android_inputmethod_latin_BinaryDictionary.cpp \
+ com_android_inputmethod_latin_DicTraverseSession.cpp \
com_android_inputmethod_latin_NativeUtils.cpp \
jni_common.cpp
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
index 74390ccdf..a01ac3780 100644
--- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
@@ -16,8 +16,6 @@
#define LOG_TAG "LatinIME: jni: ProximityInfo"
-#include <string>
-
#include "com_android_inputmethod_keyboard_ProximityInfo.h"
#include "jni.h"
#include "jni_common.h"
@@ -26,45 +24,22 @@
namespace latinime {
static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
- jstring localejStr, jint maxProximityCharsSize, jint displayWidth, jint displayHeight,
- jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityCharsArray,
- jint keyCount, jintArray keyXCoordinateArray, jintArray keyYCoordinateArray,
- jintArray keyWidthArray, jintArray keyHeightArray, jintArray keyCharCodeArray,
- jfloatArray sweetSpotCenterXArray, jfloatArray sweetSpotCenterYArray,
- jfloatArray sweetSpotRadiusArray) {
- const char *localeStrPtr = env->GetStringUTFChars(localejStr, 0);
- const std::string localeStr(localeStrPtr);
- jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, 0);
- jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray);
- jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray);
- jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray);
- jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray);
- jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray);
- jfloat *sweetSpotCenterXs = safeGetFloatArrayElements(env, sweetSpotCenterXArray);
- jfloat *sweetSpotCenterYs = safeGetFloatArrayElements(env, sweetSpotCenterYArray);
- jfloat *sweetSpotRadii = safeGetFloatArrayElements(env, sweetSpotRadiusArray);
- ProximityInfo *proximityInfo = new ProximityInfo(
- localeStr, maxProximityCharsSize, displayWidth, displayHeight, gridWidth, gridHeight,
- mostCommonkeyWidth, (const int32_t*)proximityChars, keyCount,
- (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates,
- (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes,
- (const float*)sweetSpotCenterXs, (const float*)sweetSpotCenterYs,
- (const float*)sweetSpotRadii);
- safeReleaseFloatArrayElements(env, sweetSpotRadiusArray, sweetSpotRadii);
- safeReleaseFloatArrayElements(env, sweetSpotCenterYArray, sweetSpotCenterYs);
- safeReleaseFloatArrayElements(env, sweetSpotCenterXArray, sweetSpotCenterXs);
- safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes);
- safeReleaseIntArrayElements(env, keyHeightArray, keyHeights);
- safeReleaseIntArrayElements(env, keyWidthArray, keyWidths);
- safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates);
- safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates);
- env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
- env->ReleaseStringUTFChars(localejStr, localeStrPtr);
- return (jlong)proximityInfo;
+ jstring localeJStr, jint maxProximityCharsSize, jint displayWidth, jint displayHeight,
+ jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityChars,
+ jint keyCount, jintArray keyXCoordinates, jintArray keyYCoordinates,
+ jintArray keyWidths, jintArray keyHeights, jintArray keyCharCodes,
+ jfloatArray sweetSpotCenterXs, jfloatArray sweetSpotCenterYs, jfloatArray sweetSpotRadii) {
+ const char *localeCStr = env->GetStringUTFChars(localeJStr, 0);
+ ProximityInfo *proximityInfo = new ProximityInfo(env, localeCStr, maxProximityCharsSize,
+ displayWidth, displayHeight, gridWidth, gridHeight, mostCommonkeyWidth, proximityChars,
+ keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes,
+ sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
+ env->ReleaseStringUTFChars(localeJStr, localeCStr);
+ return reinterpret_cast<jlong>(proximityInfo);
}
static void latinime_Keyboard_release(JNIEnv *env, jobject object, jlong proximityInfo) {
- ProximityInfo *pi = (ProximityInfo*)proximityInfo;
+ ProximityInfo *pi = reinterpret_cast<ProximityInfo*>(proximityInfo);
if (!pi) return;
delete pi;
}
diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp
new file mode 100644
index 000000000..8ee0450e4
--- /dev/null
+++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2012, 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.
+ */
+
+#define LOG_TAG "LatinIME: jni: Session"
+
+#include "com_android_inputmethod_latin_DicTraverseSession.h"
+#include "jni.h"
+#include "jni_common.h"
+
+namespace latinime {
+void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)() = 0;
+void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0;
+
+static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object,
+ jstring localejStr) {
+ void *session = DicTraverseWrapper::getDicTraverseSession();
+ return reinterpret_cast<jlong>(session);
+}
+
+static void latinime_DicTraverseSession_release(JNIEnv *env, jobject object, jlong session) {
+ void *pi = reinterpret_cast<void*>(session);
+ if (!pi) return;
+ DicTraverseWrapper::releaseDicTraverseSession(pi);
+}
+
+static JNINativeMethod sMethods[] = {
+ {"setDicTraverseSessionNative", "(Ljava/lang/String;)J", (void*)latinime_setDicTraverseSession},
+ {"releaseDicTraverseSessionNative", "(J)V", (void*)latinime_DicTraverseSession_release}
+};
+
+int register_DicTraverseSession(JNIEnv *env) {
+ const char *const kClassPathName = "com/android/inputmethod/latin/DicTraverseSession";
+ return registerNativeMethods(env, kClassPathName, sMethods,
+ sizeof(sMethods) / sizeof(sMethods[0]));
+}
+} // namespace latinime
diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.h b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h
new file mode 100644
index 000000000..5238fd0af
--- /dev/null
+++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2012, 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 _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H
+#define _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H
+
+#include "defines.h"
+#include "jni.h"
+
+namespace latinime {
+
+// TODO: Remove
+class DicTraverseWrapper {
+ public:
+ static void *getDicTraverseSession() {
+ if (sDicTraverseSessionFactoryMethod) {
+ return sDicTraverseSessionFactoryMethod();
+ }
+ return 0;
+ }
+ static void releaseDicTraverseSession(void *session) {
+ if (sDicTraverseSessionReleaseMethod) {
+ sDicTraverseSessionReleaseMethod(session);
+ }
+ }
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseWrapper);
+ static void *(*sDicTraverseSessionFactoryMethod)();
+ static void (*sDicTraverseSessionReleaseMethod)(void *);
+};
+int register_DicTraverseSession(JNIEnv *env);
+} // namespace latinime
+#endif // _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H
diff --git a/native/jni/jni_common.cpp b/native/jni/jni_common.cpp
index 105a4dc4c..795262aa4 100644
--- a/native/jni/jni_common.cpp
+++ b/native/jni/jni_common.cpp
@@ -16,15 +16,16 @@
#define LOG_TAG "LatinIME: jni"
+#include <cassert>
+
#include "com_android_inputmethod_keyboard_ProximityInfo.h"
#include "com_android_inputmethod_latin_BinaryDictionary.h"
+#include "com_android_inputmethod_latin_DicTraverseSession.h"
#include "com_android_inputmethod_latin_NativeUtils.h"
#include "defines.h"
#include "jni.h"
#include "jni_common.h"
-#include <cassert>
-
using namespace latinime;
/*
@@ -45,6 +46,11 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
goto bail;
}
+ if (!register_DicTraverseSession(env)) {
+ AKLOGE("ERROR: DicTraverseSession native registration failed");
+ goto bail;
+ }
+
if (!register_ProximityInfo(env)) {
AKLOGE("ERROR: ProximityInfo native registration failed");
goto bail;
diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h
index 658ff18b9..993f97e80 100644
--- a/native/jni/jni_common.h
+++ b/native/jni/jni_common.h
@@ -24,32 +24,5 @@ namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
int numMethods);
-inline jint *safeGetIntArrayElements(JNIEnv *env, jintArray jArray) {
- if (jArray) {
- return env->GetIntArrayElements(jArray, 0);
- } else {
- return 0;
- }
-}
-
-inline jfloat *safeGetFloatArrayElements(JNIEnv *env, jfloatArray jArray) {
- if (jArray) {
- return env->GetFloatArrayElements(jArray, 0);
- } else {
- return 0;
- }
-}
-
-inline void safeReleaseIntArrayElements(JNIEnv *env, jintArray jArray, jint *cArray) {
- if (jArray) {
- env->ReleaseIntArrayElements(jArray, cArray, 0);
- }
-}
-
-inline void safeReleaseFloatArrayElements(JNIEnv *env, jfloatArray jArray, jfloat *cArray) {
- if (jArray) {
- env->ReleaseFloatArrayElements(jArray, cArray, 0);
- }
-}
} // namespace latinime
#endif // LATINIME_JNI_COMMON_H
diff --git a/native/jni/src/proximity_info.cpp b/native/jni/src/proximity_info.cpp
index cee408d46..7bae41362 100644
--- a/native/jni/src/proximity_info.cpp
+++ b/native/jni/src/proximity_info.cpp
@@ -24,25 +24,36 @@
#include "additional_proximity_chars.h"
#include "char_utils.h"
#include "defines.h"
+#include "jni.h"
#include "proximity_info.h"
namespace latinime {
-inline void copyOrFillZero(void *to, const void *from, size_t size) {
- if (from) {
- memcpy(to, from, size);
- } else {
- memset(to, 0, size);
+static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray, jsize len,
+ jint *buffer) {
+ if (jArray && buffer) {
+ env->GetIntArrayRegion(jArray, 0, len, buffer);
+ } else if (buffer) {
+ memset(buffer, 0, len);
}
}
-ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
+static inline void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloatArray jArray, jsize len,
+ jfloat *buffer) {
+ if (jArray && buffer) {
+ env->GetFloatArrayRegion(jArray, 0, len, buffer);
+ } else if (buffer) {
+ memset(buffer, 0, len);
+ }
+}
+
+ProximityInfo::ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize,
const int keyboardWidth, const int keyboardHeight, const int gridWidth,
- const int gridHeight, const int mostCommonKeyWidth,
- const int32_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, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
- const float *sweetSpotRadii)
+ const int gridHeight, const int mostCommonKeyWidth, 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)
: MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
@@ -52,26 +63,21 @@ ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximity
HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
&& keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
&& sweetSpotCenterYs && sweetSpotRadii),
- mLocaleStr(localeStr) {
+ mLocaleStr(localeCStr) {
const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
if (DEBUG_PROXIMITY_INFO) {
AKLOGI("Create proximity info array %d", proximityGridLength);
}
mProximityCharsArray = new int32_t[proximityGridLength];
- memcpy(mProximityCharsArray, proximityCharsArray,
- proximityGridLength * sizeof(mProximityCharsArray[0]));
-
- copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
- copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
- copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
- copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
- copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
- copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
- KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
- copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
- KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
- copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
-
+ safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityGridLength, mProximityCharsArray);
+ safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates);
+ safeGetOrFillZeroIntArrayRegion(env, keyYCoordinates, KEY_COUNT, mKeyYCoordinates);
+ safeGetOrFillZeroIntArrayRegion(env, keyWidths, KEY_COUNT, mKeyWidths);
+ safeGetOrFillZeroIntArrayRegion(env, keyHeights, KEY_COUNT, mKeyHeights);
+ safeGetOrFillZeroIntArrayRegion(env, keyCharCodes, KEY_COUNT, mKeyCharCodes);
+ safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterXs, KEY_COUNT, mSweetSpotCenterXs);
+ safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterYs, KEY_COUNT, mSweetSpotCenterYs);
+ safeGetOrFillZeroFloatArrayRegion(env, sweetSpotRadii, KEY_COUNT, mSweetSpotRadii);
initializeCodeToKeyIndex();
}
diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h
index abd07dd3e..5dd378d21 100644
--- a/native/jni/src/proximity_info.h
+++ b/native/jni/src/proximity_info.h
@@ -21,6 +21,7 @@
#include <string>
#include "defines.h"
+#include "jni.h"
namespace latinime {
@@ -28,13 +29,13 @@ class Correction;
class ProximityInfo {
public:
- ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
+ ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize,
const int keyboardWidth, const int keyboardHeight, const int gridWidth,
- const int gridHeight, const int mostCommonkeyWidth,
- const int32_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, const float *sweetSpotCenterXs,
- const float *sweetSpotCenterYs, const float *sweetSpotRadii);
+ const int gridHeight, const int mostCommonKeyWidth, 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();
bool hasSpaceProximity(const int x, const int y) const;
int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;