Clean up in LatinIME native code
Change-Id: I0062200a0181a491690115ac0fab8d11358e2f14
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
index 3db89ed..f3e2a7e 100644
--- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
@@ -19,28 +19,15 @@
#include "com_android_inputmethod_keyboard_ProximityInfo.h"
#include "jni.h"
+#include "jni_common.h"
#include "proximity_info.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
-// ----------------------------------------------------------------------------
-
namespace latinime {
-//
-// helper function to throw an exception
-//
-static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
- if (jclass cls = env->FindClass(ex)) {
- char msg[1000];
- snprintf(msg, sizeof(msg), fmt, data);
- env->ThrowNew(cls, msg);
- env->DeleteLocalRef(cls);
- }
-}
-
static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth,
jint gridHeight, jintArray proximityCharsArray) {
@@ -57,34 +44,15 @@
delete pi;
}
-// ----------------------------------------------------------------------------
-
static JNINativeMethod sKeyboardMethods[] = {
{"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo},
{"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release}
};
-static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods,
- int numMethods) {
- jclass clazz;
-
- clazz = env->FindClass(className);
- if (clazz == NULL) {
- LOGE("Native registration unable to find class '%s'", className);
- return JNI_FALSE;
- }
- if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
- LOGE("RegisterNatives failed for '%s'", className);
- return JNI_FALSE;
- }
-
- return JNI_TRUE;
-}
-
int register_ProximityInfo(JNIEnv *env) {
const char* const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo";
return registerNativeMethods(env, kClassPathName, sKeyboardMethods,
sizeof(sKeyboardMethods) / sizeof(sKeyboardMethods[0]));
}
-}; // namespace latinime
+} // namespace latinime
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
index bdeeb8f..4a1e83b 100644
--- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
@@ -21,7 +21,9 @@
#include "jni.h"
namespace latinime {
+
int register_ProximityInfo(JNIEnv *env);
+
}
#endif // _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 4b61c14..ce874d8 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -20,6 +20,7 @@
#include "com_android_inputmethod_latin_BinaryDictionary.h"
#include "dictionary.h"
#include "jni.h"
+#include "jni_common.h"
#include "proximity_info.h"
#include <assert.h>
@@ -35,22 +36,8 @@
#include <stdlib.h>
#endif // USE_MMAP_FOR_DICTIONARY
-// ----------------------------------------------------------------------------
-
namespace latinime {
-//
-// helper function to throw an exception
-//
-static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
- if (jclass cls = env->FindClass(ex)) {
- char msg[1000];
- snprintf(msg, sizeof(msg), fmt, data);
- env->ThrowNew(cls, msg);
- env->DeleteLocalRef(cls);
- }
-}
-
static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
jstring sourceDir, jlong dictOffset, jlong dictSize,
jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
@@ -208,8 +195,6 @@
delete dictionary;
}
-// ----------------------------------------------------------------------------
-
static JNINativeMethod sMethods[] = {
{"openNative", "(Ljava/lang/String;JJIIIII)I", (void*)latinime_BinaryDictionary_open},
{"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
@@ -218,27 +203,10 @@
{"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
};
-static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods,
- int numMethods) {
- jclass clazz;
-
- clazz = env->FindClass(className);
- if (clazz == NULL) {
- LOGE("Native registration unable to find class '%s'", className);
- return JNI_FALSE;
- }
- if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
- LOGE("RegisterNatives failed for '%s'", className);
- return JNI_FALSE;
- }
-
- return JNI_TRUE;
-}
-
int register_BinaryDictionary(JNIEnv *env) {
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
return registerNativeMethods(env, kClassPathName, sMethods,
sizeof(sMethods) / sizeof(sMethods[0]));
}
-}; // namespace latinime
+} // namespace latinime
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
index f7cd81f..1b1ba7f 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
@@ -21,7 +21,9 @@
#include "jni.h"
namespace latinime {
+
int register_BinaryDictionary(JNIEnv *env);
+
}
#endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
diff --git a/native/jni/onload.cpp b/native/jni/jni_common.cpp
similarity index 73%
rename from native/jni/onload.cpp
rename to native/jni/jni_common.cpp
index f02c9a0..8643f72 100644
--- a/native/jni/onload.cpp
+++ b/native/jni/jni_common.cpp
@@ -26,11 +26,8 @@
#include <errno.h>
#include <stdio.h>
-// ----------------------------------------------------------------------------
-
using namespace latinime;
-
/*
* Returns the JNI version on success, -1 on failure.
*/
@@ -60,3 +57,23 @@
bail:
return result;
}
+
+namespace latinime {
+
+int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods,
+ int numMethods) {
+ jclass clazz = env->FindClass(className);
+ if (clazz == NULL) {
+ LOGE("Native registration unable to find class '%s'", className);
+ return JNI_FALSE;
+ }
+ if (env->RegisterNatives(clazz, methods, numMethods) < 0) {
+ LOGE("RegisterNatives failed for '%s'", className);
+ env->DeleteLocalRef(clazz);
+ return JNI_FALSE;
+ }
+ env->DeleteLocalRef(clazz);
+ return JNI_TRUE;
+}
+
+} // namespace latinime
diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h
new file mode 100644
index 0000000..c502fa3
--- /dev/null
+++ b/native/jni/jni_common.h
@@ -0,0 +1,30 @@
+/*
+**
+** 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_JNI_COMMON_H
+#define LATINIME_JNI_COMMON_H
+
+#include "jni.h"
+
+namespace latinime {
+
+int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods,
+ int numMethods);
+
+} // namespace latinime
+
+#endif // LATINIME_JNI_COMMON_H