2013-07-26 09:11:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.personalization;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2013-12-25 02:44:16 +00:00
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
2013-07-26 09:11:06 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-12-25 02:44:16 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FilenameFilter;
|
2013-07-26 09:11:06 +00:00
|
|
|
import java.lang.ref.SoftReference;
|
2013-12-13 08:09:16 +00:00
|
|
|
import java.util.Locale;
|
2013-07-26 09:11:06 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2014-02-28 09:17:09 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-07-26 09:11:06 +00:00
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
public class PersonalizationHelper {
|
|
|
|
private static final String TAG = PersonalizationHelper.class.getSimpleName();
|
2013-07-26 09:11:06 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2013-09-28 03:50:09 +00:00
|
|
|
private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>
|
2013-08-01 06:38:40 +00:00
|
|
|
sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap();
|
2013-08-22 10:51:46 +00:00
|
|
|
private static final ConcurrentHashMap<String, SoftReference<PersonalizationDictionary>>
|
|
|
|
sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap();
|
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
public static UserHistoryDictionary getUserHistoryDictionary(
|
2013-12-13 08:09:16 +00:00
|
|
|
final Context context, final Locale locale) {
|
|
|
|
final String localeStr = locale.toString();
|
2013-08-01 06:38:40 +00:00
|
|
|
synchronized (sLangUserHistoryDictCache) {
|
2013-12-13 08:09:16 +00:00
|
|
|
if (sLangUserHistoryDictCache.containsKey(localeStr)) {
|
2013-09-28 03:50:09 +00:00
|
|
|
final SoftReference<UserHistoryDictionary> ref =
|
2013-12-13 08:09:16 +00:00
|
|
|
sLangUserHistoryDictCache.get(localeStr);
|
2013-09-28 03:50:09 +00:00
|
|
|
final UserHistoryDictionary dict = ref == null ? null : ref.get();
|
2013-07-26 09:11:06 +00:00
|
|
|
if (dict != null) {
|
|
|
|
if (DEBUG) {
|
2013-09-28 03:50:09 +00:00
|
|
|
Log.w(TAG, "Use cached UserHistoryDictionary for " + locale);
|
2013-07-26 09:11:06 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
dict.reloadDictionaryIfRequired();
|
2013-07-26 09:11:06 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
final UserHistoryDictionary dict = new UserHistoryDictionary(context, locale);
|
|
|
|
sLangUserHistoryDictCache.put(localeStr,
|
|
|
|
new SoftReference<UserHistoryDictionary>(dict));
|
2013-08-01 06:38:40 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 09:17:09 +00:00
|
|
|
private static int sCurrentTimestampForTesting = 0;
|
|
|
|
public static void currentTimeChangedForTesting(final int currentTimestamp) {
|
|
|
|
if (TimeUnit.MILLISECONDS.toSeconds(
|
|
|
|
DictionaryDecayBroadcastReciever.DICTIONARY_DECAY_INTERVAL)
|
|
|
|
< currentTimestamp - sCurrentTimestampForTesting) {
|
|
|
|
runGCOnAllOpenedUserHistoryDictionaries();
|
2014-05-08 08:20:43 +00:00
|
|
|
runGCOnAllOpenedPersonalizationDictionaries();
|
2014-02-28 09:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void runGCOnAllOpenedUserHistoryDictionaries() {
|
|
|
|
runGCOnAllDictionariesIfRequired(sLangUserHistoryDictCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void runGCOnAllOpenedPersonalizationDictionaries() {
|
|
|
|
runGCOnAllDictionariesIfRequired(sLangPersonalizationDictCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <T extends DecayingExpandableBinaryDictionaryBase>
|
|
|
|
void runGCOnAllDictionariesIfRequired(
|
|
|
|
final ConcurrentHashMap<String, SoftReference<T>> dictionaryMap) {
|
|
|
|
for (final ConcurrentHashMap.Entry<String, SoftReference<T>> entry
|
|
|
|
: dictionaryMap.entrySet()) {
|
|
|
|
final DecayingExpandableBinaryDictionaryBase dict = entry.getValue().get();
|
|
|
|
if (dict != null) {
|
|
|
|
dict.runGCIfRequired();
|
|
|
|
} else {
|
|
|
|
dictionaryMap.remove(entry.getKey());
|
2013-10-02 09:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 07:43:19 +00:00
|
|
|
public static PersonalizationDictionary getPersonalizationDictionary(
|
2013-12-13 08:09:16 +00:00
|
|
|
final Context context, final Locale locale) {
|
|
|
|
final String localeStr = locale.toString();
|
2013-08-01 06:38:40 +00:00
|
|
|
synchronized (sLangPersonalizationDictCache) {
|
2013-12-13 08:09:16 +00:00
|
|
|
if (sLangPersonalizationDictCache.containsKey(localeStr)) {
|
2013-08-22 07:43:19 +00:00
|
|
|
final SoftReference<PersonalizationDictionary> ref =
|
2013-12-13 08:09:16 +00:00
|
|
|
sLangPersonalizationDictCache.get(localeStr);
|
2013-08-22 07:43:19 +00:00
|
|
|
final PersonalizationDictionary dict = ref == null ? null : ref.get();
|
|
|
|
if (dict != null) {
|
|
|
|
if (DEBUG) {
|
2013-12-13 08:09:16 +00:00
|
|
|
Log.w(TAG, "Use cached PersonalizationDictionary for " + locale);
|
2013-08-22 07:43:19 +00:00
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale);
|
2013-08-22 07:43:19 +00:00
|
|
|
sLangPersonalizationDictCache.put(
|
2013-12-13 08:09:16 +00:00
|
|
|
localeStr, new SoftReference<PersonalizationDictionary>(dict));
|
2013-07-26 09:11:06 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
2013-12-25 02:44:16 +00:00
|
|
|
|
2014-02-19 12:00:27 +00:00
|
|
|
public static void removeAllPersonalizationDictionaries(final Context context) {
|
2013-12-25 02:44:16 +00:00
|
|
|
removeAllDictionaries(context, sLangPersonalizationDictCache,
|
|
|
|
PersonalizationDictionary.NAME);
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:00:27 +00:00
|
|
|
public static void removeAllUserHistoryDictionaries(final Context context) {
|
|
|
|
removeAllDictionaries(context, sLangUserHistoryDictCache,
|
|
|
|
UserHistoryDictionary.NAME);
|
|
|
|
}
|
|
|
|
|
2013-12-25 02:44:16 +00:00
|
|
|
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) {
|
2014-04-24 18:25:47 +00:00
|
|
|
dict.clear();
|
2013-12-25 02:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 09:11:06 +00:00
|
|
|
}
|