From e6269759d642eac0a03ae6942acb5cd556e7ff46 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 11 Apr 2012 21:02:26 +0900 Subject: [PATCH] Read the dictionary resource in a more sensical place. We don't need to pass this down all the way from LatinIME any more. It fetched be done exactly where it needs to be. Change-Id: I9f277f9c4f9de70ae755a1334d86c67bbb24c988 --- .../latin/BinaryDictionaryGetter.java | 6 +++-- .../inputmethod/latin/DictionaryFactory.java | 24 ++++++++----------- .../android/inputmethod/latin/LatinIME.java | 9 ++----- .../android/inputmethod/latin/Suggest.java | 14 +++++------ .../AndroidSpellCheckerService.java | 5 +--- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 3fbe70f1b..b0c2adc79 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -255,13 +255,13 @@ class BinaryDictionaryGetter { * - Uses a content provider to get a public dictionary set, as per the protocol described * in BinaryDictionaryFileDumper. * If that fails: - * - Gets a file name from the fallback resource passed as an argument. + * - Gets a file name from the built-in dictionary for this locale, if any. * If that fails: * - Returns null. * @return The list of addresses of valid dictionary files, or null. */ public static ArrayList getDictionaryFiles(final Locale locale, - final Context context, final int fallbackResId) { + final Context context) { // cacheWordListsFromContentProvider returns the list of files it copied to local // storage, but we don't really care about what was copied NOW: what we want is the @@ -290,6 +290,8 @@ class BinaryDictionaryGetter { } if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) { + final int fallbackResId = + DictionaryFactory.getMainDictionaryResourceId(context.getResources(), locale); final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId); if (null != fallbackAsset) { fileList.add(fallbackAsset); diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index 490a32794..bf05f3bc3 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -36,24 +36,22 @@ public class DictionaryFactory { * Initializes a dictionary from a dictionary pack, with explicit flags. * * This searches for a content provider providing a dictionary pack for the specified - * locale. If none is found, it falls back to using the resource passed as fallBackResId - * as a dictionary. + * locale. If none is found, it falls back to the built-in dictionary - if any. * @param context application context for reading resources * @param locale the locale for which to create the dictionary - * @param fallbackResId the id of the resource to use as a fallback if no pack is found * @param useFullEditDistance whether to use the full edit distance in suggestions * @return an initialized instance of DictionaryCollection */ public static DictionaryCollection createDictionaryFromManager(final Context context, - final Locale locale, final int fallbackResId, final boolean useFullEditDistance) { + final Locale locale, final boolean useFullEditDistance) { if (null == locale) { Log.e(TAG, "No locale defined for dictionary"); - return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, locale)); + return new DictionaryCollection(createBinaryDictionary(context, locale)); } final LinkedList dictList = new LinkedList(); final ArrayList assetFileList = - BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId); + BinaryDictionaryGetter.getDictionaryFiles(locale, context); if (null != assetFileList) { for (final AssetFileAddress f : assetFileList) { final BinaryDictionary binaryDictionary = @@ -75,17 +73,14 @@ public class DictionaryFactory { * Initializes a dictionary from a dictionary pack, with default flags. * * This searches for a content provider providing a dictionary pack for the specified - * locale. If none is found, it falls back to using the resource passed as fallBackResId - * as a dictionary. + * locale. If none is found, it falls back to the built-in dictionary, if any. * @param context application context for reading resources * @param locale the locale for which to create the dictionary - * @param fallbackResId the id of the resource to use as a fallback if no pack is found * @return an initialized instance of DictionaryCollection */ public static DictionaryCollection createDictionaryFromManager(final Context context, - final Locale locale, final int fallbackResId) { - return createDictionaryFromManager(context, locale, fallbackResId, - false /* useFullEditDistance */); + final Locale locale) { + return createDictionaryFromManager(context, locale, false /* useFullEditDistance */); } /** @@ -96,9 +91,10 @@ public class DictionaryFactory { * @return an initialized instance of BinaryDictionary */ protected static BinaryDictionary createBinaryDictionary(final Context context, - final int resId, final Locale locale) { + final Locale locale) { AssetFileDescriptor afd = null; try { + final int resId = getMainDictionaryResourceId(context.getResources(), locale); afd = context.getResources().openRawResourceFd(resId); if (afd == null) { Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); @@ -115,7 +111,7 @@ public class DictionaryFactory { return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(), false /* useFullEditDistance */, locale); } catch (android.content.res.Resources.NotFoundException e) { - Log.e(TAG, "Could not find the resource. resId=" + resId); + Log.e(TAG, "Could not find the resource"); return null; } finally { if (null != afd) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index e16be2c97..7cdeef897 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -501,9 +501,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen oldContactsDictionary = null; } - final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId( - mResources, keyboardLocale); - mSuggest = new Suggest(this, mainDicResId, keyboardLocale); + mSuggest = new Suggest(this, keyboardLocale); if (mSettingsValues.mAutoCorrectEnabled) { mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); } @@ -552,10 +550,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } /* package private */ void resetSuggestMainDict() { - final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); - int mainDicResId = DictionaryFactory.getMainDictionaryResourceId( - mResources, keyboardLocale); - mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); + mSuggest.resetMainDict(this, mSubtypeSwitcher.getInputLocale()); } @Override diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index fa6664b1a..c3f3bd598 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -104,8 +104,8 @@ public class Suggest implements Dictionary.WordCallback { private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4; - public Suggest(final Context context, final int dictionaryResId, final Locale locale) { - initAsynchronously(context, dictionaryResId, locale); + public Suggest(final Context context, final Locale locale) { + initAsynchronously(context, locale); } /* package for test */ Suggest(final Context context, final File dictionary, @@ -119,9 +119,8 @@ public class Suggest implements Dictionary.WordCallback { addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary); } - private void initAsynchronously(final Context context, final int dictionaryResId, - final Locale locale) { - resetMainDict(context, dictionaryResId, locale); + private void initAsynchronously(final Context context, final Locale locale) { + resetMainDict(context, locale); // TODO: read the whitelist and init the pool asynchronously too. // initPool should be done asynchronously now that the pool is thread-safe. @@ -146,14 +145,13 @@ public class Suggest implements Dictionary.WordCallback { } } - public void resetMainDict(final Context context, final int dictionaryResId, - final Locale locale) { + public void resetMainDict(final Context context, final Locale locale) { mMainDict = null; new Thread("InitializeBinaryDictionary") { @Override public void run() { final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( - context, locale, dictionaryResId); + context, locale); mMainDict = newMainDict; addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 6e4ee3143..576fbe696 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -386,11 +386,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService final int script = getScriptFromLocale(locale); final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo( SpellCheckerProximityInfo.getProximityForScript(script)); - final Resources resources = getResources(); - final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId( - resources, locale); final DictionaryCollection dictionaryCollection = - DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId, + DictionaryFactory.createDictionaryFromManager(this, locale, true /* useFullEditDistance */); final String localeStr = locale.toString(); Dictionary userDictionary = mUserDictionaries.get(localeStr);