1 /* 2 * Copyright (C) 2014 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: BinaryDictionaryUtils" 18 19 #include "com_android_inputmethod_latin_BinaryDictionaryUtils.h" 20 21 #include "defines.h" 22 #include "dictionary/utils/dict_file_writing_utils.h" 23 #include "jni.h" 24 #include "jni_common.h" 25 #include "utils/autocorrection_threshold_utils.h" 26 #include "utils/char_utils.h" 27 #include "utils/jni_data_utils.h" 28 #include "utils/time_keeper.h" 29 30 namespace latinime { 31 32 static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env, jclass clazz, 33 jstring filePath, jlong dictVersion, jstring locale, jobjectArray attributeKeyStringArray, 34 jobjectArray attributeValueStringArray) { 35 const jsize filePathUtf8Length = env->GetStringUTFLength(filePath); 36 char filePathChars[filePathUtf8Length + 1]; 37 env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars); 38 filePathChars[filePathUtf8Length] = '\0'; 39 40 const jsize localeUtf8Length = env->GetStringUTFLength(locale); 41 char localeChars[localeUtf8Length + 1]; 42 env->GetStringUTFRegion(locale, 0, env->GetStringLength(locale), localeChars); 43 localeChars[localeUtf8Length] = '\0'; 44 std::vector<int> localeCodePoints; 45 HeaderReadWriteUtils::insertCharactersIntoVector(localeChars, &localeCodePoints); 46 47 const int keyCount = env->GetArrayLength(attributeKeyStringArray); 48 const int valueCount = env->GetArrayLength(attributeValueStringArray); 49 if (keyCount != valueCount) { 50 return false; 51 } 52 DictionaryHeaderStructurePolicy::AttributeMap attributeMap = 53 JniDataUtils::constructAttributeMap(env, attributeKeyStringArray, 54 attributeValueStringArray); 55 return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion), 56 localeCodePoints, &attributeMap); 57 } 58 59 static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jclass clazz, 60 jintArray before, jintArray after, jint score) { 61 jsize beforeLength = env->GetArrayLength(before); 62 jsize afterLength = env->GetArrayLength(after); 63 int beforeCodePoints[beforeLength]; 64 int afterCodePoints[afterLength]; 65 env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints); 66 env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints); 67 return AutocorrectionThresholdUtils::calcNormalizedScore(beforeCodePoints, beforeLength, 68 afterCodePoints, afterLength, score); 69 } 70 71 static int latinime_BinaryDictionaryUtils_setCurrentTimeForTest(JNIEnv *env, jclass clazz, 72 jint currentTime) { 73 if (currentTime >= 0) { 74 TimeKeeper::startTestModeWithForceCurrentTime(currentTime); 75 } else { 76 TimeKeeper::stopTestMode(); 77 } 78 TimeKeeper::setCurrentTime(); 79 return TimeKeeper::peekCurrentTime(); 80 } 81 82 static const JNINativeMethod sMethods[] = { 83 { 84 const_cast<char *>("createEmptyDictFileNative"), 85 const_cast<char *>( 86 "(Ljava/lang/String;JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)Z"), 87 reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_createEmptyDictFile) 88 }, 89 { 90 const_cast<char *>("calcNormalizedScoreNative"), 91 const_cast<char *>("([I[II)F"), 92 reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_calcNormalizedScore) 93 }, 94 { 95 const_cast<char *>("setCurrentTimeForTestNative"), 96 const_cast<char *>("(I)I"), 97 reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_setCurrentTimeForTest) 98 } 99 }; 100 101 int register_BinaryDictionaryUtils(JNIEnv *env) { 102 const char *const kClassPathName = "com/android/inputmethod/latin/utils/BinaryDictionaryUtils"; 103 return registerNativeMethods(env, kClassPathName, sMethods, NELEMS(sMethods)); 104 } 105 } // namespace latinime 106