ListenableWatchFaceControlClient


public class ListenableWatchFaceControlClient implements WatchFaceControlClient


ListenableFuture-based compatibility wrapper around WatchFaceControlClient's suspending methods. This class is open to allow mocking.

Summary

Public constructors

Public methods

void
HeadlessWatchFaceClient
createHeadlessWatchFaceClient(
    @NonNull ComponentName watchFaceName,
    @NonNull DeviceConfig deviceConfig,
    int surfaceWidth,
    int surfaceHeight
)

This method is deprecated. createHeadlessWatchFaceClient without an id is deprecated

HeadlessWatchFaceClient
createHeadlessWatchFaceClient(
    @NonNull String id,
    @NonNull ComponentName watchFaceName,
    @NonNull DeviceConfig deviceConfig,
    int surfaceWidth,
    int surfaceHeight
)

Creates a HeadlessWatchFaceClient with the specified DeviceConfig.

static final @NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClient>
createWatchFaceControlClient(
    @NonNull Context context,
    @NonNull String watchFacePackageName
)

Returns a ListenableFuture for a ListenableWatchFaceControlClient which attempts to connect to a watch face in the android package watchFacePackageName.

static final @NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClient>
createWatchFaceRuntimeControlClientAsync(
    @NonNull Context context,
    @NonNull String runtimePackageName,
    @NonNull String resourceOnlyWatchFacePackageName
)

Similar createWatchFaceControlClient this constructs a WatchFaceControlClient which attempts to connect to the watch face runtime in the android package runtimePackageName.

@NonNull Map<@NonNull Integer, @NonNull DefaultComplicationDataSourcePolicyAndType>

This method is deprecated. Use the WatchFaceMetadataClient instead.

@NonNull EditorServiceClient
InteractiveWatchFaceClient

Returns the InteractiveWatchFaceClient for the given instance id, or null if no such instance exists.

@NonNull InteractiveWatchFaceClient
getOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData
)

This method is deprecated. Use an overload that specifies a Consumer<String>

@NonNull InteractiveWatchFaceClient
getOrCreateInteractiveWatchFaceClient(
    @NonNull String instanceId,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData,
    @NonNull Executor previewImageUpdateRequestedExecutor,
    @NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener
)

Requests either an existing InteractiveWatchFaceClient with the specified instanceId or schedules creation of an InteractiveWatchFaceClient for the next time the WallpaperService creates an engine.

@NonNull ListenableFuture<@NonNull InteractiveWatchFaceClient>
listenableGetOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData
)

This method is deprecated. Use an overload that specifies Consumer<String>

@NonNull ListenableFuture<@NonNull InteractiveWatchFaceClient>
listenableGetOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData,
    @NonNull Executor previewImageUpdateRequestedExecutor,
    @NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener
)

ListenableFuture wrapper around WatchFaceControlClient.getOrCreateInteractiveWatchFaceClient.

Inherited methods

From androidx.wear.watchface.client.WatchFaceControlClient
boolean

Whether or not the watch face has a ComplicationData cache.

Public constructors

ListenableWatchFaceControlClient

Added in 1.0.0
public ListenableWatchFaceControlClient(
    @NonNull WatchFaceControlClient watchFaceControlClient
)

Public methods

close

Added in 1.0.0
public void close()

createHeadlessWatchFaceClient

Added in 1.0.0
Deprecated in 1.1.0
public HeadlessWatchFaceClient createHeadlessWatchFaceClient(
    @NonNull ComponentName watchFaceName,
    @NonNull DeviceConfig deviceConfig,
    int surfaceWidth,
    int surfaceHeight
)

Creates a HeadlessWatchFaceClient with the specified DeviceConfig. Screenshots made with HeadlessWatchFaceClient.renderWatchFaceToBitmap will be surfaceWidth x surfaceHeight in size.

When finished call HeadlessWatchFaceClient.close to release resources.

Parameters
@NonNull ComponentName watchFaceName

The ComponentName of the watch face to create a headless instance for must be in the same APK the WatchFaceControlClient is connected to. NB a single apk can contain multiple watch faces.

@NonNull DeviceConfig deviceConfig

The hardware DeviceConfig

int surfaceWidth

The width of screen shots taken by the HeadlessWatchFaceClient

int surfaceHeight

The height of screen shots taken by the HeadlessWatchFaceClient

Returns
HeadlessWatchFaceClient

The HeadlessWatchFaceClient or null if watchFaceName is unrecognized.

createHeadlessWatchFaceClient

Added in 1.1.0
public HeadlessWatchFaceClient createHeadlessWatchFaceClient(
    @NonNull String id,
    @NonNull ComponentName watchFaceName,
    @NonNull DeviceConfig deviceConfig,
    int surfaceWidth,
    int surfaceHeight
)

Creates a HeadlessWatchFaceClient with the specified DeviceConfig. Screenshots made with HeadlessWatchFaceClient.renderWatchFaceToBitmap will be surfaceWidth x surfaceHeight in size.

When finished call HeadlessWatchFaceClient.close to release resources.

Parameters
@NonNull String id

The ID for the requested HeadlessWatchFaceClient, will be exposed to the watch face via androidx.wear.watchface.WatchState.watchFaceInstanceId.

@NonNull ComponentName watchFaceName

The ComponentName of the watch face to create a headless instance for must be in the same APK the WatchFaceControlClient is connected to. NB a single apk can contain multiple watch faces.

@NonNull DeviceConfig deviceConfig

The hardware DeviceConfig

int surfaceWidth

The width of screen shots taken by the HeadlessWatchFaceClient

int surfaceHeight

The height of screen shots taken by the HeadlessWatchFaceClient

Returns
HeadlessWatchFaceClient

The HeadlessWatchFaceClient or null if watchFaceName is unrecognized.

createWatchFaceControlClient

Added in 1.0.0
public static final @NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClientcreateWatchFaceControlClient(
    @NonNull Context context,
    @NonNull String watchFacePackageName
)

Returns a ListenableFuture for a ListenableWatchFaceControlClient which attempts to connect to a watch face in the android package watchFacePackageName. Resolves as ServiceNotBoundException if the watch face control service can not be bound.

Note the returned future may resolve immediately on the calling thread or it may resolve asynchronously when the service is connected on a background thread.

Parameters
@NonNull Context context

Calling application's Context.

@NonNull String watchFacePackageName

Name of the package containing the watch face control service to bind to.

Returns
@NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClient>

ListenableFuture<ListenableWatchFaceControlClient> which on success resolves to a ListenableWatchFaceControlClient or throws a ServiceNotBoundException if the watch face control service can not be bound.

createWatchFaceRuntimeControlClientAsync

Added in 1.2.0
public static final @NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClientcreateWatchFaceRuntimeControlClientAsync(
    @NonNull Context context,
    @NonNull String runtimePackageName,
    @NonNull String resourceOnlyWatchFacePackageName
)

Similar createWatchFaceControlClient this constructs a WatchFaceControlClient which attempts to connect to the watch face runtime in the android package runtimePackageName.

A watch face runtime is a special type of watch face, which renders a watch face described by resources in another package resourceOnlyWatchFacePackageName.

Note only one watch face definition per resource only watch face package is supported.

Currently Wear OS only supports the runtime for the Android Watch Face Format (see https://developer.android.com/training/wearables/wff for more details).

Parameters
@NonNull Context context

Calling application's Context.

@NonNull String runtimePackageName

The name of the package containing the watch face runtime's control service to bind to.

@NonNull String resourceOnlyWatchFacePackageName

The name of the package from which to load the resource only watch face. This is exposed to the runtime via the resourceOnlyWatchFacePackageName parameter passed to WatchFaceRuntimeService.createUserStyleSchema, WatchFaceRuntimeService.createComplicationSlotsManager, WatchFaceRuntimeService.createUserStyleFlavors and WatchFaceRuntimeService.createWatchFace).

Returns
@NonNull ListenableFuture<@NonNull ListenableWatchFaceControlClient>

