Add proximity info to native

Bug: 3311719

Change-Id: Ie596304070e321ad23fb67a13bf05e2b6af1b54b
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
new file mode 100644
index 0000000..3db89ed
--- /dev/null
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
@@ -0,0 +1,90 @@
+/*
+**
+** Copyright 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.
+*/
+
+#define LOG_TAG "LatinIME: jni: ProximityInfo"
+
+#include "com_android_inputmethod_keyboard_ProximityInfo.h"
+#include "jni.h"
+#include "proximity_info.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+// ----------------------------------------------------------------------------
+
+namespace latinime {
+
+//
+// helper function to throw an exception
+//
+static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
+    if (jclass cls = env->FindClass(ex)) {
+        char msg[1000];
+        snprintf(msg, sizeof(msg), fmt, data);
+        env->ThrowNew(cls, msg);
+        env->DeleteLocalRef(cls);
+    }
+}
+
+static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
+        jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth,
+        jint gridHeight, jintArray proximityCharsArray) {
+    jint* proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL);
+    ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
+            displayHeight, gridWidth, gridHeight, (const uint32_t *)proximityChars);
+    env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
+    return (jint)proximityInfo;
+}
+
+static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximityInfo) {
+    ProximityInfo *pi = (ProximityInfo*)proximityInfo;
+    if (!pi) return;
+    delete pi;
+}
+
+// ----------------------------------------------------------------------------
+
+static JNINativeMethod sKeyboardMethods[] = {
+    {"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo},
+    {"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release}
+};
+
+static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods,
+        int numMethods) {
+    jclass clazz;
+
+    clazz = env->FindClass(className);
+    if (clazz == NULL) {
+        LOGE("Native registration unable to find class '%s'", className);
+        return JNI_FALSE;
+    }
+    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
+        LOGE("RegisterNatives failed for '%s'", className);
+        return JNI_FALSE;
+    }
+
+    return JNI_TRUE;
+}
+
+int register_ProximityInfo(JNIEnv *env) {
+    const char* const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo";
+    return registerNativeMethods(env, kClassPathName, sKeyboardMethods,
+            sizeof(sKeyboardMethods) / sizeof(sKeyboardMethods[0]));
+}
+
+}; // namespace latinime
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
new file mode 100644
index 0000000..bdeeb8f
--- /dev/null
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.h
@@ -0,0 +1,27 @@
+/*
+**
+** Copyright 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 _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
+#define _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
+
+#include "jni.h"
+
+namespace latinime {
+int register_ProximityInfo(JNIEnv *env);
+}
+
+#endif // _COM_ANDROID_INPUTMETHOD_KEYBOARD_PROXIMITYINFO_H
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 4660103..b10dd6d 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -15,10 +15,12 @@
 ** limitations under the License.
 */
 
-#define LOG_TAG "LatinIME: jni"
+#define LOG_TAG "LatinIME: jni: BinaryDictionary"
 
+#include "com_android_inputmethod_latin_BinaryDictionary.h"
 #include "dictionary.h"
 #include "jni.h"
+#include "proximity_info.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -35,7 +37,7 @@
 
 // ----------------------------------------------------------------------------
 
-using namespace latinime;
+namespace latinime {
 
 //
 // helper function to throw an exception
@@ -43,7 +45,7 @@
 static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
     if (jclass cls = env->FindClass(ex)) {
         char msg[1000];
-        sprintf(msg, fmt, data);
+        snprintf(msg, sizeof(msg), fmt, data);
         env->ThrowNew(cls, msg);
         env->DeleteLocalRef(cls);
     }
@@ -123,19 +125,27 @@
 }
 
 static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jint dict,
+        jint proximityInfo, jintArray xCoordinatesArray, jintArray yCoordinatesArray,
         jintArray inputArray, jint arraySize, jcharArray outputArray, jintArray frequencyArray) {
     Dictionary *dictionary = (Dictionary*)dict;
     if (!dictionary) return 0;
+    ProximityInfo *pInfo = (ProximityInfo*)proximityInfo;
+    if (!pInfo) return 0;
+
+    int *xCoordinates = env->GetIntArrayElements(xCoordinatesArray, NULL);
+    int *yCoordinates = env->GetIntArrayElements(yCoordinatesArray, NULL);
 
     int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
     int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
     jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
 
-    int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars,
-            frequencies);
+    int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, inputCodes,
+            arraySize, (unsigned short*) outputChars, frequencies);
 
     env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
     env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
