Merge "Have DictionaryFacilitator in LatinIME."

This commit is contained in:
Keisuke Kuroyanagi 2014-05-23 02:06:33 +00:00 committed by Android (Google) Code Review
commit c1442aa282
9 changed files with 62 additions and 62 deletions

View file

@ -45,8 +45,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
// TODO: Consolidate dictionaries in native code.
public class DictionaryFacilitatorForSuggest {
public static final String TAG = DictionaryFacilitatorForSuggest.class.getSimpleName();
public class DictionaryFacilitator {
public static final String TAG = DictionaryFacilitator.class.getSimpleName();
// HACK: This threshold is being used when adding a capitalized entry in the User History
// dictionary.
@ -162,7 +162,7 @@ public class DictionaryFacilitatorForSuggest {
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
}
public DictionaryFacilitatorForSuggest() {}
public DictionaryFacilitator() {}
public Locale getLocale() {
return mDictionaries.mLocale;

View file

@ -103,7 +103,7 @@ import java.util.concurrent.TimeUnit;
*/
public class LatinIME extends InputMethodService implements KeyboardActionListener,
SuggestionStripView.Listener, SuggestionStripViewAccessor,
DictionaryFacilitatorForSuggest.DictionaryInitializationListener,
DictionaryFacilitator.DictionaryInitializationListener,
ImportantNoticeDialog.ImportantNoticeDialogListener {
private static final String TAG = LatinIME.class.getSimpleName();
private static final boolean TRACE = false;
@ -122,8 +122,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private static final String SCHEME_PACKAGE = "package";
private final Settings mSettings;
private final DictionaryFacilitator mDictionaryFacilitator = new DictionaryFacilitator();
private final InputLogic mInputLogic = new InputLogic(this /* LatinIME */,
this /* SuggestionStripViewAccessor */);
this /* SuggestionStripViewAccessor */, mDictionaryFacilitator);
// We expect to have only one decoder in almost all cases, hence the default capacity of 1.
// If it turns out we need several, it will get grown seamlessly.
final SparseArray<HardwareEventDecoder> mHardwareEventDecoders
@ -493,8 +494,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.getInstance().init(this, mKeyboardSwitcher);
ResearchLogger.getInstance().initDictionary(
mInputLogic.mSuggest.mDictionaryFacilitator);
ResearchLogger.getInstance().initDictionary(mDictionaryFacilitator);
}
// Register to receive ringer mode change and network state change.
@ -543,8 +543,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
private void refreshPersonalizationDictionarySession() {
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
mInputLogic.mSuggest.mDictionaryFacilitator;
final boolean shouldKeepUserHistoryDictionaries;
final boolean shouldKeepPersonalizationDictionaries;
if (mSettings.getCurrent().mUsePersonalizedDicts) {
@ -559,7 +557,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (!shouldKeepUserHistoryDictionaries) {
// Remove user history dictionaries.
PersonalizationHelper.removeAllUserHistoryDictionaries(this);
dictionaryFacilitator.clearUserHistoryDictionary();
mDictionaryFacilitator.clearUserHistoryDictionary();
}
if (!shouldKeepPersonalizationDictionaries) {
// Remove personalization dictionaries.
@ -568,7 +566,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else {
final DistracterFilter distracterFilter = createDistracterFilter();
PersonalizationDictionarySessionRegistrar.init(
this, dictionaryFacilitator, distracterFilter);
this, mDictionaryFacilitator, distracterFilter);
}
}
@ -606,10 +604,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
* @param locale the locale
*/
private void resetSuggestForLocale(final Locale locale) {
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
mInputLogic.mSuggest.mDictionaryFacilitator;
final SettingsValues settingsValues = mSettings.getCurrent();
dictionaryFacilitator.resetDictionaries(this /* context */, locale,
mDictionaryFacilitator.resetDictionaries(this /* context */, locale,
settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts,
false /* forceReloadMainDictionary */, this);
if (settingsValues.mCorrectionEnabled) {
@ -622,17 +618,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
* Reset suggest by loading the main dictionary of the current locale.
*/
/* package private */ void resetSuggestMainDict() {
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
mInputLogic.mSuggest.mDictionaryFacilitator;
final SettingsValues settingsValues = mSettings.getCurrent();
dictionaryFacilitator.resetDictionaries(this /* context */,
dictionaryFacilitator.getLocale(), settingsValues.mUseContactsDict,
mDictionaryFacilitator.resetDictionaries(this /* context */,
mDictionaryFacilitator.getLocale(), settingsValues.mUseContactsDict,
settingsValues.mUsePersonalizedDicts, true /* forceReloadMainDictionary */, this);
}
@Override
public void onDestroy() {
mInputLogic.mSuggest.mDictionaryFacilitator.closeDictionaries();
mDictionaryFacilitator.closeDictionaries();
mSettings.onDestroy();
unregisterReceiver(mConnectivityAndRingerModeChangeReceiver);
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
@ -668,7 +662,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
final DistracterFilter distracterFilter = createDistracterFilter();
PersonalizationDictionarySessionRegistrar.onConfigurationChanged(this, conf,
mInputLogic.mSuggest.mDictionaryFacilitator, distracterFilter);
mDictionaryFacilitator, distracterFilter);
super.onConfigurationChanged(conf);
}
@ -841,7 +835,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
currentSettingsValues = mSettings.getCurrent();
if (currentSettingsValues.mCorrectionEnabled) {
suggest.setAutoCorrectionThreshold(currentSettingsValues.mAutoCorrectionThreshold);
suggest.setAutoCorrectionThreshold(
currentSettingsValues.mAutoCorrectionThreshold);
}
switcher.loadKeyboard(editorInfo, currentSettingsValues, getCurrentAutoCapsState(),
@ -870,7 +865,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mHandler.cancelUpdateSuggestionStrip();
mainKeyboardView.setMainDictionaryAvailability(
suggest.mDictionaryFacilitator.hasInitializedMainDictionary());
mDictionaryFacilitator.hasInitializedMainDictionary());
mainKeyboardView.setKeyPreviewPopupEnabled(currentSettingsValues.mKeyPreviewPopupOn,
currentSettingsValues.mKeyPreviewPopupDismissDelay);
mainKeyboardView.setSlidingKeyInputPreviewEnabled(
@ -1167,8 +1162,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else {
wordToEdit = word;
}
mInputLogic.mSuggest.mDictionaryFacilitator.addWordToUserDictionary(
this /* context */, wordToEdit);
mDictionaryFacilitator.addWordToUserDictionary(this /* context */, wordToEdit);
}
// Callback for the {@link SuggestionStripView}, to call when the important notice strip is
@ -1725,15 +1719,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
@UsedForTesting
/* package for test */ void waitForLoadingDictionaries(final long timeout, final TimeUnit unit)
throws InterruptedException {
mInputLogic.mSuggest.mDictionaryFacilitator.waitForLoadingDictionariesForTesting(
timeout, unit);
mDictionaryFacilitator.waitForLoadingDictionariesForTesting(timeout, unit);
}
// DO NOT USE THIS for any other purpose than testing. This can break the keyboard badly.
@UsedForTesting
/* package for test */ void replaceDictionariesForTest(final Locale locale) {
final SettingsValues settingsValues = mSettings.getCurrent();
mInputLogic.mSuggest.mDictionaryFacilitator.resetDictionaries(this, locale,
mDictionaryFacilitator.resetDictionaries(this, locale,
settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts,
false /* forceReloadMainDictionary */, this /* listener */);
}
@ -1741,8 +1734,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// DO NOT USE THIS for any other purpose than testing.
@UsedForTesting
/* package for test */ void clearPersonalizedDictionariesForTest() {
mInputLogic.mSuggest.mDictionaryFacilitator.clearUserHistoryDictionary();
mInputLogic.mSuggest.mDictionaryFacilitator.clearPersonalizationDictionary();
mDictionaryFacilitator.clearUserHistoryDictionary();
mDictionaryFacilitator.clearPersonalizationDictionary();
}
@UsedForTesting
@ -1754,12 +1747,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
public void dumpDictionaryForDebug(final String dictName) {
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
mInputLogic.mSuggest.mDictionaryFacilitator;
if (dictionaryFacilitator.getLocale() == null) {
if (mDictionaryFacilitator.getLocale() == null) {
resetSuggest();
}
mInputLogic.mSuggest.mDictionaryFacilitator.dumpDictionaryForDebug(dictName);
mDictionaryFacilitator.dumpDictionaryForDebug(dictName);
}
public void debugDumpStateAndCrashWithException(final String context) {

View file

@ -52,11 +52,14 @@ public final class Suggest {
private static final int SUPPRESS_SUGGEST_THRESHOLD = -2000000000;
private static final boolean DBG = LatinImeLogger.sDBG;
public final DictionaryFacilitatorForSuggest mDictionaryFacilitator =
new DictionaryFacilitatorForSuggest();
private final DictionaryFacilitator mDictionaryFacilitator;
private float mAutoCorrectionThreshold;
public Suggest(final DictionaryFacilitator dictionaryFacilitator) {
mDictionaryFacilitator = dictionaryFacilitator;
}
public Locale getLocale() {
return mDictionaryFacilitator.getLocale();
}

View file

@ -32,7 +32,7 @@ import com.android.inputmethod.event.InputTransaction;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.DictionaryFacilitatorForSuggest;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.InputPointers;
import com.android.inputmethod.latin.LastComposedWord;
import com.android.inputmethod.latin.LatinIME;
@ -79,7 +79,8 @@ public final class InputLogic {
private int mSpaceState;
// Never null
public SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
public final Suggest mSuggest = new Suggest();
public final Suggest mSuggest;
private final DictionaryFacilitator mDictionaryFacilitator;
public LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
public final WordComposer mWordComposer;
@ -102,14 +103,19 @@ public final class InputLogic {
* Create a new instance of the input logic.
* @param latinIME the instance of the parent LatinIME. We should remove this when we can.
* @param suggestionStripViewAccessor an object to access the suggestion strip view.
* @param dictionaryFacilitator facilitator for getting suggestions and updating user history
* dictionary.
*/
public InputLogic(final LatinIME latinIME,
final SuggestionStripViewAccessor suggestionStripViewAccessor) {
final SuggestionStripViewAccessor suggestionStripViewAccessor,
final DictionaryFacilitator dictionaryFacilitator) {
mLatinIME = latinIME;
mSuggestionStripViewAccessor = suggestionStripViewAccessor;
mWordComposer = new WordComposer();
mConnection = new RichInputConnection(latinIME);
mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
mSuggest = new Suggest(dictionaryFacilitator);
mDictionaryFacilitator = dictionaryFacilitator;
}
/**
@ -173,7 +179,7 @@ public final class InputLogic {
final InputLogicHandler inputLogicHandler = mInputLogicHandler;
mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
inputLogicHandler.destroy();
mSuggest.mDictionaryFacilitator.closeDictionaries();
mDictionaryFacilitator.closeDictionaries();
}
/**
@ -295,18 +301,16 @@ public final class InputLogic {
// We should show the "Touch again to save" hint if the user pressed the first entry
// AND it's in none of our current dictionaries (main, user or otherwise).
final DictionaryFacilitatorForSuggest dictionaryFacilitator =
mSuggest.mDictionaryFacilitator;
final boolean showingAddToDictionaryHint =
(SuggestedWordInfo.KIND_TYPED == suggestionInfo.mKind
|| SuggestedWordInfo.KIND_OOV_CORRECTION == suggestionInfo.mKind)
&& !dictionaryFacilitator.isValidWord(suggestion, true /* ignoreCase */);
&& !mDictionaryFacilitator.isValidWord(suggestion, true /* ignoreCase */);
if (settingsValues.mIsInternal) {
LatinImeLoggerUtils.onSeparator((char)Constants.CODE_SPACE,
Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
}
if (showingAddToDictionaryHint && dictionaryFacilitator.isUserDictionaryEnabled()) {
if (showingAddToDictionaryHint && mDictionaryFacilitator.isUserDictionaryEnabled()) {
mSuggestionStripViewAccessor.showAddToDictionaryHint(suggestion);
} else {
// If we're not showing the "Touch again to save", then update the suggestion strip.
@ -1250,7 +1254,7 @@ public final class InputLogic {
mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps();
final int timeStampInSeconds = (int)TimeUnit.MILLISECONDS.toSeconds(
System.currentTimeMillis());
mSuggest.mDictionaryFacilitator.addToUserHistory(suggestion, wasAutoCapitalized,
mDictionaryFacilitator.addToUserHistory(suggestion, wasAutoCapitalized,
prevWordsInfo, timeStampInSeconds, settingsValues.mBlockPotentiallyOffensive);
}
@ -1462,8 +1466,7 @@ public final class InputLogic {
}
mConnection.deleteSurroundingText(deleteLength, 0);
if (!TextUtils.isEmpty(prevWordsInfo.mPrevWord) && !TextUtils.isEmpty(committedWord)) {
mSuggest.mDictionaryFacilitator.cancelAddingUserHistory(
prevWordsInfo, committedWordString);
mDictionaryFacilitator.cancelAddingUserHistory(prevWordsInfo, committedWordString);
}
final String stringToCommit = originallyTypedWord + mLastComposedWord.mSeparatorString;
final SpannableString textToCommit = new SpannableString(stringToCommit);

View file

@ -19,17 +19,17 @@ package com.android.inputmethod.latin.personalization;
import android.content.Context;
import android.content.res.Configuration;
import com.android.inputmethod.latin.DictionaryFacilitatorForSuggest;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.utils.DistracterFilter;
public class PersonalizationDictionarySessionRegistrar {
public static void init(final Context context,
final DictionaryFacilitatorForSuggest dictionaryFacilitator,
final DictionaryFacilitator dictionaryFacilitator,
final DistracterFilter distracterFilter) {
}
public static void onConfigurationChanged(final Context context, final Configuration conf,
final DictionaryFacilitatorForSuggest dictionaryFacilitator,
final DictionaryFacilitator dictionaryFacilitator,
final DistracterFilter distracterFilter) {
}

View file

@ -34,6 +34,7 @@ import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.PrevWordsInfo;
import com.android.inputmethod.latin.Suggest;
import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
@ -53,6 +54,7 @@ public class DistracterFilter {
private final Context mContext;
private final Map<Locale, InputMethodSubtype> mLocaleToSubtypeMap;
private final Map<Locale, Keyboard> mLocaleToKeyboardMap;
private final DictionaryFacilitator mDictionaryFacilitator;
private final Suggest mSuggest;
private Keyboard mKeyboard;
@ -89,7 +91,8 @@ public class DistracterFilter {
}
}
mLocaleToKeyboardMap = new HashMap<>();
mSuggest = new Suggest();
mDictionaryFacilitator = new DictionaryFacilitator();
mSuggest = new Suggest(mDictionaryFacilitator);
mKeyboard = null;
}
@ -132,10 +135,10 @@ public class DistracterFilter {
}
private void loadDictionariesForLocale(final Locale newlocale) throws InterruptedException {
mSuggest.mDictionaryFacilitator.resetDictionaries(mContext, newlocale,
mDictionaryFacilitator.resetDictionaries(mContext, newlocale,
false /* useContactsDict */, false /* usePersonalizedDicts */,
false /* forceReloadMainDictionary */, null /* listener */);
mSuggest.mDictionaryFacilitator.waitForLoadingMainDictionary(
mDictionaryFacilitator.waitForLoadingMainDictionary(
TIMEOUT_TO_WAIT_LOADING_DICTIONARIES_IN_SECONDS, TimeUnit.SECONDS);
}
@ -153,7 +156,7 @@ public class DistracterFilter {
if (locale == null) {
return false;
}
if (!locale.equals(mSuggest.mDictionaryFacilitator.getLocale())) {
if (!locale.equals(mDictionaryFacilitator.getLocale())) {
if (!mLocaleToSubtypeMap.containsKey(locale)) {
Log.e(TAG, "Locale " + locale + " is not enabled.");
// TODO: Investigate what we should do for disabled locales.

View file

@ -19,7 +19,7 @@ package com.android.inputmethod.latin.utils;
import android.util.Log;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.DictionaryFacilitatorForSuggest;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.PrevWordsInfo;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
@ -80,7 +80,7 @@ public final class LanguageModelParam {
// Process a list of words and return a list of {@link LanguageModelParam} objects.
public static ArrayList<LanguageModelParam> createLanguageModelParamsFrom(
final ArrayList<String> tokens, final int timestamp,
final DictionaryFacilitatorForSuggest dictionaryFacilitator,
final DictionaryFacilitator dictionaryFacilitator,
final SpacingAndPunctuations spacingAndPunctuations,
final DistracterFilter distracterFilter) {
final ArrayList<LanguageModelParam> languageModelParams =
@ -124,7 +124,7 @@ public final class LanguageModelParam {
private static LanguageModelParam detectWhetherVaildWordOrNotAndGetLanguageModelParam(
final PrevWordsInfo prevWordsInfo, final String targetWord, final int timestamp,
final DictionaryFacilitatorForSuggest dictionaryFacilitator,
final DictionaryFacilitator dictionaryFacilitator,
final DistracterFilter distracterFilter) {
final Locale locale = dictionaryFacilitator.getLocale();
if (locale == null) {

View file

@ -20,7 +20,7 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.DictionaryFacilitatorForSuggest;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.IOException;
@ -75,7 +75,7 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
// The size of the n-grams logged. E.g. N_GRAM_SIZE = 2 means to sample bigrams.
public static final int N_GRAM_SIZE = 2;
private final DictionaryFacilitatorForSuggest mDictionaryFacilitator;
private final DictionaryFacilitator mDictionaryFacilitator;
@UsedForTesting
private Dictionary mDictionaryForTesting;
private boolean mIsStopping = false;
@ -87,7 +87,7 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
/* package for test */ int mNumWordsUntilSafeToSample;
public MainLogBuffer(final int wordsBetweenSamples, final int numInitialWordsToIgnore,
final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
final DictionaryFacilitator dictionaryFacilitator) {
super(N_GRAM_SIZE + wordsBetweenSamples);
mNumWordsBetweenNGrams = wordsBetweenSamples;
mNumWordsUntilSafeToSample = DEBUG ? 0 : numInitialWordsToIgnore;

View file

@ -52,7 +52,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.DictionaryFacilitatorForSuggest;
import com.android.inputmethod.latin.DictionaryFacilitator;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputConnection;
@ -167,7 +167,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
protected static final int SUSPEND_DURATION_IN_MINUTES = 1;
// used to check whether words are not unique
private DictionaryFacilitatorForSuggest mDictionaryFacilitator;
private DictionaryFacilitator mDictionaryFacilitator;
private MainKeyboardView mMainKeyboardView;
// TODO: Check whether a superclass can be used instead of LatinIME.
/* package for test */ LatinIME mLatinIME;
@ -656,7 +656,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
mInFeedbackDialog = false;
}
public void initDictionary(final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
public void initDictionary(final DictionaryFacilitator dictionaryFacilitator) {
mDictionaryFacilitator = dictionaryFacilitator;
// MainLogBuffer now has an out-of-date Suggest object. Close down MainLogBuffer and create
// a new one.