Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mob 4998 connectivity manager offline mode improvements #476

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Ayyanchira
Copy link
Member

@Ayyanchira Ayyanchira commented Oct 20, 2022

🔹 Jira Ticket(s) if any

✏️ Description

Improvements in ConnectivityManager for offline mode.


  1. Default value to isConnected is set to true.

  2. Extra conditions for when to set isConnected false.
    Device can be connected to multiple network at once. Usually Wifi and cellular. Previous implementation would switch SDK mode to offline if either of the one got disconnected, thereby falsely picturing device being offline.
    This implementation takes multiple network into consideration and saves them in a Set. Only if all the network are disconnected, only then isConnected is flipped to false.
    onCapabilitiesChanged is another callback that can give even granular controls over if internet is actually present or not, but to keep it simple, this is a good improvement to existing functionality of offline mode.

Ayyanchira and others added 2 commits October 19, 2022 16:14
Some improvements in ConnectivityManager.

1. By default, isConnected is set to true. to avoid situations of db bloat up and no listener registered. This will also make sure that an app restart will eventually reset the flag, in case where internet was available but callbacks were not registered.
2. ConnectivityManager also listens to onCapabilitiesChanged, which can indicate more granular detail of if internet is available or not. isConnected is set to true as soon as internet is available.
Currently, opposite case is not true as during testing, it has been seen as when one network out of the two gets disconnected, sometimes, false callbacks are made and another callback with internet available is not called.

Added a TODO statement which captures even larger context to make ConnectivityManager even better. Might have to store multiple network that device is connected to and as long as one exists isConnected switch can remain ON.
Device can be connected to multiple network at once. Mostly Wifi and cellular. Previous implementation would switch back to offline if either of the one got disconnected, thereby falsely picturing device being offline.
This implementation takes multiple network into consideration and saves them in a Set. Only if all the network are disconnected, only then `isConnected` is flipped to false.
onCapabilitiesChanged is another callback that can give even granular controls over if internet is actually present or not, but to keep it simple, this is a good improvement to existing functionality of offline mode.
@Ayyanchira Ayyanchira force-pushed the MOB-4998-ConnectivityManager-OfflineMode-improvements branch from 396512b to 357973f Compare October 20, 2022 23:58
@codecov
Copy link

codecov bot commented Oct 21, 2022

Codecov Report

Attention: Patch coverage is 43.47826% with 13 lines in your changes are missing coverage. Please review.

Project coverage is 61.38%. Comparing base (58dcf29) to head (2d3c066).

Files Patch % Lines
...terableapi/IterableNetworkConnectivityManager.java 43.47% 11 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #476      +/-   ##
==========================================
- Coverage   61.51%   61.38%   -0.14%     
==========================================
  Files          75       75              
  Lines        4695     4710      +15     
  Branches      533      535       +2     
==========================================
+ Hits         2888     2891       +3     
- Misses       1507     1515       +8     
- Partials      300      304       +4     

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

@Ayyanchira Ayyanchira marked this pull request as draft November 1, 2022 13:30
@Ayyanchira Ayyanchira marked this pull request as ready for review November 3, 2022 07:34
1. Using `connectivityManager.getNetworkCapabilities` instead of `networkRequest.hasCapability` which was giving incorrect values before.
2. ConnectivityManager is now global private variable.
3. Logic of checking initial state of connectivity is modularized and moved to constructor.
@Ayyanchira Ayyanchira force-pushed the MOB-4998-ConnectivityManager-OfflineMode-improvements branch from e2fe72c to 1fc4fc4 Compare November 16, 2022 20:57
private void checkInternetAvailabilityOnActiveNetwork() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network activeNetwork = connectivityManager.getActiveNetwork();
isConnected = activeNetwork == null ? false : connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
Copy link
Contributor

@vbabenkoru vbabenkoru Nov 16, 2022

Choose a reason for hiding this comment

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

I would also wrap all of this stuff in a try...catch. It should be safe enough, but this is Android, who knows if it's going to blow up on some obscure Android device. 😀

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍. Done

Copy link
Contributor

@evantk91 evantk91 left a comment

Choose a reason for hiding this comment

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

LGTM

@Ayyanchira
Copy link
Member Author

This can be merged once its verified working on older version of Android SDK

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.

5 participants