1/*
2 * Copyright (C) 2012 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: Session"
18
19#include "com_android_inputmethod_latin_DicTraverseSession.h"
20
21#include "defines.h"
22#include "jni.h"
23#include "jni_common.h"
24#include "suggest/core/session/dic_traverse_session.h"
25
26namespace latinime {
27class Dictionary;
28static jlong latinime_setDicTraverseSession(JNIEnv *env, jclass clazz, jstring localeJStr,
29        jlong dictSize) {
30    void *traverseSession = DicTraverseSession::getSessionInstance(env, localeJStr, dictSize);
31    return reinterpret_cast<jlong>(traverseSession);
32}
33
34static void latinime_initDicTraverseSession(JNIEnv *env, jclass clazz, jlong traverseSession,
35        jlong dictionary, jintArray previousWord, jint previousWordLength) {
36    DicTraverseSession *ts = reinterpret_cast<DicTraverseSession *>(traverseSession);
37    Dictionary *dict = reinterpret_cast<Dictionary *>(dictionary);
38    if (!previousWord) {
39        DicTraverseSession::initSessionInstance(
40                ts, dict, 0 /* prevWord */, 0 /* prevWordLength*/, 0 /* suggestOptions */);
41        return;
42    }
43    int prevWord[previousWordLength];
44    env->GetIntArrayRegion(previousWord, 0, previousWordLength, prevWord);
45    DicTraverseSession::initSessionInstance(
46            ts, dict, prevWord, previousWordLength, 0 /* suggestOptions */);
47}
48
49static void latinime_releaseDicTraverseSession(JNIEnv *env, jclass clazz, jlong traverseSession) {
50    DicTraverseSession *ts = reinterpret_cast<DicTraverseSession *>(traverseSession);
51    DicTraverseSession::releaseSessionInstance(ts);
52}
53
54static const JNINativeMethod sMethods[] = {
55    {
56        const_cast<char *>("setDicTraverseSessionNative"),
57        const_cast<char *>("(Ljava/lang/String;J)J"),
58        reinterpret_cast<void *>(latinime_setDicTraverseSession)
59    },
60    {
61        const_cast<char *>("initDicTraverseSessionNative"),
62        const_cast<char *>("(JJ[II)V"),
63        reinterpret_cast<void *>(latinime_initDicTraverseSession)
64    },
65    {
66        const_cast<char *>("releaseDicTraverseSessionNative"),
67        const_cast<char *>("(J)V"),
68        reinterpret_cast<void *>(latinime_releaseDicTraverseSession)
69    }
70};
71
72int register_DicTraverseSession(JNIEnv *env) {
73    const char *const kClassPathName = "com/android/inputmethod/latin/DicTraverseSession";
74    return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods));
75}
76} // namespace latinime
77