2014-08-27 13:00:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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;
|
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache for dictionary facilitators of multiple locales.
|
2015-02-06 19:18:06 +00:00
|
|
|
* This class automatically creates and releases up to 3 facilitator instances using LRU policy.
|
2014-08-27 13:00:48 +00:00
|
|
|
*/
|
|
|
|
public class DictionaryFacilitatorLruCache {
|
2015-02-06 19:18:06 +00:00
|
|
|
private static final String TAG = "DictionaryFacilitatorLruCache";
|
2014-08-27 13:00:48 +00:00
|
|
|
private static final int WAIT_FOR_LOADING_MAIN_DICT_IN_MILLISECONDS = 1000;
|
|
|
|
private static final int MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT = 5;
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final String mDictionaryNamePrefix;
|
|
|
|
private final Object mLock = new Object();
|
2015-02-20 01:09:03 +00:00
|
|
|
private final DictionaryFacilitator mDictionaryFacilitator;
|
2015-02-20 23:13:40 +00:00
|
|
|
private boolean mUseContactsDictionary;
|
2015-02-20 01:09:03 +00:00
|
|
|
private Locale mLocale;
|
2014-08-27 13:00:48 +00:00
|
|
|
|
2015-02-06 19:18:06 +00:00
|
|
|
public DictionaryFacilitatorLruCache(final Context context, final String dictionaryNamePrefix) {
|
2014-08-27 13:00:48 +00:00
|
|
|
mContext = context;
|
|
|
|
mDictionaryNamePrefix = dictionaryNamePrefix;
|
2015-02-23 23:14:21 +00:00
|
|
|
mDictionaryFacilitator = DictionaryFacilitatorProvider.getDictionaryFacilitator(
|
|
|
|
true /* isNeededForSpellChecking */);
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
private static void waitForLoadingMainDictionary(
|
|
|
|
final DictionaryFacilitator dictionaryFacilitator) {
|
2014-08-27 13:00:48 +00:00
|
|
|
for (int i = 0; i < MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT; i++) {
|
|
|
|
try {
|
2014-09-11 09:51:31 +00:00
|
|
|
dictionaryFacilitator.waitForLoadingMainDictionaries(
|
2014-08-27 13:00:48 +00:00
|
|
|
WAIT_FOR_LOADING_MAIN_DICT_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
|
|
|
|
return;
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.i(TAG, "Interrupted during waiting for loading main dictionary.", e);
|
|
|
|
if (i < MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT - 1) {
|
|
|
|
Log.i(TAG, "Retry", e);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Give up retrying. Retried "
|
|
|
|
+ MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT + " times.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:09:03 +00:00
|
|
|
private void resetDictionariesForLocaleLocked() {
|
2015-02-24 23:56:43 +00:00
|
|
|
// Nothing to do if the locale is null. This would be the case before any get() calls.
|
|
|
|
if (mLocale != null) {
|
|
|
|
// Note: Given that personalized dictionaries are not used here; we can pass null account.
|
2015-03-11 23:11:50 +00:00
|
|
|
mDictionaryFacilitator.resetDictionaries(mContext, mLocale,
|
2015-02-24 23:56:43 +00:00
|
|
|
mUseContactsDictionary, false /* usePersonalizedDicts */,
|
|
|
|
false /* forceReloadMainDictionary */, null /* account */,
|
|
|
|
mDictionaryNamePrefix, null /* listener */);
|
|
|
|
}
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 23:13:40 +00:00
|
|
|
public void setUseContactsDictionary(final boolean useContactsDictionary) {
|
2014-08-27 13:00:48 +00:00
|
|
|
synchronized (mLock) {
|
2015-02-20 23:13:40 +00:00
|
|
|
if (mUseContactsDictionary == useContactsDictionary) {
|
2015-02-20 01:09:03 +00:00
|
|
|
// The value has not been changed.
|
|
|
|
return;
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
2015-02-20 23:13:40 +00:00
|
|
|
mUseContactsDictionary = useContactsDictionary;
|
2015-02-20 01:09:03 +00:00
|
|
|
resetDictionariesForLocaleLocked();
|
|
|
|
waitForLoadingMainDictionary(mDictionaryFacilitator);
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DictionaryFacilitator get(final Locale locale) {
|
|
|
|
synchronized (mLock) {
|
2015-03-11 23:11:50 +00:00
|
|
|
if (!mDictionaryFacilitator.isForLocale(locale)) {
|
2015-02-20 01:09:03 +00:00
|
|
|
mLocale = locale;
|
|
|
|
resetDictionariesForLocaleLocked();
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
2015-02-20 01:09:03 +00:00
|
|
|
waitForLoadingMainDictionary(mDictionaryFacilitator);
|
|
|
|
return mDictionaryFacilitator;
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:09:03 +00:00
|
|
|
public void closeDictionaries() {
|
2014-08-27 13:00:48 +00:00
|
|
|
synchronized (mLock) {
|
2015-02-20 01:09:03 +00:00
|
|
|
mDictionaryFacilitator.closeDictionaries();
|
2014-08-27 13:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|