From 1b06b59e28743b713947947437ea5b312477f808 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 24 May 2012 19:30:19 +0900 Subject: [PATCH] Fix a bug with a concurrent modification of a map. Bug: 6538848 Change-Id: I1a8422356d9909213e53cfdc8c5bcc12eb48f19d --- .../inputmethod/latin/AutoCorrection.java | 22 ++++++++++--------- .../android/inputmethod/latin/Suggest.java | 17 +++++++------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java index da1936aef..32b213e67 100644 --- a/java/src/com/android/inputmethod/latin/AutoCorrection.java +++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java @@ -22,7 +22,7 @@ import android.text.TextUtils; import android.util.Log; import java.util.ArrayList; -import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; public class AutoCorrection { private static final boolean DBG = LatinImeLogger.sDBG; @@ -33,10 +33,10 @@ public class AutoCorrection { } public static CharSequence computeAutoCorrectionWord( - HashMap dictionaries, - WordComposer wordComposer, ArrayList suggestions, - CharSequence consideredWord, float autoCorrectionThreshold, - CharSequence whitelistedWord) { + final ConcurrentHashMap dictionaries, + final WordComposer wordComposer, final ArrayList suggestions, + final CharSequence consideredWord, final float autoCorrectionThreshold, + final CharSequence whitelistedWord) { if (hasAutoCorrectionForWhitelistedWord(whitelistedWord)) { return whitelistedWord; } else if (hasAutoCorrectionForConsideredWord( @@ -49,8 +49,8 @@ public class AutoCorrection { return null; } - public static boolean isValidWord( - HashMap dictionaries, CharSequence word, boolean ignoreCase) { + public static boolean isValidWord(final ConcurrentHashMap dictionaries, + CharSequence word, boolean ignoreCase) { if (TextUtils.isEmpty(word)) { return false; } @@ -75,7 +75,8 @@ public class AutoCorrection { } public static boolean allowsToBeAutoCorrected( - HashMap dictionaries, CharSequence word, boolean ignoreCase) { + final ConcurrentHashMap dictionaries, + final CharSequence word, final boolean ignoreCase) { final WhitelistDictionary whitelistDictionary = (WhitelistDictionary)dictionaries.get(Suggest.DICT_KEY_WHITELIST); // If "word" is in the whitelist dictionary, it should not be auto corrected. @@ -91,8 +92,9 @@ public class AutoCorrection { } private static boolean hasAutoCorrectionForConsideredWord( - HashMap dictionaries, WordComposer wordComposer, - ArrayList suggestions, CharSequence consideredWord) { + final ConcurrentHashMap dictionaries, + final WordComposer wordComposer, final ArrayList suggestions, + final CharSequence consideredWord) { if (TextUtils.isEmpty(consideredWord)) return false; return wordComposer.size() > 1 && suggestions.size() > 0 && !allowsToBeAutoCorrected(dictionaries, consideredWord, false); diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index c98a27b64..336a76f4b 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -26,9 +26,9 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.Locale; +import java.util.concurrent.ConcurrentHashMap; /** * This class loads a dictionary and provides a list of suggestions for a given sequence of @@ -68,10 +68,10 @@ public class Suggest implements Dictionary.WordCallback { private boolean mHasMainDictionary; private Dictionary mContactsDict; private WhitelistDictionary mWhiteListDictionary; - private final HashMap mUnigramDictionaries = - new HashMap(); - private final HashMap mBigramDictionaries = - new HashMap(); + private final ConcurrentHashMap mUnigramDictionaries = + new ConcurrentHashMap(); + private final ConcurrentHashMap mBigramDictionaries = + new ConcurrentHashMap(); private int mPrefMaxSuggestions = 18; @@ -117,8 +117,9 @@ public class Suggest implements Dictionary.WordCallback { initWhitelistAndAutocorrectAndPool(context, locale); } - private static void addOrReplaceDictionary(HashMap dictionaries, String key, - Dictionary dict) { + private static void addOrReplaceDictionary( + final ConcurrentHashMap dictionaries, + final String key, final Dictionary dict) { final Dictionary oldDict = (dict == null) ? dictionaries.remove(key) : dictionaries.put(key, dict); @@ -151,7 +152,7 @@ public class Suggest implements Dictionary.WordCallback { return mContactsDict; } - public HashMap getUnigramDictionaries() { + public ConcurrentHashMap getUnigramDictionaries() { return mUnigramDictionaries; }