ListenableFuture<ListenableWatchFaceControlClient> which on success resolves to a ListenableWatchFaceControlClient or throws a ServiceNotBoundException if the watch face control service can not be bound.

getDefaultComplicationDataSourcePoliciesAndType

Added in 1.0.0
Deprecated in 1.1.0
public @NonNull Map<@NonNull Integer, @NonNull DefaultComplicationDataSourcePolicyAndTypegetDefaultComplicationDataSourcePoliciesAndType(
    @NonNull ComponentName watchFaceName
)

Returns a map of androidx.wear.watchface.ComplicationSlot id to the DefaultComplicationDataSourcePolicyAndType for each androidx.wear.watchface.ComplicationSlot in the watchface corresponding to watchFaceName. Where possible a fast path is used that doesn't need to fully construct the corresponding watch face.

Parameters
@NonNull ComponentName watchFaceName

The ComponentName of the watch face to obtain the map of DefaultComplicationDataSourcePolicyAndTypes for. It must be in the same APK the WatchFaceControlClient is connected to. NB a single apk can contain multiple watch faces.

getEditorServiceClient

Added in 1.0.0
public @NonNull EditorServiceClient getEditorServiceClient()

getInteractiveWatchFaceClientInstance

Added in 1.0.0
public InteractiveWatchFaceClient getInteractiveWatchFaceClientInstance(@NonNull String instanceId)

Returns the InteractiveWatchFaceClient for the given instance id, or null if no such instance exists.

When finished call InteractiveWatchFaceClient.close to release resources.

Parameters
@NonNull String instanceId

The name of the interactive watch face instance to retrieve

Returns
InteractiveWatchFaceClient

The InteractiveWatchFaceClient or null if instanceId is unrecognized, or ServiceNotBoundException if the WatchFaceControlService is not bound.

getOrCreateInteractiveWatchFaceClient

public @NonNull InteractiveWatchFaceClient getOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData
)

Requests either an existing InteractiveWatchFaceClient with the specified id or schedules creation of an InteractiveWatchFaceClient for the next time the WallpaperService creates an engine.

NOTE that currently only one InteractiveWatchFaceClient per process can exist at a time.

Parameters
@NonNull String id

The ID for the requested InteractiveWatchFaceClient, will be exposed to the watch face via androidx.wear.watchface.WatchState.watchFaceInstanceId.

@NonNull DeviceConfig deviceConfig

The DeviceConfig for the wearable.

@NonNull WatchUiState watchUiState

The initial WatchUiState for the wearable.

UserStyleData userStyle

Optional UserStyleData to apply to the instance (whether or not it's created). If null then the pre-existing user style is preserved (if the instance is created this will be the androidx.wear.watchface.style.UserStyleSchema's default).

Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData

The initial androidx.wear.watchface.ComplicationSlot data, or null if unavailable.

Returns
@NonNull InteractiveWatchFaceClient

The InteractiveWatchFaceClient, this should be closed when finished.

Throws
ServiceStartFailureException

if the watchface dies during startup.

getOrCreateInteractiveWatchFaceClient

public @NonNull InteractiveWatchFaceClient getOrCreateInteractiveWatchFaceClient(
    @NonNull String instanceId,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData,
    @NonNull Executor previewImageUpdateRequestedExecutor,
    @NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener
)

Requests either an existing InteractiveWatchFaceClient with the specified instanceId or schedules creation of an InteractiveWatchFaceClient for the next time the WallpaperService creates an engine.

NOTE that currently only one InteractiveWatchFaceClient per process can exist at a time.

Parameters
@NonNull String instanceId

The ID for the requested InteractiveWatchFaceClient, will be exposed to the watch face via androidx.wear.watchface.WatchState.watchFaceInstanceId.

@NonNull DeviceConfig deviceConfig

The DeviceConfig for the wearable.

@NonNull WatchUiState watchUiState

The initial WatchUiState for the wearable.

UserStyleData userStyle

Optional UserStyleData to apply to the instance (whether or not it's created). If null then the pre-existing user style is preserved (if the instance is created this will be the androidx.wear.watchface.style.UserStyleSchema's default).

Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData

