am 366c0c51: Register personalization dictionary

* commit '366c0c5198f43279f4671a196556124f41297c0c':
  Register personalization dictionary
This commit is contained in:
Satoshi Kataoka 2013-08-22 02:39:44 -07:00 committed by Android Git Automerger
commit 740bf03549
6 changed files with 96 additions and 21 deletions

View file

@ -73,6 +73,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
import com.android.inputmethod.latin.personalization.PersonalizationDictionaryHelper; import com.android.inputmethod.latin.personalization.PersonalizationDictionaryHelper;
import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegister; import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegister;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary; import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
@ -173,6 +174,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private UserBinaryDictionary mUserDictionary; private UserBinaryDictionary mUserDictionary;
private UserHistoryPredictionDictionary mUserHistoryPredictionDictionary; private UserHistoryPredictionDictionary mUserHistoryPredictionDictionary;
private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary; private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary;
private PersonalizationDictionary mPersonalizationDictionary;
private boolean mIsUserDictionaryAvailable; private boolean mIsUserDictionaryAvailable;
private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@ -567,6 +569,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mUserHistoryPredictionDictionary = PersonalizationDictionaryHelper mUserHistoryPredictionDictionary = PersonalizationDictionaryHelper
.getUserHistoryPredictionDictionary(this, localeStr, prefs); .getUserHistoryPredictionDictionary(this, localeStr, prefs);
newSuggest.setUserHistoryPredictionDictionary(mUserHistoryPredictionDictionary); newSuggest.setUserHistoryPredictionDictionary(mUserHistoryPredictionDictionary);
mPersonalizationDictionary = PersonalizationDictionaryHelper
.getPersonalizationDictionary(this, localeStr, prefs);
newSuggest.setPersonalizationDictionary(mPersonalizationDictionary);
mPersonalizationPredictionDictionary = PersonalizationDictionaryHelper mPersonalizationPredictionDictionary = PersonalizationDictionaryHelper
.getPersonalizationPredictionDictionary(this, localeStr, prefs); .getPersonalizationPredictionDictionary(this, localeStr, prefs);
newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary); newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary);

View file

@ -24,6 +24,7 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary; import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
import com.android.inputmethod.latin.personalization.UserHistoryPredictionDictionary; import com.android.inputmethod.latin.personalization.UserHistoryPredictionDictionary;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
@ -200,6 +201,12 @@ public final class Suggest {
personalizationPredictionDictionary); personalizationPredictionDictionary);
} }
public void setPersonalizationDictionary(
final PersonalizationDictionary personalizationDictionary) {
addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION,
personalizationDictionary);
}
public void setAutoCorrectionThreshold(float threshold) { public void setAutoCorrectionThreshold(float threshold) {
mAutoCorrectionThreshold = threshold; mAutoCorrectionThreshold = threshold;
} }

View file

@ -389,7 +389,7 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
} }
public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) { public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
session.setPredictionDictionary(mLocale, this); session.setPredictionDictionary(this);
mSessions.add(session); mSessions.add(session);
session.onDictionaryReady(); session.onDictionaryReady();
} }

View file

@ -18,28 +18,32 @@ package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary; import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import java.util.ArrayList;
/** /**
* This class is a dictionary for the personalized language model that uses binary dictionary. * This class is a dictionary for the personalized language model that uses binary dictionary.
*/ */
public class PersonalizationDictionary extends ExpandableBinaryDictionary { public class PersonalizationDictionary extends ExpandableBinaryDictionary {
private static final String NAME = "personalization"; private static final String NAME = "personalization";
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
public static void registerUpdateListener(PersonalizationDictionaryUpdateSession listener) { CollectionUtils.newArrayList();
// TODO: Implement
}
/** Locale for which this user history dictionary is storing words */ /** Locale for which this user history dictionary is storing words */
private final String mLocale; private final String mLocale;
// Singleton public PersonalizationDictionary(final Context context, final String locale,
private PersonalizationDictionary(final Context context, final String locale) { final SharedPreferences prefs) {
// TODO: Make isUpdatable true. // TODO: Make isUpdatable true.
super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_PERSONALIZATION, super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_PERSONALIZATION,
false /* isUpdatable */); false /* isUpdatable */);
mLocale = locale; mLocale = locale;
// TODO: Restore last updated time
loadDictionary();
} }
@Override @Override
@ -49,15 +53,21 @@ public class PersonalizationDictionary extends ExpandableBinaryDictionary {
@Override @Override
protected boolean hasContentChanged() { protected boolean hasContentChanged() {
// TODO: Implement
return false; return false;
} }
@Override @Override
protected boolean needsToReloadBeforeWriting() { protected boolean needsToReloadBeforeWriting() {
// TODO: Implement
return false; return false;
} }
// TODO: Implement public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
session.setDictionary(this);
mSessions.add(session);
session.onDictionaryReady();
}
public void unRegisterUpdateSession(PersonalizationDictionaryUpdateSession session) {
mSessions.remove(session);
}
} }

View file

