|
|
@ -37,6 +37,18 @@ class ConnectivityManagerNetworkMonitor @Inject constructor(
|
|
|
|
) : NetworkMonitor {
|
|
|
|
) : NetworkMonitor {
|
|
|
|
override val isOnline: Flow<Boolean> = callbackFlow {
|
|
|
|
override val isOnline: Flow<Boolean> = callbackFlow {
|
|
|
|
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
|
|
|
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
|
|
|
|
|
|
|
if (connectivityManager == null) {
|
|
|
|
|
|
|
|
channel.trySend(false)
|
|
|
|
|
|
|
|
channel.close()
|
|
|
|
|
|
|
|
return@callbackFlow
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Sends the latest connectivity status to the underlying channel.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
fun update() {
|
|
|
|
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The callback's methods are invoked on changes to *any* network, not just the active
|
|
|
|
* The callback's methods are invoked on changes to *any* network, not just the active
|
|
|
@ -44,47 +56,37 @@ class ConnectivityManagerNetworkMonitor @Inject constructor(
|
|
|
|
* ConnectivityManager.
|
|
|
|
* ConnectivityManager.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
val callback = object : NetworkCallback() {
|
|
|
|
val callback = object : NetworkCallback() {
|
|
|
|
override fun onAvailable(network: Network) {
|
|
|
|
override fun onAvailable(network: Network) = update()
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onLost(network: Network) {
|
|
|
|
override fun onLost(network: Network) = update()
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCapabilitiesChanged(
|
|
|
|
override fun onCapabilitiesChanged(
|
|
|
|
network: Network,
|
|
|
|
network: Network,
|
|
|
|
networkCapabilities: NetworkCapabilities,
|
|
|
|
networkCapabilities: NetworkCapabilities,
|
|
|
|
) {
|
|
|
|
) = update()
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
connectivityManager?.registerNetworkCallback(
|
|
|
|
connectivityManager.registerNetworkCallback(
|
|
|
|
Builder()
|
|
|
|
Builder()
|
|
|
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
|
.build(),
|
|
|
|
.build(),
|
|
|
|
callback,
|
|
|
|
callback,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
update()
|
|
|
|
|
|
|
|
|
|
|
|
awaitClose {
|
|
|
|
awaitClose {
|
|
|
|
connectivityManager?.unregisterNetworkCallback(callback)
|
|
|
|
connectivityManager.unregisterNetworkCallback(callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.conflate()
|
|
|
|
.conflate()
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
private fun ConnectivityManager?.isCurrentlyConnected() = when (this) {
|
|
|
|
private fun ConnectivityManager.isCurrentlyConnected() = when {
|
|
|
|
null -> false
|
|
|
|
VERSION.SDK_INT >= VERSION_CODES.M ->
|
|
|
|
else -> when {
|
|
|
|
activeNetwork
|
|
|
|
VERSION.SDK_INT >= VERSION_CODES.M ->
|
|
|
|
?.let(::getNetworkCapabilities)
|
|
|
|
activeNetwork
|
|
|
|
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
|
?.let(::getNetworkCapabilities)
|
|
|
|
else -> activeNetworkInfo?.isConnected
|
|
|
|
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
|
} ?: false
|
|
|
|
?: false
|
|
|
|
|
|
|
|
else -> activeNetworkInfo?.isConnected ?: false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|