Home | History | Annotate | Download | only in jni
      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 #include "defines.h"
     21 #include "dic_traverse_wrapper.h"
     22 #include "jni.h"
     23 #include "jni_common.h"
     24 
     25 namespace latinime {
     26 class Dictionary;
     27 static jlong latinime_setDicTraverseSession(JNIEnv *env, jclass clazz, jstring localeJStr) {
     28     void *traverseSession = DicTraverseWrapper::getDicTraverseSession(env, localeJStr);
     29     return reinterpret_cast<jlong>(traverseSession);
     30 }
     31 
     32 static void latinime_initDicTraverseSession(JNIEnv *env, jclass clazz, jlong traverseSession,
     33         jlong dictionary, jintArray previousWord, jint previousWordLength) {
     34     void *ts = reinterpret_cast<void *>(traverseSession);
     35     Dictionary *dict = reinterpret_cast<Dictionary *>(dictionary);
     36     if (!previousWord) {
     37         DicTraverseWrapper::initDicTraverseSession(ts, dict, 0, 0);
     38         return;
     39     }
     40     int prevWord[previousWordLength];
     41     env->GetIntArrayRegion(previousWord, 0, previousWordLength, prevWord);
     42     DicTraverseWrapper::initDicTraverseSession(ts, dict, prevWord, previousWordLength);
     43 }
     44 
     45 static void latinime_releaseDicTraverseSession(JNIEnv *env, jclass clazz, jlong traverseSession) {
     46     void *ts = reinterpret_cast<void *>(traverseSession);
     47     DicTraverseWrapper::releaseDicTraverseSession(ts);
     48 }
     49 
     50 static JNINativeMethod sMethods[] = {
     51     {const_cast<char *>("setDicTraverseSessionNative"),
     52      const_cast<char *>("(Ljava/lang/String;)J"),
     53      reinterpret_cast<void *>(latinime_setDicTraverseSession)},
     54     {const_cast<char *>("initDicTraverseSessionNative"),
     55      const_cast<char *>("(JJ[II)V"),
     56      reinterpret_cast<void *>(latinime_initDicTraverseSession)},
     57     {const_cast<char *>("releaseDicTraverseSessionNative"),
     58      const_cast<char *>("(J)V"),
     59      reinterpret_cast<void *>(latinime_releaseDicTraverseSession)}
     60 };
     61 
     62 int register_DicTraverseSession(JNIEnv *env) {
     63     const char *const kClassPathName = "com/android/inputmethod/latin/DicTraverseSession";
     64     return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods));
     65 }
     66 } // namespace latinime
     67