17898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
27898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Copyright (C) 2009 The Android Open Source Project
37898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
47898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
57898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * you may not use this file except in compliance with the License.
67898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * You may obtain a copy of the License at
77898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
87898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
97898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * See the License for the specific language governing permissions and
147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * limitations under the License.
157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <assert.h>
187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <cutils/log.h>
197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <jni.h>
207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <string.h>
217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <sys/types.h>
227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include <unistd.h>
237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include "../include/pinyinime.h"
257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include "../include/sync.h"
267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#include "../include/userdict.h"
277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#ifdef __cplusplus
297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectextern "C" {
307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#endif
317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectusing namespace ime_pinyin;
337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#define RET_BUF_LEN 256
357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic char16 retbuf[RET_BUF_LEN];
377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic char16 (*predict_buf)[kMaxPredictSize + 1] = NULL;
387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic size_t predict_len;
397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic Sync sync_worker;
417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic struct file_descriptor_offsets_t
437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project{
447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jclass mClass;
457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jfieldID mDescriptor;
467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project} gFileDescriptorOffsets;
477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeImOpenDecoder(JNIEnv* env, jclass jclazz,
497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                               jbyteArray fn_sys_dict,
507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                               jbyteArray fn_usr_dict) {
517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jbyte *fsd = (*env).GetByteArrayElements(fn_sys_dict, 0);
527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jbyte *fud = (*env).GetByteArrayElements(fn_usr_dict, 0);
537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
544248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  jboolean jret = JNI_FALSE;
554248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (im_open_decoder((const char*)fsd, (const char*)fud))
574248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project    jret = JNI_TRUE;
587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
594248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseByteArrayElements(fn_sys_dict, fsd, 0);
604248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseByteArrayElements(fn_usr_dict, fud, 0);
614248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
624248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  return jret;
637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeImOpenDecoderFd(JNIEnv* env, jclass jclazz,
667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                                 jobject fd_sys_dict,
677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                                 jlong startoffset,
687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                                 jlong length,
697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                                 jbyteArray fn_usr_dict) {
707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jint fd = env->GetIntField(fd_sys_dict, gFileDescriptorOffsets.mDescriptor);
717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jbyte *fud = (*env).GetByteArrayElements(fn_usr_dict, 0);
727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
734248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  jboolean jret = JNI_FALSE;
744248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  int newfd = dup(fd);
764248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  if (im_open_decoder_fd(newfd, startoffset, length, (const char*)fud))
774248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project    jret = JNI_TRUE;
787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  close(newfd);
804248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
814248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseByteArrayElements(fn_usr_dict, fud, 0);
824248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
834248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  return jret;
847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT void JNICALL nativeImSetMaxLens(JNIEnv* env, jclass jclazz,
877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                          jint max_sps_len,
887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                          jint max_hzs_len) {
897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  im_set_max_lens(static_cast<size_t>(max_sps_len),
907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                  static_cast<size_t>(max_hzs_len));
917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return;
927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeImCloseDecoder(JNIEnv* env, jclass jclazz) {
957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  im_close_decoder();
967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return JNI_TRUE;
977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImSearch(JNIEnv* env, jclass jclazz,
1007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                      jbyteArray pybuf, jint pylen) {
1017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jbyte *array_body = (*env).GetByteArrayElements(pybuf, 0);
1027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1034248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  jint jret = 0;
1044248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  if (NULL != array_body) {
1054248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project    jret = im_search((const char*)array_body, pylen);
1064248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  }
1074248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
1084248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseByteArrayElements(pybuf, array_body, 0);
1097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1104248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  return jret;
1117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImDelSearch(JNIEnv* env, jclass jclazz, jint pos,
1147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                         jboolean is_pos_in_splid,
1157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                         jboolean clear_fixed_this_step) {
1167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return im_delsearch(pos, is_pos_in_splid, clear_fixed_this_step);
1177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT void JNICALL nativeImResetSearch(JNIEnv* env, jclass jclazz) {
1207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  im_reset_search();
1217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return;
1227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImAddLetter(JNIEnv *env, jclass clazz, jbyte ch) {
1257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return im_add_letter(ch);
1267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jstring JNICALL nativeImGetPyStr(JNIEnv* env, jclass jclazz,
1297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                           jboolean decoded) {
1307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t py_len;
1317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const char *py = im_get_sps_str(&py_len);  // py_len gets decoded length
1327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  assert(NULL != py);
1337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (!decoded)
1347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    py_len = strlen(py);
1357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const unsigned short *spl_start;
1377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t len;
1387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  len = im_get_spl_start_pos(spl_start);
1397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t i;
1417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  for (i = 0; i < py_len; i++)
1427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    retbuf[i] = py[i];
1437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  retbuf[i] = (char16)'\0';
1447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jstring retstr = (*env).NewString((unsigned short*)retbuf, i);
1467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return retstr;
1477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImGetPyStrLen(JNIEnv* env, jclass jclazz,
1507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                           jboolean decoded) {
1517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t py_len;
1527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const char *py = im_get_sps_str(&py_len);  // py_len gets decoded length
1537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  assert(NULL != py);
1547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (!decoded)
1557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    py_len = strlen(py);
1567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return py_len;
1577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1597898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jintArray JNICALL nativeImGetSplStart(JNIEnv* env, jclass jclazz) {
1607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  const unsigned short *spl_start;
1617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t len;
1627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  // There will be len + 1 elements in the buffer when len > 0.
1647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  len = im_get_spl_start_pos(spl_start);
1657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jintArray arr = (*env).NewIntArray(len + 2);
1677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jint *arr_body = (*env).GetIntArrayElements(arr, 0);
1687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  assert(NULL != arr_body);
1697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  arr_body[0] = len; // element 0 is used to store the length of buffer.
1707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  for (size_t i = 0; i <= len; i++)
1717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    arr_body[i + 1] = spl_start[i];
1724248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
1734248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseIntArrayElements(arr, arr_body, 0);
1744248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
1757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return arr;
1767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jstring JNICALL nativeImGetChoice(JNIEnv *env, jclass clazz,
1797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                            jint candidateId) {
1807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jstring retstr;
1817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if(im_get_candidate(candidateId, retbuf, RET_BUF_LEN)) {
1827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    retstr = (*env).NewString(retbuf, utf16_strlen(retbuf));
1837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return retstr;
1847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  } else {
1857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    retstr = (*env).NewString((unsigned short*)retbuf, 0);
1867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return retstr;
1877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  }
1887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImChoose(JNIEnv *env, jclass clazz,
1917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                      jint choice_id) {
1927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return im_choose(choice_id);
1937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImCancelLastChoice(JNIEnv *env, jclass clazz) {
1967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return im_cancel_last_choice();
1977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImGetFixedLen(JNIEnv *env, jclass clazz) {
2007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return im_get_fixed_len();
2017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeImCancelInput(JNIEnv *env, jclass clazz) {
2047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (im_cancel_input())
2057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return JNI_TRUE;
2067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return JNI_FALSE;
2087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeImFlushCache(JNIEnv *env, jclass clazz) {
2117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  im_flush_cache();
2127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return JNI_TRUE;
2137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeImGetPredictsNum(JNIEnv *env, jclass clazz,
2167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                              jstring fixed_str) {
2177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char16 *fixed_ptr = (char16*)(*env).GetStringChars(fixed_str, false);
2187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  size_t fixed_len = (size_t)(*env).GetStringLength(fixed_str);
2197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char16 fixed_buf[kMaxPredictSize + 1];
2217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (fixed_len > kMaxPredictSize) {
2237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    fixed_ptr += fixed_len - kMaxPredictSize;
2247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    fixed_len = kMaxPredictSize;
2257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  }
2267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  utf16_strncpy(fixed_buf, fixed_ptr, fixed_len);
2277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  fixed_buf[fixed_len] = (char16)'\0';
2287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  predict_len = im_get_predicts(fixed_buf, predict_buf);
2307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2314248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseStringChars(fixed_str, fixed_ptr);
2324248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
2337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return predict_len;
2347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jstring JNICALL nativeImGetPredictItem(JNIEnv *env, jclass clazz,
2377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                                 jint predict_no) {
2387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jstring retstr;
2397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (predict_no < 0 || (size_t)predict_no >= predict_len) {
2417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    retstr = (*env).NewString((unsigned short*)predict_buf[0], 0);
2427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  } else {
2437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    retstr = (*env).NewString((unsigned short*)predict_buf[predict_no],
2447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                              utf16_strlen(predict_buf[predict_no]));
2457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  }
2467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return retstr;
2477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeSyncBegin(JNIEnv *env, jclass clazz,
2507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                           jbyteArray dict_file) {
2517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jbyte *file_name = (*env).GetByteArrayElements(dict_file, 0);
2524248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
2534248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  jboolean jret = JNI_FALSE;
2547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (true == sync_worker.begin((const char *)file_name))
2554248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project    jret = JNI_TRUE;
2564248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
2574248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  (*env).ReleaseByteArrayElements(dict_file, file_name, 0);
2584248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project
2594248fb0083e2e0f2f9b379ea5ce898036b900218The Android Open Source Project  return jret;
2607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeSyncFinish(JNIEnv *env, jclass clazz) {
2637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  sync_worker.finish();
2647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return JNI_TRUE;
2657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeSyncGetCapacity(JNIEnv *env, jclass clazz) {
2687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return sync_worker.get_capacity();
2697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeSyncPutLemmas(JNIEnv *env, jclass clazz,
2727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                                           jstring tomerge) {
2737898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2747898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  char16 *ptr = (char16*)(*env).GetStringChars(tomerge, NULL);
2757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  int len = (size_t)(*env).GetStringLength(tomerge);
2767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  int added = sync_worker.put_lemmas(ptr, len);
2787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  (*env).ReleaseStringChars(tomerge, ptr);
2807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return added;
2827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jstring JNICALL nativeSyncGetLemmas(JNIEnv *env, jclass clazz) {
2857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  int len = sync_worker.get_lemmas(retbuf, RET_BUF_LEN);
2877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  if (len == 0)
2887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return NULL;
2897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  jstring retstr;
2907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  retstr = (*env).NewString((unsigned short*)retbuf, len);
2917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return retstr;
2927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeSyncGetLastCount(JNIEnv *env, jclass clazz) {
2957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return sync_worker.get_last_got_count();
2967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
2977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
2987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL nativeSyncGetTotalCount(JNIEnv *env, jclass clazz) {
2997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return sync_worker.get_total_count();
3007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
3017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jboolean JNICALL nativeSyncClearLastGot(JNIEnv *env, jclass clazz) {
3037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  sync_worker.clear_last_got();
3047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project  return JNI_TRUE;
3057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
3067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/**
3087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Table of methods associated with a single class.
3097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
3107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic JNINativeMethod gMethods[] = {
3117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* name, signature, funcPtr */
3127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* ------Functions for Pinyin-to-hanzi decoding begin--------->> */
3137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImOpenDecoder", "([B[B)Z",
3147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImOpenDecoder },
3157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImOpenDecoderFd", "(Ljava/io/FileDescriptor;JJ[B)Z",
3167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImOpenDecoderFd },
3177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImSetMaxLens", "(II)V",
3187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImSetMaxLens },
3197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImCloseDecoder", "()Z",
3207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImCloseDecoder },
3217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImSearch",  "([BI)I",
3227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImSearch },
3237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImDelSearch",  "(IZZ)I",
3247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImDelSearch },
3257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImResetSearch",  "()V",
3267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImResetSearch },
3277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImAddLetter", "(B)I",
3287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImAddLetter },
3297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetPyStr", "(Z)Ljava/lang/String;",
3307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetPyStr },
3317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetPyStrLen", "(Z)I",
3327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetPyStrLen },
3337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetSplStart", "()[I",
3347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetSplStart },
3357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetChoice", "(I)Ljava/lang/String;",
3367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetChoice },
3377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImChoose", "(I)I",
3387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImChoose },
3397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImCancelLastChoice", "()I",
3407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImCancelLastChoice },
3417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetFixedLen", "()I",
3427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetFixedLen },
3437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetPredictsNum", "(Ljava/lang/String;)I",
3447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetPredictsNum },
3457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImGetPredictItem", "(I)Ljava/lang/String;",
3467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImGetPredictItem },
3477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImCancelInput", "()Z",
3487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImCancelInput },
3497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeImFlushCache", "()Z",
3507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeImFlushCache },
3517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* <<----Functions for Pinyin-to-hanzi decoding end------------- */
3527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* ------Functions for sync begin----------------------------->> */
3547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncBegin", "([B)Z",
3557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncBegin },
3567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncFinish", "()Z",
3577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncFinish },
3587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncPutLemmas", "(Ljava/lang/String;)I",
3597898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncPutLemmas },
3607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncGetLemmas", "()Ljava/lang/String;",
3617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncGetLemmas },
3627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncGetLastCount", "()I",
3637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncGetLastCount },
3647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncGetTotalCount", "()I",
3657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncGetTotalCount },
3667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncClearLastGot", "()Z",
3677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncClearLastGot },
3687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    { "nativeSyncGetCapacity", "()I",
3697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            (void*) nativeSyncGetCapacity },
3707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* <<----Functions for sync end--------------------------------- */
3717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project};
3727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3737898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3747898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
3757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Register several native methods for one class.
3767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
3777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic int registerNativeMethods(JNIEnv* env, const char* className,
3787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    JNINativeMethod* gMethods, int numMethods)
3797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project{
3807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    jclass clazz;
3817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    clazz = (*env).FindClass(className);
3837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    if (clazz == NULL) {
3847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return JNI_FALSE;
3857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
3867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    if ((*env).RegisterNatives(clazz, gMethods, numMethods) < 0) {
3877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return JNI_FALSE;
3887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
3897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    clazz = env->FindClass("java/io/FileDescriptor");
3917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    LOG_FATAL_IF(clazz == NULL, "Unable to find Java class java.io.FileDescriptor");
3927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
3937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
3947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
3957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                 "Unable to find descriptor field in java.io.FileDescriptor");
3967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
3977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return JNI_TRUE;
3987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
3997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
4017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Register native methods for all classes we know about.
4027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
4037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectstatic int registerNatives(JNIEnv* env)
4047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project{
4057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    if (!registerNativeMethods(env,
4067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project           "com/android/inputmethod/pinyin/PinyinDecoderService",
4077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            gMethods, sizeof(gMethods) / sizeof(gMethods[0])))
4087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return JNI_FALSE;
4097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return JNI_TRUE;
4117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
4127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
4147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Set some test stuff up.
4157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
4167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Returns the JNI version on success, -1 on failure.
4177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
4187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source ProjectJNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
4197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project{
4207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    JNIEnv* env = NULL;
4217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    jint result = -1;
4227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    if ((*vm).GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
4247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        goto bail;
4257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
4267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    assert(env != NULL);
4277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    if (!registerNatives(env)) {
4297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        goto bail;
4307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
4317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    /* success -- return valid version number */
4337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    result = JNI_VERSION_1_4;
4347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectbail:
4367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    return result;
4377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
4387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
4397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#ifdef __cplusplus
4407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
4417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project#endif
442