Skip to content

Commit

Permalink
Merge pull request #5227 from wikimedia/offlineFollowup
Browse files Browse the repository at this point in the history
Follow-up to offline state tracking.
  • Loading branch information
Williamrai authored Jan 14, 2025
2 parents 86c4492 + e701f77 commit c973865
Showing 1 changed file with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import android.os.Handler
import android.os.Looper
import org.wikipedia.WikipediaApp
import org.wikipedia.analytics.eventplatform.EventPlatformClient
import org.wikipedia.savedpages.SavedPageSyncService
Expand Down Expand Up @@ -50,14 +48,12 @@ class ConnectionStateMonitor : ConnectivityManager.NetworkCallback() {

override fun onAvailable(network: Network) {
super.onAvailable(network)
updateOnlineState()
updateOnlineState(true)
}

override fun onLost(network: Network) {
super.onLost(network)
Handler(Looper.getMainLooper()).postDelayed({
updateOnlineState()
}, 100)
updateOnlineState(false)
}

private fun ensureNetworkCallbackRegistered() {
Expand All @@ -83,19 +79,25 @@ class ConnectionStateMonitor : ConnectivityManager.NetworkCallback() {
}
}

private fun updateOnlineState() {
val connectivityManager = WikipediaApp.instance.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
online = try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
} else {
connectivityManager.activeNetworkInfo?.isConnected == true
private fun updateOnlineState(state: Boolean? = null) {
if (state != null) {
online = state
lastCheckedMillis = System.currentTimeMillis()
} else {
val connectivityManager = WikipediaApp.instance.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
online = try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
} else {
connectivityManager.activeNetworkInfo?.isConnected == true
}
} catch (e: Exception) {
// Framework bug, will only be fixed in Android S:
// https://issuetracker.google.com/issues/175055271
// Assume we're online, until the next call to update the state, which will happen shortly.
true
}
} catch (e: Exception) {
// Framework bug, will only be fixed in Android S:
// https://issuetracker.google.com/issues/175055271
// Assume we're online, until the next call to update the state, which will happen shortly.
true
}

EventPlatformClient.setEnabled(online)
Expand Down

0 comments on commit c973865

Please sign in to comment.