diff --git a/logic/src/androidMain/kotlin/com/wire/kalium/logic/network/NetworkStateObserverImpl.kt b/logic/src/androidMain/kotlin/com/wire/kalium/logic/network/NetworkStateObserverImpl.kt index ba12a5286c6..c7782670592 100644 --- a/logic/src/androidMain/kotlin/com/wire/kalium/logic/network/NetworkStateObserverImpl.kt +++ b/logic/src/androidMain/kotlin/com/wire/kalium/logic/network/NetworkStateObserverImpl.kt @@ -22,7 +22,10 @@ import android.content.Context import android.net.ConnectivityManager import android.net.Network import android.net.NetworkCapabilities +import android.os.Build +import com.wire.kalium.logger.KaliumLogLevel import com.wire.kalium.logic.kaliumLogger +import com.wire.kalium.logic.logStructuredJson import com.wire.kalium.network.NetworkState import com.wire.kalium.network.NetworkStateObserver import com.wire.kalium.util.KaliumDispatcher @@ -56,7 +59,8 @@ internal actual class NetworkStateObserverImpl( init { val initialDefaultNetworkData = connectivityManager.activeNetwork?.let { - val defaultNetworkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) + val defaultNetworkCapabilities = + connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) DefaultNetworkData.Connected(it, defaultNetworkCapabilities) } ?: DefaultNetworkData.NotConnected defaultNetworkDataStateFlow = MutableStateFlow(initialDefaultNetworkData) @@ -76,14 +80,33 @@ internal actual class NetworkStateObserverImpl( val callback = object : ConnectivityManager.NetworkCallback() { - override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { + override fun onCapabilitiesChanged( + network: Network, + networkCapabilities: NetworkCapabilities + ) { super.onCapabilitiesChanged(network, networkCapabilities) - kaliumLogger.i( - "${NetworkStateObserver.TAG} capabilities changed " + - "internet:${networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)} " + - "validated:${networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)}" - ) - defaultNetworkDataStateFlow.update { DefaultNetworkData.Connected(network, networkCapabilities) } + val loggerMessage = mutableMapOf().apply { + put("internet", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).toString()) + put("validated", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED).toString()) + put("not restricted", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED).toString()) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + put("foreground", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND).toString()) + put("not congested", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED).toString()) + put("not suspended", networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED).toString()) + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + put("signalStrength", networkCapabilities.signalStrength.toString()) + } + } + kaliumLogger.logStructuredJson(KaliumLogLevel.INFO, "${NetworkStateObserver.TAG} capabilities changed", loggerMessage) + defaultNetworkDataStateFlow.update { + DefaultNetworkData.Connected( + network, + networkCapabilities + ) + } } override fun onLost(network: Network) { @@ -112,8 +135,7 @@ internal actual class NetworkStateObserverImpl( override fun onBlockedStatusChanged(network: Network, blocked: Boolean) { kaliumLogger.i("${NetworkStateObserver.TAG} block connection changed to $blocked") defaultNetworkDataStateFlow.update { - kaliumLogger.d("${NetworkStateObserver.TAG} block connection changed to $blocked current state is $it") - when (it) { + val updatedValue = when (it) { is DefaultNetworkData.Connected -> { it.copy(isBlocked = blocked) } @@ -123,6 +145,8 @@ internal actual class NetworkStateObserverImpl( else DefaultNetworkData.Connected(network) } } + kaliumLogger.d("${NetworkStateObserver.TAG} default network state $it changed to $updatedValue") + updatedValue } super.onBlockedStatusChanged(network, blocked) } diff --git a/network-util/src/commonMain/kotlin/com/wire/kalium/network/NetworkStateObserver.kt b/network-util/src/commonMain/kotlin/com/wire/kalium/network/NetworkStateObserver.kt index e5f2a0b4056..437fcd9d7be 100644 --- a/network-util/src/commonMain/kotlin/com/wire/kalium/network/NetworkStateObserver.kt +++ b/network-util/src/commonMain/kotlin/com/wire/kalium/network/NetworkStateObserver.kt @@ -31,7 +31,7 @@ interface NetworkStateObserver { // Delay which will be completed earlier if there is a reconnection in the meantime. suspend fun delayUntilConnectedWithInternetAgain(delay: Duration) { // Delay for given amount but break it if reconnected again. - kaliumUtilLogger.i("$TAG delayUntilConnectedWithInternetAgain") + kaliumUtilLogger.i("$TAG delayUntilConnectedWithInternetAgain for $delay") withTimeoutOrNull(delay) { // Drop the current value, so it will complete only if the connection changed again to connected during that time. observeNetworkState()