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 android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-11-06 11:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.common.FileUtils;
|
2014-05-23 11:18:17 +00:00
|
|
|
|
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-10-24 00:50:35 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
/**
|
2015-02-03 01:10:38 +00:00
|
|
|
* Helps handle and manage personalized dictionaries such as {@link UserHistoryDictionary}.
|
2014-10-24 00:50:35 +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;
|
2014-10-24 00:50:35 +00:00
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>
|
2014-05-23 11:18:17 +00:00
|
|
|
sLangUserHistoryDictCache = new ConcurrentHashMap<>();
|
2013-08-22 10:51:46 +00:00
|
|
|
|
2014-10-24 00:50:35 +00:00
|
|
|
@Nonnull
|
2013-09-28 03:50:09 +00:00
|
|
|
public static UserHistoryDictionary getUserHistoryDictionary(
|
2014-10-24 00:50:35 +00:00
|
|
|
final Context context, final Locale locale, @Nullable final String accountName) {
|
|
|
|
String lookupStr = locale.toString();
|
|
|
|
if (accountName != null) {
|
|
|
|
lookupStr += "." + accountName;
|
|
|
|
}
|
2013-08-01 06:38:40 +00:00
|
|
|
synchronized (sLangUserHistoryDictCache) {
|
2014-10-24 00:50:35 +00:00
|
|
|
if (sLangUserHistoryDictCache.containsKey(lookupStr)) {
|
2013-09-28 03:50:09 +00:00
|
|
|
final SoftReference<UserHistoryDictionary> ref =
|
2014-10-24 00:50:35 +00:00
|
|
|
sLangUserHistoryDictCache.get(lookupStr);
|
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) {
|
2014-10-30 18:33:39 +00:00
|
|
|
Log.d(TAG, "Use cached UserHistoryDictionary with lookup: " + lookupStr);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 21:48:16 +00:00
|
|
|
final UserHistoryDictionary dict = new UserHistoryDictionary(
|
|
|
|
context, locale, accountName);
|
2014-10-24 00:50:35 +00:00
|
|
|
sLangUserHistoryDictCache.put(lookupStr, new SoftReference<>(dict));
|
2013-08-01 06:38:40 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:00:27 +00:00
|
|
|
public static void removeAllUserHistoryDictionaries(final Context context) {
|
2015-02-25 22:43:57 +00:00
|
|
|
synchronized (sLangUserHistoryDictCache) {
|
|
|
|
for (final ConcurrentHashMap.Entry<String, SoftReference<UserHistoryDictionary>> entry
|
|
|
|
: sLangUserHistoryDictCache.entrySet()) {
|
2013-12-25 02:44:16 +00:00
|
|
|
if (entry.getValue() != null) {
|
2015-02-25 22:43:57 +00:00
|
|
|
final UserHistoryDictionary dict = entry.getValue().get();
|
2013-12-25 02:44:16 +00:00
|
|
|
if (dict != null) {
|
2014-04-24 18:25:47 +00:00
|
|
|
dict.clear();
|
2013-12-25 02:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 22:43:57 +00:00
|
|
|
sLangUserHistoryDictCache.clear();
|
2014-05-15 11:20:13 +00:00
|
|
|
final File filesDir = context.getFilesDir();
|
|
|
|
if (filesDir == null) {
|
|
|
|
Log.e(TAG, "context.getFilesDir() returned null.");
|
2014-07-28 10:25:07 +00:00
|
|
|
return;
|
2014-05-15 11:20:13 +00:00
|
|
|
}
|
2015-02-25 22:43:57 +00:00
|
|
|
final boolean filesDeleted = FileUtils.deleteFilteredFiles(
|
|
|
|
filesDir, new DictFilter(UserHistoryDictionary.NAME));
|
|
|
|
if (!filesDeleted) {
|
|
|
|
Log.e(TAG, "Cannot remove dictionary files. filesDir: " + filesDir.getAbsolutePath()
|
|
|
|
+ ", dictNamePrefix: " + UserHistoryDictionary.NAME);
|
2013-12-25 02:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|