hopefully fix spell checker language detection

This commit is contained in:
Helium314 2023-09-01 21:54:37 +02:00
parent 445eb37b5f
commit 529839b6c8
4 changed files with 28 additions and 36 deletions

View file

@ -248,7 +248,8 @@ private fun loadEnabledSubtypes(context: Context) {
}
}
private var initialized = false
var initialized = false
private set
private val enabledSubtypes = mutableListOf<InputMethodSubtype>()
private val resourceSubtypesByLocale = LinkedHashMap<String, MutableList<InputMethodSubtype>>(100)
private val additionalSubtypes = mutableListOf<InputMethodSubtype>()

View file

@ -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.

View file

@ -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);
}