Skip to content

Commit 07fc354

Browse files
Add snippets for /training/wearables/tiles/debug (#694)
* Add snippets for /training/wearables/tiles/debug * Create dynamic preview * Suppress lint warnings for code sample
1 parent c54efa0 commit 07fc354

File tree

1 file changed

+125
-0
lines changed
  • wear/src/main/java/com/example/wear/snippets/tile

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("MissingPermission")
18+
19+
package com.example.wear.snippets.tile
20+
21+
import android.content.Context
22+
import androidx.annotation.DrawableRes
23+
import androidx.wear.protolayout.DeviceParametersBuilders.DeviceParameters
24+
import androidx.wear.protolayout.ResourceBuilders
25+
import androidx.wear.protolayout.material3.materialScope
26+
import androidx.wear.protolayout.material3.primaryLayout
27+
import androidx.wear.protolayout.material3.text
28+
import androidx.wear.protolayout.types.stringLayoutConstraint
29+
import androidx.wear.tiles.tooling.preview.Preview
30+
import androidx.wear.tiles.tooling.preview.TilePreviewData
31+
import androidx.wear.tiles.tooling.preview.TilePreviewHelper
32+
import androidx.wear.tooling.preview.devices.WearDevices
33+
import androidx.wear.protolayout.ResourceBuilders.Resources
34+
import androidx.wear.protolayout.expression.DynamicDataBuilders
35+
import androidx.wear.protolayout.expression.PlatformDataValues
36+
import androidx.wear.protolayout.expression.PlatformHealthSources
37+
import com.example.wear.R
38+
import androidx.wear.protolayout.types.layoutString
39+
import androidx.wear.protolayout.types.asLayoutString
40+
41+
// [START android_wear_tile_preview_simple]
42+
@Preview(device = WearDevices.SMALL_ROUND)
43+
@Preview(device = WearDevices.LARGE_ROUND)
44+
fun tilePreview(context: Context) = TilePreviewData { request ->
45+
TilePreviewHelper.singleTimelineEntryTileBuilder(
46+
buildMyTileLayout(context, request.deviceConfiguration)
47+
).build()
48+
}
49+
// [END android_wear_tile_preview_simple]
50+
51+
fun buildMyTileLayout(
52+
context: Context,
53+
deviceParameters: DeviceParameters,
54+
) = materialScope(context = context, deviceConfiguration = deviceParameters) {
55+
primaryLayout(
56+
mainSlot = {
57+
text("Hello world!".layoutString)
58+
}
59+
)
60+
}
61+
62+
private const val RESOURCES_VERSION = "1"
63+
private const val myImageId = "myImageId"
64+
65+
// [START android_wear_tile_preview_resources]
66+
@Preview(device = WearDevices.SMALL_ROUND)
67+
fun previewWithResources(context: Context) = TilePreviewData(
68+
onTileResourceRequest = { request ->
69+
Resources.Builder()
70+
.setVersion(RESOURCES_VERSION)
71+
.addIdToImageMapping(
72+
myImageId,
73+
getImageById(R.drawable.animated_walk)
74+
)
75+
.build()
76+
},
77+
onTileRequest = { request ->
78+
TilePreviewHelper.singleTimelineEntryTileBuilder(
79+
buildMyTileLayout(context, request.deviceConfiguration)
80+
).build()
81+
}
82+
)
83+
// [END android_wear_tile_preview_resources]
84+
85+
fun getImageById(
86+
@DrawableRes id: Int,
87+
): ResourceBuilders.ImageResource =
88+
ResourceBuilders.ImageResource.Builder()
89+
.setAndroidResourceByResId(
90+
ResourceBuilders.AndroidImageResourceByResId.Builder()
91+
.setResourceId(id)
92+
.build(),
93+
)
94+
.build()
95+
96+
fun buildMyTileLayoutDynamic(
97+
context: Context,
98+
deviceParameters: DeviceParameters,
99+
) = materialScope(context = context, deviceConfiguration = deviceParameters) {
100+
primaryLayout(
101+
mainSlot = {
102+
text(
103+
text =
104+
PlatformHealthSources.heartRateBpm()
105+
.format()
106+
.asLayoutString("--", stringLayoutConstraint("999"))
107+
)
108+
}
109+
)
110+
}
111+
112+
// [START android_wear_tile_preview_platform]
113+
@Preview(device = WearDevices.SMALL_ROUND)
114+
fun previewWithPlatformOverride(context: Context) = TilePreviewData(
115+
platformDataValues = PlatformDataValues.of(
116+
PlatformHealthSources.Keys.HEART_RATE_BPM,
117+
DynamicDataBuilders.DynamicDataValue.fromFloat(160f)
118+
),
119+
onTileRequest = { request ->
120+
TilePreviewHelper.singleTimelineEntryTileBuilder(
121+
buildMyTileLayoutDynamic(context, request.deviceConfiguration)
122+
).build()
123+
}
124+
)
125+
// [END android_wear_tile_preview_platform]

0 commit comments

Comments
 (0)