Search in sources :

Example 1 with ComplicationData

use of android.support.wearable.complications.ComplicationData in project xDrip-plus by jamorham.

the class CustomComplicationProviderService method onComplicationUpdate.

/*
     * Called when the complication needs updated data from your provider. There are four scenarios
     * when this will happen:
     *
     *   1. An active watch face complication is changed to use this provider
     *   2. A complication using this provider becomes active
     *   3. The period of time you specified in the manifest has elapsed (UPDATE_PERIOD_SECONDS)
     *   4. You triggered an update from your own class via the
     *       ProviderUpdateRequester.requestUpdate() method.
     */
@Override
public void onComplicationUpdate(int complicationId, int dataType, ComplicationManager complicationManager) {
    Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
    // Create Tap Action so that the user can trigger an update by tapping the complication.
    final ComponentName thisProvider = new ComponentName(this, getClass());
    // We pass the complication id, so we can only update the specific complication tapped.
    final PendingIntent complicationPendingIntent = ComplicationTapBroadcastReceiver.getToggleIntent(this, thisProvider, complicationId);
    String numberText = "";
    BgReading bgReading = BgReading.last(false);
    if ((bgReading == null) || (JoH.msSince(bgReading.timestamp) >= FRESH_MS)) {
        try {
            // we may be in another process!
            ActiveAndroid.clearCache();
        } catch (Exception e) {
            Log.d(TAG, "Couldn't clear cache: " + e);
        }
        bgReading = BgReading.last(false);
    }
    boolean is_stale = false;
    if (bgReading == null) {
        numberText = "null";
    } else {
        if (JoH.msSince(bgReading.timestamp) < STALE_MS) {
            numberText = bgReading.displayValue(this) + " " + bgReading.displaySlopeArrow();
        } else {
            numberText = "old " + niceTimeSinceBgReading(bgReading);
            is_stale = true;
        }
    }
    Log.d(TAG, "Returning complication text: " + numberText);
    COMPLICATION_STATE state = COMPLICATION_STATE.get_enum((int) PersistentStore.getLong(ComplicationTapBroadcastReceiver.COMPLICATION_STORE));
    if (state == null)
        state = COMPLICATION_STATE.DELTA;
    ComplicationData complicationData = null;
    switch(dataType) {
        case ComplicationData.TYPE_SHORT_TEXT:
            final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT).setShortText(ComplicationText.plainText(numberText)).setTapAction(complicationPendingIntent);
            UserError.Log.d(TAG, "TYPE_SHORT_TEXT Current complication state:" + state);
            switch(state) {
                case DELTA:
                    builder.setShortTitle(ComplicationText.plainText(getDeltaText(bgReading, is_stale)));
                    break;
                case AGO:
                    builder.setShortTitle(ComplicationText.plainText(niceTimeSinceBgReading(bgReading)));
                    break;
                default:
                    builder.setShortTitle(ComplicationText.plainText("ERR!"));
            }
            complicationData = builder.build();
            break;
        case ComplicationData.TYPE_LONG_TEXT:
            String numberTextLong = numberText + " " + getDeltaText(bgReading, is_stale) + " (" + niceTimeSinceBgReading(bgReading) + ")";
            Log.d(TAG, "Returning complication text Long: " + numberTextLong);
            // Loop status by @gregorybel
            String externalStatusString = PersistentStore.getString("remote-status-string");
            Log.d(TAG, "Returning complication status: " + externalStatusString);
            final ComplicationData.Builder builderLong = new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT).setLongTitle(ComplicationText.plainText(numberTextLong)).setLongText(ComplicationText.plainText(externalStatusString)).setTapAction(complicationPendingIntent);
            UserError.Log.d(TAG, "TYPE_LONG_TEXT Current complication state:" + state);
            complicationData = builderLong.build();
            break;
        default:
            if (Log.isLoggable(TAG, Log.WARN)) {
                Log.w(TAG, "Unexpected complication type " + dataType);
            }
    }
    if (complicationData != null) {
        complicationManager.updateComplicationData(complicationId, complicationData);
    } else {
        // If no data is sent, we still need to inform the ComplicationManager, so the update
        // job can finish and the wake lock isn't held any longer than necessary.
        complicationManager.noUpdateRequired(complicationId);
    }
}
Also used : ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) BgGraphBuilder.unitizedDeltaString(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder.unitizedDeltaString) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) ComplicationData(android.support.wearable.complications.ComplicationData)

Example 2 with ComplicationData

use of android.support.wearable.complications.ComplicationData in project Memento-Calendar by alexstyl.

the class ContactEventsProviderService method onComplicationUpdate.

@Override
public void onComplicationUpdate(final int complicationId, final int dataType, final ComplicationManager complicationManager) {
    GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {

        @Override
        public void onConnected(@Nullable Bundle bundle) {
            wearCommunicationService.loadDataItems(new WearCommunicationService.Callback() {

                @Override
                public void onDataItemsLoaded(DataItem item) {
                    ComplicationData complicationData = createComplicationData(item, dataType);
                    if (complicationData != null) {
                        complicationManager.updateComplicationData(complicationId, complicationData);
                    }
                }

                @Override
                public void onNoDataItemsAvailable() {
                    ComplicationData emptyComplicationData = createComplicationData(dataType, NO_DATE_AVAILABLE, Collections.<String>emptyList());
                    complicationManager.updateComplicationData(complicationId, emptyComplicationData);
                }
            });
        }

        @Override
        public void onConnectionSuspended(int i) {
        // no-op
        }
    };
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(connectionCallbacks).build();
    wearCommunicationService = new WearCommunicationService(googleApiClient);
    googleApiClient.connect();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) DataItem(com.google.android.gms.wearable.DataItem) Bundle(android.os.Bundle) ComplicationData(android.support.wearable.complications.ComplicationData) Nullable(android.support.annotation.Nullable)

