From 3af9f05f2916e376f265974c820c369a6c63a780 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 21 Jul 2011 18:01:58 +0900 Subject: [PATCH] Load the binary dictionary asynchronously. This should help with LatinIME opening performance. It should not be merged before Ia9e4741f (if it is, there is a race condition where the wrong dictionary is opened). Bug: 5023141 Change-Id: I39c52de70a449929a622dddfdd92b38425ad9416 --- .../android/inputmethod/latin/Suggest.java | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 8ae653f2f..c2452b947 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -102,6 +102,8 @@ public class Suggest implements Dictionary.WordCallback { private ArrayList mSuggestions = new ArrayList(); ArrayList mBigramSuggestions = new ArrayList(); + // 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 mStringPool = new ArrayList(); private CharSequence mTypedWord; @@ -111,27 +113,39 @@ public class Suggest implements Dictionary.WordCallback { private int mCorrectionMode = CORRECTION_BASIC; - public Suggest(Context context, int dictionaryResId, Locale locale) { - init(context, DictionaryFactory.createDictionaryFromManager(context, locale, - dictionaryResId)); + public Suggest(final Context context, final int dictionaryResId, final Locale locale) { + initAsynchronously(context, dictionaryResId, locale); } /* package for test */ Suggest(Context context, File dictionary, long startOffset, long length, Flag[] flagArray) { - init(null, DictionaryFactory.createDictionaryForTest(context, dictionary, startOffset, - length, flagArray)); + initSynchronously(null, DictionaryFactory.createDictionaryForTest(context, dictionary, + startOffset, length, flagArray)); } - private void init(Context context, Dictionary mainDict) { - mMainDict = mainDict; - addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict); - addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict); + private void initWhitelistAndAutocorrectAndPool(final Context context) { mWhiteListDictionary = WhitelistDictionary.init(context); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary); mAutoCorrection = new AutoCorrection(); 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 dictionaries, String key, Dictionary dict) { final Dictionary oldDict = (dict == null) @@ -142,12 +156,18 @@ public class Suggest implements Dictionary.WordCallback { } } - public void resetMainDict(Context context, int dictionaryResId, Locale locale) { - final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( - context, locale, dictionaryResId); - mMainDict = newMainDict; - addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); - addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); + 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( + context, locale, dictionaryResId); + mMainDict = newMainDict; + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); + } + }.start(); } private void initPool() {