am c197b9ed: Merge "Make setAdditionalDictionaries private."

* commit 'c197b9ed902f6ea8f14764bbe04c9f1541bfb8f5':
  Make setAdditionalDictionaries private.
main
Keisuke Kuroyanagi 2013-12-26 22:24:45 -08:00 committed by Android Git Automerger
commit 5d5537b2de
3 changed files with 91 additions and 52 deletions

View File

@ -22,7 +22,6 @@ 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.Suggest.SuggestInitializationListener;
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.PersonalizationDictionary;
import com.android.inputmethod.latin.personalization.PersonalizationHelper; import com.android.inputmethod.latin.personalization.PersonalizationHelper;
@ -45,7 +44,7 @@ public class DictionaryFacilitatorForSuggest {
private final ConcurrentHashMap<String, Dictionary> mDictionaries = private final ConcurrentHashMap<String, Dictionary> mDictionaries =
CollectionUtils.newConcurrentHashMap(); CollectionUtils.newConcurrentHashMap();
private HashSet<String> mOnlyDictionarySetForDebug = null; private HashSet<String> mDictionarySubsetForDebug = null;
private Dictionary mMainDictionary; private Dictionary mMainDictionary;
private ContactsBinaryDictionary mContactsDictionary; private ContactsBinaryDictionary mContactsDictionary;
@ -56,17 +55,51 @@ public class DictionaryFacilitatorForSuggest {
@UsedForTesting @UsedForTesting
private boolean mIsCurrentlyWaitingForMainDictionary = false; private boolean mIsCurrentlyWaitingForMainDictionary = false;
public interface DictionaryInitializationListener {
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
}
/**
* Creates instance for initialization or when the locale is changed.
*
* @param context the context
* @param locale the locale
* @param settingsValues current settings values to control what dictionaries should be used
* @param listener the listener
* @param oldDictionaryFacilitator the instance having old dictionaries. This is null when the
* instance is initially created.
*/
public DictionaryFacilitatorForSuggest(final Context context, final Locale locale, public DictionaryFacilitatorForSuggest(final Context context, final Locale locale,
final SettingsValues settingsValues, final SuggestInitializationListener listener) { final SettingsValues settingsValues, final DictionaryInitializationListener listener,
resetMainDict(context, locale, listener); final DictionaryFacilitatorForSuggest oldDictionaryFacilitator) {
mContext = context; mContext = context;
mLocale = locale; mLocale = locale;
// initialize a debug flag for the personalization initForDebug(settingsValues);
if (settingsValues.mUseOnlyPersonalizationDictionaryForDebug) { reloadMainDict(context, locale, listener);
mOnlyDictionarySetForDebug = new HashSet<String>();
mOnlyDictionarySetForDebug.add(Dictionary.TYPE_PERSONALIZATION);
}
setUserDictionary(new UserBinaryDictionary(context, locale)); setUserDictionary(new UserBinaryDictionary(context, locale));
resetAdditionalDictionaries(oldDictionaryFacilitator, settingsValues);
}
/**
* Creates instance for when the settings values have been changed.
*
* @param settingsValues the new settings values
* @param oldDictionaryFacilitator the instance having old dictionaries. This must not be null.
*/
//
public DictionaryFacilitatorForSuggest(final SettingsValues settingsValues,
final DictionaryFacilitatorForSuggest oldDictionaryFacilitator) {
mContext = oldDictionaryFacilitator.mContext;
mLocale = oldDictionaryFacilitator.mLocale;
initForDebug(settingsValues);
// Transfer main dictionary.
setMainDictionary(oldDictionaryFacilitator.mMainDictionary);
oldDictionaryFacilitator.removeDictionary(Dictionary.TYPE_MAIN);
// Transfer user dictionary.
setUserDictionary(oldDictionaryFacilitator.mUserDictionary);
oldDictionaryFacilitator.removeDictionary(Dictionary.TYPE_USER);
// Transfer or create additional dictionaries depending on the settings values.
resetAdditionalDictionaries(oldDictionaryFacilitator, settingsValues);
} }
@UsedForTesting @UsedForTesting
@ -76,8 +109,15 @@ public class DictionaryFacilitatorForSuggest {
false /* useFullEditDistance */, locale); false /* useFullEditDistance */, locale);
mContext = context; mContext = context;
mLocale = locale; mLocale = locale;
mMainDictionary = mainDict; setMainDictionary(mainDict);
addOrReplaceDictionary(Dictionary.TYPE_MAIN, mainDict); }
// initialize a debug flag for the personalization
private void initForDebug(final SettingsValues settingsValues) {
if (settingsValues.mUseOnlyPersonalizationDictionaryForDebug) {
mDictionarySubsetForDebug = new HashSet<String>();
mDictionarySubsetForDebug.add(Dictionary.TYPE_PERSONALIZATION);
}
} }
public void close() { public void close() {
@ -86,15 +126,10 @@ public class DictionaryFacilitatorForSuggest {
for (final Dictionary dictionary : dictionaries) { for (final Dictionary dictionary : dictionaries) {
dictionary.close(); dictionary.close();
} }
mMainDictionary = null;
mContactsDictionary = null;
mUserDictionary = null;
mUserHistoryDictionary = null;
mPersonalizationDictionary = null;
} }
public void resetMainDict(final Context context, final Locale locale, public void reloadMainDict(final Context context, final Locale locale,
final SuggestInitializationListener listener) { final DictionaryInitializationListener listener) {
mIsCurrentlyWaitingForMainDictionary = true; mIsCurrentlyWaitingForMainDictionary = true;
mMainDictionary = null; mMainDictionary = null;
if (listener != null) { if (listener != null) {
@ -163,12 +198,12 @@ public class DictionaryFacilitatorForSuggest {
} }
/** /**
* Set dictionaries that can be turned off according to the user settings. * Reset dictionaries that can be turned off according to the user settings.
* *
* @param oldDictionaryFacilitator the instance having old dictionaries * @param oldDictionaryFacilitator the instance having old dictionaries
* @param settingsValues current SettingsValues * @param settingsValues current SettingsValues
*/ */
public void setAdditionalDictionaries( private void resetAdditionalDictionaries(
final DictionaryFacilitatorForSuggest oldDictionaryFacilitator, final DictionaryFacilitatorForSuggest oldDictionaryFacilitator,
final SettingsValues settingsValues) { final SettingsValues settingsValues) {
// Contacts dictionary // Contacts dictionary
@ -360,8 +395,12 @@ public class DictionaryFacilitatorForSuggest {
return maxFreq; return maxFreq;
} }
private void removeDictionary(final String key) {
mDictionaries.remove(key);
}
private void addOrReplaceDictionary(final String key, final Dictionary dict) { private void addOrReplaceDictionary(final String key, final Dictionary dict) {
if (mOnlyDictionarySetForDebug != null && !mOnlyDictionarySetForDebug.contains(key)) { if (mDictionarySubsetForDebug != null && !mDictionarySubsetForDebug.contains(key)) {
Log.w(TAG, "Ignore add " + key + " dictionary for debug."); Log.w(TAG, "Ignore add " + key + " dictionary for debug.");
return; return;
} }

View File

@ -96,7 +96,8 @@ import java.util.Locale;
* Input method implementation for Qwerty'ish keyboard. * Input method implementation for Qwerty'ish keyboard.
*/ */
public class LatinIME extends InputMethodService implements KeyboardActionListener, public class LatinIME extends InputMethodService implements KeyboardActionListener,
SuggestionStripView.Listener, Suggest.SuggestInitializationListener { SuggestionStripView.Listener,
DictionaryFacilitatorForSuggest.DictionaryInitializationListener {
private static final String TAG = LatinIME.class.getSimpleName(); private static final String TAG = LatinIME.class.getSimpleName();
private static final boolean TRACE = false; private static final boolean TRACE = false;
private static boolean DEBUG = false; private static boolean DEBUG = false;
@ -514,12 +515,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// be done later inside {@see #initSuggest()} when the reopenDictionaries message is // be done later inside {@see #initSuggest()} when the reopenDictionaries message is
// processed. // processed.
final SettingsValues currentSettingsValues = mSettings.getCurrent(); final SettingsValues currentSettingsValues = mSettings.getCurrent();
if (!mHandler.hasPendingReopenDictionaries() && mInputLogic.mSuggest != null) { final Suggest suggest = mInputLogic.mSuggest;
if (!mHandler.hasPendingReopenDictionaries() && suggest != null) {
// May need to reset dictionaries depending on the user settings. // May need to reset dictionaries depending on the user settings.
// TODO: Quit setting dictionaries from LatinIME. final DictionaryFacilitatorForSuggest oldDictionaryFacilitator =
mInputLogic.mSuggest.mDictionaryFacilitator.setAdditionalDictionaries( suggest.mDictionaryFacilitator;
mInputLogic.mSuggest.mDictionaryFacilitator /* oldDictionaryFacilitator */, final DictionaryFacilitatorForSuggest dictionaryFacilitator =
currentSettingsValues); new DictionaryFacilitatorForSuggest(currentSettingsValues,
oldDictionaryFacilitator);
// Create Suggest instance with the new dictionary facilitator.
mInputLogic.mSuggest = new Suggest(suggest /* oldSuggest */, dictionaryFacilitator);
suggest.close();
} }
if (currentSettingsValues.mUsePersonalizedDicts) { if (currentSettingsValues.mUsePersonalizedDicts) {
PersonalizationDictionarySessionRegistrar.init(this); PersonalizationDictionarySessionRegistrar.init(this);
@ -556,19 +562,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
final SettingsValues settingsValues = mSettings.getCurrent(); final SettingsValues settingsValues = mSettings.getCurrent();
final Suggest newSuggest = new Suggest(this /* Context */, subtypeLocale, settingsValues, final DictionaryFacilitatorForSuggest oldDictionaryFacilitator =
this /* SuggestInitializationListener */); (mInputLogic.mSuggest == null) ? null : mInputLogic.mSuggest.mDictionaryFacilitator;
// Creates new dictionary facilitator for the new locale.
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
new DictionaryFacilitatorForSuggest(this /* context */, subtypeLocale,
settingsValues, this /* DictionaryInitializationListener */,
oldDictionaryFacilitator);
final Suggest newSuggest = new Suggest(subtypeLocale, dictionaryFacilitator);
if (settingsValues.mCorrectionEnabled) { if (settingsValues.mCorrectionEnabled) {
newSuggest.setAutoCorrectionThreshold(settingsValues.mAutoCorrectionThreshold); newSuggest.setAutoCorrectionThreshold(settingsValues.mAutoCorrectionThreshold);
} }
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.getInstance().initDictionary(newSuggest.mDictionaryFacilitator); ResearchLogger.getInstance().initDictionary(newSuggest.mDictionaryFacilitator);
} }
// TODO: Quit setting dictionaries from LatinIME.
newSuggest.mDictionaryFacilitator.setAdditionalDictionaries(
(mInputLogic.mSuggest == null) ? null : mInputLogic.mSuggest.mDictionaryFacilitator
/* oldDictionaryFacilitator */, settingsValues);
final Suggest oldSuggest = mInputLogic.mSuggest; final Suggest oldSuggest = mInputLogic.mSuggest;
mInputLogic.mSuggest = newSuggest; mInputLogic.mSuggest = newSuggest;
if (oldSuggest != null) oldSuggest.close(); if (oldSuggest != null) oldSuggest.close();
@ -576,7 +583,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
/* package private */ void resetSuggestMainDict() { /* package private */ void resetSuggestMainDict() {
final Locale subtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale(); final Locale subtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale();
mInputLogic.mSuggest.mDictionaryFacilitator.resetMainDict(this, subtypeLocale, mInputLogic.mSuggest.mDictionaryFacilitator.reloadMainDict(this, subtypeLocale,
this /* SuggestInitializationListener */); this /* SuggestInitializationListener */);
} }
@ -1963,7 +1970,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// DO NOT USE THIS for any other purpose than testing. This can break the keyboard badly. // DO NOT USE THIS for any other purpose than testing. This can break the keyboard badly.
@UsedForTesting @UsedForTesting
/* package for test */ void replaceMainDictionaryForTest(final Locale locale) { /* package for test */ void replaceMainDictionaryForTest(final Locale locale) {
mInputLogic.mSuggest.mDictionaryFacilitator.resetMainDict(this, locale, null); mInputLogic.mSuggest.mDictionaryFacilitator.reloadMainDict(this, locale, null);
} }
public void debugDumpStateAndCrashWithException(final String context) { public void debugDumpStateAndCrashWithException(final String context) {

View File

@ -16,13 +16,10 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
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.settings.SettingsValues;
import com.android.inputmethod.latin.utils.AutoCorrectionUtils; import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
import com.android.inputmethod.latin.utils.BoundedTreeSet; import com.android.inputmethod.latin.utils.BoundedTreeSet;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
@ -55,10 +52,6 @@ public final class Suggest {
public static final int MAX_SUGGESTIONS = 18; public static final int MAX_SUGGESTIONS = 18;
public interface SuggestInitializationListener {
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
}
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
public final DictionaryFacilitatorForSuggest mDictionaryFacilitator; public final DictionaryFacilitatorForSuggest mDictionaryFacilitator;
@ -68,18 +61,18 @@ public final class Suggest {
// Locale used for upper- and title-casing words // Locale used for upper- and title-casing words
public final Locale mLocale; public final Locale mLocale;
public Suggest(final Context context, final Locale locale, final SettingsValues settingsValues, public Suggest(final Locale locale,
final SuggestInitializationListener listener) { final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
mLocale = locale; mLocale = locale;
mDictionaryFacilitator = new DictionaryFacilitatorForSuggest(context, locale, mDictionaryFacilitator = dictionaryFacilitator;
settingsValues, listener);
} }
@UsedForTesting // Creates instance with new dictionary facilitator.
Suggest(final Context context, final AssetFileAddress[] dictionaryList, final Locale locale) { public Suggest(final Suggest oldSuggst,
mLocale = locale; final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
mDictionaryFacilitator = new DictionaryFacilitatorForSuggest(context, dictionaryList, mLocale = oldSuggst.mLocale;
locale); mAutoCorrectionThreshold = oldSuggst.mAutoCorrectionThreshold;
mDictionaryFacilitator = dictionaryFacilitator;
} }
public void setAutoCorrectionThreshold(float threshold) { public void setAutoCorrectionThreshold(float threshold) {