Skip to content

Commit eb05a3a

Browse files
committed
Address review: keep connectivity subscribers on-main and fix handover
- SuggestionActivity's ConnectionChangeEvent subscriber used a bare @subscribe (POSTING). Since the event is now posted from a background thread, its updateEmptyView() touched Views off the main thread. Pin it to ThreadMode.MAIN. - NetworkConnectionMonitor.onLost no longer assumes the device is offline: during a network handover (e.g. Wi-Fi -> cellular) the replacement is already the default, so re-query the active network's capabilities instead of posting a spurious disconnected event.
1 parent 4886911 commit eb05a3a

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

WordPress/src/main/java/org/wordpress/android/networking/NetworkConnectionMonitor.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.wordpress.android.networking
33
import android.content.Context
44
import android.net.ConnectivityManager
55
import android.net.Network
6+
import android.net.NetworkCapabilities
67
import android.os.Handler
78
import android.os.HandlerThread
89
import org.greenrobot.eventbus.EventBus
@@ -44,7 +45,9 @@ class NetworkConnectionMonitor @Inject constructor() {
4445

4546
val callback = object : ConnectivityManager.NetworkCallback() {
4647
override fun onAvailable(network: Network) = onConnectivityChanged(true)
47-
override fun onLost(network: Network) = onConnectivityChanged(false)
48+
// onLost fires for the network that was lost, but during a handover (e.g. Wi-Fi -> cellular) the
49+
// new default has already arrived via onAvailable, so re-query rather than assuming we're offline.
50+
override fun onLost(network: Network) = onConnectivityChanged(hasActiveConnection())
4851
}
4952
networkCallback = callback
5053

@@ -76,4 +79,15 @@ class NetworkConnectionMonitor @Inject constructor() {
7679
EventBus.getDefault().post(ConnectionChangeEvent(isConnected))
7780
}
7881
}
82+
83+
/**
84+
* Whether there is currently a default network capable of reaching the internet. Used to distinguish a
85+
* true disconnection from a handover between networks, where the replacement is already the default.
86+
*/
87+
private fun hasActiveConnection(): Boolean {
88+
val manager = connectivityManager ?: return false
89+
val activeNetwork = manager.activeNetwork ?: return false
90+
val capabilities = manager.getNetworkCapabilities(activeNetwork) ?: return false
91+
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
92+
}
7993
}

WordPress/src/main/java/org/wordpress/android/ui/suggestion/SuggestionActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.view.inputmethod.EditorInfo
1212
import androidx.activity.addCallback
1313
import org.greenrobot.eventbus.EventBus
1414
import org.greenrobot.eventbus.Subscribe
15+
import org.greenrobot.eventbus.ThreadMode
1516
import org.wordpress.android.R
1617
import org.wordpress.android.WordPress
1718
import org.wordpress.android.databinding.SuggestUsersActivityBinding
@@ -260,7 +261,7 @@ class SuggestionActivity : BaseAppCompatActivity() {
260261
super.onPause()
261262
}
262263

263-
@Subscribe
264+
@Subscribe(threadMode = ThreadMode.MAIN)
264265
fun onEventMainThread(event: ConnectionChangeEvent) {
265266
viewModel.onConnectionChanged(event)
266267
updateEmptyView()

0 commit comments

Comments
 (0)