23
Headless Apps I am the invisible man.

Headless Apps on BlackBerry 10.2.1

Embed Size (px)

DESCRIPTION

The code can be found here: https://github.com/zezke/WaterThePlants

Citation preview

Page 1: Headless Apps on BlackBerry 10.2.1

Headless AppsI am the invisible man.

Page 2: Headless Apps on BlackBerry 10.2.1

Who am I?

Founder of Endare

Certified BlackBerry Developer

zezke on the BB Developer fora

@brvandewalle

Page 3: Headless Apps on BlackBerry 10.2.1

What is “a headless app”?

A headless application is an application that runs in the background without a visible UI.

Page 4: Headless Apps on BlackBerry 10.2.1

Are all headless apps equal?Short-running headless apps

maximum run time of 20 seconds

Long-running headless apps

no time limit

All are limited to:

7 to 25 percent of CPU at any given time

limited to 3MB of memory

Page 5: Headless Apps on BlackBerry 10.2.1

Long-running headless apps

Requires extended permissions

Yes, a web form

Page 6: Headless Apps on BlackBerry 10.2.1

The structure of a HA

Two separate projects in a single BAR file

UI part (required!)

headless service

Page 7: Headless Apps on BlackBerry 10.2.1

How does the headless part start?

Invoked by the system on certain events (triggers)

sytem.STARTED

PORT_DIRECTED_SMS

PUSH

GEOREGIONEXIT

GEOREGIONENTER

By any UI application via an invoke target

Page 8: Headless Apps on BlackBerry 10.2.1
Page 9: Headless Apps on BlackBerry 10.2.1

Time for some code

Page 10: Headless Apps on BlackBerry 10.2.1

What’s the goal of the demo app?

Water the plants!

Based on location

And ideally on time (like every two days)

Page 11: Headless Apps on BlackBerry 10.2.1

1. Create the projectNew template in 10.2.1 SDK!

Saves you about 15 minutes of converting another template

Page 12: Headless Apps on BlackBerry 10.2.1

2. Create the UI part

Standard Cascades stuff

a MapView showing your current location

some data

and a button to set the geofence

Page 13: Headless Apps on BlackBerry 10.2.1

3. Set the geofence (I)

Request permissions

<permission>access_location_services</permission>

Link the necessary libraries (in the .pro file)

-lgeomonitor

( -lQtLocationSubset )

Page 14: Headless Apps on BlackBerry 10.2.1

3. Set the geofence (II)

//Clear the previous geofence//If you fail/forget to do this you will get an error 257geomonitor_remove("Garden");

Page 15: Headless Apps on BlackBerry 10.2.1

3. Set the geofence (III)//Set the geofencegeomonitor_region_t region = NULL;geomonitor_create_region(&region, "Garden");geomonitor_region_set_circle_shape(region, latitude, longitude,

100.0);geomonitor_region_set_monitoring_mode(region,

GEOMONITOR_MONITORING_MODE_PERSISTENT);geomonitor_region_set_notification_invoke_target(region,

"com.endare.WaterThePlantsService", GEOMONITOR_NOTIFICATION_DIRECT);

//Adding the regionint errorCode = geomonitor_add(region);if (errorCode!= 0){

qWarning()<<"Error occurred with code " << QString::number(errorCode) << endl;

}else {

qDebug() << "Geofence set!";}geomonitor_destroy_region(&region);

Page 16: Headless Apps on BlackBerry 10.2.1

3. Set the geofence (IV)

//Adding the regionint errorCode = geomonitor_add(region);if (errorCode!= 0){

qWarning()<<"Error occurred with code " << QString::number(errorCode) << endl;

}else {

qDebug() << "Geofence set!";}geomonitor_destroy_region(&region);

Page 17: Headless Apps on BlackBerry 10.2.1

4. Handle the invoke (I)

Now we have configured a geofence

Entering this geofence will trigger the headless part to be invoked

Page 18: Headless Apps on BlackBerry 10.2.1

4. Handle the invoke (II)

First connect the slot to the signal in the constructor of the headless service

m_invokeManager->connect(m_invokeManager, SIGNAL(invoked(const bb::system::InvokeRequest&)), this, SLOT(handleInvoke(const bb::system::InvokeRequest&)));

Page 19: Headless Apps on BlackBerry 10.2.1

4. Handle the invoke (III)

void Service::handleInvoke(const bb::system::InvokeRequest & request) {if (request.action().compare("bb.action.GEOREGIONENTER") == 0) {

qDebug() << "Service:: entered the georegion";triggerNotification(request);

}else {

qDebug() << "Service:: unknown service request " << request.action();}

}

Page 20: Headless Apps on BlackBerry 10.2.1

5. Trigger a notification (I)

// clear any existing notificationsNotification::clearEffectsForAll();Notification::deleteAllFromInbox();

// turn on previews in settingsbb::platform::NotificationDefaultApplicationSettings settings;

settings.setPreview(bb::platform::NotificationPriorityPolicy::Allow);settings.apply();

Page 21: Headless Apps on BlackBerry 10.2.1

5. Trigger a notification (II)InvokeRequest invokeReqNotification;invokeReqNotification.setTarget("com.endare.WaterThePlants");

//Setting the content of the bodyQString body;body = “Don’t forget to water your plants!”;

//Setting the body and the InvokeRequest of the notification_notification.setBody(QString::fromUtf8(body.toAscii()));_notification.setInvokeRequest(invokeReqNotification);

//Displaying the Instant Preview_notification.notify();

Page 22: Headless Apps on BlackBerry 10.2.1

Conclusion

It works

But slowly (takes up to three hours to fire at the moment)

might be a bug

A lot of future work

include time aspect

Page 23: Headless Apps on BlackBerry 10.2.1

Questions?