The initial androidx.wear.watchface.ComplicationSlot data, or null if unavailable.

@NonNull Executor previewImageUpdateRequestedExecutor

The Executor on which to run previewImageUpdateRequestedListener if the watch face calls Renderer.sendPreviewImageNeedsUpdateRequest.

@NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener

The Consumer fires when the watch face calls Renderer.sendPreviewImageNeedsUpdateRequest, indicating that it now looks visually different. The string passed to the Consumer is the ID of the watch face (see instanceId passed into getOrCreateInteractiveWatchFaceClient) requesting the update. This will usually match the current watch face but it could also be from a previous watch face if InteractiveWatchFaceClient.updateWatchFaceInstance is called shortly after Renderer.sendPreviewImageNeedsUpdateRequest. The Consumer should Schedule creation of a headless instance to render a new preview image for the instanceId. This is likely an expensive operation and should be rate limited.

Returns
@NonNull InteractiveWatchFaceClient

The InteractiveWatchFaceClient, this should be closed when finished.

Throws
ServiceStartFailureException

if the watchface dies during startup.

listenableGetOrCreateInteractiveWatchFaceClient

public @NonNull ListenableFuture<@NonNull InteractiveWatchFaceClientlistenableGetOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData
)

ListenableFuture wrapper around WatchFaceControlClient.getOrCreateInteractiveWatchFaceClient. This is open to allow mocking.

Parameters
@NonNull String id

The Id of the interactive instance to get or create.

@NonNull DeviceConfig deviceConfig

The DeviceConfig of the interactive instance (only used when creating)

@NonNull WatchUiState watchUiState

The initial WatchUiState for the wearable.

UserStyleData userStyle

Optional UserStyleData to apply to the instance (whether or not it's created). If null then the pre-existing user style is preserved (if the instance is created this will be the androidx.wear.watchface.style.UserStyleSchema's default).

Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData

The initial androidx.wear.watchface.ComplicationSlot data, or null if unavailable.

listenableGetOrCreateInteractiveWatchFaceClient

public @NonNull ListenableFuture<@NonNull InteractiveWatchFaceClientlistenableGetOrCreateInteractiveWatchFaceClient(
    @NonNull String id,
    @NonNull DeviceConfig deviceConfig,
    @NonNull WatchUiState watchUiState,
    UserStyleData userStyle,
    Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData,
    @NonNull Executor previewImageUpdateRequestedExecutor,
    @NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener
)

ListenableFuture wrapper around WatchFaceControlClient.getOrCreateInteractiveWatchFaceClient. This is open to allow mocking.

Parameters
@NonNull String id

The Id of the interactive instance to get or create.

@NonNull DeviceConfig deviceConfig

The DeviceConfig of the interactive instance (only used when creating)

@NonNull WatchUiState watchUiState

The initial WatchUiState for the wearable.

UserStyleData userStyle

Optional UserStyleData to apply to the instance (whether or not it's created). If null then the pre-existing user style is preserved (if the instance is created this will be the androidx.wear.watchface.style.UserStyleSchema's default).

Map<@NonNull Integer, @NonNull ComplicationData> slotIdToComplicationData

The initial androidx.wear.watchface.ComplicationSlot data, or null if unavailable.

@NonNull Executor previewImageUpdateRequestedExecutor

The Executor on which to run previewImageUpdateRequestedListener if the watch face calls Renderer.sendPreviewImageNeedsUpdateRequest.

@NonNull Consumer<@NonNull String> previewImageUpdateRequestedListener

The Consumer fires when the watch face calls Renderer.sendPreviewImageNeedsUpdateRequest, indicating that it now looks visually different. The string passed to the Consumer is the ID of the watch face (see id passed into getOrCreateInteractiveWatchFaceClient) requesting the update. This will usually match the current watch face but it could also be from a previous watch face if InteractiveWatchFaceClient.updateWatchFaceInstance is called shortly after Renderer.sendPreviewImageNeedsUpdateRequest. The Consumer should Schedule creation of a headless instance to render a new preview image for the instanceId. This is likely an expensive operation and should be rate limited.