Da Real Fragmentation Explained Codemotion 2016

Da Real Fragmentation Explained Codemotion 2016

Nowadays, fragmentation in Android is a fact. As developers, we have to deal constantly with random behaviors between different versions of the operating system, incomplete ROMs, different hardware qualities, manufacturers that modify the specs of some APIs or even ignore them... That’s “Da real fragmentation” and it makes development much more difficult. In addition, if you have background processes running, your life becomes a nightmare!

In this session we will talk about the problems that we found along the way creating an app which needs to keep track of sensor signals in background. We will review how to deal with the different versions of alarms in Android, how to live with Doze mode and other battery optimization apps and we will show you some hardware and software peculiarities of different manufacturers.

Apart from that, we will explain how we have solved such fragmentation issues in order to develop an app that works properly in as many devices as possible.

Da Real Fragmentation series
Alarms: http://pguardiola.com/blog/darealfragmentation-alarms/
Doze: http://pguardiola.com/blog/darealfragmentation-doze/
Sensors: http://pguardiola.com/blog/darealfragmentation-sensors/
Repository: https://github.com/Guardiola31337/darealfragmentation

3c2bdd16c0ea8511dc254b8497a06f78?s=128

Pablo Guardiola

November 18, 2016
Tweet

Transcript

  1. DA REAL FRAGMENTATION EXPLAINED

  2. HELLO! Pablo Guardiola @Guardiola31337 Raúl Román @rromanl

  3. None
  4. ALARMS

  5. ∎ postDelayed() ∎ AlarmManager ∎ JobScheduler (API 21+) SCHEDULE A

    TASK
  6. ∎ Wake up ∎ Rate (Repeating/One-shot) ∎ Precision (Exact/Inexact) ∎

    Clock (Elapsed Real Time/RTC) OPTIONS
  7. ∎ set() API 1 ∎ setExact() API 19 ∎ setWindow()

    API 19 ∎ setRepeating() API 1: Type, trigger time (ms), interval, PendingIntent METHODS
  8. ∎ setInexactRepeating() API 3 ∎ setAlarmClock() API 21 ∎ setAndAllowWhileIdle()

    API 23 ∎ setExactAndAllowWhileIdle() API 23 METHODS
  9. PROBLEM SDK Fragmentation ∎ A bunch of APIs do the

    same ∎ Names of methods almost identical
  10. SOLUTIONS ∎ Master AlarmManager ∎ evernote/android-job

  11. PROBLEM <5 minutes repeating alarms NOT ALLOWED

  12. Samsung Galaxy J5

  13. None
  14. Alarm Set at mar. ago. 09 13:08:23 CEST 2016 Alarm

    received at mar. ago. 09 13:15:00 CEST 2016 Alarm received at mar. ago. 09 13:15:00 CEST 2016 Alarm received at mar. ago. 09 13:15:41 CEST 2016 Alarm received at mar. ago. 09 13:20:41 CEST 2016 Alarm received at mar. ago. 09 13:25:40 CEST 2016 Alarm received at mar. ago. 09 13:30:41 CEST 2016 Alarm received at mar. ago. 09 13:30:41 CEST 2016 Alarm received at mar. ago. 09 13:35:41 CEST 2016 Xiaomi Redmi Note 2
  15. SOLUTIONS ∎ One-shot alarms ∎ setAlarmClock() ∎ Malfunctioning alarms detectors

    ∎ Sticky services
  16. SENSORS

  17. CATEGORIES ∎ Motion ∎ Environmental ∎ Position

  18. TYPES ∎ Hardware-based ∎ Software-based

  19. FRAMEWORK SensorManager ∎ Instance sensor service ∎ Access and list

    sensors ∎ Register/Unregister listeners ∎ Data acquisition rates ∎ Sensor accuracy constants ∎ Acquire orientation
  20. FRAMEWORK SensorEventListener ∎ onSensorChanged() ∎ onAccuracyChanged()

  21. FRAMEWORK SensorEvent ∎ Raw sensor data (values) ∎ Type of

    sensor ∎ Accuracy ∎ Timestamp
  22. BATCHING API 19+ Batch period (maxReportLatencyUs)

  23. PROBLEM Delay you specify is only a SUGGESTED delay

  24. PROBLEM Samples NEVER evenly spaced

  25. Huawei P7

  26. Bq Aquaris E5

  27. None
  28. SOLUTION LOWER / HIGHER frequency

  29. SOLUTION RESAMPLER ∎ Interpolator ∎ Extrapolator

  30. PROBLEM GARBAGE DATA until sensor become stable

  31. Jiayu S3

  32. SOLUTION THROW AWAY initial data

  33. PROBLEM Common sensors are NON-WAKE-UP

  34. Nexus 6

  35. SOLUTION Acquire and release WAKELOCKS wisely

  36. PROBLEM EPOCH timestamps

  37. 1479140553176;1479140553136657000;-0.158000;-0.016000;9.647000 1479140553196;1479140553196627000;0.462000;-1.719000;3.162000 1479140553396;1479140553396613000;-0.179000;-0.006000;9.566000 1479140553596;1479140553596599000;-0.175000;0.002000;9.599000 Epoch (Jiayu S3) 1479140749449;935794384747;0.172383;0.373495;9.155427 1479140749651;935994876413;0.181959;0.335188;9.117120 1479140749850;936195380007;0.153229;0.325611;9.136273

    1479140750052;936396651360;0.162806;0.363919;9.097966 Elapsed time (Moto X 2nd Gen.)
  38. None
  39. SOLUTION BASE TIME + difference

  40. PROBLEM BACKJUMPS

  41. Nexus 5

  42. None
  43. SOLUTION REARRANGE base time of the following samples

  44. PROBLEM SIGNAL noise

  45. Jiayu S3

  46. SOLUTION ADAPTIVE filters

  47. PROBLEM OFFSET/Drifting

  48. SOLUTION CALIBRATION

  49. PROBLEM System will NOT disable sensors AUTOMATICALLY

  50. SOLUTION DISABLE sensors you don’t need anymore

  51. PROBLEM Sensor COORDINATE SYSTEM based on device NATURAL ORIENTATION

  52. SOLUTION ∎ getRotation() ∎ remapCoordinateSystem() ∎ getOrientation() ∎ getRotationMatrix()

  53. PROBLEM NOT safe to hold onto SensorEvent past onSensorChanged()

  54. SOLUTION COPY values from SensorEvent

  55. POWER-SAVING MODES

  56. DOZE API 23+ ∎ AlarmManager ∎ JobScheduler ∎ SyncManager ∎

    Network ∎ ...
  57. DEEP-DOZE Android 6.0+ ∎ Unplugged ∎ Screen off ∎ No

    motion detected
  58. None
  59. LIGHT-DOZE Android 7.0+ ∎ Unplugged ∎ Screen off ∎ Not

    necessarily stationary
  60. None
  61. None
  62. APP STANDBY API 23+ ∎ User not using the app

    ∎ App not in foreground ∎ No notifications
  63. SMART MANAGER (Samsung) ∎ Always optimizing ∎ Optimizing when not

    used for 3 days ∎ Turn off
  64. PROBLEMS ∎ Two battery optimization modes simultaneously ∎ Task killers

    ∎ Optimized by default
  65. SOLUTIONS ∎ Malfunctioning detectors ∎ Persistency ∎ Logs ∎ Whitelist

    ∎ Prevent the app to enter
  66. PROBLEM ALARMS Alarms methods of Android 5.1- when in Doze

    (Android 6.0+ devices) WON’T work properly
  67. Nexus 6 (3 min. repeating alarm)

  68. PROBLEM ALARMS Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire more than

    once per 9 minutes, per app
  69. Nexus 6 setAndAllowWhileIdle() (3 min. repeating alarm)

  70. SOLUTIONS ∎ setAndAllowWhileIdle() ∎ setExactAndAllowWhileIdle() ∎ setAlarmClock() ∎ Firebase Cloud

    Messaging
  71. SOLUTIONS ∎ Whitelist (Only wakelocks and network) ∎ Foreground services

    ∎ Hope
  72. PROBLEM SENSORS Normally in idle modes you WON’T receive samples

  73. SOLUTIONS ∎ Detect real frequency and increase it: □ Reregistering

    □ Preventing idle mode
  74. CONCLUSIONS ∎ EXPECT THE UNEXPECTED! ∎ FRAGMENTATION IS A FACT

    ∎ DEFENSIVE CODE
  75. None
  76. THANKS! Any questions?

  77. References ∎ Da Real Fragmentation series (http://pguardiola.com) ∎ Da Real

    Fragmentation GitHub repository (https://github.com/Guardiola31337/darealfragmentation) ∎ Presentation template by SlidesCarnival