Configuring watch faces

Awesome watch faces can be created to delight the wearer; however, users enjoy the ability to customize the final appearance of any personal technology.

How to do it...

Similar to communicating with wearables, our watch face configuration activity manages settings by synchronizing setting selections from the activities to the watch face service:

  1. We need to create the configuration activity, and apply an [IntentFilter] attribute with a specification value that will be used to associate the watch face with this activity:
    [Activity]
    [IntentFilter(
      new[] { "com.xamarincookbook.wearables.CONFIG" },
      Categories = new[] {
        "com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION",
        Android.Content.Intent.CategoryDefault })]
    public class WearableConfigurationActivity : Activity {
    }
  2. Then, to associate the activity with the watch face service, we apply a [MetaData] attribute using the activities action value as the metadata value:
    [MetaData(
      "com.google.android.wearable.watchface.wearableConfigurationAction",
      Value = "com.xamarincookbook.wearables.CONFIG")]

Typically, we would use one of the Google Play services to synchronize the configuration between the activity and the watch face:

  1. To receive Data API events, we implement the IDataApiDataListener interface on the watch face engine:
    class WatchFaceEngine : IDataApiDataListener{
      public void OnDataChanged(DataEventBufferdataEvents) {
        // update variables
      }
    }
  2. We connect to, and disconnect from, the client in the OnVisibilityChanged() method of the watch face engine, making sure to remove the data listener before disconnecting:
    if (visible) {
      client.Connect();
    }
    else if (client != null &&client.IsConnected) {
      WearableClass.DataApi.RemoveListener(client, this);
      client.Disconnect();
    }

How it works...

Watch faces are great additions to any wearable, but sometimes, we want to provide fewer options so that the user can customize the final appearance. This is very easy to do and requires for us to create an activity that will be launched when the user wants to configure the watch face.

If we create a configuration activity for the wearable device, then a configure button will appear next to the watch face when picking a watch face. Similarly, there is an option to configure the watch face when picking one using the companion app.

Typically, the configuration activity is not launched directly by the user, but rather through the watch face picker. Thus, when adding the [Activity] attribute to our configuration activity, we can set the MainLauncher property to false (or leave it out altogether as false is the default value).

Tip

A watch face configuration activity does not need to appear in the app drawer of the device, because it is launched from the watch face picker.

To link a configuration activity with a watch face, we add attributes to both the configuration activity and the watch face service. Slightly different values will be used if the configuration activity is available on the handheld or wearable.

The configuration activity on the wearable requires us to add an [IntentFilter] attribute. The Actions value should be a name prefixed with the package name. For example, if our package name is com.xamarincookbook.wearables, then the action could be com.xamarincookbook.wearables.CONFIG. The Categories property requires two values to be provided; the first being com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION; and the second being android.intent.category.DEFAULT.

If the configuration activity is on the handheld, then the first category will instead be com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION. The difference is in the last part, with the wearable value ending in WEARABLE_CONFIGURATION and the handheld value ending in COMPANION_CONFIGURATION.

For the watch face service, we specify the wearable configuration activity using a [MetaData] attribute. The Name parameter should be com.google.android.wearable.watchface.wearableConfigurationAction; Value being the value used for the configuration activity's Action value. In our case, the Value will be com.xamarincookbook.wearables.CONFIG.

Note

The value provided as the action for the configuration activity must match the value given to the metadata value for the watch face service.

Similar to the[IntentFilter] attribute, we need a slightly different Name value if the configuration activity is on the handheld. The Name parameter in this case will be com.google.android.wearable.watchface.companionConfigurationAction. Again, the difference is in the last part with the wearable value ending in wearableConfigurationAction, and the handheld value ending in companionConfigurationAction.

Once the service and configuration have their attributes, we can create the configuration activity as we would for any other activity. To send the configuration to the watch face, we usually make use of the Google Play services' Data API. The configuration activity will read the configuration when it is created, and then synchronize any option that a user selects.

Tip

Watch face configuration activities are just normal activities launched by the watch face picker.

One difference between the wearable and handheld configuration activities is what is displayed. As the wearable has a much smaller screen size, we only display the minimum required options. If there are many options, we should rather use the handheld configuration activity.

Tip

Configuration activities can be provided on both wearable and handheld.

A wearable configuration activity is usually very simple with only a few options, such as to select a new background. As a result, we can further enhance the user experience by automatically closing the activity once an option is selected. Then, the user will return to the watch face to see the change without having to manually close the activity.

If the Google Play service is used to manage the watch face settings, then the watch face service can be set up to receive these changes. Just like when we implement the IDataApiDataListener interface for communicating between the wearable and the handheld, we implement the interface on the service. Then, regardless of whether the configuration activity is on the handheld or on the wearable, the watch face service will receive the new data once the user makes changes.

When using the Google Play service with watch faces, we should connect and disconnect the client in the OnVisibilityChanged() method of the service. If the configuration changes when the watch face is invisible, we can always load the changes when the watch face becomes visible again.

There's more...

It is also possible to provide a configuration activity for a watch face on the handheld. This activity is shown when the user configures the watch face from the Android Wear companion app:

  1. Our activity implementation is exactly the same, except we use a different Category value with the [IntentFilter] attribute:
    [IntentFilter(
      new[] { "com.xamarincookbook.wearables.CONFIG" },
      Categories = new[] {
        "com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION",
        Android.Content.Intent.CategoryDefault })]
  2. Similarly, we register a different Value for the [MetaData] attribute on the watch face service:
    [MetaData(
      "com.google.android.wearable.watchface.companionConfigurationAction",
      Value = "com.xamarincookbook.wearables.CONFIG")]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.193.129