Manuel Serrano
Manuel Serrano

Reputation: 5

Using dataApi to update complication

Can this be done? I cannot get onDataChanged working is complicationService and if I do it in a WearableListener class, how can I pass the data to the complicationService and force an update? Thanks very much!

This code is on the handheld to update the data item when a change ocurrs:

private void syncDataItem(){
    Log.d(TAG, "PanicAlert AlarmStatus=" + isAlertActive(context));
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_ALARM);
    putDataMapRequest.getDataMap().putBoolean(FIELD_ALARM_ON, isAlertActive(context));
    putDataMapRequest.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
}

This is the DataListener on the watch:

public class DataLayerListenerService extends WearableListenerService {

private static final String TAG = "DataLayerSample";
private static final String PATH_ALARM = "/alarm_path";
private static final String FIELD_ALARM_ON = "alarm_on";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents);
    }

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    ConnectionResult connectionResult =
            googleApiClient.blockingConnect(30, TimeUnit.SECONDS);

    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient.");
        return;
    }
    for (DataEvent event : dataEvents) {
        //Force update of complication? How?
    }
}

}

This is complicationService (Google example just providing a random number):

@Override
public void onComplicationUpdate(
        int complicationId, int dataType, ComplicationManager complicationManager) {
    Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
    if (mGoogleApiClient == null){
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }

    int randomNumber = (int) Math.floor(Math.random() * 10);

    String randomNumberText =
            String.format(Locale.getDefault(), "%d!", randomNumber);

    // Create Tap Action so that the user can trigger an update by tapping the complication.
    Intent updateIntent =
            new Intent(getApplicationContext(), UpdateComplicationDataService.class);
    updateIntent.setAction(UpdateComplicationDataService.ACTION_UPDATE_COMPLICATION);
    updateIntent.putExtra(UpdateComplicationDataService.EXTRA_COMPLICATION_ID, complicationId);

    PendingIntent pendingIntent = PendingIntent.getService(
            getApplicationContext(),
            complicationId,
            updateIntent,
            0);

    ComplicationData complicationData = null;

    switch (dataType) {
        case ComplicationData.TYPE_SHORT_TEXT:
            complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
                    .setShortText(ComplicationText.plainText(String.valueOf(actualState)))
                    .setTapAction(pendingIntent)
                    .build();
            break;
    if (complicationData != null) {
        complicationManager.updateComplicationData(complicationId, complicationData);
    }
        //onStartWearableActivityClick();

}

Upvotes: 0

Views: 795

Answers (1)

Sterling
Sterling

Reputation: 6635

It sounds to me like you want a ProviderUpdateRequester in your WearableListenerService. When you receive changed data from the handheld, make a call like this:

new ProviderUpdateRequester(context, new ComponentName(context, "myComplicationClass"))
        .requestUpdateAll();

Obviously, you'll need to be storing the changed data locally (on the watch) before doing so - in a database, SharedPreferences, or whatever - as there's no way to directly pass the data to your complication provider. But you're probably already doing that to make your provider work anyway.

Documentation is here: https://developer.android.com/reference/android/support/wearable/complications/ProviderUpdateRequester.html

Upvotes: 4

Related Questions