From 4ab7e8b78e45535f9e3d09cc5362353d4f8636c8 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Mon, 17 Jun 2024 15:12:05 +0200 Subject: [PATCH] use fallback layout when custom layout file can't be opened --- .../internal/keyboard_parser/RawKeyboardParser.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/RawKeyboardParser.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/RawKeyboardParser.kt index 69abe164a..669bfc5c1 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/RawKeyboardParser.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/RawKeyboardParser.kt @@ -21,6 +21,7 @@ import helium314.keyboard.keyboard.internal.keyboard_parser.floris.toTextKey import helium314.keyboard.latin.common.splitOnWhitespace import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.utils.CUSTOM_LAYOUT_PREFIX +import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.ScriptUtils import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.getCustomFunctionalLayoutName @@ -32,6 +33,7 @@ import kotlinx.serialization.modules.polymorphic import java.io.File object RawKeyboardParser { + private const val TAG = "RawKeyboardParser" private val rawLayoutCache = hashMapOf MutableList>>() val symbolAndNumberLayouts = listOf(LAYOUT_SYMBOLS, LAYOUT_SYMBOLS_SHIFTED, LAYOUT_SYMBOLS_ARABIC, @@ -85,8 +87,15 @@ object RawKeyboardParser { private fun createCacheLambda(layoutName: String, context: Context): (KeyboardParams) -> MutableList> { val layoutFileName = getLayoutFileName(layoutName, context) - val layoutText = if (layoutFileName.startsWith(CUSTOM_LAYOUT_PREFIX)) getCustomLayoutFile(layoutFileName, context).readText() - else context.assets.open("layouts${File.separator}$layoutFileName").reader().use { it.readText() } + val layoutText = if (layoutFileName.startsWith(CUSTOM_LAYOUT_PREFIX)) { + try { + getCustomLayoutFile(layoutFileName, context).readText() + } catch (e: Exception) { // fall back to defaults if for some reason file is broken + val name = if (layoutName.contains("functional")) "functional_keys.json" else "qwerty.txt" + Log.e(TAG, "cannot open layout $layoutName, falling back to $name", e) + context.assets.open("layouts${File.separator}$name").reader().use { it.readText() } + } + } else context.assets.open("layouts${File.separator}$layoutFileName").reader().use { it.readText() } if (layoutFileName.endsWith(".json")) { val florisKeyData = parseJsonString(layoutText) return { params ->