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

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

Pablo Guardiola

November 18, 2016
Tweet

More Decks by Pablo Guardiola

Other Decks in Programming

Transcript

  1. DA REAL
    FRAGMENTATION
    EXPLAINED

    View Slide

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

    View Slide

  3. View Slide

  4. ALARMS

    View Slide

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

    View Slide

  6. ∎ Wake up
    ∎ Rate (Repeating/One-shot)
    ∎ Precision (Exact/Inexact)
    ∎ Clock (Elapsed Real Time/RTC)
    OPTIONS

    View Slide

  7. ∎ set() API 1
    ∎ setExact() API 19
    ∎ setWindow() API 19
    ∎ setRepeating() API 1:
    Type, trigger time (ms), interval,
    PendingIntent
    METHODS

    View Slide

  8. ∎ setInexactRepeating() API 3
    ∎ setAlarmClock() API 21
    ∎ setAndAllowWhileIdle() API 23
    ∎ setExactAndAllowWhileIdle() API 23
    METHODS

    View Slide

  9. PROBLEM
    SDK Fragmentation
    ∎ A bunch of APIs do the same
    ∎ Names of methods almost
    identical

    View Slide

  10. SOLUTIONS
    ∎ Master AlarmManager
    ∎ evernote/android-job

    View Slide

  11. PROBLEM
    <5 minutes repeating
    alarms
    NOT ALLOWED

    View Slide

  12. Samsung Galaxy J5

    View Slide

  13. View Slide

  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

    View Slide

  15. SOLUTIONS
    ∎ One-shot alarms
    ∎ setAlarmClock()
    ∎ Malfunctioning alarms detectors
    ∎ Sticky services

    View Slide

  16. SENSORS

    View Slide

  17. CATEGORIES
    ∎ Motion
    ∎ Environmental
    ∎ Position

    View Slide

  18. TYPES
    ∎ Hardware-based
    ∎ Software-based

    View Slide

  19. FRAMEWORK
    SensorManager
    ∎ Instance sensor service
    ∎ Access and list sensors
    ∎ Register/Unregister listeners
    ∎ Data acquisition rates
    ∎ Sensor accuracy constants
    ∎ Acquire orientation

    View Slide

  20. FRAMEWORK
    SensorEventListener
    ∎ onSensorChanged()
    ∎ onAccuracyChanged()

    View Slide

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

    View Slide

  22. BATCHING
    API 19+
    Batch period
    (maxReportLatencyUs)

    View Slide

  23. PROBLEM
    Delay you specify is
    only a SUGGESTED
    delay

    View Slide

  24. PROBLEM
    Samples NEVER
    evenly spaced

    View Slide

  25. Huawei P7

    View Slide

  26. Bq Aquaris E5

    View Slide

  27. View Slide

  28. SOLUTION
    LOWER / HIGHER
    frequency

    View Slide

  29. SOLUTION
    RESAMPLER
    ∎ Interpolator
    ∎ Extrapolator

    View Slide

  30. PROBLEM
    GARBAGE DATA until
    sensor become
    stable

    View Slide

  31. Jiayu S3

    View Slide

  32. SOLUTION
    THROW AWAY
    initial data

    View Slide

  33. PROBLEM
    Common sensors
    are NON-WAKE-UP

    View Slide

  34. Nexus 6

    View Slide

  35. SOLUTION
    Acquire and release
    WAKELOCKS wisely

    View Slide

  36. PROBLEM
    EPOCH
    timestamps

    View Slide

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

    View Slide

  38. View Slide

  39. SOLUTION
    BASE TIME +
    difference

    View Slide

  40. PROBLEM
    BACKJUMPS

    View Slide

  41. Nexus 5

    View Slide

  42. View Slide

  43. SOLUTION
    REARRANGE base
    time of the following
    samples

    View Slide

  44. PROBLEM
    SIGNAL noise

    View Slide

  45. Jiayu S3

    View Slide

  46. SOLUTION
    ADAPTIVE
    filters

    View Slide

  47. PROBLEM
    OFFSET/Drifting

    View Slide

  48. SOLUTION
    CALIBRATION

    View Slide

  49. PROBLEM
    System will NOT disable
    sensors
    AUTOMATICALLY

    View Slide

  50. SOLUTION
    DISABLE sensors
    you don’t need
    anymore

    View Slide

  51. PROBLEM
    Sensor COORDINATE
    SYSTEM based on device
    NATURAL ORIENTATION

    View Slide

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

    View Slide

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

    View Slide

  54. SOLUTION
    COPY values from
    SensorEvent

    View Slide

  55. POWER-SAVING
    MODES

    View Slide

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

    View Slide

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

    View Slide

  58. View Slide

  59. LIGHT-DOZE
    Android 7.0+
    ∎ Unplugged
    ∎ Screen off
    ∎ Not necessarily stationary

    View Slide

  60. View Slide

  61. View Slide

  62. APP STANDBY
    API 23+
    ∎ User not using the app
    ∎ App not in foreground
    ∎ No notifications

    View Slide

  63. SMART MANAGER (Samsung)
    ∎ Always optimizing
    ∎ Optimizing when not used
    for 3 days
    ∎ Turn off

    View Slide

  64. PROBLEMS
    ∎ Two battery optimization
    modes simultaneously
    ∎ Task killers
    ∎ Optimized by default

    View Slide

  65. SOLUTIONS
    ∎ Malfunctioning detectors
    ∎ Persistency
    ∎ Logs
    ∎ Whitelist
    ∎ Prevent the app to enter

    View Slide

  66. PROBLEM
    ALARMS
    Alarms methods of Android 5.1-
    when in Doze (Android 6.0+ devices)
    WON’T work properly

    View Slide

  67. Nexus 6
    (3 min. repeating alarm)

    View Slide

  68. PROBLEM
    ALARMS
    Neither setAndAllowWhileIdle() nor
    setExactAndAllowWhileIdle() can fire
    more than once per 9 minutes, per
    app

    View Slide

  69. Nexus 6
    setAndAllowWhileIdle()
    (3 min. repeating alarm)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  74. CONCLUSIONS
    ∎ EXPECT THE UNEXPECTED!
    ∎ FRAGMENTATION IS A FACT
    ∎ DEFENSIVE CODE

    View Slide

  75. View Slide

  76. THANKS!
    Any questions?

    View Slide

  77. References
    ∎ Da Real Fragmentation series (http://pguardiola.com)
    ∎ Da Real Fragmentation GitHub repository
    (https://github.com/Guardiola31337/darealfragmentation)
    ∎ Presentation template by SlidesCarnival

    View Slide