jni_common.cpp revision 112257e40f6f6d914fac1c3a45f39a770693b386
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "LatinIME: jni"
18
19#include "jni_common.h"
20
21#ifndef HOST_TOOL
22#include "com_android_inputmethod_keyboard_ProximityInfo.h"
23#include "com_android_inputmethod_latin_BinaryDictionary.h"
24#include "com_android_inputmethod_latin_DicTraverseSession.h"
25#endif
26#include "com_android_inputmethod_latin_makedict_Ver3DictDecoder.h"
27#include "defines.h"
28
29/*
30 * Returns the JNI version on success, -1 on failure.
31 */
32jint JNI_OnLoad(JavaVM *vm, void *reserved) {
33    JNIEnv *env = 0;
34
35    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
36        AKLOGE("ERROR: GetEnv failed");
37        return -1;
38    }
39    ASSERT(env);
40    if (!env) {
41        AKLOGE("ERROR: JNIEnv is invalid");
42        return -1;
43    }
44#ifndef HOST_TOOL
45    if (!latinime::register_BinaryDictionary(env)) {
46        AKLOGE("ERROR: BinaryDictionary native registration failed");
47        return -1;
48    }
49    if (!latinime::register_DicTraverseSession(env)) {
50        AKLOGE("ERROR: DicTraverseSession native registration failed");
51        return -1;
52    }
53    if (!latinime::register_ProximityInfo(env)) {
54        AKLOGE("ERROR: ProximityInfo native registration failed");
55        return -1;
56    }
57#endif
58    if (!latinime::register_Ver3DictDecoder(env)) {
59        AKLOGE("ERROR: Ver3DictDecoder native registration failed");
60        return -1;
61    }
62    /* success -- return valid version number */
63    return JNI_VERSION_1_6;
64}
65
66namespace latinime {
67int registerNativeMethods(JNIEnv *env, const char *const className, const JNINativeMethod *methods,
68        const int numMethods) {
69    jclass clazz = env->FindClass(className);
70    if (!clazz) {
71        AKLOGE("Native registration unable to find class '%s'", className);
72        return JNI_FALSE;
73    }
74    if (env->RegisterNatives(clazz, methods, numMethods) < 0) {
75        AKLOGE("RegisterNatives failed for '%s'", className);
76        env->DeleteLocalRef(clazz);
77        return JNI_FALSE;
78    }
79    env->DeleteLocalRef(clazz);
80    return JNI_TRUE;
81}
82} // namespace latinime
83