android lollipop internals and inferiority complex droidcon.hr 2015

67
Android Lollipop Internals and our inferiority complex

Upload: aleksander-piotrowski

Post on 15-Aug-2015

114 views

Category:

Mobile


2 download

TRANSCRIPT

Page 1: Android Lollipop internals and inferiority complex droidcon.hr 2015

Android Lollipop Internalsand our inferiority complex

Page 2: Android Lollipop internals and inferiority complex droidcon.hr 2015

+AleksanderPiotrowski@pelotasplus

Page 3: Android Lollipop internals and inferiority complex droidcon.hr 2015

About presentation

Page 4: Android Lollipop internals and inferiority complex droidcon.hr 2015

Inferiority complex

● mine but maybe yours as well?● gets better with each PR or a talk● … or look taken at others code

Page 5: Android Lollipop internals and inferiority complex droidcon.hr 2015

History

● originally as “What’s new in Lollipop”● about new APIs● how it works under the hood● the devil is in the detail

Page 6: Android Lollipop internals and inferiority complex droidcon.hr 2015

What to look for

● technical information - a bit● motivation - hopefully lot more

Page 7: Android Lollipop internals and inferiority complex droidcon.hr 2015

Disclaimer

Page 8: Android Lollipop internals and inferiority complex droidcon.hr 2015

Disclaimer

● nothing against the Google● actually make a living thanks to their

technologies

Page 9: Android Lollipop internals and inferiority complex droidcon.hr 2015

Disclaimer

● don’t want to diminish their achievements● or suggest anything bad about their devs

Page 10: Android Lollipop internals and inferiority complex droidcon.hr 2015

Disclaimer

● have never been recruited by them

Page 11: Android Lollipop internals and inferiority complex droidcon.hr 2015

The APIs arms race

Page 12: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 13: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 14: Android Lollipop internals and inferiority complex droidcon.hr 2015

The APIs arms race

● the more, the better● … or just a marketing?

● Android Weekly pressure ;-)

Page 15: Android Lollipop internals and inferiority complex droidcon.hr 2015

Lollipop

Page 16: Android Lollipop internals and inferiority complex droidcon.hr 2015

Significant changes

● Material Design

● WebView updated via Play Store

● FDE

Page 17: Android Lollipop internals and inferiority complex droidcon.hr 2015

Significant changes

● Bluetooth stack changes

● Android Work/Multi-user - BYOD

● JobScheduler API

Page 18: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 19: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 20: Android Lollipop internals and inferiority complex droidcon.hr 2015

One random change

● new package android.util.Range● immutable range

Page 21: Android Lollipop internals and inferiority complex droidcon.hr 2015

android.util.Range

● check if a value is in a range● check if ranges are equal● get lower/upper values● intersect two ranges

Page 22: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 23: Android Lollipop internals and inferiority complex droidcon.hr 2015

The Projects

Page 24: Android Lollipop internals and inferiority complex droidcon.hr 2015

Project Butter

● in JellyBean

● smoother UI60fps

Page 25: Android Lollipop internals and inferiority complex droidcon.hr 2015

Other projects

● Svelte in KitKatrunning low-end on deviceswith 512MB or less

● Pi ;-)Google as a telecom

Page 26: Android Lollipop internals and inferiority complex droidcon.hr 2015

Project Volta

● improve battery life

Page 27: Android Lollipop internals and inferiority complex droidcon.hr 2015

Project Volta

● at platform level● at developers level● at users level

Page 28: Android Lollipop internals and inferiority complex droidcon.hr 2015

Project Volta

● ART runtime● JobScheduler API and Battery Historian● Battery Saver

Page 29: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler API

Page 30: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

Our App

Task

Page 31: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

Our App JobInfo

JobInfo.Builder

Page 33: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

Our App JobInfo

JobInfo.Builder schedule()Job

Scheduler

Page 34: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

SystemService

Page 35: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

SystemService

BatteryService

android.content.Intent#ACTION_BATTERY_CHANGED

Page 36: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

SystemService

BatteryService

android.content.Intent#ACTION_BATTERY_CHANGED

WebViewUpdateService

android.content.Intent #ACTION_PACKAGE_REPLACED

Page 37: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

JobScheduler

JobInfo

Page 38: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

