From ce9e52a12a6af8fca0eba42aaae24602fbd5c998 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Sat, 18 Jun 2011 13:09:55 +0900 Subject: [PATCH] Clean up in LatinIME native code Change-Id: I0062200a0181a491690115ac0fab8d11358e2f14 --- native/Android.mk | 2 +- ...oid_inputmethod_keyboard_ProximityInfo.cpp | 36 ++----------------- ...droid_inputmethod_keyboard_ProximityInfo.h | 2 ++ ...oid_inputmethod_latin_BinaryDictionary.cpp | 36 ++----------------- ...droid_inputmethod_latin_BinaryDictionary.h | 2 ++ native/jni/{onload.cpp => jni_common.cpp} | 23 ++++++++++-- native/jni/jni_common.h | 30 ++++++++++++++++ native/src/bigram_dictionary.h | 5 +-- native/src/char_utils.h | 2 +- native/src/debug.h | 3 ++ native/src/dictionary.h | 5 ++- native/src/proximity_info.cpp | 4 ++- native/src/proximity_info.h | 4 ++- native/src/unigram_dictionary.h | 4 +-- 14 files changed, 75 insertions(+), 83 deletions(-) rename native/jni/{onload.cpp => jni_common.cpp} (73%) create mode 100644 native/jni/jni_common.h diff --git a/native/Android.mk b/native/Android.mk index 4727b1e39..d9f4f1d38 100644 --- a/native/Android.mk +++ b/native/Android.mk @@ -11,7 +11,7 @@ LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-function LOCAL_SRC_FILES := \ jni/com_android_inputmethod_keyboard_ProximityInfo.cpp \ jni/com_android_inputmethod_latin_BinaryDictionary.cpp \ - jni/onload.cpp \ + jni/jni_common.cpp \ src/bigram_dictionary.cpp \ src/char_utils.cpp \ src/dictionary.cpp \ diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index 3db89edf1..f3e2a7e60 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 #include #include -// ---------------------------------------------------------------------------- - 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 @@ static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximit 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 bdeeb8f37..4a1e83b09 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 4b61c1414..ce874d8d4 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 @@ -35,22 +36,8 @@ #include #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 @@ static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jint di 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 @@ static JNINativeMethod sMethods[] = { {"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 f7cd81fa7..1b1ba7f0f 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 f02c9a052..8643f723f 100644 --- a/native/jni/onload.cpp +++ b/native/jni/jni_common.cpp @@ -26,11 +26,8 @@ #include #include -// ---------------------------------------------------------------------------- - using namespace latinime; - /* * Returns the JNI version on success, -1 on failure. */ @@ -60,3 +57,23 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { 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 000000000..c502fa3a8 --- /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 diff --git a/native/src/bigram_dictionary.h b/native/src/bigram_dictionary.h index d658b93e6..c07458a38 100644 --- a/native/src/bigram_dictionary.h +++ b/native/src/bigram_dictionary.h @@ -50,6 +50,7 @@ private: int *mInputCodes; int mInputLength; }; -// ---------------------------------------------------------------------------- -}; // namespace latinime + +} // namespace latinime + #endif // LATINIME_BIGRAM_DICTIONARY_H diff --git a/native/src/char_utils.h b/native/src/char_utils.h index 921ecb4a5..a69a35e7a 100644 --- a/native/src/char_utils.h +++ b/native/src/char_utils.h @@ -21,6 +21,6 @@ namespace latinime { unsigned short latin_tolower(unsigned short c); -}; // namespace latinime +} // namespace latinime #endif // LATINIME_CHAR_UTILS_H diff --git a/native/src/debug.h b/native/src/debug.h index ae629b222..38b2f107a 100644 --- a/native/src/debug.h +++ b/native/src/debug.h @@ -28,6 +28,7 @@ static inline unsigned char* convertToUnibyteString(unsigned short* input, unsig output[i] = 0; return output; } + static inline unsigned char* convertToUnibyteStringAndReplaceLastChar(unsigned short* input, unsigned char* output, const unsigned int length, unsigned char c) { int i = 0; @@ -37,6 +38,7 @@ static inline unsigned char* convertToUnibyteStringAndReplaceLastChar(unsigned s output[i] = 0; return output; } + static inline void LOGI_S16(unsigned short* string, const unsigned int length) { unsigned char tmp_buffer[length]; convertToUnibyteString(string, tmp_buffer, length); @@ -46,6 +48,7 @@ static inline void LOGI_S16(unsigned short* string, const unsigned int length) { // TODO : refactor this in a blocking log or something. // usleep(10); } + static inline void LOGI_S16_PLUS(unsigned short* string, const unsigned int length, unsigned char c) { unsigned char tmp_buffer[length+1]; diff --git a/native/src/dictionary.h b/native/src/dictionary.h index 1b41f69dd..3dc577a56 100644 --- a/native/src/dictionary.h +++ b/native/src/dictionary.h @@ -81,7 +81,6 @@ private: BigramDictionary *mBigramDictionary; }; -// ---------------------------------------------------------------------------- // public static utility methods // static inline methods should be defined in the header file inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) { @@ -134,7 +133,6 @@ inline int Dictionary::getFreq(const unsigned char *dict, return freq; } - inline int Dictionary::wideStrLen(unsigned short *str) { if (!str) return 0; unsigned short *end = str; @@ -158,5 +156,6 @@ inline int Dictionary::setDictionaryValues(const unsigned char *dict, return position; } -}; // namespace latinime +} // namespace latinime + #endif // LATINIME_DICTIONARY_H diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp index 102123c3c..209c31e6e 100644 --- a/native/src/proximity_info.cpp +++ b/native/src/proximity_info.cpp @@ -22,6 +22,7 @@ #include "proximity_info.h" namespace latinime { + ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int gridHeight, const uint32_t *proximityCharsArray) @@ -61,4 +62,5 @@ bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { } return false; } -} // namespace latinime + +} // namespace latinime diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h index c2062e8c5..327cd0940 100644 --- a/native/src/proximity_info.h +++ b/native/src/proximity_info.h @@ -41,5 +41,7 @@ private: const int CELL_HEIGHT; uint32_t *mProximityCharsArray; }; -}; // namespace latinime + +} // namespace latinime + #endif // LATINIME_PROXIMITY_INFO_H diff --git a/native/src/unigram_dictionary.h b/native/src/unigram_dictionary.h index 3593dd656..154ac9b36 100644 --- a/native/src/unigram_dictionary.h +++ b/native/src/unigram_dictionary.h @@ -147,8 +147,6 @@ private: int mNextLettersFrequency[NEXT_LETTERS_SIZE]; }; -// ---------------------------------------------------------------------------- - -}; // namespace latinime +} // namespace latinime #endif // LATINIME_UNIGRAM_DICTIONARY_H