Merge "Add required header attributes for user/contacts dictionary."

main
Ken Wakasa 2013-12-03 10:23:51 +00:00 committed by Android (Google) Code Review
commit 64ead8d293
12 changed files with 77 additions and 62 deletions

View File

@ -72,8 +72,8 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
private final boolean mUseFirstLastBigrams;
public ContactsBinaryDictionary(final Context context, final Locale locale) {
super(context, getFilenameWithLocale(NAME, locale.toString()), Dictionary.TYPE_CONTACTS,
false /* isUpdatable */);
super(context, getFilenameWithLocale(NAME, locale), locale,
Dictionary.TYPE_CONTACTS, false /* isUpdatable */);
mLocale = locale;
mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale);
registerObserver(context);

View File

@ -33,8 +33,10 @@ import com.android.inputmethod.latin.utils.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -99,6 +101,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/
private final String mFilename;
/** Dictionary locale */
private final Locale mLocale;
/** Whether to support dynamically updating the dictionary */
private final boolean mIsUpdatable;
@ -183,15 +188,17 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* @param context The application context of the parent.
* @param filename The filename for this binary dictionary. Multiple dictionaries with the same
* filename is supported.
* @param locale the dictionary locale.
* @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.
*/
public ExpandableBinaryDictionary(final Context context, final String filename,
final String dictType, final boolean isUpdatable) {
final Locale locale, final String dictType, final boolean isUpdatable) {
super(dictType);
mFilename = filename;
mContext = context;
mLocale = locale;
mIsUpdatable = isUpdatable;
mBinaryDictionary = null;
mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename);
@ -199,8 +206,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mDictionaryWriter = getDictionaryWriter(context, dictType, isUpdatable);
}
protected static String getFilenameWithLocale(final String name, final String localeStr) {
return name + "." + localeStr + DICT_FILE_EXTENSION;
protected static String getFilenameWithLocale(final String name, final Locale locale) {
return name + "." + locale.toString() + DICT_FILE_EXTENSION;
}
/**
@ -237,9 +244,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
protected Map<String, String> getHeaderAttributeMap() {
HashMap<String, String> attributeMap = new HashMap<String, String>();
attributeMap.put(FormatSpec.FileHeader.SUPPORTS_DYNAMIC_UPDATE_ATTRIBUTE,
SUPPORTS_DYNAMIC_UPDATE);
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE, mFilename);
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, mLocale.toString());
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_VERSION_ATTRIBUTE,
String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
return attributeMap;
}

View File

@ -614,7 +614,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final Locale switcherSubtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale();
final String switcherLocaleStr = switcherSubtypeLocale.toString();
final Locale subtypeLocale;
final String localeStr;
if (TextUtils.isEmpty(switcherLocaleStr)) {
// This happens in very rare corner cases - for example, immediately after a switch
// to LatinIME has been requested, about a frame later another switch happens. In this
@ -624,10 +623,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// of knowing anyway.
Log.e(TAG, "System is reporting no current subtype.");
subtypeLocale = getResources().getConfiguration().locale;
localeStr = subtypeLocale.toString();
} else {
subtypeLocale = switcherSubtypeLocale;
localeStr = switcherLocaleStr;
}
final Suggest newSuggest = new Suggest(this /* Context */, subtypeLocale,
@ -642,19 +639,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
ResearchLogger.getInstance().initSuggest(newSuggest);
}
mUserDictionary = new UserBinaryDictionary(this, localeStr);
mUserDictionary = new UserBinaryDictionary(this, subtypeLocale);
mIsUserDictionaryAvailable = mUserDictionary.isEnabled();
newSuggest.setUserDictionary(mUserDictionary);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary(this, localeStr);
mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary(
this, subtypeLocale);
newSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
mPersonalizationDictionary =
PersonalizationHelper.getPersonalizationDictionary(this, localeStr);
PersonalizationHelper.getPersonalizationDictionary(this, subtypeLocale);
newSuggest.setPersonalizationDictionary(mPersonalizationDictionary);
mPersonalizationPredictionDictionary =
PersonalizationHelper.getPersonalizationPredictionDictionary(this, localeStr);
PersonalizationHelper.getPersonalizationPredictionDictionary(this, subtypeLocale);
newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary);
final Suggest oldSuggest = mSuggest;

View File

@ -22,14 +22,15 @@ import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
import java.util.Locale;
public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionary {
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final String locale) {
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final Locale locale) {
this(context, locale, false);
}
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final String locale,
public SynchronouslyLoadedUserBinaryDictionary(final Context context, final Locale locale,
final boolean alsoUseMoreRestrictiveLocales) {
super(context, locale, alsoUseMoreRestrictiveLocales);
}

View File

