diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt index 97f3d0ba4..127c525d1 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -83,8 +83,12 @@ object InputLibraries { if (invalid) { val encoded = encode(updated) require(encoded.length <= 2_000_000) { "The input library is too large to save." } - check(p.edit().putString(RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8)), stored) - .putString(KEY, encoded).commit()) { "Could not save the input library and its recovery copy." } + val recovery = RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8)) + if (!p.edit().putString(recovery, stored).putString(KEY, encoded).commit()) { + // A failed commit has already changed the in-memory preferences; put the previous library back. + p.edit().putString(KEY, stored).remove(recovery).apply() + error("Could not save the input library and its recovery copy.") + } } else write(context, updated) return updated } diff --git a/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt index 2966ec4ca..ef8322e9c 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt @@ -41,8 +41,8 @@ object KitLayout { /** The widest pair of side controls on any kit: a larger size never pushes the two halves, or the rows, into each other. */ private const val MIN_SPAN = 1200f private const val MAX_HEIGHT_MM = 80f - /** Insets are kept as reported until together they take more than this share of a side, which would leave no room for a usable pad. */ - private const val MAX_INSETS = .4f + /** The smallest half-size of any kit control in reference units. */ + private const val SMALLEST_HALF = 26f private const val CORNER_DP = 52f private const val ROW_DP = 60f private const val BUTTON_DP = 44f @@ -89,13 +89,15 @@ object KitLayout { /** The default controls for [kit] on a [width] × [height] screen. */ fun elements(context: Context, kit: PadKit, width: Int, height: Int, inset: Rect = Rect(), scale: Float = 1f): List { if (width <= 0 || height <= 0) return emptyList() - fun fit(a: Int, b: Int, side: Int): Pair { + // Insets stay as reported unless what's left is too small to keep every control above the minimum element size. + val smallestUnit = InputSurfaceElement.MIN_SIZE * max(width, height) / (2 * SMALLEST_HALF) + fun fit(a: Int, b: Int, side: Int, reference: Float): Pair { val first = a.coerceAtLeast(0); val second = b.coerceAtLeast(0) - val room = (side * MAX_INSETS).toInt() - return if (first + second <= room) first to second else (first * room / (first + second)) to (second * room / (first + second)) + val room = (side - (smallestUnit * reference).toInt() - 1).coerceAtLeast(0) + return if (first + second <= room) first to second else (first.toLong() * room / (first + second)).toInt() to (second.toLong() * room / (first + second)).toInt() } - val (left, right) = fit(inset.left, inset.right, width) - val (top, bottom) = fit(inset.top, inset.bottom, height) + val (left, right) = fit(inset.left, inset.right, width, MIN_SPAN) + val (top, bottom) = fit(inset.top, inset.bottom, height, REF_H) val safe = Rect(left, top, right, bottom) if (height > width) return portrait(context, kit, width, height, safe, scale) val w = width.toFloat(); val h = height.toFloat() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt index 7941c24d6..317673449 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt @@ -200,8 +200,7 @@ private fun DefaultControlsGroup(host: MenuHost, revision: Int, onEdit: (Triple< var below by remember { mutableStateOf(SessionPrefs.unfoldedControls(context)) } SettingsGroup(stringResource(R.string.osc_group_for, stringResource(R.string.osc_default))) { ChoiceRow(host, "osc-layout", stringResource(R.string.osc_layout), null, options, choice) { id -> - runCatching { ControlsChoice.choose(context, null, id, desktop = false) }.onFailure(onFailure) - onChanged() + runCatching { ControlsChoice.choose(context, null, id, desktop = false) }.onSuccess { onChanged() }.onFailure(onFailure) } if (choice != GameControls.STEAM) ActionRow(stringResource(R.string.osc_edit_layout), null, stringResource(R.string.ctrl_edit), { host.open = null diff --git a/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt index 885245856..944c83020 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt @@ -284,11 +284,11 @@ class KitLayoutTest { @Test fun insetsThatLeaveLessRoomShrinkTheKitInsteadOfOverlappingIt() { for (kit in PadKit.entries) for ((w, h, safe) in listOf(Triple(1600, 900, Rect(0, 60, 0, 0)), Triple(1600, 900, Rect(0, 730, 0, 0)), - Triple(1600, 900, Rect(0, 200, 0, 0)), Triple(1600, 900, Rect(0, 200, 0, 150)), + Triple(1600, 900, Rect(0, 200, 0, 0)), Triple(1600, 900, Rect(0, 200, 0, 150)), Triple(1600, 900, Rect(0, 390, 0, 0)), Triple(1920, 1080, Rect(80, 120, 80, 90)), Triple(2400, 1080, Rect(120, 30, 60, 40)))) { val elements = KitLayout.elements(context, kit, w, h, safe) assertEquals("$kit $w x $h $safe", emptyList>(), overlapping(elements, w, h)) - if (safe.top + safe.bottom <= h * .4f) for (e in elements) { + if (safe.top + safe.bottom <= 400) for (e in elements) { val (x, y, ew, eh) = px(e, w, h).toList() assertTrue("$kit $safe ${e.id}", x >= safe.left - .5f && y >= safe.top - .5f && x + ew <= w - safe.right + .5f && y + eh <= h - safe.bottom + .5f) } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlPrefsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlPrefsTest.kt index 4bdc4143e..df5a0cd45 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlPrefsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlPrefsTest.kt @@ -178,4 +178,28 @@ class OnScreenControlPrefsTest { val noDpad = edited.withLayout(saved.copy(elements = saved.elements.filterNot { it.id == KitLayout.DPAD_ID })) assertNull(noDpad.preset(bindings.id).elements["legacy:up"]) } + + @Test fun aFailedRecoverySaveLeavesTheStoredLibraryAsItWas() { + val stored = JSONObject().put("version", 1).put("profiles", "broken").toString() + val real = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + real.edit().clear().putString("library", stored).commit() + val failing = object : android.content.ContextWrapper(context) { + override fun getSharedPreferences(name: String, mode: Int): android.content.SharedPreferences { + val prefs = super.getSharedPreferences(name, mode) + return object : android.content.SharedPreferences by prefs { + override fun edit(): android.content.SharedPreferences.Editor { + val editor = prefs.edit() + return object : android.content.SharedPreferences.Editor by editor { + override fun putString(key: String?, value: String?) = apply { editor.putString(key, value) } + override fun remove(key: String?) = apply { editor.remove(key) } + override fun commit(): Boolean { editor.apply(); return false } + } + } + } + } + } + assertTrue(runCatching { InputLibraries.update(failing) { it } }.isFailure) + assertEquals(stored, real.getString("library", null)) + assertTrue(real.all.keys.none { it.startsWith("library-recovery.") }) + } }