28
Battery Efficient Location Services NYC Droidcon, 9/21/14 google.com/+ArunNagarajan @entaq

Battery Efficient Location Services

Embed Size (px)

DESCRIPTION

DroidCon - NYC 2014 Presentation

Citation preview

Page 1: Battery Efficient Location Services

Battery Efficient Location Services

NYC Droidcon, 9/21/14google.com/+ArunNagarajan @entaq

Page 2: Battery Efficient Location Services

About me

Arun Nagarajan (@entaq)

Currently Founding Lead Engineer, funded stealth startup (NYC)We are hiring! Email me at [email protected]

Previously2 yrs at Google - Tech Lead, Developer Platform9 yrs at Verivo Software (Boston) - VP of Architecture

Page 3: Battery Efficient Location Services

Background

Page 4: Battery Efficient Location Services

What are location services?

● Maps vs. Location○ Context is king

● Capabilities over technologies● User expectations

○ Privacy, transparency, no compromises● Background location

○ Intelligent services that become possible

Page 5: Battery Efficient Location Services

Three variables to consider

● Power● Accuracy● Coverage

Page 6: Battery Efficient Location Services

Various options

Source - developers.google.com/events/io/sessions/325337477

Page 7: Battery Efficient Location Services

Battery cost is real

Source - developers.google.com/events/io/sessions/325337477

Page 8: Battery Efficient Location Services

What takes up more power?

● CPU● Display● GPS● WiFi

What’s your guess?

Page 9: Battery Efficient Location Services

What takes up more power?

● CPU ~ 300 milliwatts● Display ~ 350 milliwatts● GPS ~100 milliwatts● WiFI ~30 milliwatts

Page 10: Battery Efficient Location Services

User first

Page 11: Battery Efficient Location Services

Transparency to the user

● Having a map (a reason) in your app helps● Permissions

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

● Showing in the UI

Page 12: Battery Efficient Location Services

Tools the user has

● Battery UI● GPS indicator

○ Some Samsung devices blink indicator!

Page 13: Battery Efficient Location Services

But there is more

Page 14: Battery Efficient Location Services

Critical APIs

Page 16: Battery Efficient Location Services

Simple/Synchronous waypublic class MainActivity extends FragmentActivity implements

GooglePlayServicesClient.ConnectionCallbacks,

GooglePlayServicesClient.OnConnectionFailedListener {

...

// Global variable to hold the current location

Location mCurrentLocation;

...

mCurrentLocation = mLocationClient.getLastLocation();

...

}

Page 17: Battery Efficient Location Services

Configure a LocationRequestmLocationRequest = LocationRequest.create();

mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

mLocationRequest.setInterval(UPDATE_INTERVAL);

mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

mLocationRequest.setSmallestDisplacement(DISTANCE_IN_METERS);

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Page 18: Battery Efficient Location Services

Callback to LocationListenerpublic class MainActivity extends FragmentActivity implements LocationListener { ...

@Override

public void onLocationChanged(Location location) {

String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," +

Double.toString(location.getLongitude());

}

...

}

Page 19: Battery Efficient Location Services

Register a pending intentPendingIntent bglocationUpdate = PendingIntent.getService(context, 0,

new Intent(context, BackgroundLocationUpdateService.class),

PendingIntent.FLAG_UPDATE_CURRENT);

locationClient.requestLocationUpdates(locationRequest,bglocationUpdate);

locationClient.disconnect();

Page 20: Battery Efficient Location Services

AlarmManager works best for usPendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this,

AlarmManagerLocationReceiver.class), 0);

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

if (foreground) {

alarmMgr.cancel(pi);

} else {

alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,

SystemClock.elapsedRealtime(), 1000 * 60 * 10, pi);

}

Page 21: Battery Efficient Location Services

Other tricks

Page 22: Battery Efficient Location Services

Activity Monitoring switch(activityType) {

case DetectedActivity.IN_VEHICLE:

return "in_vehicle";

case DetectedActivity.ON_BICYCLE:

return "on_bicycle";

case DetectedActivity.ON_FOOT:

return "on_foot";

case DetectedActivity.STILL:

return "still";

case DetectedActivity.UNKNOWN:

return "unknown";

case DetectedActivity.TILTING:

return "tilting";

}

return "unknown";

Page 23: Battery Efficient Location Services

Watch the battery life in your code

boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);

<receiver android:name=".receivers.PowerStateChangedReceiver">

<intent-filter>

<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>

<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>

</intent-filter>

</receiver>

Page 24: Battery Efficient Location Services

GeofencemLocationClient.addGeofences(mCurrentGeofences, pendingIntent, this);

if ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) ||

(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT)

) {

List <Geofence> triggerList =

getTriggeringGeofences(intent);

String[] triggerIds = new String[geofenceList.size()];

for (int i = 0; i < triggerIds.length; i++) {

triggerIds[i] = triggerList.get(i).getRequestId();

}

}

Page 25: Battery Efficient Location Services

Upcoming

Page 26: Battery Efficient Location Services

Battery Historian

Project Volta$ adb shell dumpsys batterystats --charged <package-name>

$ adb shell dumpsys batterystats --enable full-wake-history

$ adb shell dumpsys batterystats --reset

$ historian.par [-p powerfile] bugreport.txt > out.html

github.com/google/battery-historian

Page 27: Battery Efficient Location Services

Future explorations

● BLE devices● Other nearby phones● Photo uploads● Android L - Job Scheduler

Page 28: Battery Efficient Location Services

Thanks

Questions?

Arun Nagarajangoogle.com/+ArunNagarajan @[email protected]