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#include "com_android_inputmethod_keyboard_ProximityInfo.h"
22#include "com_android_inputmethod_latin_BinaryDictionary.h"
23#include "com_android_inputmethod_latin_BinaryDictionaryUtils.h"
24#include "com_android_inputmethod_latin_DicTraverseSession.h"
25#include "defines.h"
26
27/*
28 * Returns the JNI version on success, -1 on failure.
29 */
30jint JNI_OnLoad(JavaVM *vm, void *reserved) {
31    JNIEnv *env = 0;
32
33    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
34        AKLOGE("ERROR: GetEnv failed");
35        return -1;
36    }
37    ASSERT(env);
38    if (!env) {
39        AKLOGE("ERROR: JNIEnv is invalid");
40        return -1;
41    }
42    if (!latinime::register_BinaryDictionary(env)) {
43        AKLOGE("ERROR: BinaryDictionary native registration failed");
44        return -1;
45    }
46    if (!latinime::register_BinaryDictionaryUtils(env)) {
47        AKLOGE("ERROR: BinaryDictionaryUtils native registration failed");
48        return -1;
49    }
50    if (!latinime::register_DicTraverseSession(env)) {
51        AKLOGE("ERROR: DicTraverseSession native registration failed");
52        return -1;
53    }
54    if (!latinime::register_ProximityInfo(env)) {
55        AKLOGE("ERROR: ProximityInfo native registration failed");
56        return -1;
57    }
58    /* success -- return valid version number */
59    return JNI_VERSION_1_6;
60}
61
62namespace latinime {
63int registerNativeMethods(JNIEnv *env, const char *const className, const JNINativeMethod *methods,
64        const int numMethods) {
65    jclass clazz = env->FindClass(className);
66    if (!clazz) {
67        AKLOGE("Native registration unable to find class '%s'", className);
68        return JNI_FALSE;
69    }
70    if (env->RegisterNatives(clazz, methods, numMethods) != 0) {
71        AKLOGE("RegisterNatives failed for '%s'", className);
72        env->DeleteLocalRef(clazz);
73        return JNI_FALSE;
74    }
75    env->DeleteLocalRef(clazz);
76    return JNI_TRUE;
77}
78} // namespace latinime
79