/*
- * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2021-2022 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
*
import com.stoutner.privacycell.R
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
private lateinit var logcatTextView: TextView
// Define the save logcat activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
- private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument()) { fileNameUri: Uri? ->
+ private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { fileNameUri: Uri? ->
// Only save the file if the URI is not null, which happens if the user exited the file picker by pressing back.
if (fileNameUri != null) {
try {
// Open an output stream.
val outputStream = contentResolver.openOutputStream(fileNameUri)!!
- // Write the logcat string to the output stream.
- outputStream.write(logcatString.toByteArray(StandardCharsets.UTF_8))
+ // Save the logcat using a coroutine with Dispatchers.IO.
+ CoroutineScope(Dispatchers.Main).launch {
+ withContext(Dispatchers.IO) {
+ // Write the logcat string to the output stream.
+ outputStream.write(logcatString.toByteArray(StandardCharsets.UTF_8))
- // Close the output stream.
- outputStream.close()
+ // Close the output stream.
+ outputStream.close()
+ }
+ }
// Get a cursor from the content resolver.
val contentResolverCursor = contentResolver.query(fileNameUri, null, null, null)!!
// Stop the swipe to refresh animation if it is displayed.
swipeRefreshLayout.isRefreshing = false
}
-}
\ No newline at end of file
+}