The log saved to a file; a USB modem leaving the bus logged

Log: Save, beside Clear and Copy: the log's lines to a file the user
picks (the system's picker), dmodem-<date>-<time>.log offered.

A USB modem as the line: Android's word when it goes from the bus (or
comes back) in the log, so a modem that resets (an Agere dropping off
USB as it goes off hook: LIBUSB_ERROR_NO_DEVICE, a new address after)
shows as such.

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 03:47:17 +00:00
1 parent f93ac19dbc
commit 9e2cd1cf60
3 files changed
+77 -1

No files matched your search

@@ -266,6 +266,7 @@ class ModemService : Service() {
?: com.neodesktop.dmodem.tones.CountryTones.find("")!!
watchProgress()
ModemState.line = settings.line
if (settings.usb) watchUsb()
// (the speaker's sound: one socket for the process, read for good once started)
NativeModem.speakerSound()?.let { s ->
speakerSocket = s
@@ -322,6 +323,33 @@ class ModemService : Service() {
}
}
/** a USB modem as the line: Android's word when it goes from the bus, or comes back (a reset shows as both) */
private var usbWatch: android.content.BroadcastReceiver? = null
private fun watchUsb() {
if (usbWatch != null) return
val r = object : android.content.BroadcastReceiver() {
override fun onReceive(c: Context, i: Intent) {
@Suppress("DEPRECATION")
val d = i.getParcelableExtra<android.hardware.usb.UsbDevice>(android.hardware.usb.UsbManager.EXTRA_DEVICE) ?: return
val kind = UsbModems.kindOf(d) ?: return
val gone = i.action == android.hardware.usb.UsbManager.ACTION_USB_DEVICE_DETACHED
log("USB: the ${kind.label} " + if (gone) "has gone from the bus (unplugged, or it reset)" else "is on the bus again")
}
}
val f = android.content.IntentFilter().apply {
addAction(android.hardware.usb.UsbManager.ACTION_USB_DEVICE_DETACHED)
addAction(android.hardware.usb.UsbManager.ACTION_USB_DEVICE_ATTACHED)
}
androidx.core.content.ContextCompat.registerReceiver(this, r, f, androidx.core.content.ContextCompat.RECEIVER_EXPORTED)
usbWatch = r
}
private fun unwatchUsb() {
usbWatch?.let { runCatching { unregisterReceiver(it) } }
usbWatch = null
}
/** the native modem stopped: the call hung up, SIP gone, the DTE's socket closed */
private fun stopNative() {
stopping = true
@@ -334,6 +362,7 @@ class ModemService : Service() {
ModemState.speaker.setLevel(0)
com.neodesktop.dmodem.peer.Peers.stop()
NativeModem.stop()
unwatchUsb()
// (the device let go once the modem has)
usbModem?.connection?.close()
usbModem = null
@@ -18,6 +18,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
@@ -26,10 +27,26 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.neodesktop.dmodem.modem.ModemState
/** The modem's and SIP's log, the last lines (their levels in the settings). */
/** The modem's and SIP's log, the last lines (their levels in the settings): cleared, copied, or saved to a file. */
@Composable
fun LogScreen(modifier: Modifier = Modifier) {
val lines by ModemState.log.flow.collectAsStateWithLifecycle()
val context = androidx.compose.ui.platform.LocalContext.current
// (where it's saved: the user's pick, the system's file picker; the lines as they were when Save was pressed)
var toSave by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf<List<String>>(emptyList()) }
val save = androidx.activity.compose.rememberLauncherForActivityResult(
androidx.activity.result.contract.ActivityResultContracts.CreateDocument("text/plain"),
) { uri ->
if (uri == null) return@rememberLauncherForActivityResult
val ok = runCatching {
context.contentResolver.openOutputStream(uri)?.use { it.write(logText(toSave).toByteArray()) } ?: error("can't open it")
}
android.widget.Toast.makeText(
context,
ok.fold({ "Log saved" }, { "The log wasn't saved: ${it.message}" }),
android.widget.Toast.LENGTH_SHORT,
).show()
}
val state = rememberLazyListState()
LaunchedEffect(lines.size) {
if (lines.isNotEmpty()) state.scrollToItem(lines.size - 1)
@@ -42,6 +59,10 @@ fun LogScreen(modifier: Modifier = Modifier) {
OutlinedButton(onClick = { clipboard.setText(AnnotatedString(lines.joinToString("\n"))) }, enabled = lines.isNotEmpty()) {
Text("Copy")
}
OutlinedButton(onClick = {
toSave = lines
save.launch(logFileName())
}, enabled = lines.isNotEmpty()) { Text("Save") }
}
SelectionContainer(Modifier.fillMaxWidth().weight(1f)) {
LazyColumn(state = state, modifier = Modifier.fillMaxSize()) {
@@ -52,3 +73,11 @@ fun LogScreen(modifier: Modifier = Modifier) {
}
}
}
/** the log as a file's text: a line each, a newline at the end */
internal fun logText(lines: List<String>): String = if (lines.isEmpty()) "" else lines.joinToString("\n", postfix = "\n")
/** the name a saved log is offered: dmodem-<date>-<time>.log */
internal fun logFileName(now: java.util.Date = java.util.Date()): String =
"dmodem-" + java.text.SimpleDateFormat("yyyyMMdd-HHmmss", java.util.Locale.US).format(now) + ".log"
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
package com.neodesktop.dmodem.ui
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
/** The log saved: its text (a line each), the file's name offered. */
class LogScreenTest {
@Test
fun theFilesTextAndName() {
assertEquals("app: one\ndmodem0: two\n", logText(listOf("app: one", "dmodem0: two")))
assertEquals("", logText(emptyList()))
val name = logFileName(java.util.Date(0))
assertTrue(name, name.matches(Regex("dmodem-19(69|70)\\d{4}-\\d{6}\\.log")))
}
}