Add arguments for dic traverse session

Change-Id: I8776ad528eeea9a5fc4dcf394ca13bb80c04fb6d
diff --git a/native/jni/Android.mk b/native/jni/Android.mk
index 1b7301b..86ad857 100644
--- a/native/jni/Android.mk
+++ b/native/jni/Android.mk
@@ -47,6 +47,7 @@
     char_utils.cpp \
     correction.cpp \
     dictionary.cpp \
+    dic_traverse_wrapper.cpp \
     proximity_info.cpp \
     proximity_info_state.cpp \
     unigram_dictionary.cpp \
diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp
index 0f5c396..f0fa2db 100644
--- a/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp
+++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.cpp
@@ -17,25 +17,23 @@
 #define LOG_TAG "LatinIME: jni: Session"
 
 #include "com_android_inputmethod_latin_DicTraverseSession.h"
+#include "dic_traverse_wrapper.h"
 #include "jni.h"
 #include "jni_common.h"
 
 namespace latinime {
-void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)() = 0;
-void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0;
-void (*DicTraverseWrapper::sDicTraverseSessionInitMethod)(
-        JNIEnv *, void *, const jintArray, const jint) = 0;
-
-static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object,
-        jstring localejStr) {
-    void *traverseSession = DicTraverseWrapper::getDicTraverseSession();
+static jlong latinime_setDicTraverseSession(JNIEnv *env, jobject object, jstring localeJStr) {
+    void *traverseSession = DicTraverseWrapper::getDicTraverseSession(env, localeJStr);
     return reinterpret_cast<jlong>(traverseSession);
 }
 
 static void latinime_initDicTraverseSession(JNIEnv *env, jlong traverseSession,
-        jintArray previousWord, jint previousWordLength) {
+        jlong dictionary, jintArray previousWord, jint previousWordLength) {
     void *ts = reinterpret_cast<void*>(traverseSession);
-    DicTraverseWrapper::initDicTraverseSession(env, ts, previousWord, previousWordLength);
+    Dictionary *dict = reinterpret_cast<Dictionary*>(dictionary);
+    int prevWord[previousWordLength];
+    env->GetIntArrayRegion(previousWord, 0, previousWordLength, prevWord);
+    DicTraverseWrapper::initDicTraverseSession(ts, dict, prevWord, previousWordLength);
 }
 
 static void latinime_DicTraverseSession_release(
@@ -46,7 +44,7 @@
 
 static JNINativeMethod sMethods[] = {
     {"setDicTraverseSessionNative", "(Ljava/lang/String;)J", (void*)latinime_setDicTraverseSession},
-    {"initDicTraverseSessionNative", "(J[II)V", (void*)latinime_initDicTraverseSession},
+    {"initDicTraverseSessionNative", "(JJ[II)V", (void*)latinime_initDicTraverseSession},
     {"releaseDicTraverseSessionNative", "(J)V", (void*)latinime_DicTraverseSession_release}
 };
 
diff --git a/native/jni/com_android_inputmethod_latin_DicTraverseSession.h b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h
index a76815d..37531e9 100644
--- a/native/jni/com_android_inputmethod_latin_DicTraverseSession.h
+++ b/native/jni/com_android_inputmethod_latin_DicTraverseSession.h
@@ -21,33 +21,6 @@
 #include "jni.h"
 
 namespace latinime {
-
-// TODO: Remove
-class DicTraverseWrapper {
- public:
-    static void *getDicTraverseSession() {
-        if (sDicTraverseSessionFactoryMethod) {
-            return sDicTraverseSessionFactoryMethod();
-        }
-        return 0;
-    }
-    static void initDicTraverseSession(JNIEnv *env, void *traverseSession,
-            const jintArray prevWord, const jint prevWordLength) {
-        if (sDicTraverseSessionInitMethod) {
-            sDicTraverseSessionInitMethod(env, traverseSession, prevWord, prevWordLength);
-        }
-    }
-    static void releaseDicTraverseSession(void *traverseSession) {
-        if (sDicTraverseSessionReleaseMethod) {
-            sDicTraverseSessionReleaseMethod(traverseSession);
-        }
-    }
- private:
-    DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseWrapper);
-    static void *(*sDicTraverseSessionFactoryMethod)();
-    static void (*sDicTraverseSessionInitMethod)(JNIEnv *, void *, const jintArray, const jint);
-    static void (*sDicTraverseSessionReleaseMethod)(void *);
-};
 int register_DicTraverseSession(JNIEnv *env);
 } // namespace latinime
 #endif // _COM_ANDROID_INPUTMETHOD_LATIN_DICTRAVERSESESSION_H
diff --git a/native/jni/src/dic_traverse_wrapper.cpp b/native/jni/src/dic_traverse_wrapper.cpp
new file mode 100644
index 0000000..1f7dcbf
--- /dev/null
+++ b/native/jni/src/dic_traverse_wrapper.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "LatinIME: jni: Session"
+
+#include "dic_traverse_wrapper.h"
+
+namespace latinime {
+void *(*DicTraverseWrapper::sDicTraverseSessionFactoryMethod)(JNIEnv *env, jstring locale) = 0;
+void (*DicTraverseWrapper::sDicTraverseSessionReleaseMethod)(void *) = 0;
+void (*DicTraverseWrapper::sDicTraverseSessionInitMethod)(
+        void *, Dictionary *, const int *, const int) = 0;
+} // namespace latinime
diff --git a/native/jni/src/dic_traverse_wrapper.h b/native/jni/src/dic_traverse_wrapper.h
new file mode 100644
index 0000000..8396d00
--- /dev/null
+++ b/native/jni/src/dic_traverse_wrapper.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LATINIME_DIC_TRAVERSE_WRAPPER_H
+#define LATINIME_DIC_TRAVERSE_WRAPPER_H
+
+#include <stdint.h>
+
+#include "defines.h"
+#include "jni.h"
+
+namespace latinime {
+class Dictionary;
+// TODO: Remove
+class DicTraverseWrapper {
+ public:
+    static void *getDicTraverseSession(JNIEnv *env, jstring locale) {
+        if (sDicTraverseSessionFactoryMethod) {
+            return sDicTraverseSessionFactoryMethod(env, locale);
+        }
+        return 0;
+    }
+    static void initDicTraverseSession(void *traverseSession,
+            Dictionary *dictionary, const int *prevWord, const int prevWordLength) {
+        if (sDicTraverseSessionInitMethod) {
+            sDicTraverseSessionInitMethod(traverseSession, dictionary, prevWord, prevWordLength);
+        }
+    }
+    static void releaseDicTraverseSession(void *traverseSession) {
+        if (sDicTraverseSessionReleaseMethod) {
+            sDicTraverseSessionReleaseMethod(traverseSession);
+        }
+    }
+    static void setTraverseSessionFactoryMethod(
+            void *(*factoryMethod)(JNIEnv *env, jstring locale)) {
+        sDicTraverseSessionFactoryMethod = factoryMethod;
+    }
+    static void setTraverseSessionInitMethod(
+            void (*initMethod)(void *, Dictionary *, const int *, const int)) {
+        sDicTraverseSessionInitMethod = initMethod;
+    }
+    static void setTraverseSessionReleaseMethod(void (*releaseMethod)(void *)) {
+        sDicTraverseSessionReleaseMethod = releaseMethod;
+    }
+ private:
+    DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseWrapper);
+    static void *(*sDicTraverseSessionFactoryMethod)(JNIEnv *, jstring);
+    static void (*sDicTraverseSessionInitMethod)(void *, Dictionary *, const int *, const int);
+    static void (*sDicTraverseSessionReleaseMethod)(void *);
+};
+int register_DicTraverseSession(JNIEnv *env);
+} // namespace latinime
+#endif // LATINIME_DIC_TRAVERSE_WRAPPER_H
diff --git a/native/jni/src/dictionary.cpp b/native/jni/src/dictionary.cpp
index 8c785a2..f3bdb31 100644
--- a/native/jni/src/dictionary.cpp
+++ b/native/jni/src/dictionary.cpp
@@ -22,6 +22,7 @@
 #include "binary_format.h"
 #include "defines.h"
 #include "dictionary.h"