JobScheduler

#1

JobInfo

#2

JobInfo

#3

JobInfo

/data/system/job/jobs.xml

Page 39: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobScheduler

JobScheduler

#1

JobInfo

#2

JobInfo

#3

JobInfo

/data/system/job/jobs.xml

Battery TimeNetwork ***

Page 40: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 Lollipop internals and inferiority complex droidcon.hr 2015

BatteryControllerpublic 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 Lollipop internals and inferiority complex droidcon.hr 2015

BatteryControllerpublic void onReceiveInternal(Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_BATTERY_LOW.equals(action)) { [...] // If we get this action, the battery is discharging => it isn't plugged in so // there's no work to cancel. We track this variable for the case where it is // charging, but hasn't been for long enough to be healthy.

mBatteryHealthy = false; } else if (Intent.ACTION_BATTERY_OKAY.equals(action)) { [...] mBatteryHealthy = true; maybeReportNewChargingState(); } [...]}

Page 43: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobSchedulerCompat

Page 44: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobSchedulerFacebook

Google+

Ebay

JobScheduler

Service

System Service

Job

Battery

Time

Network

***

JobInfo

JobInfo

JobInfo

Page 45: Android Lollipop internals and inferiority complex droidcon.hr 2015

JobSchedulerCompatFacebook

Google+

Ebay

Battery

Time

Network

***

JobInfo

JobInfo

JobScheduler

Service

JobScheduler

Service

JobScheduler

Service

JobInfo Battery

Time

Network

***

System Service

Page 46: Android Lollipop internals and inferiority complex droidcon.hr 2015

The Idle mode

***

Page 48: Android Lollipop internals and inferiority complex droidcon.hr 2015

https://www.youtube.com/watch?v=KzSKIpJepUw

Page 49: Android Lollipop internals and inferiority complex droidcon.hr 2015

Idle mode

system has determinedthat phone is not being usedand is not likely to be used

anytime soon

Page 50: Android Lollipop internals and inferiority complex droidcon.hr 2015

Idle mode example

Job criteria:idle state + charging

When:at night, when the phone is next to your bed

Page 51: Android Lollipop internals and inferiority complex droidcon.hr 2015

Digression #1Many Googles

Page 52: Android Lollipop internals and inferiority complex droidcon.hr 2015

Many Googles

● not the same Google for every one of us● different search results● different ads● fine-grained targeting of contents

Page 53: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 54: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 55: Android Lollipop internals and inferiority complex droidcon.hr 2015
Page 56: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 57: Android Lollipop internals and inferiority complex droidcon.hr 2015

The idle state algorithmor is it “idle”?

Page 58: Android Lollipop internals and inferiority complex droidcon.hr 2015

// 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 59: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 60: Android Lollipop internals and inferiority complex droidcon.hr 2015

@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 61: Android Lollipop internals and inferiority complex droidcon.hr 2015

@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)) { // when the screen goes off or dreaming starts, we schedule the // alarm that will tell us when we have decided the device is // truly idle. final long nowElapsed = SystemClock.elapsedRealtime(); final long when = nowElapsed + INACTIVITY_IDLE_THRESHOLD; if (DEBUG) { Slog.v(TAG, "Scheduling idle : " + action + " now:" + nowElapsed + " when=" + when); } mAlarm.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, IDLE_WINDOW_SLOP, mIdleTriggerIntent); }

[...]

Page 62: Android Lollipop internals and inferiority complex droidcon.hr 2015

@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) { if (DEBUG) { Slog.v(TAG, "Idle trigger fired @ " + SystemClock.elapsedRealtime()); } mIdle = true; reportNewIdleState(mIdle); } }}

Page 63: Android Lollipop internals and inferiority complex droidcon.hr 2015

The “idle” state algorithm

● time is not a factorat night

● sensors not usedlying next to the bed

Page 64: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 65: Android Lollipop internals and inferiority complex droidcon.hr 2015

The “idle” state algorithm

● random 71 minutesor maybe there is some magic here?

Page 66: Android Lollipop internals and inferiority complex droidcon.hr 2015

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 67: Android Lollipop internals and inferiority complex droidcon.hr 2015

Thanks

● droidcon Zagreb

● YOU <3for attending my talkand others too ;-)