From f0e12a969974987f1b97929886c6ebe6a685c538 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 16 May 2012 15:08:45 +0900 Subject: [PATCH] Don't auto-correct when there is no main dictionary. Bug: 6502485 Change-Id: I5803b0db8552867110587a1140fc03fc2e0eccef --- .../latin/DictionaryCollection.java | 4 ++ .../inputmethod/latin/DictionaryFactory.java | 10 ++--- .../android/inputmethod/latin/Suggest.java | 38 ++++++++++--------- .../AndroidSpellCheckerService.java | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index 37deb0c5d..f3aa27a22 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -70,6 +70,10 @@ public class DictionaryCollection extends Dictionary { return false; } + public boolean isEmpty() { + return mDictionaries.isEmpty(); + } + @Override public void close() { for (final Dictionary dict : mDictionaries) diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index f5dc7b34a..4cd1b3883 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -36,7 +36,7 @@ public class DictionaryFactory { 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 * 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 * @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) { if (null == locale) { 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 * 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 * @return an initialized instance of DictionaryCollection */ - public static DictionaryCollection createDictionaryFromManager(final Context context, + public static DictionaryCollection createMainDictionaryFromManager(final Context context, final Locale locale) { - return createDictionaryFromManager(context, locale, false /* useFullEditDistance */); + return createMainDictionaryFromManager(context, locale, false /* useFullEditDistance */); } /** diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 845df81f6..c4877316e 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -79,7 +79,7 @@ public class Suggest implements Dictionary.WordCallback { private static final boolean DBG = LatinImeLogger.sDBG; - private Dictionary mMainDict; + private boolean mHasMainDictionary; private Dictionary mContactsDict; private WhitelistDictionary mWhiteListDictionary; private final HashMap mUnigramDictionaries = @@ -110,8 +110,12 @@ public class Suggest implements Dictionary.WordCallback { /* package for test */ Suggest(final Context context, final File dictionary, final long startOffset, final long length, final Locale locale) { - initSynchronously(context, DictionaryFactory.createDictionaryForTest(context, dictionary, - startOffset, length /* useFullEditDistance */, false, locale), locale); + final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(context, dictionary, + 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) { @@ -127,14 +131,6 @@ public class Suggest implements Dictionary.WordCallback { 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 dictionaries, String key, Dictionary dict) { final Dictionary oldDict = (dict == null) @@ -146,13 +142,13 @@ public class Suggest implements Dictionary.WordCallback { } public void resetMainDict(final Context context, final Locale locale) { - mMainDict = null; + mHasMainDictionary = false; new Thread("InitializeBinaryDictionary") { @Override public void run() { - final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( - context, locale); - mMainDict = newMainDict; + final DictionaryCollection newMainDict = + DictionaryFactory.createMainDictionaryFromManager(context, locale); + mHasMainDictionary = null != newMainDict && !newMainDict.isEmpty(); addOrReplaceDictionary(mUnigramDictionaries, 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 // of this method. public boolean hasMainDictionary() { - return mMainDict != null; + return mHasMainDictionary; } 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 // WhitelistDictionary#shouldForciblyAutoCorrectFrom. 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; if (correctionMode == CORRECTION_FULL || correctionMode == CORRECTION_FULL_BIGRAM) { @@ -563,7 +565,7 @@ public class Suggest implements Dictionary.WordCallback { for (final Dictionary dictionary : dictionaries) { dictionary.close(); } - mMainDict = null; + mHasMainDictionary = false; } // TODO: Resolve the inconsistencies between the native auto correction algorithms and diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 6f7f0c333..0c9f9fb27 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -398,7 +398,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService SpellCheckerProximityInfo.PROXIMITY_GRID_WIDTH, SpellCheckerProximityInfo.PROXIMITY_GRID_HEIGHT); final DictionaryCollection dictionaryCollection = - DictionaryFactory.createDictionaryFromManager(this, locale, + DictionaryFactory.createMainDictionaryFromManager(this, locale, true /* useFullEditDistance */); final String localeStr = locale.toString(); Dictionary userDictionary = mUserDictionaries.get(localeStr);