@ -75,20 +75,21 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
final private String mLocale;
final private boolean mAlsoUseMoreRestrictiveLocales;
public UserBinaryDictionary(final Context context, final String locale) {
public UserBinaryDictionary(final Context context, final Locale locale) {
this(context, locale, false);
}
public UserBinaryDictionary(final Context context, final String locale,
public UserBinaryDictionary(final Context context, final Locale locale,
final boolean alsoUseMoreRestrictiveLocales) {
super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_USER,
super(context, getFilenameWithLocale(NAME, locale), locale, Dictionary.TYPE_USER,
false /* isUpdatable */);
if (null == locale) throw new NullPointerException(); // Catch the error earlier
if (SubtypeLocaleUtils.NO_LANGUAGE.equals(locale)) {
final String localeStr = locale.toString();
if (SubtypeLocaleUtils.NO_LANGUAGE.equals(localeStr)) {
// If we don't have a locale, insert into the "all locales" user dictionary.
mLocale = USER_DICTIONARY_ALL_LANGUAGES;
} else {
mLocale = locale;
mLocale = localeStr;
}
mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales;
// Perform a managed query. The Activity will handle closing and re-querying the cursor

View File

@ -35,7 +35,9 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* This class is a base class of a dictionary that supports decaying for the personalized language
@ -55,7 +57,7 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
public static final int REQUIRED_BINARY_DICTIONARY_VERSION = 4;
/** Locale for which this user history dictionary is storing words */
private final String mLocale;
private final Locale mLocale;
private final String mFileName;
@ -66,11 +68,11 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
@UsedForTesting boolean mIsTest = false;
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
final String locale, final String dictionaryType, final String fileName) {
super(context, fileName, dictionaryType, true);
final Locale locale, final String dictionaryType, final String fileName) {
super(context, fileName, locale, dictionaryType, true);
mLocale = locale;
mFileName = fileName;
if (mLocale != null && mLocale.length() > 1) {
if (mLocale != null && mLocale.toString().length() > 1) {
reloadDictionaryIfRequired();
}
}
@ -93,7 +95,9 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
attributeMap.put(FormatSpec.FileHeader.USES_FORGETTING_CURVE_ATTRIBUTE,
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE);
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE, mFileName);
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, mLocale);
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, mLocale.toString());
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_VERSION_ATTRIBUTE,
String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
return attributeMap;
}
@ -164,10 +168,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
// Never loaded to memory in Java side.
}
protected String getLocale() {
return mLocale;
}
public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
session.setPredictionDictionary(this);
mSessions.add(session);

View File

@ -21,9 +21,9 @@ import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.Locale;
/**
* This class is a dictionary for the personalized language model that uses binary dictionary.
@ -33,14 +33,10 @@ public class PersonalizationDictionary extends ExpandableBinaryDictionary {
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList();
/** Locale for which this user history dictionary is storing words */
private final String mLocale;
public PersonalizationDictionary(final Context context, final String locale) {
public PersonalizationDictionary(final Context context, final Locale locale) {
// TODO: Make isUpdatable true.
super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_PERSONALIZATION,
false /* isUpdatable */);
mLocale = locale;
super(context, getFilenameWithLocale(NAME, locale), locale,
Dictionary.TYPE_PERSONALIZATION, false /* isUpdatable */);
// TODO: Restore last updated time
loadDictionary();
}

View File

