From 721a3daeb62372efd8bf0dddcbc2298038a80e1d Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 8 Apr 2024 19:35:38 +0200 Subject: [PATCH] fix: Properly handle errors when starting recording fails Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com> --- .../myzel394/alibi/helpers/BatchesFolder.kt | 39 ++++++++++++------- .../alibi/services/IntervalRecorderService.kt | 2 + .../alibi/services/RecorderService.kt | 18 +++++++-- .../organisms/RecorderEventsHandler.kt | 13 +++---- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt index 7ec8536..db4f0ad 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt @@ -441,23 +441,28 @@ abstract class BatchesFolder( } fun checkIfFolderIsAccessible(): Boolean { - return when (type) { - BatchType.INTERNAL -> true - BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead() - BatchType.MEDIA -> { - if (SUPPORTS_SCOPED_STORAGE) { - return true - } + try { + return when (type) { + BatchType.INTERNAL -> true + BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead() + BatchType.MEDIA -> { + if (SUPPORTS_SCOPED_STORAGE) { + return true + } - return PermissionHelper.hasGranted( - context, - Manifest.permission.READ_EXTERNAL_STORAGE - ) && - PermissionHelper.hasGranted( - context, - Manifest.permission.WRITE_EXTERNAL_STORAGE - ) + return PermissionHelper.hasGranted( + context, + Manifest.permission.READ_EXTERNAL_STORAGE + ) && + PermissionHelper.hasGranted( + context, + Manifest.permission.WRITE_EXTERNAL_STORAGE + ) + } } + } catch (error: NullPointerException) { + error.printStackTrace() + return false } } @@ -581,6 +586,10 @@ abstract class BatchesFolder( MEDIA, } + class InaccessibleError : RuntimeException() { + + } + companion object { fun requiredBytesForOneMinuteOfRecording(appSettings: AppSettings): Long { // 350 MiB sounds like a good default diff --git a/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt b/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt index 6783ef2..89e21f4 100644 --- a/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt +++ b/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt @@ -65,6 +65,8 @@ abstract class IntervalRecorderService : if (!batchesFolder.checkIfFolderIsAccessible()) { onBatchesFolderNotAccessible() + + throw AvoidErrorDialogError() } createTimer() diff --git a/app/src/main/java/app/myzel394/alibi/services/RecorderService.kt b/app/src/main/java/app/myzel394/alibi/services/RecorderService.kt index fa9f2a8..a49c60d 100644 --- a/app/src/main/java/app/myzel394/alibi/services/RecorderService.kt +++ b/app/src/main/java/app/myzel394/alibi/services/RecorderService.kt @@ -1,13 +1,11 @@ package app.myzel394.alibi.services import android.annotation.SuppressLint -import android.app.ActivityManager import android.app.Notification import android.content.Intent import android.os.Binder import android.os.IBinder import androidx.core.app.NotificationManagerCompat -import androidx.core.content.ContextCompat.getSystemService import androidx.lifecycle.LifecycleService import app.myzel394.alibi.NotificationHelper import app.myzel394.alibi.enums.RecorderState @@ -63,7 +61,16 @@ abstract class RecorderService : LifecycleService() { startForegroundService() changeState(RecorderState.RECORDING) - start() + + try { + start() + } catch (error: RuntimeException) { + error.printStackTrace() + + if (error !is AvoidErrorDialogError) { + onError() + } + } } suspend fun stopRecording() { @@ -194,4 +201,9 @@ abstract class RecorderService : LifecycleService() { } } } + + + // Throw this error if you show a dialog yourself. + // This will prevent the service from showing their generic error dialog. + class AvoidErrorDialogError : RuntimeException() } \ No newline at end of file diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt b/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt index 99a43d7..ce137e9 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt @@ -356,17 +356,16 @@ fun RecorderEventsHandler( progress = processingProgress, ) - if (showRecorderError) - RecorderErrorDialog( - onClose = { - showRecorderError = false - }, - ) - if (showBatchesInaccessibleError) BatchesInaccessibleDialog( onClose = { showBatchesInaccessibleError = false }, ) + else if (showRecorderError) + RecorderErrorDialog( + onClose = { + showRecorderError = false + }, + ) } \ No newline at end of file