Merge "Add language suffix to main dictionary"

main
Tadashi G. Takaoka 2012-04-11 00:21:47 -07:00 committed by Android (Google) Code Review
commit 9d95a99626
6 changed files with 73 additions and 89 deletions

View File

@ -17,11 +17,8 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;

View File

@ -20,11 +20,8 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetFileDescriptor; import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@ -155,14 +152,8 @@ class BinaryDictionaryGetter {
* Returns a file address from a resource, or null if it cannot be opened. * Returns a file address from a resource, or null if it cannot be opened.
*/ */
private static AssetFileAddress loadFallbackResource(final Context context, private static AssetFileAddress loadFallbackResource(final Context context,
final int fallbackResId, final Locale locale) { final int fallbackResId) {
final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() { final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId);
@Override
protected AssetFileDescriptor job(Resources res) {
return res.openRawResourceFd(fallbackResId);
}
};
final AssetFileDescriptor afd = job.runInLocale(context.getResources(), locale);
if (afd == null) { if (afd == null) {
Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="
+ fallbackResId); + fallbackResId);
@ -299,8 +290,7 @@ class BinaryDictionaryGetter {
} }
if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) { if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId, final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
locale);
if (null != fallbackAsset) { if (null != fallbackAsset) {
fileList.add(fallbackAsset); fileList.add(fallbackAsset);
} }

View File

@ -21,8 +21,6 @@ import android.content.res.AssetFileDescriptor;
import android.content.res.Resources; import android.content.res.Resources;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
@ -101,13 +99,7 @@ public class DictionaryFactory {
final int resId, final Locale locale) { final int resId, final Locale locale) {
AssetFileDescriptor afd = null; AssetFileDescriptor afd = null;
try { try {
final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() { afd = context.getResources().openRawResourceFd(resId);
@Override
protected AssetFileDescriptor job(Resources res) {
return res.openRawResourceFd(resId);
}
};
afd = job.runInLocale(context.getResources(), locale);
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);
return null; return null;
@ -163,41 +155,31 @@ public class DictionaryFactory {
* @return whether a (non-placeholder) dictionary is available or not. * @return whether a (non-placeholder) dictionary is available or not.
*/ */
public static boolean isDictionaryAvailable(Context context, Locale locale) { public static boolean isDictionaryAvailable(Context context, Locale locale) {
final RunInLocale<Boolean> job = new RunInLocale<Boolean>() { final Resources res = context.getResources();
@Override final int resourceId = getMainDictionaryResourceId(res, locale);
protected Boolean job(Resources res) { final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
final int resourceId = getMainDictionaryResourceId(res); final boolean hasDictionary = isFullDictionary(afd);
final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); try {
final boolean hasDictionary = isFullDictionary(afd); if (null != afd) afd.close();
try { } catch (java.io.IOException e) {
if (null != afd) afd.close(); /* Um, what can we do here exactly? */
} catch (java.io.IOException e) { }
/* Um, what can we do here exactly? */ return hasDictionary;
}
return hasDictionary;
}
};
return job.runInLocale(context.getResources(), locale);
} }
// TODO: Do not use the size of the dictionary as an unique dictionary ID. // TODO: Do not use the size of the dictionary as an unique dictionary ID.
public static Long getDictionaryId(final Context context, final Locale locale) { public static Long getDictionaryId(final Context context, final Locale locale) {
final RunInLocale<Long> job = new RunInLocale<Long>() { final Resources res = context.getResources();
@Override final int resourceId = getMainDictionaryResourceId(res, locale);
protected Long job(Resources res) { final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
final int resourceId = getMainDictionaryResourceId(res); final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH)
final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); ? afd.getLength()
final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH) : null;
? afd.getLength() try {
: null; if (null != afd) afd.close();
try { } catch (java.io.IOException e) {
if (null != afd) afd.close(); }
} catch (java.io.IOException e) { return size;
}
return size;
}
};
return job.runInLocale(context.getResources(), locale);
} }
// TODO: Find the Right Way to find out whether the resource is a placeholder or not. // 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); 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 * Returns a main dictionary resource id
* @param locale dictionary locale
* @return main dictionary resource id * @return main dictionary resource id
*/ */
public static int getMainDictionaryResourceId(Resources res) { public static int getMainDictionaryResourceId(Resources res, Locale locale) {
final String MAIN_DIC_NAME = "main"; final String packageName = LatinIME.class.getPackage().getName();
String packageName = LatinIME.class.getPackage().getName(); int resId;
return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
// 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);
} }
} }

View File

@ -493,37 +493,30 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale();
final Context context = this; final ContactsDictionary oldContactsDictionary;
final RunInLocale<Void> job = new RunInLocale<Void>() { if (mSuggest != null) {
@Override oldContactsDictionary = mSuggest.getContactsDictionary();
protected Void job(Resources res) { mSuggest.close();
final ContactsDictionary oldContactsDictionary; } else {
if (mSuggest != null) { oldContactsDictionary = null;
oldContactsDictionary = mSuggest.getContactsDictionary(); }
mSuggest.close();
} else {
oldContactsDictionary = null;
}
int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(res); final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
mSuggest = new Suggest(context, mainDicResId, keyboardLocale); mResources, keyboardLocale);
if (mSettingsValues.mAutoCorrectEnabled) { mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold); if (mSettingsValues.mAutoCorrectEnabled) {
} mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold);
}
mUserDictionary = new UserDictionary(context, localeStr); mUserDictionary = new UserDictionary(this, localeStr);
mSuggest.setUserDictionary(mUserDictionary); mSuggest.setUserDictionary(mUserDictionary);
mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); mIsUserDictionaryAvailable = mUserDictionary.isEnabled();
resetContactsDictionary(oldContactsDictionary); resetContactsDictionary(oldContactsDictionary);
mUserHistoryDictionary mUserHistoryDictionary = new UserHistoryDictionary(
= new UserHistoryDictionary(context, localeStr, Suggest.DIC_USER_HISTORY); this, localeStr, Suggest.DIC_USER_HISTORY);
mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
return null;
}
};
job.runInLocale(mResources, keyboardLocale);
} }
/** /**
@ -560,7 +553,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
/* package private */ void resetSuggestMainDict() { /* package private */ void resetSuggestMainDict() {
final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale(); final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale();
int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(mResources); int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
mResources, keyboardLocale);
mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale);
} }

View File

@ -38,6 +38,7 @@ public class WhitelistDictionary extends ExpandableDictionary {
// TODO: Conform to the async load contact of ExpandableDictionary // TODO: Conform to the async load contact of ExpandableDictionary
public WhitelistDictionary(final Context context, final Locale locale) { public WhitelistDictionary(final Context context, final Locale locale) {
super(context, Suggest.DIC_WHITELIST); super(context, Suggest.DIC_WHITELIST);
// TODO: Move whitelist dictionary into main dictionary.
final RunInLocale<Void> job = new RunInLocale<Void>() { final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override @Override
protected Void job(Resources res) { protected Void job(Resources res) {

View File

@ -387,7 +387,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService
final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo( final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
SpellCheckerProximityInfo.getProximityForScript(script)); SpellCheckerProximityInfo.getProximityForScript(script));
final Resources resources = getResources(); final Resources resources = getResources();
final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(resources); final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(
resources, locale);
final DictionaryCollection dictionaryCollection = final DictionaryCollection dictionaryCollection =
DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId, DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId,
true /* useFullEditDistance */); true /* useFullEditDistance */);