am ba18f6e6: am e44c4d25: am b6665d95: Merge "Use DistracterFilterCheckingIsInDictionary for User History" into lmp-dev
* commit 'ba18f6e6cb1fc1d8f4d30fc38b99023cd0ea0912': Use DistracterFilterCheckingIsInDictionary for User Historymain
commit
13e16c88e7
|
@ -468,7 +468,9 @@ public class DictionaryFacilitator {
|
||||||
// We don't add words with 0-frequency (assuming they would be profanity etc.).
|
// We don't add words with 0-frequency (assuming they would be profanity etc.).
|
||||||
final boolean isValid = maxFreq > 0;
|
final boolean isValid = maxFreq > 0;
|
||||||
UserHistoryDictionary.addToDictionary(userHistoryDictionary, prevWordsInfo, secondWord,
|
UserHistoryDictionary.addToDictionary(userHistoryDictionary, prevWordsInfo, secondWord,
|
||||||
isValid, timeStampInSeconds, mDistracterFilter);
|
isValid, timeStampInSeconds,
|
||||||
|
new DistracterFilterCheckingIsInDictionary(
|
||||||
|
mDistracterFilter, userHistoryDictionary));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeWord(final String dictName, final String word) {
|
private void removeWord(final String dictName, final String word) {
|
||||||
|
|
|
@ -38,6 +38,7 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
@ -163,9 +164,31 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void asyncExecuteTaskWithLock(final Lock lock, final Runnable task) {
|
private void asyncExecuteTaskWithLock(final Lock lock, final Runnable task) {
|
||||||
|
asyncPreCheckAndExecuteTaskWithLock(lock, null /* preCheckTask */, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void asyncPreCheckAndExecuteTaskWithWriteLock(
|
||||||
|
final Callable<Boolean> preCheckTask, final Runnable task) {
|
||||||
|
asyncPreCheckAndExecuteTaskWithLock(mLock.writeLock(), preCheckTask, task);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute task with lock when the result of preCheckTask is true or preCheckTask is null.
|
||||||
|
private void asyncPreCheckAndExecuteTaskWithLock(final Lock lock,
|
||||||
|
final Callable<Boolean> preCheckTask, final Runnable task) {
|
||||||
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (preCheckTask != null) {
|
||||||
|
try {
|
||||||
|
if (!preCheckTask.call().booleanValue()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (final Exception e) {
|
||||||
|
Log.e(TAG, "The pre check task throws an exception.", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
task.run();
|
task.run();
|
||||||
|
@ -278,17 +301,20 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
||||||
final boolean isBlacklisted, final int timestamp,
|
final boolean isBlacklisted, final int timestamp,
|
||||||
final DistracterFilter distracterFilter) {
|
final DistracterFilter distracterFilter) {
|
||||||
reloadDictionaryIfRequired();
|
reloadDictionaryIfRequired();
|
||||||
asyncExecuteTaskWithWriteLock(new Runnable() {
|
asyncPreCheckAndExecuteTaskWithWriteLock(
|
||||||
|
new Callable<Boolean>() {
|
||||||
|
@Override
|
||||||
|
public Boolean call() throws Exception {
|
||||||
|
return !distracterFilter.isDistracterToWordsInDictionaries(
|
||||||
|
PrevWordsInfo.EMPTY_PREV_WORDS_INFO, word, mLocale);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (mBinaryDictionary == null) {
|
if (mBinaryDictionary == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (distracterFilter.isDistracterToWordsInDictionaries(
|
|
||||||
PrevWordsInfo.EMPTY_PREV_WORDS_INFO, word, mLocale)) {
|
|
||||||
// The word is a distracter.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
||||||
addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq,
|
addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq,
|
||||||
isNotAWord, isBlacklisted, timestamp);
|
isNotAWord, isBlacklisted, timestamp);
|
||||||
|
|
Loading…
Reference in New Issue