$30 off During Our Annual Pro Sale. View Details »

Is your app really connected?

Is your app really connected?

Presentation about connectivity in the Android applications shown during DroidCon Poland in Kraków on 01.12.2017.

Piotr Wittchen

December 01, 2017
Tweet

More Decks by Piotr Wittchen

Other Decks in Programming

Transcript

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

    View Slide

  2. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. 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?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. 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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. WiFi network can be disconnected from the Internet

    View Slide

  16. Handling Internet connectivity
    Executed in a separate non-UI thread

    View Slide

  17. 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)

    View Slide

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

    View Slide

  19. View Slide

  20. 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

    View Slide

  21. 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

    View Slide

  22. 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

    View Slide

  23. View Slide

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

    View Slide

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

    View Slide

  26. 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)

    View Slide

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

    View Slide

  28. The simplest example

    View Slide

  29. The simplest example

    View Slide

  30. The simplest example

    View Slide

  31. The simplest example

    View Slide

  32. Filtering connection types

    View Slide

  33. Filtering connection types

    View Slide

  34. Filtering connection types

    View Slide

  35. 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.

    View Slide

  36. Internet observing strategies
    is implemented by:

    View Slide

  37. Observing Internet Connectivity (periodically)

    View Slide

  38. Observing Internet Connectivity (periodically)

    View Slide

  39. Observing Internet Connectivity (periodically)

    View Slide

  40. Observing Internet Connectivity (periodically)

    View Slide

  41. Checking Internet Connectivity (only once)

    View Slide

  42. Checking Internet Connectivity (only once)

    View Slide

  43. 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

    View Slide

  44. Chaining stream of network and Internet connectivity

    View Slide

  45. Chaining stream of network and Internet connectivity

    View Slide

  46. Chaining stream of network and Internet connectivity

    View Slide

  47. Chaining stream of network and Internet connectivity

    View Slide

  48. Chaining stream of network and Internet connectivity

    View Slide

  49. Chaining stream of network and Internet connectivity

    View Slide

  50. Chaining stream of network and Internet connectivity

    View Slide

  51. Chaining stream of network and Internet connectivity

    View Slide

  52. Integration with OkHttp

    View Slide

  53. Integration with OkHttp

    View Slide

  54. Integration with OkHttp

    View Slide

  55. Integration with OkHttp

    View Slide

  56. Integration with OkHttp

    View Slide

  57. Integration with OkHttp

    View Slide

  58. Integration with OkHttp

    View Slide

  59. Integration with OkHttp

    View Slide

  60. Integration with OkHttp

    View Slide

  61. Integration with OkHttp

    View Slide

  62. Integration with OkHttp

    View Slide

  63. Integration with OkHttp

    View Slide

  64. Integration with Retrofit

    View Slide

  65. Integration with Retrofit

    View Slide

  66. Integration with Retrofit

    View Slide

  67. Integration with Retrofit

    View Slide

  68. Integration with Retrofit

    View Slide

  69. Integration with Retrofit

    View Slide

  70. Integration with Retrofit

    View Slide

  71. Integration with Retrofit

    View Slide

  72. Integration with Retrofit

    View Slide

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

    View Slide

  74. https://github.com/pwittchen/RxBattery

    View Slide

  75. Respecting battery state

    View Slide

  76. Respecting battery state

    View Slide

  77. Respecting battery state

    View Slide

  78. Respecting battery state

    View Slide

  79. Respecting battery state

    View Slide

  80. Respecting battery state

    View Slide

  81. Respecting battery state

    View Slide

  82. Respecting battery state

    View Slide

  83. Respecting battery state

    View Slide

  84. Integration with any code based on RxJava is possible

    View Slide

  85. 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

    View Slide

  86. 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

    View Slide

  87. Is anyone using this library now?

    View Slide

  88. Is anyone using this library now?

    View Slide

  89. Is anyone using this library now?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide