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.os.SystemClock;
|
|
|
|
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;
|
2013-09-24 13:57:15 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2013-08-23 13:04:27 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.DynamicPersonalizationDictionaryWriter;
|
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;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2013-09-09 04:04:28 +00:00
|
|
|
import com.android.inputmethod.latin.utils.PrioritizedSerialExecutor;
|
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-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-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-09-24 13:57:15 +00:00
|
|
|
// TODO: Remove.
|
2013-09-10 09:45:39 +00:00
|
|
|
/** Whether to call binary dictionary dynamically updating methods. */
|
2013-10-26 00:12:15 +00:00
|
|
|
public static final boolean ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE = true;
|
2013-09-10 09:45:39 +00:00
|
|
|
|
2013-09-09 04:04:28 +00:00
|
|
|
private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100;
|
|
|
|
|
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-09-26 03:59:02 +00:00
|
|
|
private static final int DICTIONARY_FORMAT_VERSION = 3;
|
|
|
|
|
|
|
|
private static final String SUPPORTS_DYNAMIC_UPDATE =
|
|
|
|
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE;
|
2013-09-24 13:57:15 +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
|
|
|
|
* the filename and the value is the shared dictionary time recorder associated with that
|
|
|
|
* filename.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-27 12:44:26 +00:00
|
|
|
private static final ConcurrentHashMap<String, DictionaryUpdateController>
|
|
|
|
sFilenameDictionaryUpdateControllerMap = CollectionUtils.newConcurrentHashMap();
|
2013-09-09 04:04:28 +00:00
|
|
|
|
2013-09-27 12:44:26 +00:00
|
|
|
private static final ConcurrentHashMap<String, PrioritizedSerialExecutor>
|
2013-09-09 04:04:28 +00:00
|
|
|
sFilenameExecutorMap = CollectionUtils.newConcurrentHashMap();
|
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-09-10 09:45:39 +00:00
|
|
|
// TODO: Remove and handle dictionaries in native code.
|
2013-07-04 12:06:04 +00:00
|
|
|
/** The in-memory dictionary used to generate the binary dictionary. */
|
2013-09-06 13:12:52 +00:00
|
|
|
protected AbstractDictionaryWriter mDictionaryWriter;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of this dictionary, used as the filename for storing the binary dictionary. Multiple
|
|
|
|
* dictionary instances with the same filename is supported, with access controlled by
|
2013-09-09 04:04:28 +00:00
|
|
|
* DictionaryTimeRecorder.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
|
|
|
private final String mFilename;
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
/** Whether to support dynamically updating the dictionary */
|
|
|
|
private final boolean mIsUpdatable;
|
|
|
|
|
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. */
|
|
|
|
private final DictionaryUpdateController mFilenameDictionaryUpdateController;
|
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. */
|
|
|
|
public static final String DICT_FILE_EXTENSION = ".dict";
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Abstract method for loading the unigrams and bigrams of a given dictionary in a background
|
|
|
|
* thread.
|
|
|
|
*/
|
|
|
|
protected abstract void loadDictionaryAsync();
|
|
|
|
|
2012-04-27 06:50:21 +00:00
|
|
|
/**
|
|
|
|
* Indicates that the source dictionary content has 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.
|
|
|
|
*/
|
|
|
|
protected abstract boolean hasContentChanged();
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-09-27 12:44:26 +00:00
|
|
|
* Gets the dictionary update controller for the given filename.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-27 12:44:26 +00:00
|
|
|
private static DictionaryUpdateController getDictionaryUpdateController(
|
2012-03-24 06:13:40 +00:00
|
|
|
String filename) {
|
2013-09-27 12:44:26 +00:00
|
|
|
DictionaryUpdateController recorder = sFilenameDictionaryUpdateControllerMap.get(filename);
|
2013-09-09 04:04:28 +00:00
|
|
|
if (recorder == null) {
|
2013-09-27 12:44:26 +00:00
|
|
|
synchronized(sFilenameDictionaryUpdateControllerMap) {
|
|
|
|
recorder = new DictionaryUpdateController();
|
|
|
|
sFilenameDictionaryUpdateControllerMap.put(filename, recorder);
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return recorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the executor for the given filename.
|
|
|
|
*/
|
|
|
|
private static PrioritizedSerialExecutor getExecutor(final String filename) {
|
|
|
|
PrioritizedSerialExecutor executor = sFilenameExecutorMap.get(filename);
|
|
|
|
if (executor == null) {
|
|
|
|
synchronized(sFilenameExecutorMap) {
|
|
|
|
executor = new PrioritizedSerialExecutor();
|
|
|
|
sFilenameExecutorMap.put(filename, executor);
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
return executor;
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
private static AbstractDictionaryWriter getDictionaryWriter(final Context context,
|
2013-08-23 13:04:27 +00:00
|
|
|
final String dictType, final boolean isDynamicPersonalizationDictionary) {
|
|
|
|
if (isDynamicPersonalizationDictionary) {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return new DynamicPersonalizationDictionaryWriter(context, dictType);
|
|
|
|
}
|
2013-08-16 11:16:31 +00:00
|
|
|
} else {
|
|
|
|
return new DictionaryWriter(context, dictType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Creates a new expandable binary dictionary.
|
|
|
|
*
|
|
|
|
* @param context The application context of the parent.
|
|
|
|
* @param filename The filename for this binary dictionary. Multiple dictionaries with the same
|
|
|
|
* filename is supported.
|
2012-06-27 08:31:09 +00:00
|
|
|
* @param dictType the dictionary type, as a human-readable string
|
2013-08-16 11:16:31 +00:00
|
|
|
* @param isUpdatable whether to support dynamically updating the dictionary. Please note that
|
|
|
|
* dynamic dictionary has negative effects on memory space and computation time.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-08-16 11:16:31 +00:00
|
|
|
public ExpandableBinaryDictionary(final Context context, final String filename,
|
|
|
|
final String dictType, final boolean isUpdatable) {
|
2012-06-27 08:31:09 +00:00
|
|
|
super(dictType);
|
2012-03-24 06:13:40 +00:00
|
|
|
mFilename = filename;
|
|
|
|
mContext = context;
|
2013-08-16 11:16:31 +00:00
|
|
|
mIsUpdatable = isUpdatable;
|
2012-03-24 06:13:40 +00:00
|
|
|
mBinaryDictionary = null;
|
2013-09-27 12:44:26 +00:00
|
|
|
mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename);
|
2013-08-23 13:04:27 +00:00
|
|
|
// Currently, only dynamic personalization dictionary is updatable.
|
2013-08-16 11:16:31 +00:00
|
|
|
mDictionaryWriter = getDictionaryWriter(context, dictType, isUpdatable);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 17:45:48 +00:00
|
|
|
protected static String getFilenameWithLocale(final String name, final String localeStr) {
|
2013-08-19 01:17:02 +00:00
|
|
|
return name + "." + localeStr + DICT_FILE_EXTENSION;
|
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() {
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mBinaryDictionary!= null) {
|
|
|
|
mBinaryDictionary.close();
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
2013-09-24 13:57:15 +00:00
|
|
|
if (mDictionaryWriter != null) {
|
|
|
|
mDictionaryWriter.close();
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void closeBinaryDictionary() {
|
2012-03-24 06:13:40 +00:00
|
|
|
// Ensure that no other threads are accessing the local binary dictionary.
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mBinaryDictionary != null) {
|
|
|
|
mBinaryDictionary.close();
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
protected Map<String, String> getHeaderAttributeMap() {
|
|
|
|
HashMap<String, String> attributeMap = new HashMap<String, String>();
|
|
|
|
attributeMap.put(FormatSpec.FileHeader.SUPPORTS_DYNAMIC_UPDATE_ATTRIBUTE,
|
|
|
|
SUPPORTS_DYNAMIC_UPDATE);
|
2013-09-28 03:50:09 +00:00
|
|
|
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE, mFilename);
|
2013-09-26 03:59:02 +00:00
|
|
|
return attributeMap;
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
protected void clear() {
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE && mDictionaryWriter == null) {
|
|
|
|
mBinaryDictionary.close();
|
|
|
|
final File file = new File(mContext.getFilesDir(), mFilename);
|
2013-09-26 03:59:02 +00:00
|
|
|
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
|
|
|
|
DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
|
2013-10-03 11:55:34 +00:00
|
|
|
mBinaryDictionary = new BinaryDictionary(
|
|
|
|
file.getAbsolutePath(), 0 /* offset */, file.length(),
|
|
|
|
true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
|
|
|
mDictionaryWriter.clear();
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-07-04 12:06:04 +00:00
|
|
|
* Adds a word unigram to the dictionary. Used for loading a dictionary.
|
2013-10-04 14:26:18 +00:00
|
|
|
* @param word The word to add.
|
|
|
|
* @param shortcutTarget A shortcut target for this word, or null if none.
|
|
|
|
* @param frequency The frequency for this unigram.
|
|
|
|
* @param shortcutFreq The frequency of the shortcut (0~15, with 15 = whitelist). Ignored
|
|
|
|
* if shortcutTarget is null.
|
|
|
|
* @param isNotAWord true if this is not a word, i.e. shortcut only.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-07-04 12:06:04 +00:00
|
|
|
protected void addWord(final String word, final String shortcutTarget,
|
2013-10-04 14:26:18 +00:00
|
|
|
final int frequency, final int shortcutFreq, final boolean isNotAWord) {
|
|
|
|
mDictionaryWriter.addUnigramWord(word, shortcutTarget, frequency, shortcutFreq, isNotAWord);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 11:16:31 +00:00
|
|
|
* Adds a word bigram in the dictionary. Used for loading a dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-08-16 11:16:31 +00:00
|
|
|
protected void addBigram(final String prevWord, final String word, final int frequency,
|
|
|
|
final long lastModifiedTime) {
|
|
|
|
mDictionaryWriter.addBigramWords(prevWord, word, frequency, true /* isValid */,
|
|
|
|
lastModifiedTime);
|
2013-07-04 12:06:04 +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) {
|
2013-10-01 02:41:39 +00:00
|
|
|
if (!ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) return;
|
2013-10-03 11:55:34 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
runGCIfRequiredInternalLocked(mindsBlockByGC);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runGCIfRequiredInternalLocked(final boolean mindsBlockByGC) {
|
|
|
|
if (!ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) return;
|
|
|
|
// Calls to needsToRunGC() need to be serialized.
|
2013-10-02 09:06:08 +00:00
|
|
|
if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
|
2013-10-01 02:41:39 +00:00
|
|
|
if (setIsRegeneratingIfNotRegenerating()) {
|
2013-10-03 11:55:34 +00:00
|
|
|
// Run GC after currently existing time sensitive operations.
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
2013-10-01 02:41:39 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
} finally {
|
|
|
|
mFilenameDictionaryUpdateController.mIsRegenerating.set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
protected void addWordDynamically(final String word, final String shortcutTarget,
|
2013-10-04 14:26:18 +00:00
|
|
|
final int frequency, final int shortcutFreq, final boolean isNotAWord) {
|
2013-08-16 11:16:31 +00:00
|
|
|
if (!mIsUpdatable) {
|
|
|
|
Log.w(TAG, "addWordDynamically is called for non-updatable dictionary: " + mFilename);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-10 09:45:39 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
2013-10-03 11:55:34 +00:00
|
|
|
runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
|
2013-09-10 09:45:39 +00:00
|
|
|
mBinaryDictionary.addUnigramWord(word, frequency);
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Remove.
|
2013-10-04 14:26:18 +00:00
|
|
|
mDictionaryWriter.addUnigramWord(word, shortcutTarget, frequency, shortcutFreq,
|
|
|
|
isNotAWord);
|
2013-09-10 09:45:39 +00:00
|
|
|
}
|
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-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,
|
|
|
|
final int frequency, final boolean isValid) {
|
|
|
|
if (!mIsUpdatable) {
|
|
|
|
Log.w(TAG, "addBigramDynamically is called for non-updatable dictionary: "
|
|
|
|
+ mFilename);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-10 09:45:39 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
2013-10-03 11:55:34 +00:00
|
|
|
runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
|
2013-09-10 09:45:39 +00:00
|
|
|
mBinaryDictionary.addBigramWords(word0, word1, frequency);
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Remove.
|
|
|
|
mDictionaryWriter.addBigramWords(word0, word1, frequency, isValid,
|
|
|
|
0 /* lastTouchedTime */);
|
2013-09-10 09:45:39 +00:00
|
|
|
}
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
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) {
|
|
|
|
if (!mIsUpdatable) {
|
|
|
|
Log.w(TAG, "removeBigramDynamically is called for non-updatable dictionary: "
|
|
|
|
+ mFilename);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-10 09:45:39 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
2013-10-03 11:55:34 +00:00
|
|
|
runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
|
2013-09-10 09:45:39 +00:00
|
|
|
mBinaryDictionary.removeBigramWords(word0, word1);
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Remove.
|
|
|
|
mDictionaryWriter.removeBigramWords(word0, word1);
|
2013-09-10 09:45:39 +00:00
|
|
|
}
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +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,
|
|
|
|
final int sessionId) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2013-09-27 12:44:26 +00:00
|
|
|
if (isRegenerating()) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
|
|
|
|
final AsyncResultHolder<ArrayList<SuggestedWordInfo>> holder =
|
|
|
|
new AsyncResultHolder<ArrayList<SuggestedWordInfo>>();
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
holder.set(null);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-04 12:06:04 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> binarySuggestion =
|
2013-09-05 08:32:30 +00:00
|
|
|
mBinaryDictionary.getSuggestionsWithSessionId(composer, prevWord,
|
|
|
|
proximityInfo, blockOffensiveWords, additionalFeaturesOptions,
|
|
|
|
sessionId);
|
2013-09-24 13:57:15 +00:00
|
|
|
holder.set(binarySuggestion);
|
|
|
|
} else {
|
|
|
|
final ArrayList<SuggestedWordInfo> inMemDictSuggestion =
|
|
|
|
composer.isBatchMode() ? null :
|
|
|
|
mDictionaryWriter.getSuggestionsWithSessionId(composer,
|
|
|
|
prevWord, proximityInfo, blockOffensiveWords,
|
|
|
|
additionalFeaturesOptions, sessionId);
|
|
|
|
// TODO: Remove checking mIsUpdatable and use native suggestion.
|
|
|
|
if (mBinaryDictionary != null && !mIsUpdatable) {
|
|
|
|
final ArrayList<SuggestedWordInfo> binarySuggestion =
|
|
|
|
mBinaryDictionary.getSuggestionsWithSessionId(composer, prevWord,
|
|
|
|
proximityInfo, blockOffensiveWords,
|
|
|
|
additionalFeaturesOptions, sessionId);
|
|
|
|
if (inMemDictSuggestion == null) {
|
|
|
|
holder.set(binarySuggestion);
|
|
|
|
} else if (binarySuggestion == null) {
|
|
|
|
holder.set(inMemDictSuggestion);
|
|
|
|
} else {
|
|
|
|
binarySuggestion.addAll(inMemDictSuggestion);
|
|
|
|
holder.set(binarySuggestion);
|
|
|
|
}
|
2013-07-04 12:06:04 +00:00
|
|
|
} else {
|
2013-09-24 13:57:15 +00:00
|
|
|
holder.set(inMemDictSuggestion);
|
2013-07-04 12:06:04 +00:00
|
|
|
}
|
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,
|
|
|
|
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions) {
|
|
|
|
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
|
|
|
|
additionalFeaturesOptions, 0 /* sessionId */);
|
|
|
|
}
|
|
|
|
|
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();
|
2012-03-24 06:13:40 +00:00
|
|
|
return isValidWordInner(word);
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
protected boolean isValidWordInner(final String word) {
|
2013-09-27 12:44:26 +00:00
|
|
|
if (isRegenerating()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@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-09-27 12:44:26 +00:00
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = SystemClock.uptimeMillis();
|
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.
|
|
|
|
*/
|
2013-07-04 12:06:04 +00:00
|
|
|
private void loadBinaryDictionary() {
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2012-05-25 18:16:30 +00:00
|
|
|
Log.d(TAG, "Loading binary dictionary: " + mFilename + " request="
|
2013-09-27 12:44:26 +00:00
|
|
|
+ mFilenameDictionaryUpdateController.mLastUpdateRequestTime + " update="
|
|
|
|
+ mFilenameDictionaryUpdateController.mLastUpdateTime);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final File file = new File(mContext.getFilesDir(), mFilename);
|
|
|
|
final String filename = file.getAbsolutePath();
|
|
|
|
final long length = file.length();
|
|
|
|
|
|
|
|
// Build the new binary dictionary
|
2013-10-03 11:55:34 +00:00
|
|
|
final BinaryDictionary newBinaryDictionary = new BinaryDictionary(filename, 0 /* offset */,
|
|
|
|
length, true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-09-24 13:57:15 +00:00
|
|
|
// Ensure all threads accessing the current dictionary have finished before
|
|
|
|
// swapping in the new one.
|
|
|
|
// TODO: Ensure multi-thread assignment of mBinaryDictionary.
|
2013-09-09 04:04:28 +00:00
|
|
|
final BinaryDictionary oldBinaryDictionary = mBinaryDictionary;
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-07-03 10:46:08 +00:00
|
|
|
mBinaryDictionary = newBinaryDictionary;
|
2013-09-09 04:04:28 +00:00
|
|
|
if (oldBinaryDictionary != null) {
|
|
|
|
oldBinaryDictionary.close();
|
|
|
|
}
|
2013-07-03 10:46:08 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 12:06:04 +00:00
|
|
|
/**
|
|
|
|
* Abstract method for checking if it is required to reload the dictionary before writing
|
|
|
|
* a binary dictionary.
|
|
|
|
*/
|
|
|
|
abstract protected boolean needsToReloadBeforeWriting();
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-08-16 11:16:31 +00:00
|
|
|
* Writes a new binary dictionary based on the contents of the fusion dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-08-16 11:16:31 +00:00
|
|
|
private void writeBinaryDictionary() {
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2012-05-25 18:16:30 +00:00
|
|
|
Log.d(TAG, "Generating binary dictionary: " + mFilename + " request="
|
2013-09-27 12:44:26 +00:00
|
|
|
+ mFilenameDictionaryUpdateController.mLastUpdateRequestTime + " update="
|
|
|
|
+ mFilenameDictionaryUpdateController.mLastUpdateTime);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2013-07-04 12:06:04 +00:00
|
|
|
if (needsToReloadBeforeWriting()) {
|
|
|
|
mDictionaryWriter.clear();
|
|
|
|
loadDictionaryAsync();
|
2013-09-28 03:50:09 +00:00
|
|
|
mDictionaryWriter.write(mFilename, getHeaderAttributeMap());
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
if (mBinaryDictionary == null || !mBinaryDictionary.isValidDictionary()) {
|
|
|
|
final File file = new File(mContext.getFilesDir(), mFilename);
|
2013-09-26 03:59:02 +00:00
|
|
|
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
|
|
|
|
DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
|
2013-09-24 13:57:15 +00:00
|
|
|
} else {
|
2013-09-30 04:57:54 +00:00
|
|
|
if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
2013-09-24 13:57:15 +00:00
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
} else {
|
|
|
|
mBinaryDictionary.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-28 03:50:09 +00:00
|
|
|
mDictionaryWriter.write(mFilename, getHeaderAttributeMap());
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
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) {
|
|
|
|
final long time = SystemClock.uptimeMillis();
|
2013-09-27 12:44:26 +00:00
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = time;
|
|
|
|
mFilenameDictionaryUpdateController.mLastUpdateRequestTime = time;
|
2012-03-24 06:13:40 +00:00
|
|
|
if (DEBUG) {
|
2012-05-25 18:16:30 +00:00
|
|
|
Log.d(TAG, "Reload request: " + mFilename + ": request=" + time + " update="
|
2013-09-27 12:44:26 +00:00
|
|
|
+ mFilenameDictionaryUpdateController.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-09-27 12:44:26 +00:00
|
|
|
if (setIsRegeneratingIfNotRegenerating()) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isRegenerating() {
|
|
|
|
return mFilenameDictionaryUpdateController.mIsRegenerating.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the dictionary can be regenerated.
|
|
|
|
private boolean setIsRegeneratingIfNotRegenerating() {
|
|
|
|
return mFilenameDictionaryUpdateController.mIsRegenerating.compareAndSet(
|
|
|
|
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.
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-27 12:44:26 +00:00
|
|
|
try {
|
|
|
|
final long time = SystemClock.uptimeMillis();
|
|
|
|
final boolean dictionaryFileExists = dictionaryFileExists();
|
|
|
|
if (mFilenameDictionaryUpdateController.isOutOfDate()
|
|
|
|
|| !dictionaryFileExists) {
|
|
|
|
// If the shared dictionary file does not exist or is out of date, the
|
|
|
|
// first instance that acquires the lock will generate a new one.
|
|
|
|
if (hasContentChanged() || !dictionaryFileExists) {
|
|
|
|
// If the source content has changed or the dictionary does not exist,
|
|
|
|
// rebuild the binary dictionary. Empty dictionaries are supported (in
|
|
|
|
// the case where loadDictionaryAsync() adds nothing) in order to
|
|
|
|
// provide a uniform framework.
|
|
|
|
mFilenameDictionaryUpdateController.mLastUpdateTime = time;
|
|
|
|
writeBinaryDictionary();
|
|
|
|
loadBinaryDictionary();
|
|
|
|
} else {
|
|
|
|
// If not, the reload request was unnecessary so revert
|
|
|
|
// LastUpdateRequestTime to LastUpdateTime.
|
|
|
|
mFilenameDictionaryUpdateController.mLastUpdateRequestTime =
|
|
|
|
mFilenameDictionaryUpdateController.mLastUpdateTime;
|
|
|
|
}
|
|
|
|
} else if (mBinaryDictionary == null ||
|
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateTime
|
|
|
|
< mFilenameDictionaryUpdateController.mLastUpdateTime) {
|
|
|
|
// Otherwise, if the local dictionary is older than the shared dictionary,
|
|
|
|
// load the shared dictionary.
|
|
|
|
loadBinaryDictionary();
|
|
|
|
}
|
|
|
|
if (mBinaryDictionary != null && !mBinaryDictionary.isValidDictionary()) {
|
|
|
|
// Binary dictionary is not valid. Regenerate the dictionary file.
|
|
|
|
mFilenameDictionaryUpdateController.mLastUpdateTime = time;
|
2013-08-16 11:16:31 +00:00
|
|
|
writeBinaryDictionary();
|
|
|
|
loadBinaryDictionary();
|
|
|
|
}
|
2013-09-27 12:44:26 +00:00
|
|
|
mPerInstanceDictionaryUpdateController.mLastUpdateTime = time;
|
|
|
|
} finally {
|
|
|
|
mFilenameDictionaryUpdateController.mIsRegenerating.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() {
|
|
|
|
final File file = new File(mContext.getFilesDir(), mFilename);
|
|
|
|
return file.exists();
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
/**
|
|
|
|
* Load the dictionary to memory.
|
|
|
|
*/
|
|
|
|
protected void asyncLoadDictionaryToMemory() {
|
2013-09-09 04:04:28 +00:00
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (!ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
loadDictionaryAsync();
|
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate binary dictionary using DictionaryWriter.
|
|
|
|
*/
|
2013-09-24 13:57:15 +00:00
|
|
|
protected void asyncFlashAllBinaryDictionary() {
|
2013-09-09 04:04:28 +00:00
|
|
|
final Runnable newTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
writeBinaryDictionary();
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
};
|
|
|
|
final Runnable oldTask = mUnfinishedFlushingTask.getAndSet(newTask);
|
|
|
|
getExecutor(mFilename).replaceAndExecute(oldTask, newTask);
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2013-09-27 12:44:26 +00:00
|
|
|
* For tracking whether the dictionary is out of date and the dictionary is regenerating.
|
2013-09-09 04:04:28 +00:00
|
|
|
* 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;
|
|
|
|
public volatile AtomicBoolean mIsRegenerating = 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-09-06 13:12:52 +00:00
|
|
|
// TODO: Implement native binary methods once the dynamic dictionary implementation is done.
|
|
|
|
@UsedForTesting
|
|
|
|
public boolean isInDictionaryForTests(final String word) {
|
2013-09-09 04:04:28 +00:00
|
|
|
final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (mDictType == Dictionary.TYPE_USER_HISTORY) {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
holder.set(mBinaryDictionary.isValidWord(word));
|
|
|
|
} else {
|
|
|
|
holder.set(((DynamicPersonalizationDictionaryWriter) mDictionaryWriter)
|
2013-09-27 02:13:22 +00:00
|
|
|
.isInBigramListForTests(word));
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
|
|
|
return holder.get(false, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2013-09-13 08:32:51 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
public void shutdownExecutorForTests() {
|
|
|
|
getExecutor(mFilename).shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
public boolean isTerminatedForTests() {
|
|
|
|
return getExecutor(mFilename).isTerminated();
|
|
|
|
}
|
2013-10-26 00:12:15 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
protected void runAfterGcForDebug(final Runnable r) {
|
|
|
|
getExecutor(mFilename).executePrioritized(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
r.run();
|
|
|
|
} finally {
|
|
|
|
mFilenameDictionaryUpdateController.mIsRegenerating.set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|