diff --git a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt index 0f59b20edf84..bd9ea8c79fae 100644 --- a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt +++ b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt @@ -11,6 +11,7 @@ import android.net.ConnectivityManager import android.net.Network import android.net.NetworkCapabilities import android.os.Build +import android.os.SystemClock import com.nextcloud.client.account.UserAccountManager import com.nextcloud.operations.GetMethod import com.owncloud.android.lib.common.utils.Log_OC @@ -25,6 +26,7 @@ import java.net.ConnectException import java.net.SocketTimeoutException import java.net.UnknownHostException import javax.net.ssl.SSLException +import kotlin.time.Duration.Companion.seconds @Suppress("TooGenericExceptionCaught", "ReturnCount") class ConnectivityServiceImpl( @@ -38,6 +40,8 @@ class ConnectivityServiceImpl( companion object { private const val TAG = "ConnectivityServiceImpl" private const val CONNECTIVITY_CHECK_ROUTE = "/index.php/204" + + private val CAPABILITY_CHANGE_DEBOUNCE = 15.seconds } // region private values @@ -46,6 +50,7 @@ class ConnectivityServiceImpl( private var notifyJob: Job? = null private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager private val listeners = mutableSetOf() + private var lastCapabilityCheckMs: Long? = null @Volatile private var currentConnectivity: Connectivity = Connectivity.DISCONNECTED @@ -60,7 +65,11 @@ class ConnectivityServiceImpl( } override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { - Log_OC.d(TAG, "capability changed") + if (!shouldHandleCapabilityChange()) { + return + } + + Log_OC.d(TAG, "resolving network capabilities to compare") updateConnectivity() } } @@ -148,9 +157,7 @@ class ConnectivityServiceImpl( fun updateConnectivity() { val currentKey = key val previous = currentConnectivity - val capabilities = resolveNetworkCapabilities() - val newConnectivity = if (capabilities == null) { Log_OC.w(TAG, "no network capabilities found, connectivity is disconnected") Connectivity.DISCONNECTED @@ -169,6 +176,7 @@ class ConnectivityServiceImpl( } if (previous != newConnectivity) { + Log_OC.d(TAG, "network capability changed - notifying listeners") currentConnectivity = newConnectivity walledCheckCache.putConnectivityValue(currentKey, newConnectivity) @@ -178,6 +186,7 @@ class ConnectivityServiceImpl( ) if (isStructural) { + Log_OC.d(TAG, "network structurally capability changed - clearing walled cache as well") walledCheckCache.clear(currentKey) } notifyListeners() @@ -195,6 +204,17 @@ class ConnectivityServiceImpl( // endregion // region private methods + private fun shouldHandleCapabilityChange(): Boolean { + val now = SystemClock.elapsedRealtime() + val last = lastCapabilityCheckMs + if (last != null && now - last < CAPABILITY_CHANGE_DEBOUNCE.inWholeMilliseconds) { + return false + } + + lastCapabilityCheckMs = now + return true + } + private fun notifyListeners() { if (listeners.isEmpty()) { return