From 7248d01b1b4c1e5f32aaa4d798d4d1d28211a0e0 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:30:41 -0500 Subject: [PATCH] fix(storage): the Add game picker lists every card the file manager can open (#3123) --- .../droiddeck/launcher/ui/ExePickerDialog.kt | 17 ++-- .../droiddeck/launcher/ui/PickVolumesTest.kt | 88 +++++++++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/PickVolumesTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ExePickerDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ExePickerDialog.kt index 9fd3631553..5a1363a65e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ExePickerDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ExePickerDialog.kt @@ -60,7 +60,8 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.droiddeck.launcher.R -import com.droiddeck.launcher.session.GameStorage +import com.droiddeck.launcher.files.StorageRoot +import com.droiddeck.launcher.files.StorageRoots import com.droiddeck.launcher.stores.formatBytes import java.io.File @@ -74,10 +75,16 @@ internal class PickVolume(val root: File, val label: String, val removable: Bool } internal fun pickVolumes(context: Context): List = - listOf(PickVolume(Environment.getExternalStorageDirectory(), context.getString(R.string.games_internal), removable = false)) + - GameStorage.options(context).mapNotNull { o -> - File(o.path.substringBefore("/Android/")).takeIf { it.isDirectory }?.let { PickVolume(it, o.name, removable = true) } - }.distinctBy { it.root.absolutePath } + pickVolumes(Environment.getExternalStorageDirectory(), context.getString(R.string.games_internal), StorageRoots.list(context)) + +internal fun pickVolumes(internal: File, internalLabel: String, roots: List): List { + val internalPath = internal.absolutePath.trimEnd('/') + fun overlapsInternal(path: String) = path == internalPath || path.startsWith("$internalPath/") || internalPath.startsWith("$path/") + return listOf(PickVolume(internal, internalLabel, removable = false)) + + roots.filter { it.readable && !overlapsInternal(it.dir.absolutePath.trimEnd('/')) } + .map { PickVolume(it.dir, it.label, removable = true) } + .distinctBy { it.root.absolutePath } +} internal class PickFolder(val dir: File, val readable: Boolean) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/PickVolumesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/PickVolumesTest.kt new file mode 100644 index 0000000000..0ab95caf7f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/PickVolumesTest.kt @@ -0,0 +1,88 @@ +package com.droiddeck.launcher.ui + +import android.os.Environment +import android.os.Process +import android.os.storage.StorageManager +import com.droiddeck.launcher.files.StorageRoot +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.StorageVolumeBuilder +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class PickVolumesTest { + @get:Rule val temporary = TemporaryFolder() + + private val internal = File("/storage/emulated/0") + + private fun root(label: String, path: String, removable: Boolean = true, readable: Boolean = true) = + StorageRoot(label, File(path), removable, readable) + + @Test fun aCardOnlyReachableThroughMediaRwIsListed() { + val volumes = pickVolumes(internal, "Internal storage", listOf( + root("Internal", "/storage/emulated/0", removable = false), + root("4A1C-830B", "/mnt/media_rw/4A1C-830B"), + )) + + assertEquals(listOf("/storage/emulated/0", "/mnt/media_rw/4A1C-830B"), volumes.map { it.root.path }) + assertEquals("Internal storage", volumes.first().label) + assertFalse(volumes.first().removable) + assertEquals("4A1C-830B", volumes[1].label) + assertTrue(volumes[1].removable) + } + + @Test fun aVisibleCardKeepsItsStoragePathAndLabel() { + val volumes = pickVolumes(internal, "Internal storage", listOf( + root("Internal", "/storage/emulated/0", removable = false), + root("SanDisk SD card", "/storage/1234-ABCD"), + root("USB drive", "/storage/5678-EF01"), + )) + + assertEquals(listOf("Internal storage", "SanDisk SD card", "USB drive"), volumes.map { it.label }) + assertEquals(File("/storage/1234-ABCD"), volumes[1].root) + } + + @Test fun unreadableVolumesAndInternalStorageAreNotListedTwice() { + val volumes = pickVolumes(internal, "Internal storage", listOf( + root("Internal", "/storage/emulated", removable = false), + root("Internal", "/storage/emulated/0/Android/data/com.droiddeck.launcher/files", removable = false), + root("SD card", "/storage/1234-ABCD", readable = false), + root("USB drive", "/mnt/media_rw/5678-EF01"), + root("USB drive", "/mnt/media_rw/5678-EF01"), + )) + + assertEquals(listOf("/storage/emulated/0", "/mnt/media_rw/5678-EF01"), volumes.map { it.root.path }) + } + + @Test fun withNoExternalVolumesOnlyInternalStorageIsListed() { + val volumes = pickVolumes(internal, "Internal storage", listOf(root("Internal", "/storage/emulated/0", removable = false))) + + assertEquals(listOf(internal), volumes.map { it.root }) + } + + @Test fun aMountedVolumeWithoutAnAppFolderReachesThePicker() { + val mount = temporary.newFolder("4A1C-830B") + val context = RuntimeEnvironment.getApplication() + val volume = StorageVolumeBuilder("public:179,1", mount, "SD card", Process.myUserHandle(), Environment.MEDIA_MOUNTED) + .setIsPrimary(false) + .setIsEmulated(false) + .setIsRemovable(true) + .build() + shadowOf(context.getSystemService(StorageManager::class.java)).addStorageVolume(volume) + + val card = pickVolumes(context).single { it.root.absoluteFile == mount.absoluteFile } + + assertEquals("SD card", card.label) + assertTrue(card.removable) + } +}