Get rid of dependency on native AssetManager API. Confirmed the native code builds with the NDK r3.

Change-Id: I6a9a5bb4129e9269d74348801436c9e5e0058da5
main
Ken Wakasa 2010-04-26 19:10:14 +09:00
parent 9c025c46c9
commit 4606367f98
4 changed files with 41 additions and 85 deletions

View File

@ -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

View File

@ -11,15 +11,8 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
LOCAL_LDLIBS := -lm
LOCAL_PRELINK_MODULE := false
LOCAL_SHARED_LIBRARIES := \
libandroid_runtime \
libcutils \
libutils
LOCAL_MODULE := libjni_latinime2
LOCAL_MODULE_TAGS := optional

View File

@ -15,31 +15,18 @@
** limitations under the License.
*/
#define LOG_TAG "BinaryDictionary"
#include "utils/Log.h"
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <nativehelper/jni.h>
#include "utils/AssetManager.h"
#include "utils/Asset.h"
#include <jni.h>
#include "dictionary.h"
// ----------------------------------------------------------------------------
using namespace latinime;
using namespace android;
static jfieldID sDescriptorField;
static jfieldID sAssetManagerNativeField;
static jmethodID sAddWordMethod;
static jfieldID sDictLength;
//
// helper function to throw an exception
//
@ -54,35 +41,15 @@ static void throwException(JNIEnv *env, const char* ex, const char* fmt, int dat
}
static jint latinime_BinaryDictionary_open
(JNIEnv *env, jobject object, jobject assetManager, jstring resourceString,
(JNIEnv *env, jobject object, jobject dictDirectBuffer,
jint typedLetterMultiplier, jint fullWordMultiplier)
{
// Get the native file descriptor from the FileDescriptor object
AssetManager *am = (AssetManager*) env->GetIntField(assetManager, sAssetManagerNativeField);
if (!am) {
LOGE("DICT: Couldn't get AssetManager native peer\n");
return 0;
}
const char *resourcePath = env->GetStringUTFChars(resourceString, NULL);
Asset *dictAsset = am->openNonAsset(resourcePath, Asset::ACCESS_BUFFER);
if (dictAsset == NULL) {
LOGE("DICT: Couldn't get asset %s\n", resourcePath);
env->ReleaseStringUTFChars(resourceString, resourcePath);
return 0;
}
void *dict = (void*) dictAsset->getBuffer(false);
void *dict = env->GetDirectBufferAddress(dictDirectBuffer);
if (dict == NULL) {
LOGE("DICT: Dictionary buffer is null\n");
env->ReleaseStringUTFChars(resourceString, resourcePath);
fprintf(stderr, "DICT: Dictionary buffer is null\n");
return 0;
}
Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier);
dictionary->setAsset(dictAsset);
env->SetIntField(object, sDictLength, (jint) dictAsset->getLength());
env->ReleaseStringUTFChars(resourceString, resourcePath);
return (jint) dictionary;
}
@ -131,14 +98,13 @@ static void latinime_BinaryDictionary_close
(JNIEnv *env, jobject object, jint dict)
{
Dictionary *dictionary = (Dictionary*) dict;
((Asset*) dictionary->getAsset())->close();
delete (Dictionary*) dict;
}
// ----------------------------------------------------------------------------
static JNINativeMethod gMethods[] = {
{"openNative", "(Landroid/content/res/AssetManager;Ljava/lang/String;II)I",
{"openNative", "(Ljava/nio/ByteBuffer;II)I",
(void*)latinime_BinaryDictionary_open},
{"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
{"getSuggestionsNative", "(I[II[C[IIIII[II)I", (void*)latinime_BinaryDictionary_getSuggestions},
@ -167,30 +133,6 @@ static int registerNativeMethods(JNIEnv* env, const char* className,
static int registerNatives(JNIEnv *env)
{
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
jclass clazz;
clazz = env->FindClass("java/io/FileDescriptor");
if (clazz == NULL) {
LOGE("Can't find %s", "java/io/FileDescriptor");
return -1;
}
sDescriptorField = env->GetFieldID(clazz, "descriptor", "I");
clazz = env->FindClass("android/content/res/AssetManager");
if (clazz == NULL) {
LOGE("Can't find %s", "java/io/FileDescriptor");
return -1;
}
sAssetManagerNativeField = env->GetFieldID(clazz, "mObject", "I");
// Get the field pointer for the dictionary length
clazz = env->FindClass(kClassPathName);
if (clazz == NULL) {
LOGE("Can't find %s", kClassPathName);
return -1;
}
sDictLength = env->GetFieldID(clazz, "mDictLength", "I");
return registerNativeMethods(env,
kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
}

View File

@ -19,14 +19,8 @@
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <cutils/log.h>
//#define USE_ASSET_MANAGER
#ifdef USE_ASSET_MANAGER
#include <utils/AssetManager.h>
#include <utils/Asset.h>
#endif
//#include <cutils/log.h>
#define LOGI
#include "dictionary.h"
#include "basechars.h"