Avoid useless service restart

Restart service with networkCallback only when Internet
is available again.
This commit is contained in:
sim 2023-03-21 21:52:45 +01:00
parent ec7e27b3a3
commit 254a776244
2 changed files with 36 additions and 17 deletions

View File

@ -15,7 +15,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
Log.d(TAG, "networkCallback: restarting worker")
Log.d(TAG, "Available: restarting worker")
RestartWorker.run(context, delay = 0)
}
}
@ -24,26 +24,36 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
network: Network,
networkCapabilities: NetworkCapabilities
) {
Log.d(TAG, "Network Capabilities changed")
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
Log.d(TAG, "networkCallback: restarting worker")
RestartWorker.run(context, delay = 0)
} // else, it retries in max 2sec
if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
if (!hasInternet) {
hasInternet = true
Log.d(TAG, "Network Capabilities changed")
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
Log.d(TAG, "Internet Cap: restarting worker")
RestartWorker.run(context, delay = 0)
} // else, it retries in max 2sec
}
} else {
hasInternet = false
}
}
fun register() {
connectivityManager?.let {
Log.d(TAG, "ConnectivityManager already registered")
} ?: run {
Log.d(TAG, "Registering new ConnectivityManager")
try {
connectivityManager = (
context.getSystemService(Service.CONNECTIVITY_SERVICE) as ConnectivityManager
).apply {
registerDefaultNetworkCallback(this@RestartNetworkCallback)
if (!registered) {
registered = true
connectivityManager?.let {
Log.d(TAG, "ConnectivityManager already registered")
} ?: run {
Log.d(TAG, "Registering new ConnectivityManager")
try {
connectivityManager = (
context.getSystemService(Service.CONNECTIVITY_SERVICE) as ConnectivityManager
).apply {
registerDefaultNetworkCallback(this@RestartNetworkCallback)
}
} catch (e: Exception) {
e.printStackTrace()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
@ -52,5 +62,12 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
Log.d(TAG, "Unregistering ConnectivityManager")
connectivityManager?.unregisterNetworkCallback(this)
connectivityManager = null
registered = false
hasInternet = false
}
companion object {
private var registered = false
private var hasInternet = false
}
}

View File

@ -50,6 +50,8 @@ class StartService : Service() {
if (isServiceStarted) {
Log.d(TAG, "onDestroy: restarting worker")
RestartWorker.run(this, delay = 0)
} else {
networkCallback.unregister()
}
}