From 7a5319b78a64a9c878c0b36e56f5844caad875be Mon Sep 17 00:00:00 2001 From: neubauerm Date: Wed, 14 Aug 2024 16:34:24 +0200 Subject: [PATCH] fix: PIN-6554 do not throw un-catchable exception if there are no log files, just return an empty zip. --- kotlinlog/build.gradle | 2 +- .../kotlinlog/file/SendLogDialogFragment.kt | 29 ++++++++++--------- .../quanti/com/kotlinlog/utils/FileUtils.kt | 4 --- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/kotlinlog/build.gradle b/kotlinlog/build.gradle index c2054de..683fd40 100644 --- a/kotlinlog/build.gradle +++ b/kotlinlog/build.gradle @@ -12,7 +12,7 @@ android { minSdk 21 targetSdk 34 versionCode 8 - versionName "2.2.16" + versionName "2.2.17" buildConfigField 'int', 'VERSION_CODE', "$versionCode" buildConfigField 'String', 'VERSION_NAME', "\"$versionName\"" consumerProguardFile('proguard-rules.pro') diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/SendLogDialogFragment.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/SendLogDialogFragment.kt index 8d09280..8afab60 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/SendLogDialogFragment.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/SendLogDialogFragment.kt @@ -4,6 +4,7 @@ import android.app.AlertDialog import android.app.Dialog import android.content.DialogInterface import android.content.Intent +import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.fragment.app.DialogFragment @@ -100,17 +101,6 @@ class SendLogDialogFragment : DialogFragment() { } } - private var zipFile: Deferred? = null - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - zipFile = CoroutineScope(Dispatchers.IO).async { - val extraFiles = requireArguments().getSerializable(EXTRA_FILES) as ArrayList - val maxFileAge = requireArguments().getInt(MAX_FILE_AGE) - getZipOfLogs(requireActivity().applicationContext, maxFileAge, extraFiles) - } - } - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val hasFilePermission = requireActivity().applicationContext.hasFileWritePermission() @@ -146,7 +136,7 @@ class SendLogDialogFragment : DialogFragment() { val subject = getString(R.string.logs_email_subject) + " " + getFormattedFileNameDayNow() val bodyText = getString(R.string.logs_email_text) - val zipFileUri = zipFile?.await()?.getUriForFile(appContext) + val zipFileUri = getZipFileDeferred().await().getUriForFile(appContext) val intent = Intent(Intent.ACTION_SEND).apply { type = "message/rfc822" // email @@ -178,7 +168,7 @@ class SendLogDialogFragment : DialogFragment() { val appContext = this@SendLogDialogFragment.requireContext().applicationContext val destinationDir = requireArguments().getString(SAVE_LOGS_DIR_NAME) - val resultPath = zipFile?.await()?.copyLogsToSDCard(requireContext(), destinationDir ?: DEFAULT_SAVE_LOGS_DIR_NAME) + val resultPath = getZipFileDeferred().await().copyLogsToSDCard(requireContext(), destinationDir ?: DEFAULT_SAVE_LOGS_DIR_NAME) val text = if (resultPath == null) { "File copy failed" @@ -192,4 +182,17 @@ class SendLogDialogFragment : DialogFragment() { Toast.LENGTH_LONG ).show() } + + private fun getZipFileDeferred(): Deferred { + return CoroutineScope(Dispatchers.IO).async { + val extraFiles = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + requireArguments().getSerializable(EXTRA_FILES, ArrayList::class.java) + } else { + @Suppress("DEPRECATION") + requireArguments().getSerializable(EXTRA_FILES) + } as ArrayList + val maxFileAge = requireArguments().getInt(MAX_FILE_AGE) + getZipOfLogs(requireActivity().applicationContext, maxFileAge, extraFiles) + } + } } diff --git a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/utils/FileUtils.kt b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/utils/FileUtils.kt index fce47ed..65c3aa7 100644 --- a/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/utils/FileUtils.kt +++ b/kotlinlog/src/main/kotlin/quanti/com/kotlinlog/utils/FileUtils.kt @@ -194,10 +194,6 @@ fun getLogFiles(appCtx: Context, fileAge: Int = 4): List { appCtx.logFilesDir.listFiles()?.deleteAllZips() appCtx.logFilesDir.listFiles()?.deleteAllOldFiles(fileAge) - if (appCtx.logFilesDir.listFiles().isNullOrEmpty()) { - throw FileNotFoundException("No files were found") - } - //create metadata info file MetadataFile(appCtx).apply { write()