Skip to content

Commit d43c98f

Browse files
Add snippets for /training/wearables/tiles/debug
1 parent 75fbf5b commit d43c98f

File tree

1 file changed

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

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
package com.example.wear.snippets.tile
18+
19+
import android.content.Context
20+
import androidx.annotation.DrawableRes
21+
import androidx.wear.protolayout.DeviceParametersBuilders.DeviceParameters
22+
import androidx.wear.protolayout.ResourceBuilders
23+
import androidx.wear.protolayout.material3.materialScope
24+
import androidx.wear.protolayout.material3.primaryLayout
25+
import androidx.wear.protolayout.material3.text
26+
import androidx.wear.protolayout.types.layoutString
27+
import androidx.wear.tiles.tooling.preview.Preview
28+
import androidx.wear.tiles.tooling.preview.TilePreviewData
29+
import androidx.wear.tiles.tooling.preview.TilePreviewHelper
30+
import androidx.wear.tooling.preview.devices.WearDevices
31+
import androidx.wear.protolayout.ResourceBuilders.Resources
32+
import androidx.wear.protolayout.expression.DynamicDataBuilders
33+
import androidx.wear.protolayout.expression.PlatformDataValues
34+
import androidx.wear.protolayout.expression.PlatformHealthSources
35+
import com.example.wear.R
36+
37+
// [START android_wear_tile_preview_simple]
38+
@Preview(device = WearDevices.SMALL_ROUND)
39+
@Preview(device = WearDevices.LARGE_ROUND)
40+
fun tilePreview(context: Context) = TilePreviewData { request ->
41+
TilePreviewHelper.singleTimelineEntryTileBuilder(
42+
buildMyTileLayout(context, request.deviceConfiguration)
43+
).build()
44+
}
45+
// [END android_wear_tile_preview_simple]
46+
47+
fun buildMyTileLayout(
48+
context: Context,
49+
deviceParameters: DeviceParameters,
50+
) = materialScope(context = context, deviceConfiguration = deviceParameters) {
51+
primaryLayout(
52+
mainSlot = {
53+
text("Hello world!".layoutString)
54+
}
55+
)
56+
}
57+
58+
private const val RESOURCES_VERSION = "1"
59+
private const val myImageId = "myImageId"
60+
61+
// [START android_wear_tile_preview_resources]
62+
@Preview(device = WearDevices.SMALL_ROUND)
63+
fun previewWithResources(context: Context) = TilePreviewData(
64+
onTileResourceRequest = { request ->
65+
Resources.Builder()
66+
.setVersion(RESOURCES_VERSION)
67+
.addIdToImageMapping(
68+
myImageId, getImageById(R.drawable.animated_walk))
69+
.build()
70+
},
71+
onTileRequest = { request ->
72+
TilePreviewHelper.singleTimelineEntryTileBuilder(
73+
buildMyTileLayout(context, request.deviceConfiguration)
74+
).build()
75+
}
76+
)
77+
// [END android_wear_tile_preview_resources]
78+
79+
fun getImageById(
80+
@DrawableRes id: Int,
81+
): ResourceBuilders.ImageResource =
82+
ResourceBuilders.ImageResource.Builder()
83+
.setAndroidResourceByResId(
84+
ResourceBuilders.AndroidImageResourceByResId.Builder()
85+
.setResourceId(id)
86+
.build(),
87+
)
88+
.build()
89+
90+
// [START android_wear_tile_preview_platform]
91+
@Preview(device = WearDevices.SMALL_ROUND)
92+
fun previewWithPlatformOverride(context: Context) = TilePreviewData(
93+
platformDataValues = PlatformDataValues.of(
94+
PlatformHealthSources.Keys.HEART_RATE_BPM,
95+
DynamicDataBuilders.DynamicDataValue.fromFloat(160f)
96+
),
97+
onTileRequest = { request ->
98+
TilePreviewHelper.singleTimelineEntryTileBuilder(
99+
buildMyTileLayout(context, request.deviceConfiguration)
100+
).build()
101+
}
102+
)
103+
// [END android_wear_tile_preview_platform]

0 commit comments

Comments
 (0)