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
main
Jean Chalard 2012-04-11 21:02:26 +09:00
parent 9d95a99626
commit e6269759d6
5 changed files with 23 additions and 35 deletions

View File

@ -255,13 +255,13 @@ class BinaryDictionaryGetter {
* - Uses a content provider to get a public dictionary set, as per the protocol described * - Uses a content provider to get a public dictionary set, as per the protocol described
* in BinaryDictionaryFileDumper. * in BinaryDictionaryFileDumper.
* If that fails: * 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: * If that fails:
* - Returns null. * - Returns null.
* @return The list of addresses of valid dictionary files, or null. * @return The list of addresses of valid dictionary files, or null.
*/ */
public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale, public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
final Context context, final int fallbackResId) { final Context context) {
// cacheWordListsFromContentProvider returns the list of files it copied to local // 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 // 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)) { if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
final int fallbackResId =
DictionaryFactory.getMainDictionaryResourceId(context.getResources(), locale);
final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId); final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
if (null != fallbackAsset) { if (null != fallbackAsset) {
fileList.add(fallbackAsset); fileList.add(fallbackAsset);

View File

@ -36,24 +36,22 @@ public class DictionaryFactory {
* Initializes a dictionary from a dictionary pack, with explicit flags. * Initializes a dictionary from a dictionary pack, with explicit flags.
* *
* This searches for a content provider providing a dictionary pack for the specified * 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 * locale. If none is found, it falls back to the built-in dictionary - if any.
* as a dictionary.
* @param context application context for reading resources * @param context application context for reading resources
* @param locale the locale for which to create the dictionary * @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 * @param useFullEditDistance whether to use the full edit distance in suggestions
* @return an initialized instance of DictionaryCollection * @return an initialized instance of DictionaryCollection
*/ */
public static DictionaryCollection createDictionaryFromManager(final Context context, 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) { if (null == locale) {
Log.e(TAG, "No locale defined for dictionary"); Log.e(TAG, "No locale defined for dictionary");
return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, locale)); return new DictionaryCollection(createBinaryDictionary(context, locale));
} }
final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>(); final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>();
final ArrayList<AssetFileAddress> assetFileList = final ArrayList<AssetFileAddress> assetFileList =
BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId); BinaryDictionaryGetter.getDictionaryFiles(locale, context);
if (null != assetFileList) { if (null != assetFileList) {
for (final AssetFileAddress f : assetFileList) { for (final AssetFileAddress f : assetFileList) {
final BinaryDictionary binaryDictionary = final BinaryDictionary binaryDictionary =
@ -75,17 +73,14 @@ public class DictionaryFactory {
* Initializes a dictionary from a dictionary pack, with default flags. * Initializes a dictionary from a dictionary pack, with default flags.
* *
* This searches for a content provider providing a dictionary pack for the specified * 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 * locale. If none is found, it falls back to the built-in dictionary, if any.
* as a dictionary.
* @param context application context for reading resources * @param context application context for reading resources
* @param locale the locale for which to create the dictionary * @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 * @return an initialized instance of DictionaryCollection
*/ */
public static DictionaryCollection createDictionaryFromManager(final Context context, public static DictionaryCollection createDictionaryFromManager(final Context context,
final Locale locale, final int fallbackResId) { final Locale locale) {
return createDictionaryFromManager(context, locale, fallbackResId, return createDictionaryFromManager(context, locale, false /* useFullEditDistance */);
false /* useFullEditDistance */);
} }
/** /**
@ -96,9 +91,10 @@ public class DictionaryFactory {
* @return an initialized instance of BinaryDictionary * @return an initialized instance of BinaryDictionary
*/ */
protected static BinaryDictionary createBinaryDictionary(final Context context, protected static BinaryDictionary createBinaryDictionary(final Context context,
final int resId, final Locale locale) { final Locale locale) {
AssetFileDescriptor afd = null; AssetFileDescriptor afd = null;
try { try {
final int resId = getMainDictionaryResourceId(context.getResources(), locale);
afd = context.getResources().openRawResourceFd(resId); afd = context.getResources().openRawResourceFd(resId);
if (afd == null) { if (afd == null) {
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); 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(), return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(),
false /* useFullEditDistance */, locale); false /* useFullEditDistance */, locale);
} catch (android.content.res.Resources.NotFoundException e) { } 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; return null;
} finally { } finally {
if (null != afd) { if (null != afd) {

View File

@ -501,9 +501,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
oldContactsDictionary = null; oldContactsDictionary = null;
} }
final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId( mSuggest = new Suggest(this, keyboardLocale);
mResources, keyboardLocale);
mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
if (mSettingsValues.mAutoCorrectEnabled) { if (mSettingsValues.mAutoCorrectEnabled) {
mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold);
} }
@ -552,10 +550,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
/* package private */ void resetSuggestMainDict() { /* package private */ void resetSuggestMainDict() {
final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); mSuggest.resetMainDict(this, mSubtypeSwitcher.getInputLocale());
int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
mResources, keyboardLocale);
mSuggest.resetMainDict(this, mainDicResId, keyboardLocale);
} }
@Override @Override

View File

@ -104,8 +104,8 @@ public class Suggest implements Dictionary.WordCallback {
private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4; private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
public Suggest(final Context context, final int dictionaryResId, final Locale locale) { public Suggest(final Context context, final Locale locale) {
initAsynchronously(context, dictionaryResId, locale); initAsynchronously(context, locale);
} }
/* package for test */ Suggest(final Context context, final File dictionary, /* 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); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
} }
private void initAsynchronously(final Context context, final int dictionaryResId, private void initAsynchronously(final Context context, final Locale locale) {
final Locale locale) { resetMainDict(context, locale);
resetMainDict(context, dictionaryResId, locale);
// 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.
@ -146,14 +145,13 @@ public class Suggest implements Dictionary.WordCallback {
} }
} }
public void resetMainDict(final Context context, final int dictionaryResId, public void resetMainDict(final Context context, final Locale locale) {
final Locale locale) {
mMainDict = null; mMainDict = null;
new Thread("InitializeBinaryDictionary") { new Thread("InitializeBinaryDictionary") {
@Override @Override
public void run() { public void run() {
final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
context, locale, dictionaryResId); context, locale);
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);

View File

@ -386,11 +386,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService
final int script = getScriptFromLocale(locale); final int script = getScriptFromLocale(locale);
final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo( final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
SpellCheckerProximityInfo.getProximityForScript(script)); SpellCheckerProximityInfo.getProximityForScript(script));
final Resources resources = getResources();
final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(
resources, locale);
final DictionaryCollection dictionaryCollection = final DictionaryCollection dictionaryCollection =
DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId, DictionaryFactory.createDictionaryFromManager(this, locale,
true /* useFullEditDistance */); true /* useFullEditDistance */);
final String localeStr = locale.toString(); final String localeStr = locale.toString();
Dictionary userDictionary = mUserDictionaries.get(localeStr); Dictionary userDictionary = mUserDictionaries.get(localeStr);