Complication data provider

It immediately appeared to me that these new API has a huge potential, so I decided to use it with an existing project, in order to expose some of the already available data from the app using an ad-hoc complication.

The chosen project is an open source app created by my colleague and friend Alexandros which I’ve been using for a while now: Memento Namedays. The main goal of the app is to remind birthdays and name days of your contacts, and this looks like a perfect use case for a complication displaying information about the next upcoming birthday.

Let’s have a look at how I managed to create such a complication data provider.

First of all I made the needed data available to the wear module of the app. To do so, I used the Wearable Data Layer API to sync this data between handheld and wearable every time that a relevant change happens.

In the wear module I created the complication data provider, which is a service that extends ComplicationProviderService. This base class gives us a set of callbacks in order to know when the provider is selected as the data source for a complication in the current watch face (onComplicationActivated), when the complication is not selected anymore (onComplicationDeactivated) and finally when it is time to provide an updated set of data to populate the complication (onComplicationUpdate).

The last method is the most important one because there we need to load new data and bundle it to be forwarded to the watch face.

Among the callback parameters, the complication type allows the data provider to add the required fields depending on the type supported by the watch face.

In case of a Short Text type I populate the ComplicationData with the date of the next event and an optional icon:

ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)

.setShortText(ComplicationText.plainText(formatShortDate(date)))

.setIcon(Icon.createWithResource(context, R.drawable.ic_event))

.setTapAction(createContactEventsActivityIntent())

.build();

While in case of a Long Text I also added the list of contacts names associated to the next event:

ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)

.setLongTitle(ComplicationText.plainText(formatLongDate(date)))

.setLongText(ComplicationText.plainText(formatList(names)))

.setIcon(Icon.createWithResource(context, R.drawable.ic_event))

.setTapAction(createContactEventsActivityIntent())

.build();

It’s worth noticing that complications might be tappable by the user directly from the watch face. In order to support this, I used the builder method setTapAction to specify a PendingIntent pointing to an activity displaying all the information for the given event. It will be then responsibility of the watch face to trigger that PendingIntent on user tap.