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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:06:08 +00:00
|
|
|
public static void tryDecayingAllOpeningUserHistoryDictionary() {
|
|
|
|
for (final ConcurrentHashMap.Entry<String, SoftReference<UserHistoryDictionary>> entry
|
|
|
|
: sLangUserHistoryDictCache.entrySet()) {
|
|
|
|
if (entry.getValue() != null) {
|
|
|
|
final UserHistoryDictionary dict = entry.getValue().get();
|
|
|
|
if (dict != null) {
|
|
|
|
dict.decayIfNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 03:20:18 +00:00
|
|
|
public static void registerPersonalizationDictionaryUpdateSession(final Context context,
|
2013-12-13 08:09:16 +00:00
|
|
|
final PersonalizationDictionaryUpdateSession session, final Locale locale) {
|
|
|
|
final PersonalizationDictionary personalizationDictionary =
|
|
|
|
getPersonalizationDictionary(context, locale);
|
|
|
|
personalizationDictionary.registerUpdateSession(session);
|
2013-08-05 07:01:30 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|