From 927aff72bc9f0e59450ec6278fbcc05ba6442f25 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 4 Feb 2014 21:39:44 +0900 Subject: [PATCH] Add jni interface to get header information from native. Bug: 12810574 Change-Id: I2508483bcb3157f04cc1fe13f24f864d301f1f28 --- .../inputmethod/latin/BinaryDictionary.java | 38 +++++++++++++++++-- ...oid_inputmethod_latin_BinaryDictionary.cpp | 16 ++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 6e0bd1b66..80a27e23f 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -22,6 +22,10 @@ import android.util.SparseArray; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.makedict.DictionaryHeader; +import com.android.inputmethod.latin.makedict.FormatSpec; +import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions; +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.settings.NativeSuggestOptions; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.JniUtils; @@ -32,6 +36,7 @@ import com.android.inputmethod.latin.utils.WordProperty; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -137,6 +142,9 @@ public final class BinaryDictionary extends Dictionary { String[] attributeKeyStringArray, String[] attributeValueStringArray); private static native long openNative(String sourceDir, long dictOffset, long dictSize, boolean isUpdatable); + private static native void getHeaderInfoNative(long dict, int[] outHeaderSize, + int[] outFormatVersion, ArrayList outAttributeKeys, + ArrayList outAttributeValues); private static native void flushNative(long dict, String filePath); private static native boolean needsToRunGCNative(long dict, boolean mindsBlockByGC); private static native void flushWithGCNative(long dict, String filePath); @@ -170,7 +178,6 @@ public final class BinaryDictionary extends Dictionary { private static native int setCurrentTimeForTestNative(int currentTime); private static native String getPropertyNative(long dict, String query); - @UsedForTesting public static boolean createEmptyDictFile(final String filePath, final long dictVersion, final Map attributeMap) { final String[] keyArray = new String[attributeMap.size()]; @@ -190,6 +197,33 @@ public final class BinaryDictionary extends Dictionary { mNativeDict = openNative(path, startOffset, length, isUpdatable); } + @UsedForTesting + public DictionaryHeader getHeader() throws UnsupportedFormatException { + if (mNativeDict == 0) { + return null; + } + final int[] outHeaderSize = new int[1]; + final int[] outFormatVersion = new int[1]; + final ArrayList outAttributeKeys = CollectionUtils.newArrayList(); + final ArrayList outAttributeValues = CollectionUtils.newArrayList(); + getHeaderInfoNative(mNativeDict, outHeaderSize, outFormatVersion, outAttributeKeys, + outAttributeValues); + final HashMap attributes = new HashMap(); + for (int i = 0; i < outAttributeKeys.size(); i++) { + final String attributeKey = StringUtils.getStringFromNullTerminatedCodePointArray( + outAttributeKeys.get(i)); + final String attributeValue = StringUtils.getStringFromNullTerminatedCodePointArray( + outAttributeValues.get(i)); + attributes.put(attributeKey, attributeValue); + } + final boolean hasHistoricalInfo = + attributes.get(DictionaryHeader.HAS_HISTORICAL_INFO_KEY).equals( + DictionaryHeader.ATTRIBUTE_VALUE_TRUE); + return new DictionaryHeader(outHeaderSize[0], new DictionaryOptions(attributes), + new FormatSpec.FormatOptions(outFormatVersion[0], hasHistoricalInfo)); + } + + @Override public ArrayList getSuggestions(final WordComposer composer, final String prevWord, final ProximityInfo proximityInfo, @@ -307,7 +341,6 @@ public final class BinaryDictionary extends Dictionary { return getBigramProbabilityNative(mNativeDict, codePoints0, codePoints1); } - @UsedForTesting public WordProperty getWordProperty(final String word) { if (TextUtils.isEmpty(word)) { return null; @@ -347,7 +380,6 @@ public final class BinaryDictionary extends Dictionary { * Method to iterate all words in the dictionary for makedict. * If token is 0, this method newly starts iterating the dictionary. */ - @UsedForTesting public GetNextWordPropertyResult getNextWordProperty(final int token) { final int[] codePoints = new int[MAX_WORD_LENGTH]; final int nextToken = getNextWordNative(mNativeDict, token, codePoints); diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index c919ebd91..4372cbeb5 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -137,6 +137,17 @@ static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dic delete dictionary; } +static void latinime_BinaryDictionary_getHeaderInfo(JNIEnv *env, jclass clazz, jlong dict, + jintArray outHeaderSize, jintArray outFormatVersion, jobject outAttributeKeys, + jobject outAttributeValues) { + Dictionary *dictionary = reinterpret_cast(dict); + if (!dictionary) return; + const int formatVersion = dictionary->getFormatVersionNumber(); + env->SetIntArrayRegion(outFormatVersion, 0 /* start */, 1 /* len */, &formatVersion); + // TODO: Implement + return; +} + static int latinime_BinaryDictionary_getFormatVersion(JNIEnv *env, jclass clazz, jlong dict) { Dictionary *dictionary = reinterpret_cast(dict); if (!dictionary) return 0; @@ -510,6 +521,11 @@ static const JNINativeMethod sMethods[] = { const_cast("(J)I"), reinterpret_cast(latinime_BinaryDictionary_getFormatVersion) }, + { + const_cast("getHeaderInfoNative"), + const_cast("(J[I[ILjava/util/ArrayList;Ljava/util/ArrayList;)V"), + reinterpret_cast(latinime_BinaryDictionary_getHeaderInfo) + }, { const_cast("flushNative"), const_cast("(JLjava/lang/String;)V"),