Remove personalized dicts when the option is turned off.

Bug: 11757851

Change-Id: I94f32859d862fe41e39845ab2bfc2d396c6aea0f
main
Keisuke Kuroyanagi 2013-12-25 11:44:16 +09:00
parent afca1ddd23
commit f1457e7a22
7 changed files with 85 additions and 11 deletions

View File

@ -72,7 +72,8 @@ import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.inputlogic.InputLogic; import com.android.inputmethod.latin.inputlogic.InputLogic;
import com.android.inputmethod.latin.inputlogic.SpaceState; import com.android.inputmethod.latin.inputlogic.SpaceState;
import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; 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.Settings;
import com.android.inputmethod.latin.settings.SettingsActivity; import com.android.inputmethod.latin.settings.SettingsActivity;
import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.settings.SettingsValues;
@ -463,7 +464,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
KeyboardSwitcher.init(this); KeyboardSwitcher.init(this);
AudioAndHapticFeedbackManager.init(this); AudioAndHapticFeedbackManager.init(this);
AccessibilityUtils.init(this); AccessibilityUtils.init(this);
PersonalizationDictionarySessionRegister.init(this);
super.onCreate(); 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 // 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 // be done later inside {@see #initSuggest()} when the reopenDictionaries message is
// processed. // processed.
final SettingsValues currentSettingsValues = mSettings.getCurrent();
if (!mHandler.hasPendingReopenDictionaries() && mInputLogic.mSuggest != null) { if (!mHandler.hasPendingReopenDictionaries() && mInputLogic.mSuggest != null) {
// May need to reset dictionaries depending on the user settings. // May need to reset dictionaries depending on the user settings.
mInputLogic.mSuggest.setAdditionalDictionaries(mInputLogic.mSuggest /* oldSuggest */, 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(); ResearchLogger.getInstance().onDestroy();
} }
unregisterReceiver(mDictionaryPackInstallReceiver); unregisterReceiver(mDictionaryPackInstallReceiver);
PersonalizationDictionarySessionRegister.onDestroy(this); PersonalizationDictionarySessionRegistrar.onDestroy(this);
LatinImeLogger.commit(); LatinImeLogger.commit();
LatinImeLogger.onDestroy(); LatinImeLogger.onDestroy();
super.onDestroy(); super.onDestroy();
@ -610,7 +617,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mOptionsDialog.dismiss(); mOptionsDialog.dismiss();
} }
} }
PersonalizationDictionarySessionRegister.onConfigurationChanged(this, conf); PersonalizationDictionarySessionRegistrar.onConfigurationChanged(this, conf);
super.onConfigurationChanged(conf); super.onConfigurationChanged(conf);
} }

View File

@ -27,7 +27,7 @@ import java.util.Locale;
import android.content.Context; import android.content.Context;
public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase { public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase {
private static final String NAME = PersonalizationDictionary.class.getSimpleName(); /* package */ static final String NAME = PersonalizationDictionary.class.getSimpleName();
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions = private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList(); CollectionUtils.newArrayList();

View File

@ -19,7 +19,7 @@ package com.android.inputmethod.latin.personalization;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
public class PersonalizationDictionarySessionRegister { public class PersonalizationDictionarySessionRegistrar {
public static void init(final Context context) { 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 onRemoveData(final Context context, final String type) {
} }
public static void resetAll(final Context context) {
}
public static void onDestroy(final Context context) { public static void onDestroy(final Context context) {
} }
} }

View File

@ -17,10 +17,13 @@
package com.android.inputmethod.latin.personalization; package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.FileUtils;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import java.io.File;
import java.io.FilenameFilter;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -96,4 +99,47 @@ public class PersonalizationHelper {
return dict; return dict;
} }
} }
public static void removeAllPersonalizedDictionaries(final Context context) {
removeAllDictionaries(context, sLangUserHistoryDictCache,
UserHistoryDictionary.NAME);
removeAllDictionaries(context, sLangPersonalizationDictCache,
PersonalizationDictionary.NAME);
}
private static <T extends DecayingExpandableBinaryDictionaryBase> void removeAllDictionaries(
final Context context, final ConcurrentHashMap<String, SoftReference<T>> dictionaryMap,
final String dictNamePrefix) {
synchronized (dictionaryMap) {
for (final ConcurrentHashMap.Entry<String, SoftReference<T>> 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);
}
}
} }

View File

@ -29,8 +29,7 @@ import android.content.Context;
* cancellation or manual picks. This allows the keyboard to adapt to the typist over time. * cancellation or manual picks. This allows the keyboard to adapt to the typist over time.
*/ */
public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase { public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase {
/* package for tests */ static final String NAME = /* package */ static final String NAME = UserHistoryDictionary.class.getSimpleName();
UserHistoryDictionary.class.getSimpleName();
/* package */ UserHistoryDictionary(final Context context, final Locale locale) { /* package */ UserHistoryDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale)); super(context, locale, Dictionary.TYPE_USER_HISTORY, getDictNameWithLocale(NAME, locale));
} }

View File

@ -371,8 +371,12 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
} }
public void writeLastUsedPersonalizationToken(byte[] token) { public void writeLastUsedPersonalizationToken(byte[] token) {
final String tokenStr = StringUtils.byteArrayToHexString(token); if (token == null) {
mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply(); 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() { public byte[] readLastUsedPersonalizationToken() {

View File

@ -17,6 +17,7 @@
package com.android.inputmethod.latin.utils; package com.android.inputmethod.latin.utils;
import java.io.File; import java.io.File;
import java.io.FilenameFilter;
/** /**
* A simple class to help with removing directories recursively. * A simple class to help with removing directories recursively.
@ -30,4 +31,18 @@ public class FileUtils {
} }
return path.delete(); 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;
}
} }