LogViewerActivity: simplify scoping

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-09-16 16:16:54 +02:00
parent 746ab00794
commit eebeece856

View File

@ -147,7 +147,8 @@ class LogViewerActivity : AppCompatActivity() {
true true
} }
R.id.save_log -> { R.id.save_log -> {
lifecycleScope.launch(Dispatchers.IO) { saveLog() } saveButton?.isEnabled = false
lifecycleScope.launch { saveLog() }
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
@ -155,21 +156,19 @@ class LogViewerActivity : AppCompatActivity() {
} }
private suspend fun saveLog() { private suspend fun saveLog() {
withContext(Dispatchers.Main.immediate) {
saveButton?.isEnabled = false
withContext(Dispatchers.IO) {
var exception: Throwable? = null var exception: Throwable? = null
var outputFile: DownloadsFileSaver.DownloadsFile? = null var outputFile: DownloadsFileSaver.DownloadsFile? = null
withContext(Dispatchers.IO) {
try { try {
outputFile = DownloadsFileSaver.save(this@LogViewerActivity, "wireguard-log.txt", "text/plain", true) outputFile = DownloadsFileSaver.save(this@LogViewerActivity, "wireguard-log.txt", "text/plain", true)
outputFile.outputStream.use { outputFile?.outputStream.use {
it.write(rawLogLines.toString().toByteArray(Charsets.UTF_8)) it?.write(rawLogLines.toString().toByteArray(Charsets.UTF_8))
} }
} catch (e: Throwable) { } catch (e: Throwable) {
outputFile?.delete() outputFile?.delete()
exception = e exception = e
} }
withContext(Dispatchers.Main.immediate) { }
saveButton?.isEnabled = true saveButton?.isEnabled = true
Snackbar.make(findViewById(android.R.id.content), Snackbar.make(findViewById(android.R.id.content),
if (exception == null) getString(R.string.log_export_success, outputFile?.fileName) if (exception == null) getString(R.string.log_export_success, outputFile?.fileName)
@ -178,9 +177,6 @@ class LogViewerActivity : AppCompatActivity() {
.setAnchorView(binding.shareFab) .setAnchorView(binding.shareFab)
.show() .show()
} }
}
}
}
private suspend fun streamingLog() = withContext(Dispatchers.IO) { private suspend fun streamingLog() = withContext(Dispatchers.IO) {
val builder = ProcessBuilder().command("logcat", "-b", "all", "-v", "threadtime", "*:V") val builder = ProcessBuilder().command("logcat", "-b", "all", "-v", "threadtime", "*:V")