Improve buffered response detection

This commit is contained in:
sim 2023-03-26 22:46:41 +02:00
parent d1061df181
commit cd25fdc3cb
3 changed files with 20 additions and 20 deletions

View File

@ -9,7 +9,6 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
internal const val PREF_NAME = "NextPush"
private const val PREF_DEVICE_ID = "deviceId"
private const val PREF_ACCOUNT_TYPE = "account::type"
private const val PREF_HAS_STARTED_ONCE = "account::has_started_once"
enum class AccountType {
SSO,
@ -48,12 +47,6 @@ object Account {
}.apply()
}
var Context.hasStartedOnce: Boolean
get() = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getBoolean(PREF_HAS_STARTED_ONCE, false)
set(value) = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.edit().putBoolean(PREF_HAS_STARTED_ONCE, value).apply()
fun getAccount(context: Context, uninitialized: Boolean = false): AccountFactory? {
return account
?: run {
@ -89,7 +82,6 @@ object Account {
fun logout(context: Context) {
getAccount(context)?.logout(context)
context.deviceId = null
context.hasStartedOnce = false
}
fun Context.setTypeSSO() {

View File

@ -91,6 +91,7 @@ class Api(val context: Context) {
.get()
.build()
SSEListener.starting(context)
syncSource = EventSources.createFactory(client).newEventSource(request, SSEListener(context))
Log.d(TAG, "cSync done.")
}

View File

@ -7,7 +7,6 @@ import com.google.gson.Gson
import okhttp3.Response
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import org.unifiedpush.distributor.nextpush.account.Account.hasStartedOnce
import org.unifiedpush.distributor.nextpush.api.response.SSEResponse
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE
import org.unifiedpush.distributor.nextpush.distributor.Distributor.sendMessage
@ -20,11 +19,15 @@ import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErr
import org.unifiedpush.distributor.nextpush.utils.TAG
import java.lang.Exception
import java.util.Calendar
import java.util.Timer
import java.util.TimerTask
import kotlin.concurrent.schedule
class SSEListener(val context: Context) : EventSourceListener() {
override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource)
startingTimer?.cancel()
StartService.wakeLock?.let {
while (it.isHeld) {
it.release()
@ -43,10 +46,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
lastEventDate = Calendar.getInstance()
when (type) {
"start" -> {
started = true
context.hasStartedOnce = true
}
"start" -> started = true
"ping" -> {
pinged = true
FailureHandler.newPing()
@ -122,19 +122,14 @@ class SSEListener(val context: Context) : EventSourceListener() {
private fun shouldRestart(): Boolean {
if (!StartService.isServiceStarted) {
Log.d(TAG, "StartService not started")
return false
}
if (!context.hasStartedOnce) {
Log.d(TAG, "SSE event 'start' never received")
Log.d(TAG, "Stopping service")
StartService.stopService()
showStartErrorNotification(context)
clearVars()
return false
}
return true
}
private fun clearVars() {
startingTimer?.cancel()
started = false
pinged = false
}
@ -143,9 +138,21 @@ class SSEListener(val context: Context) : EventSourceListener() {
var lastEventDate: Calendar? = null
var keepalive = 900
private set
private var startingTimer: TimerTask? = null
var pinged = false
private set
var started = false
private set
fun starting(context: Context) {
// The request timeout after 10seconds
// If it doesn't, and onOpen hasn't been called, then
// the response is very probably buffered.
// +20secs for a margin
startingTimer = Timer().schedule(30_000L /* 30secs */) {
StartService.stopService()
showStartErrorNotification(context)
}
}
}
}