Slide 1

Slide 1 text

DA REAL FRAGMENTATION EXPLAINED

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

ALARMS

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

SOLUTIONS ∎ Master AlarmManager ∎ evernote/android-job

Slide 11

Slide 11 text

PROBLEM <5 minutes repeating alarms NOT ALLOWED

Slide 12

Slide 12 text

Samsung Galaxy J5

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

SENSORS

Slide 17

Slide 17 text

CATEGORIES ∎ Motion ∎ Environmental ∎ Position

Slide 18

Slide 18 text

TYPES ∎ Hardware-based ∎ Software-based

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

FRAMEWORK SensorEventListener ∎ onSensorChanged() ∎ onAccuracyChanged()

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

BATCHING API 19+ Batch period (maxReportLatencyUs)

Slide 23

Slide 23 text

PROBLEM Delay you specify is only a SUGGESTED delay

Slide 24

Slide 24 text

PROBLEM Samples NEVER evenly spaced

Slide 25

Slide 25 text

Huawei P7

Slide 26

Slide 26 text

Bq Aquaris E5

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

SOLUTION LOWER / HIGHER frequency

Slide 29

Slide 29 text

SOLUTION RESAMPLER ∎ Interpolator ∎ Extrapolator

Slide 30

Slide 30 text

PROBLEM GARBAGE DATA until sensor become stable

Slide 31

Slide 31 text

Jiayu S3

Slide 32

Slide 32 text

SOLUTION THROW AWAY initial data

Slide 33

Slide 33 text

PROBLEM Common sensors are NON-WAKE-UP

Slide 34

Slide 34 text

Nexus 6

Slide 35

Slide 35 text

SOLUTION Acquire and release WAKELOCKS wisely

Slide 36

Slide 36 text

PROBLEM EPOCH timestamps

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

SOLUTION BASE TIME + difference

Slide 40

Slide 40 text

PROBLEM BACKJUMPS

Slide 41

Slide 41 text

Nexus 5

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

SOLUTION REARRANGE base time of the following samples

Slide 44

Slide 44 text

PROBLEM SIGNAL noise

Slide 45

Slide 45 text

Jiayu S3

Slide 46

Slide 46 text

SOLUTION ADAPTIVE filters

Slide 47

Slide 47 text

PROBLEM OFFSET/Drifting

Slide 48

Slide 48 text

SOLUTION CALIBRATION

Slide 49

Slide 49 text

PROBLEM System will NOT disable sensors AUTOMATICALLY

Slide 50

Slide 50 text

SOLUTION DISABLE sensors you don’t need anymore

Slide 51

Slide 51 text

PROBLEM Sensor COORDINATE SYSTEM based on device NATURAL ORIENTATION

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

PROBLEM NOT safe to hold onto SensorEvent past onSensorChanged()

Slide 54

Slide 54 text

SOLUTION COPY values from SensorEvent

Slide 55

Slide 55 text

POWER-SAVING MODES

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Nexus 6 (3 min. repeating alarm)

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

THANKS! Any questions?

Slide 77

Slide 77 text

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