X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FLogcatActivity.kt;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FLogcatActivity.kt;h=e7ba180168a2c39cc54715aef9197f393e31c9cb;hp=ca396c233c010dadecc5d8721b9eee28b3982a03;hb=ff2f3992b7ecddb804a96322789bd35010529f43;hpb=ab7601a6d91b6035862bb47b1ac259855e3aba3b diff --git a/app/src/main/java/com/stoutner/privacycell/activities/LogcatActivity.kt b/app/src/main/java/com/stoutner/privacycell/activities/LogcatActivity.kt index ca396c2..e7ba180 100644 --- a/app/src/main/java/com/stoutner/privacycell/activities/LogcatActivity.kt +++ b/app/src/main/java/com/stoutner/privacycell/activities/LogcatActivity.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Soren Stoutner . + * Copyright 2021-2022 Soren Stoutner . * * This file is part of 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 +}