Fix network callback

Correctly check if internet was lost before restarting
Set failure counter to 1 from the network callback
This commit is contained in:
sim 2024-10-28 11:07:56 +00:00
parent d290b790d6
commit f17c932e9f
3 changed files with 39 additions and 25 deletions

View File

@ -132,9 +132,9 @@ class SSEListener(val context: Context) : EventSourceListener() {
}
if (!AppCompanion.hasInternet.get()) {
Log.d(TAG, "No Internet: do not restart")
if (FailureHandler.once(eventSource)) {
clearDebugVars()
}
// It will be restarted when Internet is back
eventSource.cancel()
clearDebugVars()
return
}
if (FailureHandler.newFail(context, eventSource)) {

View File

@ -66,21 +66,15 @@ object FailureHandler {
}
}
// Returns true if the fail is from the current eventSource
fun once(eventSource: EventSource?): Boolean {
/**
* Set the counter of failed events to 1.
*
* Used when there is no Internet access.
*/
fun once() {
Log.d(TAG, "once/Eventsource: $eventSource")
// ignore fails from a possible old eventSource
// if we are already reconnected
return if (isCurrentEventSource(eventSource)) {
Log.d(TAG, "EventSource is known or null")
nFails.set(1)
this.eventSource.getAndSet(null)?.cancel()
true
} else {
Log.d(TAG, "This is an old EventSource.")
eventSource?.cancel()
false
}
nFails.set(1)
this.eventSource.getAndSet(null)?.cancel()
}
fun setMaxFails(context: Context) {

View File

@ -17,9 +17,8 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
if (FailureHandler.hasFailed(orNeverStart = false)) {
Log.d(TAG, "Available: restarting worker")
RestartWorker.run(context, delay = 0)
if (!AppCompanion.hasInternet.getAndSet(true)) {
backOnline()
}
}
@ -28,19 +27,40 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
networkCapabilities: NetworkCapabilities
) {
if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
if (AppCompanion.hasInternet.getAndSet(true)) {
if (!AppCompanion.hasInternet.getAndSet(true)) {
Log.d(TAG, "Network Capabilities changed")
if (FailureHandler.hasFailed(orNeverStart = false)) {
Log.d(TAG, "Internet Cap: restarting worker")
RestartWorker.run(context, delay = 0)
} // else, it retries in max 2sec
backOnline()
}
} else {
noInternet()
}
}
override fun onLost(network: Network) {
Log.d(TAG, "Network unavailable")
noInternet()
}
/**
* When there is no internet, we set the failure count to 1 so it can try to reconnect directly
* when connection is back.
*
* We also set [AppCompanion.hasInternet] to false.
*/
private fun noInternet() {
AppCompanion.hasInternet.set(false)
FailureHandler.once()
}
/**
* When there is internet again, we start the [RestartWorker] if a failure is counted.
*/
private fun backOnline() {
// We first check if there is a fail registered, else a worker will run in 2 seconds
if (FailureHandler.hasFailed(orNeverStart = false)) {
Log.d(TAG, "Restarting worker")
RestartWorker.run(context, delay = 0)
} // else, it retries in max 2sec
}
fun register() {