34 lines
1.3 KiB
Kotlin
34 lines
1.3 KiB
Kotlin
package com.androidsmsgateway.server
|
|
|
|
import android.content.BroadcastReceiver
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import androidx.core.content.ContextCompat
|
|
import com.androidsmsgateway.GatewayApplication
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.launch
|
|
|
|
class BootReceiver : BroadcastReceiver() {
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
if (intent.action != Intent.ACTION_BOOT_COMPLETED) return
|
|
val pendingResult = goAsync()
|
|
val settingsRepository = (context.applicationContext as GatewayApplication).container.settingsRepository
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
try {
|
|
runCatching {
|
|
val settings = settingsRepository.current()
|
|
if (settings.startOnBoot && settings.serverEnabled) {
|
|
ContextCompat.startForegroundService(
|
|
context,
|
|
Intent(context, GatewayServerService::class.java).setAction(GatewayServerService.ACTION_START),
|
|
)
|
|
}
|
|
}
|
|
} finally {
|
|
pendingResult.finish()
|
|
}
|
|
}
|
|
}
|
|
}
|