fix: Properly handle errors when starting recording fails

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-04-08 19:35:38 +02:00
parent df9443eb9b
commit 721a3daeb6
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 47 additions and 25 deletions

View File

@ -441,6 +441,7 @@ abstract class BatchesFolder(
}
fun checkIfFolderIsAccessible(): Boolean {
try {
return when (type) {
BatchType.INTERNAL -> true
BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead()
@ -459,6 +460,10 @@ abstract class BatchesFolder(
)
}
}
} catch (error: NullPointerException) {
error.printStackTrace()
return false
}
}
fun asInternalGetFile(counter: Long, fileExtension: String): File {
@ -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

View File

@ -65,6 +65,8 @@ abstract class IntervalRecorderService<I, B : BatchesFolder> :
if (!batchesFolder.checkIfFolderIsAccessible()) {
onBatchesFolderNotAccessible()
throw AvoidErrorDialogError()
}
createTimer()

View File

@ -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)
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()
}

View File

@ -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
},
)
}