mirror of
https://gitlab.futo.org/keyboard/latinime.git
synced 2024-09-28 14:54:30 +01:00
Add unit test helper method to BinaryDictionary and Suggest
Bug: 3414081 Change-Id: Idee64010f2f423d3c7c548d0279c7bf287088762
This commit is contained in:
parent
39c323eb12
commit
33e0b1e79e
@ -20,6 +20,7 @@ import android.content.Context;
|
|||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,9 +73,40 @@ public class BinaryDictionary extends Dictionary {
|
|||||||
public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
|
public static BinaryDictionary initDictionary(Context context, int resId, int dicTypeId) {
|
||||||
synchronized (sInstance) {
|
synchronized (sInstance) {
|
||||||
sInstance.closeInternal();
|
sInstance.closeInternal();
|
||||||
if (resId != 0) {
|
try {
|
||||||
sInstance.loadDictionary(context, resId);
|
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
|
||||||
|
if (afd == null) {
|
||||||
|
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final String sourceDir = context.getApplicationInfo().sourceDir;
|
||||||
|
final File packagePath = new File(sourceDir);
|
||||||
|
// TODO: Come up with a way to handle a directory.
|
||||||
|
if (!packagePath.isFile()) {
|
||||||
|
Log.e(TAG, "sourceDir is not a file: " + sourceDir);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
sInstance.loadDictionary(sourceDir, afd.getStartOffset(), afd.getLength());
|
||||||
sInstance.mDicTypeId = dicTypeId;
|
sInstance.mDicTypeId = dicTypeId;
|
||||||
|
} catch (android.content.res.Resources.NotFoundException e) {
|
||||||
|
Log.e(TAG, "Could not find the resource. resId=" + resId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For unit test
|
||||||
|
/* package */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
|
||||||
|
long length, int dicTypeId) {
|
||||||
|
synchronized (sInstance) {
|
||||||
|
sInstance.closeInternal();
|
||||||
|
if (dictionary.isFile()) {
|
||||||
|
sInstance.loadDictionary(dictionary.getAbsolutePath(), startOffset, length);
|
||||||
|
sInstance.mDicTypeId = dicTypeId;
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sInstance;
|
return sInstance;
|
||||||
@ -92,22 +124,11 @@ public class BinaryDictionary extends Dictionary {
|
|||||||
int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
|
int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
|
||||||
int maxWordLength, int maxBigrams, int maxAlternatives);
|
int maxWordLength, int maxBigrams, int maxAlternatives);
|
||||||
|
|
||||||
private final void loadDictionary(Context context, int resId) {
|
private final void loadDictionary(String path, long startOffset, long length) {
|
||||||
try {
|
mNativeDict = openNative(path, startOffset, length,
|
||||||
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
|
|
||||||
if (afd == null) {
|
|
||||||
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
mNativeDict = openNative(context.getApplicationInfo().sourceDir,
|
|
||||||
afd.getStartOffset(), afd.getLength(),
|
|
||||||
TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
|
TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
|
||||||
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
|
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
|
||||||
mDictLength = afd.getLength();
|
mDictLength = length;
|
||||||
} catch (android.content.res.Resources.NotFoundException e) {
|
|
||||||
Log.e(TAG, "Could not find the resource. resId=" + resId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -22,6 +22,7 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@ -109,6 +110,12 @@ public class Suggest implements Dictionary.WordCallback {
|
|||||||
initPool();
|
initPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For unit test
|
||||||
|
/* package */ Suggest(File dictionary, long startOffset, long length) {
|
||||||
|
mMainDict = BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN);
|
||||||
|
initPool();
|
||||||
|
}
|
||||||
|
|
||||||
private void initPool() {
|
private void initPool() {
|
||||||
for (int i = 0; i < mPrefMaxSuggestions; i++) {
|
for (int i = 0; i < mPrefMaxSuggestions; i++) {
|
||||||
StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
|
StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user