Remove redundant creation of dictionary facilitator.

Change-Id: I2edab4e3ba4ea0b9b13f9bb224aac86cac9b9f31
main
Keisuke Kuroyanagi 2014-02-27 22:31:57 +09:00
parent 66c96e8813
commit e9926b0d11
3 changed files with 38 additions and 23 deletions

View File

@ -186,6 +186,16 @@ public class DictionaryFacilitatorForSuggest {
}
}
public boolean needsToBeRecreated(final Locale newLocale,
final SettingsValues newSettingsValues) {
return !mLocale.equals(newLocale)
|| (newSettingsValues.mUseContactsDict != (mContactsDictionary != null))
|| (newSettingsValues.mUsePersonalizedDicts != (mUserHistoryDictionary != null))
|| (newSettingsValues.mUsePersonalizedDicts != hasPersonalizationDictionary())
|| (newSettingsValues.mUseOnlyPersonalizationDictionaryForDebug
!= (mDictionarySubsetForDebug != null));
}
// initialize a debug flag for the personalization
private void initForDebug(final SettingsValues settingsValues) {
if (settingsValues.mUseOnlyPersonalizationDictionaryForDebug) {

View File

@ -508,27 +508,31 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final EditorInfo editorInfo = getCurrentInputEditorInfo();
final InputAttributes inputAttributes = new InputAttributes(editorInfo, isFullscreenMode());
mSettings.loadSettings(this, locale, inputAttributes);
AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(mSettings.getCurrent());
// To load the keyboard we need to load all the settings once, but resetting the
// contacts dictionary should be deferred until after the new layout has been displayed
// to improve responsivity. In the language switching process, we post a reopenDictionaries
// message, then come here to read the settings for the new language before we change
// the layout; at this time, we need to skip resetting the contacts dictionary. It will
// be done later inside {@see #initSuggest()} when the reopenDictionaries message is
// processed.
final SettingsValues currentSettingsValues = mSettings.getCurrent();
final Suggest suggest = mInputLogic.mSuggest;
if (!mHandler.hasPendingReopenDictionaries() && suggest != null) {
AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(currentSettingsValues);
// This method is called on startup and language switch, before the new layout has
// been displayed. Opening dictionaries never affects responsivity as dictionaries are
// asynchronously loaded.
initOrResetSuggestForSettingsValues(mInputLogic.mSuggest, locale, currentSettingsValues);
}
private void initOrResetSuggestForSettingsValues(final Suggest oldSuggest,
final Locale locale, final SettingsValues settingsValues) {
if (!mHandler.hasPendingReopenDictionaries() && oldSuggest != null) {
// May need to reset dictionaries depending on the user settings.
final DictionaryFacilitatorForSuggest oldDictionaryFacilitator =
suggest.mDictionaryFacilitator;
oldSuggest.mDictionaryFacilitator;
if (!oldDictionaryFacilitator.needsToBeRecreated(locale, settingsValues)) {
// Continue to use the same dictionary facilitator if no configuration has changed.
refreshPersonalizationDictionarySession();
return;
}
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
new DictionaryFacilitatorForSuggest(currentSettingsValues,
oldDictionaryFacilitator);
new DictionaryFacilitatorForSuggest(settingsValues, oldDictionaryFacilitator);
// Create Suggest instance with the new dictionary facilitator.
resetSuggest(new Suggest(suggest /* oldSuggest */, dictionaryFacilitator));
} else if (suggest == null) {
initSuggestForLocale(locale);
replaceSuggest(new Suggest(oldSuggest, dictionaryFacilitator));
} else if (oldSuggest == null) {
initSuggestForLocale(oldSuggest, locale);
}
}
@ -588,13 +592,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else {
subtypeLocale = switcherSubtypeLocale;
}
initSuggestForLocale(subtypeLocale);
initSuggestForLocale(mInputLogic.mSuggest, subtypeLocale);
}
private void initSuggestForLocale(final Locale locale) {
private void initSuggestForLocale(final Suggest oldSuggest, final Locale locale) {
final SettingsValues settingsValues = mSettings.getCurrent();
final DictionaryFacilitatorForSuggest oldDictionaryFacilitator =
(mInputLogic.mSuggest == null) ? null : mInputLogic.mSuggest.mDictionaryFacilitator;
(oldSuggest == null) ? null : oldSuggest.mDictionaryFacilitator;
// Creates new dictionary facilitator for the new locale.
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
new DictionaryFacilitatorForSuggest(this /* context */, locale, settingsValues,
@ -603,7 +607,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (settingsValues.mCorrectionEnabled) {
newSuggest.setAutoCorrectionThreshold(settingsValues.mAutoCorrectionThreshold);
}
resetSuggest(newSuggest);
replaceSuggest(newSuggest);
}
/* package private */ void resetSuggestMainDict() {
@ -611,10 +615,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mInputLogic.mSuggest.mDictionaryFacilitator;
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
new DictionaryFacilitatorForSuggest(this /* listener */, oldDictionaryFacilitator);
resetSuggest(new Suggest(mInputLogic.mSuggest /* oldSuggest */, dictionaryFacilitator));
replaceSuggest(new Suggest(mInputLogic.mSuggest /* oldSuggest */, dictionaryFacilitator));
}
private void resetSuggest(final Suggest newSuggest) {
private void replaceSuggest(final Suggest newSuggest) {
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.getInstance().initDictionary(newSuggest.mDictionaryFacilitator);
}
@ -1719,7 +1723,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
new DictionaryFacilitatorForSuggest(this, locale, mSettings.getCurrent(),
this /* listener */, oldDictionaryFacilitator);
resetSuggest(new Suggest(locale, dictionaryFacilitator));
replaceSuggest(new Suggest(locale, dictionaryFacilitator));
}
// DO NOT USE THIS for any other purpose than testing.

View File

@ -59,6 +59,7 @@ public final class Suggest {
// Locale used for upper- and title-casing words
public final Locale mLocale;
// TODO: Move dictionaryFacilitator constructing logics from LatinIME to Suggest.
public Suggest(final Locale locale,
final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
mLocale = locale;