Commit 3ab17e4
authored
Fix background ANR from connectivity broadcast receiver (#23140)
* Fix background ANR from connectivity broadcast receiver
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
* 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.
* Simplify: drop unused stop() from NetworkConnectionMonitor
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.
* Fix detekt ReturnCount in NetworkConnectionMonitor
Collapse hasActiveConnection's guard clauses into a safe-call chain so it has
two returns instead of four. No behavior change.
* Fix connection bar not updating live on connectivity 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.
* Soften handover doc comment in NetworkConnectionMonitor
Clarify that the available-networks set only stays non-empty during a handover
when the replacement network is already up; no code change.
* Replace connectivity EventBus with LiveData
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.
* Simplify connectivity transition guards to primitive booleans
* Add unit tests for NetworkConnectionMonitor
Extract the NetworkCallback logic into @VisibleForTesting onNetworkAvailable/
onNetworkLost so the connected-state model can be tested without the Android
framework wiring in start(). Add NetworkConnectionMonitorTest covering: connected
on first network, disconnected when the last network is lost, no false disconnect
during a handover, de-dupe when unchanged, and re-emit on reconnect.
Satisfies the Danger check requiring tests for the new class.
* Guard connectivity observers against LiveData replay side effects
LiveData replays the current value to each new observer, so a side effect that
should only run on an offline->online transition also fired on open.
- SuggestionActivity: only call viewModel.onConnectionChanged (which refreshes
suggestions) on a genuine offline->online transition. The initial replay was
triggering a redundant fetch on top of the one started in viewModel.init().
- ReaderPostListFragment: initialise lastConnected to true so the first replayed
"connected" value doesn't re-run the search (e.g. on a restored fragment with
an active search); only a real transition resubmits.1 parent 9e0dba0 commit 3ab17e4
12 files changed
Lines changed: 229 additions & 154 deletions
File tree
- WordPress/src
- main/java/org/wordpress/android
- networking
- ui
- main
- posts
- prefs
- reader
- suggestion
- test/java/org/wordpress/android
- networking
- ui/suggestion
Lines changed: 9 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | | - | |
20 | 17 | | |
21 | 18 | | |
22 | 19 | | |
| |||
67 | 64 | | |
68 | 65 | | |
69 | 66 | | |
70 | | - | |
| 67 | + | |
71 | 68 | | |
72 | 69 | | |
73 | 70 | | |
| |||
92 | 89 | | |
93 | 90 | | |
94 | 91 | | |
95 | | - | |
96 | 92 | | |
97 | 93 | | |
98 | 94 | | |
| |||
148 | 144 | | |
149 | 145 | | |
150 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
151 | 150 | | |
152 | 151 | | |
153 | 152 | | |
| |||
339 | 338 | | |
340 | 339 | | |
341 | 340 | | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
342 | 346 | | |
343 | 347 | | |
344 | 348 | | |
| |||
792 | 796 | | |
793 | 797 | | |
794 | 798 | | |
795 | | - | |
796 | 799 | | |
797 | 800 | | |
798 | 801 | | |
| |||
845 | 848 | | |
846 | 849 | | |
847 | 850 | | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | 851 | | |
861 | 852 | | |
862 | 853 | | |
| |||
876 | 867 | | |
877 | 868 | | |
878 | 869 | | |
879 | | - | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
885 | | - | |
886 | | - | |
887 | | - | |
888 | | - | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | 870 | | |
899 | 871 | | |
900 | 872 | | |
| |||
Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
Lines changed: 5 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| 282 | + | |
| 283 | + | |
282 | 284 | | |
283 | 285 | | |
284 | 286 | | |
| |||
414 | 416 | | |
415 | 417 | | |
416 | 418 | | |
| 419 | + | |
| 420 | + | |
417 | 421 | | |
418 | 422 | | |
419 | 423 | | |
| |||
1488 | 1492 | | |
1489 | 1493 | | |
1490 | 1494 | | |
1491 | | - | |
1492 | | - | |
1493 | | - | |
1494 | | - | |
1495 | | - | |
1496 | | - | |
1497 | 1495 | | |
1498 | 1496 | | |
1499 | 1497 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| |||
412 | 412 | | |
413 | 413 | | |
414 | 414 | | |
| 415 | + | |
| 416 | + | |
415 | 417 | | |
416 | 418 | | |
417 | 419 | | |
| |||
558 | 560 | | |
559 | 561 | | |
560 | 562 | | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
561 | 566 | | |
562 | 567 | | |
563 | 568 | | |
| |||
3858 | 3863 | | |
3859 | 3864 | | |
3860 | 3865 | | |
3861 | | - | |
3862 | | - | |
3863 | | - | |
3864 | | - | |
3865 | | - | |
3866 | 3866 | | |
3867 | 3867 | | |
3868 | 3868 | | |
| |||
0 commit comments