aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorKen Wakasa <kwakasa@google.com>2010-04-26 19:10:14 +0900
committerKen Wakasa <kwakasa@google.com>2010-04-27 08:56:56 +0900
commit4606367f98a30c97715e9b7f312d5f9b97d0a053 (patch)
tree34aeed012242180f12a4597ef54baa50aa152b30 /java/src
parent9c025c46c9a86b9e77b3bd4c3d66413e6d1decaf (diff)
downloadlatinime-4606367f98a30c97715e9b7f312d5f9b97d0a053.tar.gz
latinime-4606367f98a30c97715e9b7f312d5f9b97d0a053.tar.xz
latinime-4606367f98a30c97715e9b7f312d5f9b97d0a053.zip
Get rid of dependency on native AssetManager API. Confirmed the native code builds with the NDK r3.
Change-Id: I6a9a5bb4129e9269d74348801436c9e5e0058da5
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionary.java41
1 files changed, 34 insertions, 7 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index 6d0257b82..9fcd9e50c 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -16,6 +16,11 @@
package com.android.inputmethod.latin;
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.Channels;
import java.util.Arrays;
import android.content.Context;
@@ -27,6 +32,7 @@ import android.util.Log;
*/
public class BinaryDictionary extends Dictionary {
+ private static final String TAG = "BinaryDictionary";
public static final int MAX_WORD_LENGTH = 48;
private static final int MAX_ALTERNATIVES = 16;
private static final int MAX_WORDS = 16;
@@ -35,16 +41,19 @@ public class BinaryDictionary extends Dictionary {
private static final boolean ENABLE_MISSED_CHARACTERS = true;
private int mNativeDict;
- private int mDictLength; // This value is set from native code, don't change the name!!!!
+ private int mDictLength;
private int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES];
private char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
private int[] mFrequencies = new int[MAX_WORDS];
+ // Keep a reference to the native dict direct buffer in Java to avoid
+ // unexpected deallocation of the direct buffer.
+ private ByteBuffer mNativeDictDirectBuffer;
static {
try {
System.loadLibrary("jni_latinime2");
} catch (UnsatisfiedLinkError ule) {
- Log.e("BinaryDictionary", "Could not load native library jni_latinime");
+ Log.e("BinaryDictionary", "Could not load native library jni_latinime2");
}
}
@@ -59,8 +68,7 @@ public class BinaryDictionary extends Dictionary {
}
}
- private native int openNative(AssetManager am, String resourcePath, int typedLetterMultiplier,
- int fullWordMultiplier);
+ private native int openNative(ByteBuffer bb, int typedLetterMultiplier, int fullWordMultiplier);
private native void closeNative(int dict);
private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
@@ -69,9 +77,28 @@ public class BinaryDictionary extends Dictionary {
int[] nextLettersFrequencies, int nextLettersSize);
private final void loadDictionary(Context context, int resId) {
- AssetManager am = context.getResources().getAssets();
- String assetName = context.getResources().getString(resId);
- mNativeDict = openNative(am, assetName, TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER);
+ InputStream is = context.getResources().openRawResource(resId);
+ try {
+ int avail = is.available();
+ ByteBuffer mNativeDictDirectBuffer =
+ ByteBuffer.allocateDirect(avail).order(ByteOrder.nativeOrder());
+ int got = Channels.newChannel(is).read(mNativeDictDirectBuffer);
+ if (got != avail) {
+ Log.e(TAG, "Read " + got + " bytes, expected " + avail);
+ } else {
+ mNativeDict = openNative(mNativeDictDirectBuffer,
+ TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER);
+ mDictLength = avail;
+ }
+ } catch (IOException e) {
+ Log.w(TAG, "No available size for binary dictionary");
+ } finally {
+ try {
+ is.close();
+ } catch (IOException e) {
+ Log.w(TAG, "Failed to close input stream");
+ }
+ }
}
@Override