+    env->ReleaseIntArrayElements(xCoordinatesArray, xCoordinates, 0);
+    env->ReleaseIntArrayElements(yCoordinatesArray, yCoordinates, 0);
     env->ReleaseCharArrayElements(outputArray, outputChars, 0);
 
     return count;
@@ -200,10 +210,10 @@
 
 // ----------------------------------------------------------------------------
 
-static JNINativeMethod gMethods[] = {
+static JNINativeMethod sMethods[] = {
     {"openNative", "(Ljava/lang/String;JJIIIII)I", (void*)latinime_BinaryDictionary_open},
     {"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
-    {"getSuggestionsNative", "(I[II[C[I)I", (void*)latinime_BinaryDictionary_getSuggestions},
+    {"getSuggestionsNative", "(II[I[I[II[C[I)I", (void*)latinime_BinaryDictionary_getSuggestions},
     {"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord},
     {"getBigramsNative", "(I[CI[II[C[IIII)I", (void*)latinime_BinaryDictionary_getBigrams}
 };
@@ -225,33 +235,10 @@
     return JNI_TRUE;
 }
 
-static int registerNatives(JNIEnv *env) {
+int register_BinaryDictionary(JNIEnv *env) {
     const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
-    return registerNativeMethods(env, kClassPathName, gMethods,
-            sizeof(gMethods) / sizeof(gMethods[0]));
+    return registerNativeMethods(env, kClassPathName, sMethods,
+            sizeof(sMethods) / sizeof(sMethods[0]));
 }
 
-/*
- * Returns the JNI version on success, -1 on failure.
- */
-jint JNI_OnLoad(JavaVM* vm, void* reserved) {
-    JNIEnv* env = NULL;
-    jint result = -1;
-
-    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
-        LOGE("ERROR: GetEnv failed");
-        goto bail;
-    }
-    assert(env != NULL);
-
-    if (!registerNatives(env)) {
-        LOGE("ERROR: BinaryDictionary native registration failed");
-        goto bail;
-    }
-
-    /* success -- return valid version number */
-    result = JNI_VERSION_1_4;
-
-bail:
-    return result;
-}
+}; // namespace latinime
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.h b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
new file mode 100644
index 0000000..f7cd81f
--- /dev/null
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.h
@@ -0,0 +1,27 @@
+/*
+**
+** Copyright 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 _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
+#define _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
+
+#include "jni.h"
+
+namespace latinime {
+int register_BinaryDictionary(JNIEnv *env);
+}
+
+#endif // _COM_ANDROID_INPUTMETHOD_LATIN_BINARYDICTIONARY_H
diff --git a/native/jni/onload.cpp b/native/jni/onload.cpp
new file mode 100644
index 0000000..f02c9a0
--- /dev/null
+++ b/native/jni/onload.cpp
@@ -0,0 +1,62 @@
+/*
+**
+** Copyright 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.
+*/
+
+#define LOG_TAG "LatinIME: jni"
+
+#include "com_android_inputmethod_keyboard_ProximityInfo.h"
+#include "com_android_inputmethod_latin_BinaryDictionary.h"
+#include "jni.h"
+#include "proximity_info.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+// ----------------------------------------------------------------------------
+
+using namespace latinime;
+
+
+/*
+ * Returns the JNI version on success, -1 on failure.
+ */
+jint JNI_OnLoad(JavaVM* vm, void* reserved) {
+    JNIEnv* env = NULL;
+    jint result = -1;
+
+    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+        LOGE("ERROR: GetEnv failed");
+        goto bail;
+    }
+    assert(env != NULL);
+
+    if (!register_BinaryDictionary(env)) {
+        LOGE("ERROR: BinaryDictionary native registration failed");
+        goto bail;
+    }
+
+    if (!register_ProximityInfo(env)) {
+        LOGE("ERROR: ProximityInfo native registration failed");
+        goto bail;
+    }
+
+    /* success -- return valid version number */
+    result = JNI_VERSION_1_4;
+
+bail:
+    return result;
+}