+#include "dic_traverse_wrapper.h"
 #include "gesture_decoder_wrapper.h"
 #include "unigram_dictionary.h"
 
@@ -31,8 +32,9 @@
 Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
         int typedLetterMultiplier, int fullWordMultiplier,
         int maxWordLength, int maxWords, int maxPredictions)
-    : mDict((unsigned char*) dict), mDictSize(dictSize),
-      mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) {
+    : mDict((unsigned char*) dict),
+      mOffsetDict(((unsigned char*) dict) + BinaryFormat::getHeaderSize(mDict)),
+      mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) {
     if (DEBUG_DICT) {
         if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
             AKLOGI("Max word length (%d) is greater than %d",
@@ -40,14 +42,13 @@
             AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
         }
     }
-    const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
     const unsigned int options = BinaryFormat::getFlags(mDict);
-    mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier,
+    mUnigramDictionary = new UnigramDictionary(mOffsetDict, typedLetterMultiplier,
             fullWordMultiplier, maxWordLength, maxWords, options);
-    mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxPredictions);
+    mBigramDictionary = new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions);
     mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords);
     mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary,
-            mDict + headerSize /* dict root */, 0 /* root pos */);
+            mOffsetDict /* dict root */, 0 /* root pos */);
 }
 
 Dictionary::~Dictionary() {
@@ -64,7 +65,8 @@
         int *frequencies, int *spaceIndices, int *outputTypes) {
     int result = 0;
     if (isGesture) {
-        mGestureDecoder->setPrevWord(prevWordChars, prevWordLength);
+        DicTraverseWrapper::initDicTraverseSession(
+                traverseSession, this, prevWordChars, prevWordLength);
         result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession,
                 xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint,
                 outWords, frequencies, spaceIndices, outputTypes);
diff --git a/native/jni/src/dictionary.h b/native/jni/src/dictionary.h
index 2c79527..b550ba4 100644
--- a/native/jni/src/dictionary.h
+++ b/native/jni/src/dictionary.h
@@ -55,7 +55,8 @@
 
     int getFrequency(const int32_t *word, int length) const;
     bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const;
-    void *getDict() const { return (void *)mDict; }
+    void *getDict() const { return (void *)mDict; } // required to release dictionary buffer
+    void *getOffsetDict() const { return (void *)mOffsetDict; }
     int getDictSize() const { return mDictSize; }
     int getMmapFd() const { return mMmapFd; }
     int getDictBufAdjust() const { return mDictBufAdjust; }
@@ -68,6 +69,7 @@
  private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
     const unsigned char *mDict;
+    const unsigned char *mOffsetDict;
 
     // Used only for the mmap version of dictionary loading, but we use these as dummy variables
     // also for the malloc version.
diff --git a/native/jni/src/gesture/gesture_decoder_wrapper.h b/native/jni/src/gesture/gesture_decoder_wrapper.h
index b70c8e0..14a0705 100644
--- a/native/jni/src/gesture/gesture_decoder_wrapper.h
+++ b/native/jni/src/gesture/gesture_decoder_wrapper.h
@@ -63,13 +63,6 @@
         mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos);
     }
 
