feat: suggest Steam libraries found on internal storage and cards (#2023)

* feat: suggest Steam libraries found on internal storage and cards

* fix: add found card libraries by their readable path and drop superseded scan results
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-02 17:40:55 -05:00
1 parent bc5edb81db
commit 5fe3021e5e
5 files changed
+153 -13

No files matched your search

@@ -496,19 +496,39 @@ class FrontEndActivity : ComponentActivity() {
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> setGameStorage(path, GameStorage.labelFor(this, path)) }
}
private var steamLibraries by mutableStateOf<List<SteamLibraries.Entry>>(emptyList())
private var foundSteamLibraries by mutableStateOf<List<SteamLibraries.Found>>(emptyList())
private var allFilesAccessMissing by mutableStateOf(false)
private var steamLibraryScan = 0
private val pickSteamLibrary = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
val path = if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data) else null
if (path != null) Thread({
if (path != null) addSteamLibrary(path)
}
private fun addSteamLibrary(path: String) {
Thread({
val result = SteamLibraries.add(this, path)
ui.post {
steamLibraries = SteamLibraries.list(this)
refreshFoundSteamLibraries()
refresh()
if (result is SteamLibraries.AddResult.Refused) {
android.widget.Toast.makeText(this, getString(R.string.main_storage_not_usable, result.reason), android.widget.Toast.LENGTH_LONG).show()
when {
result is SteamLibraries.AddResult.Refused ->
android.widget.Toast.makeText(this, getString(R.string.main_storage_not_usable, result.reason), android.widget.Toast.LENGTH_LONG).show()
result is SteamLibraries.AddResult.Added && result.entry.path != path ->
android.widget.Toast.makeText(this, getString(R.string.libraries_used_inner, result.entry.label), android.widget.Toast.LENGTH_LONG).show()
}
}
}, "steam-library-add").start()
}
private fun refreshFoundSteamLibraries() {
allFilesAccessMissing = com.droiddeck.launcher.files.AllFilesAccess.missing()
val scan = ++steamLibraryScan
Thread({
val found = SteamLibraries.found(this)
ui.post { if (scan == steamLibraryScan) foundSteamLibraries = found }
}, "steam-library-scan").start()
}
private var romsDir by mutableStateOf<String?>(null)
private var steamGames by mutableStateOf<List<Library.SteamGame>>(emptyList())
private var steamGamesX64 by mutableStateOf(false)
@@ -1681,6 +1701,7 @@ class FrontEndActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
if (settingsMode == SessionService.MODE_STEAM) refreshFoundSteamLibraries()
compatLaunchVisible = true
com.droiddeck.launcher.ui.DualScreenPanel.yieldTo(taskId, false)
com.droiddeck.launcher.ui.DualScreenPanel.ownerPaused(taskId, false)
@@ -2141,6 +2162,8 @@ class FrontEndActivity : ComponentActivity() {
gameStorage = if (mode == SessionService.MODE_STEAM) gameStorage else null,
storageOptions = storageOptions,
steamLibraries = steamLibraries,
foundSteamLibraries = foundSteamLibraries,
allFilesAccessMissing = allFilesAccessMissing,
storageDiagnostics = mode == SessionService.MODE_STEAM && storageDiagnostics,
fexPreset = if (mode == SessionService.MODE_STEAM) fexPreset else null,
forceSsbs = forceSsbs,
@@ -2208,6 +2231,8 @@ class FrontEndActivity : ComponentActivity() {
onAddSteamLibrary = {
pickSteamLibrary.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.libraries_pick), null))
},
onAddFoundSteamLibrary = { path -> addSteamLibrary(path) },
onAllowAllFilesAccess = { com.droiddeck.launcher.files.AllFilesAccess.open(this) },
onRenameSteamLibrary = { id, label ->
SteamLibraries.rename(this, id, label)
steamLibraries = SteamLibraries.list(this)
@@ -2217,6 +2242,7 @@ class FrontEndActivity : ComponentActivity() {
SteamLibraries.remove(this, id)
steamLibraries = SteamLibraries.list(this)
refresh()
refreshFoundSteamLibraries()
},
onStorageDiagnostics = { on ->
SessionPrefs.setStorageDiagnosticsEnabled(this, on)
@@ -2453,6 +2479,7 @@ class FrontEndActivity : ComponentActivity() {
refreshWifiDiscovery()
gameStorage = SessionPrefs.gameStorage(this)
steamLibraries = SteamLibraries.list(this)
if (mode == SessionService.MODE_STEAM) refreshFoundSteamLibraries()
storageDiagnostics = SessionPrefs.storageDiagnosticsEnabled(this)
settingsMode = mode
Thread({
@@ -2483,6 +2510,7 @@ class FrontEndActivity : ComponentActivity() {
SessionPrefs.setGameStorage(this, path, label)
gameStorage = path
checkUsbLink()
if (settingsMode == SessionService.MODE_STEAM) refreshFoundSteamLibraries()
}
private fun checkUsbLink() {
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.session
import android.content.Context
import android.os.storage.StorageManager
import com.droiddeck.launcher.R
import com.droiddeck.launcher.files.StorageRoots
import com.droiddeck.launcher.frontend.AddedGames
import java.io.File
import org.json.JSONArray
@@ -49,15 +50,12 @@ object SteamLibraries {
class Refused(val reason: String) : AddResult()
}
fun add(context: Context, path: String, label: String = defaultLabel(context, File(path))): AddResult {
fun add(context: Context, picked: String, label: String? = null): AddResult {
val entries = list(context)
if (entries.size >= MAX) return AddResult.Refused(context.getString(R.string.libraries_full, MAX))
val path = innerLibrary(File(picked))?.path ?: picked
val host = canonical(File(path))
val taken = entries.map { canonical(it.host) } + listOfNotNull(
GameStorage.effective(context)?.let { canonical(File(it.path)) },
canonical(SessionPrefs.steamRoot(context, "arm64")),
)
if (taken.any { overlaps(it, host) }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
if (taken(context, entries).any { overlaps(it, host) }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
// Adding a Games folder itself is fine; a child of one would make it overlap and hide the rest of that folder.
if (AddedGames.roots(context).any { host.startsWith(canonical(it.host) + "/") }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
GameStorage.prepare(context, path)?.let { return AddResult.Refused(it) }
@@ -67,13 +65,70 @@ object SteamLibraries {
if (current.any { overlaps(canonical(it.host), host) }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
// A removed library's mount stays registered with Steam until its next session, so ids are never reused.
val next = maxOf(SessionPrefs.steamLibraryNextId(context), (current.maxOfOrNull { it.id.toInt() } ?: 0) + 1)
val entry = Entry(next.toString(), path, clean(label).ifEmpty { File(path).name })
val entry = Entry(next.toString(), path, clean(label ?: defaultLabel(context, File(path))).ifEmpty { File(path).name })
save(context, current + entry)
SessionPrefs.setSteamLibraryNextId(context, next + 1)
return AddResult.Added(entry)
}
}
private fun taken(context: Context, entries: List<Entry>): List<String> = entries.map { canonical(it.host) } + listOfNotNull(
GameStorage.effective(context)?.let { canonical(File(it.path)) },
canonical(SessionPrefs.steamRoot(context, "arm64")),
)
class Found(val path: String, val shown: String, val label: String, val games: Int)
/** Reads storage; call off the main thread. */
fun found(context: Context): List<Found> {
val entries = list(context)
val taken = taken(context, entries) + AddedGames.roots(context).map { canonical(it.host) }
val hits = ArrayList<Pair<File, Int>>()
for (root in StorageRoots.list(context)) {
if (!root.readable) continue
for (dir in candidates(rawView(root.dir), SCAN_DEPTH)) {
val games = gameCount(dir)
if (games > 0 && File(dir, "steamapps").canWrite() && taken.none { overlaps(it, canonical(dir)) }) hits += dir to games
}
}
val picked = ArrayList<Pair<File, Int>>()
for (hit in hits.sortedByDescending { it.second }) {
if (picked.none { overlaps(canonical(it.first), canonical(hit.first)) }) picked += hit
}
return picked.map { (dir, games) ->
val shown = File(storagePath(dir.path))
Found(dir.path, shown.path, defaultLabel(context, shown), games)
}
}
/** The FUSE view under /storage hides files other apps wrote unless DroidDeck has all files access; the raw card mount does not. */
private fun rawView(dir: File): File {
if (!dir.path.startsWith("/storage/") || dir.path.startsWith("/storage/emulated")) return dir
val raw = File("/mnt/media_rw/" + dir.path.removePrefix("/storage/"))
return if (raw.canRead() && raw.list() != null) raw else dir
}
internal fun innerLibrary(picked: File): File? {
if (gameCount(picked) > 0) return null
return candidates(picked, 1).filter { it != picked && gameCount(it) > 0 }.singleOrNull()
}
internal fun candidates(root: File, depth: Int): List<File> {
val out = arrayListOf(root)
var level = listOf(root)
repeat(depth) {
level = level.flatMap { dir ->
dir.listFiles { f -> f.isDirectory && !f.name.startsWith(".") && f.name != "steamapps" && !(dir == root && f.name == "Android") }
.orEmpty().sortedBy { it.name }
}.take(SCAN_LIMIT - out.size)
out += level
}
return out
}
internal fun gameCount(library: File): Int =
File(library, "steamapps").list()?.count { it.startsWith("appmanifest_") && it.endsWith(".acf") } ?: 0
fun rename(context: Context, id: String, label: String) {
val name = clean(label).takeIf { it.isNotEmpty() } ?: return
synchronized(lock) { save(context, list(context).map { if (it.id == id) Entry(it.id, it.path, name) else it }) }
@@ -84,17 +139,27 @@ object SteamLibraries {
synchronized(lock) { save(context, list(context).filterNot { it.id == id }) }
}
fun defaultLabel(context: Context, folder: File): String {
fun defaultLabel(context: Context, picked: File): String {
val folder = File(storagePath(picked.path))
val volume = try {
context.getSystemService(StorageManager::class.java)?.getStorageVolume(folder)?.getDescription(context)
} catch (e: Exception) {
null
}
val name = folder.name.ifEmpty { context.getString(R.string.gstore_folder) }
val parent = folder.parentFile
val named = if (folder.name.equals("Steam", true) && parent != null && !volumeRoot(parent)) parent else folder
val name = named.name.ifEmpty { context.getString(R.string.gstore_folder) }
return clean(if (volume.isNullOrBlank()) name else context.getString(R.string.gstore_folder_on_volume, name, volume))
}
private fun volumeRoot(dir: File): Boolean = dir.parent in setOf("/storage", "/mnt/media_rw", "/storage/emulated") || dir.parent == null
internal fun overlaps(a: String, b: String): Boolean = a == b || a.startsWith("$b/") || b.startsWith("$a/")
private fun canonical(file: File): String = runCatching { file.canonicalPath }.getOrDefault(file.absolutePath)
private fun storagePath(path: String): String = if (path.startsWith("/mnt/media_rw/")) "/storage/" + path.removePrefix("/mnt/media_rw/") else path
private fun canonical(file: File): String = storagePath(runCatching { file.canonicalPath }.getOrDefault(file.absolutePath))
private const val SCAN_DEPTH = 2
private const val SCAN_LIMIT = 4000
}
@@ -75,6 +75,8 @@ class ModeSettings(
val gameStorage: String? = null,
val storageOptions: List<com.droiddeck.launcher.session.GameStorage.Option> = emptyList(),
val steamLibraries: List<com.droiddeck.launcher.session.SteamLibraries.Entry> = emptyList(),
val foundSteamLibraries: List<com.droiddeck.launcher.session.SteamLibraries.Found> = emptyList(),
val allFilesAccessMissing: Boolean = false,
val storageDiagnostics: Boolean = false,
val fexPreset: String? = null,
val forceSsbs: Boolean = false,
@@ -126,6 +128,8 @@ class ModeSettingsActions(
val onPickGameStorageFolder: () -> Unit = {},
val onStorageDiagnostics: (Boolean) -> Unit = {},
val onAddSteamLibrary: () -> Unit = {},
val onAddFoundSteamLibrary: (path: String) -> Unit = {},
val onAllowAllFilesAccess: () -> Unit = {},
val onRenameSteamLibrary: (id: String, label: String) -> Unit = { _, _ -> },
val onRemoveSteamLibrary: (id: String) -> Unit = {},
val onFexPreset: (String) -> Unit = {},
@@ -421,6 +425,19 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
}
}
}
if (s.steamLibraries.size < com.droiddeck.launcher.session.SteamLibraries.MAX) for (found in s.foundSteamLibraries) {
SettingsRow(
found.label,
stringResource(R.string.libraries_found_hint, pluralStringResource(R.plurals.mode_added_count, found.games, found.games), found.shown),
) {
SecondaryButton(stringResource(R.string.libraries_add_found), compact = true) { a.onAddFoundSteamLibrary(found.path) }
}
}
if (s.allFilesAccessMissing && s.steamLibraries.size < com.droiddeck.launcher.session.SteamLibraries.MAX) {
SettingsRow(stringResource(R.string.libraries_access_title), stringResource(R.string.libraries_access_hint)) {
SecondaryButton(stringResource(R.string.libraries_access_allow), compact = true, onClick = a.onAllowAllFilesAccess)
}
}
SettingsRow(
stringResource(R.string.libraries_add),
if (s.steamLibraries.size < com.droiddeck.launcher.session.SteamLibraries.MAX) stringResource(R.string.libraries_hint)