34 lines
1.2 KiB
Kotlin
34 lines
1.2 KiB
Kotlin
package com.example.smartalarm.service
|
|
|
|
import android.content.BroadcastReceiver
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import com.example.smartalarm.data.db.AlarmDatabase
|
|
import com.example.smartalarm.data.repository.AlarmRepository
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.SupervisorJob
|
|
import kotlinx.coroutines.flow.first
|
|
import kotlinx.coroutines.launch
|
|
|
|
class BootReceiver : BroadcastReceiver() {
|
|
|
|
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
if (intent.action == Intent.ACTION_BOOT_COMPLETED ||
|
|
intent.action == "android.intent.action.QUICKBOOT_POWERON") {
|
|
|
|
// Reschedule all enabled alarms
|
|
scope.launch {
|
|
val database = AlarmDatabase.getDatabase(context)
|
|
val repository = AlarmRepository(database.alarmDao())
|
|
val scheduler = AlarmScheduler(context)
|
|
|
|
repository.enabledAlarms.first().forEach { alarm ->
|
|
scheduler.scheduleAlarm(alarm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |