Make the whitelist an instanciable class.
This goes together with I6b8628b9acc32449e4147a2a754b222fbb76c754 or it will break the build Bug: 5402436 Change-Id: I07c6266b713773a8de80bb22afdd4c566261f78a
This commit is contained in:
parent
afb9076503
commit
3458d61807
6 changed files with 32 additions and 32 deletions
|
@ -113,14 +113,15 @@ public class Suggest implements Dictionary.WordCallback {
|
|||
initAsynchronously(context, dictionaryResId, locale);
|
||||
}
|
||||
|
||||
/* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
|
||||
Flag[] flagArray) {
|
||||
/* package for test */ Suggest(final Context context, final File dictionary,
|
||||
final long startOffset, final long length, final Flag[] flagArray,
|
||||
final Locale locale) {
|
||||
initSynchronously(null, DictionaryFactory.createDictionaryForTest(context, dictionary,
|
||||
startOffset, length, flagArray));
|
||||
startOffset, length, flagArray), locale);
|
||||
}
|
||||
|
||||
private void initWhitelistAndAutocorrectAndPool(final Context context) {
|
||||
mWhiteListDictionary = WhitelistDictionary.init(context);
|
||||
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
|
||||
mWhiteListDictionary = new WhitelistDictionary(context, locale);
|
||||
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
|
||||
mAutoCorrection = new AutoCorrection();
|
||||
StringBuilderPool.ensureCapacity(mPrefMaxSuggestions, getApproxMaxWordLength());
|
||||
|
@ -132,14 +133,15 @@ public class Suggest implements Dictionary.WordCallback {
|
|||
|
||||
// TODO: read the whitelist and init the pool asynchronously too.
|
||||
// initPool should be done asynchronously now that the pool is thread-safe.
|
||||
initWhitelistAndAutocorrectAndPool(context);
|
||||
initWhitelistAndAutocorrectAndPool(context, locale);
|
||||
}
|
||||
|
||||
private void initSynchronously(Context context, Dictionary mainDict) {
|
||||
private void initSynchronously(final Context context, final Dictionary mainDict,
|
||||
final Locale locale) {
|
||||
mMainDict = mainDict;
|
||||
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
|
||||
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
|
||||
initWhitelistAndAutocorrectAndPool(context);
|
||||
initWhitelistAndAutocorrectAndPool(context, locale);
|
||||
}
|
||||
|
||||
private void addOrReplaceDictionary(Map<String, Dictionary> dictionaries, String key,
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package com.android.inputmethod.latin;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
@ -24,6 +25,7 @@ import android.util.Pair;
|
|||
import com.android.inputmethod.keyboard.ProximityInfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WhitelistDictionary extends Dictionary {
|
||||
|
||||
|
@ -33,22 +35,13 @@ public class WhitelistDictionary extends Dictionary {
|
|||
private final HashMap<String, Pair<Integer, String>> mWhitelistWords =
|
||||
new HashMap<String, Pair<Integer, String>>();
|
||||
|
||||
private static final WhitelistDictionary sInstance = new WhitelistDictionary();
|
||||
|
||||
private WhitelistDictionary() {
|
||||
}
|
||||
|
||||
public static WhitelistDictionary init(Context context) {
|
||||
synchronized (sInstance) {
|
||||
public WhitelistDictionary(final Context context, final Locale locale) {
|
||||
final Resources res = context.getResources();
|
||||
final Locale previousLocale = LocaleUtils.setSystemLocale(res, locale);
|
||||
if (context != null) {
|
||||
// Wordlist is initialized by the proper language in Suggestion.java#init
|
||||
sInstance.initWordlist(
|
||||
context.getResources().getStringArray(R.array.wordlist_whitelist));
|
||||
} else {
|
||||
sInstance.mWhitelistWords.clear();
|
||||
initWordlist(context.getResources().getStringArray(R.array.wordlist_whitelist));
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
LocaleUtils.setSystemLocale(res, previousLocale);
|
||||
}
|
||||
|
||||
private void initWordlist(String[] wordlist) {
|
||||
|
|
|
@ -25,6 +25,7 @@ import com.android.inputmethod.keyboard.KeyboardId;
|
|||
import com.android.inputmethod.keyboard.LatinKeyboard;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Locale;
|
||||
|
||||
public class SuggestHelper {
|
||||
protected final Suggest mSuggest;
|
||||
|
@ -40,9 +41,10 @@ public class SuggestHelper {
|
|||
init();
|
||||
}
|
||||
|
||||
protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length,
|
||||
KeyboardId keyboardId) {
|
||||
mSuggest = new Suggest(context, dictionaryPath, startOffset, length, null);
|
||||
protected SuggestHelper(final Context context, final File dictionaryPath,
|
||||
final long startOffset, final long length, final KeyboardId keyboardId,
|
||||
final Locale locale) {
|
||||
mSuggest = new Suggest(context, dictionaryPath, startOffset, length, null, locale);
|
||||
mKeyboard = new LatinKeyboard.Builder(context).load(keyboardId).build();
|
||||
mKeyDetector = new KeyDetector(0);
|
||||
init();
|
||||
|
|
|
@ -30,9 +30,10 @@ public class SuggestTests extends SuggestTestsBase {
|
|||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
final AssetFileDescriptor dict = openTestRawResourceFd(R.raw.test);
|
||||
final Locale locale = Locale.US;
|
||||
mHelper = new SuggestHelper(
|
||||
getContext(), mTestPackageFile, dict.getStartOffset(), dict.getLength(),
|
||||
createKeyboardId(Locale.US, Configuration.ORIENTATION_PORTRAIT));
|
||||
createKeyboardId(locale, Configuration.ORIENTATION_PORTRAIT), locale);
|
||||
mHelper.setCorrectionMode(Suggest.CORRECTION_FULL_BIGRAM);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,11 +29,12 @@ public class UserBigramSuggestHelper extends SuggestHelper {
|
|||
private final Context mContext;
|
||||
private UserBigramDictionary mUserBigram;
|
||||
|
||||
public UserBigramSuggestHelper(Context context, File dictionaryPath, long startOffset,
|
||||
long length, int userBigramMax, int userBigramDelete, KeyboardId keyboardId) {
|
||||
super(context, dictionaryPath, startOffset, length, keyboardId);
|
||||
public UserBigramSuggestHelper(final Context context, final File dictionaryPath,
|
||||
final long startOffset, final long length, final int userBigramMax,
|
||||
final int userBigramDelete, final KeyboardId keyboardId, final Locale locale) {
|
||||
super(context, dictionaryPath, startOffset, length, keyboardId, locale);
|
||||
mContext = context;
|
||||
mUserBigram = new UserBigramDictionary(context, null, Locale.US.toString(),
|
||||
mUserBigram = new UserBigramDictionary(context, null, locale.toString(),
|
||||
Suggest.DIC_USER);
|
||||
mUserBigram.setDatabaseMax(userBigramMax);
|
||||
mUserBigram.setDatabaseDelete(userBigramDelete);
|
||||
|
|
|
@ -33,10 +33,11 @@ public class UserBigramSuggestTests extends SuggestTestsBase {
|
|||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
final AssetFileDescriptor dict = openTestRawResourceFd(R.raw.test);
|
||||
final Locale locale = Locale.US;
|
||||
mHelper = new UserBigramSuggestHelper(
|
||||
getContext(), mTestPackageFile, dict.getStartOffset(), dict.getLength(),
|
||||
MAX_DATA, DELETE_DATA,
|
||||
createKeyboardId(Locale.US, Configuration.ORIENTATION_PORTRAIT));
|
||||
createKeyboardId(locale, Configuration.ORIENTATION_PORTRAIT), locale);
|
||||
}
|
||||
|
||||
/************************** Tests ************************/
|
||||
|
|
Loading…
Reference in a new issue