Slide 1

Slide 1 text

Is your app really connected? Piotr Wittchen wittchen.io github.com/pwittchen

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Does your app call any web API? Sure. Almost every mobile app does.

Slide 4

Slide 4 text

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 ;-(

Slide 5

Slide 5 text

What can we do about it? Source: https://developer.android.com/training/basics/network-ops/managing.html Is that enough?

Slide 6

Slide 6 text

Of course not It would be too easy. Don’t you think so? :-)

Slide 7

Slide 7 text

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?

Slide 8

Slide 8 text

Let’s use the BroadcastReceiver! Now, we can react on the connectivity changes in real time

Slide 9

Slide 9 text

Lollipop arrives! We got a new shiny Android version! Hooray! But wait… Network Monitoring API has changed.

Slide 10

Slide 10 text

Lollipop arrives! We got a new shiny Android version! Hooray! But wait… Network Monitoring API has changed.

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

Handling different connection types https://developer.android.com/training/basics/network-ops/managing.html

Slide 13

Slide 13 text

Handling different connection types https://developer.android.com/training/basics/network-ops/managing.html

Slide 14

Slide 14 text

Handling different connection types https://developer.android.com/training/basics/network-ops/managing.html

Slide 15

Slide 15 text

WiFi network can be disconnected from the Internet

Slide 16

Slide 16 text

Handling Internet connectivity Executed in a separate non-UI thread

Slide 17

Slide 17 text

China banned the Google! China is behind the Walled Garden We need to ping other host than Google or verify HTTP Status 204 (No Content)

Slide 18

Slide 18 text

Checking Internet connectivity in the Walled Garden Executed in a separate non-UI thread

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

So what should I do now? You can implement all of that stuff by yourself or...

Slide 25

Slide 25 text

You can use ReactiveNetwork library! It will do all the work for you

Slide 26

Slide 26 text

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)

Slide 27

Slide 27 text

ReactiveNetwork does not force you to use any concrete solution or strategy Pick whatever you need from it or extend it

Slide 28

Slide 28 text

The simplest example

Slide 29

Slide 29 text

The simplest example

Slide 30

Slide 30 text

The simplest example

Slide 31

Slide 31 text

The simplest example

Slide 32

Slide 32 text

Filtering connection types

Slide 33

Slide 33 text

Filtering connection types

Slide 34

Slide 34 text

Filtering connection types

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

Internet observing strategies is implemented by:

Slide 37

Slide 37 text

Observing Internet Connectivity (periodically)

Slide 38

Slide 38 text

Observing Internet Connectivity (periodically)

Slide 39

Slide 39 text

Observing Internet Connectivity (periodically)

Slide 40

Slide 40 text

Observing Internet Connectivity (periodically)

Slide 41

Slide 41 text

Checking Internet Connectivity (only once)

Slide 42

Slide 42 text

Checking Internet Connectivity (only once)

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Chaining stream of network and Internet connectivity

Slide 45

Slide 45 text

Chaining stream of network and Internet connectivity

Slide 46

Slide 46 text

Chaining stream of network and Internet connectivity

Slide 47

Slide 47 text

Chaining stream of network and Internet connectivity

Slide 48

Slide 48 text

Chaining stream of network and Internet connectivity

Slide 49

Slide 49 text

Chaining stream of network and Internet connectivity

Slide 50

Slide 50 text

Chaining stream of network and Internet connectivity

Slide 51

Slide 51 text

Chaining stream of network and Internet connectivity

Slide 52

Slide 52 text

Integration with OkHttp

Slide 53

Slide 53 text

Integration with OkHttp

Slide 54

Slide 54 text

Integration with OkHttp

Slide 55

Slide 55 text

Integration with OkHttp

Slide 56

Slide 56 text

Integration with OkHttp

Slide 57

Slide 57 text

Integration with OkHttp

Slide 58

Slide 58 text

Integration with OkHttp

Slide 59

Slide 59 text

Integration with OkHttp

Slide 60

Slide 60 text

Integration with OkHttp

Slide 61

Slide 61 text

Integration with OkHttp

Slide 62

Slide 62 text

Integration with OkHttp

Slide 63

Slide 63 text

Integration with OkHttp

Slide 64

Slide 64 text

Integration with Retrofit

Slide 65

Slide 65 text

Integration with Retrofit

Slide 66

Slide 66 text

Integration with Retrofit

Slide 67

Slide 67 text

Integration with Retrofit

Slide 68

Slide 68 text

Integration with Retrofit

Slide 69

Slide 69 text

Integration with Retrofit

Slide 70

Slide 70 text

Integration with Retrofit

Slide 71

Slide 71 text

Integration with Retrofit

Slide 72

Slide 72 text

Integration with Retrofit

Slide 73

Slide 73 text

Respecting battery state How to perform a data synchronization, while being connected to AC and WiFi network at the same time?

Slide 74

Slide 74 text

https://github.com/pwittchen/RxBattery

Slide 75

Slide 75 text

Respecting battery state

Slide 76

Slide 76 text

Respecting battery state

Slide 77

Slide 77 text

Respecting battery state

Slide 78

Slide 78 text

Respecting battery state

Slide 79

Slide 79 text

Respecting battery state

Slide 80

Slide 80 text

Respecting battery state

Slide 81

Slide 81 text

Respecting battery state

Slide 82

Slide 82 text

Respecting battery state

Slide 83

Slide 83 text

Respecting battery state

Slide 84

Slide 84 text

Integration with any code based on RxJava is possible

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

Is anyone using this library now?

Slide 88

Slide 88 text

Is anyone using this library now?

Slide 89

Slide 89 text

Is anyone using this library now?

Slide 90

Slide 90 text

Contributions are welcome! https://github.com/pwittchen/ReactiveNetwork

Slide 91

Slide 91 text

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 :-)

Slide 92

Slide 92 text

Is your app really connected? Piotr Wittchen wittchen.io github.com/pwittchen Thank you for the attention! Questions?