fallback to default layouts on parsing errors

This commit is contained in:
Helium314 2025-04-28 19:50:31 +02:00
parent 8b36ff1c54
commit 38547b0c81
2 changed files with 17 additions and 5 deletions

View file

@ -157,10 +157,9 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
} catch (KeyboardLayoutSetException e) {
Log.e(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause());
try {
final InputMethodSubtype qwerty = SubtypeUtilsAdditional.INSTANCE
.createEmojiCapableAdditionalSubtype(mRichImm.getCurrentSubtypeLocale(), SubtypeLocaleUtils.QWERTY, true);
final InputMethodSubtype defaults = SubtypeUtilsAdditional.INSTANCE.createDefaultSubtype(mRichImm.getCurrentSubtypeLocale());
mKeyboardLayoutSet = builder.setKeyboardGeometry(keyboardWidth, keyboardHeight)
.setSubtype(RichInputMethodSubtype.Companion.get(qwerty))
.setSubtype(RichInputMethodSubtype.Companion.get(defaults))
.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey)
.setNumberRowEnabled(settingsValues.mShowsNumberRow)
.setLanguageSwitchKeyEnabled(settingsValues.isLanguageSwitchKeyEnabled())
@ -169,9 +168,9 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
.setOneHandedModeEnabled(oneHandedModeEnabled)
.build();
mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState, oneHandedModeEnabled);
showToast("error loading the keyboard, falling back to qwerty", false);
showToast("error loading the keyboard, falling back to defaults", false);
} catch (KeyboardLayoutSetException e2) {
Log.e(TAG, "even fallback to qwerty failed: " + e2.mKeyboardId, e2.getCause());
Log.e(TAG, "even fallback to defaults failed: " + e2.mKeyboardId, e2.getCause());
}
}
}

View file

@ -9,9 +9,13 @@ import helium314.keyboard.latin.common.Constants
import helium314.keyboard.latin.common.Constants.Separators
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Defaults.default
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.SettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.LayoutType.Companion.toExtraValue
import helium314.keyboard.latin.utils.ScriptUtils.script
import java.util.EnumMap
import java.util.Locale
object SubtypeUtilsAdditional {
@ -46,9 +50,18 @@ object SubtypeUtilsAdditional {
fun createDummyAdditionalSubtype(locale: Locale, mainLayoutName: String) =
createAdditionalSubtype(locale, "${ExtraValue.KEYBOARD_LAYOUT_SET}=MAIN${Separators.KV}$mainLayoutName", false, false)
// only used in tests
fun createEmojiCapableAdditionalSubtype(locale: Locale, mainLayoutName: String, asciiCapable: Boolean) =
createAdditionalSubtype(locale, "${ExtraValue.KEYBOARD_LAYOUT_SET}=MAIN${Separators.KV}$mainLayoutName", asciiCapable, true)
/** creates a subtype with every layout being the default for its type */
fun createDefaultSubtype(locale: Locale): InputMethodSubtype {
val layouts = LayoutType.entries.associateWithTo(LayoutType.getLayoutMap(null)) { it.default }
SubtypeSettings.getResourceSubtypesForLocale(locale).firstOrNull()?.mainLayoutName()?.let { layouts[LayoutType.MAIN] = it }
val extra = ExtraValue.KEYBOARD_LAYOUT_SET + "=" + layouts.toExtraValue()
return createAdditionalSubtype(locale, extra, locale.script() == ScriptUtils.SCRIPT_LATIN, true)
}
fun removeAdditionalSubtype(context: Context, subtype: InputMethodSubtype) {
val prefs = context.prefs()
SubtypeSettings.removeEnabledSubtype(context, subtype)