]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/LogcatActivity.kt
Fix possibly blocking OutputStream calls. https://redmine.stoutner.com/issues/914
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / LogcatActivity.kt
index 9d9d5d53cb220279a77e625dce42e148ca2efa22..25c2bfc6e3d3a1af9b47b0ba8bd5f482b6660be3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019-2022 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -43,6 +43,11 @@ import com.stoutner.privacybrowser.BuildConfig
 
 import com.stoutner.privacybrowser.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
@@ -72,11 +77,16 @@ class LogcatActivity : AppCompatActivity() {
                 // Open an output stream.
                 val outputStream = contentResolver.openOutputStream(fileUri)!!
 
-                // 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()
+                    }
+                }
 
                 // Initialize the file name string from the file URI last path segment.
                 var fileNameString = fileUri.lastPathSegment
@@ -275,4 +285,4 @@ class LogcatActivity : AppCompatActivity() {
         // Stop the swipe to refresh animation if it is displayed.
         swipeRefreshLayout.isRefreshing = false
     }
-}
\ No newline at end of file
+}