Check buffered response once per service started

This commit is contained in:
sim 2023-04-11 18:38:16 +02:00
parent 120c44d203
commit 59ffef6f05
2 changed files with 9 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback
import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.RestartWorker
import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.services.StartService
import org.unifiedpush.distributor.nextpush.services.StartService.StartServiceCompanion.bufferedResponseChecked
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showLowKeepaliveNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showLowKeepaliveNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErrorNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErrorNotification
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
@ -28,10 +29,12 @@ class SSEListener(val context: Context) : EventSourceListener() {
override fun onOpen(eventSource: EventSource, response: Response) { override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource) FailureHandler.newEventSource(context, eventSource)
startingTimer?.cancel() startingTimer?.cancel()
if (!bufferedResponseChecked) {
startingTimer = Timer().schedule(45_000L /* 45secs */) { startingTimer = Timer().schedule(45_000L /* 45secs */) {
StartService.stopService() StartService.stopService()
showStartErrorNotification(context) showStartErrorNotification(context)
} }
}
StartService.wakeLock?.let { StartService.wakeLock?.let {
while (it.isHeld) { while (it.isHeld) {
it.release() it.release()
@ -53,6 +56,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
"start" -> { "start" -> {
started = true started = true
startingTimer?.cancel() startingTimer?.cancel()
bufferedResponseChecked = true
} }
"ping" -> { "ping" -> {
pinged = true pinged = true

View File

@ -85,6 +85,7 @@ class StartService : Service() {
private set private set
var wakeLock: PowerManager.WakeLock? = null var wakeLock: PowerManager.WakeLock? = null
private set private set
var bufferedResponseChecked = false
fun startListener(context: Context) { fun startListener(context: Context) {
if (isServiceStarted && !FailureHandler.hasFailed()) return if (isServiceStarted && !FailureHandler.hasFailed()) return
@ -102,6 +103,7 @@ class StartService : Service() {
fun stopService(block: () -> Unit = {}) { fun stopService(block: () -> Unit = {}) {
Log.d(TAG, "Stopping Service") Log.d(TAG, "Stopping Service")
isServiceStarted = false isServiceStarted = false
bufferedResponseChecked = false
lastEventDate = null lastEventDate = null
service?.stopSelf() service?.stopSelf()
block() block()