fix(storage): the Add game picker lists every card the file manager can open (#3123)

This commit is contained in:
The412Banner authored and GitHub committed 2026-11-24 17:30:41 -05:00
1 parent f1f98c3bab
commit 7248d01b1b
2 files changed
+100 -5

No files matched your search

@@ -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<PickVolume> =
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<StorageRoot>): List<PickVolume> {
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)
@@ -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)
}
}