Skip to content

Commit a4309ce

Browse files
Merge branch 'main' into ea_profilingmanager_v2_docs_snippets
2 parents 7d374a5 + 07fc354 commit a4309ce

File tree

10 files changed

+368
-1
lines changed

10 files changed

+368
-1
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ coil = "2.7.0"
3939
compileSdk = "36"
4040
compose-latest = "1.9.4"
4141
composeUiTooling = "1.5.4"
42-
coreSplashscreen = "1.0.1"
42+
coreSplashscreen = "1.2.0"
4343
coroutines = "1.10.2"
4444
dataStore = "1.1.7"
4545
datastoreCore = "1.1.7"

wear/src/main/AndroidManifest.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@
2929
android:label="@string/app_name"
3030
android:supportsRtl="true"
3131
android:theme="@android:style/Theme.DeviceDefault">
32+
33+
<!-- [START android_wear_splash_manifest] -->
34+
<activity
35+
android:name=".snippets.SplashScreenActivity"
36+
android:exported="true"
37+
android:taskAffinity=""
38+
android:theme="@style/Theme.App.Starting">
39+
<!-- ... -->
40+
<!-- [START_EXCLUDE silent] -->
41+
<intent-filter>
42+
<action android:name="android.intent.action.MAIN" />
43+
<category android:name="android.intent.category.LAUNCHER" />
44+
</intent-filter>
45+
<!-- [END_EXCLUDE] -->
46+
</activity>
47+
<!-- [END android_wear_splash_manifest] -->
48+
3249
<uses-library
3350
android:name="com.google.android.wearable"
3451
android:required="true" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 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
18+
19+
import android.os.Bundle
20+
import androidx.activity.ComponentActivity
21+
import androidx.activity.compose.setContent
22+
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
23+
24+
// [START android_wear_splash_activity]
25+
class SplashScreenActivity : ComponentActivity() {
26+
override fun onCreate(savedInstanceState: Bundle?) {
27+
installSplashScreen()
28+
super.onCreate(savedInstanceState)
29+
30+
setContent {
31+
WearApp()
32+
}
33+
}
34+
}
35+
// [END android_wear_splash_activity]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.alwayson
18+
19+
import android.os.Bundle
20+
import androidx.activity.ComponentActivity
21+
import androidx.wear.ambient.AmbientLifecycleObserver
22+
23+
// [START android_wear_ongoing_activity_ambientlifecycleobserver]
24+
val ambientCallback = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
25+
override fun onEnterAmbient(ambientDetails: AmbientLifecycleObserver.AmbientDetails) {
26+
// ... Called when moving from interactive mode into ambient mode.
27+
// Adjust UI for low-power state: dim colors, hide non-essential elements.
28+
}
29+
30+
override fun onExitAmbient() {
31+
// ... Called when leaving ambient mode, back into interactive mode.
32+
// Restore full UI.
33+
}
34+
35+
override fun onUpdateAmbient() {
36+
// ... Called by the system periodically (typically once per minute)
37+
// to allow the app to update its display while in ambient mode.
38+
}
39+
}
40+
// [END android_wear_ongoing_activity_ambientlifecycleobserver]
41+
42+
class AmbientLifecycleActivity : ComponentActivity() {
43+
44+
private val activity = this // rename so the snippet reads better
45+
// [START android_wear_ongoing_activity_ambientlifecycleobserver_oncreate]
46+
private val ambientObserver = AmbientLifecycleObserver(activity, ambientCallback)
47+
48+
override fun onCreate(savedInstanceState: Bundle?) {
49+
super.onCreate(savedInstanceState)
50+
lifecycle.addObserver(ambientObserver)
51+
52+
// ...
53+
}
54+
// [END android_wear_ongoing_activity_ambientlifecycleobserver_oncreate]
55+
56+
// [START android_wear_ongoing_activity_ambientlifecycleobserver_ondestroy]
57+
override fun onDestroy() {
58+
super.onDestroy()
59+
lifecycle.removeObserver(ambientObserver)
60+
61+
// ...
62+
}
63+
// [END android_wear_ongoing_activity_ambientlifecycleobserver_ondestroy]
64+
}
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]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START android_wear_splash_drawable] -->
18+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
19+
<item
20+
android:width="@dimen/splash_screen_icon_size"
21+
android:height="@dimen/splash_screen_icon_size"
22+
android:drawable="@mipmap/ic_launcher"
23+
android:gravity="center" />
24+
</layer-list>
25+
<!-- [END android_wear_splash_drawable] -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START android_wear_splash_dimens] -->
18+
<resources>
19+
<!-- Round app icon can take all of default space -->
20+
<dimen name="splash_screen_icon_size">48dp</dimen>
21+
</resources>
22+
<!-- [END android_wear_splash_dimens] -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START android_wear_splash_style] -->
18+
<resources>
19+
<style name="Theme.App" parent="@android:style/Theme.DeviceDefault" />
20+
21+
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
22+
<!-- Set the splash screen background to black -->
23+
<item name="windowSplashScreenBackground">@android:color/black</item>
24+
<!-- Use windowSplashScreenAnimatedIcon to add a drawable or an animated
25+
drawable. -->
26+
<item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen</item>
27+
<!-- Set the theme of the Activity that follows your splash screen. -->
28+
<item name="postSplashScreenTheme">@style/Theme.App</item>
29+
</style>
30+
</resources>
31+
<!-- [END android_wear_splash_style] -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START android_wear_splash_dimens_other] -->
18+
<resources>
19+
<!-- Non-round icon with background must use reduced size to fit circle -->
20+
<dimen name="splash_screen_icon_size">36dp</dimen>
21+
</resources>
22+
<!-- [END android_wear_splash_dimens_other] -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<resources>
18+
<style name="Theme.App" parent="@android:style/Theme.DeviceDefault" />
19+
20+
<!-- [START android_wear_splash_style_other] -->
21+
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
22+
<!-- Set a white background behind the splash screen icon. -->
23+
<item name="windowSplashScreenIconBackgroundColor">@android:color/white</item>
24+
</style>
25+
<!-- [END android_wear_splash_style_other] -->
26+
</resources>

0 commit comments

Comments
 (0)