android 5.0 internals and inferiority complex droidcon.de 2015
TRANSCRIPT
![Page 1: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/1.jpg)
Android Lollipop Internalsand our inferiority complex
or impostor syndrome
![Page 2: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/2.jpg)
+AleksanderPiotrowski@pelotasplus
![Page 3: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/3.jpg)
About presentationand the gap between us mortals
and rockstar developers fromSquare and Google
![Page 4: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/4.jpg)
History
● originally as “What’s new in Lollipop”● about new APIs● how it works under the hood● the devil is in the detail
http://nelenkov.blogspot.de/@kapitanpetko
![Page 5: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/5.jpg)
What to look for
● technical informationa bit
● motivationhopefully lot more
● are we rockstar devsor not?
![Page 6: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/6.jpg)
Disclaimer
![Page 7: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/7.jpg)
Disclaimer
● nothing against the Google● actually make a living thanks to their
technologies
![Page 8: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/8.jpg)
Disclaimer
● don’t want to diminish their achievements● or suggest anything bad about their devs
![Page 9: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/9.jpg)
Disclaimer
● have never been recruited by them
![Page 10: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/10.jpg)
The APIs arms race
![Page 11: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/11.jpg)
![Page 12: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/12.jpg)
The APIs arms race
● each new release thousands new APIs● iOS 8 includes over 4,000 new APIs● thousands of new Material Designs or new
Bluetooth stacks?
![Page 13: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/13.jpg)
The APIs arms race
● the more, the better● … or just a marketing?
● Android Weekly pressure ;-)
![Page 14: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/14.jpg)
LollipopOr any other Android version, I guess
![Page 15: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/15.jpg)
Significant changes
● Material Design
● WebView updated via Play Store
● FDE
![Page 16: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/16.jpg)
Significant changes
● Bluetooth stack changes
● Android Work/Multi-user - BYOD
● JobScheduler API
![Page 17: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/17.jpg)
![Page 18: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/18.jpg)
![Page 19: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/19.jpg)
One random change
● new package android.util.Range● immutable range
![Page 20: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/20.jpg)
android.util.Range
● check if a value is in a range● check if ranges are equal● get lower/upper values● intersect two ranges
![Page 21: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/21.jpg)
Range::contains(value)public boolean contains(T value) {
checkNotNull(value, "value must not be null");
boolean gteLower = value.compareTo(mLower) >= 0;
boolean lteUpper = value.compareTo(mUpper) <= 0;
return gteLower && lteUpper;
}
![Page 22: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/22.jpg)
The Projects
![Page 23: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/23.jpg)
Project Butter
● in JellyBean
● smoother UI60fps
![Page 24: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/24.jpg)
Other projects
● Svelte in KitKatrunning low-end on deviceswith 512MB or less
● Pi ;-)Google as a telecom
![Page 25: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/25.jpg)
Project Volta
● improve battery life
![Page 26: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/26.jpg)
Project Volta
● at platform level● at developers level● at users level
![Page 27: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/27.jpg)
Project Volta
● ART runtime● JobScheduler API and Battery Historian● Battery Saver
![Page 28: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/28.jpg)
JobScheduler API
![Page 29: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/29.jpg)
Our task
Our App
Task
● non user-facingbackground task ;-)
● can wait to be run laterexecution time doesn’t matter
● resource intensive
![Page 30: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/30.jpg)
The old way
Our App
Task
AlarmManager
NetworkManager
BatteryManager
![Page 31: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/31.jpg)
JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent) .setMinimumLatency(4000) .setOverrideDeadline(5000) .setRequiredNetworkType( JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresCharging(true) .setPersisted(true);JobInfo jobInfo = builder.build();
constraints
our task
task is now a job
![Page 32: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/32.jpg)
JobScheduler
Our App JobInfo
JobInfo.Builder
![Page 33: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/33.jpg)
JobScheduler
Our App JobInfo
JobInfo.Builder schedule()Job
Scheduler
![Page 34: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/34.jpg)
JobScheduler
SystemService
![Page 35: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/35.jpg)
JobScheduler
SystemService
BatteryService
android.content.Intent#ACTION_BATTERY_CHANGED
![Page 36: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/36.jpg)
JobScheduler
SystemService
BatteryService
android.content.Intent#ACTION_BATTERY_CHANGED
WebViewUpdateService
android.content.Intent #ACTION_PACKAGE_REPLACED
![Page 37: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/37.jpg)
JobScheduler
JobScheduler
JobInfo
![Page 38: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/38.jpg)
JobScheduler
JobScheduler
#1
JobInfo
#2
JobInfo
#3
JobInfo
/data/system/job/jobs.xml
![Page 39: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/39.jpg)
JobScheduler
JobScheduler
#1
JobInfo
#2
JobInfo
#3
JobInfo
/data/system/job/jobs.xml
Battery TimeNetwork ***
![Page 40: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/40.jpg)
BatteryControllerpublic class ChargingTracker extends BroadcastReceiver { private final AlarmManager mAlarm; private final PendingIntent mStableChargingTriggerIntent;
[...]
@Override public void onReceive(Context context, Intent intent) { onReceiveInternal(intent); }}
![Page 41: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/41.jpg)
public void startTracking() { IntentFilter filter = new IntentFilter();
// Battery health. filter.addAction(Intent.ACTION_BATTERY_LOW); filter.addAction(Intent.ACTION_BATTERY_OKAY); // Charging/not charging. filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); // Charging stable. filter.addAction(ACTION_CHARGING_STABLE); mContext.registerReceiver(this, filter);
[...]}
![Page 42: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/42.jpg)
public void onReceiveInternal(Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_BATTERY_LOW.equals(action)) { [...] mBatteryHealthy = false; } else if (Intent.ACTION_BATTERY_OKAY.equals(action)) { [...] mBatteryHealthy = true; maybeReportNewChargingState(); } [...]}
![Page 43: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/43.jpg)
JobSchedulerCompat
![Page 44: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/44.jpg)
JobSchedulerFacebook
Google+
Ebay
JobScheduler
Service
System Service
Job
Battery
Time
Network
***
JobInfo
JobInfo
JobInfo
![Page 45: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/45.jpg)
JobSchedulerCompatFacebook
Google+
Ebay
Battery
Time
Network
***
JobInfo
JobInfo
JobScheduler
Service
JobScheduler
Service
JobScheduler
Service
JobInfo Battery
Time
Network
***
System Service
![Page 46: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/46.jpg)
GCMNetworkManagerFresh from Google I/O 2015
Cloud Messaging 3.0
![Page 47: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/47.jpg)
GCMNetworkManager
setRequiresChargingsetRequiresIdlesetRequiredNetworksetPersisted
setRequiresChargingsetRequiresDeviceIdlesetRequiredNetworkTypesetPersisted
![Page 48: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/48.jpg)
● based on GPS not API 21so portable
● tasks vs jobsso similar API
● jobs executed on a serviceso similar API again ;-)
GCMNetworkManager
![Page 49: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/49.jpg)
The Idle mode
***
![Page 51: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/51.jpg)
https://www.youtube.com/watch?v=KzSKIpJepUw
![Page 52: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/52.jpg)
Idle mode
system has determinedthat phone is not being usedand is not likely to be used
anytime soon
![Page 53: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/53.jpg)
Idle mode example
Job criteria:idle state + charging
When:at night, when the phone is next to your bed
![Page 54: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/54.jpg)
Digression #1Many Googles
![Page 55: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/55.jpg)
Many Googles
● not the same Google for every one of us● different search results● different ads● fine-grained targeting of contents
![Page 56: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/56.jpg)
![Page 57: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/57.jpg)
![Page 58: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/58.jpg)
![Page 59: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/59.jpg)
Almost there...
● strong technology marketingvideos, blog posts, APIs arms race
● bold statements about possibilitiesidle state, at night, next to the bed
● proven track recordsearch and ads tailored to our behaviour
![Page 60: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/60.jpg)
The idle state algorithmor is it “idle”?
![Page 61: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/61.jpg)
// Policy: we decide that we're "idle" if the device has been unused /// screen off or dreaming for at least this longprivate static final long INACTIVITY_IDLE_THRESHOLD = 71 * 60 * 1000;// millis; 71 min
private static final long IDLE_WINDOW_SLOP = 5 * 60 * 1000;// 5 minute window, to be nice
quotes are here ;-)
![Page 62: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/62.jpg)
The “idle” state algorithm
1. display turns off2. start the timer for 71 minutes +/- 5 minutes3. alarm goes off4. if screen still turned off
we are in the idle state
![Page 63: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/63.jpg)
@Overridepublic void onReceive(Context context, Intent intent) { final String action = intent.getAction();
[...]
} else if (action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_DREAMING_STARTED)) { final long nowElapsed = SystemClock.elapsedRealtime(); final long when = nowElapsed + INACTIVITY_IDLE_THRESHOLD; mAlarm.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, IDLE_WINDOW_SLOP, mIdleTriggerIntent); }
[...]
![Page 64: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/64.jpg)
@Overridepublic void onReceive(Context context, Intent intent) { final String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_ON) || action.equals(Intent.ACTION_DREAMING_STOPPED)) { // possible transition to not-idle if (mIdle) { [...] mAlarm.cancel(mIdleTriggerIntent); mIdle = false; reportNewIdleState(mIdle); } [...]
![Page 65: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/65.jpg)
@Overridepublic void onReceive(Context context, Intent intent) { final String action = intent.getAction();
[...]
} else if (action.equals(ACTION_TRIGGER_IDLE)) { // idle time starts now if (!mIdle) { [...]
mIdle = true; reportNewIdleState(mIdle); } }}
![Page 66: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/66.jpg)
The “idle” state algorithm
● time is not a factorat night
● sensors not usedlying next to the bed
Android Doze to the rescue?
![Page 67: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/67.jpg)
The “idle” state algorithm
● display the only factorhow long is being turned off
● not tuned per usersame for everyonenot based on our own behaviour
![Page 68: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/68.jpg)
The “idle” state algorithm
● random 71 minutesor maybe there is some magic here?
![Page 69: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/69.jpg)
Takeaways
● don’t be afraid to look at the codenot a rocket science therecan cure from inferiority complex
● write code to get betterit always sucks with the first versiongets better which each commit or PR
![Page 70: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/70.jpg)
Thanks
● droidcon Berlin
● YOUfor attending my talkand others too ;-)
![Page 71: Android 5.0 internals and inferiority complex droidcon.de 2015](https://reader034.vdocuments.mx/reader034/viewer/2022042819/55c64e12bb61eb6e6c8b45fd/html5/thumbnails/71.jpg)
+AleksanderPiotrowski@pelotasplus