Remove personalized dicts when the option is turned off.

Bug: 11757851

Change-Id: I94f32859d862fe41e39845ab2bfc2d396c6aea0f
This commit is contained in:
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.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);
}

View file

@ -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<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList();

View file

@ -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) {
}
}

View file

@ -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 <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.
*/
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));
}

View file

@ -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() {

View file

@ -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;
}
}