Merge "Handle user dict as an ExpandableBinaryDictionary."

main
Keisuke Kuroyanagi 2014-04-30 12:44:25 +00:00 committed by Android (Google) Code Review
commit a9f0a96377
4 changed files with 23 additions and 33 deletions

View File

@ -29,8 +29,8 @@ public final class UserDictionaryCompatUtils {
Context.class, String.class, Integer.TYPE, String.class, Locale.class); Context.class, String.class, Integer.TYPE, String.class, Locale.class);
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static void addWord(final Context context, final String word, final int freq, public static void addWord(final Context context, final String word,
final String shortcut, final Locale locale) { final int freq, final String shortcut, final Locale locale) {
if (hasNewerAddWord()) { if (hasNewerAddWord()) {
CompatUtils.invoke(Words.class, null, METHOD_addWord, context, word, freq, shortcut, CompatUtils.invoke(Words.class, null, METHOD_addWord, context, word, freq, shortcut,
locale); locale);

View File

@ -49,6 +49,7 @@ public class DictionaryFacilitatorForSuggest {
private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140; private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140;
private Dictionaries mDictionaries = new Dictionaries(); private Dictionaries mDictionaries = new Dictionaries();
private boolean mIsUserDictEnabled = false;
private volatile CountDownLatch mLatchForWaitingLoadingMainDictionary = new CountDownLatch(0); private volatile CountDownLatch mLatchForWaitingLoadingMainDictionary = new CountDownLatch(0);
// To synchronize assigning mDictionaries to ensure closing dictionaries. // To synchronize assigning mDictionaries to ensure closing dictionaries.
private Object mLock = new Object(); private Object mLock = new Object();
@ -71,24 +72,21 @@ public class DictionaryFacilitatorForSuggest {
CollectionUtils.newConcurrentHashMap(); CollectionUtils.newConcurrentHashMap();
public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap = public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap =
CollectionUtils.newConcurrentHashMap(); CollectionUtils.newConcurrentHashMap();
// TODO: Remove sub dictionary members and use mSubDictMap.
public final UserBinaryDictionary mUserDictionary;
public Dictionaries() { public Dictionaries() {
mLocale = null; mLocale = null;
mUserDictionary = null;
} }
public Dictionaries(final Locale locale, final Dictionary mainDict, public Dictionaries(final Locale locale, final Dictionary mainDict,
final ExpandableBinaryDictionary contactsDict, final UserBinaryDictionary userDict, final ExpandableBinaryDictionary contactsDict,
final ExpandableBinaryDictionary userDict,
final ExpandableBinaryDictionary userHistoryDict, final ExpandableBinaryDictionary userHistoryDict,
final ExpandableBinaryDictionary personalizationDict) { final ExpandableBinaryDictionary personalizationDict) {
mLocale = locale; mLocale = locale;
// Main dictionary can be asynchronously loaded. // Main dictionary can be asynchronously loaded.
setMainDict(mainDict); setMainDict(mainDict);
setSubDict(Dictionary.TYPE_CONTACTS, contactsDict); setSubDict(Dictionary.TYPE_CONTACTS, contactsDict);
mUserDictionary = userDict; setSubDict(Dictionary.TYPE_USER, userDict);
setSubDict(Dictionary.TYPE_USER, mUserDictionary);
setSubDict(Dictionary.TYPE_USER_HISTORY, userHistoryDict); setSubDict(Dictionary.TYPE_USER_HISTORY, userHistoryDict);
setSubDict(Dictionary.TYPE_PERSONALIZATION, personalizationDict); setSubDict(Dictionary.TYPE_PERSONALIZATION, personalizationDict);
} }
@ -176,11 +174,12 @@ public class DictionaryFacilitatorForSuggest {
} }
// Open or move user dictionary. // Open or move user dictionary.
final UserBinaryDictionary newUserDictionary; final ExpandableBinaryDictionary newUserDictionary;
if (!closeUserDictionary && mDictionaries.hasDict(Dictionary.TYPE_USER)) { if (!closeUserDictionary && mDictionaries.hasDict(Dictionary.TYPE_USER)) {
newUserDictionary = mDictionaries.mUserDictionary; newUserDictionary = mDictionaries.getSubDict(Dictionary.TYPE_USER);
} else { } else {
newUserDictionary = new UserBinaryDictionary(context, newLocale); newUserDictionary = new UserBinaryDictionary(context, newLocale);
mIsUserDictEnabled = UserBinaryDictionary.isEnabled(context);
} }
// Open or move user history dictionary. // Open or move user history dictionary.
@ -364,19 +363,15 @@ public class DictionaryFacilitatorForSuggest {
} }
public boolean isUserDictionaryEnabled() { public boolean isUserDictionaryEnabled() {
final UserBinaryDictionary userDictionary = mDictionaries.mUserDictionary; return mIsUserDictEnabled;
if (userDictionary == null) {
return false;
}
return userDictionary.mEnabled;
} }
public void addWordToUserDictionary(String word) { public void addWordToUserDictionary(final Context context, final String word) {
final UserBinaryDictionary userDictionary = mDictionaries.mUserDictionary; final Locale locale = getLocale();
if (userDictionary == null) { if (locale == null) {
return; return;
} }
userDictionary.addWordToUserDictionary(word); UserBinaryDictionary.addWordToUserDictionary(context, locale, word);
} }
public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized, public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized,

View File

@ -1179,7 +1179,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else { } else {
wordToEdit = word; wordToEdit = word;
} }
mInputLogic.mSuggest.mDictionaryFacilitator.addWordToUserDictionary(wordToEdit); mInputLogic.mSuggest.mDictionaryFacilitator.addWordToUserDictionary(
this /* context */, wordToEdit);
} }
// Callback for the {@link SuggestionStripView}, to call when the important notice strip is // Callback for the {@link SuggestionStripView}, to call when the important notice strip is

View File

@ -29,7 +29,6 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.compat.UserDictionaryCompatUtils; import com.android.inputmethod.compat.UserDictionaryCompatUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.io.File; import java.io.File;
@ -74,7 +73,6 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
private ContentObserver mObserver; private ContentObserver mObserver;
final private String mLocale; final private String mLocale;
final private boolean mAlsoUseMoreRestrictiveLocales; final private boolean mAlsoUseMoreRestrictiveLocales;
final public boolean mEnabled;
public UserBinaryDictionary(final Context context, final Locale locale) { public UserBinaryDictionary(final Context context, final Locale locale) {
this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */); this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */);
@ -120,7 +118,6 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
} }
}; };
cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
mEnabled = readIsEnabled();
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
} }
@ -198,8 +195,8 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
} }
} }
private boolean readIsEnabled() { public static boolean isEnabled(final Context context) {
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = context.getContentResolver();
final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI); final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI);
if (client != null) { if (client != null) {
client.release(); client.release();
@ -212,18 +209,15 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
/** /**
* Adds a word to the user dictionary and makes it persistent. * Adds a word to the user dictionary and makes it persistent.
* *
* @param context the context
* @param locale the locale
* @param word the word to add. If the word is capitalized, then the dictionary will * @param word the word to add. If the word is capitalized, then the dictionary will
* recognize it as a capitalized word when searched. * recognize it as a capitalized word when searched.
*/ */
public synchronized void addWordToUserDictionary(final String word) { public static void addWordToUserDictionary(final Context context, final Locale locale,
final String word) {
// Update the user dictionary provider // Update the user dictionary provider
final Locale locale; UserDictionaryCompatUtils.addWord(context, word,
if (USER_DICTIONARY_ALL_LANGUAGES == mLocale) {
locale = null;
} else {
locale = LocaleUtils.constructLocaleFromString(mLocale);
}
UserDictionaryCompatUtils.addWord(mContext, word,
HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale); HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale);
} }