1 package com.example.android.wearable.watchface.provider;
2 
3 import android.app.PendingIntent;
4 import android.content.Intent;
5 import android.support.wearable.complications.ComplicationData;
6 import android.support.wearable.complications.ComplicationManager;
7 import android.support.wearable.complications.ComplicationProviderService;
8 import android.support.wearable.complications.ComplicationText;
9 import android.util.Log;
10 
11 import java.util.Locale;
12 
13 /**
14  * Example Watch Face Complication data provider provides a random number on every update.
15  */
16 public class RandomNumberProviderService extends ComplicationProviderService {
17 
18     private static final String TAG = "RandomNumberProvider";
19 
20     /*
21      * Called when a complication has been activated. The method is for any one-time
22      * (per complication) set-up.
23      *
24      * You can continue sending data for the active complicationId until onComplicationDeactivated()
25      * is called.
26      */
27     @Override
onComplicationActivated( int complicationId, int dataType, ComplicationManager complicationManager)28     public void onComplicationActivated(
29             int complicationId, int dataType, ComplicationManager complicationManager) {
30         Log.d(TAG, "onComplicationActivated(): " + complicationId);
31         super.onComplicationActivated(complicationId, dataType, complicationManager);
32     }
33 
34     /*
35      * Called when the complication needs updated data from your provider. There are four scenarios
36      * when this will happen:
37      *
38      *   1. An active watch face complication is changed to use this provider
39      *   2. A complication using this provider becomes active
40      *   3. The period of time you specified in the manifest has elapsed (UPDATE_PERIOD_SECONDS)
41      *   4. You triggered an update from your own class via the
42      *       ProviderUpdateRequester.requestUpdate() method.
43      */
44     @Override
onComplicationUpdate( int complicationId, int dataType, ComplicationManager complicationManager)45     public void onComplicationUpdate(
46             int complicationId, int dataType, ComplicationManager complicationManager) {
47         Log.d(TAG, "onComplicationUpdate() id: " + complicationId);
48 
49 
50         // Retrieve your data, in this case, we simply create a random number to display.
51         int randomNumber = (int) Math.floor(Math.random() * 10);
52 
53         String randomNumberText =
54                 String.format(Locale.getDefault(), "%d!", randomNumber);
55 
56         // Create Tap Action so that the user can trigger an update by tapping the complication.
57         Intent updateIntent =
58                 new Intent(getApplicationContext(), UpdateComplicationDataService.class);
59         updateIntent.setAction(UpdateComplicationDataService.ACTION_UPDATE_COMPLICATION);
60         // We pass the complication id, so we can only update the specific complication tapped.
61         updateIntent.putExtra(UpdateComplicationDataService.EXTRA_COMPLICATION_ID, complicationId);
62 
63         PendingIntent pendingIntent = PendingIntent.getService(
64                 getApplicationContext(),
65                 // Set the requestCode to the complication id. This ensures the system doesn't
66                 // combine other PendingIntents with the same context with this one (basically it
67                 // would then reuse the Extra you set in the initial PendingIntent). If you don't
68                 // do this and multiple complications with your data are active, every PendingIntent
69                 // assigned for tap, would use the same complication id (first one created).
70                 complicationId,
71                 updateIntent,
72                 0);
73 
74         ComplicationData complicationData = null;
75 
76         switch (dataType) {
77             case ComplicationData.TYPE_RANGED_VALUE:
78                 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_RANGED_VALUE)
79                         .setValue(randomNumber)
80                         .setMinValue(0)
81                         .setMaxValue(10)
82                         .setShortText(ComplicationText.plainText(randomNumberText))
83                         .setTapAction(pendingIntent)
84                         .build();
85                 break;
86             case ComplicationData.TYPE_SHORT_TEXT:
87                 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
88                         .setShortText(ComplicationText.plainText(randomNumberText))
89                         .setTapAction(pendingIntent)
90                         .build();
91                 break;
92             case ComplicationData.TYPE_LONG_TEXT:
93                 complicationData = new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT)
94                         .setLongText(
95                                 ComplicationText.plainText("Random Number: " + randomNumberText))
96                         .setTapAction(pendingIntent)
97                         .build();
98                 break;
99             default:
100                 if (Log.isLoggable(TAG, Log.WARN)) {
101                     Log.w(TAG, "Unexpected complication type " + dataType);
102                 }
103         }
104 
105         if (complicationData != null) {
106             complicationManager.updateComplicationData(complicationId, complicationData);
107         }
108     }
109 
110     /*
111      * Called when the complication has been deactivated. If you are updating the complication
112      * manager outside of this class with updates, you will want to update your class to stop.
113      */
114     @Override
onComplicationDeactivated(int complicationId)115     public void onComplicationDeactivated(int complicationId) {
116         Log.d(TAG, "onComplicationDeactivated(): " + complicationId);
117         super.onComplicationDeactivated(complicationId);
118     }
119 }