feat: Add custom save folder to AppSettings

This commit is contained in:
Myzel394 2023-10-30 12:38:39 +01:00
parent fd07d04502
commit 6605f44eec
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
2 changed files with 33 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package app.myzel394.alibi.db package app.myzel394.alibi.db
import android.content.Context
import android.media.MediaRecorder import android.media.MediaRecorder
import android.os.Build import android.os.Build
import app.myzel394.alibi.R import app.myzel394.alibi.R
import app.myzel394.alibi.helpers.AudioRecorderExporter
import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.ReturnCode import com.arthenica.ffmpegkit.ReturnCode
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -94,6 +96,7 @@ data class AudioRecorderSettings(
val encoder: Int? = null, val encoder: Int? = null,
val showAllMicrophones: Boolean = false, val showAllMicrophones: Boolean = false,
val deleteRecordingsImmediately: Boolean = false, val deleteRecordingsImmediately: Boolean = false,
val saveFolder: String? = null,
) { ) {
fun getOutputFormat(): Int { fun getOutputFormat(): Int {
if (outputFormat != null) { if (outputFormat != null) {
@ -161,6 +164,28 @@ data class AudioRecorderSettings(
else else
MediaRecorder.AudioEncoder.AMR_NB MediaRecorder.AudioEncoder.AMR_NB
fun getSaveFolder(context: Context): File {
val defaultFolder = AudioRecorderExporter.getFolder(context)
if (saveFolder == null) {
return defaultFolder
}
runCatching {
return File(saveFolder!!).apply {
if (!AudioRecorderExporter.canFolderBeUsed(this)) {
throw SecurityException("Can't write to folder")
}
if (!exists()) {
mkdirs()
}
}
}
return defaultFolder
}
fun setIntervalDuration(duration: Long): AudioRecorderSettings { fun setIntervalDuration(duration: Long): AudioRecorderSettings {
if (duration < 10 * 1000L || duration > 60 * 60 * 1000L) { if (duration < 10 * 1000L || duration > 60 * 60 * 1000L) {
throw Exception("Interval duration must be between 10 seconds and 1 hour") throw Exception("Interval duration must be between 10 seconds and 1 hour")
@ -229,6 +254,10 @@ data class AudioRecorderSettings(
return copy(deleteRecordingsImmediately = deleteRecordingsImmediately) return copy(deleteRecordingsImmediately = deleteRecordingsImmediately)
} }
fun setSaveFolder(saveFolder: String?): AudioRecorderSettings {
return copy(saveFolder = saveFolder)
}
fun isEncoderCompatible(encoder: Int): Boolean { fun isEncoderCompatible(encoder: Int): Boolean {
if (outputFormat == null || outputFormat == MediaRecorder.OutputFormat.DEFAULT) { if (outputFormat == null || outputFormat == MediaRecorder.OutputFormat.DEFAULT) {
return true return true

View File

@ -114,5 +114,9 @@ data class AudioRecorderExporter(
fun hasRecordingsAvailable(context: Context) = fun hasRecordingsAvailable(context: Context) =
getFolder(context).listFiles()?.isNotEmpty() ?: false getFolder(context).listFiles()?.isNotEmpty() ?: false
// Write required for saving the audio files
// Read required for concatenating the audio files
fun canFolderBeUsed(file: File) = file.canRead() && file.canWrite()
} }
} }