Clean up: Update variable names to comply with spec of ApplicationInfo.
ApplicationInfo.sourceDir may or may not be apk file name. It can be a directory as well. The spec just says it's "Full path to the location of this package". Also, added error handling in loadDictionary(). Change-Id: I5e64d0aba4b1ec7634f4b3ac5537e7a774433ecemain
parent
7a42a46069
commit
90d96615bc
|
@ -55,7 +55,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
try {
|
try {
|
||||||
System.loadLibrary("jni_latinime");
|
System.loadLibrary("jni_latinime");
|
||||||
} catch (UnsatisfiedLinkError ule) {
|
} catch (UnsatisfiedLinkError ule) {
|
||||||
Log.e("BinaryDictionary", "Could not load native library jni_latinime");
|
Log.e(TAG, "Could not load native library jni_latinime");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
mDicTypeId = dicTypeId;
|
mDicTypeId = dicTypeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
private native int openNative(String apkFileName, long dictOffset, long dictSize,
|
private native int openNative(String sourceDir, long dictOffset, long dictSize,
|
||||||
int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength,
|
int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength,
|
||||||
int maxWords, int maxAlternatives);
|
int maxWords, int maxAlternatives);
|
||||||
private native void closeNative(int dict);
|
private native void closeNative(int dict);
|
||||||
|
@ -84,12 +84,21 @@ public class BinaryDictionary extends Dictionary {
|
||||||
int maxWordLength, int maxBigrams, int maxAlternatives);
|
int maxWordLength, int maxBigrams, int maxAlternatives);
|
||||||
|
|
||||||
private final void loadDictionary(Context context, int resId) {
|
private final void loadDictionary(Context context, int resId) {
|
||||||
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
|
try {
|
||||||
mNativeDict = openNative(context.getApplicationInfo().sourceDir,
|
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
|
||||||
afd.getStartOffset(), afd.getLength(),
|
if (afd == null) {
|
||||||
TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
|
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
||||||
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
|
return;
|
||||||
mDictLength = afd.getLength();
|
}
|
||||||
|
mNativeDict = openNative(context.getApplicationInfo().sourceDir,
|
||||||
|
afd.getStartOffset(), afd.getLength(),
|
||||||
|
TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
|
||||||
|
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
|
||||||
|
mDictLength = afd.getLength();
|
||||||
|
} catch (android.content.res.Resources.NotFoundException e) {
|
||||||
|
Log.e(TAG, "Could not find the resource. resId=" + resId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -165,7 +174,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getSize() {
|
public long getSize() {
|
||||||
return mDictLength; // This value is initialized on the call to openNative()
|
return mDictLength; // This value is initialized in loadDictionary()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -32,7 +32,7 @@ LOCAL_MODULE := libjni_latinime
|
||||||
LOCAL_MODULE_TAGS := user
|
LOCAL_MODULE_TAGS := user
|
||||||
|
|
||||||
ifeq ($(FLAG_DBG), true)
|
ifeq ($(FLAG_DBG), true)
|
||||||
$(warning "Making debug build.")
|
$(warning Making debug version of native library)
|
||||||
LOCAL_CFLAGS += -DFLAG_DBG
|
LOCAL_CFLAGS += -DFLAG_DBG
|
||||||
LOCAL_SHARED_LIBRARIES := libcutils libutils
|
LOCAL_SHARED_LIBRARIES := libcutils libutils
|
||||||
endif
|
endif
|
||||||
|
|
|
@ -50,14 +50,14 @@ static void throwException(JNIEnv *env, const char* ex, const char* fmt, int dat
|
||||||
}
|
}
|
||||||
|
|
||||||
static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
|
static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
|
||||||
jstring apkFileName, jlong dictOffset, jlong dictSize,
|
jstring sourceDir, jlong dictOffset, jlong dictSize,
|
||||||
jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
|
jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
|
||||||
jint maxAlternatives) {
|
jint maxAlternatives) {
|
||||||
PROF_OPEN;
|
PROF_OPEN;
|
||||||
PROF_START(66);
|
PROF_START(66);
|
||||||
const char *apkFileNameChars = env->GetStringUTFChars(apkFileName, NULL);
|
const char *sourceDirChars = env->GetStringUTFChars(sourceDir, NULL);
|
||||||
if (apkFileNameChars == NULL) {
|
if (sourceDirChars == NULL) {
|
||||||
LOGE("DICT: Can't get apk file name");
|
LOGE("DICT: Can't get sourceDir string");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int fd = 0;
|
int fd = 0;
|
||||||
|
@ -65,9 +65,9 @@ static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
|
||||||
int adjust = 0;
|
int adjust = 0;
|
||||||
#ifdef USE_MMAP_FOR_DICTIONARY
|
#ifdef USE_MMAP_FOR_DICTIONARY
|
||||||
/* mmap version */
|
/* mmap version */
|
||||||
fd = open(apkFileNameChars, O_RDONLY);
|
fd = open(sourceDirChars, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LOGE("DICT: Can't open apk file. errno=%d", errno);
|
LOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int pagesize = getpagesize();
|
int pagesize = getpagesize();
|
||||||
|
@ -76,16 +76,16 @@ static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
|
||||||
int adjDictSize = dictSize + adjust;
|
int adjDictSize = dictSize + adjust;
|
||||||
dictBuf = mmap(NULL, sizeof(char) * adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
|
dictBuf = mmap(NULL, sizeof(char) * adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
|
||||||
if (dictBuf == MAP_FAILED) {
|
if (dictBuf == MAP_FAILED) {
|
||||||
LOGE("DICT: Can't mmap dictionary file. errno=%d", errno);
|
LOGE("DICT: Can't mmap dictionary. errno=%d", errno);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
dictBuf = (void *)((char *)dictBuf + adjust);
|
dictBuf = (void *)((char *)dictBuf + adjust);
|
||||||
#else // USE_MMAP_FOR_DICTIONARY
|
#else // USE_MMAP_FOR_DICTIONARY
|
||||||
/* malloc version */
|
/* malloc version */
|
||||||
FILE *file = NULL;
|
FILE *file = NULL;
|
||||||
file = fopen(apkFileNameChars, "rb");
|
file = fopen(sourceDirChars, "rb");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
LOGE("DICT: Can't fopen apk file. errno=%d", errno);
|
LOGE("DICT: Can't fopen sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
dictBuf = malloc(sizeof(char) * dictSize);
|
dictBuf = malloc(sizeof(char) * dictSize);
|
||||||
|
@ -109,7 +109,7 @@ static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif // USE_MMAP_FOR_DICTIONARY
|
#endif // USE_MMAP_FOR_DICTIONARY
|
||||||
env->ReleaseStringUTFChars(apkFileName, apkFileNameChars);
|
env->ReleaseStringUTFChars(sourceDir, sourceDirChars);
|
||||||
|
|
||||||
if (!dictBuf) {
|
if (!dictBuf) {
|
||||||
LOGE("DICT: dictBuf is null");
|
LOGE("DICT: dictBuf is null");
|
||||||
|
|
Loading…
Reference in New Issue