PersonalizationHelper.java revision d4528b88e132ce2f25e45455a073b81385fcbd81
1d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka/* 2d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * Copyright (C) 2013 The Android Open Source Project 3d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * 4d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * Licensed under the Apache License, Version 2.0 (the "License"); 5d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * you may not use this file except in compliance with the License. 6d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * You may obtain a copy of the License at 7d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * 8d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * http://www.apache.org/licenses/LICENSE-2.0 9d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * 10d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * Unless required by applicable law or agreed to in writing, software 11d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * distributed under the License is distributed on an "AS IS" BASIS, 12d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * See the License for the specific language governing permissions and 14d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka * limitations under the License. 15d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka */ 16d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 17d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokapackage com.android.inputmethod.latin.personalization; 18d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 19d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport com.android.inputmethod.latin.utils.CollectionUtils; 20d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 21d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport android.content.Context; 22d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport android.content.SharedPreferences; 2380aa7197b47a693ecd75910479970d3279041164Satoshi Kataokaimport android.preference.PreferenceManager; 24d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport android.util.Log; 25d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 26d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport java.lang.ref.SoftReference; 27d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokaimport java.util.concurrent.ConcurrentHashMap; 28d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 29d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataokapublic class PersonalizationDictionaryHelper { 30d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka private static final String TAG = PersonalizationDictionaryHelper.class.getSimpleName(); 31d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka private static final boolean DEBUG = false; 32d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka 3387d06afc66db68f0b30b36593095511314793517Satoshi Kataoka private static final ConcurrentHashMap<String, SoftReference<UserHistoryPredictionDictionary>> 3460586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap(); 3560586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka 36d4528b88e132ce2f25e45455a073b81385fcbd81Satoshi Kataoka private static final ConcurrentHashMap<String, SoftReference<PersonalizationDictionary>> 37d4528b88e132ce2f25e45455a073b81385fcbd81Satoshi Kataoka sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap(); 38d4528b88e132ce2f25e45455a073b81385fcbd81Satoshi Kataoka 3960586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka private static final ConcurrentHashMap<String, 4060586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka SoftReference<PersonalizationPredictionDictionary>> 41366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka sLangPersonalizationPredictionDictCache = 42366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka CollectionUtils.newConcurrentHashMap(); 43366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka 4487d06afc66db68f0b30b36593095511314793517Satoshi Kataoka public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary( 45d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka final Context context, final String locale, final SharedPreferences sp) { 4660586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka synchronized (sLangUserHistoryDictCache) { 4760586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka if (sLangUserHistoryDictCache.containsKey(locale)) { 4887d06afc66db68f0b30b36593095511314793517Satoshi Kataoka final SoftReference<UserHistoryPredictionDictionary> ref = 4960586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka sLangUserHistoryDictCache.get(locale); 5087d06afc66db68f0b30b36593095511314793517Satoshi Kataoka final UserHistoryPredictionDictionary dict = ref == null ? null : ref.get(); 51d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka if (dict != null) { 52d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka if (DEBUG) { 5360586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka Log.w(TAG, "Use cached UserHistoryPredictionDictionary for " + locale); 54d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka } 55d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka return dict; 56d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka } 57d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka } 5887d06afc66db68f0b30b36593095511314793517Satoshi Kataoka final UserHistoryPredictionDictionary dict = 5987d06afc66db68f0b30b36593095511314793517Satoshi Kataoka new UserHistoryPredictionDictionary(context, locale, sp); 6060586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka sLangUserHistoryDictCache.put( 6160586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka locale, new SoftReference<UserHistoryPredictionDictionary>(dict)); 6260586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka return dict; 6360586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka } 6460586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka } 6560586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka 668c4fcb108fb0da7544edf32ccafd7eb8a0178200Satoshi Kataoka public static void registerPersonalizationDictionaryUpdateSession(final Context context, 67366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final PersonalizationDictionaryUpdateSession session, String locale) { 68366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final PersonalizationPredictionDictionary predictionDictionary = 69366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka getPersonalizationPredictionDictionary(context, locale, 70366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka PreferenceManager.getDefaultSharedPreferences(context)); 71366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka predictionDictionary.registerUpdateSession(session); 72366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final PersonalizationDictionary dictionary = 73366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka getPersonalizationDictionary(context, locale, 7480aa7197b47a693ecd75910479970d3279041164Satoshi Kataoka PreferenceManager.getDefaultSharedPreferences(context)); 7580aa7197b47a693ecd75910479970d3279041164Satoshi Kataoka dictionary.registerUpdateSession(session); 7680aa7197b47a693ecd75910479970d3279041164Satoshi Kataoka } 7780aa7197b47a693ecd75910479970d3279041164Satoshi Kataoka 78366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka public static PersonalizationDictionary getPersonalizationDictionary( 7960586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka final Context context, final String locale, final SharedPreferences sp) { 8060586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka synchronized (sLangPersonalizationDictCache) { 8160586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka if (sLangPersonalizationDictCache.containsKey(locale)) { 82366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final SoftReference<PersonalizationDictionary> ref = 8360586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka sLangPersonalizationDictCache.get(locale); 84366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final PersonalizationDictionary dict = ref == null ? null : ref.get(); 85366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka if (dict != null) { 86366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka if (DEBUG) { 87366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka Log.w(TAG, "Use cached PersonalizationDictCache for " + locale); 88366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka } 89366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka return dict; 90366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka } 91366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka } 92366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final PersonalizationDictionary dict = 93366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka new PersonalizationDictionary(context, locale, sp); 94366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka sLangPersonalizationDictCache.put( 95366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka locale, new SoftReference<PersonalizationDictionary>(dict)); 96366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka return dict; 97366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka } 98366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka } 99366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka 100366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( 101366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final Context context, final String locale, final SharedPreferences sp) { 102366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka synchronized (sLangPersonalizationPredictionDictCache) { 103366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka if (sLangPersonalizationPredictionDictCache.containsKey(locale)) { 104366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka final SoftReference<PersonalizationPredictionDictionary> ref = 105366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka sLangPersonalizationPredictionDictCache.get(locale); 10660586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get(); 10760586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka if (dict != null) { 10860586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka if (DEBUG) { 10960586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale); 11060586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka } 11160586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka return dict; 11260586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka } 11360586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka } 11460586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka final PersonalizationPredictionDictionary dict = 11560586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka new PersonalizationPredictionDictionary(context, locale, sp); 116366c0c5198f43279f4671a196556124f41297c0cSatoshi Kataoka sLangPersonalizationPredictionDictCache.put( 11760586b57cf4ca4af16cf9a9261aaba9490f128bcSatoshi Kataoka locale, new SoftReference<PersonalizationPredictionDictionary>(dict)); 118d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka return dict; 119d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka } 120d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka } 121d45e4b6e5bf8d9a7edf95e400b2ce6e7b49b41e1Satoshi Kataoka} 122