Skip to content

Commit

Permalink
fix: Update coroutines usage in NetworkAvailability
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Nov 27, 2024
1 parent c2de88a commit 574e513
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Core2/Network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
}

dependencies {
implementation(project(":Core2:Sentry"))

implementation(libs.androidx.core.ktx)
implementation(core2.sentry.android)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import com.infomaniak.sentry.SentryLog
import io.sentry.Sentry
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext

class NetworkAvailability(private val context: Context, private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) {

Expand All @@ -49,7 +50,7 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
launch {
mutex.withLock {
networks.add(network)
sendNetworkAvailability(networks)
send(element = hasAvailableNetwork(networks))
}
}
}
Expand All @@ -58,7 +59,7 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
launch {
mutex.withLock {
networks.remove(network)
sendNetworkAvailability(networks)
send(element = hasAvailableNetwork(networks))
}
}
}
Expand All @@ -68,21 +69,23 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
send(getInitialNetworkAvailability(connectivityManager))
}

registerNetworkCallback(connectivityManager, callback)
registerNetworkCallback(connectivityManager, callback, ::send)

awaitClose { unregisterNetworkCallback(connectivityManager, callback) }
}

private suspend fun ProducerScope<Boolean>.registerNetworkCallback(
private suspend fun registerNetworkCallback(
connectivityManager: ConnectivityManager,
callback: NetworkCallback,
send: suspend (Boolean) -> Unit,
) {
runCatching {
connectivityManager.registerNetworkCallback(networkRequestBuilder(), callback)
}.onFailure { exception ->
// Fix potential Exception thrown by ConnectivityManager on Android 11
// Already fixed in Android S and above
// Fix potential Exception thrown by ConnectivityManager on Android 11.
// Already fixed in Android S and above.
// https://issuetracker.google.com/issues/175055271
SentryLog.e(TAG, "Android 11 exception", exception)
Sentry.captureException(exception)
send(false)
}
Expand All @@ -96,7 +99,6 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
}
}

@Suppress("DEPRECATION")
private fun getInitialNetworkAvailability(connectivityManager: ConnectivityManager): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.activeNetwork?.let(::hasInternetConnectivity) ?: false
Expand All @@ -105,10 +107,6 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
}
}

private fun ProducerScope<Boolean>.sendNetworkAvailability(networks: List<Network>) {
launch(ioDispatcher) { send(hasAvailableNetwork(networks)) }
}

private fun networkRequestBuilder(): NetworkRequest {
return NetworkRequest.Builder().apply {
addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
Expand All @@ -119,9 +117,12 @@ class NetworkAvailability(private val context: Context, private val ioDispatcher
network.getByName(ROOT_SERVER_URL) != null
}.getOrDefault(false)

private suspend fun hasAvailableNetwork(networks: List<Network>) = mutex.withLock { networks.any(::hasInternetConnectivity) }
private suspend fun hasAvailableNetwork(networks: List<Network>) = withContext(ioDispatcher) {
networks.any(::hasInternetConnectivity)
}

companion object {
private val TAG = NetworkAvailability::class.java.simpleName
private const val ROOT_SERVER_URL = "a.root-servers.net"
}
}

0 comments on commit 574e513

Please sign in to comment.