What happens when the user wants to download some data? Scenario #1 1. User opens the app 2. Device is connected to the network 3. App downloads data from server 4. User can read data Senario #2 1. User opens the app 2. Device is not connected to the network 3. App tries to download data from server 4. ... 5. Connection timeout 6. Nothing happens or app crashes 7. User is not happy :-( Happy path :-D Unhappy path ;-(
Let’s see another use case 1. User opens an app 2. Device is disconnected from the network 3. Connectivity check fails 4. Device gets connected to the network 5. User cannot access the network with the app 6. User is unhappy :-( Unhappy path ;-( How to fix this situation?
Marshmallow arrives! We got a new shiny Android version! Hooray! But wait… There’s a Doze Mode. We need yet another BroadcastReceiver notifying app that network is down when the phone sleeps.
ClearText Traffic Starting with Android 9.0 (API level 28), cleartext support is disabled by default. We need to apply additional network security configuration in such cases. ● https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted ● https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted/50834 600#50834600
How often should we check Internet connectivity? It depends on our use case Continuous checking in a given interval is more reliable, but consumes more data transfer and drains the battery Sometimes one time check is enough
Mixing it all together Network BroadcastReceiver for pre-Lollipop devices Managing different connection types Network Callback from Lollipop Checking Internet connectivity for Walled Garden Doze mode Checking Internet connectivity with Socket Frequency of checking Internet connectivity Handling connectivity check asynchronously ClearText traffic
Why would I use it? ● ~3 years of development and maintenance ● 10 developers contributed to the code-base ● 41 releases so far ● More than 80% of code covered by unit tests ● Documentation, JavaDocs and exemplary apps ● Works with RxJava1.x and RxJava2.x (on separate artifacts and git branches) ● Up to date with the latest RxJava version (which is used as a dependency) ● Works with Java and Kotlin ● It’s free and open-source: https://github.com/pwittchen/ReactiveNetwork ● Backed by Travis Continuous Integration Server ● Verified by static code analysis tools (CheckStyle, PMD, FindBugs, Lint, NullAway) ● Tested in production ● Available on the Maven Central Repository (easy to use with Gradle)
Network observing strategies is implemented by: Correct strategy is chosen by the library automatically, but we can use any strategy explicitly if we want to.
Customization of the Internet connectivity check We can use and type from RxJava2 depending on our use case We can customize: ● host ● port ● timeout in ms ● initial observing interval in ms ● ping interval in ms ● strategy https://github.com/pwittchen/ReactiveNetwork#observing-internet-connectivity
ReactiveNetwork follows the official Google recommendations https://developer.android.com/develop/quality-guidelines/building-for-billions-connectivity.html https://developer.android.com/develop/quality-guidelines/building-for-billions-data-cost.html https://developer.android.com/training/basics/network-ops/managing.html Make your app usable offline Use less bandwidth on slower connections Detect network changes, then change app behavior
https://github.com/pwittchen/ReactiveNetwork/#who-is-using-this-library Is anyone using this library now? and others 5M+ downloads 10M+ downloads 50k+ downloads 1k+ downloads 500k+ downloads
Summary ● We need to be aware of network and Internet connectivity ● Apps can work off-line ● Apps can be disconnected from the network anytime ● WiFi network can be disconnected from the Internet anytime ● Battery of the device should not be drained ● Mobile data transfer should be used carefully ● We should provide the best User Experience possible ● Do not reinvent the wheel - we have libraries :-)