mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat: warn when a game library drive is linked at USB 2.0 speed (#1746)
* Warn when a drive used as a game library is linked at USB 2.0 speed Reads the drive's USB link speed from sysfs through its mount, and shows a dismissible warning with the USB generation, link speed and a tip for a faster cable or drive. The warning returns after the drive is plugged in again unless Don't show this again is switched on. * Keep slow USB dismissals per plug-in across restarts and recheck after a dismiss Dismissals are stored with the boot count and the USB bus and device number, so they survive a process restart, end when that drive is plugged in again even while the launcher is stopped, and never clear other drives. A dismiss looks for the next slow drive, stale scans are dropped, and an unknown port speed is no longer treated as a USB 2.0 port. * Drop slow USB scans that a newer library or mount check has superseded * Invalidate pending slow USB scans as soon as a media broadcast arrives * Revalidate an open slow USB warning on every check and match raw mount library paths Every check now rescans, so a warning that is already showing is replaced or cleared when its drive is removed or the library changes. Library paths saved under /mnt/media_rw are treated as their /storage volume. * Keep the Don't show this again choice when a rescan closes the slow USB warning * Save the slow USB opt-out before the launcher activity is torn down
This commit is contained in:
1 parent
2410b8c67c
commit
d1bd23df16
38 files changed
+504
No files matched your search
@@ -213,6 +213,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
}
|
||||
private val mediaReceiver = object : android.content.BroadcastReceiver() {
|
||||
override fun onReceive(context: android.content.Context?, intent: Intent?) {
|
||||
usbScan++
|
||||
ui.removeCallbacks(mediaRefresh)
|
||||
ui.postDelayed(mediaRefresh, MEDIA_SETTLE_MS)
|
||||
}
|
||||
@@ -472,6 +473,16 @@ class FrontEndActivity : ComponentActivity() {
|
||||
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
|
||||
private var backActionsInverted by mutableStateOf(false)
|
||||
private var gameStorage by mutableStateOf("")
|
||||
private var slowUsb by mutableStateOf<com.droiddeck.launcher.session.UsbLink.SlowDrive?>(null)
|
||||
private var usbScan = 0
|
||||
private var slowUsbNever by mutableStateOf(false)
|
||||
|
||||
private fun showSlowUsb(drive: com.droiddeck.launcher.session.UsbLink.SlowDrive?) {
|
||||
val off = slowUsbNever
|
||||
if (off) SessionPrefs.setSlowUsbWarningOff(this)
|
||||
slowUsbNever = false
|
||||
slowUsb = drive.takeUnless { off }
|
||||
}
|
||||
private var storageDiagnostics by mutableStateOf(false)
|
||||
private var storageOptions by mutableStateOf<List<GameStorage.Option>>(emptyList())
|
||||
private val pickGameStorage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
|
||||
@@ -965,6 +976,13 @@ class FrontEndActivity : ComponentActivity() {
|
||||
if (decky.deckyPendingZip != null) com.droiddeck.launcher.ui.DeckyZipWarningDialog(
|
||||
onConfirm = { decky.confirmPluginZip() }, onCancel = { decky.cancelPluginZip() },
|
||||
)
|
||||
slowUsb?.let { drive ->
|
||||
com.droiddeck.launcher.ui.SlowUsbDialog(drive, slowUsbNever, { slowUsbNever = it }) {
|
||||
com.droiddeck.launcher.session.UsbLink.markShown(this, drive)
|
||||
showSlowUsb(null)
|
||||
checkUsbLink()
|
||||
}
|
||||
}
|
||||
val page: (@Composable () -> Unit)? = when {
|
||||
showPerformance -> { { PerformanceHost() } }
|
||||
showComponents -> { { ComponentsHost() } }
|
||||
@@ -1617,6 +1635,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
if (slowUsbNever) SessionPrefs.setSlowUsbWarningOff(this)
|
||||
saveCompatLaunch(outState)
|
||||
pendingSwitch?.let { outState.putParcelable(KEY_PENDING_SWITCH, it) }
|
||||
switchingTo?.let {
|
||||
@@ -2198,6 +2217,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
private fun refreshAddedGames() {
|
||||
checkUsbLink()
|
||||
sgdbKeyKnown()
|
||||
fetchAddedGameArt()
|
||||
}
|
||||
@@ -2302,6 +2322,20 @@ class FrontEndActivity : ComponentActivity() {
|
||||
}
|
||||
SessionPrefs.setGameStorage(this, path, label)
|
||||
gameStorage = path
|
||||
checkUsbLink()
|
||||
}
|
||||
|
||||
private fun checkUsbLink() {
|
||||
if (android.os.Looper.myLooper() != android.os.Looper.getMainLooper()) { ui.post { checkUsbLink() }; return }
|
||||
val scan = ++usbScan
|
||||
Thread({
|
||||
val drive = runCatching { com.droiddeck.launcher.session.UsbLink.slowLibrary(this) }.getOrNull()
|
||||
ui.post {
|
||||
if (scan != usbScan || isFinishing) return@post
|
||||
val due = drive?.takeIf { com.droiddeck.launcher.session.UsbLink.stillDue(this, it) }
|
||||
if (due == null) { if (slowUsb != null) showSlowUsb(null) } else if (slowUsb?.key != due.key) showSlowUsb(due)
|
||||
}
|
||||
}, "usb-link").start()
|
||||
}
|
||||
|
||||
private fun refreshCores() {
|
||||
|
||||
@@ -21,6 +21,8 @@ object SessionPrefs {
|
||||
private const val LEGACY_SUSPEND_DOWNLOADS = "downloads"
|
||||
private const val STEAM_DOWNLOADS_IN_BACKGROUND = "steamDownloadsInBackground"
|
||||
private const val DECKY_ZIP_WARNING_SEEN = "deckyZipWarningSeen"
|
||||
private const val SLOW_USB_WARNING_OFF = "slowUsbWarningOff"
|
||||
private const val SLOW_USB_DISMISSED = "slowUsbDismissed"
|
||||
|
||||
const val CONTROLLER_DECK = "deck"
|
||||
const val CONTROLLER_XBOX360 = "xbox360"
|
||||
@@ -1297,6 +1299,18 @@ object SessionPrefs {
|
||||
prefs(context).edit().putBoolean(DECKY_ZIP_WARNING_SEEN, true).apply()
|
||||
}
|
||||
|
||||
fun slowUsbWarningOff(context: Context): Boolean = prefs(context).getBoolean(SLOW_USB_WARNING_OFF, false)
|
||||
|
||||
fun setSlowUsbWarningOff(context: Context) {
|
||||
prefs(context).edit().putBoolean(SLOW_USB_WARNING_OFF, true).apply()
|
||||
}
|
||||
|
||||
fun slowUsbDismissed(context: Context): Set<String> = prefs(context).getStringSet(SLOW_USB_DISMISSED, null)?.toSet().orEmpty()
|
||||
|
||||
fun setSlowUsbDismissed(context: Context, keys: Set<String>) {
|
||||
prefs(context).edit().putStringSet(SLOW_USB_DISMISSED, keys.toSet()).apply()
|
||||
}
|
||||
|
||||
fun steamDownloadsInBackground(context: Context): Boolean {
|
||||
val prefs = prefs(context)
|
||||
return prefs.getBoolean(STEAM_DOWNLOADS_IN_BACKGROUND, false) ||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.content.Context
|
||||
import android.os.storage.StorageManager
|
||||
import com.droiddeck.launcher.frontend.AddedGames
|
||||
import java.io.File
|
||||
|
||||
object UsbLink {
|
||||
data class Link(val device: String, val mbps: Double, val name: String, val portMbps: Double?, val plug: String = "")
|
||||
|
||||
class SlowDrive(val key: String, val name: String, val mbps: Double, val portMbps: Double?) {
|
||||
val portLimited get() = portMbps != null && portMbps < FAST_MBPS
|
||||
}
|
||||
|
||||
const val FAST_MBPS = 5000.0
|
||||
|
||||
fun markShown(context: Context, drive: SlowDrive) = synchronized(this) {
|
||||
SessionPrefs.setSlowUsbDismissed(context, SessionPrefs.slowUsbDismissed(context) + drive.key)
|
||||
}
|
||||
|
||||
fun stillDue(context: Context, drive: SlowDrive) =
|
||||
!SessionPrefs.slowUsbWarningOff(context) && drive.key !in SessionPrefs.slowUsbDismissed(context)
|
||||
|
||||
fun slowLibrary(context: Context): SlowDrive? {
|
||||
if (SessionPrefs.slowUsbWarningOff(context)) return null
|
||||
val mounts = runCatching { File("/proc/self/mounts").readText() }.getOrNull() ?: return null
|
||||
val sm = context.getSystemService(StorageManager::class.java)
|
||||
val boot = runCatching { android.provider.Settings.Global.getInt(context.contentResolver, android.provider.Settings.Global.BOOT_COUNT) }.getOrDefault(0)
|
||||
val drives = libraries(context).mapNotNull { path ->
|
||||
val link = forPath(path, mounts) ?: return@mapNotNull null
|
||||
if (link.mbps >= FAST_MBPS) return@mapNotNull null
|
||||
val volume = runCatching { sm?.getStorageVolume(File(path))?.getDescription(context) }.getOrNull()
|
||||
SlowDrive(dismissKey(boot, link), link.name.ifBlank { volume.orEmpty() }.ifBlank { File(path).name }, link.mbps, link.portMbps)
|
||||
}
|
||||
return synchronized(this) {
|
||||
val dismissed = SessionPrefs.slowUsbDismissed(context)
|
||||
val current = dismissed.intersect(drives.map { it.key }.toSet())
|
||||
if (current != dismissed) SessionPrefs.setSlowUsbDismissed(context, current)
|
||||
drives.firstOrNull { it.key !in current }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun dismissKey(boot: Int, link: Link) = "$boot/${link.plug}/${link.device}@${link.mbps}"
|
||||
|
||||
private fun libraries(context: Context): List<String> {
|
||||
val pref = SessionPrefs.gameStorage(context)
|
||||
val storage = when (pref) {
|
||||
SessionPrefs.GAME_STORAGE_OFF -> null
|
||||
"" -> GameStorage.effective(context)?.path
|
||||
else -> pref
|
||||
}
|
||||
val folders = runCatching { AddedGames.roots(context).map { it.host.absolutePath } }.getOrDefault(emptyList())
|
||||
return (listOfNotNull(storage) + folders).map(::storagePath).filter { external(it) }.distinct()
|
||||
}
|
||||
|
||||
internal fun storagePath(path: String) = if (path.startsWith("/mnt/media_rw/")) "/storage/" + path.removePrefix("/mnt/media_rw/") else path
|
||||
|
||||
internal fun external(path: String) = path.startsWith("/storage/") && !path.startsWith("/storage/emulated/") && !path.startsWith("/storage/self/")
|
||||
|
||||
fun forPath(path: String, mounts: String, sys: File = File("/sys")): Link? {
|
||||
val device = mountDevice(rawPath(path), mounts) ?: return null
|
||||
val block = blockDir(device, sys) ?: return null
|
||||
val usb = usbDevice(block, sys) ?: return null
|
||||
val mbps = readSpeed(usb) ?: return null
|
||||
val name = listOf("manufacturer", "product").mapNotNull { read(File(usb, it)) }.joinToString(" ").trim()
|
||||
val plug = listOf("busnum", "devnum").mapNotNull { read(File(usb, it)) }.joinToString("-")
|
||||
return Link(device, mbps, name, portSpeed(usb, sys), plug)
|
||||
}
|
||||
|
||||
internal fun rawPath(path: String) = if (external(path)) "/mnt/media_rw/" + path.removePrefix("/storage/") else path
|
||||
|
||||
internal fun mountDevice(path: String, mounts: String): String? {
|
||||
var best: Pair<String, String>? = null
|
||||
for (line in mounts.lineSequence()) {
|
||||
val parts = line.split(' ')
|
||||
if (parts.size < 2) continue
|
||||
val point = parts[1].replace("\\040", " ")
|
||||
if (path != point && !path.startsWith("${point.trimEnd('/')}/")) continue
|
||||
if (best == null || point.length > best.first.length) best = point to parts[0]
|
||||
}
|
||||
val source = best?.second ?: return null
|
||||
Regex(":(\\d+),(\\d+)$").find(source)?.let { return "${it.groupValues[1]}:${it.groupValues[2]}" }
|
||||
return if (source.startsWith("/dev/block/") || source.startsWith("/dev/sd")) "name:" + source.substringAfterLast('/') else null
|
||||
}
|
||||
|
||||
internal fun blockDir(device: String, sys: File): File? {
|
||||
if (device.startsWith("name:")) return File(sys, "class/block/${device.removePrefix("name:")}").takeIf { it.exists() }?.canonicalFile
|
||||
File(sys, "dev/block/$device").takeIf { it.exists() }?.let { return it.canonicalFile }
|
||||
return File(sys, "class/block").listFiles()?.firstOrNull { read(File(it, "dev")) == device }?.canonicalFile
|
||||
}
|
||||
|
||||
internal fun usbDevice(block: File, sys: File): File? {
|
||||
val top = sys.canonicalFile
|
||||
var dir: File? = block
|
||||
while (dir != null && dir != top) {
|
||||
if (File(dir, "speed").isFile && File(dir, "idVendor").isFile) return dir
|
||||
dir = dir.parentFile
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun portSpeed(usb: File, sys: File): Double? {
|
||||
val bus = read(File(usb, "busnum")) ?: return null
|
||||
val hub = File(sys, "bus/usb/devices/usb$bus").takeIf { it.exists() }?.canonicalFile ?: return null
|
||||
val controller = hub.parentFile ?: return null
|
||||
return controller.listFiles { f -> f.name.matches(Regex("usb\\d+")) }?.mapNotNull { readSpeed(it) }?.maxOrNull()
|
||||
}
|
||||
|
||||
private fun readSpeed(dir: File) = read(File(dir, "speed"))?.toDoubleOrNull()
|
||||
|
||||
private fun read(file: File) = runCatching { file.readText().trim() }.getOrNull()?.takeIf { it.isNotEmpty() }
|
||||
|
||||
fun generation(mbps: Double) = when {
|
||||
mbps <= 12 -> "USB 1.1"
|
||||
mbps < FAST_MBPS -> "USB 2.0"
|
||||
mbps < 10000 -> "USB 3.2 Gen 1"
|
||||
mbps < 20000 -> "USB 3.2 Gen 2"
|
||||
else -> "USB 3.2 Gen 2x2"
|
||||
}
|
||||
|
||||
fun megabytesPerSecond(mbps: Double) = (mbps / 8 * 0.7).toInt()
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.hoverable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Usb
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.focus.focusProperties
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.session.UsbLink
|
||||
|
||||
@Composable
|
||||
internal fun SlowUsbDialog(drive: UsbLink.SlowDrive, never: Boolean, onNever: (Boolean) -> Unit, onDismiss: () -> Unit) {
|
||||
val shown = rememberShown(onDismiss)
|
||||
val close = { shown.targetState = false }
|
||||
val focus = remember { FocusRequester() }
|
||||
LaunchedEffect(Unit) { runCatching { focus.requestFocus() } }
|
||||
val speed = if (drive.mbps % 1.0 == 0.0) drive.mbps.toLong().toString() else drive.mbps.toString()
|
||||
val message = if (drive.portLimited) R.string.slow_usb_port else R.string.slow_usb_cable
|
||||
AppDialog(shown, close, "slowUsb", wide = false, maxWidth = 520.dp) {
|
||||
DialogTitleRow(stringResource(R.string.slow_usb_title), close) {
|
||||
Icon(Icons.Filled.Usb, null, tint = LocalPalette.current.signal, modifier = Modifier.size(20.dp))
|
||||
}
|
||||
Text(
|
||||
stringResource(message, drive.name, UsbLink.generation(drive.mbps), speed, UsbLink.megabytesPerSecond(drive.mbps)),
|
||||
fontSize = 13.sp, lineHeight = 18.sp, color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
val flip = { onNever(!never) }
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.fillMaxWidth().clip(Shape12)
|
||||
.background(if (hot) pal.signal.copy(alpha = 0.12f) else Color.Transparent)
|
||||
.border(if (hot) 2.dp else 1.dp, if (hot) pal.signal else pal.line, Shape12)
|
||||
.hoverable(src)
|
||||
.clickable(interactionSource = src, indication = null, role = Role.Switch, onClick = flip)
|
||||
.controllerConfirm(onClick = flip)
|
||||
.padding(start = 14.dp, end = 4.dp),
|
||||
) {
|
||||
Text(stringResource(R.string.slow_usb_never), fontSize = 14.sp, color = MaterialTheme.colorScheme.onBackground, modifier = Modifier.weight(1f))
|
||||
ToggleSwitch(never, label = stringResource(R.string.slow_usb_never), modifier = Modifier.focusProperties { canFocus = false }) { onNever(it) }
|
||||
}
|
||||
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
|
||||
PrimaryButton(stringResource(R.string.slow_usb_dismiss), modifier = Modifier.focusRequester(focus), onClick = close)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user