|
|
@ -36,48 +36,50 @@ import javax.inject.Inject
|
|
|
|
class ConnectivityManagerNetworkMonitor @Inject constructor(
|
|
|
|
class ConnectivityManagerNetworkMonitor @Inject constructor(
|
|
|
|
@ApplicationContext private val context: Context,
|
|
|
|
@ApplicationContext private val context: Context,
|
|
|
|
) : NetworkMonitor {
|
|
|
|
) : NetworkMonitor {
|
|
|
|
override val isOnline: Flow<Boolean> {
|
|
|
|
override val isOnline: Flow<Boolean> = callbackFlow {
|
|
|
|
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
|
|
|
val connectivityManager = context.getSystemService<ConnectivityManager>()
|
|
|
|
?: return flowOf(false)
|
|
|
|
?: run {
|
|
|
|
|
|
|
|
channel.trySend(false)
|
|
|
|
return callbackFlow {
|
|
|
|
channel.close()
|
|
|
|
|
|
|
|
return@callbackFlow
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* network. So to check for network connectivity, one must query the active network of the
|
|
|
|
* network. So to check for network connectivity, one must query the active network of the
|
|
|
|
* ConnectivityManager.
|
|
|
|
* ConnectivityManager.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
val callback = object : NetworkCallback() {
|
|
|
|
val callback = object : NetworkCallback() {
|
|
|
|
override fun onAvailable(network: Network) {
|
|
|
|
override fun onAvailable(network: Network) {
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onLost(network: Network) {
|
|
|
|
override fun onLost(network: Network) {
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCapabilitiesChanged(
|
|
|
|
override fun onCapabilitiesChanged(
|
|
|
|
network: Network,
|
|
|
|
network: Network,
|
|
|
|
networkCapabilities: NetworkCapabilities,
|
|
|
|
networkCapabilities: NetworkCapabilities,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
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())
|
|
|
|
channel.trySend(connectivityManager.isCurrentlyConnected())
|
|
|
|
|
|
|
|
|
|
|
|
awaitClose {
|
|
|
|
awaitClose {
|
|
|
|
connectivityManager.unregisterNetworkCallback(callback)
|
|
|
|
connectivityManager.unregisterNetworkCallback(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.conflate()
|
|
|
|
}
|
|
|
|
|
|
|
|
.conflate()
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
private fun ConnectivityManager.isCurrentlyConnected() = when {
|
|
|
|
private fun ConnectivityManager.isCurrentlyConnected() = when {
|
|
|
|