Skip to content

Commit 3970dda

Browse files
Adds ambient snippet in Compose (#974)
* Adds ambient snippet in Compose * Apply Spotless * Restores AlwaysOnActivity * Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Apply Spotless * Fixes compilation error introduced by Gemini assist * Restore deprecated snippets for now * Restore deprecated snippets for now * Apply Spotless --------- Co-authored-by: garanj <25058277+garanj@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent a1b2902 commit 3970dda

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class AlwaysOnActivity : ComponentActivity() {
8383
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
8484
when {
8585
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
86-
PackageManager.PERMISSION_GRANTED -> {
86+
PackageManager.PERMISSION_GRANTED -> {
8787
Log.d(TAG, "POST_NOTIFICATIONS permission already granted")
8888
}
8989
shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
@@ -186,7 +186,7 @@ fun WearApp() {
186186
if (runningService != null) {
187187
Log.d(TAG, "Stopping ${runningService?.simpleName}")
188188
context.stopService(Intent(context, runningService))
189-
}
189+
}
190190
Log.d(TAG, "Starting ${serviceClass.simpleName}")
191191
val intent = Intent(context, serviceClass)
192192
context.startForegroundService(intent)
@@ -208,4 +208,4 @@ fun WearApp() {
208208
}
209209
}
210210
}
211-
}
211+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2026 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 androidx.compose.foundation.layout.Arrangement
20+
import androidx.compose.foundation.layout.Column
21+
import androidx.compose.foundation.layout.fillMaxSize
22+
import androidx.compose.runtime.Composable
23+
import androidx.compose.runtime.CompositionLocalProvider
24+
import androidx.compose.ui.Alignment
25+
import androidx.compose.ui.Modifier
26+
import androidx.wear.compose.foundation.AmbientMode
27+
import androidx.wear.compose.foundation.AmbientTickEffect
28+
import androidx.wear.compose.foundation.LocalAmbientModeManager
29+
import androidx.wear.compose.foundation.rememberAmbientModeManager
30+
import androidx.wear.compose.material3.Text
31+
32+
@Composable
33+
fun AmbientModeBasicSample() {
34+
// [START android_wear_ongoing_activity_ambient_compose]
35+
// In a production application, the AmbientModeManager should be instantiated and provided at
36+
// the highest level of the Compose hierarchy (typically in the host Activity's setContent
37+
// block) using a CompositionLocalProvider. This ensures proper lifecycle management and
38+
// broad accessibility.
39+
40+
// For this self-contained demo, AmbientModeManager is created and provided locally:
41+
val activityAmbientModeManager = rememberAmbientModeManager()
42+
CompositionLocalProvider(LocalAmbientModeManager provides activityAmbientModeManager) {
43+
val ambientModeManager = LocalAmbientModeManager.current
44+
val ambientMode = ambientModeManager?.currentAmbientMode
45+
46+
if (ambientModeManager != null) {
47+
ambientModeManager.AmbientTickEffect {
48+
// While device is in ambient mode, update properties every minute or so
49+
// ...
50+
}
51+
}
52+
53+
// [START_EXCLUDE]
54+
Column(
55+
verticalArrangement = Arrangement.Center,
56+
horizontalAlignment = Alignment.CenterHorizontally,
57+
modifier = Modifier.fillMaxSize(),
58+
) {
59+
// [END_EXCLUDE]
60+
val ambientModeName =
61+
when (ambientMode) {
62+
is AmbientMode.Interactive -> "Interactive"
63+
is AmbientMode.Ambient -> "Ambient"
64+
else -> "Unknown"
65+
}
66+
67+
Text(text = "$ambientModeName Mode")
68+
// [START_EXCLUDE]
69+
}
70+
// [END_EXCLUDE]
71+
}
72+
// [END android_wear_ongoing_activity_ambient_compose]
73+
}

0 commit comments

Comments
 (0)