-    void setPrevWord(const int32_t *prevWord, int prevWordLength) {
-        if (!mIncrementalDecoderInterface) {
-            return;
-        }
-        mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength);
-    }
-
     static void setGestureDecoderFactoryMethod(
             IncrementalDecoderInterface *(*factoryMethod)(int, int)) {
         sGestureDecoderFactoryMethod = factoryMethod;
diff --git a/native/jni/src/gesture/incremental_decoder_interface.h b/native/jni/src/gesture/incremental_decoder_interface.h
index e8d3a53..cc219bf 100644
--- a/native/jni/src/gesture/incremental_decoder_interface.h
+++ b/native/jni/src/gesture/incremental_decoder_interface.h
@@ -35,8 +35,9 @@
     virtual void reset() = 0;
     virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram,
             const uint8_t *dictRoot, int rootPos) = 0;
-    virtual void setPrevWord(const int32_t *prevWord, int prevWordLength) = 0;
     virtual ~IncrementalDecoderInterface() { };
+ private:
+    //DISALLOW_COPY_AND_ASSIGN(IncrementalDecoderInterface);
 };
 } // namespace latinime
 #endif // LATINIME_INCREMENTAL_DECODER_INTERFACE_H
diff --git a/native/jni/src/gesture/incremental_decoder_wrapper.h b/native/jni/src/gesture/incremental_decoder_wrapper.h
index 8d66b4e..2f96028 100644
--- a/native/jni/src/gesture/incremental_decoder_wrapper.h
+++ b/native/jni/src/gesture/incremental_decoder_wrapper.h
@@ -63,13 +63,6 @@
         mIncrementalDecoderInterface->setDict(dict, bigram, dictRoot, rootPos);
     }
 
-    void setPrevWord(const int32_t *prevWord, int prevWordLength) {
-        if (!mIncrementalDecoderInterface) {
-            return;
-        }
-        mIncrementalDecoderInterface->setPrevWord(prevWord, prevWordLength);
-    }
-
     static void setIncrementalDecoderFactoryMethod(
             IncrementalDecoderInterface *(*factoryMethod)(int, int)) {
         sIncrementalDecoderFactoryMethod = factoryMethod;