Add language suffix to main dictionary

Bug: 6319377
Change-Id: Ie6a887fefa12e33c17bfeb5d22984e7c1a7bdb46
main
Tadashi G. Takaoka 2012-04-11 14:58:02 +09:00
parent 2be51f4fd0
commit 78ab80844b
6 changed files with 73 additions and 89 deletions

View File

@ -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;

View File

@ -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<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
@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);
}

View File

@ -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<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
@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<Boolean> job = new RunInLocale<Boolean>() {
@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<Long> job = new RunInLocale<Long>() {
@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);
}
}

View File

@ -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<Void> job = new RunInLocale<Void>() {
@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);
}

View File

@ -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<Void> job = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {

View File

@ -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 */);