@ -35,7 +35,13 @@ public class PersonalizationDictionaryHelper {
private static final ConcurrentHashMap<String, private static final ConcurrentHashMap<String,
SoftReference<PersonalizationPredictionDictionary>> SoftReference<PersonalizationPredictionDictionary>>
sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap(); sLangPersonalizationPredictionDictCache =
CollectionUtils.newConcurrentHashMap();
private static final ConcurrentHashMap<String,
SoftReference<PersonalizationDictionary>>
sLangPersonalizationDictCache =
CollectionUtils.newConcurrentHashMap();
public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary( public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary(
final Context context, final String locale, final SharedPreferences sp) { final Context context, final String locale, final SharedPreferences sp) {
@ -60,20 +66,45 @@ public class PersonalizationDictionaryHelper {
} }
public static void registerPersonalizationDictionaryUpdateSession(final Context context, public static void registerPersonalizationDictionaryUpdateSession(final Context context,
final PersonalizationDictionaryUpdateSession session) { final PersonalizationDictionaryUpdateSession session, String locale) {
final PersonalizationPredictionDictionary dictionary = final PersonalizationPredictionDictionary predictionDictionary =
getPersonalizationPredictionDictionary(context, getPersonalizationPredictionDictionary(context, locale,
context.getResources().getConfiguration().locale.toString(), PreferenceManager.getDefaultSharedPreferences(context));
predictionDictionary.registerUpdateSession(session);
final PersonalizationDictionary dictionary =
getPersonalizationDictionary(context, locale,
PreferenceManager.getDefaultSharedPreferences(context)); PreferenceManager.getDefaultSharedPreferences(context));
dictionary.registerUpdateSession(session); dictionary.registerUpdateSession(session);
} }
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( public static PersonalizationDictionary getPersonalizationDictionary(
final Context context, final String locale, final SharedPreferences sp) { final Context context, final String locale, final SharedPreferences sp) {
synchronized (sLangPersonalizationDictCache) { synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationDictCache.containsKey(locale)) { if (sLangPersonalizationDictCache.containsKey(locale)) {
final SoftReference<PersonalizationPredictionDictionary> ref = final SoftReference<PersonalizationDictionary> ref =
sLangPersonalizationDictCache.get(locale); sLangPersonalizationDictCache.get(locale);
final PersonalizationDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
Log.w(TAG, "Use cached PersonalizationDictCache for " + locale);
}
return dict;
}
}
final PersonalizationDictionary dict =
new PersonalizationDictionary(context, locale, sp);
sLangPersonalizationDictCache.put(
locale, new SoftReference<PersonalizationDictionary>(dict));
return dict;
}
}
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
final Context context, final String locale, final SharedPreferences sp) {
synchronized (sLangPersonalizationPredictionDictCache) {
if (sLangPersonalizationPredictionDictCache.containsKey(locale)) {
final SoftReference<PersonalizationPredictionDictionary> ref =
sLangPersonalizationPredictionDictCache.get(locale);
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get(); final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
if (dict != null) { if (dict != null) {
if (DEBUG) { if (DEBUG) {
@ -84,7 +115,7 @@ public class PersonalizationDictionaryHelper {
} }
final PersonalizationPredictionDictionary dict = final PersonalizationPredictionDictionary dict =
new PersonalizationPredictionDictionary(context, locale, sp); new PersonalizationPredictionDictionary(context, locale, sp);
sLangPersonalizationDictCache.put( sLangPersonalizationPredictionDictCache.put(
locale, new SoftReference<PersonalizationPredictionDictionary>(dict)); locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
return dict; return dict;
} }

View file

@ -16,6 +16,8 @@
package com.android.inputmethod.latin.personalization; package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import android.content.Context; import android.content.Context;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -45,22 +47,41 @@ public abstract class PersonalizationDictionaryUpdateSession {
} }
// TODO: Use a dynamic binary dictionary instead // TODO: Use a dynamic binary dictionary instead
public WeakReference<PersonalizationDictionary> mDictionary;
public WeakReference<DynamicPredictionDictionaryBase> mPredictionDictionary; public WeakReference<DynamicPredictionDictionaryBase> mPredictionDictionary;
public String mLocale; public final String mLocale;
public PersonalizationDictionaryUpdateSession(String locale) {
mLocale = locale;
}
public abstract void onDictionaryReady(); public abstract void onDictionaryReady();
public abstract void onDictionaryClosed(Context context); public abstract void onDictionaryClosed(Context context);
public void setPredictionDictionary(String locale, DynamicPredictionDictionaryBase dictionary) { public void setDictionary(PersonalizationDictionary dictionary) {
mDictionary = new WeakReference<PersonalizationDictionary>(dictionary);
}
public void setPredictionDictionary(DynamicPredictionDictionaryBase dictionary) {
mPredictionDictionary = new WeakReference<DynamicPredictionDictionaryBase>(dictionary); mPredictionDictionary = new WeakReference<DynamicPredictionDictionaryBase>(dictionary);
mLocale = locale; }
protected PersonalizationDictionary getDictionary() {
return mDictionary == null ? null : mDictionary.get();
} }
protected DynamicPredictionDictionaryBase getPredictionDictionary() { protected DynamicPredictionDictionaryBase getPredictionDictionary() {
return mPredictionDictionary == null ? null : mPredictionDictionary.get(); return mPredictionDictionary == null ? null : mPredictionDictionary.get();
} }
private void unsetDictionary() {
final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) {
return;
}
dictionary.unRegisterUpdateSession(this);
}
private void unsetPredictionDictionary() { private void unsetPredictionDictionary() {
final DynamicPredictionDictionaryBase dictionary = getPredictionDictionary(); final DynamicPredictionDictionaryBase dictionary = getPredictionDictionary();
if (dictionary == null) { if (dictionary == null) {
@ -78,6 +99,7 @@ public abstract class PersonalizationDictionaryUpdateSession {
} }
public void closeSession(Context context) { public void closeSession(Context context) {
unsetDictionary();
unsetPredictionDictionary(); unsetPredictionDictionary();
onDictionaryClosed(context); onDictionaryClosed(context);
} }