@ -24,6 +24,7 @@ import android.preference.PreferenceManager;
import android.util.Log;
import java.lang.ref.SoftReference;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
public class PersonalizationHelper {
@ -41,11 +42,12 @@ public class PersonalizationHelper {
CollectionUtils.newConcurrentHashMap();
public static UserHistoryDictionary getUserHistoryDictionary(
final Context context, final String locale) {
final Context context, final Locale locale) {
final String localeStr = locale.toString();
synchronized (sLangUserHistoryDictCache) {
if (sLangUserHistoryDictCache.containsKey(locale)) {
if (sLangUserHistoryDictCache.containsKey(localeStr)) {
final SoftReference<UserHistoryDictionary> ref =
sLangUserHistoryDictCache.get(locale);
sLangUserHistoryDictCache.get(localeStr);
final UserHistoryDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
@ -56,7 +58,8 @@ public class PersonalizationHelper {
}
}
final UserHistoryDictionary dict = new UserHistoryDictionary(context, locale);
sLangUserHistoryDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict));
sLangUserHistoryDictCache.put(localeStr,
new SoftReference<UserHistoryDictionary>(dict));
return dict;
}
}
@ -74,7 +77,7 @@ public class PersonalizationHelper {
}
public static void registerPersonalizationDictionaryUpdateSession(final Context context,
final PersonalizationDictionaryUpdateSession session, String locale) {
final PersonalizationDictionaryUpdateSession session, Locale locale) {
final PersonalizationPredictionDictionary predictionDictionary =
getPersonalizationPredictionDictionary(context, locale);
predictionDictionary.registerUpdateSession(session);
@ -83,11 +86,12 @@ public class PersonalizationHelper {
}
public static PersonalizationDictionary getPersonalizationDictionary(
final Context context, final String locale) {
final Context context, final Locale locale) {
final String localeStr = locale.toString();
synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationDictCache.containsKey(locale)) {
if (sLangPersonalizationDictCache.containsKey(localeStr)) {
final SoftReference<PersonalizationDictionary> ref =
sLangPersonalizationDictCache.get(locale);
sLangPersonalizationDictCache.get(localeStr);
final PersonalizationDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
@ -98,17 +102,18 @@ public class PersonalizationHelper {
}
final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale);
sLangPersonalizationDictCache.put(
locale, new SoftReference<PersonalizationDictionary>(dict));
localeStr, new SoftReference<PersonalizationDictionary>(dict));
return dict;
}
}
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
final Context context, final String locale) {
final Context context, final Locale locale) {
final String localeStr = locale.toString();
synchronized (sLangPersonalizationPredictionDictCache) {
if (sLangPersonalizationPredictionDictCache.containsKey(locale)) {
if (sLangPersonalizationPredictionDictCache.containsKey(localeStr)) {
final SoftReference<PersonalizationPredictionDictionary> ref =
sLangPersonalizationPredictionDictCache.get(locale);
sLangPersonalizationPredictionDictCache.get(localeStr);
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
@ -120,7 +125,7 @@ public class PersonalizationHelper {
final PersonalizationPredictionDictionary dict =
new PersonalizationPredictionDictionary(context, locale);
sLangPersonalizationPredictionDictCache.put(
locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
localeStr, new SoftReference<PersonalizationPredictionDictionary>(dict));
return dict;
}
}

View File

@ -19,14 +19,16 @@ package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import java.util.Locale;
import android.content.Context;
public class PersonalizationPredictionDictionary extends DecayingExpandableBinaryDictionaryBase {
private static final String NAME = PersonalizationPredictionDictionary.class.getSimpleName();
/* package */ PersonalizationPredictionDictionary(final Context context, final String locale) {
/* package */ PersonalizationPredictionDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA,
getDictionaryFileName(locale));
getDictionaryFileName(locale.toString()));
}
private static String getDictionaryFileName(final String locale) {

View File

@ -19,6 +19,8 @@ package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import java.util.Locale;
import android.content.Context;
/**
@ -28,8 +30,9 @@ import android.content.Context;
public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase {
/* package for tests */ static final String NAME =
UserHistoryDictionary.class.getSimpleName();
/* package */ UserHistoryDictionary(final Context context, final String locale) {
super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictionaryFileName(locale));
/* package */ UserHistoryDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_USER_HISTORY,
getDictionaryFileName(locale.toString()));
}
private static String getDictionaryFileName(final String locale) {

View File

@ -428,7 +428,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
final String localeStr = locale.toString();
UserBinaryDictionary userDictionary = mUserDictionaries.get(localeStr);
if (null == userDictionary) {
userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, localeStr, true);
userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, locale, true);
mUserDictionaries.put(localeStr, userDictionary);
}
dictionaryCollection.addDictionary(userDictionary);

View File

@ -26,6 +26,7 @@ import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -84,7 +85,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
final List<String> words = generateWords(numberOfWords, random);
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix /* locale */);
new Locale(testFilenameSuffix));
// Add random words to the user history dictionary.
addToDict(dict, words);
if (checkContents) {
@ -108,7 +109,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
private void clearHistory(final String testFilenameSuffix) {
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix /* locale */);
new Locale(testFilenameSuffix));
dict.clearAndFlushDictionary();
dict.close();
}
@ -121,7 +122,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
try {
final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(),
testFilenameSuffix);
new Locale(testFilenameSuffix));
dict.shutdownExecutorForTests();
while (!dict.isTerminatedForTests()) {
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
@ -134,7 +135,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
public void testRandomWords() {
Log.d(TAG, "This test can be used for profiling.");
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
final String testFilenameSuffix = "test_random_words" + System.currentTimeMillis();
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
@ -169,7 +170,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
// Create filename suffixes for this test.
for (int i = 0; i < numberOfLanguages; i++) {
testFilenameSuffixes[i] = "testSwitchingLanguages" + i;
testFilenameSuffixes[i] = "test_switching_languages" + i;
final String fileName = UserHistoryDictionary.NAME + "." +
testFilenameSuffixes[i] + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
dictFiles[i] = new File(getContext().getFilesDir(), fileName);
@ -205,7 +206,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
}
public void testAddManyWords() {
final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
final String testFilenameSuffix = "test_random_words" + System.currentTimeMillis();
final int numberOfWords = 10000;
final Random random = new Random(123456);
clearHistory(testFilenameSuffix);