Add proximity info to native

Bug: 3311719

Change-Id: Ie596304070e321ad23fb67a13bf05e2b6af1b54b
diff --git a/native/src/defines.h b/native/src/defines.h
index 918028a..ddd65c9 100644
--- a/native/src/defines.h
+++ b/native/src/defines.h
@@ -28,6 +28,7 @@
 #define DEBUG_SHOW_FOUND_WORD DEBUG_DICT_FULL
 #define DEBUG_NODE DEBUG_DICT_FULL
 #define DEBUG_TRACE DEBUG_DICT_FULL
+#define DEBUG_PROXIMITY_INFO true
 
 // Profiler
 #include <time.h>
@@ -83,6 +84,7 @@
 #define DEBUG_SHOW_FOUND_WORD false
 #define DEBUG_NODE false
 #define DEBUG_TRACE false
+#define DEBUG_PROXIMITY_INFO false
 
 #define PROF_BUF_SIZE 0
 #define PROF_RESET
diff --git a/native/src/dictionary.cpp b/native/src/dictionary.cpp
index fe33757..d69cb2a 100644
--- a/native/src/dictionary.cpp
+++ b/native/src/dictionary.cpp
@@ -23,6 +23,7 @@
 
 namespace latinime {
 
+// TODO: Change the type of all keyCodes to uint32_t
 Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
         int typedLetterMultiplier, int fullWordMultiplier,
         int maxWordLength, int maxWords, int maxAlternatives)
@@ -53,8 +54,7 @@
 }
 
 // TODO: use uint16_t instead of unsigned short
-bool Dictionary::isValidWord(unsigned short *word, int length)
-{
+bool Dictionary::isValidWord(unsigned short *word, int length) {
     if (IS_LATEST_DICT_VERSION) {
         return (isValidWordRec(DICTIONARY_HEADER_SIZE, word, 0, length) != NOT_VALID_WORD);
     } else {
diff --git a/native/src/dictionary.h b/native/src/dictionary.h
index 941bd19..fbbb831 100644
--- a/native/src/dictionary.h
+++ b/native/src/dictionary.h
@@ -19,6 +19,7 @@
 
 #include "bigram_dictionary.h"
 #include "defines.h"
+#include "proximity_info.h"
 #include "unigram_dictionary.h"
 
 namespace latinime {
@@ -27,8 +28,10 @@
 public:
     Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler,
             int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
-    int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies) {
-        return mUnigramDictionary->getSuggestions(codes, codesSize, outWords, frequencies);
+    int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
+            int *codes, int codesSize, unsigned short *outWords, int *frequencies) {
+        return mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, codes,
+                codesSize, outWords, frequencies);
     }
 
     // TODO: Call mBigramDictionary instead of mUnigramDictionary
@@ -38,6 +41,7 @@
         return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
                 maxWordLength, maxBigrams, maxAlternatives);
     }
+
     bool isValidWord(unsigned short *word, int length);
     int isValidWordRec(int pos, unsigned short *word, int offset, int length);
     void *getDict() { return (void *)mDict; }
diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp
new file mode 100644
index 0000000..d0cba3e
--- /dev/null
+++ b/native/src/proximity_info.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2011 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.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "proximity_info.h"
+
+namespace latinime {
+ProximityInfo::ProximityInfo(int maxProximityCharsSize, int displayWidth, int displayHeight,
+        int gridWidth, int gridHeight, uint32_t const *proximityCharsArray)
+        : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), DISPLAY_WIDTH(displayWidth),
+          DISPLAY_HEIGHT(displayHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight) {
+    mProximityCharsArray = new uint32_t[GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE];
+    memcpy(mProximityCharsArray, proximityCharsArray, sizeof(mProximityCharsArray));
+}
+
+ProximityInfo::~ProximityInfo() {
+    delete[] mProximityCharsArray;
+}
+}
diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h
new file mode 100644
index 0000000..54e9950
--- /dev/null
+++ b/native/src/proximity_info.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2011 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_PROXIMITY_INFO_H
+#define LATINIME_PROXIMITY_INFO_H
+
+#include <stdint.h>
+
+#include "defines.h"
+
+namespace latinime {
+
+class ProximityInfo {
+public:
+    ProximityInfo(int maxProximityCharsSize, int displayWidth, int displayHeight, int gridWidth,
+            int gridHeight, uint32_t const *proximityCharsArray);
+    ~ProximityInfo();
+private:
+    const int MAX_PROXIMITY_CHARS_SIZE;
+    const int DISPLAY_WIDTH;
+    const int DISPLAY_HEIGHT;
+    const int GRID_WIDTH;
+    const int GRID_HEIGHT;
+    uint32_t *mProximityCharsArray;
+};
+}; // namespace latinime
+#endif // LATINIME_PROXIMITY_INFO_H
diff --git a/native/src/unigram_dictionary.cpp b/native/src/unigram_dictionary.cpp
index 0ea6506..72b0f36 100644
--- a/native/src/unigram_dictionary.cpp
+++ b/native/src/unigram_dictionary.cpp
@@ -41,13 +41,19 @@
 
 UnigramDictionary::~UnigramDictionary() {}
 
-int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords,
-        int *frequencies) {
+int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates,
+        int *ycoordinates, int *codes, int codesSize, unsigned short *outWords, int *frequencies) {
     PROF_OPEN;
     PROF_START(0);
     initSuggestions(codes, codesSize, outWords, frequencies);
     if (DEBUG_DICT) assert(codesSize == mInputLength);
 
+    if (DEBUG_PROXIMITY_INFO) {
+        for (int i = 0; i < codesSize; ++i) {
+            LOGI("Input[%d] x = %d, y = %d", i, xcoordinates[i], ycoordinates[i]);
+        }
+    }
+
     const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
     PROF_END(0);
 
@@ -406,7 +412,7 @@
 
 inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
         const int inputIndex, const int skipPos, const int depth) {
-    const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
+    const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
     // Skip the ' or other letter and continue deeper
     return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
 }
@@ -517,7 +523,7 @@
         *newDiffs = diffs;
         *newInputIndex = inputIndex;
     } else {
-        int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
+        int *currentChars = getInputCharsAt(inputIndex);
 
         if (transposedPos >= 0) {
             if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
@@ -620,7 +626,7 @@
         const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
         int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
     const int inputIndex = startInputIndex + depth;
-    const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
+    const int *currentChars = getInputCharsAt(inputIndex);
     unsigned short c;
     *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
             newChildPosition, newTerminal, newFreq);
diff --git a/native/src/unigram_dictionary.h b/native/src/unigram_dictionary.h
index db40646..e84875b 100644
--- a/native/src/unigram_dictionary.h
+++ b/native/src/unigram_dictionary.h
@@ -18,6 +18,7 @@
 #define LATINIME_UNIGRAM_DICTIONARY_H
 
 #include "defines.h"
+#include "proximity_info.h"
 
 namespace latinime {
 
@@ -32,7 +33,8 @@
 public:
     UnigramDictionary(const unsigned char *dict, int typedLetterMultipler, int fullWordMultiplier,
             int maxWordLength, int maxWords, int maxProximityChars, const bool isLatestDictVersion);
-    int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies);
+    int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
+            int *codes, int codesSize, unsigned short *outWords, int *frequencies);
     ~UnigramDictionary();
 
 private:
@@ -84,7 +86,9 @@
             const int startInputIndex, const int depth, unsigned short *word,
             int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos);
     bool existsAdjacentProximityChars(const int inputIndex, const int inputLength);
-    int* getInputCharsAt(const int index) {return mInputCodes + (index * MAX_PROXIMITY_CHARS);}
+    inline int* getInputCharsAt(const int index) {
+        return mInputCodes + (index * MAX_PROXIMITY_CHARS);
+    }
     const unsigned char *DICT;
     const int MAX_WORD_LENGTH;
     const int MAX_WORDS;