Skip to content

Conversation

@Beerosagos
Copy link
Collaborator

Our MainActivity code is quite complicated and mixes a lot of different things. This change moves the code related to network management (e.g. to display alerts when the network is not available) to a dedicated helper class and declutters the main activity a bit.

@Beerosagos Beerosagos marked this pull request as ready for review November 6, 2025 15:25
@Beerosagos Beerosagos requested a review from benma November 6, 2025 15:25
Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if the 21->23 bump commit would have a commit message body explaining the reason.

private final BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Mobileserver.usingMobileDataChanged();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is deleted with no replacement, so it seems the mobile data warning banner will never be shown?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in my tests it was displayed anyway. But in the meantime I have heavily refactored the code anyway.

@Beerosagos Beerosagos changed the title frontend/android: move network related code to dedicated class frontend/android: refactor network related code Nov 12, 2025
@Beerosagos Beerosagos force-pushed the android-helpers branch 3 times, most recently from dfb5813 to 2383f8b Compare November 12, 2025 11:19
@Beerosagos
Copy link
Collaborator Author

@benma I upgraded min sdk to 24 to match the code requirements and tweaked a bit the PR to make the whole feature more reliable. PTAL

Comment on lines -69 to -74
// To avoid false positives, if we can't obtain connectivity info,
// we return true.
// Note: this should never happen per Android documentation, as:
// - these can not be null it come from the onCapabilitiesChanged callback.
// - when obtained with getNetworkCapabilities(network), they can only be null if the
// network is null or unknown, but we guard against both in the caller.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment and the comment below are very useful, any reason you did not copy them to the new file like the test of this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this applies anymore in the proposed implementation: checkConnectivity() gets the active network and verifies its capabilities. We don't have guaranties that that network capabilities are not null, and if they are null we should consider it a signal of missing connectivity, not a false positive.

I tested this implementation on various Android versions and I never experienced false positives (or negatives) with this approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the comment below, about the VALIDATED capability: I don't think that the comment matches closely the API docs. I wanted to replace it but eventually I forgot about it, I'll make a new one trying to clarify 🙏


## Unreleased
- Add feedback link to guide and about settings
- Android: fix connectivity misdetection when switching between WIFI and cellular network.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention dropped support for Android <6 too?

Util.log("hasInternetConnectivity");
if (capabilities == null) {
Util.log("hasInternetConnectivity: null capabilities");
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original PR where @bznein introduced the connectivity check, we carefully returned true in all the failure cases, so that the user does not get false banners about being offline:

// To avoid false positives, if we can't obtain connectivity info,
// we return true.

It seems you have changed back all these defaults. Please change it back.

See also

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered in my other comment about this 😇

// Fetches the active network and verifies if that provides internet access.
public void checkConnectivity() {
if (connectivityManager == null) {
Mobileserver.setOnline(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, it makes sense here to behave as if we are connected 👍 Thanks

Some of the calls used in the code (e.g. those that fetch the native
locale of the device) require sdk versions 24.
Our MainActivity code is quite complicated and mixes a lot of different
things. This change moves the code related to network management (e.g. to
display alerts when the network is not available) to a dedicated
helper class and declutters the main activity a bit.

It also fixes a bug that caused the app to misdetect mobile connection
or missing connectivity when switching between WIFI and cellular
connection. The bug was caused by the fact that the "onLost" call is not
guaranteed to be executed after the network is actually lost. This can
bring both to false positive and false negatives results, depending on
race conditions. This caused to occasionally not display the "connection
lost" banner when needed, or to e.g. display both the "connection lost" and
the "mobile connection" banner at the same time.

Adding a 250ms delay in the execution of the "onLost" handler is not
nice, but seems to reliably fix the issue on all the tested Android
versions.

This also drops the `netWorkStateReceiver`, as it was registered as a
broadcast receiver for the `CONNECTIVITY_ACTION`, which is now
deprecated.
See https://developer.android.com/reference/android/net/ConnectivityManager#CONNECTIVITY_ACTION

The `usingMobileDataChanged` call, which is used to force the mobile
server to fetch again the network source (wifi/mobile) and optionally
display the "mobile connection" banner, is moved inside the
`checkNetworkConnectivity` call, so that it's executed every time we are
reassessing the connectivity status.
@Beerosagos
Copy link
Collaborator Author

@benma I updated the PR, added some more details to the commit message and hopefully answered all the comments :) PTAL 🙏

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@Beerosagos Beerosagos merged commit 0c2c303 into BitBoxSwiss:master Nov 19, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants