1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.personalization; 18 19 import com.android.inputmethod.latin.utils.CollectionUtils; 20 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.preference.PreferenceManager; 24 import android.util.Log; 25 26 import java.lang.ref.SoftReference; 27 import java.util.concurrent.ConcurrentHashMap; 28 29 public class PersonalizationHelper { 30 private static final String TAG = PersonalizationHelper.class.getSimpleName(); 31 private static final boolean DEBUG = false; 32 private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>> 33 sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap(); 34 35 private static final ConcurrentHashMap<String, SoftReference<PersonalizationDictionary>> 36 sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap(); 37 38 private static final ConcurrentHashMap<String, 39 SoftReference<PersonalizationPredictionDictionary>> 40 sLangPersonalizationPredictionDictCache = 41 CollectionUtils.newConcurrentHashMap(); 42 43 public static UserHistoryDictionary getUserHistoryDictionary( 44 final Context context, final String locale, final SharedPreferences sp) { 45 synchronized (sLangUserHistoryDictCache) { 46 if (sLangUserHistoryDictCache.containsKey(locale)) { 47 final SoftReference<UserHistoryDictionary> ref = 48 sLangUserHistoryDictCache.get(locale); 49 final UserHistoryDictionary dict = ref == null ? null : ref.get(); 50 if (dict != null) { 51 if (DEBUG) { 52 Log.w(TAG, "Use cached UserHistoryDictionary for " + locale); 53 } 54 dict.reloadDictionaryIfRequired(); 55 return dict; 56 } 57 } 58 final UserHistoryDictionary dict = new UserHistoryDictionary(context, locale, sp); 59 sLangUserHistoryDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict)); 60 return dict; 61 } 62 } 63 64 public static void tryDecayingAllOpeningUserHistoryDictionary() { 65 for (final ConcurrentHashMap.Entry<String, SoftReference<UserHistoryDictionary>> entry 66 : sLangUserHistoryDictCache.entrySet()) { 67 if (entry.getValue() != null) { 68 final UserHistoryDictionary dict = entry.getValue().get(); 69 if (dict != null) { 70 dict.decayIfNeeded(); 71 } 72 } 73 } 74 } 75 76 public static void registerPersonalizationDictionaryUpdateSession(final Context context, 77 final PersonalizationDictionaryUpdateSession session, String locale) { 78 final PersonalizationPredictionDictionary predictionDictionary = 79 getPersonalizationPredictionDictionary(context, locale, 80 PreferenceManager.getDefaultSharedPreferences(context)); 81 predictionDictionary.registerUpdateSession(session); 82 final PersonalizationDictionary dictionary = 83 getPersonalizationDictionary(context, locale, 84 PreferenceManager.getDefaultSharedPreferences(context)); 85 dictionary.registerUpdateSession(session); 86 } 87 88 public static PersonalizationDictionary getPersonalizationDictionary( 89 final Context context, final String locale, final SharedPreferences sp) { 90 synchronized (sLangPersonalizationDictCache) { 91 if (sLangPersonalizationDictCache.containsKey(locale)) { 92 final SoftReference<PersonalizationDictionary> ref = 93 sLangPersonalizationDictCache.get(locale); 94 final PersonalizationDictionary dict = ref == null ? null : ref.get(); 95 if (dict != null) { 96 if (DEBUG) { 97 Log.w(TAG, "Use cached PersonalizationDictCache for " + locale); 98 } 99 return dict; 100 } 101 } 102 final PersonalizationDictionary dict = 103 new PersonalizationDictionary(context, locale, sp); 104 sLangPersonalizationDictCache.put( 105 locale, new SoftReference<PersonalizationDictionary>(dict)); 106 return dict; 107 } 108 } 109 110 public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( 111 final Context context, final String locale, final SharedPreferences sp) { 112 synchronized (sLangPersonalizationPredictionDictCache) { 113 if (sLangPersonalizationPredictionDictCache.containsKey(locale)) { 114 final SoftReference<PersonalizationPredictionDictionary> ref = 115 sLangPersonalizationPredictionDictCache.get(locale); 116 final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get(); 117 if (dict != null) { 118 if (DEBUG) { 119 Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale); 120 } 121 return dict; 122 } 123 } 124 final PersonalizationPredictionDictionary dict = 125 new PersonalizationPredictionDictionary(context, locale, sp); 126 sLangPersonalizationPredictionDictCache.put( 127 locale, new SoftReference<PersonalizationPredictionDictionary>(dict)); 128 return dict; 129 } 130 } 131 } 132