feat: Add automatic recording functionality on boot

This commit is contained in:
Myzel394 2023-10-28 18:20:00 +02:00
parent 2e9f1c7ade
commit a0bf2d03eb
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
5 changed files with 26 additions and 5 deletions

View File

@ -47,6 +47,7 @@
</receiver> </receiver>
<service <service
android:name=".services.AudioRecorderService" android:name=".services.AudioRecorderService"
android:exported="false"
android:foregroundServiceType="microphone" /> android:foregroundServiceType="microphone" />
<!-- Change locale for Android <= 12 --> <!-- Change locale for Android <= 12 -->

View File

@ -19,7 +19,7 @@ data class AppSettings(
val showAdvancedSettings: Boolean = false, val showAdvancedSettings: Boolean = false,
val theme: Theme = Theme.SYSTEM, val theme: Theme = Theme.SYSTEM,
val lastRecording: RecordingInformation? = null, val lastRecording: RecordingInformation? = null,
val bootBehavior: BootBehavior? = BootBehavior.SHOW_NOTIFICATION, val bootBehavior: BootBehavior? = BootBehavior.START_RECORDING,
) { ) {
fun setShowAdvancedSettings(showAdvancedSettings: Boolean): AppSettings { fun setShowAdvancedSettings(showAdvancedSettings: Boolean): AppSettings {
return copy(showAdvancedSettings = showAdvancedSettings) return copy(showAdvancedSettings = showAdvancedSettings)

View File

@ -45,8 +45,15 @@ class BootReceiver : BroadcastReceiver() {
} }
} }
println("BootReceiver.startRecording()")
val intent = Intent(context, AudioRecorderService::class.java).apply { val intent = Intent(context, AudioRecorderService::class.java).apply {
action = "init" action = "init"
putExtra(
"startImmediately",
true,
)
if (settings.notificationSettings != null) { if (settings.notificationSettings != null) {
putExtra( putExtra(
"notificationDetails", "notificationDetails",
@ -61,7 +68,6 @@ class BootReceiver : BroadcastReceiver() {
} }
} }
ContextCompat.startForegroundService(context, intent) ContextCompat.startForegroundService(context, intent)
context.bindService(intent, connection, 0)
} }
private fun showNotification(context: Context) { private fun showNotification(context: Context) {
@ -98,8 +104,11 @@ class BootReceiver : BroadcastReceiver() {
return return
} }
println("BootReceiver.onReceive()")
scope.launch { scope.launch {
context.dataStore.data.collectLatest { settings -> context.dataStore.data.collectLatest { settings ->
println("BootBehavior: ${settings.bootBehavior}")
when (settings.bootBehavior) { when (settings.bootBehavior) {
AppSettings.BootBehavior.CONTINUE_RECORDING -> { AppSettings.BootBehavior.CONTINUE_RECORDING -> {
if (AudioRecorderExporter.hasRecordingsAvailable(context)) { if (AudioRecorderExporter.hasRecordingsAvailable(context)) {

View File

@ -58,10 +58,21 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
} }
} }
private fun fetchCounterValue() {
val files = outputFolder.listFiles()?.filter {
val name = it.nameWithoutExtension
name.toIntOrNull() != null
}?.toList() ?: emptyList()
counter = files.size
}
override fun start() { override fun start() {
super.start() super.start()
outputFolder.mkdirs() outputFolder.mkdirs()
fetchCounterValue()
scope.launch { scope.launch {
dataStore.data.collectLatest { preferenceSettings -> dataStore.data.collectLatest { preferenceSettings ->

View File

@ -55,11 +55,11 @@ abstract class RecorderService : Service() {
it it
) )
} }
}
"initStart" -> { if (intent.getBooleanExtra("startImmediately", false)) {
startRecording() startRecording()
} }
}
"changeState" -> { "changeState" -> {
val newState = intent.getStringExtra("newState")?.let { val newState = intent.getStringExtra("newState")?.let {