WatchFaceService

public abstract class WatchFaceService

Known direct subclasses

WatchFaceService and WatchFace are a pair of classes intended to handle much of the boilerplate needed to implement a watch face without being too opinionated. The suggested structure of a WatchFaceService based watch face is:

import android.graphics.RectF
import androidx.wear.complications.ComplicationSlotBounds
import androidx.wear.complications.DefaultComplicationDataSourcePolicy
import androidx.wear.watchface.CanvasComplicationFactory
import androidx.wear.watchface.ComplicationSlotsManager
import androidx.wear.watchface.WatchFace
import androidx.wear.watchface.complications.rendering.CanvasComplicationDrawable
import androidx.wear.watchface.complications.rendering.ComplicationDrawable
import androidx.wear.watchface.style.UserStyleSchema
import androidx.wear.watchface.style.UserStyleSetting.ListUserStyleSetting

class ExampleCanvasWatchFaceService : WatchFaceService() {
    override fun createUserStyleSchema() =
        UserStyleSchema(
            listOf(
                ListUserStyleSetting(
                    UserStyleSetting.Id("color_style_setting"),
                    "Colors",
                    "Watchface colorization",
                    icon = null,
                    options = listOf(
                        ListUserStyleSetting.ListOption(
                            Option.Id("red_style"),
                            "Red",
                            icon = null
                        ),
                        ListUserStyleSetting.ListOption(
                            Option.Id("green_style"),
                            "Green",
                            icon = null
                        ),
                        ListUserStyleSetting.ListOption(
                            Option.Id("blue_style"),
                            "Blue",
                            icon = null
                        )
                    ),
                    listOf(
                        WatchFaceLayer.BASE,
                        WatchFaceLayer.COMPLICATIONS,
                        WatchFaceLayer.COMPLICATIONS_OVERLAY
                    )
                ),
                ListUserStyleSetting(
                    UserStyleSetting.Id("hand_style_setting"),
                    "Hand Style",
                    "Hand visual look",
                    icon = null,
                    options = listOf(
                        ListUserStyleSetting.ListOption(
                            Option.Id("classic_style"), "Classic", icon = null
                        ),
                        ListUserStyleSetting.ListOption(
                            Option.Id("modern_style"), "Modern", icon = null
                        ),
                        ListUserStyleSetting.ListOption(
                            Option.Id("gothic_style"),
                            "Gothic",
                            icon = null
                        )
                    ),
                    listOf(WatchFaceLayer.COMPLICATIONS_OVERLAY)
                )
            )
        )

    override fun createComplicationSlotsManager(
        currentUserStyleRepository: CurrentUserStyleRepository
    ): ComplicationSlotsManager {
        val canvasComplicationFactory =
            CanvasComplicationFactory { watchState, listener ->
                CanvasComplicationDrawable(ComplicationDrawable(this), watchState, listener)
            }
        return ComplicationSlotsManager(
            listOf(
                ComplicationSlot.createRoundRectComplicationSlotBuilder(
                    /*id */ 0,
                    canvasComplicationFactory,
                    listOf(
                        ComplicationType.RANGED_VALUE,
                        ComplicationType.LONG_TEXT,
                        ComplicationType.SHORT_TEXT,
                        ComplicationType.MONOCHROMATIC_IMAGE,
                        ComplicationType.SMALL_IMAGE
                    ),
                    DefaultComplicationDataSourcePolicy(
                        SystemDataSources.DATA_SOURCE_DAY_OF_WEEK
                    ),
                    ComplicationSlotBounds(RectF(0.15625f, 0.1875f, 0.84375f, 0.3125f))
                ).setDefaultDataSourceType(ComplicationType.SHORT_TEXT)
                    .build(),
                ComplicationSlot.createRoundRectComplicationSlotBuilder(
                    /*id */ 1,
                    canvasComplicationFactory,
                    listOf(
                        ComplicationType.RANGED_VALUE,
                        ComplicationType.LONG_TEXT,
                        ComplicationType.SHORT_TEXT,
                        ComplicationType.MONOCHROMATIC_IMAGE,
                        ComplicationType.SMALL_IMAGE
                    ),
                    DefaultComplicationDataSourcePolicy(
                        SystemDataSources.DATA_SOURCE_STEP_COUNT
                    ),
                    ComplicationSlotBounds(RectF(0.1f, 0.5625f, 0.35f, 0.8125f))
                ).setDefaultDataSourceType(ComplicationType.SHORT_TEXT)
                    .build()
            ),
            currentUserStyleRepository
        )
    }

    override suspend fun createWatchFace(
        surfaceHolder: SurfaceHolder,
        watchState: WatchState,
        complicationSlotsManager: ComplicationSlotsManager,
        currentUserStyleRepository: CurrentUserStyleRepository
    ) = WatchFace(
        WatchFaceType.ANALOG,
        object : Renderer.CanvasRenderer(
            surfaceHolder,
            currentUserStyleRepository,
            watchState,
            CanvasType.HARDWARE,
            /* interactiveUpdateRateMillis */ 16,
        ) {
            init {
                currentUserStyleRepository.addUserStyleChangeListener(
                    object : CurrentUserStyleRepository.UserStyleChangeListener {
                        override fun onUserStyleChanged(userStyle: UserStyle) {
                            // `userStyle` will contain two userStyle categories with options
                            // from the lists above. ...
                        }
                    }
                )
            }

            override fun render(
                canvas: Canvas,
                bounds: Rect,
                zonedDateTime: ZonedDateTime
            ) {
                // ...
            }

            override fun renderHighlightLayer(
                canvas: Canvas,
                bounds: Rect,
                zonedDateTime: ZonedDateTime
            ) {
                canvas.drawColor(renderParameters.highlightLayer!!.backgroundTint)

                // ...
            }
        }
    )
}

