Refactor dictionary constructor

This change must be checked in together with I3923d5af2a.

Bug: 12994268
Change-Id: I19de6efbdfa3b6c6c69a20a836e53962fd5737de
This commit is contained in:
Tadashi G. Takaoka 2014-02-13 12:12:13 +09:00
parent 50fbe4cc09
commit 870d89b1a8
7 changed files with 34 additions and 59 deletions

View file

@ -29,7 +29,6 @@ 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;
@ -73,8 +72,13 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
private final boolean mUseFirstLastBigrams;
public ContactsBinaryDictionary(final Context context, final Locale locale) {
super(context, getDictNameWithLocale(NAME, locale), locale,
Dictionary.TYPE_CONTACTS, false /* isUpdatable */);
this(context, locale, null /* dictFile */);
}
public ContactsBinaryDictionary(final Context context, final Locale locale,
final File dictFile) {
super(context, getDictName(NAME, locale, dictFile), locale, Dictionary.TYPE_CONTACTS,
false /* isUpdatable */, dictFile);
mLocale = locale;
mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale);
registerObserver(context);
@ -84,12 +88,6 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
loadDictionary();
}
// Dummy constructor for tests.
@UsedForTesting
public ContactsBinaryDictionary(final Context context, final Locale locale, final File file) {
this(context, locale);
}
private synchronized void registerObserver(final Context context) {
// Perform a managed query. The Activity will handle closing and requerying the cursor
// when needed.

View file

@ -213,14 +213,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* @param dictType the dictionary type, as a human-readable string
* @param isUpdatable whether to support dynamically updating the dictionary. Please note that
* dynamic dictionary has negative effects on memory space and computation time.
* @param dictFile dictionary file path. if null, use default dictionary path based on
* dictionary type.
*/
public ExpandableBinaryDictionary(final Context context, final String dictName,
final Locale locale, final String dictType, final boolean isUpdatable) {
this(context, dictName, locale, dictType, isUpdatable,
new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION));
}
// Creates an instance that uses a given dictionary file.
public ExpandableBinaryDictionary(final Context context, final String dictName,
final Locale locale, final String dictType, final boolean isUpdatable,
final File dictFile) {
@ -229,15 +224,17 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mContext = context;
mLocale = locale;
mIsUpdatable = isUpdatable;
mDictFile = dictFile;
mDictFile = (dictFile != null) ? dictFile
: new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION);
mBinaryDictionary = null;
mDictNameDictionaryUpdateController = getDictionaryUpdateController(dictName);
// Currently, only dynamic personalization dictionary is updatable.
mDictionaryWriter = getDictionaryWriter(isUpdatable);
}
protected static String getDictNameWithLocale(final String name, final Locale locale) {
return name + "." + locale.toString();
protected static String getDictName(final String name, final Locale locale,
final File dictFile) {
return dictFile != null ? dictFile.getName() : name + "." + locale.toString();
}
/**

View file

@ -28,12 +28,12 @@ public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDic
private final Object mLock = new Object();
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final Locale locale) {
this(context, locale, false);
this(context, locale, false /* alsoUseMoreRestrictiveLocales */);
}
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final Locale locale,
final boolean alsoUseMoreRestrictiveLocales) {
super(context, locale, alsoUseMoreRestrictiveLocales);
super(context, locale, alsoUseMoreRestrictiveLocales, null /* dictFile */);
}
@Override

View file

@ -28,7 +28,6 @@ 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.LocaleUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
@ -78,19 +77,17 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
final public boolean mEnabled;
public UserBinaryDictionary(final Context context, final Locale locale) {
this(context, locale, false);
this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */);
}
// Dummy constructor for tests.
@UsedForTesting
public UserBinaryDictionary(final Context context, final Locale locale, final File file) {
this(context, locale);
public UserBinaryDictionary(final Context context, final Locale locale, final File dictFile) {
this(context, locale, false /* alsoUseMoreRestrictiveLocales */, dictFile);
}
public UserBinaryDictionary(final Context context, final Locale locale,
final boolean alsoUseMoreRestrictiveLocales) {
super(context, getDictNameWithLocale(NAME, locale), locale, Dictionary.TYPE_USER,
false /* isUpdatable */);
final boolean alsoUseMoreRestrictiveLocales, final File dictFile) {
super(context, getDictName(NAME, locale, dictFile), locale, Dictionary.TYPE_USER,
false /* isUpdatable */, dictFile);
if (null == locale) throw new NullPointerException(); // Catch the error earlier
final String localeStr = locale.toString();
if (SubtypeLocaleUtils.NO_LANGUAGE.equals(localeStr)) {

View file

@ -52,21 +52,10 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
private final String mDictName;
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
final Locale locale, final String dictionaryType, final String dictName) {
super(context, dictName, locale, dictionaryType, true);
mLocale = locale;
mDictName = dictName;
if (mLocale != null && mLocale.toString().length() > 1) {
reloadDictionaryIfRequired();
}
}
// Creates an instance that uses a given dictionary file for testing.
@UsedForTesting
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
final Locale locale, final String dictionaryType, final String dictName,
final String dictName, final Locale locale, final String dictionaryType,
final File dictFile) {
super(context, dictName, locale, dictionaryType, true, dictFile);
super(context, getDictName(dictName, locale, dictFile), locale, dictionaryType,
true /* isUpdatable */, dictFile);
mLocale = locale;
mDictName = dictName;
if (mLocale != null && mLocale.toString().length() > 1) {

View file

@ -16,27 +16,23 @@
package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.annotations.UsedForTesting;
import android.content.Context;
import com.android.inputmethod.latin.Dictionary;
import java.io.File;
import java.util.Locale;
import android.content.Context;
public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase {
/* package */ static final String NAME = PersonalizationDictionary.class.getSimpleName();
/* package */ PersonalizationDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_PERSONALIZATION,
getDictNameWithLocale(NAME, locale));
this(context, locale, null /* dictFile */);
}
// Creates an instance that uses a given dictionary file for testing.
@UsedForTesting
public PersonalizationDictionary(final Context context, final Locale locale,
final File dictFile) {
super(context, locale, Dictionary.TYPE_PERSONALIZATION, getDictNameWithLocale(NAME, locale),
super(context, getDictName(NAME, locale, dictFile), locale, Dictionary.TYPE_PERSONALIZATION,
dictFile);
}

View file

@ -16,29 +16,27 @@
package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.annotations.UsedForTesting;
import android.content.Context;
import com.android.inputmethod.latin.Dictionary;
import java.io.File;
import java.util.Locale;
import android.content.Context;
/**
* Locally gathers stats about the words user types and various other signals like auto-correction
* cancellation or manual picks. This allows the keyboard to adapt to the typist over time.
*/
public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase {
/* package */ static final String NAME = UserHistoryDictionary.class.getSimpleName();
/* package */ UserHistoryDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale));
this(context, locale, null /* dictFile */);
}
// Creates an instance that uses a given dictionary file for testing.
@UsedForTesting
public UserHistoryDictionary(final Context context, final Locale locale,
final File dictFile) {
super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale),
super(context, getDictName(NAME, locale, dictFile), locale, Dictionary.TYPE_USER_HISTORY,
dictFile);
}