2012-03-24 06:13:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-03-24 06:13:40 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-24 06:13:40 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2012-03-24 06:13:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2014-02-04 12:36:04 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictionaryHeader;
|
2013-09-24 13:57:15 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2014-02-06 08:55:45 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
|
2014-02-06 06:13:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WordProperty;
|
2013-09-24 13:57:15 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2013-09-09 04:04:28 +00:00
|
|
|
import com.android.inputmethod.latin.utils.AsyncResultHolder;
|
2014-03-05 09:19:34 +00:00
|
|
|
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2014-02-06 08:55:45 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CombinedFormatUtils;
|
2014-02-24 11:52:49 +00:00
|
|
|
import com.android.inputmethod.latin.utils.ExecutorUtils;
|
2013-12-06 06:05:52 +00:00
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
2014-01-15 07:04:05 +00:00
|
|
|
import com.android.inputmethod.latin.utils.LanguageModelParam;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2012-04-23 17:45:48 +00:00
|
|
|
import java.util.ArrayList;
|
2013-09-24 13:57:15 +00:00
|
|
|
import java.util.HashMap;
|
2013-12-03 10:22:17 +00:00
|
|
|
import java.util.Locale;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.Map;
|
2013-09-09 04:04:28 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2013-12-12 08:08:51 +00:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2013-12-03 10:22:17 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-09-27 12:44:26 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2013-08-26 09:50:22 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for an expandable dictionary that can be created and updated dynamically
|
|
|
|
* during runtime. When updated it automatically generates a new binary dictionary to handle future
|
|
|
|
* queries in native code. This binary dictionary is written to internal storage, and potentially
|
|
|
|
* shared across multiple ExpandableBinaryDictionary instances. Updates to each dictionary filename
|
|
|
|
* are controlled across multiple instances to ensure that only one instance can update the same
|
|
|
|
* dictionary at the same time.
|
|
|
|
*/
|
|
|
|
abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|
|
|
|
|
|
|
/** Used for Log actions from this class */
|
|
|
|
private static final String TAG = ExpandableBinaryDictionary.class.getSimpleName();
|
|
|
|
|
|
|
|
/** Whether to print debug output to log */
|
|
|
|
private static boolean DEBUG = false;
|
2013-12-02 09:45:32 +00:00
|
|
|
private static final boolean DBG_STRESS_TEST = false;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-09-09 04:04:28 +00:00
|
|
|
private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100;
|
2014-02-20 09:36:46 +00:00
|
|
|
private static final int TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS = 10000;
|
2013-09-09 04:04:28 +00:00
|
|
|
|
2014-02-28 12:06:03 +00:00
|
|
|
private static final int DEFAULT_MAX_UNIGRAM_COUNT = 10000;
|
|
|
|
private static final int DEFAULT_MAX_BIGRAM_COUNT = 10000;
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2012-12-17 08:43:09 +00:00
|
|
|
* The maximum length of a word in this dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-07-26 03:35:11 +00:00
|
|
|
protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-12-05 09:02:20 +00:00
|
|
|
private static final int DICTIONARY_FORMAT_VERSION = FormatSpec.VERSION4;
|
2013-09-26 03:59:02 +00:00
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-09-27 12:44:26 +00:00
|
|
|
* A static map of update controllers, each of which records the time of accesses to a single
|
|
|
|
* binary dictionary file and tracks whether the file is regenerating. The key for this map is
|
2013-12-11 10:30:18 +00:00
|
|
|
* the dictionary name and the value is the shared dictionary time recorder associated with
|
|
|
|
* that dictionary name.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-27 12:44:26 +00:00
|
|
|
private static final ConcurrentHashMap<String, DictionaryUpdateController>
|
2013-12-11 10:30:18 +00:00
|
|
|
sDictNameDictionaryUpdateControllerMap = CollectionUtils.newConcurrentHashMap();
|
2013-09-09 04:04:28 +00:00
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/** The application context. */
|
|
|
|
protected final Context mContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The binary dictionary generated dynamically from the fusion dictionary. This is used to
|
|
|
|
* answer unigram and bigram queries.
|
|
|
|
*/
|
|
|
|
private BinaryDictionary mBinaryDictionary;
|
|
|
|
|
|
|
|
/**
|
2013-12-11 10:30:18 +00:00
|
|
|
* The name of this dictionary, used as a part of the filename for storing the binary
|
|
|
|
* dictionary. Multiple dictionary instances with the same name is supported, with access
|
|
|
|
* controlled by DictionaryUpdateController.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-12-11 10:30:18 +00:00
|
|
|
private final String mDictName;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-12-03 10:22:17 +00:00
|
|
|
/** Dictionary locale */
|
|
|
|
private final Locale mLocale;
|
|
|
|
|
2013-12-12 11:14:06 +00:00
|
|
|
/** Dictionary file */
|
|
|
|
private final File mDictFile;
|
|
|
|
|
2013-09-09 03:26:40 +00:00
|
|
|
// TODO: remove, once dynamic operations is serialized
|
2013-09-27 12:44:26 +00:00
|
|
|
/** Controls updating the shared binary dictionary file across multiple instances. */
|
2013-12-11 10:30:18 +00:00
|
|
|
private final DictionaryUpdateController mDictNameDictionaryUpdateController;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-09-09 03:26:40 +00:00
|
|
|
// TODO: remove, once dynamic operations is serialized
|
2013-09-27 12:44:26 +00:00
|
|
|
/** Controls updating the local binary dictionary for this instance. */
|
|
|
|
private final DictionaryUpdateController mPerInstanceDictionaryUpdateController =
|
|
|
|
new DictionaryUpdateController();
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-08-19 01:17:02 +00:00
|
|
|
/* A extension for a binary dictionary file. */
|
2014-02-13 07:16:44 +00:00
|
|
|
protected static final String DICT_FILE_EXTENSION = ".dict";
|
2013-08-19 01:17:02 +00:00
|
|
|
|
2013-09-09 04:04:28 +00:00
|
|
|
private final AtomicReference<Runnable> mUnfinishedFlushingTask =
|
|
|
|
new AtomicReference<Runnable>();
|
2013-08-26 09:50:22 +00:00
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Abstract method for loading initial contents of a given dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
protected abstract void loadInitialContentsLocked();
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2012-04-27 06:50:21 +00:00
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Indicates that the source dictionary contents have changed and a rebuild of the binary file
|
|
|
|
* is required. If it returns false, the next reload will only read the current binary
|
|
|
|
* dictionary from file. Note that the shared binary dictionary is locked when this is called.
|
2012-04-27 06:50:21 +00:00
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
protected abstract boolean haveContentsChanged();
|
2012-04-27 06:50:21 +00:00
|
|
|
|
2014-01-17 18:30:16 +00:00
|
|
|
private boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) {
|
|
|
|
return formatVersion == FormatSpec.VERSION4;
|
2013-11-28 11:40:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 08:46:35 +00:00
|
|
|
private boolean needsToMigrateDictionary(final int formatVersion) {
|
|
|
|
// TODO: Check version.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
public boolean isValidDictionaryLocked() {
|
2013-12-05 09:02:20 +00:00
|
|
|
return mBinaryDictionary.isValidDictionary();
|
2013-12-05 06:14:29 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-12-11 10:30:18 +00:00
|
|
|
* Gets the dictionary update controller for the given dictionary name.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-27 12:44:26 +00:00
|
|
|
private static DictionaryUpdateController getDictionaryUpdateController(
|
2013-12-11 10:30:18 +00:00
|
|
|
final String dictName) {
|
|
|
|
DictionaryUpdateController recorder = sDictNameDictionaryUpdateControllerMap.get(dictName);
|
2013-09-09 04:04:28 +00:00
|
|
|
if (recorder == null) {
|
2013-12-11 10:30:18 +00:00
|
|
|
synchronized(sDictNameDictionaryUpdateControllerMap) {
|
2013-09-27 12:44:26 +00:00
|
|
|
recorder = new DictionaryUpdateController();
|
2013-12-11 10:30:18 +00:00
|
|
|
sDictNameDictionaryUpdateControllerMap.put(dictName, recorder);
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return recorder;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Creates a new expandable binary dictionary.
|
|
|
|
*
|
|
|
|
* @param context The application context of the parent.
|
2013-12-11 10:30:18 +00:00
|
|
|
* @param dictName The name of the dictionary. Multiple instances with the same
|
|
|
|
* name is supported.
|
2013-12-03 10:22:17 +00:00
|
|
|
* @param locale the dictionary locale.
|
2012-06-27 08:31:09 +00:00
|
|
|
* @param dictType the dictionary type, as a human-readable string
|
2014-02-13 03:12:13 +00:00
|
|
|
* @param dictFile dictionary file path. if null, use default dictionary path based on
|
|
|
|
* dictionary type.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-12-12 11:14:06 +00:00
|
|
|
public ExpandableBinaryDictionary(final Context context, final String dictName,
|
2014-02-27 14:21:09 +00:00
|
|
|
final Locale locale, final String dictType, final File dictFile) {
|
2012-06-27 08:31:09 +00:00
|
|
|
super(dictType);
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictName = dictName;
|
2012-03-24 06:13:40 +00:00
|
|
|
mContext = context;
|
2013-12-03 10:22:17 +00:00
|
|
|
mLocale = locale;
|
2014-02-13 07:16:44 +00:00
|
|
|
mDictFile = getDictFile(context, dictName, dictFile);
|
2012-03-24 06:13:40 +00:00
|
|
|
mBinaryDictionary = null;
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictNameDictionaryUpdateController = getDictionaryUpdateController(dictName);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:16:44 +00:00
|
|
|
public static File getDictFile(final Context context, final String dictName,
|
|
|
|
final File dictFile) {
|
|
|
|
return (dictFile != null) ? dictFile
|
|
|
|
: new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getDictName(final String name, final Locale locale,
|
2014-02-13 03:12:13 +00:00
|
|
|
final File dictFile) {
|
|
|
|
return dictFile != null ? dictFile.getName() : name + "." + locale.toString();
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Closes and cleans up the binary dictionary.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void close() {
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-24 11:52:49 +00:00
|
|
|
if (mBinaryDictionary != null) {
|
2013-09-09 04:04:28 +00:00
|
|
|
mBinaryDictionary.close();
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
protected Map<String, String> getHeaderAttributeMap() {
|
|
|
|
HashMap<String, String> attributeMap = new HashMap<String, String>();
|
2014-02-04 12:36:04 +00:00
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName);
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString());
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY,
|
2013-12-03 10:22:17 +00:00
|
|
|
String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
|
2014-02-28 12:06:03 +00:00
|
|
|
attributeMap.put(DictionaryHeader.MAX_UNIGRAM_COUNT_KEY,
|
|
|
|
String.valueOf(DEFAULT_MAX_UNIGRAM_COUNT));
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_BIGRAM_COUNT_KEY,
|
|
|
|
String.valueOf(DEFAULT_MAX_BIGRAM_COUNT));
|
2013-09-26 03:59:02 +00:00
|
|
|
return attributeMap;
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:09:37 +00:00
|
|
|
private void removeBinaryDictionaryLocked() {
|
|
|
|
if (mBinaryDictionary != null) {
|
|
|
|
mBinaryDictionary.close();
|
|
|
|
}
|
|
|
|
if (mDictFile.exists() && !FileUtils.deleteRecursively(mDictFile)) {
|
|
|
|
Log.e(TAG, "Can't remove a file: " + mDictFile.getName());
|
|
|
|
}
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
private void createBinaryDictionaryLocked() {
|
2014-03-05 09:19:34 +00:00
|
|
|
BinaryDictionaryUtils.createEmptyDictFile(mDictFile.getAbsolutePath(),
|
2014-02-27 14:21:09 +00:00
|
|
|
DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void openBinaryDictionaryLocked() {
|
|
|
|
mBinaryDictionary = new BinaryDictionary(
|
|
|
|
mDictFile.getAbsolutePath(), 0 /* offset */, mDictFile.length(),
|
|
|
|
true /* useFullEditDistance */, mLocale, mDictType, true /* isUpdatable */);
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
protected void clear() {
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-27 14:21:09 +00:00
|
|
|
removeBinaryDictionaryLocked();
|
|
|
|
createBinaryDictionaryLocked();
|
|
|
|
openBinaryDictionaryLocked();
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 11:55:34 +00:00
|
|
|
/**
|
|
|
|
* Check whether GC is needed and run GC if required.
|
|
|
|
*/
|
2013-10-02 09:06:08 +00:00
|
|
|
protected void runGCIfRequired(final boolean mindsBlockByGC) {
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-10-03 11:55:34 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCAfterAllPrioritizedTasksIfRequiredLocked(mindsBlockByGC);
|
2013-10-03 11:55:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
protected void runGCIfRequiredLocked(final boolean mindsBlockByGC) {
|
|
|
|
if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runGCAfterAllPrioritizedTasksIfRequiredLocked(final boolean mindsBlockByGC) {
|
|
|
|
// needsToRunGC() have to be called with lock.
|
2013-10-02 09:06:08 +00:00
|
|
|
if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
|
2013-11-19 09:12:07 +00:00
|
|
|
if (setProcessingLargeTaskIfNot()) {
|
2013-10-03 11:55:34 +00:00
|
|
|
// Run GC after currently existing time sensitive operations.
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).executePrioritized(new Runnable() {
|
2013-10-01 02:41:39 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
} finally {
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictNameDictionaryUpdateController.mProcessingLargeTask.set(false);
|
2013-10-01 02:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 12:06:04 +00:00
|
|
|
/**
|
2013-08-16 11:16:31 +00:00
|
|
|
* Dynamically adds a word unigram to the dictionary. May overwrite an existing entry.
|
2013-07-04 12:06:04 +00:00
|
|
|
*/
|
2013-12-04 02:47:28 +00:00
|
|
|
protected void addWordDynamically(final String word, final int frequency,
|
|
|
|
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
|
|
|
|
final boolean isBlacklisted, final int timestamp) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
|
|
|
|
addWordDynamicallyLocked(word, frequency, shortcutTarget, shortcutFreq,
|
2013-12-04 02:47:28 +00:00
|
|
|
isNotAWord, isBlacklisted, timestamp);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
protected void addWordDynamicallyLocked(final String word, final int frequency,
|
|
|
|
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
|
|
|
|
final boolean isBlacklisted, final int timestamp) {
|
|
|
|
mBinaryDictionary.addUnigramWord(word, frequency, shortcutTarget, shortcutFreq,
|
|
|
|
isNotAWord, isBlacklisted, timestamp);
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-08-16 11:16:31 +00:00
|
|
|
* Dynamically adds a word bigram in the dictionary. May overwrite an existing entry.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-08-16 11:16:31 +00:00
|
|
|
protected void addBigramDynamically(final String word0, final String word1,
|
2013-12-04 02:47:28 +00:00
|
|
|
final int frequency, final int timestamp) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
|
|
|
|
addBigramDynamicallyLocked(word0, word1, frequency, timestamp);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
protected void addBigramDynamicallyLocked(final String word0, final String word1,
|
|
|
|
final int frequency, final int timestamp) {
|
|
|
|
mBinaryDictionary.addBigramWords(word0, word1, frequency, timestamp);
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
/**
|
|
|
|
* Dynamically remove a word bigram in the dictionary.
|
|
|
|
*/
|
|
|
|
protected void removeBigramDynamically(final String word0, final String word1) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCAfterAllPrioritizedTasksIfRequiredLocked(true /* mindsBlockByGC */);
|
2013-11-14 05:18:02 +00:00
|
|
|
mBinaryDictionary.removeBigramWords(word0, word1);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 09:12:07 +00:00
|
|
|
public interface AddMultipleDictionaryEntriesCallback {
|
|
|
|
public void onFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically add multiple entries to the dictionary.
|
|
|
|
*/
|
|
|
|
protected void addMultipleDictionaryEntriesDynamically(
|
|
|
|
final ArrayList<LanguageModelParam> languageModelParams,
|
|
|
|
final AddMultipleDictionaryEntriesCallback callback) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-11-19 09:12:07 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-19 09:12:07 +00:00
|
|
|
final boolean locked = setProcessingLargeTaskIfNot();
|
|
|
|
try {
|
2013-11-26 07:03:10 +00:00
|
|
|
mBinaryDictionary.addMultipleDictionaryEntries(
|
|
|
|
languageModelParams.toArray(
|
|
|
|
new LanguageModelParam[languageModelParams.size()]));
|
2013-11-19 09:12:07 +00:00
|
|
|
} finally {
|
|
|
|
if (callback != null) {
|
|
|
|
callback.onFinished();
|
|
|
|
}
|
|
|
|
if (locked) {
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictNameDictionaryUpdateController.mProcessingLargeTask.set(false);
|
2013-11-19 09:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-09 09:25:27 +00:00
|
|
|
@Override
|
2013-09-05 08:32:30 +00:00
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
|
2013-05-01 11:36:36 +00:00
|
|
|
final String prevWord, final ProximityInfo proximityInfo,
|
2013-09-05 08:32:30 +00:00
|
|
|
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
|
2014-04-02 15:47:28 +00:00
|
|
|
final int sessionId, final float[] inOutLanguageWeight) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2013-11-19 09:12:07 +00:00
|
|
|
if (processingLargeTask()) {
|
2013-09-27 12:44:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final AsyncResultHolder<ArrayList<SuggestedWordInfo>> holder =
|
|
|
|
new AsyncResultHolder<ArrayList<SuggestedWordInfo>>();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).executePrioritized(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-11-14 05:18:02 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
holder.set(null);
|
|
|
|
return;
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
2013-11-14 05:18:02 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> binarySuggestion =
|
|
|
|
mBinaryDictionary.getSuggestionsWithSessionId(composer, prevWord,
|
|
|
|
proximityInfo, blockOffensiveWords, additionalFeaturesOptions,
|
2014-04-02 15:47:28 +00:00
|
|
|
sessionId, inOutLanguageWeight);
|
2013-11-14 05:18:02 +00:00
|
|
|
holder.set(binarySuggestion);
|
2014-02-21 06:09:37 +00:00
|
|
|
if (mBinaryDictionary.isCorrupted()) {
|
|
|
|
removeBinaryDictionaryLocked();
|
|
|
|
}
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
|
|
|
return holder.get(null, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 08:32:30 +00:00
|
|
|
@Override
|
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
|
|
|
final String prevWord, final ProximityInfo proximityInfo,
|
2014-04-02 15:47:28 +00:00
|
|
|
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
|
|
|
|
final float[] inOutLanguageWeight) {
|
2013-09-05 08:32:30 +00:00
|
|
|
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
|
2014-04-02 15:47:28 +00:00
|
|
|
additionalFeaturesOptions, 0 /* sessionId */, inOutLanguageWeight);
|
2013-09-05 08:32:30 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public boolean isValidWord(final String word) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2013-11-19 09:12:07 +00:00
|
|
|
if (processingLargeTask()) {
|
2013-09-27 12:44:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).executePrioritized(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
holder.set(isValidWordLocked(word));
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
|
|
|
return holder.get(false, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
protected boolean isValidWordLocked(final String word) {
|
2012-04-27 06:50:21 +00:00
|
|
|
if (mBinaryDictionary == null) return false;
|
|
|
|
return mBinaryDictionary.isValidWord(word);
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
protected boolean isValidBigramLocked(final String word1, final String word2) {
|
2012-04-27 06:50:21 +00:00
|
|
|
if (mBinaryDictionary == null) return false;
|
|
|
|
return mBinaryDictionary.isValidBigram(word1, word2);
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Load the current binary dictionary from internal storage in a background thread. If no binary
|
|
|
|
* dictionary exists, this method will generate one.
|
|
|
|
*/
|
|
|
|
protected void loadDictionary() {
|
2013-12-13 05:48:43 +00:00
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = System.currentTimeMillis();
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the current binary dictionary from internal storage. Assumes the dictionary file
|
|
|
|
* exists.
|
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void loadBinaryDictionaryLocked() {
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2013-12-11 10:30:18 +00:00
|
|
|
Log.d(TAG, "Loading binary dictionary: " + mDictName + " request="
|
|
|
|
+ mDictNameDictionaryUpdateController.mLastUpdateRequestTime + " update="
|
|
|
|
+ mDictNameDictionaryUpdateController.mLastUpdateTime);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2013-12-02 09:45:32 +00:00
|
|
|
if (DBG_STRESS_TEST) {
|
|
|
|
// Test if this class does not cause problems when it takes long time to load binary
|
|
|
|
// dictionary.
|
|
|
|
try {
|
2013-12-11 10:30:18 +00:00
|
|
|
Log.w(TAG, "Start stress in loading: " + mDictName);
|
2013-12-02 09:45:32 +00:00
|
|
|
Thread.sleep(15000);
|
|
|
|
Log.w(TAG, "End stress in loading");
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final BinaryDictionary oldBinaryDictionary = mBinaryDictionary;
|
2014-02-27 14:21:09 +00:00
|
|
|
openBinaryDictionaryLocked();
|
|
|
|
if (oldBinaryDictionary != null) {
|
|
|
|
oldBinaryDictionary.close();
|
|
|
|
}
|
2014-03-27 08:46:35 +00:00
|
|
|
if (mBinaryDictionary.isValidDictionary()
|
|
|
|
&& needsToMigrateDictionary(mBinaryDictionary.getFormatVersion())) {
|
|
|
|
mBinaryDictionary.migrateTo(DICTIONARY_FORMAT_VERSION);
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Create a new binary dictionary and load initial contents.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void createNewDictionaryLocked() {
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2013-12-11 10:30:18 +00:00
|
|
|
Log.d(TAG, "Generating binary dictionary: " + mDictName + " request="
|
|
|
|
+ mDictNameDictionaryUpdateController.mLastUpdateRequestTime + " update="
|
|
|
|
+ mDictNameDictionaryUpdateController.mLastUpdateTime);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
removeBinaryDictionaryLocked();
|
|
|
|
createBinaryDictionaryLocked();
|
|
|
|
openBinaryDictionaryLocked();
|
|
|
|
loadInitialContentsLocked();
|
2014-04-21 22:21:55 +00:00
|
|
|
// Run GC and flush to file when initial contents have been loaded.
|
|
|
|
mBinaryDictionary.flushWithGCIfHasUpdated();
|
2014-02-27 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void flushDictionaryLocked() {
|
2014-03-28 03:17:44 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
2014-02-27 14:21:09 +00:00
|
|
|
mBinaryDictionary.flush();
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-27 06:50:21 +00:00
|
|
|
* Marks that the dictionary is out of date and requires a reload.
|
|
|
|
*
|
|
|
|
* @param requiresRebuild Indicates that the source dictionary content has changed and a rebuild
|
|
|
|
* of the binary file is required. If not true, the next reload process will only read
|
|
|
|
* the current binary dictionary from file.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2012-04-27 06:50:21 +00:00
|
|
|
protected void setRequiresReload(final boolean requiresRebuild) {
|
2013-12-13 05:48:43 +00:00
|
|
|
final long time = System.currentTimeMillis();
|
2013-09-27 12:44:26 +00:00
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = time;
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictNameDictionaryUpdateController.mLastUpdateRequestTime = time;
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2013-12-11 10:30:18 +00:00
|
|
|
Log.d(TAG, "Reload request: " + mDictName + ": request=" + time + " update="
|
|
|
|
+ mDictNameDictionaryUpdateController.mLastUpdateTime);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-25 18:16:30 +00:00
|
|
|
* Reloads the dictionary if required.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-09 04:04:28 +00:00
|
|
|
public final void reloadDictionaryIfRequired() {
|
2012-05-25 18:16:30 +00:00
|
|
|
if (!isReloadRequired()) return;
|
2013-11-19 09:12:07 +00:00
|
|
|
if (setProcessingLargeTaskIfNot()) {
|
2013-09-27 12:44:26 +00:00
|
|
|
reloadDictionary();
|
|
|
|
}
|
2012-05-25 18:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a dictionary reload is required.
|
|
|
|
*/
|
|
|
|
private boolean isReloadRequired() {
|
2013-09-27 12:44:26 +00:00
|
|
|
return mBinaryDictionary == null || mPerInstanceDictionaryUpdateController.isOutOfDate();
|
|
|
|
}
|
|
|
|
|
2013-11-19 09:12:07 +00:00
|
|
|
private boolean processingLargeTask() {
|
2013-12-11 10:30:18 +00:00
|
|
|
return mDictNameDictionaryUpdateController.mProcessingLargeTask.get();
|
2013-09-27 12:44:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 09:12:07 +00:00
|
|
|
// Returns whether the dictionary is being used for a large task. If true, we should not use
|
|
|
|
// this dictionary for latency sensitive operations.
|
|
|
|
private boolean setProcessingLargeTaskIfNot() {
|
2013-12-11 10:30:18 +00:00
|
|
|
return mDictNameDictionaryUpdateController.mProcessingLargeTask.compareAndSet(
|
2013-09-27 12:44:26 +00:00
|
|
|
false /* expect */ , true /* update */);
|
2012-05-25 18:16:30 +00:00
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2012-05-25 18:16:30 +00:00
|
|
|
/**
|
|
|
|
* Reloads the dictionary. Access is controlled on a per dictionary file basis and supports
|
|
|
|
* concurrent calls from multiple instances that share the same dictionary file.
|
|
|
|
*/
|
2013-09-09 04:04:28 +00:00
|
|
|
private final void reloadDictionary() {
|
2012-03-24 06:13:40 +00:00
|
|
|
// Ensure that only one thread attempts to read or write to the shared binary dictionary
|
|
|
|
// file at the same time.
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-27 12:44:26 +00:00
|
|
|
try {
|
2013-12-13 05:48:43 +00:00
|
|
|
final long time = System.currentTimeMillis();
|
2014-02-27 14:21:09 +00:00
|
|
|
final boolean openedDictIsOutOfDate =
|
|
|
|
mDictNameDictionaryUpdateController.isOutOfDate();
|
|
|
|
if (!dictionaryFileExists()
|
|
|
|
|| (openedDictIsOutOfDate && haveContentsChanged())) {
|
|
|
|
// If the shared dictionary file does not exist or is out of date and
|
|
|
|
// contents have been updated, the first instance that acquires the lock
|
|
|
|
// will generate a new one
|
|
|
|
mDictNameDictionaryUpdateController.mLastUpdateTime = time;
|
|
|
|
createNewDictionaryLocked();
|
|
|
|
} else if (openedDictIsOutOfDate) {
|
|
|
|
// If not, the reload request was unnecessary so revert
|
|
|
|
// LastUpdateRequestTime to LastUpdateTime.
|
|
|
|
mDictNameDictionaryUpdateController.mLastUpdateRequestTime =
|
|
|
|
mDictNameDictionaryUpdateController.mLastUpdateTime;
|
2013-09-27 12:44:26 +00:00
|
|
|
} else if (mBinaryDictionary == null ||
|
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateTime
|
2013-12-11 10:30:18 +00:00
|
|
|
< mDictNameDictionaryUpdateController.mLastUpdateTime) {
|
2013-09-27 12:44:26 +00:00
|
|
|
// Otherwise, if the local dictionary is older than the shared dictionary,
|
|
|
|
// load the shared dictionary.
|
2014-02-27 14:21:09 +00:00
|
|
|
loadBinaryDictionaryLocked();
|
2013-09-27 12:44:26 +00:00
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
if (mBinaryDictionary != null && !(isValidDictionaryLocked()
|
|
|
|
// TODO: remove the check below
|
|
|
|
&& matchesExpectedBinaryDictFormatVersionForThisType(
|
|
|
|
mBinaryDictionary.getFormatVersion()))) {
|
|
|
|
// Binary dictionary or its format version is not valid. Regenerate
|
|
|
|
// the dictionary file. writeBinaryDictionary will remove the
|
|
|
|
// existing files if appropriate.
|
|
|
|
mDictNameDictionaryUpdateController.mLastUpdateTime = time;
|
|
|
|
createNewDictionaryLocked();
|
|
|
|
}
|
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateTime = time;
|
2013-09-27 12:44:26 +00:00
|
|
|
} finally {
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictNameDictionaryUpdateController.mProcessingLargeTask.set(false);
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
2013-07-01 06:25:33 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 06:50:21 +00:00
|
|
|
// TODO: cache the file's existence so that we avoid doing a disk access each time.
|
2012-03-24 06:13:40 +00:00
|
|
|
private boolean dictionaryFileExists() {
|
2014-02-13 07:16:44 +00:00
|
|
|
return mDictFile.exists();
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Flush binary dictionary to dictionary file.
|
2013-08-26 09:50:22 +00:00
|
|
|
*/
|
2013-12-10 09:52:58 +00:00
|
|
|
protected void asyncFlushBinaryDictionary() {
|
2013-09-09 04:04:28 +00:00
|
|
|
final Runnable newTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-27 14:21:09 +00:00
|
|
|
flushDictionaryLocked();
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
};
|
|
|
|
final Runnable oldTask = mUnfinishedFlushingTask.getAndSet(newTask);
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).replaceAndExecute(oldTask, newTask);
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-11-19 09:12:07 +00:00
|
|
|
* For tracking whether the dictionary is out of date and the dictionary is used in a large
|
|
|
|
* task. Can be shared across multiple dictionary instances that access the same filename.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-27 12:44:26 +00:00
|
|
|
private static class DictionaryUpdateController {
|
|
|
|
public volatile long mLastUpdateTime = 0;
|
|
|
|
public volatile long mLastUpdateRequestTime = 0;
|
2013-11-19 09:12:07 +00:00
|
|
|
public volatile AtomicBoolean mProcessingLargeTask = new AtomicBoolean();
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-09-27 12:44:26 +00:00
|
|
|
public boolean isOutOfDate() {
|
2012-03-24 06:13:40 +00:00
|
|
|
return (mLastUpdateRequestTime > mLastUpdateTime);
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 11:16:31 +00:00
|
|
|
|
2013-11-14 05:18:02 +00:00
|
|
|
// TODO: Implement BinaryDictionary.isInDictionary().
|
2013-09-06 13:12:52 +00:00
|
|
|
@UsedForTesting
|
2014-02-05 08:00:51 +00:00
|
|
|
public boolean isInUnderlyingBinaryDictionaryForTests(final String word) {
|
2013-09-09 04:04:28 +00:00
|
|
|
final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mDictType == Dictionary.TYPE_USER_HISTORY) {
|
2013-11-14 05:18:02 +00:00
|
|
|
holder.set(mBinaryDictionary.isValidWord(word));
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2014-01-15 02:18:39 +00:00
|
|
|
return holder.get(false, TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS);
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2013-09-13 08:32:51 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
2013-12-12 08:08:51 +00:00
|
|
|
public void waitAllTasksForTests() {
|
|
|
|
final CountDownLatch countDownLatch = new CountDownLatch(1);
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-12-12 08:08:51 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
countDownLatch.countDown();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
countDownLatch.await();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted while waiting for finishing dictionary operations.", e);
|
|
|
|
}
|
2013-09-13 08:32:51 +00:00
|
|
|
}
|
2013-10-26 00:12:15 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
2014-02-04 04:51:49 +00:00
|
|
|
public void dumpAllWordsForDebug() {
|
|
|
|
reloadDictionaryIfRequired();
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-10-26 00:12:15 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-06 08:55:45 +00:00
|
|
|
Log.d(TAG, "Dump dictionary: " + mDictName);
|
|
|
|
try {
|
|
|
|
final DictionaryHeader header = mBinaryDictionary.getHeader();
|
|
|
|
Log.d(TAG, CombinedFormatUtils.formatAttributeMap(
|
|
|
|
header.mDictionaryOptions.mAttributes));
|
|
|
|
} catch (final UnsupportedFormatException e) {
|
|
|
|
Log.d(TAG, "Cannot fetch header information.", e);
|
|
|
|
}
|
2014-02-04 04:51:49 +00:00
|
|
|
int token = 0;
|
|
|
|
do {
|
|
|
|
final BinaryDictionary.GetNextWordPropertyResult result =
|
|
|
|
mBinaryDictionary.getNextWordProperty(token);
|
|
|
|
final WordProperty wordProperty = result.mWordProperty;
|
|
|
|
if (wordProperty == null) {
|
|
|
|
Log.d(TAG, " dictionary is empty.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Log.d(TAG, wordProperty.toString());
|
|
|
|
token = result.mNextToken;
|
|
|
|
} while (token != 0);
|
2013-10-26 00:12:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|