Merge "Load the binary dictionary asynchronously."
This commit is contained in:
commit
a48f8b3764
1 changed files with 35 additions and 15 deletions
|
@ -102,6 +102,8 @@ public class Suggest implements Dictionary.WordCallback {
|
||||||
|
|
||||||
private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>();
|
private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>();
|
||||||
ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
|
ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
|
||||||
|
// TODO: maybe this should be synchronized, it's quite scary as it is.
|
||||||
|
// TODO: if it becomes synchronized, also move initPool in the thread in initAsynchronously
|
||||||
private ArrayList<CharSequence> mStringPool = new ArrayList<CharSequence>();
|
private ArrayList<CharSequence> mStringPool = new ArrayList<CharSequence>();
|
||||||
private CharSequence mTypedWord;
|
private CharSequence mTypedWord;
|
||||||
|
|
||||||
|
@ -111,27 +113,39 @@ public class Suggest implements Dictionary.WordCallback {
|
||||||
|
|
||||||
private int mCorrectionMode = CORRECTION_BASIC;
|
private int mCorrectionMode = CORRECTION_BASIC;
|
||||||
|
|
||||||
public Suggest(Context context, int dictionaryResId, Locale locale) {
|
public Suggest(final Context context, final int dictionaryResId, final Locale locale) {
|
||||||
init(context, DictionaryFactory.createDictionaryFromManager(context, locale,
|
initAsynchronously(context, dictionaryResId, locale);
|
||||||
dictionaryResId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
|
/* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
|
||||||
Flag[] flagArray) {
|
Flag[] flagArray) {
|
||||||
init(null, DictionaryFactory.createDictionaryForTest(context, dictionary, startOffset,
|
initSynchronously(null, DictionaryFactory.createDictionaryForTest(context, dictionary,
|
||||||
length, flagArray));
|
startOffset, length, flagArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(Context context, Dictionary mainDict) {
|
private void initWhitelistAndAutocorrectAndPool(final Context context) {
|
||||||
mMainDict = mainDict;
|
|
||||||
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
|
|
||||||
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
|
|
||||||
mWhiteListDictionary = WhitelistDictionary.init(context);
|
mWhiteListDictionary = WhitelistDictionary.init(context);
|
||||||
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
|
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
|
||||||
mAutoCorrection = new AutoCorrection();
|
mAutoCorrection = new AutoCorrection();
|
||||||
initPool();
|
initPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initAsynchronously(final Context context, final int dictionaryResId,
|
||||||
|
final Locale locale) {
|
||||||
|
resetMainDict(context, dictionaryResId, locale);
|
||||||
|
|
||||||
|
// TODO: read the whitelist and init the pool asynchronously too.
|
||||||
|
// initPool should be done asynchronously but the pool is not thread-safe at the moment.
|
||||||
|
initWhitelistAndAutocorrectAndPool(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initSynchronously(Context context, Dictionary mainDict) {
|
||||||
|
mMainDict = mainDict;
|
||||||
|
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
|
||||||
|
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
|
||||||
|
initWhitelistAndAutocorrectAndPool(context);
|
||||||
|
}
|
||||||
|
|
||||||
private void addOrReplaceDictionary(Map<String, Dictionary> dictionaries, String key,
|
private void addOrReplaceDictionary(Map<String, Dictionary> dictionaries, String key,
|
||||||
Dictionary dict) {
|
Dictionary dict) {
|
||||||
final Dictionary oldDict = (dict == null)
|
final Dictionary oldDict = (dict == null)
|
||||||
|
@ -142,13 +156,19 @@ public class Suggest implements Dictionary.WordCallback {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetMainDict(Context context, int dictionaryResId, Locale locale) {
|
public void resetMainDict(final Context context, final int dictionaryResId,
|
||||||
|
final Locale locale) {
|
||||||
|
mMainDict = null;
|
||||||
|
new Thread("InitializeBinaryDictionary") {
|
||||||
|
public void run() {
|
||||||
final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
|
final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
|
||||||
context, locale, dictionaryResId);
|
context, locale, dictionaryResId);
|
||||||
mMainDict = newMainDict;
|
mMainDict = newMainDict;
|
||||||
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
|
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
|
||||||
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
|
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
|
||||||
}
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
private void initPool() {
|
private void initPool() {
|
||||||
for (int i = 0; i < mPrefMaxSuggestions; i++) {
|
for (int i = 0; i < mPrefMaxSuggestions; i++) {
|
||||||
|
|
Loading…
Reference in a new issue