diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index cc7540e4e..2d958e17d 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -17,11 +17,8 @@ package com.android.inputmethod.latin; import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.Resources; import com.android.inputmethod.keyboard.ProximityInfo; -import com.android.inputmethod.latin.LocaleUtils.RunInLocale; import java.util.Arrays; import java.util.Locale; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index e4d839690..3fbe70f1b 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -20,11 +20,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetFileDescriptor; -import android.content.res.Resources; import android.util.Log; -import com.android.inputmethod.latin.LocaleUtils.RunInLocale; - import java.io.File; import java.util.ArrayList; import java.util.Locale; @@ -155,14 +152,8 @@ class BinaryDictionaryGetter { * Returns a file address from a resource, or null if it cannot be opened. */ private static AssetFileAddress loadFallbackResource(final Context context, - final int fallbackResId, final Locale locale) { - final RunInLocale job = new RunInLocale() { - @Override - protected AssetFileDescriptor job(Resources res) { - return res.openRawResourceFd(fallbackResId); - } - }; - final AssetFileDescriptor afd = job.runInLocale(context.getResources(), locale); + final int fallbackResId) { + final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId); if (afd == null) { Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" + fallbackResId); @@ -299,8 +290,7 @@ class BinaryDictionaryGetter { } if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) { - final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId, - 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 fedb45407..490a32794 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -21,8 +21,6 @@ import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.util.Log; -import com.android.inputmethod.latin.LocaleUtils.RunInLocale; - import java.io.File; import java.util.ArrayList; import java.util.LinkedList; @@ -101,13 +99,7 @@ public class DictionaryFactory { final int resId, final Locale locale) { AssetFileDescriptor afd = null; try { - final RunInLocale job = new RunInLocale() { - @Override - protected AssetFileDescriptor job(Resources res) { - return res.openRawResourceFd(resId); - } - }; - afd = job.runInLocale(context.getResources(), locale); + afd = context.getResources().openRawResourceFd(resId); if (afd == null) { Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); return null; @@ -163,41 +155,31 @@ public class DictionaryFactory { * @return whether a (non-placeholder) dictionary is available or not. */ public static boolean isDictionaryAvailable(Context context, Locale locale) { - final RunInLocale job = new RunInLocale() { - @Override - protected Boolean job(Resources res) { - final int resourceId = getMainDictionaryResourceId(res); - final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); - final boolean hasDictionary = isFullDictionary(afd); - try { - if (null != afd) afd.close(); - } catch (java.io.IOException e) { - /* Um, what can we do here exactly? */ - } - return hasDictionary; - } - }; - return job.runInLocale(context.getResources(), locale); + final Resources res = context.getResources(); + final int resourceId = getMainDictionaryResourceId(res, locale); + final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); + final boolean hasDictionary = isFullDictionary(afd); + try { + if (null != afd) afd.close(); + } catch (java.io.IOException e) { + /* Um, what can we do here exactly? */ + } + return hasDictionary; } // TODO: Do not use the size of the dictionary as an unique dictionary ID. public static Long getDictionaryId(final Context context, final Locale locale) { - final RunInLocale job = new RunInLocale() { - @Override - protected Long job(Resources res) { - final int resourceId = getMainDictionaryResourceId(res); - final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); - final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH) - ? afd.getLength() - : null; - try { - if (null != afd) afd.close(); - } catch (java.io.IOException e) { - } - return size; - } - }; - return job.runInLocale(context.getResources(), locale); + final Resources res = context.getResources(); + final int resourceId = getMainDictionaryResourceId(res, locale); + final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); + final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH) + ? afd.getLength() + : null; + try { + if (null != afd) afd.close(); + } catch (java.io.IOException e) { + } + return size; } // TODO: Find the Right Way to find out whether the resource is a placeholder or not. @@ -214,13 +196,32 @@ public class DictionaryFactory { return (afd != null && afd.getLength() > PLACEHOLDER_LENGTH); } + private static final String DEFAULT_MAIN_DICT = "main"; + private static final String MAIN_DICT_PREFIX = "main_"; + /** * Returns a main dictionary resource id + * @param locale dictionary locale * @return main dictionary resource id */ - public static int getMainDictionaryResourceId(Resources res) { - final String MAIN_DIC_NAME = "main"; - String packageName = LatinIME.class.getPackage().getName(); - return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName); + public static int getMainDictionaryResourceId(Resources res, Locale locale) { + final String packageName = LatinIME.class.getPackage().getName(); + int resId; + + // Try to find main_language_country dictionary. + if (!locale.getCountry().isEmpty()) { + final String dictLanguageCountry = MAIN_DICT_PREFIX + locale.toString().toLowerCase(); + if ((resId = res.getIdentifier(dictLanguageCountry, "raw", packageName)) != 0) { + return resId; + } + } + + // Try to find main_language dictionary. + final String dictLanguage = MAIN_DICT_PREFIX + locale.getLanguage(); + if ((resId = res.getIdentifier(dictLanguage, "raw", packageName)) != 0) { + return resId; + } + + return res.getIdentifier(DEFAULT_MAIN_DICT, "raw", packageName); } } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index e0fa2f838..e16be2c97 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -493,37 +493,30 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); - final Context context = this; - final RunInLocale job = new RunInLocale() { - @Override - protected Void job(Resources res) { - final ContactsDictionary oldContactsDictionary; - if (mSuggest != null) { - oldContactsDictionary = mSuggest.getContactsDictionary(); - mSuggest.close(); - } else { - oldContactsDictionary = null; - } + final ContactsDictionary oldContactsDictionary; + if (mSuggest != null) { + oldContactsDictionary = mSuggest.getContactsDictionary(); + mSuggest.close(); + } else { + oldContactsDictionary = null; + } - int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(res); - mSuggest = new Suggest(context, mainDicResId, keyboardLocale); - if (mSettingsValues.mAutoCorrectEnabled) { - mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); - } + final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId( + mResources, keyboardLocale); + mSuggest = new Suggest(this, mainDicResId, keyboardLocale); + if (mSettingsValues.mAutoCorrectEnabled) { + mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); + } - mUserDictionary = new UserDictionary(context, localeStr); - mSuggest.setUserDictionary(mUserDictionary); - mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); + mUserDictionary = new UserDictionary(this, localeStr); + mSuggest.setUserDictionary(mUserDictionary); + mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); - resetContactsDictionary(oldContactsDictionary); + resetContactsDictionary(oldContactsDictionary); - mUserHistoryDictionary - = new UserHistoryDictionary(context, localeStr, Suggest.DIC_USER_HISTORY); - mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); - return null; - } - }; - job.runInLocale(mResources, keyboardLocale); + mUserHistoryDictionary = new UserHistoryDictionary( + this, localeStr, Suggest.DIC_USER_HISTORY); + mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); } /** @@ -560,7 +553,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen /* package private */ void resetSuggestMainDict() { final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); - int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(mResources); + int mainDicResId = DictionaryFactory.getMainDictionaryResourceId( + mResources, keyboardLocale); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); } diff --git a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java index 7bb307662..bb3ba8651 100644 --- a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java +++ b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java @@ -38,6 +38,7 @@ public class WhitelistDictionary extends ExpandableDictionary { // TODO: Conform to the async load contact of ExpandableDictionary public WhitelistDictionary(final Context context, final Locale locale) { super(context, Suggest.DIC_WHITELIST); + // TODO: Move whitelist dictionary into main dictionary. final RunInLocale job = new RunInLocale() { @Override protected Void job(Resources res) { diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 97296147f..6e4ee3143 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -387,7 +387,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo( SpellCheckerProximityInfo.getProximityForScript(script)); final Resources resources = getResources(); - final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(resources); + final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId( + resources, locale); final DictionaryCollection dictionaryCollection = DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId, true /* useFullEditDistance */);