Skip to content

Commit

Permalink
fix: PIN-6554 do not throw un-catchable exception if there are no log…
Browse files Browse the repository at this point in the history
… files, just return an empty zip.
  • Loading branch information
neubauerm committed Aug 14, 2024
1 parent 5508bfa commit 7a5319b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion kotlinlog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,17 +101,6 @@ class SendLogDialogFragment : DialogFragment() {
}
}

private var zipFile: Deferred<File>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
zipFile = CoroutineScope(Dispatchers.IO).async {
val extraFiles = requireArguments().getSerializable(EXTRA_FILES) as ArrayList<File>
val maxFileAge = requireArguments().getInt(MAX_FILE_AGE)
getZipOfLogs(requireActivity().applicationContext, maxFileAge, extraFiles)
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val hasFilePermission = requireActivity().applicationContext.hasFileWritePermission()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -192,4 +182,17 @@ class SendLogDialogFragment : DialogFragment() {
Toast.LENGTH_LONG
).show()
}

private fun getZipFileDeferred(): Deferred<File> {
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<File>
val maxFileAge = requireArguments().getInt(MAX_FILE_AGE)
getZipOfLogs(requireActivity().applicationContext, maxFileAge, extraFiles)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ fun getLogFiles(appCtx: Context, fileAge: Int = 4): List<File> {
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()
Expand Down

0 comments on commit 7a5319b

Please sign in to comment.