Example 3 with ComplicationData

use of android.support.wearable.complications.ComplicationData in project xDrip by NightscoutFoundation.

the class CustomComplicationProviderService method onComplicationUpdate.

/*
     * Called when the complication needs updated data from your provider. There are four scenarios
     * when this will happen:
     *
     *   1. An active watch face complication is changed to use this provider
     *   2. A complication using this provider becomes active
     *   3. The period of time you specified in the manifest has elapsed (UPDATE_PERIOD_SECONDS)
     *   4. You triggered an update from your own class via the
     *       ProviderUpdateRequester.requestUpdate() method.
     */
@Override
public void onComplicationUpdate(int complicationId, int dataType, ComplicationManager complicationManager) {
    Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
    // Create Tap Action so that the user can trigger an update by tapping the complication.
    final ComponentName thisProvider = new ComponentName(this, getClass());
    // We pass the complication id, so we can only update the specific complication tapped.
    final PendingIntent complicationPendingIntent = ComplicationTapBroadcastReceiver.getToggleIntent(this, thisProvider, complicationId);
    String numberText = "";
    BgReading bgReading = BgReading.last(false);
    if ((bgReading == null) || (JoH.msSince(bgReading.timestamp) >= FRESH_MS)) {
        try {
            // we may be in another process!
            ActiveAndroid.clearCache();
        } catch (Exception e) {
            Log.d(TAG, "Couldn't clear cache: " + e);
        }
        bgReading = BgReading.last(false);
    }
    boolean is_stale = false;
    if (bgReading == null) {
        numberText = "null";
    } else {
        if (JoH.msSince(bgReading.timestamp) < STALE_MS) {
            numberText = bgReading.displayValue(this) + " " + bgReading.displaySlopeArrow();
        } else {
            numberText = "old " + niceTimeSinceBgReading(bgReading);
            is_stale = true;
        }
    }
    Log.d(TAG, "Returning complication text: " + numberText);
    COMPLICATION_STATE state = COMPLICATION_STATE.get_enum((int) PersistentStore.getLong(ComplicationTapBroadcastReceiver.COMPLICATION_STORE));
    if (state == null)
        state = COMPLICATION_STATE.DELTA;
    ComplicationData complicationData = null;
    switch(dataType) {
        case ComplicationData.TYPE_SHORT_TEXT:
            final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT).setShortText(ComplicationText.plainText(numberText)).setTapAction(complicationPendingIntent);
            UserError.Log.d(TAG, "TYPE_SHORT_TEXT Current complication state:" + state);
            switch(state) {
                case DELTA:
                    builder.setShortTitle(ComplicationText.plainText(getDeltaText(bgReading, is_stale)));
                    break;
                case AGO:
                    builder.setShortTitle(ComplicationText.plainText(niceTimeSinceBgReading(bgReading)));
                    break;
                default:
                    builder.setShortTitle(ComplicationText.plainText("ERR!"));
            }
            complicationData = builder.build();
            break;
        case ComplicationData.TYPE_LONG_TEXT:
            String numberTextLong = numberText + " " + getDeltaText(bgReading, is_stale) + " (" + niceTimeSinceBgReading(bgReading) + ")";
            Log.d(TAG, "Returning complication text Long: " + numberTextLong);
            // Loop status by @gregorybel
            String externalStatusString = PersistentStore.getString("remote-status-string");
            Log.d(TAG, "Returning complication status: " + externalStatusString);
            final ComplicationData.Builder builderLong = new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT).setLongTitle(ComplicationText.plainText(numberTextLong)).setLongText(ComplicationText.plainText(externalStatusString)).setTapAction(complicationPendingIntent);
            UserError.Log.d(TAG, "TYPE_LONG_TEXT Current complication state:" + state);
            complicationData = builderLong.build();
            break;
        default:
            if (Log.isLoggable(TAG, Log.WARN)) {
                Log.w(TAG, "Unexpected complication type " + dataType);
            }
    }
    if (complicationData != null) {
        complicationManager.updateComplicationData(complicationId, complicationData);
    } else {
        // If no data is sent, we still need to inform the ComplicationManager, so the update
        // job can finish and the wake lock isn't held any longer than necessary.
        complicationManager.noUpdateRequired(complicationId);
    }
}
Also used : ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) BgGraphBuilder.unitizedDeltaString(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder.unitizedDeltaString) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) ComplicationData(android.support.wearable.complications.ComplicationData)

Aggregations

ComplicationData (android.support.wearable.complications.ComplicationData)3 PendingIntent (android.app.PendingIntent)2 ComponentName (android.content.ComponentName)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 BgGraphBuilder.unitizedDeltaString (com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder.unitizedDeltaString)2 Bundle (android.os.Bundle)1 Nullable (android.support.annotation.Nullable)1 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)1 DataItem (com.google.android.gms.wearable.DataItem)1