1 package com.example.android.wearable.watchface.provider; 2 3 import android.support.wearable.complications.ComplicationData; 4 import android.support.wearable.complications.ComplicationManager; 5 import android.support.wearable.complications.ComplicationProviderService; 6 import android.support.wearable.complications.ComplicationText; 7 import android.util.Log; 8 9 import java.util.Locale; 10 11 /** 12 * Example Watch Face Complication data provider provides a random number on every update. 13 */ 14 public class RandomNumberProviderService extends ComplicationProviderService { 15 16 private static final String TAG = "RandomNumberProvider"; 17 18 /* 19 * Called when a complication has been activated. The method is for any one-time 20 * (per complication) set-up. 21 * 22 * You can continue sending data for the active complicationId until onComplicationDeactivated() 23 * is called. 24 */ 25 @Override 26 public void onComplicationActivated( 27 int complicationId, int dataType, ComplicationManager complicationManager) { 28 Log.d(TAG, "onComplicationActivated(): " + complicationId); 29 super.onComplicationActivated(complicationId, dataType, complicationManager); 30 } 31 32 /* 33 * Called when the complication needs updated data from your provider. There are four scenarios 34 * when this will happen: 35 * 36 * 1. An active watch face complication is changed to use this provider 37 * 2. A complication using this provider becomes active 38 * 3. The period of time you specified in the manifest has elapsed (UPDATE_PERIOD_SECONDS) 39 * 4. You triggered an update from your own class via the 40 * ProviderUpdateRequester.requestUpdate() method. 41 */ 42 @Override 43 public void onComplicationUpdate( 44 int complicationId, int dataType, ComplicationManager complicationManager) { 45 Log.d(TAG, "onComplicationUpdate()"); 46 47 48 // Retrieve your data, in this case, we simply create a random number to display. 49 int randomNumber = (int) Math.floor(Math.random() * 10); 50 51 String randomNumberText = 52 String.format(Locale.getDefault(), "%d!", randomNumber); 53 54 ComplicationData complicationData = null; 55 56 switch (dataType) { 57 case ComplicationData.TYPE_RANGED_VALUE: 58 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_RANGED_VALUE) 59 .setValue(randomNumber) 60 .setMinValue(0) 61 .setMaxValue(10) 62 .setShortText(ComplicationText.plainText(randomNumberText)) 63 .build(); 64 break; 65 case ComplicationData.TYPE_SHORT_TEXT: 66 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) 67 .setShortText(ComplicationText.plainText(randomNumberText)) 68 .build(); 69 break; 70 case ComplicationData.TYPE_LONG_TEXT: 71 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT) 72 .setLongText( 73 ComplicationText.plainText("Random Number: " + randomNumberText)) 74 .build(); 75 break; 76 default: 77 if (Log.isLoggable(TAG, Log.WARN)) { 78 Log.w(TAG, "Unexpected complication type " + dataType); 79 } 80 } 81 82 if (complicationData != null) { 83 complicationManager.updateComplicationData(complicationId, complicationData); 84 } 85 } 86 87 /* 88 * Called when the complication has been deactivated. If you are updating the complication 89 * manager outside of this class with updates, you will want to update your class to stop. 90 */ 91 @Override 92 public void onComplicationDeactivated(int complicationId) { 93 Log.d(TAG, "onComplicationDeactivated(): " + complicationId); 94 super.onComplicationDeactivated(complicationId); 95 } 96 }