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