Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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<NetworkChangeListener>()
private var lastCapabilityCheckMs: Long? = null

@Volatile
private var currentConnectivity: Connectivity = Connectivity.DISCONNECTED
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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
Expand All @@ -169,6 +176,7 @@ class ConnectivityServiceImpl(
}

if (previous != newConnectivity) {
Log_OC.d(TAG, "network capability changed - notifying listeners")
currentConnectivity = newConnectivity
walledCheckCache.putConnectivityValue(currentKey, newConnectivity)

Expand All @@ -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()
Expand All @@ -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
Expand Down
Loading