Fix falsely reported internet connectivity

pull/1710/head
SessionHero01 4 weeks ago
parent 703136eb94
commit c5912c36e0
No known key found for this signature in database

@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
@ -64,9 +65,12 @@ class GroupPollerManager @Inject constructor(
@Suppress("OPT_IN_USAGE")
private val groupPollers: StateFlow<Map<AccountId, GroupPollerHandle>> =
combine(
connectivity.networkAvailable,
connectivity.networkAvailable.debounce(200L),
preferences.watchLocalNumber()
) { networkAvailable, localNumber -> networkAvailable && localNumber != null }
) { networkAvailable, localNumber ->
Log.v(TAG, "Network available: $networkAvailable, hasLocalNumber: ${localNumber != null}")
networkAvailable && localNumber != null
}
// This flatMap produces a flow of groups that should be polled now
.flatMapLatest { shouldPoll ->
if (shouldPoll) {

@ -7,14 +7,12 @@ import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import org.session.libsignal.utilities.Log
import javax.inject.Inject
import javax.inject.Singleton
@ -31,24 +29,21 @@ class InternetConnectivity @Inject constructor(application: Application) {
network: Network,
networkCapabilities: NetworkCapabilities
) {
trySend(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET))
}
override fun onAvailable(network: Network) {
trySend(true)
super.onCapabilitiesChanged(network, networkCapabilities)
val hasInternet =
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
Log.v("InternetConnectivity", "Network capabilities changed: hasInternet? $hasInternet")
trySend(hasInternet)
}
override fun onLost(network: Network) {
super.onLost(network)
Log.v("InternetConnectivity", "Network become lost")
trySend(false)
}
}
connectivityManager.registerNetworkCallback(
NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(),
callback
)
connectivityManager.registerDefaultNetworkCallback(callback)
awaitClose {
connectivityManager.unregisterNetworkCallback(callback)

Loading…
Cancel
Save