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.

E8bb35e5d8c868e7dffcd9859248d6f1?s=128

Piotr Wittchen

December 01, 2017
Tweet

Transcript

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

  2. None
  3. Does your app call any web API? Sure. Almost every

    mobile app does.
  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 ;-(
  5. What can we do about it? Source: https://developer.android.com/training/basics/network-ops/managing.html Is that

    enough?
  6. Of course not It would be too easy. Don’t you

    think so? :-)
  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?
  8. Let’s use the BroadcastReceiver! Now, we can react on the

    connectivity changes in real time
  9. Lollipop arrives! We got a new shiny Android version! Hooray!

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

    But wait… Network Monitoring API has changed.
  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.
  12. Handling different connection types https://developer.android.com/training/basics/network-ops/managing.html

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

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

  15. WiFi network can be disconnected from the Internet

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

  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)
  18. Checking Internet connectivity in the Walled Garden Executed in a

    separate non-UI thread
  19. None
  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
  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
  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
  23. None
  24. So what should I do now? You can implement all

    of that stuff by yourself or...
  25. You can use ReactiveNetwork library! It will do all the

    work for you
  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)
  27. ReactiveNetwork does not force you to use any concrete solution

    or strategy Pick whatever you need from it or extend it
  28. The simplest example

  29. The simplest example

  30. The simplest example

  31. The simplest example

  32. Filtering connection types

  33. Filtering connection types

  34. Filtering connection types

  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.
  36. Internet observing strategies is implemented by:

  37. Observing Internet Connectivity (periodically)

  38. Observing Internet Connectivity (periodically)

  39. Observing Internet Connectivity (periodically)

  40. Observing Internet Connectivity (periodically)

  41. Checking Internet Connectivity (only once)

  42. Checking Internet Connectivity (only once)

  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
  44. Chaining stream of network and Internet connectivity

  45. Chaining stream of network and Internet connectivity

  46. Chaining stream of network and Internet connectivity

  47. Chaining stream of network and Internet connectivity

  48. Chaining stream of network and Internet connectivity

  49. Chaining stream of network and Internet connectivity

  50. Chaining stream of network and Internet connectivity

  51. Chaining stream of network and Internet connectivity

  52. Integration with OkHttp

  53. Integration with OkHttp

  54. Integration with OkHttp

  55. Integration with OkHttp

  56. Integration with OkHttp

  57. Integration with OkHttp

  58. Integration with OkHttp

  59. Integration with OkHttp

  60. Integration with OkHttp

  61. Integration with OkHttp

  62. Integration with OkHttp

  63. Integration with OkHttp

  64. Integration with Retrofit

  65. Integration with Retrofit

  66. Integration with Retrofit

  67. Integration with Retrofit

  68. Integration with Retrofit

  69. Integration with Retrofit

  70. Integration with Retrofit

  71. Integration with Retrofit

  72. Integration with Retrofit

  73. Respecting battery state How to perform a data synchronization, while

    being connected to AC and WiFi network at the same time?
  74. https://github.com/pwittchen/RxBattery

  75. Respecting battery state

  76. Respecting battery state

  77. Respecting battery state

  78. Respecting battery state

  79. Respecting battery state

  80. Respecting battery state

  81. Respecting battery state

  82. Respecting battery state

  83. Respecting battery state

  84. Integration with any code based on RxJava is possible

  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
  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
  87. Is anyone using this library now?

  88. Is anyone using this library now?

  89. Is anyone using this library now?

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

  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 :-)
  92. Is your app really connected? Piotr Wittchen wittchen.io github.com/pwittchen Thank

    you for the attention! Questions?