diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 2e5e4c32f..54c654313 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -72,7 +72,8 @@ import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.inputlogic.InputLogic; import com.android.inputmethod.latin.inputlogic.SpaceState; import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; -import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegister; +import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegistrar; +import com.android.inputmethod.latin.personalization.PersonalizationHelper; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.SettingsActivity; import com.android.inputmethod.latin.settings.SettingsValues; @@ -463,7 +464,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen KeyboardSwitcher.init(this); AudioAndHapticFeedbackManager.init(this); AccessibilityUtils.init(this); - PersonalizationDictionarySessionRegister.init(this); super.onCreate(); @@ -515,10 +515,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // 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(); if (!mHandler.hasPendingReopenDictionaries() && mInputLogic.mSuggest != null) { // May need to reset dictionaries depending on the user settings. mInputLogic.mSuggest.setAdditionalDictionaries(mInputLogic.mSuggest /* oldSuggest */, - mSettings.getCurrent()); + currentSettingsValues); + } + if (currentSettingsValues.mUsePersonalizedDicts) { + PersonalizationDictionarySessionRegistrar.init(this); + } else { + PersonalizationHelper.removeAllPersonalizedDictionaries(this); + PersonalizationDictionarySessionRegistrar.resetAll(this); } } @@ -590,7 +597,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen ResearchLogger.getInstance().onDestroy(); } unregisterReceiver(mDictionaryPackInstallReceiver); - PersonalizationDictionarySessionRegister.onDestroy(this); + PersonalizationDictionarySessionRegistrar.onDestroy(this); LatinImeLogger.commit(); LatinImeLogger.onDestroy(); super.onDestroy(); @@ -610,7 +617,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mOptionsDialog.dismiss(); } } - PersonalizationDictionarySessionRegister.onConfigurationChanged(this, conf); + PersonalizationDictionarySessionRegistrar.onConfigurationChanged(this, conf); super.onConfigurationChanged(conf); } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java index 596562f1d..9b2b981d5 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java @@ -27,7 +27,7 @@ import java.util.Locale; import android.content.Context; public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase { - private static final String NAME = PersonalizationDictionary.class.getSimpleName(); + /* package */ static final String NAME = PersonalizationDictionary.class.getSimpleName(); private final ArrayList mSessions = CollectionUtils.newArrayList(); diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegistrar.java similarity index 90% rename from java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java rename to java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegistrar.java index 542bda621..76965112f 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegistrar.java @@ -19,7 +19,7 @@ package com.android.inputmethod.latin.personalization; import android.content.Context; import android.content.res.Configuration; -public class PersonalizationDictionarySessionRegister { +public class PersonalizationDictionarySessionRegistrar { public static void init(final Context context) { } @@ -32,6 +32,9 @@ public class PersonalizationDictionarySessionRegister { public static void onRemoveData(final Context context, final String type) { } + public static void resetAll(final Context context) { + } + public static void onDestroy(final Context context) { } } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java index d55cae132..38b22e5f6 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java @@ -17,10 +17,13 @@ package com.android.inputmethod.latin.personalization; import com.android.inputmethod.latin.utils.CollectionUtils; +import com.android.inputmethod.latin.utils.FileUtils; import android.content.Context; import android.util.Log; +import java.io.File; +import java.io.FilenameFilter; import java.lang.ref.SoftReference; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; @@ -96,4 +99,47 @@ public class PersonalizationHelper { return dict; } } + + public static void removeAllPersonalizedDictionaries(final Context context) { + removeAllDictionaries(context, sLangUserHistoryDictCache, + UserHistoryDictionary.NAME); + removeAllDictionaries(context, sLangPersonalizationDictCache, + PersonalizationDictionary.NAME); + } + + private static void removeAllDictionaries( + final Context context, final ConcurrentHashMap> dictionaryMap, + final String dictNamePrefix) { + synchronized (dictionaryMap) { + for (final ConcurrentHashMap.Entry> entry + : dictionaryMap.entrySet()) { + if (entry.getValue() != null) { + final DecayingExpandableBinaryDictionaryBase dict = entry.getValue().get(); + if (dict != null) { + dict.clearAndFlushDictionary(); + } + } + } + dictionaryMap.clear(); + if (!FileUtils.deleteFilteredFiles( + context.getFilesDir(), new DictFilter(dictNamePrefix))) { + Log.e(TAG, "Cannot remove all existing dictionary files. filesDir: " + + context.getFilesDir().getAbsolutePath() + ", dictNamePrefix: " + + dictNamePrefix); + } + } + } + + private static class DictFilter implements FilenameFilter { + private final String mName; + + DictFilter(final String name) { + mName = name; + } + + @Override + public boolean accept(final File dir, final String name) { + return name.startsWith(mName); + } + } } diff --git a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java index 868f21cbc..c23bc9bc0 100644 --- a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java @@ -29,8 +29,7 @@ import android.content.Context; * cancellation or manual picks. This allows the keyboard to adapt to the typist over time. */ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase { - /* package for tests */ static final String NAME = - UserHistoryDictionary.class.getSimpleName(); + /* package */ static final String NAME = UserHistoryDictionary.class.getSimpleName(); /* package */ UserHistoryDictionary(final Context context, final Locale locale) { super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale)); } diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java index 9bd2b9389..64f53b525 100644 --- a/java/src/com/android/inputmethod/latin/settings/Settings.java +++ b/java/src/com/android/inputmethod/latin/settings/Settings.java @@ -371,8 +371,12 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang } public void writeLastUsedPersonalizationToken(byte[] token) { - final String tokenStr = StringUtils.byteArrayToHexString(token); - mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply(); + if (token == null) { + mPrefs.edit().remove(PREF_LAST_USED_PERSONALIZATION_TOKEN).apply(); + } else { + final String tokenStr = StringUtils.byteArrayToHexString(token); + mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply(); + } } public byte[] readLastUsedPersonalizationToken() { diff --git a/java/src/com/android/inputmethod/latin/utils/FileUtils.java b/java/src/com/android/inputmethod/latin/utils/FileUtils.java index 83c1e7c4d..22b0fbbcd 100644 --- a/java/src/com/android/inputmethod/latin/utils/FileUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/FileUtils.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin.utils; import java.io.File; +import java.io.FilenameFilter; /** * A simple class to help with removing directories recursively. @@ -30,4 +31,18 @@ public class FileUtils { } return path.delete(); } + + public static boolean deleteFilteredFiles(final File dir, final FilenameFilter fileNameFilter) { + if (!dir.isDirectory()) { + return false; + } + final File[] files = dir.listFiles(fileNameFilter); + boolean hasDeletedAllFiles = true; + for (final File file : files) { + if (!deleteRecursively(file)) { + hasDeletedAllFiles = false; + } + } + return hasDeletedAllFiles; + } }