return ExampleCanvasWatchFaceService()

Sub classes of WatchFaceService are expected to implement createWatchFace which is the factory for making WatchFaces. If the watch faces uses complications then createComplicationSlotsManager should be overridden. All ComplicationSlots are assumed to be enumerated up upfront and passed as a collection into ComplicationSlotsManager's constructor which is returned by createComplicationSlotsManager.

Watch face styling (color and visual look of watch face elements such as numeric fonts, watch hands and ticks, etc...) and companion editing is directly supported via UserStyleSchema and UserStyleSetting. To enable support for styling override createUserStyleSchema.

WatchFaces are initially constructed on a background thread before being used exclusively on the ui thread afterwards. There is a memory barrier between construction and rendering so no special threading primitives are required.

To aid debugging watch face animations, WatchFaceService allows you to speed up or slow down time, and to loop between two instants. This is controlled by MOCK_TIME_INTENT intents with a float extra called "androidx.wear.watchface.extra.MOCK_TIME_SPEED_MULTIPLIE" and to long extras called "androidx.wear.watchface.extra.MOCK_TIME_WRAPPING_MIN_TIME" and "androidx.wear.watchface.extra.MOCK_TIME_WRAPPING_MAX_TIME" (which are UTC time in milliseconds). If minTime is omitted or set to -1 then the current time is sampled as minTime.

E.g, to make time go twice as fast: adb shell am broadcast -a androidx.wear.watchface.MockTime
--ef androidx.wear.watchface.extra.MOCK_TIME_SPEED_MULTIPLIER 2.0

To use the sample on watch face editor UI, import the wear:wear-watchface-editor-samples library and add the following into your watch face's AndroidManifest.xml:

<activity
android:name="androidx.wear.watchface.editor.sample.WatchFaceConfigActivity"
android:exported="true"
android:label="Config"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="androidx.wear.watchface.editor.action.WATCH_FACE_EDITOR" />
<category android:name=
"com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

To register a WatchFaceService with the system add a tag to the in your watch face's AndroidManifest.xml:

<service
android:name=".MyWatchFaceServiceClass"
android:exported="true"
android:label="@string/watch_face_name"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
</intent-filter>
<meta-data
android:name="com.google.android.wearable.watchface.preview"
android:resource="@drawable/my_watch_preview" />
<meta-data
android:name="com.google.android.wearable.watchface.preview_circular"
android:resource="@drawable/my_watch_circular_preview" />
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="androidx.wear.watchface.editor.action.WATCH_FACE_EDITOR"/>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/watch_face" />
<meta-data
android:name=
"com.google.android.wearable.watchface.companionBuiltinConfigurationEnabled"
android:value="true" />
</service>

Multiple watch faces can be defined in the same package, requiring multiple tags.

Summary

Public constructors

Public methods

final @NonNull Handler

Returns the lazily constructed background thread Handler.

final @NonNull Handler

Returns the ui thread Handler.

final @NonNull WallpaperService.Engine

Creates an interactive engine for WallpaperService.

Inherited methods

From class Context
From class ContextWrapper
boolean
bindIsolatedService(
    @NonNull Intent p0,
    int p1,
    @NonNull String p2,
    @NonNull Executor p3,
    @NonNull ServiceConnection p4
)
boolean
boolean
bindService(
    @NonNull Intent p0,
    int p1,
    @NonNull Executor p2,
    @NonNull ServiceConnection p3
)
boolean
bindServiceAsUser(
    @NonNull Intent p0,
    @NonNull ServiceConnection p1,
    int p2,
    @NonNull UserHandle p3
)
int
int
@NonNull int[]
int
int
@NonNull int[]
int
checkPermission(@NonNull String p0, int p1, int p2)
int
int
checkUriPermission(@NonNull Uri p0, int p1, int p2, int p3)
int
checkUriPermission(
    @Nullable Uri p0,
    @Nullable String p1,
    @Nullable String p2,
    int p3,
    int p4,
    int p5
)
@NonNull int[]
checkUriPermissions(@NonNull List<@NonNull Uri> p0, int p1, int p2, int p3)
void

This method is deprecated. Deprecated in Java

@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Array<@NonNull String>
boolean
boolean
boolean
void
void
enforceCallingOrSelfUriPermission(
    @NonNull Uri p0,
    int p1,
    @NonNull String p2
)
void
void
void
enforcePermission(@NonNull String p0, int p1, int p2, @Nullable String p3)
void
enforceUriPermission(
    @NonNull Uri p0,
    int p1,
    int p2,
    int p3,
    @NonNull String p4
)
void
enforceUriPermission(
    @Nullable Uri p0,
    @Nullable String p1,
    @Nullable String p2,
    int p3,
    int p4,
    int p5,
    @Nullable String p6
)
@NonNull Array<@NonNull String>
@NonNull Context
@NonNull ApplicationInfo
@NonNull AssetManager