Clean up in LatinIME native code

Change-Id: I0062200a0181a491690115ac0fab8d11358e2f14
main
Ken Wakasa 2011-06-18 13:09:55 +09:00
parent c029e0f4a7
commit ce9e52a12a
14 changed files with 75 additions and 83 deletions

View File

@ -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 \

View File

@ -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 @@ 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

View File

@ -21,7 +21,9 @@
#include "jni.h"
namespace latinime {
int register_ProximityInfo(JNIEnv *env);
}
#endif // _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H

View File

@ -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 @@ 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

View File

@ -21,7 +21,9 @@
#include "jni.h"
namespace latinime {
int register_BinaryDictionary(JNIEnv *env);
}
#endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H

View File

@ -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 @@ 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

30
native/jni/jni_common.h Normal file
View File

@ -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

View File

@ -50,6 +50,7 @@ private:
int *mInputCodes;
int mInputLength;
};
// ----------------------------------------------------------------------------
}; // namespace latinime
} // namespace latinime
#endif // LATINIME_BIGRAM_DICTIONARY_H

View File

@ -21,6 +21,6 @@ namespace latinime {
unsigned short latin_tolower(unsigned short c);
}; // namespace latinime
} // namespace latinime
#endif // LATINIME_CHAR_UTILS_H

View File

@ -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];

View File

@ -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

View File

@ -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

View File

@ -41,5 +41,7 @@ private:
const int CELL_HEIGHT;
uint32_t *mProximityCharsArray;
};
}; // namespace latinime
} // namespace latinime
#endif // LATINIME_PROXIMITY_INFO_H

View File

@ -147,8 +147,6 @@ private:
int mNextLettersFrequency[NEXT_LETTERS_SIZE];
};
// ----------------------------------------------------------------------------
}; // namespace latinime
} // namespace latinime
#endif // LATINIME_UNIGRAM_DICTIONARY_H