X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FLogcatActivity.kt;h=ca396c233c010dadecc5d8721b9eee28b3982a03;hp=122ccc544364f1fe2b69108bb35c5d34f31fb57e;hb=HEAD;hpb=45b64d4cf242b28abd6747681086f0a080fca009 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 122ccc5..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 @@ -58,8 +63,8 @@ class LogcatActivity : AppCompatActivity() { private lateinit var logcatScrollView: ScrollView private lateinit var logcatTextView: TextView - // Define the save logcat activity result launcher. - private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument()) { fileNameUri: Uri? -> + // 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("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)!! @@ -82,7 +92,7 @@ class LogcatActivity : AppCompatActivity() { contentResolverCursor.moveToFirst() // Get the file name from the cursor. - val fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + val fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)) // Close the cursor. contentResolverCursor.close() @@ -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 +}