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

Use NetworkCallback to monitor connectivity changes on newer API levels #501

Merged
merged 12 commits into from
Jun 28, 2019

Conversation

fractalwrench
Copy link
Contributor

@fractalwrench fractalwrench commented Jun 14, 2019

Goal

We rely on the ConnectivityManager to detect whether or not we have a network connection. This allows us to save battery by avoiding unnecessary radio use when there is no available connection.

Several APIs in NetworkInfo (returned by ConnectivityManager) have been deprecated when targeting API 28 - namely isConnectedOrConnecting(), and getType() which sets the device.networkStatus field.

This changeset refactors the notifier to use modern APIs where possible, and otherwise falls back to the legacy behaviour on API < N.

This PR also adds Kotlin as a dependency to the bugsnag-android artefact.

Changeset

  • Added Kotlin stdlib as a compile-time dependency
  • Enabled allWarningsAsErrors for Kotlin compilation, which fails the build when using deprecated code
  • Created a Connectivity interface that encapsulates all methods related to network state
  • Created a ConnectivityCompat shim that forwards method calls onto the modern or legacy implementation, dependent on API level

Within the Connectivity implementations:

  • Created a higher order function as a constructor parameter, which is invoked whenever the network connection changes
  • Registered a BroadcastReceiver to listen to network changes on API < N, and a NetworkCallback otherwise
  • Used networkInfo.type to get network state on API < N, and activeNetwork otherwise

Tests

Verified that reports are cached then automatically flushed when a device is put into airplane mode, on API 27 + API 22 physical devices, and that device.networkStatus is populated correctly.

Note: the unit tests currently fail locally due to many client objects being created. The fix for this is implemented in #503.

Copy link
Contributor

@bengourley bengourley left a comment

Choose a reason for hiding this comment

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

Verified that reports are cached then automatically flushed when a device is put into airplane mode, on API 27 + API 22 physical devices, and that device.networkStatus is populated correctly.

I did this same manual check on API 29 and API 26.

Read through the code and mostly understood it. Looks sensible. Yay for Kotlin!

@fractalwrench fractalwrench merged commit beb4749 into next Jun 28, 2019
@fractalwrench fractalwrench deleted the use-network-callback branch June 28, 2019 13:32
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