2019-12-31 18:19:35 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
2023-10-17 13:44:01 +02:00
|
|
|
|
* modified
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
|
2019-12-31 18:19:35 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
|
package helium314.keyboard.latin.spellcheck;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.service.textservice.SpellCheckerService;
|
|
|
|
|
import android.text.InputType;
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
import android.view.inputmethod.InputMethodSubtype;
|
|
|
|
|
import android.view.textservice.SuggestionsInfo;
|
|
|
|
|
|
2023-09-01 08:08:36 +02:00
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
|
import helium314.keyboard.keyboard.Keyboard;
|
|
|
|
|
import helium314.keyboard.keyboard.KeyboardId;
|
|
|
|
|
import helium314.keyboard.keyboard.KeyboardLayoutSet;
|
|
|
|
|
import helium314.keyboard.latin.DictionaryFacilitator;
|
|
|
|
|
import helium314.keyboard.latin.DictionaryFacilitatorLruCache;
|
2024-02-01 08:51:17 +01:00
|
|
|
|
import helium314.keyboard.latin.InputAttributes;
|
2024-01-31 18:32:43 +01:00
|
|
|
|
import helium314.keyboard.latin.NgramContext;
|
|
|
|
|
import helium314.keyboard.latin.R;
|
|
|
|
|
import helium314.keyboard.latin.RichInputMethodSubtype;
|
|
|
|
|
import helium314.keyboard.latin.SuggestedWords;
|
|
|
|
|
import helium314.keyboard.latin.common.ComposedData;
|
2025-02-09 19:01:57 +01:00
|
|
|
|
import helium314.keyboard.latin.settings.Defaults;
|
2024-02-01 08:51:17 +01:00
|
|
|
|
import helium314.keyboard.latin.settings.Settings;
|
2024-01-31 18:32:43 +01:00
|
|
|
|
import helium314.keyboard.latin.settings.SettingsValuesForSuggestion;
|
2025-02-09 13:52:59 +01:00
|
|
|
|
import helium314.keyboard.latin.utils.KtxKt;
|
2025-02-16 12:49:19 +01:00
|
|
|
|
import helium314.keyboard.latin.utils.SubtypeSettings;
|
2025-02-15 12:22:53 +01:00
|
|
|
|
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional;
|
2024-01-31 18:32:43 +01:00
|
|
|
|
import helium314.keyboard.latin.utils.SuggestionResults;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Service for spell checking, using LatinIME's dictionaries and mechanisms.
|
|
|
|
|
*/
|
|
|
|
|
public final class AndroidSpellCheckerService extends SpellCheckerService
|
|
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
|
|
2023-12-11 20:00:52 +01:00
|
|
|
|
public static final int SPELLCHECKER_DUMMY_KEYBOARD_WIDTH = 480;
|
|
|
|
|
public static final int SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT = 301;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
|
|
|
|
private static final String DICTIONARY_NAME_PREFIX = "spellcheck_";
|
|
|
|
|
|
|
|
|
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
|
|
|
|
|
|
|
|
private final int MAX_NUM_OF_THREADS_READ_DICTIONARY = 2;
|
2024-02-01 08:51:17 +01:00
|
|
|
|
private final Semaphore mSemaphore = new Semaphore(MAX_NUM_OF_THREADS_READ_DICTIONARY, true);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
// TODO: Make each spell checker session has its own session id.
|
|
|
|
|
private final ConcurrentLinkedQueue<Integer> mSessionIdPool = new ConcurrentLinkedQueue<>();
|
|
|
|
|
|
|
|
|
|
private final DictionaryFacilitatorLruCache mDictionaryFacilitatorCache =
|
2024-02-01 08:51:17 +01:00
|
|
|
|
new DictionaryFacilitatorLruCache(this, DICTIONARY_NAME_PREFIX);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
private final ConcurrentHashMap<Locale, Keyboard> mKeyboardCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
// The threshold for a suggestion to be considered "recommended".
|
|
|
|
|
private float mRecommendedThreshold;
|
2024-02-01 08:51:17 +01:00
|
|
|
|
private SettingsValuesForSuggestion mSettingsValuesForSuggestion;
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
2024-01-29 16:00:24 +01:00
|
|
|
|
public static final String SINGLE_QUOTE = "'";
|
|
|
|
|
public static final String APOSTROPHE = "’";
|
2019-12-31 18:19:35 +01:00
|
|
|
|
|
|
|
|
|
public AndroidSpellCheckerService() {
|
|
|
|
|
super();
|
|
|
|
|
for (int i = 0; i < MAX_NUM_OF_THREADS_READ_DICTIONARY; i++) {
|
|
|
|
|
mSessionIdPool.add(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
super.onCreate();
|
2024-01-28 10:42:42 +01:00
|
|
|
|
mRecommendedThreshold = Float.parseFloat(getString(R.string.spellchecker_recommended_threshold_value));
|
2025-02-09 13:52:59 +01:00
|
|
|
|
final SharedPreferences prefs = KtxKt.prefs(this);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
2024-02-01 08:51:17 +01:00
|
|
|
|
onSharedPreferenceChanged(prefs, Settings.PREF_USE_CONTACTS);
|
2025-05-14 08:41:50 -06:00
|
|
|
|
onSharedPreferenceChanged(prefs, Settings.PREF_USE_APPS);
|
2025-02-09 19:01:57 +01:00
|
|
|
|
final boolean blockOffensive = prefs.getBoolean(Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE, Defaults.PREF_BLOCK_POTENTIALLY_OFFENSIVE);
|
2024-02-01 08:51:17 +01:00
|
|
|
|
mSettingsValuesForSuggestion = new SettingsValuesForSuggestion(blockOffensive, false);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getRecommendedThreshold() {
|
|
|
|
|
return mRecommendedThreshold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
|
2025-05-14 08:41:50 -06:00
|
|
|
|
if (key != null) switch (key) {
|
|
|
|
|
case Settings.PREF_USE_CONTACTS -> {
|
2025-02-09 19:01:57 +01:00
|
|
|
|
final boolean useContactsDictionary = prefs.getBoolean(Settings.PREF_USE_CONTACTS, Defaults.PREF_USE_CONTACTS);
|
2024-02-01 08:51:17 +01:00
|
|
|
|
mDictionaryFacilitatorCache.setUseContactsDictionary(useContactsDictionary);
|
2025-05-14 08:41:50 -06:00
|
|
|
|
}
|
|
|
|
|
case Settings.PREF_USE_APPS -> {
|
|
|
|
|
final boolean useAppsDictionary = prefs.getBoolean(Settings.PREF_USE_APPS, Defaults.PREF_USE_APPS);
|
|
|
|
|
mDictionaryFacilitatorCache.setUseAppsDictionary(useAppsDictionary);
|
|
|
|
|
}
|
|
|
|
|
case Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE -> {
|
2025-02-09 19:01:57 +01:00
|
|
|
|
final boolean blockOffensive = prefs.getBoolean(Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE, Defaults.PREF_BLOCK_POTENTIALLY_OFFENSIVE);
|
2024-02-01 08:51:17 +01:00
|
|
|
|
mSettingsValuesForSuggestion = new SettingsValuesForSuggestion(blockOffensive, false);
|
2025-05-14 08:41:50 -06:00
|
|
|
|
}}
|
2019-12-31 18:19:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Session createSession() {
|
|
|
|
|
// Should not refer to AndroidSpellCheckerSession directly considering
|
|
|
|
|
// that AndroidSpellCheckerSession may be overlaid.
|
|
|
|
|
return AndroidSpellCheckerSessionFactory.newInstance(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
|
|
|
|
|
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
|
|
|
|
|
* @return the empty SuggestionsInfo with the appropriate flags set.
|
|
|
|
|
*/
|
|
|
|
|
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
|
|
|
|
|
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
|
|
|
|
|
EMPTY_STRING_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an empty suggestionInfo with flags signaling the word is in the dictionary.
|
|
|
|
|
* @return the empty SuggestionsInfo with the appropriate flags set.
|
|
|
|
|
*/
|
|
|
|
|
public static SuggestionsInfo getInDictEmptySuggestions() {
|
2024-02-01 08:51:17 +01:00
|
|
|
|
return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, EMPTY_STRING_ARRAY);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isValidWord(final Locale locale, final String word) {
|
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
|
try {
|
2023-12-11 20:00:52 +01:00
|
|
|
|
DictionaryFacilitator dictionaryFacilitatorForLocale = mDictionaryFacilitatorCache.get(locale);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
return dictionaryFacilitatorForLocale.isValidSpellingWord(word);
|
|
|
|
|
} finally {
|
|
|
|
|
mSemaphore.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SuggestionResults getSuggestionResults(final Locale locale,
|
|
|
|
|
final ComposedData composedData, final NgramContext ngramContext,
|
2023-09-01 08:08:36 +02:00
|
|
|
|
@NonNull final Keyboard keyboard) {
|
2019-12-31 18:19:35 +01:00
|
|
|
|
Integer sessionId = null;
|
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
|
try {
|
|
|
|
|
sessionId = mSessionIdPool.poll();
|
2024-02-01 08:51:17 +01:00
|
|
|
|
DictionaryFacilitator dictionaryFacilitatorForLocale = mDictionaryFacilitatorCache.get(locale);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
return dictionaryFacilitatorForLocale.getSuggestionResults(composedData, ngramContext,
|
|
|
|
|
keyboard, mSettingsValuesForSuggestion,
|
|
|
|
|
sessionId, SuggestedWords.INPUT_STYLE_TYPING);
|
|
|
|
|
} finally {
|
|
|
|
|
if (sessionId != null) {
|
|
|
|
|
mSessionIdPool.add(sessionId);
|
|
|
|
|
}
|
|
|
|
|
mSemaphore.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasMainDictionaryForLocale(final Locale locale) {
|
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
|
try {
|
|
|
|
|
final DictionaryFacilitator dictionaryFacilitator =
|
|
|
|
|
mDictionaryFacilitatorCache.get(locale);
|
|
|
|
|
return dictionaryFacilitator.hasAtLeastOneInitializedMainDictionary();
|
|
|
|
|
} finally {
|
|
|
|
|
mSemaphore.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onUnbind(final Intent intent) {
|
|
|
|
|
mSemaphore.acquireUninterruptibly(MAX_NUM_OF_THREADS_READ_DICTIONARY);
|
|
|
|
|
try {
|
|
|
|
|
mDictionaryFacilitatorCache.closeDictionaries();
|
|
|
|
|
} finally {
|
|
|
|
|
mSemaphore.release(MAX_NUM_OF_THREADS_READ_DICTIONARY);
|
|
|
|
|
}
|
|
|
|
|
mKeyboardCache.clear();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Keyboard getKeyboardForLocale(final Locale locale) {
|
|
|
|
|
Keyboard keyboard = mKeyboardCache.get(locale);
|
|
|
|
|
if (keyboard == null) {
|
|
|
|
|
keyboard = createKeyboardForLocale(locale);
|
2024-01-28 10:42:42 +01:00
|
|
|
|
mKeyboardCache.put(locale, keyboard);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
}
|
|
|
|
|
return keyboard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Keyboard createKeyboardForLocale(final Locale locale) {
|
2025-02-28 22:17:06 +01:00
|
|
|
|
if (Settings.getValues() == null) {
|
2024-02-01 08:51:17 +01:00
|
|
|
|
// creating a keyboard reads SettingsValues from Settings instance
|
|
|
|
|
// maybe it would be "more correct" to create an instance of SettingsValues and use that one instead
|
|
|
|
|
// but creating a global one if not existing should be fine too
|
|
|
|
|
final EditorInfo editorInfo = new EditorInfo();
|
|
|
|
|
editorInfo.inputType = InputType.TYPE_CLASS_TEXT;
|
|
|
|
|
Settings.getInstance().loadSettings(this, locale, new InputAttributes(editorInfo, false, getPackageName()));
|
|
|
|
|
}
|
2025-02-16 12:49:19 +01:00
|
|
|
|
final String mainLayoutName = SubtypeSettings.INSTANCE.getMatchingMainLayoutNameForLocale(locale);
|
2025-02-16 10:44:11 +01:00
|
|
|
|
final InputMethodSubtype subtype = SubtypeUtilsAdditional.INSTANCE.createDummyAdditionalSubtype(locale, mainLayoutName);
|
2019-12-31 18:19:35 +01:00
|
|
|
|
final KeyboardLayoutSet keyboardLayoutSet = createKeyboardSetForSpellChecker(subtype);
|
|
|
|
|
return keyboardLayoutSet.getKeyboard(KeyboardId.ELEMENT_ALPHABET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private KeyboardLayoutSet createKeyboardSetForSpellChecker(final InputMethodSubtype subtype) {
|
|
|
|
|
final EditorInfo editorInfo = new EditorInfo();
|
|
|
|
|
editorInfo.inputType = InputType.TYPE_CLASS_TEXT;
|
|
|
|
|
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(this, editorInfo);
|
2023-09-09 18:47:21 +02:00
|
|
|
|
return builder
|
|
|
|
|
.setKeyboardGeometry(SPELLCHECKER_DUMMY_KEYBOARD_WIDTH, SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT)
|
2025-02-13 17:29:51 +01:00
|
|
|
|
.setSubtype(RichInputMethodSubtype.Companion.get(subtype))
|
2024-02-01 08:51:17 +01:00
|
|
|
|
.setIsSpellChecker(true)
|
2023-09-09 18:47:21 +02:00
|
|
|
|
.disableTouchPositionCorrectionData()
|
|
|
|
|
.build();
|
2019-12-31 18:19:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|