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.content.SharedPreferences;
|
2013-08-05 07:01:30 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2013-07-26 09:11:06 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.lang.ref.SoftReference;
|
|
|
|
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-07-31 05:42:50 +00:00
|
|
|
private static final ConcurrentHashMap<String, SoftReference<UserHistoryPredictionDictionary>>
|
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-08-01 06:38:40 +00:00
|
|
|
private static final ConcurrentHashMap<String,
|
|
|
|
SoftReference<PersonalizationPredictionDictionary>>
|
2013-08-22 07:43:19 +00:00
|
|
|
sLangPersonalizationPredictionDictCache =
|
|
|
|
CollectionUtils.newConcurrentHashMap();
|
|
|
|
|
2013-07-31 05:42:50 +00:00
|
|
|
public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary(
|
2013-07-26 09:11:06 +00:00
|
|
|
final Context context, final String locale, final SharedPreferences sp) {
|
2013-08-01 06:38:40 +00:00
|
|
|
synchronized (sLangUserHistoryDictCache) {
|
|
|
|
if (sLangUserHistoryDictCache.containsKey(locale)) {
|
2013-07-31 05:42:50 +00:00
|
|
|
final SoftReference<UserHistoryPredictionDictionary> ref =
|
2013-08-01 06:38:40 +00:00
|
|
|
sLangUserHistoryDictCache.get(locale);
|
2013-07-31 05:42:50 +00:00
|
|
|
final UserHistoryPredictionDictionary dict = ref == null ? null : ref.get();
|
2013-07-26 09:11:06 +00:00
|
|
|
if (dict != null) {
|
|
|
|
if (DEBUG) {
|
2013-08-01 06:38:40 +00:00
|
|
|
Log.w(TAG, "Use cached UserHistoryPredictionDictionary 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-07-31 05:42:50 +00:00
|
|
|
final UserHistoryPredictionDictionary dict =
|
|
|
|
new UserHistoryPredictionDictionary(context, locale, sp);
|
2013-08-01 06:38:40 +00:00
|
|
|
sLangUserHistoryDictCache.put(
|
|
|
|
locale, new SoftReference<UserHistoryPredictionDictionary>(dict));
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 03:20:18 +00:00
|
|
|
public static void registerPersonalizationDictionaryUpdateSession(final Context context,
|
2013-08-22 07:43:19 +00:00
|
|
|
final PersonalizationDictionaryUpdateSession session, String locale) {
|
|
|
|
final PersonalizationPredictionDictionary predictionDictionary =
|
|
|
|
getPersonalizationPredictionDictionary(context, locale,
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(context));
|
|
|
|
predictionDictionary.registerUpdateSession(session);
|
|
|
|
final PersonalizationDictionary dictionary =
|
|
|
|
getPersonalizationDictionary(context, locale,
|
2013-08-05 07:01:30 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(context));
|
|
|
|
dictionary.registerUpdateSession(session);
|
|
|
|
}
|
|
|
|
|
2013-08-22 07:43:19 +00:00
|
|
|
public static PersonalizationDictionary getPersonalizationDictionary(
|
2013-08-01 06:38:40 +00:00
|
|
|
final Context context, final String locale, final SharedPreferences sp) {
|
|
|
|
synchronized (sLangPersonalizationDictCache) {
|
|
|
|
if (sLangPersonalizationDictCache.containsKey(locale)) {
|
2013-08-22 07:43:19 +00:00
|
|
|
final SoftReference<PersonalizationDictionary> ref =
|
2013-08-01 06:38:40 +00:00
|
|
|
sLangPersonalizationDictCache.get(locale);
|
2013-08-22 07:43:19 +00:00
|
|
|
final PersonalizationDictionary dict = ref == null ? null : ref.get();
|
|
|
|
if (dict != null) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.w(TAG, "Use cached PersonalizationDictCache for " + locale);
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final PersonalizationDictionary dict =
|
|
|
|
new PersonalizationDictionary(context, locale, sp);
|
|
|
|
sLangPersonalizationDictCache.put(
|
|
|
|
locale, new SoftReference<PersonalizationDictionary>(dict));
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
|
|
|
|
final Context context, final String locale, final SharedPreferences sp) {
|
|
|
|
synchronized (sLangPersonalizationPredictionDictCache) {
|
|
|
|
if (sLangPersonalizationPredictionDictCache.containsKey(locale)) {
|
|
|
|
final SoftReference<PersonalizationPredictionDictionary> ref =
|
|
|
|
sLangPersonalizationPredictionDictCache.get(locale);
|
2013-08-01 06:38:40 +00:00
|
|
|
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
|
|
|
|
if (dict != null) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale);
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final PersonalizationPredictionDictionary dict =
|
|
|
|
new PersonalizationPredictionDictionary(context, locale, sp);
|
2013-08-22 07:43:19 +00:00
|
|
|
sLangPersonalizationPredictionDictCache.put(
|
2013-08-01 06:38:40 +00:00
|
|
|
locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
|
2013-07-26 09:11:06 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|