]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/activities/LogcatActivity.kt
Bump target API to 33. https://redmine.stoutner.com/issues/890
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / activities / LogcatActivity.kt
index ca396c233c010dadecc5d8721b9eee28b3982a03..e7ba180168a2c39cc54715aef9197f393e31c9cb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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>.
  *
@@ -40,6 +40,11 @@ import com.google.android.material.snackbar.Snackbar
 
 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
@@ -59,7 +64,7 @@ class LogcatActivity : AppCompatActivity() {
     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 {
@@ -69,11 +74,16 @@ class LogcatActivity : AppCompatActivity() {
                 // 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)!!
@@ -260,4 +270,4 @@ class LogcatActivity : AppCompatActivity() {
         // Stop the swipe to refresh animation if it is displayed.
         swipeRefreshLayout.isRefreshing = false
     }
-}
\ No newline at end of file
+}