"Plugged in" follows USB modems plugged in and out while it's on screen

The home card and the line settings looked once, when shown: now they
follow Android's attached/detached broadcasts (one just detached left
out, as Android may still list it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
Claude committed 2026-10-26 11:55:27 +00:00
1 parent 482fef123c
commit 7f28b05ef7
5 files changed
+120 -9

No files matched your search

@@ -43,14 +43,14 @@ object UsbModems {
fun kindOf(d: UsbDevice): Kind? = kindOf(d.vendorId, d.productId)
/** the ones plugged in, and which each is */
fun plugged(context: Context): List<Pair<UsbDevice, Kind>> {
/** the ones plugged in, and which each is; not [gone] (the device name of one just unplugged, which Android may still list) */
fun plugged(context: Context, gone: String? = null): List<Pair<UsbDevice, Kind>> {
val m = context.getSystemService(UsbManager::class.java) ?: return emptyList()
return m.deviceList.values.mapNotNull { d -> kindOf(d)?.let { d to it } }
return m.deviceList.values.filter { it.deviceName != gone }.mapNotNull { d -> kindOf(d)?.let { d to it } }
}
/** what the line setting says of what's plugged in */
fun describe(context: Context): String = plugged(context).let { p ->
/** what the line setting says of what's plugged in (see [plugged]) */
fun describe(context: Context, gone: String? = null): String = plugged(context, gone).let { p ->
if (p.isEmpty()) "none" else p.joinToString(", ") { it.second.label }
}
@@ -308,9 +308,8 @@ private fun LineCard(s: ModemSettings, run: ModemState.Run, network: String?, on
return@Column
}
if (s.usb) {
val context = androidx.compose.ui.platform.LocalContext.current
InfoRow("Via", "a phone line, through a USB modem")
InfoRow("Plugged in", remember(run) { com.neodesktop.dmodem.modem.UsbModems.describe(context) })
InfoRow("Plugged in", rememberUsbModemsPlugged())
if (run == ModemState.Run.RUNNING) {
Text("Calls ring in the terminal (RING); ATS0=1 answers them", style = MaterialTheme.typography.bodySmall)
}
@@ -98,8 +98,7 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
@Composable
fun UsbLineFields() {
val context = LocalContext.current
// (looked at again when the screen is: plugging one in brings the app up)
val plugged = remember { com.neodesktop.dmodem.modem.UsbModems.describe(context) }
val plugged = rememberUsbModemsPlugged()
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(
"The modem dials and answers a real phone line through a USB modem: a Smart Link SmartUSB56, a " +
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
package com.neodesktop.dmodem.ui
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import com.neodesktop.dmodem.modem.UsbModems
/** which USB modems are plugged in ("none", "Agere USB 2.0 modem"), kept up to date as they're plugged in and out while it's shown */
@Composable
fun rememberUsbModemsPlugged(): String {
val context = LocalContext.current
var plugged by remember { mutableStateOf(UsbModems.describe(context)) }
DisposableEffect(context) {
val r = object : BroadcastReceiver() {
override fun onReceive(c: Context, i: Intent) {
@Suppress("DEPRECATION")
val d = i.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
val gone = if (i.action == UsbManager.ACTION_USB_DEVICE_DETACHED) d?.deviceName else null
plugged = UsbModems.describe(context, gone)
}
}
val f = IntentFilter().apply {
addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
}
// (the system's broadcasts)
ContextCompat.registerReceiver(context, r, f, ContextCompat.RECEIVER_EXPORTED)
// (and anything plugged in or out between the first look and now)
plugged = UsbModems.describe(context)
onDispose { runCatching { context.unregisterReceiver(r) } }
}
return plugged
}
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
package com.neodesktop.dmodem.ui
import android.content.Intent
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import androidx.compose.material3.Text
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.app.ApplicationProvider
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers
import org.robolectric.util.ReflectionHelpers.ClassParameter
/** "Plugged in" follows a USB modem plugged in and out while it's on screen */
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [35])
class UsbPluggedTest {
@get:Rule
val compose = createComposeRule()
/** an Agere USB 2.0 modem's device (047e:2892) at [name] */
private fun agere(name: String): UsbDevice {
val s = String::class.java
val i = Int::class.javaPrimitiveType
val b = ReflectionHelpers.callConstructor(
Class.forName("android.hardware.usb.UsbDevice\$Builder"),
ClassParameter(s, name), ClassParameter(i, 0x047E), ClassParameter(i, 0x2892),
ClassParameter(i, 0xFF), ClassParameter(i, 0), ClassParameter(i, 0),
ClassParameter(s, "Agere"), ClassParameter(s, "USB modem"), ClassParameter(s, "1.0"),
ClassParameter(Array<android.hardware.usb.UsbConfiguration>::class.java, emptyArray<android.hardware.usb.UsbConfiguration>()),
ClassParameter(s, "1"),
ClassParameter(Boolean::class.javaPrimitiveType, false), ClassParameter(Boolean::class.javaPrimitiveType, false),
ClassParameter(Boolean::class.javaPrimitiveType, false), ClassParameter(Boolean::class.javaPrimitiveType, false),
ClassParameter(Boolean::class.javaPrimitiveType, false),
)
// (its serial number's reader: an interface of Android's own, answered with nothing)
val reader = Class.forName("android.hardware.usb.IUsbSerialReader")
val none = java.lang.reflect.Proxy.newProxyInstance(reader.classLoader, arrayOf(reader)) { _, _, _ -> null }
return ReflectionHelpers.callInstanceMethod(b, "build", ClassParameter(reader, none))
}
@Test
fun followsPlugAndUnplug() {
val context = ApplicationProvider.getApplicationContext<android.content.Context>()
val usb = shadowOf(context.getSystemService(UsbManager::class.java))
compose.setContent { Text("Plugged in: " + rememberUsbModemsPlugged()) }
compose.onNodeWithText("Plugged in: none").assertExists()
val d = agere("/dev/bus/usb/001/002")
usb.addOrUpdateUsbDevice(d, true)
context.sendBroadcast(Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED).putExtra(UsbManager.EXTRA_DEVICE, d))
compose.waitForIdle()
compose.onNodeWithText("Plugged in: Agere USB 2.0 modem").assertExists()
// (Android may still list it as it says it's gone)
context.sendBroadcast(Intent(UsbManager.ACTION_USB_DEVICE_DETACHED).putExtra(UsbManager.EXTRA_DEVICE, d))
compose.waitForIdle()
compose.onNodeWithText("Plugged in: none").assertExists()
}
}