mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
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
This commit is contained in:
1 parent
51ec4a3029
commit
637f4b21ee
7 files changed
+86
-6
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user