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