am c005c1dc: Merge "Use dictType to class map to create sub dicts."

* commit 'c005c1dc565bd02cb1a24d5349a8078ed49dbae9':
  Use dictType to class map to create sub dicts.
main
Keisuke Kuroyanagi 2014-05-01 10:19:51 +00:00 committed by Android Git Automerger
commit 1c08c201c5
5 changed files with 40 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.personalization.AccountUtils;
import com.android.inputmethod.latin.utils.StringUtils;
@ -82,6 +83,7 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
reloadDictionaryIfRequired();
}
@UsedForTesting
public static ContactsBinaryDictionary getDictionary(final Context context, final Locale locale,
final File dictFile) {
return new ContactsBinaryDictionary(context, locale, dictFile);

View File

@ -31,6 +31,8 @@ import com.android.inputmethod.latin.utils.LanguageModelParam;
import com.android.inputmethod.latin.utils.SuggestionResults;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -64,6 +66,20 @@ public class DictionaryFacilitatorForSuggest {
Dictionary.TYPE_CONTACTS
};
private static final Map<String, Class<? extends ExpandableBinaryDictionary>>
DICT_TYPE_TO_CLASS = CollectionUtils.newHashMap();
static {
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER_HISTORY, UserHistoryDictionary.class);
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_PERSONALIZATION, PersonalizationDictionary.class);
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER, UserBinaryDictionary.class);
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_CONTACTS, ContactsBinaryDictionary.class);
}
private static final String DICT_FACTORY_METHOD_NAME = "getDictionary";
private static final Class<?>[] DICT_FACTORY_METHOD_ARG_TYPES =
new Class[] { Context.class, Locale.class, File.class };
private static final String[] SUB_DICT_TYPES =
Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTION, 1 /* start */,
DICT_TYPES_ORDERED_TO_GET_SUGGESTION.length);
@ -145,15 +161,20 @@ public class DictionaryFacilitatorForSuggest {
private static ExpandableBinaryDictionary getSubDict(final String dictType,
final Context context, final Locale locale, final File dictFile) {
if (Dictionary.TYPE_CONTACTS.equals(dictType)) {
return ContactsBinaryDictionary.getDictionary(context, locale, dictFile);
} else if (Dictionary.TYPE_USER.equals(dictType)) {
return UserBinaryDictionary.getDictionary(context, locale, dictFile);
} else if (Dictionary.TYPE_USER_HISTORY.equals(dictType)) {
return UserHistoryDictionary.getDictionary(context, locale, dictFile);
} else if (Dictionary.TYPE_PERSONALIZATION.equals(dictType)) {
return PersonalizationDictionary.getDictionary(context, locale, dictFile);
} else {
final Class<? extends ExpandableBinaryDictionary> dictClass =
DICT_TYPE_TO_CLASS.get(dictType);
if (dictClass == null) {
return null;
}
try {
final Method factoryMethod = dictClass.getMethod(DICT_FACTORY_METHOD_NAME,
DICT_FACTORY_METHOD_ARG_TYPES);
final Object dict = factoryMethod.invoke(null /* obj */,
new Object[] { context, locale, dictFile });
return (ExpandableBinaryDictionary) dict;
} catch (final NoSuchMethodException | SecurityException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
Log.e(TAG, "Cannot create dictionary: " + dictType, e);
return null;
}
}
@ -165,9 +186,10 @@ public class DictionaryFacilitatorForSuggest {
final boolean localeHasBeenChanged = !newLocale.equals(mDictionaries.mLocale);
// We always try to have the main dictionary. Other dictionaries can be unused.
final boolean reloadMainDictionary = localeHasBeenChanged || forceReloadMainDictionary;
// TODO: Make subDictTypesToUse configurable by resource or a static final list.
final Set<String> subDictTypesToUse = CollectionUtils.newHashSet();
if (useContactsDict) {
subDictTypesToUse.add(Dictionary.TYPE_USER);
subDictTypesToUse.add(Dictionary.TYPE_CONTACTS);
}
subDictTypesToUse.add(Dictionary.TYPE_USER);
if (usePersonalizedDicts) {

View File

@ -28,6 +28,7 @@ import android.provider.UserDictionary.Words;
import android.text.TextUtils;
import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.compat.UserDictionaryCompatUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
@ -104,6 +105,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
reloadDictionaryIfRequired();
}
@UsedForTesting
public static UserBinaryDictionary getDictionary(final Context context, final Locale locale,
final File dictFile) {
return new UserBinaryDictionary(context, locale, dictFile);

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.latin.personalization;
import android.content.Context;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Dictionary;
import java.io.File;
@ -32,6 +33,7 @@ public class PersonalizationDictionary extends DecayingExpandableBinaryDictionar
Dictionary.TYPE_PERSONALIZATION, null /* dictFile */);
}
@UsedForTesting
public static PersonalizationDictionary getDictionary(final Context context,
final Locale locale, final File dictFile) {
return PersonalizationHelper.getPersonalizationDictionary(context, locale);

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.latin.personalization;
import android.content.Context;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
@ -38,6 +39,7 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas
Dictionary.TYPE_USER_HISTORY, null /* dictFile */);
}
@UsedForTesting
public static UserHistoryDictionary getDictionary(final Context context, final Locale locale,
final File dictFile) {
return PersonalizationHelper.getUserHistoryDictionary(context, locale);