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 f76d5d5..f461a65 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt @@ -22,7 +22,11 @@ data class BatchesFolder( fun initFolders() { when (type) { BatchType.INTERNAL -> getFolder(context).mkdirs() - BatchType.CUSTOM -> customFolder?.createDirectory(subfolderName) + BatchType.CUSTOM -> { + if (customFolder!!.findFile(subfolderName) == null) { + customFolder.createDirectory(subfolderName) + } + } } } @@ -93,7 +97,9 @@ data class BatchesFolder( fun deleteRecordings() { when (type) { BatchType.INTERNAL -> getInternalFolder().deleteRecursively() - BatchType.CUSTOM -> getCustomDefinedFolder().delete() + BatchType.CUSTOM -> getCustomDefinedFolder().listFiles().forEach { + it.delete() + } } } @@ -120,7 +126,7 @@ data class BatchesFolder( fun checkIfFolderIsAccessible(): Boolean { return when (type) { BatchType.INTERNAL -> true - BatchType.CUSTOM -> customFolder!!.canWrite() && customFolder.canRead() + BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead() } } @@ -132,7 +138,8 @@ data class BatchesFolder( counter: Long, fileExtension: String, ): FileDescriptor { - val file = customFolder!!.createFile("audio/$fileExtension", "$counter.$fileExtension")!! + val file = + getCustomDefinedFolder().createFile("audio/$fileExtension", "$counter.$fileExtension")!! customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!! diff --git a/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt b/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt index 4c1cf1d..79d04fa 100644 --- a/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt +++ b/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt @@ -69,22 +69,22 @@ class AudioRecorderService : IntervalRecorderService() { when (batchesFolder.type) { BatchesFolder.BatchType.INTERNAL -> { setOutputFile( - batchesFolder.asInternalGetOutputPath(counter, settings!!.fileExtension) + batchesFolder.asInternalGetOutputPath(counter, settings.fileExtension) ) } BatchesFolder.BatchType.CUSTOM -> { setOutputFile( - batchesFolder.asCustomGetFileDescriptor(counter, settings!!.fileExtension) + batchesFolder.asCustomGetFileDescriptor(counter, settings.fileExtension) ) } } - setOutputFormat(settings!!.outputFormat) + setOutputFormat(settings.outputFormat) - setAudioEncoder(settings!!.encoder) - setAudioEncodingBitRate(settings!!.bitRate) - setAudioSamplingRate(settings!!.samplingRate) + setAudioEncoder(settings.encoder) + setAudioEncodingBitRate(settings.bitRate) + setAudioSamplingRate(settings.samplingRate) setOnErrorListener(OnErrorListener { _, _, _ -> onError() }) 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 32bbac0..c947ede 100644 --- a/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt +++ b/app/src/main/java/app/myzel394/alibi/services/IntervalRecorderService.kt @@ -26,14 +26,10 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() { protected var counter = 0L private set - var settings: Settings? = null - protected set + lateinit var settings: Settings private lateinit var cycleTimer: ScheduledExecutorService - protected val defaultOutputFolder: File - get() = AudioRecorderExporter.getFolder(this) - var batchesFolder: BatchesFolder = BatchesFolder.viaInternalFolder(this) var onCustomOutputFolderNotAccessible: () -> Unit = {} @@ -41,10 +37,10 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() { fun getRecordingInformation(): RecordingInformation = RecordingInformation( folderPath = batchesFolder.exportFolderForSettings(), recordingStart = recordingStart, - maxDuration = settings!!.maxDuration, - fileExtension = settings!!.fileExtension, - intervalDuration = settings!!.intervalDuration, - forceExactMaxDuration = settings!!.forceExactMaxDuration, + maxDuration = settings.maxDuration, + fileExtension = settings.fileExtension, + intervalDuration = settings.intervalDuration, + forceExactMaxDuration = settings.forceExactMaxDuration, ) // Make overrideable @@ -60,7 +56,7 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() { startNewCycle() }, 0, - settings!!.intervalDuration, + settings.intervalDuration, TimeUnit.MILLISECONDS ) } @@ -69,12 +65,13 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() { override fun start() { super.start() + batchesFolder.initFolders() if (!batchesFolder.checkIfFolderIsAccessible()) { batchesFolder = BatchesFolder.viaInternalFolder(this@IntervalRecorderService) + batchesFolder.initFolders() onCustomOutputFolderNotAccessible() } - batchesFolder.initFolders() createTimer() } diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt index 5672c91..b583665 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt @@ -77,29 +77,7 @@ fun StartRecording( if (startRecording) { startRecording = false - audioRecorder.let { recorder -> - recorder.notificationDetails = appSettings.notificationSettings.let { - if (it == null) - null - else - RecorderNotificationHelper.NotificationDetails.fromNotificationSettings( - context, - it - ) - } - recorder.batchesFolder = if (appSettings.audioRecorderSettings.saveFolder == null) - BatchesFolder.viaInternalFolder(context) - else - BatchesFolder.viaCustomFolder( - context, - DocumentFile.fromTreeUri( - context, - Uri.parse(appSettings.audioRecorderSettings.saveFolder) - )!! - ) - - recorder.startRecording(context) - } + audioRecorder.startRecording(context, appSettings) } } diff --git a/app/src/main/java/app/myzel394/alibi/ui/models/AudioRecorderModel.kt b/app/src/main/java/app/myzel394/alibi/ui/models/AudioRecorderModel.kt index a1f1648..09e7adc 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/models/AudioRecorderModel.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/models/AudioRecorderModel.kt @@ -4,6 +4,7 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.ServiceConnection +import android.net.Uri import android.os.IBinder import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -11,11 +12,13 @@ import androidx.compose.runtime.setValue import androidx.core.content.ContextCompat import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.ViewModel +import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.db.RecordingInformation import app.myzel394.alibi.enums.RecorderState import app.myzel394.alibi.helpers.AudioRecorderExporter import app.myzel394.alibi.helpers.BatchesFolder import app.myzel394.alibi.services.AudioRecorderService +import app.myzel394.alibi.services.IntervalRecorderService import app.myzel394.alibi.services.RecorderNotificationHelper import app.myzel394.alibi.services.RecorderService import kotlinx.serialization.json.Json @@ -50,6 +53,8 @@ class AudioRecorderModel : ViewModel() { var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null var batchesFolder: BatchesFolder? = null + private lateinit var settings: AppSettings + var microphoneStatus: MicrophoneConnectivityStatus = MicrophoneConnectivityStatus.CONNECTED private set @@ -62,7 +67,9 @@ class AudioRecorderModel : ViewModel() { override fun onServiceConnected(className: ComponentName, service: IBinder) { recorderService = ((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also { recorder -> - // Update UI when the service changes + recorder.clearAllRecordings() + + // Init variables from us to the service recorder.onStateChange = { state -> recorderState = state } @@ -86,6 +93,8 @@ class AudioRecorderModel : ViewModel() { microphoneStatus = MicrophoneConnectivityStatus.CONNECTED } recorder.batchesFolder = batchesFolder ?: recorder.batchesFolder + recorder.settings = + IntervalRecorderService.Settings.from(settings.audioRecorderSettings) }.also { // Init UI from the service it.startRecording() @@ -111,11 +120,33 @@ class AudioRecorderModel : ViewModel() { microphoneStatus = MicrophoneConnectivityStatus.CONNECTED } - fun startRecording(context: Context) { + fun startRecording(context: Context, settings: AppSettings) { runCatching { context.unbindService(connection) + recorderService?.clearAllRecordings() } + notificationDetails = settings.notificationSettings.let { + if (it == null) + null + else + RecorderNotificationHelper.NotificationDetails.fromNotificationSettings( + context, + it + ) + } + batchesFolder = if (settings.audioRecorderSettings.saveFolder == null) + BatchesFolder.viaInternalFolder(context) + else + BatchesFolder.viaCustomFolder( + context, + DocumentFile.fromTreeUri( + context, + Uri.parse(settings.audioRecorderSettings.saveFolder) + )!! + ) + this.settings = settings + val intent = Intent(context, AudioRecorderService::class.java).apply { action = "init"