Upload
aleksander-piotrowski
View
340
Download
2
Tags:
Embed Size (px)
Citation preview
Android Lollipop Internalsand our inferiority complex
or impostor syndrome
+AleksanderPiotrowski@pelotasplus
About presentationand the gap between us mortals
and rockstar developers fromSquare and Google
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
What to look for
● technical informationa bit
● motivationhopefully lot more
● are we rockstar devsor not?
Disclaimer
Disclaimer
● nothing against the Google● actually make a living thanks to their
technologies
Disclaimer
● don’t want to diminish their achievements● or suggest anything bad about their devs
Disclaimer
● have never been recruited by them
The APIs arms race
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?
The APIs arms race
● the more, the better● … or just a marketing?
● Android Weekly pressure ;-)
LollipopOr any other Android version, I guess
Significant changes
● Material Design
● WebView updated via Play Store
● FDE
Significant changes
● Bluetooth stack changes
● Android Work/Multi-user - BYOD
● JobScheduler API
One random change
● new package android.util.Range● immutable range
android.util.Range
● check if a value is in a range● check if ranges are equal● get lower/upper values● intersect two ranges
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;
}
The Projects
Project Butter
● in JellyBean
● smoother UI60fps
Other projects
● Svelte in KitKatrunning low-end on deviceswith 512MB or less
● Pi ;-)Google as a telecom
Project Volta
● improve battery life
Project Volta
● at platform level● at developers level● at users level
Project Volta
● ART runtime● JobScheduler API and Battery Historian● Battery Saver
JobScheduler API
Our task
Our App
Task
● non user-facingbackground task ;-)
● can wait to be run laterexecution time doesn’t matter
● resource intensive
The old way
Our App
Task
AlarmManager
NetworkManager
BatteryManager
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
JobScheduler
Our App JobInfo
JobInfo.Builder
JobScheduler
Our App JobInfo
JobInfo.Builder schedule()Job
Scheduler
JobScheduler
SystemService
JobScheduler
SystemService
BatteryService
android.content.Intent#ACTION_BATTERY_CHANGED
JobScheduler
SystemService
BatteryService
android.content.Intent#ACTION_BATTERY_CHANGED
WebViewUpdateService
android.content.Intent #ACTION_PACKAGE_REPLACED
JobScheduler
JobScheduler
JobInfo
JobScheduler
JobScheduler
#1
JobInfo
#2
JobInfo
#3
JobInfo
/data/system/job/jobs.xml
JobScheduler
JobScheduler
#1
JobInfo
#2
JobInfo
#3
JobInfo
/data/system/job/jobs.xml
Battery TimeNetwork ***
BatteryControllerpublic class ChargingTracker extends BroadcastReceiver { private final AlarmManager mAlarm; private final PendingIntent mStableChargingTriggerIntent;
[...]
@Override public void onReceive(Context context, Intent intent) { onReceiveInternal(intent); }}
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);
[...]}
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(); } [...]}
JobSchedulerCompat
JobSchedulerFacebook
Google+
Ebay
JobScheduler
Service
System Service
Job
Battery
Time
Network
***
JobInfo
JobInfo
JobInfo
JobSchedulerCompatFacebook
Google+
Ebay
Battery
Time
Network
***
JobInfo
JobInfo
JobScheduler
Service
JobScheduler
Service
JobScheduler
Service
JobInfo Battery
Time
Network
***
System Service
GCMNetworkManagerFresh from Google I/O 2015
Cloud Messaging 3.0
GCMNetworkManager
setRequiresChargingsetRequiresIdlesetRequiredNetworksetPersisted
setRequiresChargingsetRequiresDeviceIdlesetRequiredNetworkTypesetPersisted
● based on GPS not API 21so portable
● tasks vs jobsso similar API
● jobs executed on a serviceso similar API again ;-)
GCMNetworkManager
The Idle mode
***
https://www.youtube.com/watch?v=KzSKIpJepUw
Idle mode
system has determinedthat phone is not being usedand is not likely to be used
anytime soon
Idle mode example
Job criteria:idle state + charging
When:at night, when the phone is next to your bed
Digression #1Many Googles
Many Googles
● not the same Google for every one of us● different search results● different ads● fine-grained targeting of contents
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
The idle state algorithmor is it “idle”?
// 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 ;-)
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
@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); }
[...]
@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); } [...]
@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); } }}
The “idle” state algorithm
● time is not a factorat night
● sensors not usedlying next to the bed
Android Doze to the rescue?
The “idle” state algorithm
● display the only factorhow long is being turned off
● not tuned per usersame for everyonenot based on our own behaviour
The “idle” state algorithm
● random 71 minutesor maybe there is some magic here?
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
Thanks
● droidcon Berlin
● YOUfor attending my talkand others too ;-)
+AleksanderPiotrowski@pelotasplus