Add Android app shortcuts to Steam

This commit is contained in:
Kurt Himebauch committed 2026-12-05 22:05:32 -05:00
1 parent 70be3d3eea
commit 788920ff89
12 files changed
+185 -8

No files matched your search

@@ -0,0 +1,59 @@
package com.droiddeck.launcher.frontend
import android.content.Context
import android.os.Build
import com.droiddeck.launcher.agent.AgentAdb
import com.droiddeck.launcher.core.WirelessAdbFix
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.util.zip.CRC32
internal object AndroidSteamApps {
const val MIN_ANDROID = 35
private const val LAUNCHER = "/usr/local/bin/droiddeck-android-app"
private val packagePattern = Regex("[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)+")
private fun prefs(context: Context) =
context.getSharedPreferences("android-steam-apps", Context.MODE_PRIVATE)
fun contains(context: Context, pkg: String): Boolean = prefs(context).contains(pkg)
fun set(context: Context, pkg: String, enabled: Boolean) {
require(packagePattern.matches(pkg))
val editor = prefs(context).edit()
if (enabled) editor.putBoolean(pkg, true) else editor.remove(pkg)
editor.apply()
}
fun supports(pkg: String): Boolean = Build.VERSION.SDK_INT >= MIN_ANDROID && packagePattern.matches(pkg)
fun sessionEnvironment(context: Context, sessionDir: File): List<String> {
if (Build.VERSION.SDK_INT < MIN_ANDROID) return emptyList()
val packages = prefs(context).all.keys.sorted()
val apps = JSONArray()
for (pkg in packages) {
if (!supports(pkg)) continue
val pm = context.packageManager
val launch = pm.getLaunchIntentForPackage(pkg) ?: continue
val component = launch.component ?: continue
val label = runCatching {
pm.getActivityInfo(component, 0).loadLabel(pm).toString()
}.getOrDefault(pkg).ifBlank { pkg }
val id = CRC32().apply { update("droiddeck-android-app:$pkg".toByteArray()) }.value or 0x80000000L
apps.put(JSONObject().put("name", label).put("appid", id)
.put("exe", LAUNCHER).put("dir", "/usr/local/bin").put("launch", pkg).put("native", true))
}
if (apps.length() == 0) return emptyList()
val listing = File(sessionDir, "android-apps.json").apply { writeText(apps.toString()) }
val env = mutableListOf("DD_ANDROID_APPS=${listing.absolutePath}")
if (WirelessAdbFix.hasSavedPairing(context)) {
val link = AgentAdb.prepare(context)
if (link.paired) {
env.add("DD_ANDROID_ADB_SERIAL=${link.serial}")
env.add("DD_ANDROID_ADB_KEY=${link.vendorKeys}")
}
}
return env
}
}
@@ -277,9 +277,16 @@ public final class PadBridge {
return lower.startsWith("uinput-") || lower.startsWith("uinput_") || lower.equals("ui-input-xiaomi");
}
private static boolean isVirtualInput(InputDevice device) {
// scrcpy's UHID pad is internal and reports the wired Xbox 360 USB identity.
return device.isVirtual() || isSystemVirtualInputName(device.getName())
|| (!device.isExternal() && device.getVendorId() == 0x045e
&& device.getProductId() == 0x028e && "Microsoft X-Box 360 Pad".equals(device.getName()));
}
public static boolean isFromController(InputDevice device) {
if (device == null) return false;
if (device.isVirtual() || isSystemVirtualInputName(device.getName())) return false;
if (isVirtualInput(device)) return false;
int sources = device.getSources();
if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) return true;
if ((sources & InputDevice.SOURCE_JOYSTICK) != InputDevice.SOURCE_JOYSTICK) return false;
@@ -299,7 +306,7 @@ public final class PadBridge {
}
static boolean isControllerKeyEvent(KeyEvent event, InputDevice device) {
if (device == null || device.isVirtual() || isSystemVirtualInputName(device.getName())) return false;
if (device == null || isVirtualInput(device)) return false;
if (KeyEvent.isGamepadButton(event.getKeyCode())) {
return device.supportsSource(InputDevice.SOURCE_GAMEPAD)
|| device.supportsSource(InputDevice.SOURCE_JOYSTICK);
@@ -327,8 +334,7 @@ public final class PadBridge {
}
static boolean isControllerMotionEvent(MotionEvent event, InputDevice device) {
return event.isFromSource(InputDevice.SOURCE_JOYSTICK) && device != null && !device.isVirtual()
&& !isSystemVirtualInputName(device.getName())
return event.isFromSource(InputDevice.SOURCE_JOYSTICK) && device != null && !isVirtualInput(device)
&& (device.supportsSource(InputDevice.SOURCE_GAMEPAD)
|| device.supportsSource(InputDevice.SOURCE_JOYSTICK));
}
@@ -191,6 +191,7 @@ object SessionFiles {
"usr/local/bin/droiddeck-snap-cli" to "usr/local/bin/snap",
"usr/local/bin/droiddeck-steam-shim" to "usr/local/bin/droiddeck-steam-shim",
"usr/local/bin/droiddeck-steam-shortcuts" to "usr/local/bin/droiddeck-steam-shortcuts",
"usr/local/bin/droiddeck-android-app" to "usr/local/bin/droiddeck-android-app",
"usr/local/bin/droiddeck-xbox" to "usr/local/bin/droiddeck-xbox",
"usr/local/bin/droiddeck-store-launch" to "usr/local/bin/droiddeck-store-launch",
"usr/local/bin/droiddeck-gog-maintenance" to "usr/local/bin/droiddeck-gog-maintenance",
@@ -714,6 +714,9 @@ class SessionService : Service() {
SessionEvents.step("added-games-listing") { com.droiddeck.launcher.frontend.AddedGames.writeListing(this, added) }
}
guest.add("DD_ADDED_GAMES=" + listing.path)
if (steamHere) runCatching {
guest.addAll(com.droiddeck.launcher.frontend.AndroidSteamApps.sessionEnvironment(this, sessionDir))
}.onFailure { Log.w(TAG, "Android Steam shortcuts", it) }
if (OfflineMode.active(this)) guest.add("DD_STEAM_OFFLINE=1")
if (com.droiddeck.launcher.gpu.Lossless.owned(this)) guest.add("DD_LOSSLESS_OWNED=1")
}
@@ -798,6 +798,17 @@ private fun AppMenu(
onDismiss()
HomeIcons.clear(ctx, app.key)
}
val steamApps = com.droiddeck.launcher.frontend.AndroidSteamApps
if (!app.work && steamApps.supports(app.packageName)) {
val added = steamApps.contains(ctx, app.packageName)
MenuItem(stringResource(if (added) R.string.apps_remove_from_steam else R.string.apps_add_to_steam), checked = false) {
onDismiss()
steamApps.set(ctx, app.packageName, !added)
val message = if (!added && !com.droiddeck.launcher.core.WirelessAdbFix.hasSavedPairing(ctx))
R.string.apps_steam_pairing_required else R.string.apps_steam_restart
android.widget.Toast.makeText(ctx, message, android.widget.Toast.LENGTH_LONG).show()
}
}
MenuItem(stringResource(R.string.apps_app_info), checked = false) {
onDismiss()
runCatching { HomeApp.openAppInfo(ctx, app) }