diff --git a/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt b/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt index 8091a56..439713e 100644 --- a/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt +++ b/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt @@ -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.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 diff --git a/app/src/main/java/com/neodesktop/dmodem/ui/LogScreen.kt b/app/src/main/java/com/neodesktop/dmodem/ui/LogScreen.kt index 937f762..ff7862c 100644 --- a/app/src/main/java/com/neodesktop/dmodem/ui/LogScreen.kt +++ b/app/src/main/java/com/neodesktop/dmodem/ui/LogScreen.kt @@ -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>(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 = if (lines.isEmpty()) "" else lines.joinToString("\n", postfix = "\n") + +/** the name a saved log is offered: dmodem--