From aff97387484c26598a35dfff2cf278d6c7e8fe4d Mon Sep 17 00:00:00 2001 From: Delete228 Date: Sat, 7 Nov 2026 02:44:47 +0300 Subject: [PATCH] Preserve second-screen choices across partial selections and migration retries Track explicit whole-profile choices, including Auto, separately from inherited profiles so physical and main-screen overrides do not suppress legacy panel migration. Persist this distinction in library version 8 and read older selection records compatibly. Keep saved Deck panels during unavailable-controller fallbacks and retain legacy preferences when migration fails. Add regressions for all three review findings, explicit Auto selection and older saved libraries. Validation: all 992 input/UI tests passed. --- .../com/droiddeck/launcher/SessionActivity.kt | 21 ++++--- .../launcher/input/InputLibraries.kt | 8 ++- .../droiddeck/launcher/input/InputLibrary.kt | 6 +- .../launcher/input/InputLibraryTest.kt | 30 ++++++++++ .../input/SecondScreenProfileMigrationTest.kt | 58 +++++++++++++++++++ 5 files changed, 110 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 9bc5170130..7df65cd605 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -3317,17 +3317,22 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (pipUi || !started) return val mode = if (requested == SecondScreenMode.DECK_CONTROLS || requested == SecondScreenMode.KEYBOARD_TRACKPAD) { val remembered = SessionPrefs.secondScreenPanel(this) - if (remembered == SecondScreenMode.DECK_CONTROLS.id || remembered == SecondScreenMode.KEYBOARD_TRACKPAD.id) { + if (remembered == requested.id) { val desktop = SessionState.mode == SessionService.MODE_DESKTOP - val library = com.droiddeck.launcher.input.InputLibraries.read(this) val key = if (desktop) InputProfile.DESKTOP else InputProfile.GAME - if (key !in library.selections) { - val preset = if (requested == SecondScreenMode.DECK_CONTROLS) com.droiddeck.launcher.input.InputStockLayouts.DECK else com.droiddeck.launcher.input.InputStockLayouts.TRACKPAD - runCatching { com.droiddeck.launcher.input.InputLibraries.update(this) { it.selectPart(desktop, InputRole.SECONDARY, preset) } } - .onSuccess { refreshInputProfile() } - .onFailure { Log.w(TAG, "could not migrate the saved second-screen selection", it) } + val preset = if (requested == SecondScreenMode.DECK_CONTROLS) com.droiddeck.launcher.input.InputStockLayouts.DECK else com.droiddeck.launcher.input.InputStockLayouts.TRACKPAD + runCatching { + com.droiddeck.launcher.input.InputLibraries.update(this) { library -> + val selection = library.selections[key] + if (selection?.profileSelected == true || selection?.parts?.containsKey(InputRole.SECONDARY) == true) library + else library.selectPart(desktop, InputRole.SECONDARY, preset) + } + }.onSuccess { + refreshInputProfile() + SessionPrefs.setSecondScreenPanel(this, SecondScreenMode.CUSTOM_CONTROLS.id) + }.onFailure { + Log.w(TAG, "could not migrate the saved second-screen selection", it) } - SessionPrefs.setSecondScreenPanel(this, SecondScreenMode.CUSTOM_CONTROLS.id) } SecondScreenMode.CUSTOM_CONTROLS } else requested 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 534717c1f9..28e36f1806 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -9,7 +9,7 @@ import org.json.JSONObject import java.util.UUID object InputLibraries { - private const val VERSION = 7 + private const val VERSION = 8 private const val KEY = "library" private const val RECOVERY = "library-recovery." const val OVERLAY = "builtin.overlay" @@ -231,7 +231,7 @@ object InputLibraries { for ((mode, selection) in library.selections) { val parts = JSONObject() selection.parts.forEach { (role, id) -> parts.put(role.name, id) } - selections.put(mode, JSONObject().put("profile", selection.profile).put("parts", parts)) + selections.put(mode, JSONObject().put("profile", selection.profile).put("parts", parts).put("profileSelected", selection.profileSelected)) } return JSONObject().put("selections", selections).put("version", VERSION).put("selected", library.selected).put("steam", library.steam).put("desktop", library.desktop) .put("profiles", profiles).put("layouts", layouts).put("bindings", bindings).toString() @@ -272,7 +272,9 @@ object InputLibraries { val value = saved.getJSONObject(mode) val parts = value.getJSONObject("parts") require(parts.length() <= InputRole.entries.size) - InputSelection(value.getString("profile"), parts.keys().asSequence().associate { role -> InputRole.valueOf(role) to parts.getString(role) }) + val profile = value.getString("profile") + InputSelection(profile, parts.keys().asSequence().associate { role -> InputRole.valueOf(role) to parts.getString(role) }, + value.optBoolean("profileSelected", profile != InputProfiles.AUTO || parts.length() == 0)) } }.orEmpty() InputLibrary(profiles, layouts, bindings, doc.getString("selected"), doc.getString("steam"), doc.getString("desktop"), selections).validate() diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt index 3b3f391f4d..217e42216a 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -44,7 +44,9 @@ data class SavedInputBindings( ?: defaultElementClick(element, action(element)) { controls[it] ?: InputAction.Native() } } -data class InputSelection(val profile: String = InputProfiles.AUTO, val parts: Map = emptyMap()) +data class InputSelection( + val profile: String = InputProfiles.AUTO, val parts: Map = emptyMap(), val profileSelected: Boolean = false, +) data class InputPart(val bindings: String, val layout: String? = null) @@ -68,7 +70,7 @@ data class InputLibrary( val id = selection(isDesktop).profile return if (id == InputProfiles.AUTO) { if (isDesktop) desktop else steam } else id } - fun selectProfile(isDesktop: Boolean, id: String) = copy(selections = selections + (mode(isDesktop) to InputSelection(id))) + fun selectProfile(isDesktop: Boolean, id: String) = copy(selections = selections + (mode(isDesktop) to InputSelection(id, profileSelected = true))) fun selectPart(isDesktop: Boolean, role: InputRole, bindingsId: String): InputLibrary { val previous = selection(isDesktop) return copy(selections = selections + (mode(isDesktop) to previous.copy(parts = previous.parts + (role to bindingsId)))) diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt index 1c02220167..b96515a009 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -284,6 +284,36 @@ class InputLibraryTest { assertEquals(original.compileActive(true), restored.compileActive(true)) } + @Test fun explicitAutoSelectionStaysDistinctFromAnInheritedProfileAfterSaving() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val inherited = original.selectPart(false, InputRole.PHYSICAL, binding) + val explicit = original.selectProfile(false, InputProfiles.AUTO).selectPart(false, InputRole.PHYSICAL, binding) + assertFalse(InputLibraries.decode(InputLibraries.encode(inherited))!!.selection(false).profileSelected) + assertTrue(InputLibraries.decode(InputLibraries.encode(explicit))!!.selection(false).profileSelected) + assertEquals(inherited.compileActive(false), explicit.compileActive(false)) + } + + @Test fun versionSevenSelectionsRetainKnownProfileAndPartChoices() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val choices = listOf( + original.selectPart(false, InputRole.PHYSICAL, binding) to false, + original.selectProfile(false, InputProfiles.AUTO) to true, + original.selectProfile(false, "game").selectPart(false, InputRole.PHYSICAL, binding) to true, + original.selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) to false, + ) + for ((library, selected) in choices) { + val old = org.json.JSONObject(InputLibraries.encode(library)).put("version", 7) + old.getJSONObject("selections").getJSONObject("game").remove("profileSelected") + prefs.edit().putString("library", old.toString()).commit() + val restored = InputLibraries.read(context) + assertEquals(selected, restored.selection(false).profileSelected) + assertEquals(library, restored) + assertEquals(restored, InputLibraries.read(context)) + } + } + @Test fun savedQuickCombinationOwnsIndependentCopiesOfAllThreeParts() { val original = InputLibraries.read(context) val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) diff --git a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt index 485b55c46d..a14dcbbc6b 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.input +import android.content.Context import android.hardware.display.DisplayManager import android.view.Display import androidx.compose.runtime.MutableState @@ -7,6 +8,7 @@ import com.droiddeck.launcher.SessionActivity import com.droiddeck.launcher.session.SessionPrefs import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState +import org.json.JSONObject import org.junit.After import org.junit.Assert.* import org.junit.Before @@ -88,6 +90,62 @@ class SecondScreenProfileMigrationTest { assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) } + @Test fun unrelatedPartSelectionsDoNotSkipTheSavedPanelMigration() { + for (desktop in listOf(false, true)) for (role in listOf(InputRole.PHYSICAL, InputRole.PRIMARY)) { + customLibrary(desktop) + val original = InputLibraries.update(activity) { it.selectPart(desktop, role, it.profile("game").binding(role)) } + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.KEYBOARD_TRACKPAD.id) + open(SecondScreenMode.KEYBOARD_TRACKPAD.id, deckPad = true) + assertEquals(original.selectPart(desktop, InputRole.SECONDARY, InputStockLayouts.TRACKPAD), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + + @Test fun explicitProfilesIncludingAutoKeepTheirSecondaryLayoutAfterOtherPartChanges() { + for (desktop in listOf(false, true)) for (profile in listOf(InputProfiles.AUTO, InputProfile.GAME, InputProfile.DESKTOP)) { + for (role in listOf(null, InputRole.PHYSICAL, InputRole.PRIMARY)) { + customLibrary(desktop) + val original = InputLibraries.update(activity) { library -> + val selected = library.selectProfile(desktop, profile) + if (role == null) selected else selected.selectPart(desktop, role, selected.profile("game").binding(role)) + } + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.DECK_CONTROLS.id) + open(SecondScreenMode.DECK_CONTROLS.id, deckPad = true) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + } + + @Test fun unavailableDeckPanelKeepsItsSavedChoiceUntilItCanMigrate() { + for (desktop in listOf(false, true)) { + val original = customLibrary(desktop) + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.DECK_CONTROLS.id) + open(SecondScreenMode.DECK_CONTROLS.id, deckPad = false) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.DECK_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + open(SessionPrefs.secondScreenPanel(activity), deckPad = true) + assertEquals(original.selectPart(desktop, InputRole.SECONDARY, InputStockLayouts.DECK), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + + @Test fun failedMigrationKeepsTheSavedPanelAndCanBeRetried() { + val original = customLibrary(desktop = false) + val prefs = activity.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + val valid = prefs.getString("library", null)!! + val unsupported = JSONObject(valid).put("version", 999).toString() + prefs.edit().putString("library", unsupported).commit() + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.KEYBOARD_TRACKPAD.id) + open(SecondScreenMode.KEYBOARD_TRACKPAD.id, deckPad = true) + assertEquals(unsupported, prefs.getString("library", null)) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD.id, SessionPrefs.secondScreenPanel(activity)) + prefs.edit().putString("library", valid).commit() + open(SessionPrefs.secondScreenPanel(activity), deckPad = true) + assertEquals(original.selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + private fun customLibrary(desktop: Boolean): InputLibrary { SessionState.mode = if (desktop) SessionService.MODE_DESKTOP else SessionService.MODE_STEAM return InputLibraries.update(activity) { library ->