From 637f4b21eed470c1b5c665e40f1a611f4d999e09 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:03:25 -0500 Subject: [PATCH] fix(shortcuts): the companion no longer keeps DroidDeck running, so Cocoon stops calling it open (#2764) * fix(shortcuts): the companion no longer keeps DroidDeck running, so Cocoon stops calling it open * fix(shortcuts): reset the relay holder when it is installed again --- .../main/java/com/droiddeck/launcher/App.kt | 1 + .../launcher/core/SteamIntegration.kt | 6 ++- .../launcher/input/ShortcutsRelay.kt | 52 +++++++++++++++++++ .../launcher/input/ShortcutsRelayTest.kt | 25 ++++++++- shortcuts/build.gradle | 4 +- .../droiddeck/shortcuts/ShortcutsService.java | 3 +- .../java/com/droiddeck/shortcuts/Relay.java | 1 + 7 files changed, 86 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/App.kt b/app/src/main/java/com/droiddeck/launcher/App.kt index 3cedb3f171..6a3f123644 100644 --- a/app/src/main/java/com/droiddeck/launcher/App.kt +++ b/app/src/main/java/com/droiddeck/launcher/App.kt @@ -43,6 +43,7 @@ class App : Application() { if (process == "$packageName:vr_recovery" || process == "$packageName:rgb_diagnostic" || process == "$packageName:shortcuts_sync" || process == "$packageName:bottom_desktop") return CrashHandler.install(this) + com.droiddeck.launcher.input.ShortcutsRelay.Holder.install(this) com.droiddeck.launcher.agent.AgentHost.refresh(this) com.droiddeck.launcher.agent.AgentTab.load(this) com.droiddeck.launcher.ui.NavTabs.load(this) diff --git a/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt b/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt index 84f87cca13..cd10300f4f 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt @@ -37,7 +37,7 @@ object SteamIntegration { locationOn = WifiDiscovery.locationEnabled(context), brightness = Settings.System.canWrite(context), home = HomeApp.isDefault(context), - shortcuts = shortcutsEnabled(context), + shortcuts = shortcutsEnabled(context) && shortcutsCurrent(context), shortcutsRestricted = restrictedSettings(context), suggested = !SessionPrefs.sessionPrefs(context).getBoolean("steam_integration_seen", false), ) @@ -61,6 +61,10 @@ object SteamIntegration { return enabled.split(':').any { ComponentName.unflattenFromString(it.trim()) == shortcutsComponent } } + @Suppress("DEPRECATION") + fun shortcutsCurrent(context: Context): Boolean = + runCatching { context.packageManager.getPackageInfo(Relay.PACKAGE, 0).versionCode }.getOrDefault(0) >= Relay.VERSION + fun shortcutsListed(context: Context): Boolean = context.getSystemService(android.view.accessibility.AccessibilityManager::class.java) ?.installedAccessibilityServiceList.orEmpty() diff --git a/app/src/main/java/com/droiddeck/launcher/input/ShortcutsRelay.kt b/app/src/main/java/com/droiddeck/launcher/input/ShortcutsRelay.kt index 6c24a7a472..a39a84adf1 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ShortcutsRelay.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ShortcutsRelay.kt @@ -178,4 +178,56 @@ class ShortcutsRelay : Service(), InputManager.InputDeviceListener { private companion object { const val KEY_TIMEOUT_MS = 200L } + + // Frontends such as Cocoon treat a running player process as an open game, so the relay runs only while a screen is started. + object Holder : android.app.Application.ActivityLifecycleCallbacks, android.content.ServiceConnection { + private const val RELEASE_MS = 1_000L + private val handler = Handler(Looper.getMainLooper()) + private var app: android.app.Application? = null + private var started = 0 + private var bound = false + private val release = Runnable { unbind() } + + fun install(application: android.app.Application) { + app?.unregisterActivityLifecycleCallbacks(this) + handler.removeCallbacks(release) + started = 0 + bound = false + app = application + application.registerActivityLifecycleCallbacks(this) + } + + override fun onActivityStarted(activity: android.app.Activity) { + started++ + handler.removeCallbacks(release) + if (!bound) bind() + } + + override fun onActivityStopped(activity: android.app.Activity) { + started = (started - 1).coerceAtLeast(0) + if (started == 0) handler.postDelayed(release, RELEASE_MS) + } + + private fun bind() { + val context = app ?: return + if (runCatching { context.packageManager.getPackageInfo(Relay.PACKAGE, 0) }.isFailure) return + bound = runCatching { + context.bindService(Intent(context, ShortcutsRelay::class.java), this, Context.BIND_AUTO_CREATE) + }.getOrDefault(false) + } + + private fun unbind() { + if (!bound) return + bound = false + runCatching { app?.unbindService(this) } + } + + override fun onServiceConnected(name: android.content.ComponentName?, service: IBinder?) = Unit + override fun onServiceDisconnected(name: android.content.ComponentName?) = Unit + override fun onActivityCreated(activity: android.app.Activity, savedInstanceState: android.os.Bundle?) = Unit + override fun onActivityResumed(activity: android.app.Activity) = Unit + override fun onActivityPaused(activity: android.app.Activity) = Unit + override fun onActivitySaveInstanceState(activity: android.app.Activity, outState: android.os.Bundle) = Unit + override fun onActivityDestroyed(activity: android.app.Activity) = Unit + } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/ShortcutsRelayTest.kt b/app/src/test/java/com/droiddeck/launcher/input/ShortcutsRelayTest.kt index 2a8d99cd81..283951b5e0 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/ShortcutsRelayTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/ShortcutsRelayTest.kt @@ -32,9 +32,10 @@ class ShortcutsRelayTest { private fun choose(value: String?) = Settings.Secure.putString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, value) - private fun installCompanion() { + @Suppress("DEPRECATION") + private fun installCompanion(version: Int = Relay.VERSION) { val app = ApplicationInfo().apply { packageName = Relay.PACKAGE; uid = companionUid } - shadowOf(context.packageManager).installPackage(PackageInfo().apply { packageName = Relay.PACKAGE; applicationInfo = app }) + shadowOf(context.packageManager).installPackage(PackageInfo().apply { packageName = Relay.PACKAGE; applicationInfo = app; versionCode = version }) shadowOf(context.packageManager).setPackagesForUid(companionUid, Relay.PACKAGE) } @@ -84,6 +85,26 @@ class ShortcutsRelayTest { assertEquals(0, config(binder, companionUid)) } + @Test fun anOldCompanionIsOfferedAsAnUpdate() { + installCompanion(version = 1) + choose("${Relay.PACKAGE}/${Relay.SERVICE}") + assertTrue(SteamIntegration.shortcutsEnabled(context)) + assertFalse(SteamIntegration.read(context).shortcuts) + installCompanion() + assertTrue(SteamIntegration.read(context).shortcuts) + } + + @Test fun droidDeckHoldsTheRelayOnlyWhileAScreenIsStarted() { + installCompanion() + ShortcutsRelay.Holder.install(context) + val activity = Robolectric.buildActivity(android.app.Activity::class.java).create() + activity.start() + assertEquals(1, shadowOf(context).boundServiceConnections.size) + activity.stop() + shadowOf(android.os.Looper.getMainLooper()).idleFor(java.time.Duration.ofSeconds(2)) + assertEquals(0, shadowOf(context).boundServiceConnections.size) + } + @Test fun windowSnapshotsSurviveTheTrip() { val sent = mapOf(2 to listOf(Relay.Window(7, 1, 3, Rect(0, 0, 1080, 1240), "guest.app"), Relay.Window(8, 2, 1, Rect(1, 2, 3, 4), null))) val got = Relay.decode(Relay.encode(sent)) diff --git a/shortcuts/build.gradle b/shortcuts/build.gradle index 4ed0fab21a..62f6a84201 100644 --- a/shortcuts/build.gradle +++ b/shortcuts/build.gradle @@ -10,8 +10,8 @@ android { applicationId 'com.droiddeck.shortcuts' minSdkVersion 26 targetSdkVersion 34 - versionCode 2 - versionName '2' + versionCode 3 + versionName '3' } sourceSets.main.java.srcDir('src/protocol/java') diff --git a/shortcuts/src/main/java/com/droiddeck/shortcuts/ShortcutsService.java b/shortcuts/src/main/java/com/droiddeck/shortcuts/ShortcutsService.java index 91624c99a0..789f5968b3 100644 --- a/shortcuts/src/main/java/com/droiddeck/shortcuts/ShortcutsService.java +++ b/shortcuts/src/main/java/com/droiddeck/shortcuts/ShortcutsService.java @@ -89,7 +89,8 @@ public class ShortcutsService extends AccessibilityService { if (linked) continue; Link link = new Link(component); try { - if (bindService(new Intent(Relay.ACTION).setComponent(component), link, Context.BIND_AUTO_CREATE)) links.add(link); + // No BIND_AUTO_CREATE: frontends read a running player as an open game, so the relay app decides when it runs. + if (bindService(new Intent(Relay.ACTION).setComponent(component), link, 0)) links.add(link); else unbindService(link); } catch (RuntimeException ignored) { } diff --git a/shortcuts/src/protocol/java/com/droiddeck/shortcuts/Relay.java b/shortcuts/src/protocol/java/com/droiddeck/shortcuts/Relay.java index 77f354d055..bc8b03b9b4 100644 --- a/shortcuts/src/protocol/java/com/droiddeck/shortcuts/Relay.java +++ b/shortcuts/src/protocol/java/com/droiddeck/shortcuts/Relay.java @@ -13,6 +13,7 @@ public final class Relay { public static final String SERVICE = PACKAGE + ".ShortcutsService"; public static final String ACTION = "com.droiddeck.shortcuts.RELAY"; public static final String DESCRIPTOR = "com.droiddeck.shortcuts.Relay"; + public static final int VERSION = 3; public static final int CONFIG = 1; public static final int KEY = 2;