Skip to content

Drop EventBus from connectivity delivery (use LiveData) - #23141

Closed
nbradbury wants to merge 8 commits into
trunkfrom
issue/remove-connectivity-event-bus
Closed

Drop EventBus from connectivity delivery (use LiveData)#23141
nbradbury wants to merge 8 commits into
trunkfrom
issue/remove-connectivity-event-bus

Conversation

@nbradbury

@nbradbury nbradbury commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

What

Stacked on #23140. Removes the ConnectionChangeEvent-over-EventBus delivery that #23140 kept for connectivity, and exposes the connected state as a lifecycle-aware LiveData<Boolean> on NetworkConnectionMonitor that subscribers observe directly.

Why

EventBus for a single connectivity signal is legacy global-bus plumbing: untyped dispatch, manual register/unregister with leak risk, and no current-value semantics. The monitor is already a Dagger @Singleton and every subscriber is a LifecycleOwner with field injection, so LiveData is a better fit — automatic lifecycle-scoped observation and current-value replay on subscribe.

Testing

Smoke-tested on an emulator (toggle airplane mode):

  • Main screen "No connection" bar shows/hides on connectivity change (and shows correct state on open).
  • Blog Settings: editing disabled + "disconnected" toast when going offline; re-enabled on reconnect (no toast on reconnect).
  • Reader search: results resubmit after reconnecting.

Replace the deprecated CONNECTIVITY_ACTION BroadcastReceiver
(ConnectionChangeReceiver) with a ConnectivityManager.NetworkCallback
(NetworkConnectionMonitor) registered on a background thread. Connectivity
checks and the EventBus dispatch no longer run on the main thread, and a
NetworkCallback is not a broadcast so it cannot trip the background-broadcast
ANR timeout. The monitor is registered once for the process lifetime, dropping
the unreliable foreground/background register/unregister workaround.

The ConnectionChangeEvent contract is preserved, so subscribers are unchanged
apart from the renamed enclosing type.

Sentry: JETPACK-ANDROID-PB9
- 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.
The monitor is registered once for the whole process lifetime and never torn
down, so stop() had no callers. Removing it also lets the handlerThread and
networkCallback fields go — they were only read by stop(). connectivityManager
stays since hasActiveConnection() uses it.
Collapse hasActiveConnection's guard clauses into a safe-call chain so it has
two returns instead of four. No behavior change.
Fix 2 re-queried ConnectivityManager.getActiveNetwork() in onLost to avoid
misreporting a network handover as a disconnect. On real devices getActiveNetwork()
can still return the network being torn down at that instant, so a genuine
disconnect was suppressed by the de-dupe, leaving wasConnected stuck true. The
"No connection" bar then only updated on the next onResume poll, not live.

Track the set of available internet-capable networks instead (registerNetworkCallback
with a NET_CAPABILITY_INTERNET request): a handover adds the replacement before
removing the old network so the set never empties (no false disconnect), while a
real disconnect empties it reliably. Removes the fragile getActiveNetwork() query.
Clarify that the available-networks set only stays non-empty during a handover
when the replacement network is already up; no code change.
Drop the ConnectionChangeEvent-over-EventBus delivery introduced with
NetworkConnectionMonitor and expose the connected state as a
LiveData<Boolean> that subscribers observe via their lifecycle owner.
Removes EventBus entirely from SuggestionActivity and
BlogPreferencesActivity; the other subscribers keep EventBus for
unrelated events.
@dangermattic

Copy link
Copy Markdown
Collaborator
1 Message
📖 This PR is still a Draft: some checks will be skipped.

Generated by 🚫 Danger

@wpmobilebot

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in WordPress Android by scanning the QR code below to install the corresponding build.

App NameWordPress Android
Build TypeDebug
Versionpr23141-8f8b0cd
Build Number1498
Application IDorg.wordpress.android.prealpha
Commit8f8b0cd
Installation URL22qfibt5aalpg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in Jetpack Android by scanning the QR code below to install the corresponding build.

App NameJetpack Android
Build TypeDebug
Versionpr23141-8f8b0cd
Build Number1498
Application IDcom.jetpack.android.prealpha
Commit8f8b0cd
Installation URL1t9uct2gv7sso
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.82%. Comparing base (5f85a77) to head (8f8b0cd).

Files with missing lines Patch % Lines
...ess/android/networking/NetworkConnectionMonitor.kt 0.00% 3 Missing ⚠️
Additional details and impacted files
@@                      Coverage Diff                      @@
##           issue/network-changed-anr   #23141      +/-   ##
=============================================================
- Coverage                      37.82%   37.82%   -0.01%     
=============================================================
  Files                           2344     2344              
  Lines                         127449   127450       +1     
  Branches                       17694    17694              
=============================================================
- Hits                           48210    48209       -1     
- Misses                         75282    75284       +2     
  Partials                        3957     3957              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nbradbury
nbradbury changed the base branch from issue/network-changed-anr to trunk July 23, 2026 19:11
@nbradbury

Copy link
Copy Markdown
Contributor Author

Closing this in favor of including these changes in #23140

@nbradbury nbradbury closed this Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants