Don't auto-correct when there is no main dictionary.

Bug: 6502485
Change-Id: I5803b0db8552867110587a1140fc03fc2e0eccef
This commit is contained in:
Jean Chalard 2012-05-16 15:08:45 +09:00
parent 2bbdac539a
commit f0e12a9699
4 changed files with 30 additions and 24 deletions

View file

@ -70,6 +70,10 @@ public class DictionaryCollection extends Dictionary {
return false; return false;
} }
public boolean isEmpty() {
return mDictionaries.isEmpty();
}
@Override @Override
public void close() { public void close() {
for (final Dictionary dict : mDictionaries) for (final Dictionary dict : mDictionaries)

View file

@ -36,7 +36,7 @@ public class DictionaryFactory {
DictionaryFactory.class.getPackage().getName(); DictionaryFactory.class.getPackage().getName();
/** /**
* Initializes a dictionary from a dictionary pack, with explicit flags. * Initializes a main dictionary collection 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 the built-in dictionary - if any. * locale. If none is found, it falls back to the built-in dictionary - if any.
@ -45,7 +45,7 @@ public class DictionaryFactory {
* @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 createMainDictionaryFromManager(final Context context,
final Locale locale, 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");
@ -73,7 +73,7 @@ public class DictionaryFactory {
} }
/** /**
* Initializes a dictionary from a dictionary pack, with default flags. * Initializes a main dictionary collection 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 the built-in dictionary, if any. * locale. If none is found, it falls back to the built-in dictionary, if any.
@ -81,9 +81,9 @@ public class DictionaryFactory {
* @param locale the locale for which to create the dictionary * @param locale the locale for which to create the dictionary
* @return an initialized instance of DictionaryCollection * @return an initialized instance of DictionaryCollection
*/ */
public static DictionaryCollection createDictionaryFromManager(final Context context, public static DictionaryCollection createMainDictionaryFromManager(final Context context,
final Locale locale) { final Locale locale) {
return createDictionaryFromManager(context, locale, false /* useFullEditDistance */); return createMainDictionaryFromManager(context, locale, false /* useFullEditDistance */);
} }
/** /**

View file

@ -79,7 +79,7 @@ public class Suggest implements Dictionary.WordCallback {
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
private Dictionary mMainDict; private boolean mHasMainDictionary;
private Dictionary mContactsDict; private Dictionary mContactsDict;
private WhitelistDictionary mWhiteListDictionary; private WhitelistDictionary mWhiteListDictionary;
private final HashMap<String, Dictionary> mUnigramDictionaries = private final HashMap<String, Dictionary> mUnigramDictionaries =
@ -110,8 +110,12 @@ public class Suggest implements Dictionary.WordCallback {
/* package for test */ Suggest(final Context context, final File dictionary, /* package for test */ Suggest(final Context context, final File dictionary,
final long startOffset, final long length, final Locale locale) { final long startOffset, final long length, final Locale locale) {
initSynchronously(context, DictionaryFactory.createDictionaryForTest(context, dictionary, final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(context, dictionary,
startOffset, length /* useFullEditDistance */, false, locale), locale); startOffset, length /* useFullEditDistance */, false, locale);
mHasMainDictionary = null != mainDict;
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
initWhitelistAndAutocorrectAndPool(context, locale);
} }
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) { private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
@ -127,14 +131,6 @@ public class Suggest implements Dictionary.WordCallback {
initWhitelistAndAutocorrectAndPool(context, locale); initWhitelistAndAutocorrectAndPool(context, locale);
} }
private void initSynchronously(final Context context, final Dictionary mainDict,
final Locale locale) {
mMainDict = mainDict;
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict);
initWhitelistAndAutocorrectAndPool(context, locale);
}
private static void addOrReplaceDictionary(HashMap<String, Dictionary> dictionaries, String key, private static void addOrReplaceDictionary(HashMap<String, Dictionary> dictionaries, String key,
Dictionary dict) { Dictionary dict) {
final Dictionary oldDict = (dict == null) final Dictionary oldDict = (dict == null)
@ -146,13 +142,13 @@ public class Suggest implements Dictionary.WordCallback {
} }
public void resetMainDict(final Context context, final Locale locale) { public void resetMainDict(final Context context, final Locale locale) {
mMainDict = null; mHasMainDictionary = false;
new Thread("InitializeBinaryDictionary") { new Thread("InitializeBinaryDictionary") {
@Override @Override
public void run() { public void run() {
final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( final DictionaryCollection newMainDict =
context, locale); DictionaryFactory.createMainDictionaryFromManager(context, locale);
mMainDict = newMainDict; mHasMainDictionary = null != newMainDict && !newMainDict.isEmpty();
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
} }
@ -162,7 +158,7 @@ public class Suggest implements Dictionary.WordCallback {
// The main dictionary could have been loaded asynchronously. Don't cache the return value // The main dictionary could have been loaded asynchronously. Don't cache the return value
// of this method. // of this method.
public boolean hasMainDictionary() { public boolean hasMainDictionary() {
return mMainDict != null; return mHasMainDictionary;
} }
public Dictionary getContactsDictionary() { public Dictionary getContactsDictionary() {
@ -376,7 +372,13 @@ public class Suggest implements Dictionary.WordCallback {
// a boolean flag. Right now this is handled with a slight hack in // a boolean flag. Right now this is handled with a slight hack in
// WhitelistDictionary#shouldForciblyAutoCorrectFrom. // WhitelistDictionary#shouldForciblyAutoCorrectFrom.
final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected( final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized()); getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized())
// If we don't have a main dictionary, we never want to auto-correct. The reason for this
// is, the user may have a contact whose name happens to match a valid word in their
// language, and it will unexpectedly auto-correct. For example, if the user types in
// English with no dictionary and has a "Will" in their contact list, "will" would
// always auto-correct to "Will" which is unwanted. Hence, no main dict => no auto-correct.
&& mHasMainDictionary;
boolean autoCorrectionAvailable = hasAutoCorrection; boolean autoCorrectionAvailable = hasAutoCorrection;
if (correctionMode == CORRECTION_FULL || correctionMode == CORRECTION_FULL_BIGRAM) { if (correctionMode == CORRECTION_FULL || correctionMode == CORRECTION_FULL_BIGRAM) {
@ -563,7 +565,7 @@ public class Suggest implements Dictionary.WordCallback {
for (final Dictionary dictionary : dictionaries) { for (final Dictionary dictionary : dictionaries) {
dictionary.close(); dictionary.close();
} }
mMainDict = null; mHasMainDictionary = false;
} }
// TODO: Resolve the inconsistencies between the native auto correction algorithms and // TODO: Resolve the inconsistencies between the native auto correction algorithms and

View file

@ -398,7 +398,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService
SpellCheckerProximityInfo.PROXIMITY_GRID_WIDTH, SpellCheckerProximityInfo.PROXIMITY_GRID_WIDTH,
SpellCheckerProximityInfo.PROXIMITY_GRID_HEIGHT); SpellCheckerProximityInfo.PROXIMITY_GRID_HEIGHT);
final DictionaryCollection dictionaryCollection = final DictionaryCollection dictionaryCollection =
DictionaryFactory.createDictionaryFromManager(this, locale, DictionaryFactory.createMainDictionaryFromManager(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);