Skip to content

Commit

Permalink
chore: add more debug information for sync #WPB-11603 🍒 (#3072)
Browse files Browse the repository at this point in the history
* Commit with unresolved merge conflicts

* conflict solved

---------

Co-authored-by: Damian Kaczmarek <[email protected]>
Co-authored-by: Damian Kaczmarek <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent 71aef39 commit 2a06026
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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<String, String>().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) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 $delay")
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()
Expand Down

0 comments on commit 2a06026

Please sign in to comment.