From 529839b6c82bd27358aed5ceaf0de52222b33fbb Mon Sep 17 00:00:00 2001 From: Helium314 Date: Fri, 1 Sep 2023 21:54:37 +0200 Subject: [PATCH] hopefully fix spell checker language detection --- .../latin/settings/SubtypeSettings.kt | 3 +- .../AndroidWordLevelSpellCheckerSession.java | 31 +++++++++++-------- .../SpellCheckerSettingsActivity.java | 29 +++++------------ app/src/main/res/xml/method_dummy.xml | 1 + 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SubtypeSettings.kt b/app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SubtypeSettings.kt index a628bae46..c9e40bdeb 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SubtypeSettings.kt +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SubtypeSettings.kt @@ -248,7 +248,8 @@ private fun loadEnabledSubtypes(context: Context) { } } -private var initialized = false +var initialized = false + private set private val enabledSubtypes = mutableListOf() private val resourceSubtypesByLocale = LinkedHashMap>(100) private val additionalSubtypes = mutableListOf() diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index 6dbf981c3..f95e0537c 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -17,6 +17,7 @@ package org.dslul.openboard.inputmethod.latin.spellcheck; import android.content.ContentResolver; +import android.content.SharedPreferences; import android.database.ContentObserver; import android.os.Binder; import android.provider.UserDictionary.Words; @@ -38,6 +39,9 @@ import org.dslul.openboard.inputmethod.latin.common.LocaleUtils; import org.dslul.openboard.inputmethod.latin.common.StringUtils; import org.dslul.openboard.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; + +import org.dslul.openboard.inputmethod.latin.settings.SubtypeSettingsKt; +import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils; import org.dslul.openboard.inputmethod.latin.utils.ScriptUtils; import org.dslul.openboard.inputmethod.latin.utils.StatsUtils; import org.dslul.openboard.inputmethod.latin.utils.SuggestionResults; @@ -148,17 +152,21 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { final InputMethodManager imm; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { - imm = mService.getApplicationContext() - .getSystemService(InputMethodManager.class); + imm = mService.getApplicationContext().getSystemService(InputMethodManager.class); if (imm != null) { - final InputMethodSubtype currentInputMethodSubtype = - imm.getCurrentInputMethodSubtype(); + final InputMethodSubtype currentInputMethodSubtype = imm.getCurrentInputMethodSubtype(); if (currentInputMethodSubtype != null) { final String localeString = currentInputMethodSubtype.getLocale(); if (!TextUtils.isEmpty(localeString)) { // Use keyboard locale if available in the spell checker return localeString; } + // localeString for this app is always empty, get it from settings if possible + // and we're sure this app is used + if (SubtypeSettingsKt.getInitialized() && "dummy".equals(currentInputMethodSubtype.getExtraValue())) { + final SharedPreferences prefs = DeviceProtectedUtils.getSharedPreferences(mService); + return SubtypeSettingsKt.getSelectedSubtype(prefs).getLocale(); + } } } } @@ -253,8 +261,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { // If the lower case version is not in the dictionary, it's still possible // that we have an all-caps version of a word that needs to be capitalized // according to the dictionary. E.g. "GERMANS" only exists in the dictionary as "Germans". - return mService.isValidWord(mLocale, - StringUtils.capitalizeFirstAndDowncaseRest(lowerCaseText, mLocale)); + return mService.isValidWord(mLocale, StringUtils.capitalizeFirstAndDowncaseRest(lowerCaseText, mLocale)); } // Note : this must be reentrant @@ -274,11 +281,10 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { updateLocale(); // It's good to keep this not local specific since the standard // ones may show up in other languages also. - String text = textInfo.getText(). - replaceAll(AndroidSpellCheckerService.APOSTROPHE, - AndroidSpellCheckerService.SINGLE_QUOTE). - replaceAll("^" + quotesRegexp, ""). - replaceAll(quotesRegexp + "$", ""); + String text = textInfo.getText() + .replaceAll(AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE) + .replaceAll("^" + quotesRegexp, "") + .replaceAll(quotesRegexp + "$", ""); final String localeRegex = scriptToPunctuationRegexMap.get( ScriptUtils.getScriptFromSpellCheckerLocale(mLocale) @@ -289,8 +295,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { } if (!mService.hasMainDictionaryForLocale(mLocale)) { - return AndroidSpellCheckerService.getNotInDictEmptySuggestions( - false /* reportAsTypo */); + return AndroidSpellCheckerService.getNotInDictEmptySuggestions(false /* reportAsTypo */); } // Handle special patterns like email, URI, telephone number. diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java index 98872bb20..a844a1840 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java @@ -16,46 +16,31 @@ package org.dslul.openboard.inputmethod.latin.spellcheck; -import android.annotation.TargetApi; -import android.content.Intent; -import android.os.Build; import android.os.Bundle; -import android.preference.PreferenceActivity; import org.dslul.openboard.inputmethod.latin.permissions.PermissionsManager; -import org.dslul.openboard.inputmethod.latin.utils.FragmentUtils; +import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; /** * Spell checker preference screen. */ -public final class SpellCheckerSettingsActivity extends PreferenceActivity +public final class SpellCheckerSettingsActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback { private static final String DEFAULT_FRAGMENT = SpellCheckerSettingsFragment.class.getName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + getSupportFragmentManager().beginTransaction() + .replace(android.R.id.content, new SpellCheckerSettingsFragment()) + .commit(); } @Override - public Intent getIntent() { - final Intent modIntent = new Intent(super.getIntent()); - modIntent.putExtra(EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT); - modIntent.putExtra(EXTRA_NO_HEADERS, true); - return modIntent; - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - @Override - public boolean isValidFragment(String fragmentName) { - return FragmentUtils.isValidFragment(fragmentName); - } - - @Override - public void onRequestPermissionsResult( - int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); PermissionsManager.get(this).onRequestPermissionsResult( requestCode, permissions, grantResults); } diff --git a/app/src/main/res/xml/method_dummy.xml b/app/src/main/res/xml/method_dummy.xml index 9c35b88ba..b4035be65 100644 --- a/app/src/main/res/xml/method_dummy.xml +++ b/app/src/main/res/xml/method_dummy.xml @@ -16,6 +16,7 @@ the system picker crashes on some devices for unknown reasons. android:isDefault="@bool/im_is_default" android:supportsSwitchingToNextInputMethod="true">