HeliBoard/app/src/main/java/helium314/keyboard/latin/DictionaryFacilitator.java

153 lines
5.8 KiB
Java
Raw Normal View History

2019-12-31 18:19:35 +01:00
/*
* Copyright (C) 2013 The Android Open Source Project
* modified
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
2019-12-31 18:19:35 +01:00
*/
package helium314.keyboard.latin;
2019-12-31 18:19:35 +01:00
import android.content.Context;
import android.util.LruCache;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import helium314.keyboard.keyboard.Keyboard;
import helium314.keyboard.latin.common.ComposedData;
import helium314.keyboard.latin.settings.SettingsValuesForSuggestion;
import helium314.keyboard.latin.utils.SuggestionResults;
2019-12-31 18:19:35 +01:00
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
/**
* Interface that facilitates interaction with different kinds of dictionaries. Provides APIs to
2025-05-20 21:32:23 +02:00
* instantiate and select the correct dictionaries (based on language and settings), update entries
2019-12-31 18:19:35 +01:00
* and fetch suggestions. Currently AndroidSpellCheckerService and LatinIME both use
* DictionaryFacilitator as a client for interacting with dictionaries.
*/
public interface DictionaryFacilitator {
2020-01-26 23:19:47 +01:00
String[] ALL_DICTIONARY_TYPES = new String[] {
2019-12-31 18:19:35 +01:00
Dictionary.TYPE_MAIN,
Dictionary.TYPE_CONTACTS,
2025-05-14 08:41:50 -06:00
Dictionary.TYPE_APPS,
2019-12-31 18:19:35 +01:00
Dictionary.TYPE_USER_HISTORY,
Dictionary.TYPE_USER};
2020-01-26 23:19:47 +01:00
String[] DYNAMIC_DICTIONARY_TYPES = new String[] {
2019-12-31 18:19:35 +01:00
Dictionary.TYPE_CONTACTS,
2025-05-14 08:41:50 -06:00
Dictionary.TYPE_APPS,
2019-12-31 18:19:35 +01:00
Dictionary.TYPE_USER_HISTORY,
Dictionary.TYPE_USER};
/** The facilitator will put words into the cache whenever it decodes them. */
2019-12-31 18:19:35 +01:00
void setValidSpellingWordReadCache(final LruCache<String, Boolean> cache);
/** The facilitator will get words from the cache whenever it needs to check their spelling. */
2019-12-31 18:19:35 +01:00
void setValidSpellingWordWriteCache(final LruCache<String, Boolean> cache);
/**
* Returns whether this facilitator is exactly for this locale.
*
* @param locale the locale to test against
*/
boolean isForLocale(final Locale locale);
interface DictionaryInitializationListener {
void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
}
/**
* Called every time {@link LatinIME} starts on a new text field.
2024-01-01 17:42:26 +01:00
* <p>
2019-12-31 18:19:35 +01:00
* WARNING: The service methods that call start/finish are very spammy.
*/
void onStartInput();
/**
* Called every time the {@link LatinIME} finishes with the current text field.
* May be followed by {@link #onStartInput} again in another text field,
* or it may be done for a while.
2024-01-01 17:42:26 +01:00
* <p>
2019-12-31 18:19:35 +01:00
* WARNING: The service methods that call start/finish are very spammy.
*/
void onFinishInput(Context context);
/** whether a dictionary is set */
2019-12-31 18:19:35 +01:00
boolean isActive();
/** the locale provided in resetDictionaries */
@NonNull Locale getMainLocale();
2019-12-31 18:19:35 +01:00
/** the most "trusted" locale, differs from getMainLocale only if multilingual typing is used */
@NonNull Locale getCurrentLocale();
boolean usesSameSettings(
@NonNull final List<Locale> locales,
final boolean contacts,
2025-05-14 08:41:50 -06:00
final boolean apps,
2025-05-20 21:32:23 +02:00
final boolean personalization
);
/** switches to newLocale, gets secondary locales from current settings, and sets secondary dictionaries */
2019-12-31 18:19:35 +01:00
void resetDictionaries(
final Context context,
final Locale newLocale,
final boolean useContactsDict,
2025-05-14 08:41:50 -06:00
final boolean useAppsDict,
2019-12-31 18:19:35 +01:00
final boolean usePersonalizedDicts,
final boolean forceReloadMainDictionary,
final String dictNamePrefix,
@Nullable final DictionaryInitializationListener listener);
/** removes the word from all editable dictionaries, and adds it to a blacklist in case it's in a read-only dictionary */
2023-06-28 20:40:35 +02:00
void removeWord(String word);
2019-12-31 18:19:35 +01:00
void closeDictionaries();
/** main dictionaries are loaded asynchronously after resetDictionaries */
2019-12-31 18:19:35 +01:00
boolean hasAtLeastOneInitializedMainDictionary();
/** main dictionaries are loaded asynchronously after resetDictionaries */
2019-12-31 18:19:35 +01:00
boolean hasAtLeastOneUninitializedMainDictionary();
/** main dictionaries are loaded asynchronously after resetDictionaries */
2019-12-31 18:19:35 +01:00
void waitForLoadingMainDictionaries(final long timeout, final TimeUnit unit)
throws InterruptedException;
/** adds the word to user history dictionary, calls adjustConfindences, and might add it to personal dictionary if the setting is enabled */
2019-12-31 18:19:35 +01:00
void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized,
@NonNull final NgramContext ngramContext, final long timeStampInSeconds,
2019-12-31 18:19:35 +01:00
final boolean blockPotentiallyOffensive);
/** adjust confidences for multilingual typing */
void adjustConfidences(final String word, final boolean wasAutoCapitalized);
/** a string with all used locales and their current confidences, null if multilingual typing is not used */
@Nullable String localesAndConfidences();
/** completely removes the word from user history (currently not if event is a backspace event) */
2019-12-31 18:19:35 +01:00
void unlearnFromUserHistory(final String word,
@NonNull final NgramContext ngramContext, final long timeStampInSeconds,
2019-12-31 18:19:35 +01:00
final int eventType);
@NonNull SuggestionResults getSuggestionResults(final ComposedData composedData,
final NgramContext ngramContext, @NonNull final Keyboard keyboard,
2019-12-31 18:19:35 +01:00
final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId,
final int inputStyle);
boolean isValidSpellingWord(final String word);
boolean isValidSuggestionWord(final String word);
void clearUserHistoryDictionary(final Context context);
2019-12-31 18:19:35 +01:00
String dump(final Context context);
void dumpDictionaryForDebug(final String dictName);
@NonNull List<DictionaryStats> getDictionaryStats(final Context context);
2019-12-31 18:19:35 +01:00
}