From af1a06d7e1e57d420da1cbbe89ea8e221a528496 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 2 Sep 2014 18:03:49 +0900 Subject: [PATCH] Enable personalization for multilingual users. Bug: 16547557 Change-Id: I03eda34edc902fc4732d16d234216344d8d84d88 --- .../PersonalizationDictionaryUpdater.java | 9 +-------- .../latin/DictionaryFacilitator.java | 6 +++++- .../com/android/inputmethod/latin/LatinIME.java | 9 +++------ .../PersonalizationDictionaryFacilitator.java | 17 ++++++++++++++--- .../PersonalizationDataChunk.java | 2 ++ 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/java-overridable/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdater.java b/java-overridable/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdater.java index c97a0d232..8b66cff53 100644 --- a/java-overridable/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdater.java +++ b/java-overridable/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdater.java @@ -16,8 +16,6 @@ package com.android.inputmethod.latin.personalization; -import java.util.Locale; - import android.content.Context; import com.android.inputmethod.latin.DictionaryFacilitator; @@ -33,12 +31,7 @@ public class PersonalizationDictionaryUpdater { mDictionaryFacilitator = dictionaryFacilitator; } - public Locale getLocale() { - return null; - } - - public void onLoadSettings(final boolean usePersonalizedDicts, - final boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes) { + public void onLoadSettings(final boolean usePersonalizedDicts) { if (!mDictCleared) { // Clear and never update the personalization dictionary. PersonalizationHelper.removeAllPersonalizationDictionaries(mContext); diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java index e1acf3da8..46428839f 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java @@ -189,6 +189,10 @@ public class DictionaryFacilitator { mPersonalizationDictionaryFacilitator.updateEnabledSubtypes(enabledSubtypes); } + public void setIsMonolingualUser(final boolean isMonolingualUser) { + mPersonalizationDictionaryFacilitator.setIsMonolingualUser(isMonolingualUser); + } + public Locale getLocale() { return mDictionaryGroup.mLocale; } @@ -600,7 +604,7 @@ public class DictionaryFacilitator { final SpacingAndPunctuations spacingAndPunctuations, final AddMultipleDictionaryEntriesCallback callback) { mPersonalizationDictionaryFacilitator.addEntriesToPersonalizationDictionariesToUpdate( - personalizationDataChunk, spacingAndPunctuations, callback); + getLocale(), personalizationDataChunk, spacingAndPunctuations, callback); } public void addPhraseToContextualDictionary(final String[] phrase, final int probability, diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 67e2ca5c7..d2104997c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -614,9 +614,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void refreshPersonalizationDictionarySession( final SettingsValues currentSettingsValues) { - mPersonalizationDictionaryUpdater.onLoadSettings( - currentSettingsValues.mUsePersonalizedDicts, + mDictionaryFacilitator.setIsMonolingualUser( mSubtypeSwitcher.isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes()); + mPersonalizationDictionaryUpdater.onLoadSettings( + currentSettingsValues.mUsePersonalizedDicts); mContextualDictionaryUpdater.onLoadSettings(currentSettingsValues.mUsePersonalizedDicts); final boolean shouldKeepUserHistoryDictionaries; if (currentSettingsValues.mUsePersonalizedDicts) { @@ -734,10 +735,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen cleanupInternalStateForFinishInput(); } } - // TODO: Remove this test. - if (!conf.locale.equals(mPersonalizationDictionaryUpdater.getLocale())) { - refreshPersonalizationDictionarySession(settingsValues); - } super.onConfigurationChanged(conf); } diff --git a/java/src/com/android/inputmethod/latin/PersonalizationDictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/PersonalizationDictionaryFacilitator.java index 03c108f0e..aa8e312a4 100644 --- a/java/src/com/android/inputmethod/latin/PersonalizationDictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/PersonalizationDictionaryFacilitator.java @@ -44,6 +44,7 @@ public class PersonalizationDictionaryFacilitator { private final HashMap> mLangToLocalesMap = new HashMap<>(); private final HashMap mPersonalizationDictsToUpdate = new HashMap<>(); + private boolean mIsMonolingualUser = false;; PersonalizationDictionaryFacilitator(final Context context, final DistracterFilter distracterFilter) { @@ -81,6 +82,10 @@ public class PersonalizationDictionaryFacilitator { } } + public void setIsMonolingualUser(final boolean isMonolingualUser) { + mIsMonolingualUser = isMonolingualUser; + } + /** * Flush personalization dictionaries to dictionary files. Close dictionaries after writing * files except the dictionary that is used for generating suggestions. @@ -141,12 +146,18 @@ public class PersonalizationDictionaryFacilitator { personalizationDict.addMultipleDictionaryEntriesDynamically(languageModelParams, callback); } - public void addEntriesToPersonalizationDictionariesToUpdate( + public void addEntriesToPersonalizationDictionariesToUpdate(final Locale defaultLocale, final PersonalizationDataChunk personalizationDataChunk, final SpacingAndPunctuations spacingAndPunctuations, final AddMultipleDictionaryEntriesCallback callback) { - final HashSet locales = - mLangToLocalesMap.get(personalizationDataChunk.mDetectedLanguage); + final String language = personalizationDataChunk.mDetectedLanguage; + final HashSet locales; + if (mIsMonolingualUser && PersonalizationDataChunk.LANGUAGE_UNKNOWN.equals(language) + && mLangToLocalesMap.size() == 1) { + locales = mLangToLocalesMap.get(defaultLocale.getLanguage()); + } else { + locales = mLangToLocalesMap.get(language); + } if (locales == null || locales.isEmpty()) { if (callback != null) { callback.onFinished(); diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDataChunk.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDataChunk.java index 6f4b09741..734ed5583 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDataChunk.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDataChunk.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.List; public class PersonalizationDataChunk { + public static final String LANGUAGE_UNKNOWN = ""; + public final boolean mInputByUser; public final List mTokens; public final int mTimestampInSeconds;