diff --git a/.github/workflows/apply_spotless.yml b/.github/workflows/apply_spotless.yml
index 5ba1f6fe5..d69f817b0 100644
--- a/.github/workflows/apply_spotless.yml
+++ b/.github/workflows/apply_spotless.yml
@@ -42,13 +42,7 @@ jobs:
java-version: '17'
- name: Run spotlessApply
- run: ./gradlew :compose:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
-
- - name: Run spotlessApply for Wear
- run: ./gradlew :wear:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
-
- - name: Run spotlessApply for Misc
- run: ./gradlew :misc:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
+ run: ./gradlew spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
- name: Auto-commit if spotlessApply has changes
uses: stefanzweifel/git-auto-commit-action@v5
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 97b36f468..72e815a31 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -37,13 +37,7 @@ jobs:
with:
distribution: 'zulu'
java-version: '17'
- - name: Build Compose
- run: ./gradlew :compose:snippets:build
- - name: Build recompose snippets
- run: ./gradlew :compose:recomposehighlighter:build
- - name: Build kotlin snippets
- run: ./gradlew :kotlin:build
- - name: Build Wear snippets
- run: ./gradlew :wear:build
- - name: Build misc snippets
- run: ./gradlew :misc:build
+ - name: Build All
+ run: ./gradlew build --stacktrace
+ - name: Build Watch Face Push validation snippets
+ run: ./gradlew :watchfacepush:validator:run --stacktrace
diff --git a/.gitignore b/.gitignore
index 30e5f7bdc..9b10be5e8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,7 @@
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
-/build
+build
/captures
.externalNativeBuild
.idea/*
diff --git a/bluetoothle/src/main/AndroidManifest.xml b/bluetoothle/src/main/AndroidManifest.xml
index 1661e746b..d15b213e2 100644
--- a/bluetoothle/src/main/AndroidManifest.xml
+++ b/bluetoothle/src/main/AndroidManifest.xml
@@ -1,6 +1,5 @@
-
+
@@ -23,6 +22,6 @@
+
-
\ No newline at end of file
diff --git a/bluetoothle/src/main/java/com/sample/android/bluetoothle/java/MainActivity.java b/bluetoothle/src/main/java/com/sample/android/bluetoothle/java/MainActivity.java
index b296ce70b..5e25d58e6 100644
--- a/bluetoothle/src/main/java/com/sample/android/bluetoothle/java/MainActivity.java
+++ b/bluetoothle/src/main/java/com/sample/android/bluetoothle/java/MainActivity.java
@@ -1,13 +1,15 @@
package com.sample.android.bluetoothle.java;
-import androidx.appcompat.app.AppCompatActivity;
-
+import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import androidx.annotation.RequiresPermission;
+import androidx.appcompat.app.AppCompatActivity;
+
import com.sample.android.bluetoothle.R;
public class MainActivity extends AppCompatActivity {
@@ -19,6 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
}
+ @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
private void setUpBLE() {
// [START get_bluetooth_adapter]
// Initializes Bluetooth adapter.
diff --git a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/DeviceScanActivity.kt b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/DeviceScanActivity.kt
index fd1662998..585a9e3f5 100644
--- a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/DeviceScanActivity.kt
+++ b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/DeviceScanActivity.kt
@@ -1,10 +1,28 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.sample.android.bluetoothle.kotlin
+import android.Manifest
import android.app.ListActivity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.os.Handler
+import androidx.annotation.RequiresPermission
import com.sample.android.bluetoothle.java.LeDeviceListAdapter
/**
@@ -31,6 +49,7 @@ class DeviceScanActivity : ListActivity() {
// Stops scanning after 10 seconds.
private val SCAN_PERIOD: Long = 10000
+ @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
private fun scanLeDevice() {
if (!mScanning) { // Stops scanning after a pre-defined scan period.
handler.postDelayed({
@@ -45,4 +64,4 @@ class DeviceScanActivity : ListActivity() {
}
}
// [END start_and_stop_scan]
-}
\ No newline at end of file
+}
diff --git a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/LeDeviceListAdapter.kt b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/LeDeviceListAdapter.kt
index c8670e094..42e37c07e 100644
--- a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/LeDeviceListAdapter.kt
+++ b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/LeDeviceListAdapter.kt
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.sample.android.bluetoothle.kotlin
import android.bluetooth.BluetoothClass
@@ -5,11 +21,11 @@ import android.bluetooth.BluetoothDevice
import android.content.Context
import android.widget.ArrayAdapter
-class LeDeviceListAdapter(context: Context?, layout: Int)
- : ArrayAdapter(context!!, layout) {
+class LeDeviceListAdapter(context: Context?, layout: Int) :
+ ArrayAdapter(context!!, layout) {
fun addDevice(device: BluetoothDevice?) {
// This is where you can add devices to the adapter to
// show a list of discovered devices in the UI.
}
-}
\ No newline at end of file
+}
diff --git a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt
index 5555c720d..95fe7df91 100644
--- a/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt
+++ b/bluetoothle/src/main/java/com/sample/android/bluetoothle/kotlin/MainActivity.kt
@@ -1,10 +1,28 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.sample.android.bluetoothle.kotlin
+import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
+import androidx.annotation.RequiresPermission
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
@@ -15,6 +33,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
}
+ @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
private fun setUpBLE() {
// [START get_bluetooth_adapter]
// Initializes Bluetooth adapter.
diff --git a/buildscripts/toml-updater-config.gradle b/buildscripts/toml-updater-config.gradle
index 6441ad0f0..f7c9af0a5 100644
--- a/buildscripts/toml-updater-config.gradle
+++ b/buildscripts/toml-updater-config.gradle
@@ -19,10 +19,6 @@ versionCatalogUpdate {
keep {
// keep versions without any library or plugin reference
keepUnusedVersions.set(true)
- // keep all libraries that aren't used in the project
- keepUnusedLibraries.set(true)
- // keep all plugins that aren't used in the project
- keepUnusedPlugins.set(true)
}
}
diff --git a/compose/recomposehighlighter/src/main/java/com/example/android/compose/recomposehighlighter/RecomposeHighlighter.kt b/compose/recomposehighlighter/src/main/java/com/example/android/compose/recomposehighlighter/RecomposeHighlighter.kt
index fdcbbda30..cc92daed6 100644
--- a/compose/recomposehighlighter/src/main/java/com/example/android/compose/recomposehighlighter/RecomposeHighlighter.kt
+++ b/compose/recomposehighlighter/src/main/java/com/example/android/compose/recomposehighlighter/RecomposeHighlighter.kt
@@ -89,6 +89,11 @@ private class RecomposeHighlighterModifier : Modifier.Node(), DrawModifierNode {
override val shouldAutoInvalidate: Boolean = false
+ override fun onReset() {
+ totalCompositions = 0
+ timerJob?.cancel()
+ }
+
override fun onDetach() {
timerJob?.cancel()
}
diff --git a/compose/snippets/build.gradle.kts b/compose/snippets/build.gradle.kts
index 8b644b694..c8d728da0 100644
--- a/compose/snippets/build.gradle.kts
+++ b/compose/snippets/build.gradle.kts
@@ -160,6 +160,7 @@ dependencies {
debugImplementation(libs.androidx.compose.ui.tooling)
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
+ androidTestImplementation(libs.androidx.compose.ui.test.junit4.accessibility)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}
diff --git a/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt b/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
index 724876a87..ee6b51325 100644
--- a/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
+++ b/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
@@ -16,25 +16,109 @@
package com.example.compose.snippets.accessibility
+import androidx.activity.ComponentActivity
+import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.size
+import androidx.compose.material3.Text
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.SemanticsActions
+import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.getOrNull
+import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
+import androidx.compose.ui.test.junit4.accessibility.enableAccessibilityChecks
import androidx.compose.ui.test.junit4.createAndroidComposeRule
-import com.example.compose.snippets.MyActivity
+import androidx.compose.ui.test.onRoot
+import androidx.compose.ui.test.tryPerformAccessibilityChecks
+import androidx.compose.ui.unit.dp
+import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResult
+import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
-class AccessibilitySnippetsTest {
+class AccessibilityTest {
+
+// [START android_compose_accessibility_testing_label]
@Rule
@JvmField
- val composeTestRule = createAndroidComposeRule()
+ val composeTestRule = createAndroidComposeRule()
+
+ @Test
+ fun noAccessibilityLabel() {
+ composeTestRule.setContent {
+ Box(
+ modifier = Modifier
+ .size(50.dp, 50.dp)
+ .background(color = Color.Gray)
+ .clickable { }
+ .semantics {
+ contentDescription = ""
+ }
+ )
+ }
- private val nodeMatcher = SemanticsMatcher("DUMMY") { it.isRoot }
+ composeTestRule.enableAccessibilityChecks()
- @Ignore("Dummy test")
+ // Any action (such as performClick) will perform accessibility checks too:
+ composeTestRule.onRoot().tryPerformAccessibilityChecks()
+ }
+// [END android_compose_accessibility_testing_label]
+// [START android_compose_accessibility_testing_click]
+ @Test
+ fun smallClickTarget() {
+ composeTestRule.setContent {
+ Box(
+ modifier = Modifier
+ .size(20.dp, 20.dp)
+ .background(color = Color(0xFFFAFBFC))
+ .clickable { }
+ )
+ }
+
+ composeTestRule.enableAccessibilityChecks()
+
+ // Any action (such as performClick) will perform accessibility checks too:
+ composeTestRule.onRoot().tryPerformAccessibilityChecks()
+ }
+// [END android_compose_accessibility_testing_click]
+
+// [START android_compose_accessibility_testing_validator]
+ @Test
+ fun lowContrastScreen() {
+ composeTestRule.setContent {
+ Box(
+ modifier = Modifier
+ .fillMaxSize()
+ .background(color = Color(0xFFFAFBFC)),
+ contentAlignment = Alignment.Center
+ ) {
+ Text(text = "Hello", color = Color(0xFFB0B1B2))
+ }
+ }
+
+ // Optionally, set AccessibilityValidator manually
+ val accessibilityValidator = AccessibilityValidator()
+ .setThrowExceptionFor(
+ AccessibilityCheckResult.AccessibilityCheckResultType.WARNING
+ )
+
+ composeTestRule.enableAccessibilityChecks(accessibilityValidator)
+
+ composeTestRule.onRoot().tryPerformAccessibilityChecks()
+ }
+// [END android_compose_accessibility_testing_validator]
+
+ private val nodeMatcher = SemanticsMatcher(description = "DUMMY") { it.isRoot }
+
+ @Ignore("Dummy test")
// [START android_compose_accessibility_testing]
@Test
fun test() {
diff --git a/compose/snippets/src/main/AndroidManifest.xml b/compose/snippets/src/main/AndroidManifest.xml
index c9b15e6c5..4ec4d9b70 100644
--- a/compose/snippets/src/main/AndroidManifest.xml
+++ b/compose/snippets/src/main/AndroidManifest.xml
@@ -27,6 +27,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
+ android:enableOnBackInvokedCallback="true"
android:theme="@style/Theme.Snippets">
TooltipExamples()
TopComponentsDestination.NavigationDrawerExamples -> NavigationDrawerExamples()
TopComponentsDestination.SegmentedButtonExamples -> SegmentedButtonExamples()
+ TopComponentsDestination.SwipeToDismissBoxExamples -> SwipeToDismissBoxExamples()
+ TopComponentsDestination.SearchBarExamples -> SearchBarExamples()
}
}
}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
index 3c6be8afc..a83111f71 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt
@@ -19,9 +19,11 @@
package com.example.compose.snippets.accessibility
import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
+import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
@@ -34,6 +36,7 @@ import androidx.compose.foundation.selection.toggleable
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Add
+import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Checkbox
@@ -44,10 +47,13 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
+import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
+import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@@ -58,16 +64,29 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.semantics.CollectionInfo
+import androidx.compose.ui.semantics.CollectionItemInfo
import androidx.compose.ui.semantics.CustomAccessibilityAction
+import androidx.compose.ui.semantics.LiveRegionMode
+import androidx.compose.ui.semantics.ProgressBarRangeInfo
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
+import androidx.compose.ui.semantics.collectionInfo
+import androidx.compose.ui.semantics.collectionItemInfo
import androidx.compose.ui.semantics.customActions
import androidx.compose.ui.semantics.heading
+import androidx.compose.ui.semantics.hideFromAccessibility
import androidx.compose.ui.semantics.isTraversalGroup
+import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.onClick
+import androidx.compose.ui.semantics.paneTitle
+import androidx.compose.ui.semantics.progressBarRangeInfo
+import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
+import androidx.compose.ui.semantics.toggleableState
import androidx.compose.ui.semantics.traversalIndex
+import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.compose.snippets.R
@@ -155,7 +174,7 @@ private fun LargeBox() {
// [START android_compose_accessibility_click_label]
@Composable
-private fun ArticleListItem(openArticle: () -> Unit) {
+private fun ArticleListItem(openArticle: () -> Unit = {}) {
Row(
Modifier.clickable(
// R.string.action_read_article = "read article"
@@ -418,6 +437,376 @@ fun FloatingBox() {
}
// [END android_compose_accessibility_traversal_fab]
+@OptIn(ExperimentalFoundationApi::class)
+@Composable
+fun InteractiveElements(
+ openArticle: () -> Unit = {},
+ addToBookmarks: () -> Unit = {},
+) {
+// [START android_compose_accessibility_interactive_clickable]
+ Row(
+ // Uses `mergeDescendants = true` under the hood
+ modifier = Modifier.clickable { openArticle() }
+ ) {
+ Icon(
+ painter = painterResource(R.drawable.ic_logo),
+ contentDescription = "Open",
+ )
+ Text("Accessibility in Compose")
+ }
+// [END android_compose_accessibility_interactive_clickable]
+
+// [START android_compose_accessibility_interactive_click_label]
+ Row(
+ modifier = Modifier
+ .clickable(onClickLabel = "Open this article") {
+ openArticle()
+ }
+ ) {
+ Icon(
+ painter = painterResource(R.drawable.ic_logo),
+ contentDescription = "Open"
+ )
+ Text("Accessibility in Compose")
+ }
+// [END android_compose_accessibility_interactive_click_label]
+
+// [START android_compose_accessibility_interactive_long_click]
+ Row(
+ modifier = Modifier
+ .combinedClickable(
+ onLongClickLabel = "Bookmark this article",
+ onLongClick = { addToBookmarks() },
+ onClickLabel = "Open this article",
+ onClick = { openArticle() },
+ )
+ ) {}
+// [END android_compose_accessibility_interactive_long_click]
+}
+
+// [START android_compose_accessibility_interactive_nested_click]
+@Composable
+private fun ArticleList(openArticle: () -> Unit) {
+ NestedArticleListItem(
+ // Clickable is set separately, in a nested layer:
+ onClickAction = openArticle,
+ // Semantics are set here:
+ modifier = Modifier.semantics {
+ onClick(
+ label = "Open this article",
+ action = {
+ // Not needed here: openArticle()
+ true
+ }
+ )
+ }
+ )
+}
+// [END android_compose_accessibility_interactive_nested_click]
+
+@Composable
+private fun NestedArticleListItem(
+ onClickAction: () -> Unit,
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+private fun Semantics(
+ removeArticle: () -> Unit,
+ openArticle: () -> Unit,
+ addToBookmarks: () -> Unit,
+) {
+
+// [START android_compose_accessibility_semantics_alert_polite]
+ PopupAlert(
+ message = "You have a new message",
+ modifier = Modifier.semantics {
+ liveRegion = LiveRegionMode.Polite
+ }
+ )
+// [END android_compose_accessibility_semantics_alert_polite]
+
+// [START android_compose_accessibility_semantics_alert_assertive]
+ PopupAlert(
+ message = "Emergency alert incoming",
+ modifier = Modifier.semantics {
+ liveRegion = LiveRegionMode.Assertive
+ }
+ )
+// [END android_compose_accessibility_semantics_alert_assertive]
+
+ Box() {
+// [START android_compose_accessibility_semantics_window]
+ ShareSheet(
+ message = "Choose how to share this photo",
+ modifier = Modifier
+ .fillMaxWidth()
+ .align(Alignment.TopCenter)
+ .semantics { paneTitle = "New bottom sheet" }
+ )
+// [END android_compose_accessibility_semantics_window]
+ }
+
+// [START android_compose_accessibility_semantics_error]
+ Error(
+ errorText = "Fields cannot be empty",
+ modifier = Modifier
+ .semantics {
+ error("Please add both email and password")
+ }
+ )
+// [END android_compose_accessibility_semantics_error]
+
+ val progress by remember { mutableFloatStateOf(0F) }
+// [START android_compose_accessibility_semantics_progress]
+ ProgressInfoBar(
+ modifier = Modifier
+ .semantics {
+ progressBarRangeInfo =
+ ProgressBarRangeInfo(
+ current = progress,
+ range = 0F..1F
+ )
+ }
+ )
+// [END android_compose_accessibility_semantics_progress]
+
+ val milkyWay = List(10) { it.toString() }
+// [START android_compose_accessibility_semantics_long_list]
+ MilkyWayList(
+ modifier = Modifier
+ .semantics {
+ collectionInfo = CollectionInfo(
+ rowCount = milkyWay.count(),
+ columnCount = 1
+ )
+ }
+ ) {
+ milkyWay.forEachIndexed { index, text ->
+ Text(
+ text = text,
+ modifier = Modifier.semantics {
+ collectionItemInfo =
+ CollectionItemInfo(index, 0, 0, 0)
+ }
+ )
+ }
+ }
+// [END android_compose_accessibility_semantics_long_list]
+
+// [START android_compose_accessibility_semantics_custom_action_swipe]
+ SwipeToDismissBox(
+ modifier = Modifier.semantics {
+ // Represents the swipe to dismiss for accessibility
+ customActions = listOf(
+ CustomAccessibilityAction(
+ label = "Remove article from list",
+ action = {
+ removeArticle()
+ true
+ }
+ )
+ )
+ },
+ state = rememberSwipeToDismissBoxState(),
+ backgroundContent = {}
+ ) {
+ ArticleListItem()
+ }
+// [END android_compose_accessibility_semantics_custom_action_swipe]
+
+// [START android_compose_accessibility_semantics_custom_action_long_list]
+ ArticleListItemRow(
+ modifier = Modifier
+ .semantics {
+ customActions = listOf(
+ CustomAccessibilityAction(
+ label = "Open article",
+ action = {
+ openArticle()
+ true
+ }
+ ),
+ CustomAccessibilityAction(
+ label = "Add to bookmarks",
+ action = {
+ addToBookmarks()
+ true
+ }
+ ),
+ )
+ }
+ ) {
+ Article(
+ modifier = Modifier.clearAndSetSemantics { },
+ onClick = openArticle,
+ )
+ BookmarkButton(
+ modifier = Modifier.clearAndSetSemantics { },
+ onClick = addToBookmarks,
+ )
+ }
+// [END android_compose_accessibility_semantics_custom_action_long_list]
+}
+
+@Composable
+private fun PopupAlert(
+ message: String,
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+fun ShareSheet(
+ message: String,
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+private fun Error(
+ errorText: String,
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+private fun ProgressInfoBar(
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+private fun MilkyWayList(
+ modifier: Modifier = Modifier,
+ content: @Composable () -> Unit,
+) {
+ content()
+}
+
+@Composable
+private fun ArticleListItemRow(
+ modifier: Modifier = Modifier,
+ content: @Composable () -> Unit,
+) {
+ content()
+}
+
+@Composable
+fun Article(
+ onClick: () -> Unit,
+ modifier: Modifier = Modifier,
+) {
+}
+
+@Composable
+fun BookmarkButton(
+ onClick: () -> Unit,
+ modifier: Modifier = Modifier,
+) {
+}
+
+// [START android_compose_accessibility_merging]
+@Composable
+private fun ArticleListItem(
+ openArticle: () -> Unit,
+ addToBookmarks: () -> Unit,
+) {
+
+ Row(modifier = Modifier.clickable { openArticle() }) {
+ // Merges with parent clickable:
+ Icon(
+ painter = painterResource(R.drawable.ic_logo),
+ contentDescription = "Article thumbnail"
+ )
+ ArticleDetails()
+
+ // Defies the merge due to its own clickable:
+ BookmarkButton(onClick = addToBookmarks)
+ }
+}
+// [END android_compose_accessibility_merging]
+
+@Composable
+fun ArticleDetails(
+ modifier: Modifier = Modifier,
+) {
+}
+
+// [START android_compose_accessibility_clearing]
+// Developer might intend this to be a toggleable.
+// Using `clearAndSetSemantics`, on the Row, a clickable modifier is applied,
+// a custom description is set, and a Role is applied.
+
+@Composable
+fun FavoriteToggle() {
+ val checked = remember { mutableStateOf(true) }
+ Row(
+ modifier = Modifier
+ .toggleable(
+ value = checked.value,
+ onValueChange = { checked.value = it }
+ )
+ .clearAndSetSemantics {
+ stateDescription = if (checked.value) "Favorited" else "Not favorited"
+ toggleableState = ToggleableState(checked.value)
+ role = Role.Switch
+ },
+ ) {
+ Icon(
+ imageVector = Icons.Default.Favorite,
+ contentDescription = null // not needed here
+
+ )
+ Text("Favorite?")
+ }
+}
+// [END android_compose_accessibility_clearing]
+
+// [START android_compose_accessibility_hiding]
+@Composable
+fun WatermarkExample(
+ watermarkText: String,
+ content: @Composable () -> Unit,
+) {
+ Box {
+ WatermarkedContent()
+ // Mark the watermark as hidden to accessibility services.
+ WatermarkText(
+ text = watermarkText,
+ color = Color.Gray.copy(alpha = 0.5f),
+ modifier = Modifier
+ .align(Alignment.BottomEnd)
+ .semantics { hideFromAccessibility() }
+ )
+ }
+}
+
+@Composable
+fun DecorativeExample() {
+ Text(
+ modifier =
+ Modifier.semantics {
+ hideFromAccessibility()
+ },
+ text = "A dot character that is used to decoratively separate information, like •"
+ )
+}
+// [END android_compose_accessibility_hiding]
+
+@Composable
+private fun WatermarkedContent() {
+}
+
+@Composable
+private fun WatermarkText(
+ text: String,
+ color: Color,
+ modifier: Modifier = Modifier,
+) {
+}
+
private object ColumnWithFab {
// [START android_compose_accessibility_traversal_fab_scaffold]
@OptIn(ExperimentalMaterial3Api::class)
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt
index b79f7ee71..a6be83b43 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt
@@ -17,7 +17,6 @@
package com.example.compose.snippets.adaptivelayouts
import android.os.Parcelable
-import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
@@ -30,33 +29,39 @@ import androidx.compose.material3.Card
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
+import androidx.compose.material3.adaptive.WindowAdaptiveInfo
+import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.material3.adaptive.layout.AnimatedPane
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
+import androidx.compose.material3.adaptive.layout.PaneScaffoldDirective
+import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
+import androidx.compose.material3.adaptive.navigation.NavigableListDetailPaneScaffold
+import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldPredictiveBackHandler
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
+import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_EXPANDED_LOWER_BOUND
+import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_MEDIUM_LOWER_BOUND
+import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun SampleListDetailPaneScaffoldParts() {
+fun SampleNavigableListDetailPaneScaffoldParts() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
- val navigator = rememberListDetailPaneScaffoldNavigator()
-
- BackHandler(navigator.canNavigateBack()) {
- navigator.navigateBack()
- }
+ val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
- ListDetailPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ NavigableListDetailPaneScaffold(
+ navigator = scaffoldNavigator,
// [START_EXCLUDE]
listPane = {},
detailPane = {},
@@ -65,16 +70,21 @@ fun SampleListDetailPaneScaffoldParts() {
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
- ListDetailPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ NavigableListDetailPaneScaffold(
+ navigator = scaffoldNavigator,
listPane = {
AnimatedPane {
MyList(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
- navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
- }
+ scope.launch {
+ scaffoldNavigator
+ .navigateTo(
+ ListDetailPaneScaffoldRole.Detail,
+ item
+ )
+ }
+ },
)
}
},
@@ -85,16 +95,14 @@ fun SampleListDetailPaneScaffoldParts() {
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05]
- ListDetailPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
- listPane =
+ NavigableListDetailPaneScaffold(
+ navigator = scaffoldNavigator,
// [START_EXCLUDE]
- {},
+ listPane = {},
// [END_EXCLUDE]
detailPane = {
AnimatedPane {
- navigator.currentDestination?.content?.let {
+ scaffoldNavigator.currentDestination?.contentKey?.let {
MyDetails(it)
}
}
@@ -106,23 +114,67 @@ fun SampleListDetailPaneScaffoldParts() {
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Preview
@Composable
-fun SampleListDetailPaneScaffoldFull() {
-// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
- val navigator = rememberListDetailPaneScaffoldNavigator()
+fun SampleNavigableListDetailPaneScaffoldFull() {
+ // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
+ val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
- BackHandler(navigator.canNavigateBack()) {
- navigator.navigateBack()
- }
+ NavigableListDetailPaneScaffold(
+ navigator = scaffoldNavigator,
+ listPane = {
+ AnimatedPane {
+ MyList(
+ onItemClick = { item ->
+ // Navigate to the detail pane with the passed item
+ scope.launch {
+ scaffoldNavigator.navigateTo(
+ ListDetailPaneScaffoldRole.Detail,
+ item
+ )
+ }
+ },
+ )
+ }
+ },
+ detailPane = {
+ AnimatedPane {
+ // Show the detail pane content if selected item is available
+ scaffoldNavigator.currentDestination?.contentKey?.let {
+ MyDetails(it)
+ }
+ }
+ },
+ )
+ // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
+}
+
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
+@Composable
+fun SampleListDetailPaneScaffoldWithPredictiveBackFull() {
+ // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
+ val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator()
+ val customScaffoldDirective = customPaneScaffoldDirective(currentWindowAdaptiveInfo())
+ val scope = rememberCoroutineScope()
+
+ ThreePaneScaffoldPredictiveBackHandler(
+ navigator = scaffoldNavigator,
+ backBehavior = BackNavigationBehavior.PopUntilContentChange
+ )
ListDetailPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ directive = customScaffoldDirective,
+ scaffoldState = scaffoldNavigator.scaffoldState,
listPane = {
AnimatedPane {
MyList(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
- navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
+ scope.launch {
+ scaffoldNavigator.navigateTo(
+ ListDetailPaneScaffoldRole.Detail,
+ item
+ )
+ }
},
)
}
@@ -130,15 +182,35 @@ fun SampleListDetailPaneScaffoldFull() {
detailPane = {
AnimatedPane {
// Show the detail pane content if selected item is available
- navigator.currentDestination?.content?.let {
+ scaffoldNavigator.currentDestination?.contentKey?.let {
MyDetails(it)
}
}
},
)
-// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
}
+fun customPaneScaffoldDirective(currentWindowAdaptiveInfo: WindowAdaptiveInfo): PaneScaffoldDirective {
+ val horizontalPartitions = when {
+ currentWindowAdaptiveInfo.windowSizeClass.isWidthAtLeastBreakpoint(
+ WIDTH_DP_EXPANDED_LOWER_BOUND
+ ) -> 3
+ currentWindowAdaptiveInfo.windowSizeClass.isWidthAtLeastBreakpoint(
+ WIDTH_DP_MEDIUM_LOWER_BOUND
+ ) -> 2
+ else -> 1
+ }
+
+ return PaneScaffoldDirective(
+ maxHorizontalPartitions = horizontalPartitions,
+ horizontalPartitionSpacerSize = 16.dp,
+ maxVerticalPartitions = 1,
+ verticalPartitionSpacerSize = 8.dp,
+ defaultPanePreferredWidth = 320.dp,
+ excludedBounds = emptyList()
+ )
+}
+// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
@Composable
fun MyList(
onItemClick: (MyItem) -> Unit,
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleNavigationSuiteScaffold.kt b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleNavigationSuiteScaffold.kt
index 76006e684..e8d06f783 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleNavigationSuiteScaffold.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleNavigationSuiteScaffold.kt
@@ -39,7 +39,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
-import androidx.window.core.layout.WindowWidthSizeClass
+import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_EXPANDED_LOWER_BOUND
import com.example.compose.snippets.R
// [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_destinations]
@@ -159,7 +159,7 @@ fun SampleNavigationSuiteScaffoldCustomType() {
// [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_layout_type]
val adaptiveInfo = currentWindowAdaptiveInfo()
val customNavSuiteType = with(adaptiveInfo) {
- if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED) {
+ if (windowSizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_EXPANDED_LOWER_BOUND)) {
NavigationSuiteType.NavigationDrawer
} else {
NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(adaptiveInfo)
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleSupportingPaneScaffold.kt b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleSupportingPaneScaffold.kt
index ad2e0d30f..7132c5e7f 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleSupportingPaneScaffold.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleSupportingPaneScaffold.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2024 The Android Open Source Project
+ * Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,11 +14,9 @@
* limitations under the License.
*/
-@file:OptIn(ExperimentalMaterial3AdaptiveApi::class)
-
package com.example.compose.snippets.adaptivelayouts
-import androidx.activity.compose.BackHandler
+import androidx.compose.foundation.background
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Button
@@ -28,52 +26,61 @@ import androidx.compose.material3.adaptive.layout.AnimatedPane
import androidx.compose.material3.adaptive.layout.PaneAdaptedValue
import androidx.compose.material3.adaptive.layout.SupportingPaneScaffold
import androidx.compose.material3.adaptive.layout.SupportingPaneScaffoldRole
+import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldPaneScope
import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldRole
-import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldScope
+import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
+import androidx.compose.material3.adaptive.navigation.NavigableSupportingPaneScaffold
+import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldPredictiveBackHandler
import androidx.compose.material3.adaptive.navigation.rememberSupportingPaneScaffoldNavigator
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.tooling.preview.Preview
+import kotlinx.coroutines.launch
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun SampleSupportingPaneScaffoldParts() {
+fun SampleNavigableSupportingPaneScaffoldParts() {
// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_nav_and_back]
- val navigator = rememberSupportingPaneScaffoldNavigator()
+ val scaffoldNavigator = rememberSupportingPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
- BackHandler(navigator.canNavigateBack()) {
- navigator.navigateBack()
- }
// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_nav_and_back]
// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_params]
- SupportingPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ NavigableSupportingPaneScaffold(
+ navigator = scaffoldNavigator,
mainPane = { /*...*/ },
supportingPane = { /*...*/ },
)
// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_params]
}
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun SampleSupportingPaneScaffoldFull() {
+@Preview
+fun SampleNavigableSupportingPaneScaffoldFull() {
// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_full]
- val navigator = rememberSupportingPaneScaffoldNavigator()
+ val scaffoldNavigator = rememberSupportingPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
- BackHandler(navigator.canNavigateBack()) {
- navigator.navigateBack()
- }
-
- SupportingPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ NavigableSupportingPaneScaffold(
+ navigator = scaffoldNavigator,
mainPane = {
- AnimatedPane(modifier = Modifier.safeContentPadding()) {
- // Main pane content
- if (navigator.scaffoldValue[SupportingPaneScaffoldRole.Supporting] == PaneAdaptedValue.Hidden) {
+ AnimatedPane(
+ modifier = Modifier
+ .safeContentPadding()
+ .background(Color.Red)
+ ) {
+ if (scaffoldNavigator.scaffoldValue[SupportingPaneScaffoldRole.Supporting] == PaneAdaptedValue.Hidden) {
Button(
- modifier = Modifier.wrapContentSize(),
+ modifier = Modifier
+ .wrapContentSize(),
onClick = {
- navigator.navigateTo(SupportingPaneScaffoldRole.Supporting)
+ scope.launch {
+ scaffoldNavigator.navigateTo(SupportingPaneScaffoldRole.Supporting)
+ }
}
) {
Text("Show supporting pane")
@@ -85,22 +92,24 @@ fun SampleSupportingPaneScaffoldFull() {
},
supportingPane = {
AnimatedPane(modifier = Modifier.safeContentPadding()) {
- // Supporting pane content
Text("Supporting pane")
}
- },
+ }
)
// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_full]
}
// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_extracted_panes]
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun ThreePaneScaffoldScope.MainPane(
+fun ThreePaneScaffoldPaneScope.MainPane(
shouldShowSupportingPaneButton: Boolean,
onNavigateToSupportingPane: () -> Unit,
modifier: Modifier = Modifier,
) {
- AnimatedPane(modifier = modifier.safeContentPadding()) {
+ AnimatedPane(
+ modifier = modifier.safeContentPadding()
+ ) {
// Main pane content
if (shouldShowSupportingPaneButton) {
Button(onClick = onNavigateToSupportingPane) {
@@ -112,8 +121,9 @@ fun ThreePaneScaffoldScope.MainPane(
}
}
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun ThreePaneScaffoldScope.SupportingPane(
+fun ThreePaneScaffoldPaneScope.SupportingPane(
modifier: Modifier = Modifier,
) {
AnimatedPane(modifier = modifier.safeContentPadding()) {
@@ -123,25 +133,56 @@ fun ThreePaneScaffoldScope.SupportingPane(
}
// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_extracted_panes]
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
-fun SampleSupportingPaneScaffoldSimplified() {
-// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified]
- val navigator = rememberSupportingPaneScaffoldNavigator()
+fun SampleNavigableSupportingPaneScaffoldSimplified() {
+ // [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified]
+ val scaffoldNavigator = rememberSupportingPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
- BackHandler(navigator.canNavigateBack()) {
- navigator.navigateBack()
- }
+ NavigableSupportingPaneScaffold(
+ navigator = scaffoldNavigator,
+ mainPane = {
+ MainPane(
+ shouldShowSupportingPaneButton = scaffoldNavigator.scaffoldValue.secondary == PaneAdaptedValue.Hidden,
+ onNavigateToSupportingPane = {
+ scope.launch {
+ scaffoldNavigator.navigateTo(ThreePaneScaffoldRole.Secondary)
+ }
+ }
+ )
+ },
+ supportingPane = { SupportingPane() },
+ )
+ // [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified]
+}
+
+@OptIn(ExperimentalMaterial3AdaptiveApi::class)
+@Composable
+fun SampleSupportingPaneScaffoldSimplifiedWithPredictiveBackHandler() {
+ // [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified_with_predictive_back_handler]
+ val scaffoldNavigator = rememberSupportingPaneScaffoldNavigator()
+ val scope = rememberCoroutineScope()
+
+ ThreePaneScaffoldPredictiveBackHandler(
+ navigator = scaffoldNavigator,
+ backBehavior = BackNavigationBehavior.PopUntilScaffoldValueChange
+ )
SupportingPaneScaffold(
- directive = navigator.scaffoldDirective,
- value = navigator.scaffoldValue,
+ directive = scaffoldNavigator.scaffoldDirective,
+ scaffoldState = scaffoldNavigator.scaffoldState,
mainPane = {
MainPane(
- shouldShowSupportingPaneButton = navigator.scaffoldValue.secondary == PaneAdaptedValue.Hidden,
- onNavigateToSupportingPane = { navigator.navigateTo(ThreePaneScaffoldRole.Secondary) }
+ shouldShowSupportingPaneButton = scaffoldNavigator.scaffoldValue.secondary == PaneAdaptedValue.Hidden,
+ onNavigateToSupportingPane = {
+ scope.launch {
+ scaffoldNavigator.navigateTo(ThreePaneScaffoldRole.Secondary)
+ }
+ }
)
},
supportingPane = { SupportingPane() },
)
-// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified]
+ // [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified_with_predictive_back_handler]
}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt
index 38a1eb44d..bdb82790d 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt
@@ -26,9 +26,11 @@ import androidx.compose.animation.EnterExitState
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.animateColor
import androidx.compose.animation.animateContentSize
+import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.Easing
+import androidx.compose.animation.core.ExperimentalAnimationSpecApi
import androidx.compose.animation.core.ExperimentalTransitionApi
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.FastOutSlowInEasing
@@ -43,11 +45,13 @@ import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.animateOffsetAsState
import androidx.compose.animation.core.animateRect
import androidx.compose.animation.core.animateValueAsState
import androidx.compose.animation.core.createChildTransition
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.keyframes
+import androidx.compose.animation.core.keyframesWithSpline
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.core.repeatable
@@ -71,11 +75,13 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.sizeIn
@@ -93,25 +99,34 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
+import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.drawBehind
+import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.PathEffect
+import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.layout.boundsInParent
+import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.round
import com.example.compose.snippets.R
import java.text.BreakIterator
import java.text.StringCharacterIterator
import kotlinx.coroutines.delay
+import kotlinx.coroutines.isActive
/*
* Copyright 2023 The Android Open Source Project
@@ -265,14 +280,15 @@ private fun AnimateAsStateSimple() {
// [START android_compose_animations_animate_as_state]
var enabled by remember { mutableStateOf(true) }
- val alpha: Float by animateFloatAsState(if (enabled) 1f else 0.5f, label = "alpha")
+ val animatedAlpha: Float by animateFloatAsState(if (enabled) 1f else 0.5f, label = "alpha")
Box(
Modifier
.fillMaxSize()
- .graphicsLayer(alpha = alpha)
+ .graphicsLayer { alpha = animatedAlpha }
.background(Color.Red)
)
// [END android_compose_animations_animate_as_state]
+ { Button(onClick = { enabled = !enabled }) { Text("Animate me!") } }
}
@Preview
@@ -709,6 +725,101 @@ private fun AnimationSpecKeyframe() {
// [END android_compose_animations_spec_keyframe]
}
+@OptIn(ExperimentalAnimationSpecApi::class)
+@Composable
+private fun AnimationSpecKeyframeWithSpline() {
+ // [START android_compose_animation_spec_keyframes_with_spline]
+ val offset by animateOffsetAsState(
+ targetValue = Offset(300f, 300f),
+ animationSpec = keyframesWithSpline {
+ durationMillis = 6000
+ Offset(0f, 0f) at 0
+ Offset(150f, 200f) atFraction 0.5f
+ Offset(0f, 100f) atFraction 0.7f
+ }
+ )
+ // [END android_compose_animation_spec_keyframes_with_spline]
+}
+
+@OptIn(ExperimentalAnimationSpecApi::class)
+@Preview
+@Composable
+private fun OffsetKeyframeWithSplineDemo() {
+ val points = remember { mutableStateListOf() }
+ val offsetAnim = remember {
+ Animatable(
+ Offset.Zero,
+ Offset.VectorConverter
+ )
+ }
+ val density = LocalDensity.current
+
+ BoxWithConstraints(
+ Modifier.fillMaxSize().drawBehind {
+ drawPoints(
+ points = points,
+ pointMode = PointMode.Lines,
+ color = Color.LightGray,
+ strokeWidth = 4f,
+ pathEffect = PathEffect.dashPathEffect(floatArrayOf(30f, 20f))
+ )
+ }
+ ) {
+ val minDimension = minOf(maxWidth, maxHeight)
+ val size = minDimension / 4
+
+ val sizePx = with(density) { size.toPx() }
+ val widthPx = with(density) { maxWidth.toPx() }
+ val heightPx = with(density) { maxHeight.toPx() }
+
+ val maxXOff = (widthPx - sizePx) / 2f
+ val maxYOff = heightPx - (sizePx / 2f)
+
+ Box(
+ Modifier.align(Alignment.TopCenter)
+ .offset { offsetAnim.value.round() }
+ .size(size)
+ .background(Color.Red, RoundedCornerShape(50))
+ .onPlaced { points.add(it.boundsInParent().center) }
+ )
+
+ LaunchedEffect(Unit) {
+ delay(1000)
+ while (isActive) {
+ offsetAnim.animateTo(
+ targetValue = Offset.Zero,
+ animationSpec =
+ keyframesWithSpline {
+ durationMillis = 4400
+
+ // Increasingly approach the halfway point moving from side to side
+ for (i in 0..4) {
+ val sign = if (i % 2 == 0) 1 else -1
+ Offset(
+ x = maxXOff * (i.toFloat() / 5f) * sign,
+ y = (maxYOff) * (i.toFloat() / 5f)
+ ) atFraction (0.1f * i)
+ }
+
+ // Halfway point (at bottom of the screen)
+ Offset(0f, maxYOff) atFraction 0.5f
+
+ // Return with mirrored movement
+ for (i in 0..4) {
+ val sign = if (i % 2 == 0) 1 else -1
+ Offset(
+ x = maxXOff * (1f - i.toFloat() / 5f) * sign,
+ y = (maxYOff) * (1f - i.toFloat() / 5f)
+ ) atFraction ((0.1f * i) + 0.5f)
+ }
+ }
+ )
+ points.clear()
+ }
+ }
+ }
+}
+
@Composable
private fun AnimationSpecRepeatable() {
// [START android_compose_animations_spec_repeatable]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/Cheese.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/Cheese.kt
new file mode 100644
index 000000000..2468bce9f
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/Cheese.kt
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo
+
+import androidx.annotation.DrawableRes
+import com.example.compose.snippets.R
+
+val CheeseImages = listOf(
+ R.drawable.cheese_1,
+ R.drawable.cheese_2,
+ R.drawable.cheese_3,
+ R.drawable.cheese_4,
+ R.drawable.cheese_5
+)
+
+val CheeseNames = listOf(
+ "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
+ "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
+ "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
+ "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
+ "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
+ "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss",
+ "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
+ "Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase",
+ "Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
+ "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy",
+ "Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille",
+ "Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
+ "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)",
+ "Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves",
+ "Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur",
+ "Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon",
+ "Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin",
+ "Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)",
+ "Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine",
+ "Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza",
+ "Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)",
+ "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta",
+ "Caerphilly", "Cairnsmore", "Calenzana", "Cambazola", "Camembert de Normandie",
+ "Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux", "Capricorn Goat",
+ "Capriole Banon", "Carre de l'Est", "Casciotta di Urbino", "Cashel Blue", "Castellano",
+ "Castelleno", "Castelmagno", "Castelo Branco", "Castigliano", "Cathelain",
+ "Celtic Promise", "Cendre d'Olivet", "Cerney", "Chabichou", "Chabichou du Poitou",
+ "Chabis de Gatine", "Chaource", "Charolais", "Chaumes", "Cheddar",
+ "Cheddar Clothbound", "Cheshire", "Chevres", "Chevrotin des Aravis", "Chontaleno",
+ "Civray", "Coeur de Camembert au Calvados", "Coeur de Chevre", "Colby", "Cold Pack",
+ "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper",
+ "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)",
+ "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese",
+ "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza",
+ "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley",
+ "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino",
+ "Cypress Grove Chevre", "Danablu (Danish Blue)", "Danbo", "Danish Fontina",
+ "Daralagjazsky", "Dauphin", "Delice des Fiouves", "Denhany Dorset Drum", "Derby",
+ "Dessertnyj Belyj", "Devon Blue", "Devon Garland", "Dolcelatte", "Doolin",
+ "Doppelrhamstufel", "Dorset Blue Vinney", "Double Gloucester", "Double Worcester",
+ "Dreux a la Feuille", "Dry Jack", "Duddleswell", "Dunbarra", "Dunlop", "Dunsyre Blue",
+ "Duroblando", "Durrus", "Dutch Mimolette (Commissiekaas)", "Edam", "Edelpilz",
+ "Emental Grand Cru", "Emlett", "Emmental", "Epoisses de Bourgogne", "Esbareich",
+ "Esrom", "Etorki", "Evansdale Farmhouse Brie", "Evora De L'Alentejo", "Exmoor Blue",
+ "Explorateur", "Feta", "Feta (Australian)", "Figue", "Filetta", "Fin-de-Siecle",
+ "Finlandia Swiss", "Finn", "Fiore Sardo", "Fleur du Maquis", "Flor de Guia",
+ "Flower Marie", "Folded", "Folded cheese with mint", "Fondant de Brebis",
+ "Fontainebleau", "Fontal", "Fontina Val d'Aosta", "Formaggio di capra", "Fougerus",
+ "Four Herb Gouda", "Fourme d' Ambert", "Fourme de Haute Loire", "Fourme de Montbrison",
+ "Fresh Jack", "Fresh Mozzarella", "Fresh Ricotta", "Fresh Truffles", "Fribourgeois",
+ "Friesekaas", "Friesian", "Friesla", "Frinault", "Fromage a Raclette", "Fromage Corse",
+ "Fromage de Montagne de Savoie", "Fromage Frais", "Fruit Cream Cheese",
+ "Frying Cheese", "Fynbo", "Gabriel", "Galette du Paludier", "Galette Lyonnaise",
+ "Galloway Goat's Milk Gems", "Gammelost", "Gaperon a l'Ail", "Garrotxa", "Gastanberra",
+ "Geitost", "Gippsland Blue", "Gjetost", "Gloucester", "Golden Cross", "Gorgonzola",
+ "Gornyaltajski", "Gospel Green", "Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost",
+ "Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel",
+ "Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh", "Greve",
+ "Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny", "Halloumi",
+ "Halloumy (Australian)", "Haloumi-Style Cheese", "Harbourne Blue", "Havarti",
+ "Heidi Gruyere", "Hereford Hop", "Herrgardsost", "Herriot Farmhouse", "Herve",
+ "Hipi Iti", "Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster",
+ "Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu", "Isle of Mull", "Jarlsberg",
+ "Jermi Tortes", "Jibneh Arabieh", "Jindi Brie", "Jubilee Blue", "Juustoleipa",
+ "Kadchgall", "Kaseri", "Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine",
+ "Kikorangi", "King Island Cape Wickham Brie", "King River Gold", "Klosterkaese",
+ "Knockalara", "Kugelkase", "L'Aveyronnais", "L'Ecir de l'Aubrac", "La Taupiniere",
+ "La Vache Qui Rit", "Laguiole", "Lairobell", "Lajta", "Lanark Blue", "Lancashire",
+ "Langres", "Lappi", "Laruns", "Lavistown", "Le Brin", "Le Fium Orbo", "Le Lacandou",
+ "Le Roule", "Leafield", "Lebbene", "Leerdammer", "Leicester", "Leyden", "Limburger",
+ "Lincolnshire Poacher", "Lingot Saint Bousquet d'Orb", "Liptauer", "Little Rydings",
+ "Livarot", "Llanboidy", "Llanglofan Farmhouse", "Loch Arthur Farmhouse",
+ "Loddiswell Avondale", "Longhorn", "Lou Palou", "Lou Pevre", "Lyonnais", "Maasdam",
+ "Macconais", "Mahoe Aged Gouda", "Mahon", "Malvern", "Mamirolle", "Manchego",
+ "Manouri", "Manur", "Marble Cheddar", "Marbled Cheeses", "Maredsous", "Margotin",
+ "Maribo", "Maroilles", "Mascares", "Mascarpone", "Mascarpone (Australian)",
+ "Mascarpone Torta", "Matocq", "Maytag Blue", "Meira", "Menallack Farmhouse",
+ "Menonita", "Meredith Blue", "Mesost", "Metton (Cancoillotte)", "Meyer Vintage Gouda",
+ "Mihalic Peynir", "Milleens", "Mimolette", "Mine-Gabhar", "Mini Baby Bells", "Mixte",
+ "Molbo", "Monastery Cheeses", "Mondseer", "Mont D'or Lyonnais", "Montasio",
+ "Monterey Jack", "Monterey Jack Dry", "Morbier", "Morbier Cru de Montagne",
+ "Mothais a la Feuille", "Mozzarella", "Mozzarella (Australian)",
+ "Mozzarella di Bufala", "Mozzarella Fresh, in water", "Mozzarella Rolls", "Munster",
+ "Murol", "Mycella", "Myzithra", "Naboulsi", "Nantais", "Neufchatel",
+ "Neufchatel (Australian)", "Niolo", "Nokkelost", "Northumberland", "Oaxaca",
+ "Olde York", "Olivet au Foin", "Olivet Bleu", "Olivet Cendre",
+ "Orkney Extra Mature Cheddar", "Orla", "Oschtjepka", "Ossau Fermier", "Ossau-Iraty",
+ "Oszczypek", "Oxford Blue", "P'tit Berrichon", "Palet de Babligny", "Paneer", "Panela",
+ "Pannerone", "Pant ys Gawn", "Parmesan (Parmigiano)", "Parmigiano Reggiano",
+ "Pas de l'Escalette", "Passendale", "Pasteurized Processed", "Pate de Fromage",
+ "Patefine Fort", "Pave d'Affinois", "Pave d'Auge", "Pave de Chirac", "Pave du Berry",
+ "Pecorino", "Pecorino in Walnut Leaves", "Pecorino Romano", "Peekskill Pyramid",
+ "Pelardon des Cevennes", "Pelardon des Corbieres", "Penamellera", "Penbryn",
+ "Pencarreg", "Perail de Brebis", "Petit Morin", "Petit Pardou", "Petit-Suisse",
+ "Picodon de Chevre", "Picos de Europa", "Piora", "Pithtviers au Foin",
+ "Plateau de Herve", "Plymouth Cheese", "Podhalanski", "Poivre d'Ane", "Polkolbin",
+ "Pont l'Eveque", "Port Nicholson", "Port-Salut", "Postel", "Pouligny-Saint-Pierre",
+ "Pourly", "Prastost", "Pressato", "Prince-Jean", "Processed Cheddar", "Provolone",
+ "Provolone (Australian)", "Pyengana Cheddar", "Pyramide", "Quark",
+ "Quark (Australian)", "Quartirolo Lombardo", "Quatre-Vents", "Quercy Petit",
+ "Queso Blanco", "Queso Blanco con Frutas --Pina y Mango", "Queso de Murcia",
+ "Queso del Montsec", "Queso del Tietar", "Queso Fresco", "Queso Fresco (Adobera)",
+ "Queso Iberico", "Queso Jalapeno", "Queso Majorero", "Queso Media Luna",
+ "Queso Para Frier", "Queso Quesadilla", "Rabacal", "Raclette", "Ragusano", "Raschera",
+ "Reblochon", "Red Leicester", "Regal de la Dombes", "Reggianito", "Remedou",
+ "Requeson", "Richelieu", "Ricotta", "Ricotta (Australian)", "Ricotta Salata", "Ridder",
+ "Rigotte", "Rocamadour", "Rollot", "Romano", "Romans Part Dieu", "Roncal", "Roquefort",
+ "Roule", "Rouleau De Beaulieu", "Royalp Tilsit", "Rubens", "Rustinu", "Saaland Pfarr",
+ "Saanenkaese", "Saga", "Sage Derby", "Sainte Maure", "Saint-Marcellin",
+ "Saint-Nectaire", "Saint-Paulin", "Salers", "Samso", "San Simon", "Sancerre",
+ "Sap Sago", "Sardo", "Sardo Egyptian", "Sbrinz", "Scamorza", "Schabzieger", "Schloss",
+ "Selles sur Cher", "Selva", "Serat", "Seriously Strong Cheddar", "Serra da Estrela",
+ "Sharpam", "Shelburne Cheddar", "Shropshire Blue", "Siraz", "Sirene", "Smoked Gouda",
+ "Somerset Brie", "Sonoma Jack", "Sottocenare al Tartufo", "Soumaintrain",
+ "Sourire Lozerien", "Spenwood", "Sraffordshire Organic", "St. Agur Blue Cheese",
+ "Stilton", "Stinking Bishop", "String", "Sussex Slipcote", "Sveciaost", "Swaledale",
+ "Sweet Style Swiss", "Swiss", "Syrian (Armenian String)", "Tala", "Taleggio", "Tamie",
+ "Tasmania Highland Chevre Log", "Taupiniere", "Teifi", "Telemea", "Testouri",
+ "Tete de Moine", "Tetilla", "Texas Goat Cheese", "Tibet", "Tillamook Cheddar",
+ "Tilsit", "Timboon Brie", "Toma", "Tomme Brulee", "Tomme d'Abondance",
+ "Tomme de Chevre", "Tomme de Romans", "Tomme de Savoie", "Tomme des Chouans", "Tommes",
+ "Torta del Casar", "Toscanello", "Touree de L'Aubier", "Tourmalet",
+ "Trappe (Veritable)", "Trois Cornes De Vendee", "Tronchon", "Trou du Cru", "Truffe",
+ "Tupi", "Turunmaa", "Tymsboro", "Tyn Grug", "Tyning", "Ubriaco", "Ulloa",
+ "Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco", "Vendomois",
+ "Vieux Corse", "Vignotte", "Vulscombe", "Waimata Farmhouse Blue",
+ "Washed Rind Cheese (Australian)", "Waterloo", "Weichkaese", "Wellington",
+ "Wensleydale", "White Stilton", "Whitestone Farmhouse", "Wigmore", "Woodside Cabecou",
+ "Xanadu", "Xynotyro", "Yarg Cornish", "Yarra Valley Pyramid", "Yorkshire Blue",
+ "Zamorano", "Zanetti Grana Padano", "Zanetti Parmigiano Reggiano"
+)
+
+data class Cheese(
+ val id: Long,
+ val name: String,
+ @DrawableRes val image: Int
+) {
+ companion object {
+ fun all(): List {
+ return CheeseNames.mapIndexed { i, name ->
+ Cheese(
+ id = (i + 1).toLong(),
+ name = name,
+ image = CheeseImages[
+ ((name.hashCode() % CheeseImages.size) + CheeseImages.size) %
+ CheeseImages.size
+ ]
+ )
+ }
+ }
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/SimpleScaffold.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/SimpleScaffold.kt
new file mode 100644
index 000000000..987d545a3
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/SimpleScaffold.kt
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo
+
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.BoxScope
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.statusBarsPadding
+import androidx.compose.material3.ExperimentalMaterial3Api
+import androidx.compose.material3.Scaffold
+import androidx.compose.material3.Text
+import androidx.compose.material3.TopAppBar
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+
+@OptIn(ExperimentalMaterial3Api::class)
+@Composable
+fun SimpleScaffold(
+ title: String,
+ modifier: Modifier = Modifier,
+ content: @Composable BoxScope.() -> Unit
+) {
+ Scaffold(
+ topBar = {
+ TopAppBar(
+ title = {
+ Text(text = title)
+ },
+ modifier = Modifier
+ .statusBarsPadding()
+ )
+ },
+ modifier = modifier
+ ) { padding ->
+ Box(
+ modifier = Modifier
+ .fillMaxSize()
+ .padding(padding)
+ ) {
+ content()
+ }
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/custom/CustomCanvasAnimation.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/custom/CustomCanvasAnimation.kt
new file mode 100644
index 000000000..e82794346
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/custom/CustomCanvasAnimation.kt
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo.custom
+
+import androidx.compose.animation.core.Animatable
+import androidx.compose.animation.core.Spring
+import androidx.compose.animation.core.SpringSpec
+import androidx.compose.animation.core.spring
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.size
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.layout.onSizeChanged
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.IntSize
+import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.delay
+
+/**
+ * A custom loading animation example using Canvas and draw APIs, combined with
+ * Animatable to show the use of the animateTo() function used sequentially.
+ */
+@Composable
+@Preview
+fun CustomCanvasBouncyLoader() {
+ val yOffset = remember {
+ Animatable(0f)
+ }
+ val scale = remember {
+ Animatable(1f)
+ }
+ LaunchedEffect("bouncyLoader") {
+ delay(400)
+ // We use the Animatable.animateTo() API here to demonstrate the coroutine usage - each
+ // item is animating one after the other, as the animateTo function is sequential.
+ // Animate y to half the height
+ yOffset.animateTo(0.5f, bouncyAnimationSpec)
+ scale.animateTo(3f, bouncyAnimationSpec)
+ delay(500)
+ scale.animateTo(10f, bouncyAnimationSpec)
+ delay(500)
+ scale.animateTo(50f, bouncyAnimationSpec)
+ }
+ val size = remember {
+ mutableStateOf(IntSize.Zero)
+ }
+ Box(
+ Modifier
+ .fillMaxSize()
+ .onSizeChanged {
+ // We get the size change of the whole composable, and use this to determine how
+ // big the ball should be.
+ size.value = it
+ }
+ ) {
+ GradientCircle(
+ Modifier
+ .align(Alignment.TopCenter)
+ .size(26.dp)
+ .graphicsLayer {
+ // We use .graphicsLayer here to perform the animation as we are animating
+ // multiple properties of our Gradient circle at once, and this is more
+ // efficient than using multiple modifiers.
+ // .graphicsLayer also defers these changes to the Draw phase of Compose,
+ // therefore minimizing recompositions required to do this.
+ scaleX = scale.value
+ scaleY = scale.value
+ translationY = yOffset.value * size.value.height
+ }
+ )
+ }
+}
+
+@Composable
+private fun GradientCircle(modifier: Modifier = Modifier) {
+ val brush = remember {
+ Brush.verticalGradient(listOf(Color(0xFFF56E34), Color(0xFF234EDA)))
+ }
+ Canvas(modifier = modifier) {
+ drawCircle(brush = brush)
+ }
+}
+
+private val bouncyAnimationSpec: SpringSpec = spring(
+ dampingRatio = Spring.DampingRatioMediumBouncy,
+ stiffness = Spring.StiffnessLow
+)
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fade/FadeDemo.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fade/FadeDemo.kt
new file mode 100644
index 000000000..c46735f25
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fade/FadeDemo.kt
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo.fade
+
+import androidx.compose.animation.AnimatedContent
+import androidx.compose.animation.AnimatedContentTransitionScope
+import androidx.compose.animation.ContentTransform
+import androidx.compose.animation.core.LinearOutSlowInEasing
+import androidx.compose.animation.core.snap
+import androidx.compose.animation.core.tween
+import androidx.compose.animation.fadeIn
+import androidx.compose.animation.fadeOut
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.Button
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import com.example.compose.snippets.animations.demo.CheeseImages
+import com.example.compose.snippets.animations.demo.SimpleScaffold
+
+/**
+ * A fade creates a smooth sequence between elements that fully overlap each other, such as
+ * photos inside of a card or another container. When a new element enters, it fades in
+ * over the current element.
+ */
+@Preview
+@Composable
+fun FadeDemo() {
+ SimpleScaffold(title = "Fade") {
+
+ val painters = CheeseImages.map { painterResource(it) }
+ var index by remember { mutableIntStateOf(0) }
+
+ AnimatedContent(
+ targetState = index,
+ modifier = Modifier.align(Alignment.Center),
+ transitionSpec = fade()
+ ) { targetIndex ->
+ Image(
+ painter = painters[targetIndex],
+ contentDescription = "Cheese",
+ modifier = Modifier
+ .size(256.dp, 192.dp)
+ .clip(shape = RoundedCornerShape(16.dp)),
+ contentScale = ContentScale.Crop
+ )
+ }
+
+ Button(
+ onClick = { index = (index + 1) % painters.size },
+ modifier = Modifier
+ .align(Alignment.BottomCenter)
+ .padding(64.dp)
+ ) {
+ Text(text = "NEXT")
+ }
+ }
+}
+
+/**
+ * Creates a transitionSpec for configuring [AnimatedContent] to the fade pattern.
+ */
+private fun fade(
+ durationMillis: Int = 300
+): AnimatedContentTransitionScope.() -> ContentTransform {
+ return {
+ ContentTransform(
+ // The initial content should stay until the target content is completely opaque.
+ initialContentExit = fadeOut(animationSpec = snap(delayMillis = durationMillis)),
+ // The target content fades in. This is shown on top of the initial content.
+ targetContentEnter = fadeIn(
+ animationSpec = tween(
+ durationMillis = durationMillis,
+ // LinearOutSlowInEasing is suitable for incoming elements.
+ easing = LinearOutSlowInEasing
+ )
+ )
+ )
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fadethrough/FadeThroughDemo.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fadethrough/FadeThroughDemo.kt
new file mode 100644
index 000000000..efdd35745
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/fadethrough/FadeThroughDemo.kt
@@ -0,0 +1,256 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo.fadethrough
+
+import androidx.compose.animation.AnimatedContent
+import androidx.compose.animation.AnimatedContentTransitionScope
+import androidx.compose.animation.ContentTransform
+import androidx.compose.animation.SizeTransform
+import androidx.compose.animation.core.FastOutLinearInEasing
+import androidx.compose.animation.core.LinearOutSlowInEasing
+import androidx.compose.animation.core.tween
+import androidx.compose.animation.fadeIn
+import androidx.compose.animation.fadeOut
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.IntrinsicSize
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.Favorite
+import androidx.compose.material.icons.filled.Share
+import androidx.compose.material3.Button
+import androidx.compose.material3.Icon
+import androidx.compose.material3.IconButton
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Surface
+import androidx.compose.material3.Text
+import androidx.compose.material3.TextButton
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import com.example.compose.snippets.R
+import com.example.compose.snippets.animations.demo.SimpleScaffold
+
+/**
+ * Fade through involves one element fading out completely before a new one fades in. These
+ * transitions can be applied to text, icons, and other elements that don't perfectly
+ * overlap. This technique lets the background show through during a transition, and it can
+ * provide continuity between screens when paired with a shared transformation.
+ */
+@Preview
+@Composable
+fun FadeThroughDemo() {
+ SimpleScaffold(title = "Fade through") {
+ var expanded by rememberSaveable { mutableStateOf(false) }
+ DemoCard(
+ expanded = expanded,
+ modifier = Modifier.align(Alignment.Center)
+ )
+
+ Button(
+ onClick = { expanded = !expanded },
+ modifier = Modifier
+ .align(Alignment.BottomCenter)
+ .padding(64.dp)
+ ) {
+ Text(text = "TOGGLE")
+ }
+ }
+}
+
+/**
+ * Shows the card. The card can be either expanded or collapsed, and the transition between the two
+ * states is animated with the fade-through effect.
+ *
+ * @param expanded Whether the card is expanded or collapsed.
+ */
+@Composable
+private fun DemoCard(
+ expanded: Boolean,
+ modifier: Modifier = Modifier
+) {
+ Surface(
+ modifier = modifier,
+ tonalElevation = 2.dp,
+ shape = RoundedCornerShape(16.dp)
+ ) {
+ // Use `AnimatedContent` to switch between different content.
+ AnimatedContent(
+ // `targetState` specifies the input state.
+ targetState = expanded,
+ // `transitionSpec` defines the behavior of the transition animation.
+ transitionSpec = fadeThrough()
+ ) { targetExpanded ->
+ if (targetExpanded) {
+ ExpandedContent()
+ } else {
+ CollapsedContent()
+ }
+ }
+ }
+}
+
+/**
+ * Creates a transitionSpec for configuring [AnimatedContent] to the fade through pattern.
+ * See [Fade through](https://material.io/design/motion/the-motion-system.html#fade-through) for
+ * the motion spec.
+ */
+fun fadeThrough(
+ durationMillis: Int = 300
+): AnimatedContentTransitionScope.() -> ContentTransform {
+ return {
+ ContentTransform(
+ // The initial content fades out.
+ initialContentExit = fadeOut(
+ animationSpec = tween(
+ // The duration is 3/8 of the overall duration.
+ durationMillis = durationMillis * 3 / 8,
+ // FastOutLinearInEasing is suitable for elements exiting the screen.
+ easing = FastOutLinearInEasing
+ )
+ ),
+ // The target content fades in after the current content fades out.
+ targetContentEnter = fadeIn(
+ animationSpec = tween(
+ // The duration is 5/8 of the overall duration.
+ durationMillis = durationMillis * 5 / 8,
+ // Delays the EnterTransition by the duration of the ExitTransition.
+ delayMillis = durationMillis * 3 / 8,
+ // LinearOutSlowInEasing is suitable for incoming elements.
+ easing = LinearOutSlowInEasing
+ )
+ ),
+ // The size changes along with the content transition.
+ sizeTransform = SizeTransform(
+ sizeAnimationSpec = { _, _ ->
+ tween(durationMillis = durationMillis)
+ }
+ )
+ )
+ }
+}
+
+@Composable
+private fun CollapsedContent() {
+ Row(
+ verticalAlignment = Alignment.CenterVertically,
+ modifier = Modifier.padding(16.dp),
+ horizontalArrangement = Arrangement.spacedBy(16.dp)
+ ) {
+ Image(
+ painter = painterResource(R.drawable.cheese_1),
+ contentDescription = null,
+ modifier = Modifier
+ .size(64.dp)
+ .clip(CircleShape)
+ )
+ Text(
+ text = "Cheese",
+ style = MaterialTheme.typography.headlineSmall
+ )
+ }
+}
+
+@Composable
+private fun ExpandedContent() {
+ Column(
+ modifier = Modifier.width(IntrinsicSize.Min)
+ ) {
+ Image(
+ painter = painterResource(R.drawable.cheese_1),
+ contentDescription = null,
+ modifier = Modifier.size(320.dp, 128.dp),
+ contentScale = ContentScale.Crop
+ )
+ Spacer(modifier = Modifier.height(16.dp))
+ Text(
+ text = "Cheese",
+ style = MaterialTheme.typography.headlineSmall,
+ modifier = Modifier.padding(horizontal = 16.dp)
+ )
+ Text(
+ text = "Hello, world",
+ style = MaterialTheme.typography.titleMedium,
+ modifier = Modifier.padding(horizontal = 16.dp)
+ )
+ Spacer(modifier = Modifier.height(16.dp))
+ Text(
+ text = stringResource(R.string.lorem_ipsum),
+ style = MaterialTheme.typography.bodyMedium,
+ modifier = Modifier.padding(horizontal = 16.dp),
+ maxLines = 2,
+ overflow = TextOverflow.Ellipsis
+ )
+ Spacer(modifier = Modifier.height(16.dp))
+ Row(
+ modifier = Modifier.padding(horizontal = 16.dp),
+ horizontalArrangement = Arrangement.spacedBy(4.dp)
+ ) {
+ TextButton(onClick = { /* Do nothing */ }) {
+ Text(text = "DETAIL")
+ }
+ TextButton(onClick = { /* Do nothing */ }) {
+ Text(text = "ORDER")
+ }
+ Spacer(modifier = Modifier.weight(1f))
+ IconButton(onClick = { /* Do nothing */ }) {
+ Icon(
+ imageVector = Icons.Default.Favorite,
+ contentDescription = "Favorite"
+ )
+ }
+ IconButton(onClick = { /* Do nothing */ }) {
+ Icon(
+ imageVector = Icons.Default.Share,
+ contentDescription = "Share"
+ )
+ }
+ }
+ Spacer(modifier = Modifier.height(8.dp))
+ }
+}
+
+@Preview
+@Composable
+private fun PreviewDemoCardCollapsed() {
+ DemoCard(expanded = false)
+}
+
+@Preview
+@Composable
+private fun PreviewDemoCardExpanded() {
+ DemoCard(expanded = true)
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedaxis/SharedAxisDemo.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedaxis/SharedAxisDemo.kt
new file mode 100644
index 000000000..ceb7b4a61
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedaxis/SharedAxisDemo.kt
@@ -0,0 +1,282 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo.sharedaxis
+
+import androidx.annotation.DrawableRes
+import androidx.compose.animation.AnimatedContent
+import androidx.compose.animation.AnimatedVisibilityScope
+import androidx.compose.animation.ContentTransform
+import androidx.compose.animation.animateColor
+import androidx.compose.animation.core.FastOutLinearInEasing
+import androidx.compose.animation.core.FiniteAnimationSpec
+import androidx.compose.animation.core.LinearOutSlowInEasing
+import androidx.compose.animation.core.tween
+import androidx.compose.animation.core.updateTransition
+import androidx.compose.animation.fadeIn
+import androidx.compose.animation.fadeOut
+import androidx.compose.animation.slideInVertically
+import androidx.compose.animation.slideOutVertically
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.aspectRatio
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.navigationBarsPadding
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.wrapContentSize
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.foundation.verticalScroll
+import androidx.compose.material3.IconButton
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.text.style.TextAlign
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import com.example.compose.snippets.animations.demo.CheeseImages
+import com.example.compose.snippets.animations.demo.CheeseNames
+import com.example.compose.snippets.animations.demo.SimpleScaffold
+
+/**
+ * The shared axis pattern is used for transitions between UI elements that have a spatial
+ * or navigational relationship. This demo uses a shared transformation on the Y-axis to
+ * reinforce the sequential order of elements.
+ */
+@Composable
+fun SharedAxisDemo() {
+ SimpleScaffold(title = "Layout > Shared axis (Y-axis)") {
+ val pages = remember { createPages() }
+ // Indicator column
+ var id by rememberSaveable { mutableIntStateOf(1) }
+ Row(modifier = Modifier.padding(end = 16.dp)) {
+ PageIndicatorsColumn(
+ pages = pages,
+ selectedId = id,
+ onIndicatorClick = { id = it }
+ )
+
+ // SharedYAxis animates the content change.
+ SharedYAxis(targetState = pages.first { it.id == id }) { page ->
+ PageContent(page = page)
+ }
+ }
+ }
+}
+
+/**
+ * Animates content change with the vertical shared axis pattern.
+ *
+ * See [Shared axis](https://material.io/design/motion/the-motion-system.html#shared-axis) for the
+ * detail about this motion pattern.
+ */
+@Composable
+private fun > SharedYAxis(
+ targetState: T,
+ modifier: Modifier = Modifier,
+ content: @Composable AnimatedVisibilityScope.(T) -> Unit
+) {
+ val exitDurationMillis = 80
+ val enterDurationMillis = 220
+
+ // This local function creates the AnimationSpec for outgoing elements.
+ fun exitSpec(): FiniteAnimationSpec =
+ tween(
+ durationMillis = exitDurationMillis,
+ easing = FastOutLinearInEasing
+ )
+
+ // This local function creates the AnimationSpec for incoming elements.
+ fun enterSpec(): FiniteAnimationSpec =
+ tween(
+ // The enter animation runs right after the exit animation.
+ delayMillis = exitDurationMillis,
+ durationMillis = enterDurationMillis,
+ easing = LinearOutSlowInEasing
+ )
+
+ val slideDistance = with(LocalDensity.current) { 30.dp.roundToPx() }
+
+ AnimatedContent(
+ targetState = targetState,
+ transitionSpec = {
+ // The state type () is Comparable.
+ // We compare the initial state and the target state to determine whether we are moving
+ // down or up.
+ if (initialState < targetState) { // Move down
+ ContentTransform(
+ // Outgoing elements fade out and slide up to the top.
+ initialContentExit = fadeOut(exitSpec()) +
+ slideOutVertically(exitSpec()) { -slideDistance },
+ // Incoming elements fade in and slide up from the bottom.
+ targetContentEnter = fadeIn(enterSpec()) +
+ slideInVertically(enterSpec()) { slideDistance }
+ )
+ } else { // Move up
+ ContentTransform(
+ // Outgoing elements fade out and slide down to the bottom.
+ initialContentExit = fadeOut(exitSpec()) +
+ slideOutVertically(exitSpec()) { slideDistance },
+ // Outgoing elements fade in and slide down from the top.
+ targetContentEnter = fadeIn(enterSpec()) +
+ slideInVertically(enterSpec()) { -slideDistance }
+ )
+ }
+ },
+ modifier = modifier,
+ content = content
+ )
+}
+
+private class Page(
+ val id: Int,
+ @DrawableRes
+ val image: Int,
+ val title: String,
+ val body: String
+) : Comparable {
+
+ override fun compareTo(other: Page): Int {
+ return id.compareTo(other.id)
+ }
+}
+
+private fun createPages(): List {
+ val body = LoremIpsum().values.joinToString(separator = " ").replace('\n', ' ')
+ return (0..4).map { i ->
+ Page(
+ id = i + 1,
+ image = CheeseImages[i % CheeseImages.size],
+ title = CheeseNames[i * 128],
+ body = body
+ )
+ }
+}
+
+@Composable
+private fun PageIndicatorsColumn(
+ pages: List,
+ selectedId: Int,
+ onIndicatorClick: (index: Int) -> Unit,
+ modifier: Modifier = Modifier
+) {
+ Column(
+ modifier = modifier
+ .verticalScroll(rememberScrollState())
+ .padding(16.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ verticalArrangement = Arrangement.spacedBy(32.dp),
+ ) {
+ for (page in pages) {
+ PageIndicator(
+ index = page.id,
+ selected = selectedId == page.id,
+ onClick = { onIndicatorClick(page.id) }
+ )
+ }
+ }
+}
+
+@Composable
+private fun PageIndicator(
+ index: Int,
+ selected: Boolean,
+ onClick: () -> Unit
+) {
+ val transition = updateTransition(targetState = selected, label = "indicator")
+ val backgroundColor by transition.animateColor(label = "background color") { targetSelected ->
+ if (targetSelected) {
+ MaterialTheme.colorScheme.primary
+ } else {
+ MaterialTheme.colorScheme.surfaceVariant
+ }
+ }
+ val textColor by transition.animateColor(label = "text color") { targetSelected ->
+ if (targetSelected) {
+ MaterialTheme.colorScheme.onPrimary
+ } else {
+ MaterialTheme.colorScheme.onSurfaceVariant
+ }
+ }
+ IconButton(onClick = onClick) {
+ Text(
+ text = index.toString(),
+ modifier = Modifier
+ .size(32.dp)
+ .background(backgroundColor, CircleShape)
+ .wrapContentSize(),
+ color = textColor,
+ fontSize = 20.sp,
+ fontWeight = FontWeight.Bold
+ )
+ }
+}
+
+@Composable
+private fun PageContent(
+ page: Page,
+ modifier: Modifier = Modifier
+) {
+ Column(
+ modifier = modifier
+ .verticalScroll(rememberScrollState())
+ .navigationBarsPadding(),
+ verticalArrangement = Arrangement.spacedBy(16.dp)
+ ) {
+ Image(
+ painter = painterResource(page.image),
+ contentDescription = null,
+ modifier = Modifier
+ .fillMaxWidth()
+ .aspectRatio(16f / 9f)
+ .clip(RoundedCornerShape(16.dp)),
+ contentScale = ContentScale.Crop
+ )
+ Text(
+ text = page.title,
+ style = MaterialTheme.typography.titleLarge
+ )
+ Text(
+ text = page.body,
+ textAlign = TextAlign.Justify
+ )
+ }
+}
+
+@Preview(showBackground = true)
+@Composable
+private fun PreviewSharedAxisDemo() {
+ SharedAxisDemo()
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedtransform/SharedTransformDemo.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedtransform/SharedTransformDemo.kt
new file mode 100644
index 000000000..3e913c782
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/demo/sharedtransform/SharedTransformDemo.kt
@@ -0,0 +1,261 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.animations.demo.sharedtransform
+
+import androidx.compose.animation.AnimatedContent
+import androidx.compose.animation.animateColor
+import androidx.compose.animation.core.updateTransition
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.aspectRatio
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.widthIn
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.HorizontalDivider
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Surface
+import androidx.compose.material3.Text
+import androidx.compose.material3.TextButton
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.text.style.TextAlign
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
+import androidx.compose.ui.unit.dp
+import androidx.constraintlayout.compose.ConstraintLayout
+import com.example.compose.snippets.R
+import com.example.compose.snippets.animations.demo.SimpleScaffold
+import com.example.compose.snippets.animations.demo.fadethrough.fadeThrough
+import com.example.compose.snippets.ui.theme.SnippetsTheme
+
+/**
+ * Complex layout changes use a shared transformation to create smooth transitions from
+ * one layout to the next. Elements are grouped together and transform as a single unit,
+ * rather than animating independently. This avoids multiple transformations overlapping
+ * and competing for attention.
+ */
+@Composable
+fun SharedTransformDemo() {
+ SimpleScaffold(title = "Shared transform") {
+ DemoCard(
+ modifier = Modifier
+ .align(Alignment.Center)
+ .padding(horizontal = 32.dp)
+ .widthIn(max = 384.dp)
+ .fillMaxWidth()
+ )
+ }
+}
+
+@Composable
+private fun DemoCard(
+ modifier: Modifier = Modifier
+) {
+ Surface(
+ modifier = modifier,
+ tonalElevation = 2.dp,
+ shape = RoundedCornerShape(16.dp),
+ ) {
+ // The content of this card is laid out by this ConstraintLayout.
+ ConstraintLayout {
+ // The card is either expanded or collapsed.
+ var expanded by rememberSaveable { mutableStateOf(false) }
+
+ // The ConstraintLayout has 4 constrained elements. They animate separately during the
+ // animation, except for the icon that is shared in both the expanded and the
+ // collapsed states.
+ val (content, icon, divider, button) = createRefs()
+
+ // This transition object coordinates different kinds of animations.
+ val transition = updateTransition(targetState = expanded, label = "card")
+
+ // This is the main content of the card.
+ // By using the AnimatedContent composable as an extension function of the transition
+ // object, the animation runs in sync with other animations of the transition.
+ // The height of this element animates on the state change (SizeTransform), and the
+ // ConstraintLayout can lay out its children based on the constraints continuously
+ // during the animation.
+ transition.AnimatedContent(
+ // We use the fade-through effect for elements that change between the states.
+ transitionSpec = fadeThrough(),
+ modifier = Modifier.constrainAs(content) {
+ top.linkTo(parent.top, margin = 16.dp)
+ start.linkTo(parent.start)
+ end.linkTo(parent.end)
+ }
+ ) { targetExpanded ->
+ CardContent(expanded = targetExpanded)
+ }
+
+ // The icon is shared between the expanded and collapsed states.
+ CardIcon(
+ modifier = Modifier.constrainAs(icon) {
+ top.linkTo(parent.top, margin = 16.dp)
+ end.linkTo(parent.end, margin = 16.dp)
+ }
+ )
+
+ // The divider becomes transparent in the collapsed state.
+ val dividerColor by transition.animateColor(label = "divider color") { targetExpanded ->
+ if (targetExpanded) {
+ MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f)
+ } else {
+ Color.Transparent
+ }
+ }
+ HorizontalDivider(
+ modifier = Modifier.constrainAs(divider) {
+ top.linkTo(content.bottom)
+ start.linkTo(parent.start)
+ end.linkTo(parent.end)
+ },
+ color = dividerColor
+ )
+
+ // The expand/collapse button is shared between the expanded and collapsed states.
+ TextButton(
+ onClick = { expanded = !expanded },
+ modifier = Modifier.constrainAs(button) {
+ top.linkTo(divider.bottom, margin = 8.dp)
+ start.linkTo(parent.start, margin = 8.dp)
+ // The button is constrained to the bottom of the parent so that it remains
+ // visible during the animations.
+ bottom.linkTo(parent.bottom, margin = 8.dp)
+ }
+ ) {
+ // The AnimatedContent extension function can be used for any descendant elements,
+ // not just direct children.
+ transition.AnimatedContent(transitionSpec = fadeThrough()) { targetExpanded ->
+ Text(text = if (targetExpanded) "COLLAPSE" else "EXPAND")
+ }
+ }
+ }
+ }
+}
+
+private val CheeseImages = listOf(
+ R.drawable.cheese_1 to "Cheese 1",
+ R.drawable.cheese_2 to "Cheese 2",
+ R.drawable.cheese_3 to "Cheese 3",
+ R.drawable.cheese_4 to "Cheese 4",
+ R.drawable.cheese_5 to "Cheese 5"
+)
+
+@Composable
+private fun CardContent(expanded: Boolean, modifier: Modifier = Modifier) {
+ Column(modifier = modifier) {
+ if (expanded) {
+ ContentTitle(modifier = Modifier.padding(horizontal = 16.dp))
+ ContentMaker(
+ modifier = Modifier.padding(top = 8.dp, start = 16.dp, end = 16.dp, bottom = 16.dp)
+ )
+ ContentImagesRow(images = CheeseImages.subList(0, 2))
+ Spacer(modifier = Modifier.height(1.dp))
+ ContentImagesRow(images = CheeseImages.subList(2, 5))
+ ContentBody(maxLines = 2, modifier = Modifier.padding(16.dp))
+ } else {
+ ContentMaker(modifier = Modifier.padding(horizontal = 16.dp))
+ ContentTitle(modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp))
+ ContentBody(maxLines = 1, Modifier.padding(start = 16.dp, end = 16.dp, bottom = 16.dp))
+ ContentImagesRow(images = CheeseImages)
+ }
+ }
+}
+
+@Composable
+private fun ContentTitle(modifier: Modifier = Modifier) {
+ Text(
+ text = "Cheeses",
+ modifier = modifier,
+ style = MaterialTheme.typography.titleLarge
+ )
+}
+
+@Composable
+private fun ContentMaker(modifier: Modifier = Modifier) {
+ Text(
+ text = "Maker: Android Cheese",
+ modifier = modifier,
+ style = MaterialTheme.typography.bodyMedium,
+ color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.8f)
+ )
+}
+
+@Composable
+private fun CardIcon(modifier: Modifier = Modifier) {
+ Image(
+ painter = painterResource(R.drawable.cheese_1),
+ contentDescription = null,
+ modifier = modifier
+ .size(48.dp)
+ .clip(CircleShape)
+ )
+}
+
+@Composable
+private fun ContentImagesRow(images: List>, modifier: Modifier = Modifier) {
+ Row(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(1.dp)) {
+ for ((resourceId, contentDescription) in images) {
+ Image(
+ painter = painterResource(resourceId),
+ contentDescription = contentDescription,
+ contentScale = ContentScale.Crop,
+ modifier = Modifier
+ .weight(1f)
+ .aspectRatio(1f)
+ )
+ }
+ }
+}
+
+@Composable
+private fun ContentBody(maxLines: Int, modifier: Modifier = Modifier) {
+ Text(
+ text = LoremIpsum(32).values.joinToString(" ").replace('\n', ' '),
+ modifier = modifier,
+ style = MaterialTheme.typography.bodyMedium,
+ maxLines = maxLines,
+ overflow = TextOverflow.Ellipsis,
+ textAlign = TextAlign.Justify,
+ color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.8f)
+ )
+}
+
+@Preview(showBackground = true)
+@Composable
+private fun PreviewExpandedContent() {
+ SnippetsTheme {
+ SharedTransformDemo()
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementBlurSnippet.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementBlurSnippet.kt
index 92bee9098..e07cbec6f 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementBlurSnippet.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementBlurSnippet.kt
@@ -157,7 +157,7 @@ fun SharedTransitionScope.SnackItem(
SnackContents(
snack = snack,
modifier = Modifier.sharedElement(
- state = rememberSharedContentState(key = snack.name),
+ sharedContentState = rememberSharedContentState(key = snack.name),
animatedVisibilityScope = this@AnimatedVisibility,
boundsTransform = boundsTransition,
),
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementSnippets.kt
index 955dbe460..5a51b8d56 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/AnimatedVisibilitySharedElementSnippets.kt
@@ -111,7 +111,7 @@ private fun AnimatedVisibilitySharedElementShortenedExample() {
SnackContents(
snack = snack,
modifier = Modifier.sharedElement(
- state = rememberSharedContentState(key = snack.name),
+ sharedContentState = rememberSharedContentState(key = snack.name),
animatedVisibilityScope = this@AnimatedVisibility
),
onClick = {
@@ -175,7 +175,7 @@ fun SharedTransitionScope.SnackEditDetails(
SnackContents(
snack = targetSnack,
modifier = Modifier.sharedElement(
- state = rememberSharedContentState(key = targetSnack.name),
+ sharedContentState = rememberSharedContentState(key = targetSnack.name),
animatedVisibilityScope = this@AnimatedContent,
),
onClick = {
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/CustomizeSharedElementsSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/CustomizeSharedElementsSnippets.kt
index 3e60c10e8..079c68347 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/CustomizeSharedElementsSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/CustomizeSharedElementsSnippets.kt
@@ -18,6 +18,7 @@
package com.example.compose.snippets.animations.sharedelement
+import androidx.activity.compose.PredictiveBackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
@@ -30,7 +31,9 @@ import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.core.ArcMode
import androidx.compose.animation.core.ExperimentalAnimationSpecApi
import androidx.compose.animation.core.FastOutSlowInEasing
+import androidx.compose.animation.core.SeekableTransitionState
import androidx.compose.animation.core.keyframes
+import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
@@ -60,12 +63,15 @@ import androidx.compose.material.icons.outlined.Create
import androidx.compose.material.icons.outlined.Favorite
import androidx.compose.material.icons.outlined.Share
import androidx.compose.material3.Icon
+import androidx.compose.material3.Slider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -85,6 +91,8 @@ import androidx.navigation.navArgument
import com.example.compose.snippets.R
import com.example.compose.snippets.ui.theme.LavenderLight
import com.example.compose.snippets.ui.theme.RoseLight
+import kotlin.coroutines.cancellation.CancellationException
+import kotlinx.coroutines.launch
@Preview
@Composable
@@ -628,3 +636,97 @@ fun PlaceholderSizeAnimated_Demo() {
}
// [END android_compose_shared_element_placeholder_size]
}
+
+private sealed class Screen {
+ data object Home : Screen()
+ data class Details(val id: Int) : Screen()
+}
+
+@Preview
+@Composable
+fun CustomPredictiveBackHandle() {
+ // [START android_compose_shared_element_custom_seeking]
+ val seekableTransitionState = remember {
+ SeekableTransitionState(Screen.Home)
+ }
+ val transition = rememberTransition(transitionState = seekableTransitionState)
+
+ PredictiveBackHandler(seekableTransitionState.currentState is Screen.Details) { progress ->
+ try {
+ // Whilst a back gesture is in progress, backEvents will be fired for each progress
+ // update.
+ progress.collect { backEvent ->
+ // For each backEvent that comes in, we manually seekTo the reported back progress
+ try {
+ seekableTransitionState.seekTo(backEvent.progress, targetState = Screen.Home)
+ } catch (_: CancellationException) {
+ // seekTo may be cancelled as expected, if animateTo or subsequent seekTo calls
+ // before the current seekTo finishes, in this case, we ignore the cancellation.
+ }
+ }
+ // Once collection has completed, we are either fully in the target state, or need
+ // to progress towards the end.
+ seekableTransitionState.animateTo(seekableTransitionState.targetState)
+ } catch (e: CancellationException) {
+ // When the predictive back gesture is cancelled, we snap to the end state to ensure
+ // it completes its seeking animation back to the currentState
+ seekableTransitionState.snapTo(seekableTransitionState.currentState)
+ throw e
+ }
+ }
+ val coroutineScope = rememberCoroutineScope()
+ var lastNavigatedIndex by remember {
+ mutableIntStateOf(0)
+ }
+ Column {
+ Slider(
+ modifier = Modifier.height(48.dp),
+ value = seekableTransitionState.fraction,
+ onValueChange = {
+ coroutineScope.launch {
+ if (seekableTransitionState.currentState is Screen.Details) {
+ seekableTransitionState.seekTo(it, Screen.Home)
+ } else {
+ // seek to the previously navigated index
+ seekableTransitionState.seekTo(it, Screen.Details(lastNavigatedIndex))
+ }
+ }
+ }
+ )
+ SharedTransitionLayout(modifier = Modifier.weight(1f)) {
+ transition.AnimatedContent { targetState ->
+ when (targetState) {
+ Screen.Home -> {
+ HomeScreen(
+ this@SharedTransitionLayout,
+ this@AnimatedContent,
+ onItemClick = {
+ coroutineScope.launch {
+ lastNavigatedIndex = it
+ seekableTransitionState.animateTo(Screen.Details(it))
+ }
+ }
+ )
+ }
+
+ is Screen.Details -> {
+ val snack = listSnacks[targetState.id]
+ DetailsScreen(
+ targetState.id,
+ snack,
+ this@SharedTransitionLayout,
+ this@AnimatedContent,
+ onBackPressed = {
+ coroutineScope.launch {
+ seekableTransitionState.animateTo(Screen.Home)
+ }
+ }
+ )
+ }
+ }
+ }
+ }
+ }
+
+ // [END android_compose_shared_element_custom_seeking]
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/SharedElementsWithNavigationSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/SharedElementsWithNavigationSnippets.kt
index da1d178fb..cac68a586 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/SharedElementsWithNavigationSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/SharedElementsWithNavigationSnippets.kt
@@ -46,7 +46,6 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
-import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
@@ -75,9 +74,9 @@ fun SharedElement_PredictiveBack() {
) {
composable("home") {
HomeScreen(
- navController,
this@SharedTransitionLayout,
- this@composable
+ this@composable,
+ { navController.navigate("details/$it") }
)
}
composable(
@@ -87,11 +86,13 @@ fun SharedElement_PredictiveBack() {
val id = backStackEntry.arguments?.getInt("item")
val snack = listSnacks[id!!]
DetailsScreen(
- navController,
id,
snack,
this@SharedTransitionLayout,
- this@composable
+ this@composable,
+ {
+ navController.navigate("home")
+ }
)
}
}
@@ -99,19 +100,19 @@ fun SharedElement_PredictiveBack() {
}
@Composable
-private fun DetailsScreen(
- navController: NavHostController,
+fun DetailsScreen(
id: Int,
snack: Snack,
sharedTransitionScope: SharedTransitionScope,
- animatedContentScope: AnimatedContentScope
+ animatedContentScope: AnimatedContentScope,
+ onBackPressed: () -> Unit
) {
with(sharedTransitionScope) {
Column(
Modifier
.fillMaxSize()
.clickable {
- navController.navigate("home")
+ onBackPressed()
}
) {
Image(
@@ -141,10 +142,10 @@ private fun DetailsScreen(
}
@Composable
-private fun HomeScreen(
- navController: NavHostController,
+fun HomeScreen(
sharedTransitionScope: SharedTransitionScope,
- animatedContentScope: AnimatedContentScope
+ animatedContentScope: AnimatedContentScope,
+ onItemClick: (Int) -> Unit,
) {
LazyColumn(
modifier = Modifier
@@ -155,7 +156,7 @@ private fun HomeScreen(
itemsIndexed(listSnacks) { index, item ->
Row(
Modifier.clickable {
- navController.navigate("details/$index")
+ onItemClick(index)
}
) {
Spacer(modifier = Modifier.width(8.dp))
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/IconButton.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/IconButton.kt
new file mode 100644
index 000000000..59727c71b
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/IconButton.kt
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.components
+
+import androidx.compose.foundation.interaction.MutableInteractionSource
+import androidx.compose.foundation.interaction.collectIsPressedAsState
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.material3.Icon
+import androidx.compose.material3.IconButton
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberUpdatedState
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.tooling.preview.Preview
+import com.example.compose.snippets.R
+import kotlinx.coroutines.delay
+
+// [START android_compose_components_togglebuttonexample]
+@Preview
+@Composable
+fun ToggleIconButtonExample() {
+ // isToggled initial value should be read from a view model or persistent storage.
+ var isToggled by rememberSaveable { mutableStateOf(false) }
+
+ IconButton(
+ onClick = { isToggled = !isToggled }
+ ) {
+ Icon(
+ painter = if (isToggled) painterResource(R.drawable.favorite_filled) else painterResource(R.drawable.favorite),
+ contentDescription = if (isToggled) "Selected icon button" else "Unselected icon button."
+ )
+ }
+}
+// [END android_compose_components_togglebuttonexample]
+
+// [START android_compose_components_iconbutton]
+@Composable
+fun MomentaryIconButton(
+ unselectedImage: Int,
+ selectedImage: Int,
+ contentDescription: String,
+ modifier: Modifier = Modifier,
+ stepDelay: Long = 100L, // Minimum value is 1L milliseconds.
+ onClick: () -> Unit
+) {
+ val interactionSource = remember { MutableInteractionSource() }
+ val isPressed by interactionSource.collectIsPressedAsState()
+ val pressedListener by rememberUpdatedState(onClick)
+
+ LaunchedEffect(isPressed) {
+ while (isPressed) {
+ delay(stepDelay.coerceIn(1L, Long.MAX_VALUE))
+ pressedListener()
+ }
+ }
+
+ IconButton(
+ modifier = modifier,
+ onClick = onClick,
+ interactionSource = interactionSource
+ ) {
+ Icon(
+ painter = if (isPressed) painterResource(id = selectedImage) else painterResource(id = unselectedImage),
+ contentDescription = contentDescription,
+ )
+ }
+}
+// [END android_compose_components_iconbutton]
+
+// [START android_compose_components_momentaryiconbuttons]
+@Preview()
+@Composable
+fun MomentaryIconButtonExample() {
+ var pressedCount by remember { mutableIntStateOf(0) }
+
+ Row(
+ modifier = Modifier.fillMaxWidth(),
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ MomentaryIconButton(
+ unselectedImage = R.drawable.fast_rewind,
+ selectedImage = R.drawable.fast_rewind_filled,
+ stepDelay = 100L,
+ onClick = { pressedCount -= 1 },
+ contentDescription = "Decrease count button"
+ )
+ Spacer(modifier = Modifier)
+ Text("advanced by $pressedCount frames")
+ Spacer(modifier = Modifier)
+ MomentaryIconButton(
+ unselectedImage = R.drawable.fast_forward,
+ selectedImage = R.drawable.fast_forward_filled,
+ contentDescription = "Increase count button",
+ stepDelay = 100L,
+ onClick = { pressedCount += 1 }
+ )
+ }
+}
+// [END android_compose_components_momentaryiconbuttons]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt
new file mode 100644
index 000000000..9cad68934
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.components
+
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.Album
+import androidx.compose.material.icons.filled.MusicNote
+import androidx.compose.material.icons.filled.PlaylistAddCircle
+import androidx.compose.material3.ExperimentalMaterial3Api
+import androidx.compose.material3.Icon
+import androidx.compose.material3.NavigationBar
+import androidx.compose.material3.NavigationBarDefaults
+import androidx.compose.material3.NavigationBarItem
+import androidx.compose.material3.NavigationRail
+import androidx.compose.material3.NavigationRailItem
+import androidx.compose.material3.PrimaryTabRow
+import androidx.compose.material3.Scaffold
+import androidx.compose.material3.Tab
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.vector.ImageVector
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.navigation.NavHostController
+import androidx.navigation.compose.NavHost
+import androidx.navigation.compose.composable
+import androidx.navigation.compose.rememberNavController
+
+@Composable
+fun SongsScreen(modifier: Modifier = Modifier) {
+ Box(
+ modifier = Modifier.fillMaxSize(),
+ contentAlignment = Alignment.Center
+ ) {
+ Text("Songs Screen")
+ }
+}
+
+@Composable
+fun AlbumScreen(modifier: Modifier = Modifier) {
+ Box(
+ modifier = Modifier.fillMaxSize(),
+ contentAlignment = Alignment.Center
+ ) {
+ Text("Album Screen")
+ }
+}
+
+@Composable
+fun PlaylistScreen(modifier: Modifier = Modifier) {
+ Box(
+ modifier = Modifier.fillMaxSize(),
+ contentAlignment = Alignment.Center
+ ) {
+ Text("Playlist Screen")
+ }
+}
+
+enum class Destination(
+ val route: String,
+ val label: String,
+ val icon: ImageVector,
+ val contentDescription: String
+) {
+ SONGS("songs", "Songs", Icons.Default.MusicNote, "Songs"),
+ ALBUM("album", "Album", Icons.Default.Album, "Album"),
+ PLAYLISTS("playlist", "Playlist", Icons.Default.PlaylistAddCircle, "Playlist")
+}
+
+@Composable
+fun AppNavHost(
+ navController: NavHostController,
+ startDestination: Destination,
+ modifier: Modifier = Modifier
+) {
+ NavHost(
+ navController,
+ startDestination = startDestination.route
+ ) {
+ Destination.entries.forEach { destination ->
+ composable(destination.route) {
+ when (destination) {
+ Destination.SONGS -> SongsScreen()
+ Destination.ALBUM -> AlbumScreen()
+ Destination.PLAYLISTS -> PlaylistScreen()
+ }
+ }
+ }
+ }
+}
+
+@Preview()
+// [START android_compose_components_navigationbarexample]
+@Composable
+fun NavigationBarExample(modifier: Modifier = Modifier) {
+ val navController = rememberNavController()
+ val startDestination = Destination.SONGS
+ var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }
+
+ Scaffold(
+ modifier = modifier,
+ bottomBar = {
+ NavigationBar(windowInsets = NavigationBarDefaults.windowInsets) {
+ Destination.entries.forEachIndexed { index, destination ->
+ NavigationBarItem(
+ selected = selectedDestination == index,
+ onClick = {
+ navController.navigate(route = destination.route)
+ selectedDestination = index
+ },
+ icon = {
+ Icon(
+ destination.icon,
+ contentDescription = destination.contentDescription
+ )
+ },
+ label = { Text(destination.label) }
+ )
+ }
+ }
+ }
+ ) { contentPadding ->
+ AppNavHost(navController, startDestination, modifier = Modifier.padding(contentPadding))
+ }
+}
+// [END android_compose_components_navigationbarexample]
+
+@Preview()
+// [START android_compose_components_navigationrailexample]
+@Composable
+fun NavigationRailExample(modifier: Modifier = Modifier) {
+ val navController = rememberNavController()
+ val startDestination = Destination.SONGS
+ var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }
+
+ Scaffold(modifier = modifier) { contentPadding ->
+ NavigationRail(modifier = Modifier.padding(contentPadding)) {
+ Destination.entries.forEachIndexed { index, destination ->
+ NavigationRailItem(
+ selected = selectedDestination == index,
+ onClick = {
+ navController.navigate(route = destination.route)
+ selectedDestination = index
+ },
+ icon = {
+ Icon(
+ destination.icon,
+ contentDescription = destination.contentDescription
+ )
+ },
+ label = { Text(destination.label) }
+ )
+ }
+ }
+ AppNavHost(navController, startDestination)
+ }
+}
+// [END android_compose_components_navigationrailexample]
+
+@OptIn(ExperimentalMaterial3Api::class)
+@Preview(showBackground = true)
+// [START android_compose_components_navigationtabexample]
+@Composable
+fun NavigationTabExample(modifier: Modifier = Modifier) {
+ val navController = rememberNavController()
+ val startDestination = Destination.SONGS
+ var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }
+
+ Scaffold(modifier = modifier) { contentPadding ->
+ PrimaryTabRow(selectedTabIndex = selectedDestination, modifier = Modifier.padding(contentPadding)) {
+ Destination.entries.forEachIndexed { index, destination ->
+ Tab(
+ selected = selectedDestination == index,
+ onClick = {
+ navController.navigate(route = destination.route)
+ selectedDestination = index
+ },
+ text = {
+ Text(
+ text = destination.label,
+ maxLines = 2,
+ overflow = TextOverflow.Ellipsis
+ )
+ }
+ )
+ }
+ }
+ AppNavHost(navController, startDestination)
+ }
+}
+// [END android_compose_components_navigationtabexample]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt
new file mode 100644
index 000000000..93961f245
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/RadioButton.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.components
+
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.selection.selectable
+import androidx.compose.foundation.selection.selectableGroup
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.RadioButton
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.semantics.Role
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+
+// [START android_compose_components_radiobuttonsingleselection]
+@Composable
+fun RadioButtonSingleSelection(modifier: Modifier = Modifier) {
+ val radioOptions = listOf("Calls", "Missed", "Friends")
+ val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
+ // Note that Modifier.selectableGroup() is essential to ensure correct accessibility behavior
+ Column(modifier.selectableGroup()) {
+ radioOptions.forEach { text ->
+ Row(
+ Modifier
+ .fillMaxWidth()
+ .height(56.dp)
+ .selectable(
+ selected = (text == selectedOption),
+ onClick = { onOptionSelected(text) },
+ role = Role.RadioButton
+ )
+ .padding(horizontal = 16.dp),
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ RadioButton(
+ selected = (text == selectedOption),
+ onClick = null // null recommended for accessibility with screen readers
+ )
+ Text(
+ text = text,
+ style = MaterialTheme.typography.bodyLarge,
+ modifier = Modifier.padding(start = 16.dp)
+ )
+ }
+ }
+ }
+}
+// [END android_compose_components_radiobuttonsingleselection]
+
+@Preview
+@Composable
+private fun RadioButtonSingleSelectionPreview() {
+ RadioButtonSingleSelection()
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.kt
new file mode 100644
index 000000000..f3243e299
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.kt
@@ -0,0 +1,299 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.components
+
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.PaddingValues
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.text.input.TextFieldState
+import androidx.compose.foundation.text.input.rememberTextFieldState
+import androidx.compose.foundation.verticalScroll
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.MoreVert
+import androidx.compose.material.icons.filled.Search
+import androidx.compose.material.icons.filled.Star
+import androidx.compose.material3.Button
+import androidx.compose.material3.ExperimentalMaterial3Api
+import androidx.compose.material3.Icon
+import androidx.compose.material3.ListItem
+import androidx.compose.material3.ListItemDefaults
+import androidx.compose.material3.SearchBar
+import androidx.compose.material3.SearchBarDefaults
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.derivedStateOf
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.semantics.isTraversalGroup
+import androidx.compose.ui.semantics.semantics
+import androidx.compose.ui.semantics.traversalIndex
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+
+@Preview
+@Composable
+fun SearchBarExamples() {
+ Column(
+ modifier = Modifier
+ .padding(16.dp)
+ .fillMaxSize(),
+ verticalArrangement = Arrangement.spacedBy(24.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ ) {
+ var currentExample by remember { mutableStateOf(null) }
+
+ when (currentExample) {
+ "simple" -> SimpleSearchBarExample()
+ "fancy" -> CustomizableSearchBarExample()
+ else -> {
+ Button(onClick = { currentExample = "simple" }) {
+ Text("Simple SearchBar")
+ }
+ Button(onClick = { currentExample = "fancy" }) {
+ Text("Customizable SearchBar")
+ }
+ }
+ }
+ }
+}
+
+// [START android_compose_components_simple_searchbar]
+@OptIn(ExperimentalMaterial3Api::class)
+@Composable
+fun SimpleSearchBar(
+ textFieldState: TextFieldState,
+ onSearch: (String) -> Unit,
+ searchResults: List,
+ modifier: Modifier = Modifier
+) {
+ // Controls expansion state of the search bar
+ var expanded by rememberSaveable { mutableStateOf(false) }
+
+ Box(
+ modifier
+ .fillMaxSize()
+ .semantics { isTraversalGroup = true }
+ ) {
+ SearchBar(
+ modifier = Modifier
+ .align(Alignment.TopCenter)
+ .semantics { traversalIndex = 0f },
+ inputField = {
+ SearchBarDefaults.InputField(
+ query = textFieldState.text.toString(),
+ onQueryChange = { textFieldState.edit { replace(0, length, it) } },
+ onSearch = {
+ onSearch(textFieldState.text.toString())
+ expanded = false
+ },
+ expanded = expanded,
+ onExpandedChange = { expanded = it },
+ placeholder = { Text("Search") }
+ )
+ },
+ expanded = expanded,
+ onExpandedChange = { expanded = it },
+ ) {
+ // Display search results in a scrollable column
+ Column(Modifier.verticalScroll(rememberScrollState())) {
+ searchResults.forEach { result ->
+ ListItem(
+ headlineContent = { Text(result) },
+ modifier = Modifier
+ .clickable {
+ textFieldState.edit { replace(0, length, result) }
+ expanded = false
+ }
+ .fillMaxWidth()
+ )
+ }
+ }
+ }
+ }
+}
+// [END android_compose_components_simple_searchbar]
+
+@Preview(showBackground = true)
+@Composable
+private fun SimpleSearchBarExample() {
+ // Create and remember the text field state
+ val textFieldState = rememberTextFieldState()
+ val items = listOf(
+ "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
+ "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop"
+ )
+
+ // Filter items based on the current search text
+ val filteredItems by remember {
+ derivedStateOf {
+ val searchText = textFieldState.text.toString()
+ if (searchText.isEmpty()) {
+ emptyList()
+ } else {
+ items.filter { it.contains(searchText, ignoreCase = true) }
+ }
+ }
+ }
+
+ SimpleSearchBar(
+ textFieldState = textFieldState,
+ onSearch = { /* Handle search submission */ },
+ searchResults = filteredItems
+ )
+}
+
+// [START android_compose_components_customizable_searchbar]
+@OptIn(ExperimentalMaterial3Api::class)
+@Composable
+fun CustomizableSearchBar(
+ query: String,
+ onQueryChange: (String) -> Unit,
+ onSearch: (String) -> Unit,
+ searchResults: List,
+ onResultClick: (String) -> Unit,
+ // Customization options
+ placeholder: @Composable () -> Unit = { Text("Search") },
+ leadingIcon: @Composable (() -> Unit)? = { Icon(Icons.Default.Search, contentDescription = "Search") },
+ trailingIcon: @Composable (() -> Unit)? = null,
+ supportingContent: (@Composable (String) -> Unit)? = null,
+ leadingContent: (@Composable () -> Unit)? = null,
+ modifier: Modifier = Modifier
+) {
+ // Track expanded state of search bar
+ var expanded by rememberSaveable { mutableStateOf(false) }
+
+ Box(
+ modifier
+ .fillMaxSize()
+ .semantics { isTraversalGroup = true }
+ ) {
+ SearchBar(
+ modifier = Modifier
+ .align(Alignment.TopCenter)
+ .semantics { traversalIndex = 0f },
+ inputField = {
+ // Customizable input field implementation
+ SearchBarDefaults.InputField(
+ query = query,
+ onQueryChange = onQueryChange,
+ onSearch = {
+ onSearch(query)
+ expanded = false
+ },
+ expanded = expanded,
+ onExpandedChange = { expanded = it },
+ placeholder = placeholder,
+ leadingIcon = leadingIcon,
+ trailingIcon = trailingIcon
+ )
+ },
+ expanded = expanded,
+ onExpandedChange = { expanded = it },
+ ) {
+ // Show search results in a lazy column for better performance
+ LazyColumn {
+ items(count = searchResults.size) { index ->
+ val resultText = searchResults[index]
+ ListItem(
+ headlineContent = { Text(resultText) },
+ supportingContent = supportingContent?.let { { it(resultText) } },
+ leadingContent = leadingContent,
+ colors = ListItemDefaults.colors(containerColor = Color.Transparent),
+ modifier = Modifier
+ .clickable {
+ onResultClick(resultText)
+ expanded = false
+ }
+ .fillMaxWidth()
+ .padding(horizontal = 16.dp, vertical = 4.dp)
+ )
+ }
+ }
+ }
+ }
+}
+// [END android_compose_components_customizable_searchbar]
+
+@Preview(showBackground = true)
+@Composable
+fun CustomizableSearchBarExample() {
+ // Manage query state
+ var query by rememberSaveable { mutableStateOf("") }
+ val items = listOf(
+ "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
+ "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow",
+ "Nougat", "Oreo", "Pie"
+ )
+
+ // Filter items based on query
+ val filteredItems by remember {
+ derivedStateOf {
+ if (query.isEmpty()) {
+ items
+ } else {
+ items.filter { it.contains(query, ignoreCase = true) }
+ }
+ }
+ }
+
+ Column(modifier = Modifier.fillMaxSize()) {
+ CustomizableSearchBar(
+ query = query,
+ onQueryChange = { query = it },
+ onSearch = { /* Handle search submission */ },
+ searchResults = filteredItems,
+ onResultClick = { query = it },
+ // Customize appearance with optional parameters
+ placeholder = { Text("Search desserts") },
+ leadingIcon = { Icon(Icons.Default.Search, contentDescription = "Search") },
+ trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = "More options") },
+ supportingContent = { Text("Android dessert") },
+ leadingContent = { Icon(Icons.Filled.Star, contentDescription = "Starred item") }
+ )
+
+ // Display the filtered list below the search bar
+ LazyColumn(
+ contentPadding = PaddingValues(
+ start = 16.dp,
+ top = 72.dp, // Provides space for the search bar
+ end = 16.dp,
+ bottom = 16.dp
+ ),
+ verticalArrangement = Arrangement.spacedBy(8.dp),
+ modifier = Modifier.semantics {
+ traversalIndex = 1f
+ },
+ ) {
+ items(count = filteredItems.size) {
+ Text(text = filteredItems[it])
+ }
+ }
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/SwipeToDismissBox.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/SwipeToDismissBox.kt
new file mode 100644
index 000000000..22dc99303
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/SwipeToDismissBox.kt
@@ -0,0 +1,257 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.components
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.wrapContentSize
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.lazy.items
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.CheckBox
+import androidx.compose.material.icons.filled.CheckBoxOutlineBlank
+import androidx.compose.material.icons.filled.Delete
+import androidx.compose.material3.Icon
+import androidx.compose.material3.ListItem
+import androidx.compose.material3.OutlinedCard
+import androidx.compose.material3.SwipeToDismissBox
+import androidx.compose.material3.SwipeToDismissBoxValue.EndToStart
+import androidx.compose.material3.SwipeToDismissBoxValue.Settled
+import androidx.compose.material3.SwipeToDismissBoxValue.StartToEnd
+import androidx.compose.material3.Text
+import androidx.compose.material3.rememberSwipeToDismissBoxState
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.mutableStateListOf
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.drawBehind
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.RectangleShape
+import androidx.compose.ui.graphics.lerp
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+
+@Preview
+@Composable
+fun SwipeToDismissBoxExamples() {
+ Column(
+ modifier = Modifier
+ .padding(16.dp)
+ .fillMaxSize(),
+ verticalArrangement = Arrangement.spacedBy(24.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ ) {
+ Text("Swipe to dismiss with change of background", fontWeight = FontWeight.Bold)
+ SwipeItemExample()
+ Text("Swipe to dismiss with a cross-fade animation", fontWeight = FontWeight.Bold)
+ SwipeItemWithAnimationExample()
+ }
+}
+
+// [START android_compose_components_todoitem]
+data class TodoItem(
+ val itemDescription: String,
+ var isItemDone: Boolean = false
+)
+// [END android_compose_components_todoitem]
+
+// [START android_compose_components_swipeitem]
+@Composable
+fun TodoListItem(
+ todoItem: TodoItem,
+ onToggleDone: (TodoItem) -> Unit,
+ onRemove: (TodoItem) -> Unit,
+ modifier: Modifier = Modifier,
+) {
+ val swipeToDismissBoxState = rememberSwipeToDismissBoxState(
+ confirmValueChange = {
+ if (it == StartToEnd) onToggleDone(todoItem)
+ else if (it == EndToStart) onRemove(todoItem)
+ // Reset item when toggling done status
+ it != StartToEnd
+ }
+ )
+
+ SwipeToDismissBox(
+ state = swipeToDismissBoxState,
+ modifier = modifier.fillMaxSize(),
+ backgroundContent = {
+ when (swipeToDismissBoxState.dismissDirection) {
+ StartToEnd -> {
+ Icon(
+ if (todoItem.isItemDone) Icons.Default.CheckBox else Icons.Default.CheckBoxOutlineBlank,
+ contentDescription = if (todoItem.isItemDone) "Done" else "Not done",
+ modifier = Modifier
+ .fillMaxSize()
+ .background(Color.Blue)
+ .wrapContentSize(Alignment.CenterStart)
+ .padding(12.dp),
+ tint = Color.White
+ )
+ }
+ EndToStart -> {
+ Icon(
+ imageVector = Icons.Default.Delete,
+ contentDescription = "Remove item",
+ modifier = Modifier
+ .fillMaxSize()
+ .background(Color.Red)
+ .wrapContentSize(Alignment.CenterEnd)
+ .padding(12.dp),
+ tint = Color.White
+ )
+ }
+ Settled -> {}
+ }
+ }
+ ) {
+ ListItem(
+ headlineContent = { Text(todoItem.itemDescription) },
+ supportingContent = { Text("swipe me to update or remove.") }
+ )
+ }
+}
+// [END android_compose_components_swipeitem]
+
+@Preview(showBackground = true)
+// [START android_compose_components_swipeitemexample]
+@Composable
+private fun SwipeItemExample() {
+ val todoItems = remember {
+ mutableStateListOf(
+ TodoItem("Pay bills"), TodoItem("Buy groceries"),
+ TodoItem("Go to gym"), TodoItem("Get dinner")
+ )
+ }
+
+ LazyColumn {
+ items(
+ items = todoItems,
+ key = { it.itemDescription }
+ ) { todoItem ->
+ TodoListItem(
+ todoItem = todoItem,
+ onToggleDone = { todoItem ->
+ todoItem.isItemDone = !todoItem.isItemDone
+ },
+ onRemove = { todoItem ->
+ todoItems -= todoItem
+ },
+ modifier = Modifier.animateItem()
+ )
+ }
+ }
+}
+// [END android_compose_components_swipeitemexample]
+
+// [START android_compose_components_swipecarditem]
+@Composable
+fun TodoListItemWithAnimation(
+ todoItem: TodoItem,
+ onToggleDone: (TodoItem) -> Unit,
+ onRemove: (TodoItem) -> Unit,
+ modifier: Modifier = Modifier,
+) {
+ val swipeToDismissBoxState = rememberSwipeToDismissBoxState(
+ confirmValueChange = {
+ if (it == StartToEnd) onToggleDone(todoItem)
+ else if (it == EndToStart) onRemove(todoItem)
+ // Reset item when toggling done status
+ it != StartToEnd
+ }
+ )
+
+ SwipeToDismissBox(
+ state = swipeToDismissBoxState,
+ modifier = modifier.fillMaxSize(),
+ backgroundContent = {
+ when (swipeToDismissBoxState.dismissDirection) {
+ StartToEnd -> {
+ Icon(
+ if (todoItem.isItemDone) Icons.Default.CheckBox else Icons.Default.CheckBoxOutlineBlank,
+ contentDescription = if (todoItem.isItemDone) "Done" else "Not done",
+ modifier = Modifier
+ .fillMaxSize()
+ .drawBehind {
+ drawRect(lerp(Color.LightGray, Color.Blue, swipeToDismissBoxState.progress))
+ }
+ .wrapContentSize(Alignment.CenterStart)
+ .padding(12.dp),
+ tint = Color.White
+ )
+ }
+ EndToStart -> {
+ Icon(
+ imageVector = Icons.Default.Delete,
+ contentDescription = "Remove item",
+ modifier = Modifier
+ .fillMaxSize()
+ .background(lerp(Color.LightGray, Color.Red, swipeToDismissBoxState.progress))
+ .wrapContentSize(Alignment.CenterEnd)
+ .padding(12.dp),
+ tint = Color.White
+ )
+ }
+ Settled -> {}
+ }
+ }
+ ) {
+ OutlinedCard(shape = RectangleShape) {
+ ListItem(
+ headlineContent = { Text(todoItem.itemDescription) },
+ supportingContent = { Text("swipe me to update or remove.") }
+ )
+ }
+ }
+}
+// [END android_compose_components_swipecarditem]
+
+@Preview
+// [START android_compose_components_swipecarditemexample]
+@Composable
+private fun SwipeItemWithAnimationExample() {
+ val todoItems = remember {
+ mutableStateListOf(
+ TodoItem("Pay bills"), TodoItem("Buy groceries"),
+ TodoItem("Go to gym"), TodoItem("Get dinner")
+ )
+ }
+
+ LazyColumn {
+ items(
+ items = todoItems,
+ key = { it.itemDescription }
+ ) { todoItem ->
+ TodoListItemWithAnimation(
+ todoItem = todoItem,
+ onToggleDone = { todoItem ->
+ todoItem.isItemDone = !todoItem.isItemDone
+ },
+ onRemove = { todoItem ->
+ todoItems -= todoItem
+ },
+ modifier = Modifier.animateItem()
+ )
+ }
+ }
+}
+// [END android_compose_components_swipecarditemexample]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.kt
index 1b5d9ea04..1066d0cfb 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.kt
@@ -36,14 +36,14 @@ import androidx.compose.material3.TooltipBox
import androidx.compose.material3.TooltipDefaults
import androidx.compose.material3.rememberTooltipState
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.getValue
-import androidx.compose.runtime.setValue
+import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.launch
@Composable
fun TooltipExamples() {
@@ -147,6 +147,7 @@ fun AdvancedRichTooltipExample(
richTooltipActionText: String = "Dismiss"
) {
val tooltipState = rememberTooltipState()
+ val coroutineScope = rememberCoroutineScope()
TooltipBox(
modifier = modifier,
@@ -156,7 +157,11 @@ fun AdvancedRichTooltipExample(
title = { Text(richTooltipSubheadText) },
action = {
Row {
- TextButton(onClick = { tooltipState.dismiss() }) {
+ TextButton(onClick = {
+ coroutineScope.launch {
+ tooltipState.dismiss()
+ }
+ }) {
Text(richTooltipActionText)
}
}
@@ -168,7 +173,11 @@ fun AdvancedRichTooltipExample(
},
state = tooltipState
) {
- IconButton(onClick = { tooltipState.dismiss() }) {
+ IconButton(onClick = {
+ coroutineScope.launch {
+ tooltipState.show()
+ }
+ }) {
Icon(
imageVector = Icons.Filled.Camera,
contentDescription = "Open camera"
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt
index 46e245a3f..12542079e 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt
@@ -20,11 +20,11 @@ import android.content.ClipData
import android.content.ClipDescription
import android.os.Build
import android.view.View
+import androidx.activity.compose.LocalActivity
import androidx.annotation.RequiresApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.foundation.draganddrop.dragAndDropTarget
-import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
@@ -32,6 +32,7 @@ import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTarget
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.draganddrop.mimeTypes
+import androidx.compose.ui.draganddrop.toAndroidDragEvent
@RequiresApi(Build.VERSION_CODES.N)
@OptIn(ExperimentalFoundationApi::class)
@@ -40,40 +41,24 @@ private fun DragAndDropSnippet() {
val url = ""
- // [START android_compose_drag_and_drop_1]
- Modifier.dragAndDropSource {
- detectTapGestures(onLongPress = {
- // Transfer data here.
- })
- }
- // [END android_compose_drag_and_drop_1]
-
// [START android_compose_drag_and_drop_2]
- Modifier.dragAndDropSource {
- detectTapGestures(onLongPress = {
- startTransfer(
- DragAndDropTransferData(
- ClipData.newPlainText(
- "image Url", url
- )
- )
+ Modifier.dragAndDropSource { _ ->
+ DragAndDropTransferData(
+ ClipData.newPlainText(
+ "image Url", url
)
- })
+ )
}
// [END android_compose_drag_and_drop_2]
// [START android_compose_drag_and_drop_3]
- Modifier.dragAndDropSource {
- detectTapGestures(onLongPress = {
- startTransfer(
- DragAndDropTransferData(
- ClipData.newPlainText(
- "image Url", url
- ),
- flags = View.DRAG_FLAG_GLOBAL
- )
- )
- })
+ Modifier.dragAndDropSource { _ ->
+ DragAndDropTransferData(
+ ClipData.newPlainText(
+ "image Url", url
+ ),
+ flags = View.DRAG_FLAG_GLOBAL
+ )
}
// [END android_compose_drag_and_drop_3]
@@ -88,11 +73,27 @@ private fun DragAndDropSnippet() {
}
// [END android_compose_drag_and_drop_4]
+ LocalActivity.current?.let { activity ->
+ // [START android_compose_drag_and_drop_7]
+ val externalAppCallback = remember {
+ object : DragAndDropTarget {
+ override fun onDrop(event: DragAndDropEvent): Boolean {
+ val permission =
+ activity.requestDragAndDropPermissions(event.toAndroidDragEvent())
+ // Parse received data
+ permission?.release()
+ return true
+ }
+ }
+ }
+ // [END android_compose_drag_and_drop_7]
+ }
+
// [START android_compose_drag_and_drop_5]
Modifier.dragAndDropTarget(
shouldStartDragAndDrop = { event ->
event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN)
- }, target = callback
+ }, target = callback // or externalAppCallback
)
// [END android_compose_drag_and_drop_5]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlancePinAppWidget.kt b/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlancePinAppWidget.kt
new file mode 100644
index 000000000..428212068
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlancePinAppWidget.kt
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.glance
+
+import android.content.Context
+import androidx.compose.material3.Button
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.unit.DpSize
+import androidx.compose.ui.unit.dp
+import androidx.glance.GlanceId
+import androidx.glance.appwidget.GlanceAppWidget
+import androidx.glance.appwidget.GlanceAppWidgetManager
+import androidx.glance.appwidget.GlanceAppWidgetReceiver
+import kotlinx.coroutines.launch
+
+class MyWidgetReceiver : GlanceAppWidgetReceiver() {
+ override val glanceAppWidget: GlanceAppWidget = MyWidget()
+}
+
+class MyWidget : GlanceAppWidget() {
+ override suspend fun provideGlance(
+ context: Context,
+ id: GlanceId
+ ) {}
+}
+
+// [START android_compose_glance_in_app_pinning]
+@Composable
+fun AnInAppComposable() {
+ val context = LocalContext.current
+ val coroutineScope = rememberCoroutineScope()
+ Button(
+ onClick = {
+ coroutineScope.launch {
+ GlanceAppWidgetManager(context).requestPinGlanceAppWidget(
+ receiver = MyWidgetReceiver::class.java,
+ preview = MyWidget(),
+ previewState = DpSize(245.dp, 115.dp)
+ )
+ }
+ }
+ ) {}
+}
+// [END android_compose_glance_in_app_pinning]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt
index e1300358b..bb575a8b5 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt
@@ -39,6 +39,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.Button
@@ -69,6 +70,7 @@ import androidx.glance.appwidget.action.ActionCallback
import androidx.glance.appwidget.action.actionRunCallback
import androidx.glance.appwidget.action.actionSendBroadcast
import androidx.glance.appwidget.action.actionStartService
+import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.appwidget.updateAll
import androidx.glance.appwidget.updateIf
@@ -868,6 +870,32 @@ object GlanceTheming {
}
}
+object GlanceInnerPadding {
+
+ // [START android_compose_glance_innercornerradius]
+ /**
+ * Applies corner radius for views that are visually positioned [widgetPadding]dp inside of the
+ * widget background.
+ */
+ @Composable
+ fun GlanceModifier.appWidgetInnerCornerRadius(widgetPadding: Dp): GlanceModifier {
+
+ if (Build.VERSION.SDK_INT < 31) {
+ return this
+ }
+
+ val resources = LocalContext.current.resources
+ // get dimension in float (without rounding).
+ val px = resources.getDimension(android.R.dimen.system_app_widget_background_radius)
+ val widgetBackgroundRadiusDpValue = px / resources.displayMetrics.density
+ if (widgetBackgroundRadiusDpValue < widgetPadding.value) {
+ return this
+ }
+ return this.cornerRadius(Dp(widgetBackgroundRadiusDpValue - widgetPadding.value))
+ }
+ // [END android_compose_glance_innercornerradius]
+}
+
object GlanceInteroperability {
@Composable
fun example01() {
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/AdaptiveLayoutSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/AdaptiveLayoutSnippets.kt
index 7980eae05..9447582cf 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/AdaptiveLayoutSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/AdaptiveLayoutSnippets.kt
@@ -28,7 +28,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.dp
-import androidx.window.core.layout.WindowHeightSizeClass
import androidx.window.core.layout.WindowSizeClass
/*
@@ -51,10 +50,10 @@ import androidx.window.core.layout.WindowSizeClass
fun MyApp(
windowSizeClass: WindowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
) {
- // Perform logic on the size class to decide whether to show the top app bar.
- val showTopAppBar = windowSizeClass.windowHeightSizeClass != WindowHeightSizeClass.COMPACT
+ // Decide whether to show the top app bar based on window size class.
+ val showTopAppBar = windowSizeClass.isHeightAtLeastBreakpoint(WindowSizeClass.HEIGHT_DP_MEDIUM_LOWER_BOUND)
- // MyScreen knows nothing about window sizes, and performs logic based on a Boolean flag.
+ // MyScreen logic is based on the showTopAppBar boolean flag.
MyScreen(
showTopAppBar = showTopAppBar,
/* ... */
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/InsetsSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/InsetsSnippets.kt
index 4599fd392..77568686d 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/InsetsSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/InsetsSnippets.kt
@@ -22,7 +22,6 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
-import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
@@ -30,8 +29,6 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.consumeWindowInsets
-import androidx.compose.foundation.layout.fillMaxWidth
-import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.only
@@ -49,7 +46,6 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
-import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@@ -124,7 +120,6 @@ fun ConsumedFromPaddingSnippet() {
@Preview
@Composable
fun M3SupportScaffoldSnippet() {
- val colors = listOf(Color.Red, Color.Blue, Color.Yellow)
// [START android_compose_insets_m3_scaffold]
Scaffold { innerPadding ->
// innerPadding contains inset information for you to use and apply
@@ -133,14 +128,7 @@ fun M3SupportScaffoldSnippet() {
modifier = Modifier.consumeWindowInsets(innerPadding),
contentPadding = innerPadding
) {
- items(count = 100) {
- Box(
- Modifier
- .fillMaxWidth()
- .height(50.dp)
- .background(colors[it % colors.size])
- )
- }
+ // ..
}
}
// [END android_compose_insets_m3_scaffold]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt
index 9bc39b292..edca298d7 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt
@@ -39,10 +39,10 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
-import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
+import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -71,7 +71,7 @@ private object IntrinsicsSnippet1 {
.wrapContentWidth(Alignment.Start),
text = text1
)
- HorizontalDivider(
+ VerticalDivider(
color = Color.Black,
modifier = Modifier.fillMaxHeight().width(1.dp)
)
@@ -111,7 +111,7 @@ private object IntrinsicsSnippet2 {
.wrapContentWidth(Alignment.Start),
text = text1
)
- HorizontalDivider(
+ VerticalDivider(
color = Color.Black,
modifier = Modifier.fillMaxHeight().width(1.dp)
)
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/layouts/SystemBarProtectionSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/SystemBarProtectionSnippets.kt
new file mode 100644
index 000000000..429e7ffd1
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/layouts/SystemBarProtectionSnippets.kt
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.layouts
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.enableEdgeToEdge
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.WindowInsets
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.statusBars
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.material3.Card
+import androidx.compose.material3.CardDefaults
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Scaffold
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.geometry.Size
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import com.example.compose.snippets.designsystems.MyTheme
+
+// [START android_compose_system_bar_protection]
+class SystemBarProtectionSnippets : ComponentActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // enableEdgeToEdge sets window.isNavigationBarContrastEnforced = true
+ // which is used to add a translucent scrim to three-button navigation
+ enableEdgeToEdge()
+
+ setContent {
+ MyTheme {
+ // Main content
+ MyContent()
+
+ // After drawing main content, draw status bar protection
+ StatusBarProtection()
+ }
+ }
+ }
+}
+
+@Composable
+private fun StatusBarProtection(
+ color: Color = MaterialTheme.colorScheme.surfaceContainer,
+ heightProvider: () -> Float = calculateGradientHeight(),
+) {
+
+ Canvas(Modifier.fillMaxSize()) {
+ val calculatedHeight = heightProvider()
+ val gradient = Brush.verticalGradient(
+ colors = listOf(
+ color.copy(alpha = 1f),
+ color.copy(alpha = .8f),
+ Color.Transparent
+ ),
+ startY = 0f,
+ endY = calculatedHeight
+ )
+ drawRect(
+ brush = gradient,
+ size = Size(size.width, calculatedHeight),
+ )
+ }
+}
+
+@Composable
+fun calculateGradientHeight(): () -> Float {
+ val statusBars = WindowInsets.statusBars
+ val density = LocalDensity.current
+ return { statusBars.getTop(density).times(1.2f) }
+}
+// [END android_compose_system_bar_protection]
+
+@Composable
+fun MyContent() {
+ Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
+ val colorScheme = MaterialTheme.colorScheme
+ val loremIpsum = LoremIpsum()
+
+ LazyColumn(
+ contentPadding = innerPadding
+ ) {
+ items(13) { index ->
+ Card(
+ modifier = Modifier.fillMaxWidth().padding(8.dp),
+ colors = CardDefaults.cardColors(
+ containerColor = colorScheme.surfaceVariant
+ )
+ ) {
+ Column(modifier = Modifier.padding(8.dp)) {
+ Text(
+ text = loremIpsum.titles[index],
+ style = TextStyle(
+ fontWeight = FontWeight.Bold,
+ fontSize = 18.sp,
+ color = colorScheme.onSurfaceVariant
+ )
+ )
+ Spacer(modifier = Modifier.height(4.dp))
+ Text(
+ text = loremIpsum.descriptions[index],
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ style = TextStyle(
+ color = colorScheme.onSurfaceVariant
+ )
+ )
+ }
+ }
+ }
+ }
+ }
+}
+
+class LoremIpsum {
+ private val lorem = "First item of the list." +
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." +
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." +
+ "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." +
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +
+ "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo." +
+ "Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt." +
+ "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem." +
+ "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" +
+ "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga." +
+ "Et harum quidem rerum facilis est et expedita distinctio." +
+ "Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus." +
+ "Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae." +
+ "Last item of the list."
+ val descriptions = lorem.split(".").map { description ->
+ description.trim()
+ }
+ val titles = descriptions.map { sentence ->
+ sentence.trim().split(" ").take(2).joinToString(" ")
+ }
+}
+
+@Preview
+@Composable
+fun MyContentPreview() {
+ MyTheme {
+ MyContent()
+ }
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/modifiers/CustomModifierSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/modifiers/CustomModifierSnippets.kt
index 7b778ef25..650c4beb8 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/modifiers/CustomModifierSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/modifiers/CustomModifierSnippets.kt
@@ -18,6 +18,7 @@ package com.example.compose.snippets.modifiers
import android.annotation.SuppressLint
import androidx.compose.animation.core.Animatable
+import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
@@ -259,7 +260,7 @@ class ScrollableNode :
object CustomModifierSnippets14 {
// [START android_compose_custom_modifiers_14]
class CircleNode(var color: Color) : Modifier.Node(), DrawModifierNode {
- private val alpha = Animatable(1f)
+ private lateinit var alpha: Animatable
override fun ContentDrawScope.draw() {
drawCircle(color = color, alpha = alpha.value)
@@ -267,6 +268,7 @@ object CustomModifierSnippets14 {
}
override fun onAttach() {
+ alpha = Animatable(1f)
coroutineScope.launch {
alpha.animateTo(
0f,
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt
index 783963906..8d86b28b4 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt
@@ -49,5 +49,7 @@ enum class TopComponentsDestination(val route: String, val title: String) {
MenusExample("menusExamples", "Menus"),
TooltipExamples("tooltipExamples", "Tooltips"),
NavigationDrawerExamples("navigationDrawerExamples", "Navigation drawer"),
- SegmentedButtonExamples("segmentedButtonExamples", "Segmented button")
+ SegmentedButtonExamples("segmentedButtonExamples", "Segmented button"),
+ SwipeToDismissBoxExamples("swipeToDismissBoxExamples", "Swipe to dismiss box examples"),
+ SearchBarExamples("searchBarExamples", "Search bar")
}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.kt
index 88ec5c4a1..f9999f67c 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.kt
@@ -37,6 +37,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TransformOrigin
+import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
@@ -46,6 +47,10 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.flow.Flow
+import kotlinx.serialization.Serializable
+
+@Serializable data object Home
+@Serializable data object Settings
@Composable
private fun PredictiveBackOverrideExit(
@@ -56,7 +61,7 @@ private fun PredictiveBackOverrideExit(
// [START android_compose_predictiveback_navhost]
NavHost(
navController = navController,
- startDestination = "home",
+ startDestination = Home,
popExitTransition = {
scaleOut(
targetScale = 0.9f,
@@ -70,13 +75,13 @@ private fun PredictiveBackOverrideExit(
)
// [END android_compose_predictiveback_navhost]
{
- composable("home") {
+ composable {
HomeScreen(
modifier = modifier,
navController = navController,
)
}
- composable("settings") {
+ composable {
SettingsScreen(
modifier = modifier,
navController = navController,
@@ -106,7 +111,10 @@ private fun PredictiveBackHandlerBasicExample() {
Box(
modifier = Modifier
- .fillMaxSize(boxScale)
+ .graphicsLayer {
+ scaleX = boxScale
+ scaleY = scaleX
+ }
.background(Color.Blue)
)
@@ -119,9 +127,11 @@ private fun PredictiveBackHandlerBasicExample() {
boxScale = 1F - (1F * backEvent.progress)
}
// code for completion
+ boxScale = 0F
} catch (e: CancellationException) {
// code for cancellation
boxScale = 1F
+ throw e
}
}
// [END android_compose_predictivebackhandler_basic]
@@ -175,8 +185,10 @@ private fun PredictiveBackHandlerManualProgress() {
closeDrawer(velocityTracker.calculateVelocity().x)
} catch (e: CancellationException) {
openDrawer(velocityTracker.calculateVelocity().x)
+ throw e
+ } finally {
+ velocityTracker.resetTracking()
}
- velocityTracker.resetTracking()
}
// [END android_compose_predictivebackhandler_manualprogress]
}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/stylus/StylusSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/stylus/StylusSnippets.kt
new file mode 100644
index 000000000..82c785984
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/stylus/StylusSnippets.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@file:Suppress("unused")
+
+package com.example.compose.snippets.stylus
+
+import android.view.MotionEvent
+import androidx.compose.foundation.Canvas
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.ExperimentalComposeUiApi
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clipToBounds
+import androidx.compose.ui.input.pointer.pointerInteropFilter
+import androidx.lifecycle.ViewModel
+
+class UserViewModel : ViewModel() {
+ fun processMotionEvent(e: MotionEvent): Boolean {
+ return true
+ }
+}
+
+val viewModel = UserViewModel()
+
+// [START android_compose_stylus_motion_event_access]
+@Composable
+@OptIn(ExperimentalComposeUiApi::class)
+fun DrawArea(modifier: Modifier = Modifier) {
+ Canvas(
+ modifier = modifier
+ .clipToBounds()
+ .pointerInteropFilter {
+ viewModel.processMotionEvent(it)
+ }
+
+ ) {
+ // Drawing code here.
+ }
+}
+// [END android_compose_stylus_motion_event_access]
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/text/AutofillSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/text/AutofillSnippets.kt
new file mode 100644
index 000000000..6e30ba93b
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/text/AutofillSnippets.kt
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.text
+
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.text.LocalAutofillHighlightColor
+import androidx.compose.foundation.text.input.rememberTextFieldState
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material.TextField
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.autofill.ContentType
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.platform.LocalAutofillManager
+import androidx.compose.ui.semantics.contentType
+import androidx.compose.ui.semantics.semantics
+import androidx.compose.ui.text.input.TextFieldValue
+import androidx.compose.ui.unit.dp
+import com.example.compose.snippets.touchinput.Button
+
+@Composable
+fun AddAutofill() {
+ // [START android_compose_autofill_1]
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.Username }
+ )
+ // [END android_compose_autofill_1]
+}
+
+@Composable
+fun AddMultipleTypesOfAutofill() {
+ // [START android_compose_autofill_2]
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics {
+ contentType = ContentType.Username + ContentType.EmailAddress
+ }
+ )
+ // [END android_compose_autofill_2]
+}
+
+@Composable
+fun AutofillManager() {
+ // [START android_compose_autofill_3]
+ val autofillManager = LocalAutofillManager.current
+ // [END android_compose_autofill_3]
+}
+
+@Composable
+fun SaveDataWithAutofill() {
+ var textFieldValue = remember {
+ mutableStateOf(TextFieldValue(""))
+ }
+ // [START android_compose_autofill_4]
+ val autofillManager = LocalAutofillManager.current
+
+ Column {
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.NewUsername }
+ )
+
+ Spacer(modifier = Modifier.height(16.dp))
+
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.NewPassword }
+ )
+ }
+ // [END android_compose_autofill_4]
+}
+
+@Composable
+fun SaveDataWithAutofillOnClick() {
+ // [START android_compose_autofill_5]
+ val autofillManager = LocalAutofillManager.current
+
+ Column {
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.NewUsername },
+ )
+
+ Spacer(modifier = Modifier.height(16.dp))
+
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.NewPassword },
+ )
+
+ // Submit button
+ Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
+ }
+ // [END android_compose_autofill_5]
+}
+
+@Composable
+fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
+ // [START android_compose_autofill_6]
+ val customHighlightColor = Color.Red
+
+ CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
+ TextField(
+ state = rememberTextFieldState(),
+ modifier = Modifier.semantics { contentType = ContentType.Username }
+ )
+ }
+ // [END android_compose_autofill_6]
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/text/FilterText.kt b/compose/snippets/src/main/java/com/example/compose/snippets/text/FilterText.kt
new file mode 100644
index 000000000..6967ea4f8
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/text/FilterText.kt
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.text
+
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.material3.ListItem
+import androidx.compose.material3.OutlinedTextField
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.compose.collectAsStateWithLifecycle
+import androidx.lifecycle.viewmodel.compose.viewModel
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+
+// [START android_compose_text_filtertextviewmodel]
+class FilterTextViewModel : ViewModel() {
+ private val items = listOf(
+ "Cupcake",
+ "Donut",
+ "Eclair",
+ "Froyo",
+ "Gingerbread",
+ "Honeycomb",
+ "Ice Cream Sandwich"
+ )
+
+ private val _filteredItems = MutableStateFlow(items)
+ var filteredItems: StateFlow> = _filteredItems
+
+ fun filterText(input: String) {
+ // This filter returns the full items list when input is an empty string.
+ _filteredItems.value = items.filter { it.contains(input, ignoreCase = true) }
+ }
+}
+// [END android_compose_text_filtertextviewmodel]
+
+// [START android_compose_text_filtertextview]
+@Composable
+fun FilterTextView(modifier: Modifier = Modifier, viewModel: FilterTextViewModel = viewModel()) {
+ val filteredItems by viewModel.filteredItems.collectAsStateWithLifecycle()
+ var text by rememberSaveable { mutableStateOf("") }
+
+ Column(
+ modifier = Modifier
+ .fillMaxSize()
+ .padding(all = 10.dp)
+ ) {
+ OutlinedTextField(
+ value = text,
+ onValueChange = {
+ text = it
+ viewModel.filterText(text)
+ },
+ label = { Text("Filter Text") },
+ modifier = Modifier.fillMaxWidth()
+ )
+
+ LazyColumn {
+ items(
+ count = filteredItems.size,
+ key = { index -> filteredItems[index] }
+ ) {
+ ListItem(
+ headlineContent = { Text(filteredItems[it]) },
+ modifier = Modifier
+ .fillParentMaxWidth()
+ .padding(10.dp)
+ )
+ }
+ }
+ }
+}
+// [END android_compose_text_filtertextview]
+
+@Preview(showBackground = true)
+@Composable
+private fun FilterTextViewPreview() {
+ FilterTextView()
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/text/StateBasedText.kt b/compose/snippets/src/main/java/com/example/compose/snippets/text/StateBasedText.kt
new file mode 100644
index 000000000..b43aef25f
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/text/StateBasedText.kt
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.text
+
+import android.text.TextUtils
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.text.KeyboardOptions
+import androidx.compose.foundation.text.input.InputTransformation
+import androidx.compose.foundation.text.input.OutputTransformation
+import androidx.compose.foundation.text.input.TextFieldBuffer
+import androidx.compose.foundation.text.input.TextFieldLineLimits
+import androidx.compose.foundation.text.input.TextFieldState
+import androidx.compose.foundation.text.input.clearText
+import androidx.compose.foundation.text.input.insert
+import androidx.compose.foundation.text.input.maxLength
+import androidx.compose.foundation.text.input.rememberTextFieldState
+import androidx.compose.foundation.text.input.selectAll
+import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
+import androidx.compose.foundation.text.input.then
+import androidx.compose.material.OutlinedTextField
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material.TextField
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.text.input.ImeAction
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.core.text.isDigitsOnly
+import androidx.lifecycle.ViewModel
+
+@Preview
+@Composable
+fun StateBasedTextSnippets() {
+ Column() {
+ // [START android_compose_state_text_0]
+ TextField(
+ state = rememberTextFieldState(initialText = "Hello"),
+ label = { Text("Label") }
+ )
+ // [END android_compose_state_text_0]
+
+ // [START android_compose_state_text_1]
+ OutlinedTextField(
+ state = rememberTextFieldState(),
+ label = { Text("Label") }
+ )
+ // [END android_compose_state_text_1]
+ }
+}
+
+@Preview
+@Composable
+fun StyleTextField() {
+ // [START android_compose_state_text_2]
+ TextField(
+ state = rememberTextFieldState("Hello\nWorld\nInvisible"),
+ lineLimits = TextFieldLineLimits.MultiLine(maxHeightInLines = 2),
+ placeholder = { Text("") },
+ textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
+ label = { Text("Enter text") },
+ modifier = Modifier.padding(20.dp)
+ )
+ // [END android_compose_state_text_2]
+}
+
+@Composable
+fun ConfigureLineLimits() {
+ // [START android_compose_state_text_3]
+ TextField(
+ state = rememberTextFieldState(),
+ lineLimits = TextFieldLineLimits.SingleLine
+ )
+ // [END android_compose_state_text_3]
+}
+
+@Preview
+@Composable
+fun Multiline() {
+ Spacer(modifier = Modifier.height(15.dp))
+ // [START android_compose_state_text_4]
+ TextField(
+ state = rememberTextFieldState("Hello\nWorld\nHello\nWorld"),
+ lineLimits = TextFieldLineLimits.MultiLine(1, 4)
+ )
+ // [END android_compose_state_text_4]
+}
+
+@Composable
+fun StyleWithBrush() {
+ // [START android_compose_state_text_5]
+ val brush = remember {
+ Brush.linearGradient(
+ colors = listOf(Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Magenta)
+ )
+ }
+ TextField(
+ state = rememberTextFieldState(), textStyle = TextStyle(brush = brush)
+ )
+ // [END android_compose_state_text_5]
+}
+
+@Composable
+fun StateHoisting() {
+ // [START android_compose_state_text_6]
+ val usernameState = rememberTextFieldState()
+ TextField(
+ state = usernameState,
+ lineLimits = TextFieldLineLimits.SingleLine,
+ placeholder = { Text("Enter Username") }
+ )
+ // [END android_compose_state_text_6]
+}
+
+@Composable
+fun TextFieldInitialState() {
+ // [START android_compose_state_text_7]
+ TextField(
+ state = rememberTextFieldState(initialText = "Username"),
+ lineLimits = TextFieldLineLimits.SingleLine,
+ )
+ // [END android_compose_state_text_7]
+}
+
+@Composable
+fun TextFieldBuffer() {
+ // [START android_compose_state_text_8]
+ val phoneNumberState = rememberTextFieldState()
+
+ LaunchedEffect(phoneNumberState) {
+ phoneNumberState.edit { // TextFieldBuffer scope
+ append("123456789")
+ }
+ }
+
+ TextField(
+ state = phoneNumberState,
+ inputTransformation = InputTransformation { // TextFieldBuffer scope
+ if (asCharSequence().isDigitsOnly()) {
+ revertAllChanges()
+ }
+ },
+ outputTransformation = OutputTransformation {
+ if (length > 0) insert(0, "(")
+ if (length > 4) insert(4, ")")
+ if (length > 8) insert(8, "-")
+ }
+ )
+ // [END android_compose_state_text_8]
+}
+
+@Preview
+@Composable
+fun EditTextFieldState() {
+ // [START android_compose_state_text_9]
+ val usernameState = rememberTextFieldState("I love Android")
+ // textFieldState.text : I love Android
+ // textFieldState.selection: TextRange(14, 14)
+ usernameState.edit { insert(14, "!") }
+ // textFieldState.text : I love Android!
+ // textFieldState.selection: TextRange(15, 15)
+ usernameState.edit { replace(7, 14, "Compose") }
+ // textFieldState.text : I love Compose!
+ // textFieldState.selection: TextRange(15, 15)
+ usernameState.edit { append("!!!") }
+ // textFieldState.text : I love Compose!!!!
+ // textFieldState.selection: TextRange(18, 18)
+ usernameState.edit { selectAll() }
+ // textFieldState.text : I love Compose!!!!
+ // textFieldState.selection: TextRange(0, 18)
+ // [END android_compose_state_text_9]
+
+ // [START android_compose_state_text_10]
+ usernameState.setTextAndPlaceCursorAtEnd("I really love Android")
+ // textFieldState.text : I really love Android
+ // textFieldState.selection : TextRange(21, 21)
+ // [END android_compose_state_text_10]
+
+ // [START android_compose_state_text_11]
+ usernameState.clearText()
+ // textFieldState.text :
+ // textFieldState.selection : TextRange(0, 0)
+ // [END android_compose_state_text_11]
+}
+
+class TextFieldViewModel : ViewModel() {
+ val usernameState = TextFieldState()
+ fun validateUsername() {
+ }
+}
+val textFieldViewModel = TextFieldViewModel()
+
+@Composable
+fun TextFieldKeyboardOptions() {
+ // [START android_compose_state_text_13]
+ TextField(
+ state = textFieldViewModel.usernameState,
+ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
+ onKeyboardAction = { performDefaultAction ->
+ textFieldViewModel.validateUsername()
+ performDefaultAction()
+ }
+ )
+ // [END android_compose_state_text_13]
+}
+
+@Composable
+fun TextFieldInputTransformation() {
+ // [START android_compose_state_text_14]
+ TextField(
+ state = rememberTextFieldState(),
+ lineLimits = TextFieldLineLimits.SingleLine,
+ inputTransformation = InputTransformation.maxLength(10)
+ )
+ // [END android_compose_state_text_14]
+}
+
+// [START android_compose_state_text_15]
+class CustomInputTransformation : InputTransformation {
+ override fun TextFieldBuffer.transformInput() {
+ }
+}
+// [END android_compose_state_text_15]
+
+// [START android_compose_state_text_16]
+class DigitOnlyInputTransformation : InputTransformation {
+ override fun TextFieldBuffer.transformInput() {
+ if (!TextUtils.isDigitsOnly(asCharSequence())) {
+ revertAllChanges()
+ }
+ }
+}
+// [END android_compose_state_text_16]
+
+@Composable
+fun ChainInputTransformation() {
+ // [START android_compose_state_text_17]
+ TextField(
+ state = rememberTextFieldState(),
+ inputTransformation = InputTransformation.maxLength(6)
+ .then(CustomInputTransformation()),
+ )
+ // [END android_compose_state_text_17]
+}
+
+// [START android_compose_state_text_18]
+class CustomOutputTransformation : OutputTransformation {
+ override fun TextFieldBuffer.transformOutput() {
+ }
+}
+// [END android_compose_state_text_18]
+
+// [START android_compose_state_text_19]
+class PhoneNumberOutputTransformation : OutputTransformation {
+ override fun TextFieldBuffer.transformOutput() {
+ if (length > 0) insert(0, "(")
+ if (length > 4) insert(4, ")")
+ if (length > 8) insert(8, "-")
+ }
+}
+// [END android_compose_state_text_19]
+
+@Composable
+fun TextFieldOutputTransformation() {
+ // [START android_compose_state_text_20]
+ TextField(
+ state = rememberTextFieldState(),
+ outputTransformation = PhoneNumberOutputTransformation()
+ )
+ // [END android_compose_state_text_20]
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/text/TextFieldMigrationSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/text/TextFieldMigrationSnippets.kt
new file mode 100644
index 000000000..a9e970266
--- /dev/null
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/text/TextFieldMigrationSnippets.kt
@@ -0,0 +1,345 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.compose.snippets.text
+
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.text.input.InputTransformation
+import androidx.compose.foundation.text.input.OutputTransformation
+import androidx.compose.foundation.text.input.TextFieldLineLimits
+import androidx.compose.foundation.text.input.TextFieldState
+import androidx.compose.foundation.text.input.delete
+import androidx.compose.foundation.text.input.insert
+import androidx.compose.foundation.text.input.maxLength
+import androidx.compose.foundation.text.input.rememberTextFieldState
+import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material.SecureTextField
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material.Text
+//noinspection UsingMaterialAndMaterial3Libraries
+import androidx.compose.material.TextField
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.runtime.snapshotFlow
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.text.AnnotatedString
+import androidx.compose.ui.text.TextRange
+import androidx.compose.ui.text.input.OffsetMapping
+import androidx.compose.ui.text.input.PasswordVisualTransformation
+import androidx.compose.ui.text.input.TextFieldValue
+import androidx.compose.ui.text.input.TransformedText
+import androidx.compose.ui.text.input.VisualTransformation
+import androidx.compose.ui.text.substring
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.compose.collectAsStateWithLifecycle
+import com.example.compose.snippets.touchinput.Button
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.collectLatest
+import kotlinx.coroutines.flow.update
+
+// [START android_compose_text_textfield_migration_old_simple]
+@Composable
+fun OldSimpleTextField() {
+ var state by rememberSaveable { mutableStateOf("") }
+ TextField(
+ value = state,
+ onValueChange = { state = it },
+ singleLine = true,
+ )
+}
+// [END android_compose_text_textfield_migration_old_simple]
+
+// [START android_compose_text_textfield_migration_new_simple]
+@Composable
+fun NewSimpleTextField() {
+ TextField(
+ state = rememberTextFieldState(),
+ lineLimits = TextFieldLineLimits.SingleLine
+ )
+}
+// [END android_compose_text_textfield_migration_new_simple]
+
+// [START android_compose_text_textfield_migration_old_filtering]
+@Composable
+fun OldNoLeadingZeroes() {
+ var input by rememberSaveable { mutableStateOf("") }
+ TextField(
+ value = input,
+ onValueChange = { newText ->
+ input = newText.trimStart { it == '0' }
+ }
+ )
+}
+// [END android_compose_text_textfield_migration_old_filtering]
+
+// [START android_compose_text_textfield_migration_new_filtering]
+
+@Preview
+@Composable
+fun NewNoLeadingZeros() {
+ TextField(
+ state = rememberTextFieldState(),
+ inputTransformation = InputTransformation {
+ while (length > 0 && charAt(0) == '0') delete(0, 1)
+ }
+ )
+}
+// [END android_compose_text_textfield_migration_new_filtering]
+
+// [START android_compose_text_textfield_migration_old_credit_card_formatter]
+@Composable
+fun OldTextFieldCreditCardFormatter() {
+ var state by remember { mutableStateOf("") }
+ TextField(
+ value = state,
+ onValueChange = { if (it.length <= 16) state = it },
+ visualTransformation = VisualTransformation { text ->
+ // Making XXXX-XXXX-XXXX-XXXX string.
+ var out = ""
+ for (i in text.indices) {
+ out += text[i]
+ if (i % 4 == 3 && i != 15) out += "-"
+ }
+
+ TransformedText(
+ text = AnnotatedString(out),
+ offsetMapping = object : OffsetMapping {
+ override fun originalToTransformed(offset: Int): Int {
+ if (offset <= 3) return offset
+ if (offset <= 7) return offset + 1
+ if (offset <= 11) return offset + 2
+ if (offset <= 16) return offset + 3
+ return 19
+ }
+
+ override fun transformedToOriginal(offset: Int): Int {
+ if (offset <= 4) return offset
+ if (offset <= 9) return offset - 1
+ if (offset <= 14) return offset - 2
+ if (offset <= 19) return offset - 3
+ return 16
+ }
+ }
+ )
+ }
+ )
+}
+// [END android_compose_text_textfield_migration_old_credit_card_formatter]
+
+// [START android_compose_text_textfield_migration_new_credit_card_formatter]
+@Composable
+fun NewTextFieldCreditCardFormatter() {
+ val state = rememberTextFieldState()
+ TextField(
+ state = state,
+ inputTransformation = InputTransformation.maxLength(16),
+ outputTransformation = OutputTransformation {
+ if (length > 4) insert(4, "-")
+ if (length > 9) insert(9, "-")
+ if (length > 14) insert(14, "-")
+ },
+ )
+}
+// [END android_compose_text_textfield_migration_new_credit_card_formatter]
+
+private object StateUpdateSimpleSnippet {
+ object UserRepository {
+ suspend fun fetchUsername(): String = TODO()
+ }
+ // [START android_compose_text_textfield_migration_old_update_state_simple]
+ @Composable
+ fun OldTextFieldStateUpdate(userRepository: UserRepository) {
+ var username by remember { mutableStateOf("") }
+ LaunchedEffect(Unit) {
+ username = userRepository.fetchUsername()
+ }
+ TextField(
+ value = username,
+ onValueChange = { username = it }
+ )
+ }
+ // [END android_compose_text_textfield_migration_old_update_state_simple]
+
+ // [START android_compose_text_textfield_migration_new_update_state_simple]
+ @Composable
+ fun NewTextFieldStateUpdate(userRepository: UserRepository) {
+ val usernameState = rememberTextFieldState()
+ LaunchedEffect(Unit) {
+ usernameState.setTextAndPlaceCursorAtEnd(userRepository.fetchUsername())
+ }
+ TextField(state = usernameState)
+ }
+ // [END android_compose_text_textfield_migration_new_update_state_simple]
+}
+
+// [START android_compose_text_textfield_migration_old_state_update_complex]
+@Composable
+fun OldTextFieldAddMarkdownEmphasis() {
+ var markdownState by remember { mutableStateOf(TextFieldValue()) }
+ Button(onClick = {
+ // add ** decorations around the current selection, also preserve the selection
+ markdownState = with(markdownState) {
+ copy(
+ text = buildString {
+ append(text.take(selection.min))
+ append("**")
+ append(text.substring(selection))
+ append("**")
+ append(text.drop(selection.max))
+ },
+ selection = TextRange(selection.min + 2, selection.max + 2)
+ )
+ }
+ }) {
+ Text("Bold")
+ }
+ TextField(
+ value = markdownState,
+ onValueChange = { markdownState = it },
+ maxLines = 10
+ )
+}
+// [END android_compose_text_textfield_migration_old_state_update_complex]
+
+// [START android_compose_text_textfield_migration_new_state_update_complex]
+@Composable
+fun NewTextFieldAddMarkdownEmphasis() {
+ val markdownState = rememberTextFieldState()
+ LaunchedEffect(Unit) {
+ // add ** decorations around the current selection
+ markdownState.edit {
+ insert(originalSelection.max, "**")
+ insert(originalSelection.min, "**")
+ selection = TextRange(originalSelection.min + 2, originalSelection.max + 2)
+ }
+ }
+ TextField(
+ state = markdownState,
+ lineLimits = TextFieldLineLimits.MultiLine(1, 10)
+ )
+}
+// [END android_compose_text_textfield_migration_new_state_update_complex]
+
+private object ViewModelMigrationOldSnippet {
+ // [START android_compose_text_textfield_migration_old_viewmodel]
+ class LoginViewModel : ViewModel() {
+ private val _uiState = MutableStateFlow(UiState())
+ val uiState: StateFlow
+ get() = _uiState.asStateFlow()
+
+ fun updateUsername(username: String) = _uiState.update { it.copy(username = username) }
+
+ fun updatePassword(password: String) = _uiState.update { it.copy(password = password) }
+ }
+
+ data class UiState(
+ val username: String = "",
+ val password: String = ""
+ )
+
+ @Composable
+ fun LoginForm(
+ loginViewModel: LoginViewModel,
+ modifier: Modifier = Modifier
+ ) {
+ val uiState by loginViewModel.uiState.collectAsStateWithLifecycle()
+ Column(modifier) {
+ TextField(
+ value = uiState.username,
+ onValueChange = { loginViewModel.updateUsername(it) }
+ )
+ TextField(
+ value = uiState.password,
+ onValueChange = { loginViewModel.updatePassword(it) },
+ visualTransformation = PasswordVisualTransformation()
+ )
+ }
+ }
+ // [END android_compose_text_textfield_migration_old_viewmodel]
+}
+
+private object ViewModelMigrationNewSimpleSnippet {
+ // [START android_compose_text_textfield_migration_new_viewmodel_simple]
+ class LoginViewModel : ViewModel() {
+ val usernameState = TextFieldState()
+ val passwordState = TextFieldState()
+ }
+
+ @Composable
+ fun LoginForm(
+ loginViewModel: LoginViewModel,
+ modifier: Modifier = Modifier
+ ) {
+ Column(modifier) {
+ TextField(state = loginViewModel.usernameState,)
+ SecureTextField(state = loginViewModel.passwordState)
+ }
+ }
+ // [END android_compose_text_textfield_migration_new_viewmodel_simple]
+}
+
+private object ViewModelMigrationNewConformingSnippet {
+ // [START android_compose_text_textfield_migration_new_viewmodel_conforming]
+ class LoginViewModel : ViewModel() {
+ private val _uiState = MutableStateFlow(UiState())
+ val uiState: StateFlow
+ get() = _uiState.asStateFlow()
+
+ fun updateUsername(username: String) = _uiState.update { it.copy(username = username) }
+
+ fun updatePassword(password: String) = _uiState.update { it.copy(password = password) }
+ }
+
+ data class UiState(
+ val username: String = "",
+ val password: String = ""
+ )
+
+ @Composable
+ fun LoginForm(
+ loginViewModel: LoginViewModel,
+ modifier: Modifier = Modifier
+ ) {
+ val initialUiState = remember(loginViewModel) { loginViewModel.uiState.value }
+ Column(modifier) {
+ val usernameState = rememberTextFieldState(initialUiState.username)
+ LaunchedEffect(usernameState) {
+ snapshotFlow { usernameState.text.toString() }.collectLatest {
+ loginViewModel.updateUsername(it)
+ }
+ }
+ TextField(usernameState)
+
+ val passwordState = rememberTextFieldState(initialUiState.password)
+ LaunchedEffect(usernameState) {
+ snapshotFlow { usernameState.text.toString() }.collectLatest {
+ loginViewModel.updatePassword(it)
+ }
+ }
+ SecureTextField(passwordState)
+ }
+ }
+ // [END android_compose_text_textfield_migration_new_viewmodel_conforming]
+}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt
index 82b5b2d1e..d48412c3c 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2023 The Android Open Source Project
+ * Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,20 +14,17 @@
* limitations under the License.
*/
-@file:Suppress("DEPRECATION_ERROR")
-
package com.example.compose.snippets.touchinput.focus
-import androidx.compose.foundation.Indication
-import androidx.compose.foundation.IndicationInstance
+import androidx.compose.foundation.IndicationNodeFactory
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.focusable
+import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.MutableInteractionSource
-import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@@ -42,7 +39,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -70,9 +66,13 @@ import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
+import androidx.compose.ui.node.DelegatableNode
+import androidx.compose.ui.node.DrawModifierNode
+import androidx.compose.ui.node.invalidateDraw
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.launch
@Preview
@Composable
@@ -297,7 +297,7 @@ private fun RequestFocus2() {
private fun Capture() {
var text by remember { mutableStateOf("") }
// [START android_compose_touchinput_focus_capture]
- val textField = FocusRequester()
+ val textField = remember { FocusRequester() }
TextField(
value = text,
@@ -436,45 +436,64 @@ private fun ReactToFocus() {
}
// [START android_compose_touchinput_focus_advanced_cues]
-private class MyHighlightIndicationInstance(isEnabledState: State) :
- IndicationInstance {
- private val isEnabled by isEnabledState
- override fun ContentDrawScope.drawIndication() {
+private class MyHighlightIndicationNode(private val interactionSource: InteractionSource) :
+ Modifier.Node(), DrawModifierNode {
+ private var isFocused = false
+
+ override fun onAttach() {
+ coroutineScope.launch {
+ var focusCount = 0
+ interactionSource.interactions.collect { interaction ->
+ when (interaction) {
+ is FocusInteraction.Focus -> focusCount++
+ is FocusInteraction.Unfocus -> focusCount--
+ }
+ val focused = focusCount > 0
+ if (isFocused != focused) {
+ isFocused = focused
+ invalidateDraw()
+ }
+ }
+ }
+ }
+
+ override fun ContentDrawScope.draw() {
drawContent()
- if (isEnabled) {
+ if (isFocused) {
drawRect(size = size, color = Color.White, alpha = 0.2f)
}
}
}
+
// [END android_compose_touchinput_focus_advanced_cues]
// [START android_compose_touchinput_focus_indication]
-class MyHighlightIndication : Indication {
- @Composable
- override fun rememberUpdatedInstance(interactionSource: InteractionSource):
- IndicationInstance {
- val isFocusedState = interactionSource.collectIsFocusedAsState()
- return remember(interactionSource) {
- MyHighlightIndicationInstance(isEnabledState = isFocusedState)
- }
+object MyHighlightIndication : IndicationNodeFactory {
+ override fun create(interactionSource: InteractionSource): DelegatableNode {
+ return MyHighlightIndicationNode(interactionSource)
}
+
+ override fun hashCode(): Int = -1
+
+ override fun equals(other: Any?) = other === this
}
// [END android_compose_touchinput_focus_indication]
@Composable
private fun ApplyIndication() {
// [START android_compose_touchinput_focus_apply_indication]
- val highlightIndication = remember { MyHighlightIndication() }
var interactionSource = remember { MutableInteractionSource() }
Card(
modifier = Modifier
.clickable(
interactionSource = interactionSource,
- indication = highlightIndication,
+ indication = MyHighlightIndication,
enabled = true,
onClick = { }
)
- ) {}
+ ) {
+ Text("hello")
+ }
// [END android_compose_touchinput_focus_apply_indication]
}
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/keyboardinput/KeyboardShortcutsHelper.kt b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/keyboardinput/KeyboardShortcutsHelper.kt
index a7f21362f..610143cec 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/keyboardinput/KeyboardShortcutsHelper.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/keyboardinput/KeyboardShortcutsHelper.kt
@@ -16,7 +16,6 @@
package com.example.compose.snippets.touchinput.keyboardinput
-import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
@@ -24,13 +23,13 @@ import android.view.KeyboardShortcutGroup
import android.view.KeyboardShortcutInfo
import android.view.Menu
import androidx.activity.ComponentActivity
+import androidx.activity.compose.LocalActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
-import androidx.compose.ui.platform.LocalContext
class MainActivity : ComponentActivity() {
// Activity codes such as overridden onStart method.
@@ -66,7 +65,7 @@ class AnotherActivity : ComponentActivity() {
setContent {
MaterialTheme {
// [START android_compose_keyboard_shortcuts_helper_request]
- val activity = LocalContext.current as? Activity
+ val activity = LocalActivity.current
Button(
onClick = {
diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/userinteractions/UserInteractions.kt b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/userinteractions/UserInteractions.kt
index 07248a6f9..12de9f923 100644
--- a/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/userinteractions/UserInteractions.kt
+++ b/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/userinteractions/UserInteractions.kt
@@ -30,13 +30,14 @@ import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Box
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.LocalRippleConfiguration
-import androidx.compose.material.LocalUseFallbackRippleImplementation
import androidx.compose.material.RippleConfiguration
import androidx.compose.material.ripple
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.material.ripple.rememberRipple
+import androidx.compose.material3.ExperimentalMaterial3Api
+import androidx.compose.material3.LocalUseFallbackRippleImplementation
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
@@ -239,7 +240,7 @@ private class ScaleIndicationNode(
fun App() {
}
-@OptIn(ExperimentalMaterialApi::class)
+@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun LocalUseFallbackRippleImplementationExample() {
// [START android_compose_userinteractions_localusefallbackrippleimplementation]
@@ -252,7 +253,7 @@ private fun LocalUseFallbackRippleImplementationExample() {
}
// [START android_compose_userinteractions_localusefallbackrippleimplementation_app_theme]
-@OptIn(ExperimentalMaterialApi::class)
+@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyAppTheme(content: @Composable () -> Unit) {
CompositionLocalProvider(LocalUseFallbackRippleImplementation provides true) {
diff --git a/compose/snippets/src/main/res/drawable-nodpi/cheese_1.jpg b/compose/snippets/src/main/res/drawable-nodpi/cheese_1.jpg
new file mode 100644
index 000000000..65be51c6b
Binary files /dev/null and b/compose/snippets/src/main/res/drawable-nodpi/cheese_1.jpg differ
diff --git a/compose/snippets/src/main/res/drawable-nodpi/cheese_2.jpg b/compose/snippets/src/main/res/drawable-nodpi/cheese_2.jpg
new file mode 100644
index 000000000..ae83e1ac6
Binary files /dev/null and b/compose/snippets/src/main/res/drawable-nodpi/cheese_2.jpg differ
diff --git a/compose/snippets/src/main/res/drawable-nodpi/cheese_3.jpg b/compose/snippets/src/main/res/drawable-nodpi/cheese_3.jpg
new file mode 100644
index 000000000..362ef3874
Binary files /dev/null and b/compose/snippets/src/main/res/drawable-nodpi/cheese_3.jpg differ
diff --git a/compose/snippets/src/main/res/drawable-nodpi/cheese_4.jpg b/compose/snippets/src/main/res/drawable-nodpi/cheese_4.jpg
new file mode 100644
index 000000000..052ef4c72
Binary files /dev/null and b/compose/snippets/src/main/res/drawable-nodpi/cheese_4.jpg differ
diff --git a/compose/snippets/src/main/res/drawable-nodpi/cheese_5.jpg b/compose/snippets/src/main/res/drawable-nodpi/cheese_5.jpg
new file mode 100644
index 000000000..137a7f3a1
Binary files /dev/null and b/compose/snippets/src/main/res/drawable-nodpi/cheese_5.jpg differ
diff --git a/compose/snippets/src/main/res/drawable/fast_forward.xml b/compose/snippets/src/main/res/drawable/fast_forward.xml
new file mode 100644
index 000000000..d49dffbf3
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/fast_forward.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/drawable/fast_forward_filled.xml b/compose/snippets/src/main/res/drawable/fast_forward_filled.xml
new file mode 100644
index 000000000..2986028f5
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/fast_forward_filled.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/drawable/fast_rewind.xml b/compose/snippets/src/main/res/drawable/fast_rewind.xml
new file mode 100644
index 000000000..aec6e80d9
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/fast_rewind.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/drawable/fast_rewind_filled.xml b/compose/snippets/src/main/res/drawable/fast_rewind_filled.xml
new file mode 100644
index 000000000..e9426630e
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/fast_rewind_filled.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/drawable/favorite.xml b/compose/snippets/src/main/res/drawable/favorite.xml
new file mode 100644
index 000000000..f9256d68d
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/favorite.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/drawable/favorite_filled.xml b/compose/snippets/src/main/res/drawable/favorite_filled.xml
new file mode 100644
index 000000000..1e1136d7b
--- /dev/null
+++ b/compose/snippets/src/main/res/drawable/favorite_filled.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/compose/snippets/src/main/res/values/strings.xml b/compose/snippets/src/main/res/values/strings.xml
index faf8fd472..02254e29a 100644
--- a/compose/snippets/src/main/res/values/strings.xml
+++ b/compose/snippets/src/main/res/values/strings.xml
@@ -53,4 +53,6 @@
Shopping
Profile
This is just a placeholder.
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+
\ No newline at end of file
diff --git a/gradle.properties b/gradle.properties
index 2dedea847..24e350250 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -11,7 +11,7 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Mon May 22 14:59:56 BST 2023
-org.gradle.jvmargs=-Xmx2048m
+org.gradle.jvmargs=-Xmx4g
# Turn on parallel compilation, caching and on-demand configuration
org.gradle.configureondemand=true
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 6c83d9d77..77f80c6d1 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -1,74 +1,92 @@
[versions]
accompanist = "0.36.0"
-androidGradlePlugin = "8.7.2"
-androidx-activity-compose = "1.9.3"
+activityKtx = "1.10.1"
+android-googleid = "1.1.1"
+androidGradlePlugin = "8.12.1"
+androidx-activity-compose = "1.10.1"
androidx-appcompat = "1.7.0"
-androidx-compose-bom = "2024.10.01"
+androidx-compose-bom = "2025.08.00"
androidx-compose-ui-test = "1.7.0-alpha08"
-androidx-constraintlayout = "2.2.0"
-androidx-constraintlayout-compose = "1.1.0"
-androidx-coordinator-layout = "1.2.0"
-androidx-corektx = "1.15.0"
+androidx-compose-ui-test-junit4-accessibility = "1.10.0-alpha01"
+androidx-constraintlayout = "2.2.1"
+androidx-constraintlayout-compose = "1.1.1"
+androidx-coordinator-layout = "1.3.0"
+androidx-corektx = "1.17.0"
+androidx-credentials = "1.5.0"
+androidx-credentials-play-services-auth = "1.5.0"
androidx-emoji2-views = "1.5.0"
-androidx-fragment-ktx = "1.8.5"
+androidx-fragment-ktx = "1.8.9"
androidx-glance-appwidget = "1.1.1"
-androidx-lifecycle-compose = "2.8.7"
-androidx-lifecycle-runtime-compose = "2.8.7"
-androidx-navigation = "2.8.3"
-androidx-paging = "3.3.2"
-androidx-test = "1.6.1"
-androidx-test-espresso = "3.6.1"
-androidx-window = "1.3.0"
+androidx-lifecycle-compose = "2.9.2"
+androidx-lifecycle-runtime-compose = "2.9.2"
+androidx-navigation = "2.9.3"
+androidx-paging = "3.3.6"
+androidx-startup-runtime = "1.2.0"
+androidx-test = "1.7.0"
+androidx-test-espresso = "3.7.0"
+androidx-test-junit = "1.3.0"
+androidx-window = "1.5.0-beta02"
+androidx-window-core = "1.5.0-beta02"
+androidx-window-java = "1.5.0-beta02"
+androidx-xr-arcore = "1.0.0-alpha05"
+androidx-xr-compose = "1.0.0-alpha06"
+androidx-xr-scenecore = "1.0.0-alpha06"
androidxHiltNavigationCompose = "1.2.0"
+appcompat = "1.7.1"
coil = "2.7.0"
# @keep
-compileSdk = "35"
-compose-latest = "1.7.5"
-composeUiTooling = "1.4.0"
+compileSdk = "36"
+compose-latest = "1.9.0"
+composeUiTooling = "1.4.1"
coreSplashscreen = "1.0.1"
-coroutines = "1.9.0"
+coroutines = "1.10.2"
glide = "1.0.0-beta01"
-google-maps = "19.0.0"
-gradle-versions = "0.51.0"
-guava = "33.2.1-android"
-hilt = "2.52"
-horologist = "0.6.20"
+google-maps = "19.2.0"
+gradle-versions = "0.52.0"
+guava = "33.4.8-jre"
+hilt = "2.57"
+horologist = "0.8.1-alpha"
junit = "4.13.2"
-kotlin = "2.0.21"
-kotlinxSerializationJson = "1.7.3"
-ksp = "2.0.21-1.0.26"
-maps-compose = "6.2.0"
-material = "1.13.0-alpha07"
-material3-adaptive = "1.0.0"
-material3-adaptive-navigation-suite = "1.3.1"
-media3 = "1.4.1"
+kotlin = "2.2.10"
+kotlinCoroutinesOkhttp = "1.0"
+kotlinxCoroutinesGuava = "1.10.2"
+kotlinxSerializationJson = "1.9.0"
+ksp = "2.2.10-2.0.2"
+lifecycleService = "2.9.2"
+maps-compose = "6.7.2"
+material = "1.14.0-alpha03"
+material3-adaptive = "1.1.0"
+material3-adaptive-navigation-suite = "1.3.2"
+media3 = "1.8.0"
# @keep
-minSdk = "21"
-playServicesWearable = "18.2.0"
-protolayout = "1.3.0-alpha04"
-protolayoutExpression = "1.3.0-alpha04"
-protolayoutMaterial = "1.3.0-alpha04"
-recyclerview = "1.3.2"
-# @keep
-targetSdk = "34"
-tiles = "1.5.0-alpha04"
-tilesRenderer = "1.5.0-alpha04"
-tilesTesting = "1.5.0-alpha04"
-tilesTooling = "1.5.0-alpha04"
-tilesToolingPreview = "1.5.0-alpha04"
-version-catalog-update = "0.8.5"
+minSdk = "35"
+okHttp = "5.1.0"
+playServicesWearable = "19.0.0"
+protolayout = "1.3.0"
+recyclerview = "1.4.0"
+targetSdk = "35"
+tiles = "1.5.0"
+tracing = "1.3.0"
+validatorPush = "1.0.0-alpha06"
+version-catalog-update = "1.0.0"
+watchfaceComplicationsDataSourceKtx = "1.2.1"
wear = "1.3.0"
-wearComposeFoundation = "1.4.0"
-wearComposeMaterial = "1.4.0"
+wearComposeFoundation = "1.5.0-rc02"
+wearComposeMaterial = "1.5.0-rc02"
+wearComposeMaterial3 = "1.5.0-rc02"
+wearOngoing = "1.0.0"
wearToolingPreview = "1.0.0"
+webkit = "1.14.0"
[libraries]
-accompanist-adaptive = { module = "com.google.accompanist:accompanist-adaptive", version.ref = "accompanist" }
-accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanist" }
+accompanist-adaptive = "com.google.accompanist:accompanist-adaptive:0.37.3"
+accompanist-permissions = "com.google.accompanist:accompanist-permissions:0.37.3"
accompanist-theme-adapter-appcompat = { module = "com.google.accompanist:accompanist-themeadapter-appcompat", version.ref = "accompanist" }
accompanist-theme-adapter-material = { module = "com.google.accompanist:accompanist-themeadapter-material", version.ref = "accompanist" }
accompanist-theme-adapter-material3 = { module = "com.google.accompanist:accompanist-themeadapter-material3", version.ref = "accompanist" }
+android-identity-googleid = { module = "com.google.android.libraries.identity.googleid:googleid", version.ref = "android-googleid" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
+androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activityKtx" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
androidx-compose-animation-graphics = { module = "androidx.compose.animation:animation-graphics", version.ref = "compose-latest" }
androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "androidx-compose-bom" }
@@ -87,8 +105,9 @@ androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose-latest" }
androidx-compose-ui-googlefonts = { module = "androidx.compose.ui:ui-text-google-fonts" }
androidx-compose-ui-graphics = { module = "androidx.compose.ui:ui-graphics" }
-androidx-compose-ui-test = { module = "androidx.compose.ui:ui-test" }
-androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" }
+androidx-compose-ui-test = { module = "androidx.compose.ui:ui-test", version.ref = "compose-latest" }
+androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose-latest" }
+androidx-compose-ui-test-junit4-accessibility = { module = "androidx.compose.ui:ui-test-junit4-accessibility", version.ref = "androidx-compose-ui-test-junit4-accessibility" }
androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
@@ -99,6 +118,8 @@ androidx-constraintlayout-compose = { module = "androidx.constraintlayout:constr
androidx-coordinator-layout = { module = "androidx.coordinatorlayout:coordinatorlayout", version.ref = "androidx-coordinator-layout" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-corektx" }
androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "coreSplashscreen" }
+androidx-credentials = { module = "androidx.credentials:credentials", version.ref = "androidx-credentials" }
+androidx-credentials-play-services-auth = { module = "androidx.credentials:credentials-play-services-auth", version.ref = "androidx-credentials-play-services-auth" }
androidx-emoji2-views = { module = "androidx.emoji2:emoji2-views", version.ref = "androidx-emoji2-views" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment-ktx" }
androidx-glance-appwidget = { module = "androidx.glance:glance-appwidget", version.ref = "androidx-glance-appwidget" }
@@ -107,6 +128,7 @@ androidx-graphics-shapes = "androidx.graphics:graphics-shapes:1.0.1"
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "androidxHiltNavigationCompose" }
androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle-runtime-compose" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle-runtime-compose" }
+androidx-lifecycle-service = { module = "androidx.lifecycle:lifecycle-service", version.ref = "lifecycleService" }
androidx-lifecycle-viewModelCompose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle-compose" }
androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle-compose" }
androidx-material-icons-core = { module = "androidx.compose.material:material-icons-core" }
@@ -115,24 +137,36 @@ androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", versi
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" }
androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "androidx-paging" }
androidx-protolayout = { module = "androidx.wear.protolayout:protolayout", version.ref = "protolayout" }
-androidx-protolayout-expression = { module = "androidx.wear.protolayout:protolayout-expression", version.ref = "protolayoutExpression" }
-androidx-protolayout-material = { module = "androidx.wear.protolayout:protolayout-material", version.ref = "protolayoutMaterial" }
+androidx-protolayout-expression = { module = "androidx.wear.protolayout:protolayout-expression", version.ref = "protolayout" }
+androidx-protolayout-material = { module = "androidx.wear.protolayout:protolayout-material", version.ref = "protolayout" }
+androidx-protolayout-material3 = { module = "androidx.wear.protolayout:protolayout-material3", version.ref = "protolayout" }
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
+androidx-startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "androidx-startup-runtime" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
-androidx-test-runner = "androidx.test:runner:1.6.2"
+androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-junit" }
+androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test" }
androidx-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "tiles" }
-androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", version.ref = "tilesRenderer" }
-androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tilesTesting" }
-androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tilesTooling" }
-androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tilesToolingPreview" }
+androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", version.ref = "tiles" }
+androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" }
+androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tiles" }
+androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tiles" }
+androidx-tracing = { module = "androidx.tracing:tracing", version.ref = "tracing" }
+androidx-watchface-complications-data-source-ktx = { module = "androidx.wear.watchface:watchface-complications-data-source-ktx", version.ref = "watchfaceComplicationsDataSourceKtx" }
androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" }
+androidx-wear-ongoing = { module = "androidx.wear:wear-ongoing", version.ref = "wearOngoing" }
androidx-wear-tooling-preview = { module = "androidx.wear:wear-tooling-preview", version.ref = "wearToolingPreview" }
-androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window" }
-androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.10.0"
+androidx-webkit = { module = "androidx.webkit:webkit", version.ref = "webkit" }
+androidx-window = { module = "androidx.window:window", version.ref = "androidx-window" }
+androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window-core" }
+androidx-window-java = { module = "androidx.window:window-java", version.ref = "androidx-window-java" }
+androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.10.3"
+androidx-xr-arcore = { module = "androidx.xr.arcore:arcore", version.ref = "androidx-xr-arcore" }
+androidx-xr-compose = { module = "androidx.xr.compose:compose", version.ref = "androidx-xr-compose" }
+androidx-xr-scenecore = { module = "androidx.xr.scenecore:scenecore", version.ref = "androidx-xr-scenecore" }
+appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
compose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "wearComposeFoundation" }
-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearComposeMaterial" }
compose-ui-tooling = { module = "androidx.wear.compose:compose-ui-tooling", version.ref = "composeUiTooling" }
glide-compose = { module = "com.github.bumptech.glide:compose", version.ref = "glide" }
google-android-material = { module = "com.google.android.material:material", version.ref = "material" }
@@ -144,11 +178,17 @@ hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.re
horologist-compose-layout = { module = "com.google.android.horologist:horologist-compose-layout", version.ref = "horologist" }
horologist-compose-material = { module = "com.google.android.horologist:horologist-compose-material", version.ref = "horologist" }
junit = { module = "junit:junit", version.ref = "junit" }
+kotlin-coroutines-okhttp = { module = "ru.gildor.coroutines:kotlin-coroutines-okhttp", version.ref = "kotlinCoroutinesOkhttp" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
+kotlinx-coroutines-guava = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-guava", version.ref = "kotlinxCoroutinesGuava" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
+okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okHttp" }
play-services-wearable = { module = "com.google.android.gms:play-services-wearable", version.ref = "playServicesWearable" }
+validator-push = { module = "com.google.android.wearable.watchface.validator:validator-push", version.ref = "validatorPush" }
+wear-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearComposeMaterial" }
+wear-compose-material3 = { module = "androidx.wear.compose:compose-material3", version.ref = "wearComposeMaterial3" }
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
@@ -156,7 +196,7 @@ android-library = { id = "com.android.library", version.ref = "androidGradlePlug
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
gradle-versions = { id = "com.github.ben-manes.versions", version.ref = "gradle-versions" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
+kotlin-android = "org.jetbrains.kotlin.android:2.2.10"
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 0aaefbcaf..37f853b1c 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
diff --git a/gradlew b/gradlew
index 1aa94a426..983fb2e97 100755
--- a/gradlew
+++ b/gradlew
@@ -205,7 +205,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
-# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
+# * For validator: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
diff --git a/identity/credentialmanager/.gitignore b/identity/credentialmanager/.gitignore
new file mode 100644
index 000000000..42afabfd2
--- /dev/null
+++ b/identity/credentialmanager/.gitignore
@@ -0,0 +1 @@
+/build
\ No newline at end of file
diff --git a/identity/credentialmanager/build.gradle.kts b/identity/credentialmanager/build.gradle.kts
new file mode 100644
index 000000000..2e35608a7
--- /dev/null
+++ b/identity/credentialmanager/build.gradle.kts
@@ -0,0 +1,83 @@
+plugins {
+ alias(libs.plugins.android.application)
+ // [START android_identity_fido2_migration_dependency]
+ alias(libs.plugins.kotlin.android)
+ // [END android_identity_fido2_migration_dependency]
+ alias(libs.plugins.compose.compiler)
+}
+
+android {
+ namespace = "com.example.identity.credentialmanager"
+ compileSdk = libs.versions.compileSdk.get().toInt()
+
+ defaultConfig {
+ applicationId = "com.example.identity.credentialmanager"
+ minSdk = libs.versions.minSdk.get().toInt()
+ targetSdk = libs.versions.targetSdk.get().toInt()
+ versionCode = 1
+ versionName = "1.0"
+
+ testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
+ }
+
+ buildTypes {
+ release {
+ isMinifyEnabled = false
+ proguardFiles(
+ getDefaultProguardFile("proguard-android-optimize.txt"),
+ "proguard-rules.pro"
+ )
+ }
+ }
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_11
+ targetCompatibility = JavaVersion.VERSION_11
+ }
+ kotlinOptions {
+ jvmTarget = "11"
+ }
+ buildFeatures {
+ compose = true
+ }
+ sourceSets {
+ named("main") {
+ java {
+ srcDir("src/main/java")
+ }
+ }
+ }
+}
+
+dependencies {
+ implementation(libs.androidx.core.ktx)
+ implementation(libs.androidx.lifecycle.runtime)
+ implementation(libs.androidx.activity.compose)
+ implementation(platform(libs.androidx.compose.bom))
+ implementation(libs.androidx.compose.ui)
+ implementation(libs.androidx.compose.ui.graphics)
+ implementation(libs.androidx.compose.ui.tooling.preview)
+ implementation(libs.androidx.compose.material3)
+
+ // [START android_identity_credman_dependency]
+ implementation(libs.androidx.credentials)
+ // [END android_identity_credman_dependency]
+
+ // [START android_identity_gradle_dependencies]
+ implementation(libs.androidx.credentials)
+
+ // optional - needed for credentials support from play services, for devices running
+ // Android 13 and below.
+ implementation(libs.androidx.credentials.play.services.auth)
+ // [END android_identity_gradle_dependencies]
+ // [START android_identity_siwg_gradle_dependencies]
+ implementation(libs.androidx.credentials)
+ implementation(libs.androidx.credentials.play.services.auth)
+ implementation(libs.android.identity.googleid)
+ // [END android_identity_siwg_gradle_dependencies]
+ implementation(libs.okhttp)
+ implementation(libs.kotlin.coroutines.okhttp)
+ implementation(libs.androidx.webkit)
+ implementation(libs.appcompat)
+ debugImplementation(libs.androidx.compose.ui.tooling)
+ debugImplementation(libs.androidx.compose.ui.test.manifest)
+}
diff --git a/identity/credentialmanager/proguard-rules.pro b/identity/credentialmanager/proguard-rules.pro
new file mode 100644
index 000000000..6075f01e2
--- /dev/null
+++ b/identity/credentialmanager/proguard-rules.pro
@@ -0,0 +1,28 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile
+
+# // [START android_identity_proguard_rules]
+-if class androidx.credentials.CredentialManager
+-keep class androidx.credentials.playservices.** {
+ *;
+}
+# // [END android_identity_proguard_rules]
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/AndroidManifest.xml b/identity/credentialmanager/src/main/AndroidManifest.xml
new file mode 100644
index 000000000..fb060cc44
--- /dev/null
+++ b/identity/credentialmanager/src/main/AndroidManifest.xml
@@ -0,0 +1,62 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/identity/credentialmanager/src/main/digitalAssetLinking/assetlinks.json b/identity/credentialmanager/src/main/digitalAssetLinking/assetlinks.json
new file mode 100644
index 000000000..34168d435
--- /dev/null
+++ b/identity/credentialmanager/src/main/digitalAssetLinking/assetlinks.json
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// [START android_identity_assetlinks_json]
+[
+ {
+ "relation" : [
+ "delegate_permission/common.handle_all_urls",
+ "delegate_permission/common.get_login_creds"
+ ],
+ "target" : {
+ "namespace" : "android_app",
+ "package_name" : "com.example.android",
+ "sha256_cert_fingerprints" : [
+ SHA_HEX_VALUE
+ ]
+ }
+ }
+]
+// [END android_identity_assetlinks_json]
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/digitalAssetLinking/othersnippets b/identity/credentialmanager/src/main/digitalAssetLinking/othersnippets
new file mode 100644
index 000000000..66ea809a8
--- /dev/null
+++ b/identity/credentialmanager/src/main/digitalAssetLinking/othersnippets
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Enable host to permit Google to retrieve the DAL
+// [START android_identity_assetlinks_allow_host]
+User-agent: *
+Allow: /.well-known/
+// [END android_identity_assetlinks_allow_host]
+
+// Manifest file addition
+// [START android_identity_assetlinks_manifest]
+
+// [END android_identity_assetlinks_manifest]
+
+// Declare association in Android app
+// [START android_identity_assetlinks_app_association]
+
+[{
+ \"include\": \"https://signin.example.com/.well-known/assetlinks.json/"
+}]
+
+// [END android_identity_assetlinks_app_association]
+
+// Example status code to test DAL
+// [START android_identity_assetlinks_curl_check]
+> GET /.well-known/assetlinks.json HTTP/1.1
+> User-Agent: curl/7.35.0
+> Host: signin.example.com
+
+< HTTP/1.1 200 OK
+< Content-Type: application/json
+// [END android_identity_assetlinks_curl_check]
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialManagerHandler.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialManagerHandler.kt
new file mode 100644
index 000000000..381bc8fc3
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialManagerHandler.kt
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.app.Activity
+import android.util.Log
+import androidx.credentials.CreatePublicKeyCredentialRequest
+import androidx.credentials.CreatePublicKeyCredentialResponse
+import androidx.credentials.CredentialManager
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.GetPublicKeyCredentialOption
+import androidx.credentials.exceptions.CreateCredentialException
+import androidx.credentials.exceptions.GetCredentialException
+
+// This class is mostly copied from https://github.com/android/identity-samples/blob/main/WebView/CredentialManagerWebView/CredentialManagerHandler.kt.
+class CredentialManagerHandler(private val activity: Activity) {
+ private val mCredMan = CredentialManager.create(activity.applicationContext)
+ private val TAG = "CredentialManagerHandler"
+ /**
+ * Encapsulates the create passkey API for credential manager in a less error-prone manner.
+ *
+ * @param request a create public key credential request JSON required by [CreatePublicKeyCredentialRequest].
+ * @return [CreatePublicKeyCredentialResponse] containing the result of the credential creation.
+ */
+ suspend fun createPasskey(request: String): CreatePublicKeyCredentialResponse {
+ val createRequest = CreatePublicKeyCredentialRequest(request)
+ try {
+ return mCredMan.createCredential(activity, createRequest) as CreatePublicKeyCredentialResponse
+ } catch (e: CreateCredentialException) {
+ // For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
+ Log.i(TAG, "Error creating credential: ErrMessage: ${e.errorMessage}, ErrType: ${e.type}")
+ throw e
+ }
+ }
+
+ /**
+ * Encapsulates the get passkey API for credential manager in a less error-prone manner.
+ *
+ * @param request a get public key credential request JSON required by [GetCredentialRequest].
+ * @return [GetCredentialResponse] containing the result of the credential retrieval.
+ */
+ suspend fun getPasskey(request: String): GetCredentialResponse {
+ val getRequest = GetCredentialRequest(listOf(GetPublicKeyCredentialOption(request, null)))
+ try {
+ return mCredMan.getCredential(activity, getRequest)
+ } catch (e: GetCredentialException) {
+ // For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
+ Log.i(TAG, "Error retrieving credential: ${e.message}")
+ throw e
+ }
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialProviderDummyActivity.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialProviderDummyActivity.kt
new file mode 100644
index 000000000..ed65bd831
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialProviderDummyActivity.kt
@@ -0,0 +1,506 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.annotation.SuppressLint
+import android.app.Activity
+import android.app.PendingIntent
+import android.content.Intent
+import android.os.Build.VERSION_CODES
+import android.os.Bundle
+import android.os.PersistableBundle
+import androidx.annotation.RequiresApi
+import androidx.biometric.BiometricManager
+import androidx.biometric.BiometricPrompt
+import androidx.biometric.BiometricPrompt.AuthenticationCallback
+import androidx.biometric.BiometricPrompt.AuthenticationResult
+import androidx.credentials.CreatePasswordRequest
+import androidx.credentials.CreatePasswordResponse
+import androidx.credentials.CreatePublicKeyCredentialRequest
+import androidx.credentials.CreatePublicKeyCredentialResponse
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.GetPasswordOption
+import androidx.credentials.GetPublicKeyCredentialOption
+import androidx.credentials.PasswordCredential
+import androidx.credentials.PublicKeyCredential
+import androidx.credentials.provider.BeginCreateCredentialRequest
+import androidx.credentials.provider.BeginCreateCredentialResponse
+import androidx.credentials.provider.BeginCreatePasswordCredentialRequest
+import androidx.credentials.provider.BeginCreatePublicKeyCredentialRequest
+import androidx.credentials.provider.CallingAppInfo
+import androidx.credentials.provider.CreateEntry
+import androidx.credentials.provider.PendingIntentHandler
+import androidx.credentials.webauthn.AuthenticatorAssertionResponse
+import androidx.credentials.webauthn.AuthenticatorAttestationResponse
+import androidx.credentials.webauthn.FidoPublicKeyCredential
+import androidx.credentials.webauthn.PublicKeyCredentialCreationOptions
+import androidx.credentials.webauthn.PublicKeyCredentialRequestOptions
+import androidx.fragment.app.FragmentActivity
+import java.math.BigInteger
+import java.security.KeyPair
+import java.security.KeyPairGenerator
+import java.security.MessageDigest
+import java.security.SecureRandom
+import java.security.Signature
+import java.security.interfaces.ECPrivateKey
+import java.security.spec.ECGenParameterSpec
+import java.security.spec.ECParameterSpec
+import java.security.spec.ECPoint
+import java.security.spec.EllipticCurve
+
+class CredentialProviderDummyActivity : FragmentActivity() {
+
+ private val PERSONAL_ACCOUNT_ID: String = ""
+ private val FAMILY_ACCOUNT_ID: String = ""
+ private val CREATE_PASSWORD_INTENT: String = ""
+
+ @RequiresApi(VERSION_CODES.M)
+ // [START android_identity_credential_provider_handle_passkey]
+ override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
+ super.onCreate(savedInstanceState, persistentState)
+ // ...
+
+ val request =
+ PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
+
+ val accountId = intent.getStringExtra(CredentialsRepo.EXTRA_KEY_ACCOUNT_ID)
+ if (request != null && request.callingRequest is CreatePublicKeyCredentialRequest) {
+ val publicKeyRequest: CreatePublicKeyCredentialRequest =
+ request.callingRequest as CreatePublicKeyCredentialRequest
+ createPasskey(
+ publicKeyRequest.requestJson,
+ request.callingAppInfo,
+ publicKeyRequest.clientDataHash,
+ accountId
+ )
+ }
+ }
+
+ @SuppressLint("RestrictedApi")
+ fun createPasskey(
+ requestJson: String,
+ callingAppInfo: CallingAppInfo?,
+ clientDataHash: ByteArray?,
+ accountId: String?
+ ) {
+ val request = PublicKeyCredentialCreationOptions(requestJson)
+
+ val biometricPrompt = BiometricPrompt(
+ this,
+ { }, // Pass in your own executor
+ object : AuthenticationCallback() {
+ override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
+ super.onAuthenticationError(errorCode, errString)
+ finish()
+ }
+
+ override fun onAuthenticationFailed() {
+ super.onAuthenticationFailed()
+ finish()
+ }
+
+ @RequiresApi(VERSION_CODES.P)
+ override fun onAuthenticationSucceeded(
+ result: AuthenticationResult
+ ) {
+ super.onAuthenticationSucceeded(result)
+
+ // Generate a credentialId
+ val credentialId = ByteArray(32)
+ SecureRandom().nextBytes(credentialId)
+
+ // Generate a credential key pair
+ val spec = ECGenParameterSpec("secp256r1")
+ val keyPairGen = KeyPairGenerator.getInstance("EC")
+ keyPairGen.initialize(spec)
+ val keyPair = keyPairGen.genKeyPair()
+
+ // Save passkey in your database as per your own implementation
+
+ // Create AuthenticatorAttestationResponse object to pass to
+ // FidoPublicKeyCredential
+
+ val response = AuthenticatorAttestationResponse(
+ requestOptions = request,
+ credentialId = credentialId,
+ credentialPublicKey = getPublicKeyFromKeyPair(keyPair),
+ origin = appInfoToOrigin(callingAppInfo!!),
+ up = true,
+ uv = true,
+ be = true,
+ bs = true,
+ packageName = callingAppInfo.packageName
+ )
+
+ val credential = FidoPublicKeyCredential(
+ rawId = credentialId,
+ response = response,
+ authenticatorAttachment = "", // Add your authenticator attachment
+ )
+ val result = Intent()
+
+ val createPublicKeyCredResponse =
+ CreatePublicKeyCredentialResponse(credential.json())
+
+ // Set the CreateCredentialResponse as the result of the Activity
+ PendingIntentHandler.setCreateCredentialResponse(
+ result,
+ createPublicKeyCredResponse
+ )
+ setResult(RESULT_OK, result)
+ finish()
+ }
+ }
+ )
+
+ val promptInfo = BiometricPrompt.PromptInfo.Builder()
+ .setTitle("Use your screen lock")
+ .setSubtitle("Create passkey for ${request.rp.name}")
+ .setAllowedAuthenticators(
+ BiometricManager.Authenticators.BIOMETRIC_STRONG
+ /* or BiometricManager.Authenticators.DEVICE_CREDENTIAL */
+ )
+ .build()
+ biometricPrompt.authenticate(promptInfo)
+ }
+
+ @RequiresApi(VERSION_CODES.P)
+ fun appInfoToOrigin(info: CallingAppInfo): String {
+ val cert = info.signingInfo.apkContentsSigners[0].toByteArray()
+ val md = MessageDigest.getInstance("SHA-256")
+ val certHash = md.digest(cert)
+ // This is the format for origin
+ return "android:apk-key-hash:${b64Encode(certHash)}"
+ }
+ // [END android_identity_credential_provider_handle_passkey]
+
+ @RequiresApi(VERSION_CODES.M)
+ // [START android_identity_credential_provider_password_creation]
+ fun processCreateCredentialRequest(
+ request: BeginCreateCredentialRequest
+ ): BeginCreateCredentialResponse? {
+ when (request) {
+ is BeginCreatePublicKeyCredentialRequest -> {
+ // Request is passkey type
+ return handleCreatePasskeyQuery(request)
+ }
+
+ is BeginCreatePasswordCredentialRequest -> {
+ // Request is password type
+ return handleCreatePasswordQuery(request)
+ }
+ }
+ return null
+ }
+
+ @RequiresApi(VERSION_CODES.M)
+ private fun handleCreatePasswordQuery(
+ request: BeginCreatePasswordCredentialRequest
+ ): BeginCreateCredentialResponse {
+ val createEntries: MutableList = mutableListOf()
+
+ // Adding two create entries - one for storing credentials to the 'Personal'
+ // account, and one for storing them to the 'Family' account. These
+ // accounts are local to this sample app only.
+ createEntries.add(
+ CreateEntry(
+ PERSONAL_ACCOUNT_ID,
+ createNewPendingIntent(PERSONAL_ACCOUNT_ID, CREATE_PASSWORD_INTENT)
+ )
+ )
+ createEntries.add(
+ CreateEntry(
+ FAMILY_ACCOUNT_ID,
+ createNewPendingIntent(FAMILY_ACCOUNT_ID, CREATE_PASSWORD_INTENT)
+ )
+ )
+
+ return BeginCreateCredentialResponse(createEntries)
+ }
+ // [END android_identity_credential_provider_password_creation]
+
+ @RequiresApi(VERSION_CODES.M)
+ fun handleEntrySelectionForPasswordCreation(
+ mDatabase: MyDatabase
+ ) {
+ // [START android_identity_credential_provider_entry_selection_password_creation]
+ val createRequest = PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
+ val accountId = intent.getStringExtra(CredentialsRepo.EXTRA_KEY_ACCOUNT_ID)
+
+ if (createRequest == null) {
+ return
+ }
+
+ val request: CreatePasswordRequest = createRequest.callingRequest as CreatePasswordRequest
+
+ // Fetch the ID and password from the request and save it in your database
+ mDatabase.addNewPassword(
+ PasswordInfo(
+ request.id,
+ request.password,
+ createRequest.callingAppInfo.packageName
+ )
+ )
+
+ // Set the final response back
+ val result = Intent()
+ val response = CreatePasswordResponse()
+ PendingIntentHandler.setCreateCredentialResponse(result, response)
+ setResult(Activity.RESULT_OK, result)
+ finish()
+ // [END android_identity_credential_provider_entry_selection_password_creation]
+ }
+
+ @RequiresApi(VERSION_CODES.P)
+ private fun handleUserSelectionForPasskeys(
+ mDatabase: MyDatabase
+ ) {
+ // [START android_identity_credential_provider_user_pk_selection]
+ val getRequest = PendingIntentHandler.retrieveProviderGetCredentialRequest(intent)
+ val publicKeyRequest = getRequest?.credentialOptions?.first() as GetPublicKeyCredentialOption
+
+ val requestInfo = intent.getBundleExtra("CREDENTIAL_DATA")
+ val credIdEnc = requestInfo?.getString("credId").orEmpty()
+
+ // Get the saved passkey from your database based on the credential ID from the PublicKeyRequest
+ val passkey = mDatabase.getPasskey(credIdEnc)
+
+ // Decode the credential ID, private key and user ID
+ val credId = b64Decode(credIdEnc)
+ val privateKey = b64Decode(passkey.credPrivateKey)
+ val uid = b64Decode(passkey.uid)
+
+ val origin = appInfoToOrigin(getRequest.callingAppInfo)
+ val packageName = getRequest.callingAppInfo.packageName
+
+ validatePasskey(
+ publicKeyRequest.requestJson,
+ origin,
+ packageName,
+ uid,
+ passkey.username,
+ credId,
+ privateKey
+ )
+ // [END android_identity_credential_provider_user_pk_selection]
+ }
+
+ @SuppressLint("RestrictedApi")
+ @RequiresApi(VERSION_CODES.M)
+ private fun validatePasskey(
+ requestJson: String,
+ origin: String,
+ packageName: String,
+ uid: ByteArray,
+ username: String,
+ credId: ByteArray,
+ privateKeyBytes: ByteArray,
+ ) {
+ // [START android_identity_credential_provider_user_validation_biometric]
+ val request = PublicKeyCredentialRequestOptions(requestJson)
+ val privateKey: ECPrivateKey = convertPrivateKey(privateKeyBytes)
+
+ val biometricPrompt = BiometricPrompt(
+ this,
+ { }, // Pass in your own executor
+ object : BiometricPrompt.AuthenticationCallback() {
+ override fun onAuthenticationError(
+ errorCode: Int,
+ errString: CharSequence
+ ) {
+ super.onAuthenticationError(errorCode, errString)
+ finish()
+ }
+
+ override fun onAuthenticationFailed() {
+ super.onAuthenticationFailed()
+ finish()
+ }
+
+ override fun onAuthenticationSucceeded(
+ result: BiometricPrompt.AuthenticationResult
+ ) {
+ super.onAuthenticationSucceeded(result)
+ val response = AuthenticatorAssertionResponse(
+ requestOptions = request,
+ credentialId = credId,
+ origin = origin,
+ up = true,
+ uv = true,
+ be = true,
+ bs = true,
+ userHandle = uid,
+ packageName = packageName
+ )
+
+ val sig = Signature.getInstance("SHA256withECDSA")
+ sig.initSign(privateKey)
+ sig.update(response.dataToSign())
+ response.signature = sig.sign()
+
+ val credential = FidoPublicKeyCredential(
+ rawId = credId,
+ response = response,
+ authenticatorAttachment = "", // Add your authenticator attachment
+ )
+ val result = Intent()
+ val passkeyCredential = PublicKeyCredential(credential.json())
+ PendingIntentHandler.setGetCredentialResponse(
+ result, GetCredentialResponse(passkeyCredential)
+ )
+ setResult(RESULT_OK, result)
+ finish()
+ }
+ }
+ )
+
+ val promptInfo = BiometricPrompt.PromptInfo.Builder()
+ .setTitle("Use your screen lock")
+ .setSubtitle("Use passkey for ${request.rpId}")
+ .setAllowedAuthenticators(
+ BiometricManager.Authenticators.BIOMETRIC_STRONG
+ /* or BiometricManager.Authenticators.DEVICE_CREDENTIAL */
+ )
+ .build()
+ biometricPrompt.authenticate(promptInfo)
+ // [END android_identity_credential_provider_user_validation_biometric]
+ }
+
+ @RequiresApi(VERSION_CODES.M)
+ private fun handleUserSelectionForPasswordAuthentication(
+ mDatabase: MyDatabase,
+ callingAppInfo: CallingAppInfo,
+ ) {
+ // [START android_identity_credential_provider_user_selection_password]
+ val getRequest = PendingIntentHandler.retrieveProviderGetCredentialRequest(intent)
+
+ val passwordOption = getRequest?.credentialOptions?.first() as GetPasswordOption
+
+ val username = passwordOption.allowedUserIds.first()
+ // Fetch the credentials for the calling app package name
+ val creds = mDatabase.getCredentials(callingAppInfo.packageName)
+ val passwords = creds.passwords
+ val it = passwords.iterator()
+ var password = ""
+ while (it.hasNext()) {
+ val passwordItemCurrent = it.next()
+ if (passwordItemCurrent.username == username) {
+ password = passwordItemCurrent.password
+ break
+ }
+ }
+ // [END android_identity_credential_provider_user_selection_password]
+
+ // [START android_identity_credential_provider_set_response]
+ // Set the response back
+ val result = Intent()
+ val passwordCredential = PasswordCredential(username, password)
+ PendingIntentHandler.setGetCredentialResponse(
+ result, GetCredentialResponse(passwordCredential)
+ )
+ setResult(Activity.RESULT_OK, result)
+ finish()
+ // [END android_identity_credential_provider_set_response]
+ }
+
+ // [START android_identity_credential_pending_intent]
+ fun createSettingsPendingIntent(): PendingIntent { // [END android_identity_credential_pending_intent]
+ return PendingIntent.getBroadcast(this, 0, Intent(), PendingIntent.FLAG_IMMUTABLE)
+ }
+
+ private fun getPublicKeyFromKeyPair(keyPair: KeyPair): ByteArray {
+ return byteArrayOf()
+ }
+
+ private fun b64Encode(certHash: ByteArray) {}
+
+ private fun handleCreatePasskeyQuery(
+ request: BeginCreatePublicKeyCredentialRequest
+ ): BeginCreateCredentialResponse {
+ return BeginCreateCredentialResponse()
+ }
+
+ private fun createNewPendingIntent(
+ accountId: String,
+ intent: String
+ ): PendingIntent {
+ return PendingIntent.getBroadcast(this, 0, Intent(), PendingIntent.FLAG_IMMUTABLE)
+ }
+
+ private fun b64Decode(encodedString: String): ByteArray {
+ return byteArrayOf()
+ }
+
+ private fun convertPrivateKey(privateKeyBytes: ByteArray): ECPrivateKey {
+ return ECPrivateKeyImpl()
+ }
+}
+
+object CredentialsRepo {
+ const val EXTRA_KEY_ACCOUNT_ID: String = ""
+}
+
+class MyDatabase {
+ fun addNewPassword(passwordInfo: PasswordInfo) {}
+
+ fun getPasskey(credIdEnc: String): PasskeyInfo {
+ return PasskeyInfo()
+ }
+
+ fun getCredentials(packageName: String): CredentialsInfo {
+ return CredentialsInfo()
+ }
+}
+
+data class PasswordInfo(
+ val id: String = "",
+ val password: String = "",
+ val packageName: String = "",
+ val username: String = ""
+)
+
+data class PasskeyInfo(
+ val credPrivateKey: String = "",
+ val uid: String = "",
+ val username: String = ""
+)
+
+data class CredentialsInfo(
+ val passwords: List = listOf()
+)
+
+class ECPrivateKeyImpl : ECPrivateKey {
+ override fun getAlgorithm(): String = ""
+ override fun getFormat(): String = ""
+ override fun getEncoded(): ByteArray = byteArrayOf()
+ override fun getParams(): ECParameterSpec {
+ return ECParameterSpec(
+ EllipticCurve(
+ { 0 },
+ BigInteger.ZERO,
+ BigInteger.ZERO
+ ),
+ ECPoint(
+ BigInteger.ZERO,
+ BigInteger.ZERO
+ ),
+ BigInteger.ZERO,
+ 0
+ )
+ }
+ override fun getS(): BigInteger = BigInteger.ZERO
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/Fido2ToCredmanMigration.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/Fido2ToCredmanMigration.kt
new file mode 100644
index 000000000..05b06cce7
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/Fido2ToCredmanMigration.kt
@@ -0,0 +1,271 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.app.Activity
+import android.content.Context
+import android.util.JsonWriter
+import android.util.Log
+import android.widget.Toast
+import androidx.credentials.CreateCredentialRequest
+import androidx.credentials.CreatePublicKeyCredentialRequest
+import androidx.credentials.CreatePublicKeyCredentialResponse
+import androidx.credentials.CredentialManager
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.GetPasswordOption
+import androidx.credentials.GetPublicKeyCredentialOption
+import androidx.credentials.PublicKeyCredential
+import androidx.credentials.exceptions.CreateCredentialException
+import com.example.identity.credentialmanager.ApiResult.Success
+import java.io.StringWriter
+import okhttp3.MediaType.Companion.toMediaTypeOrNull
+import okhttp3.OkHttpClient
+import okhttp3.Request.Builder
+import okhttp3.RequestBody
+import okhttp3.RequestBody.Companion.toRequestBody
+import okhttp3.Response
+import okhttp3.ResponseBody
+import org.json.JSONObject
+import ru.gildor.coroutines.okhttp.await
+
+class Fido2ToCredmanMigration(
+ private val context: Context,
+ private val client: OkHttpClient,
+) {
+ private val BASE_URL = ""
+ private val JSON = "".toMediaTypeOrNull()
+ private val PUBLIC_KEY = ""
+
+ // [START android_identity_fido2_credman_init]
+ val credMan = CredentialManager.create(context)
+ // [END android_identity_fido2_credman_init]
+
+ // [START android_identity_fido2_migration_post_request_body]
+ suspend fun registerRequest() {
+ // ...
+ val call = client.newCall(
+ Builder()
+ .method(
+ "POST",
+ jsonRequestBody {
+ name("attestation").value("none")
+ name("authenticatorSelection").objectValue {
+ name("residentKey").value("required")
+ }
+ }
+ ).build()
+ )
+ // ...
+ }
+ // [END android_identity_fido2_migration_post_request_body]
+
+ // [START android_identity_fido2_migration_register_request]
+ suspend fun registerRequest(sessionId: String): ApiResult {
+ val call = client.newCall(
+ Builder()
+ .url("https://github.com/$BASE_URL/%3Cyour%20api%20url%3E")
+ .addHeader("Cookie", formatCookie(sessionId))
+ .method(
+ "POST",
+ jsonRequestBody {
+ name("attestation").value("none")
+ name("authenticatorSelection").objectValue {
+ name("authenticatorAttachment").value("platform")
+ name("userVerification").value("required")
+ name("residentKey").value("required")
+ }
+ }
+ ).build()
+ )
+ val response = call.await()
+ return response.result("Error calling the api") {
+ parsePublicKeyCredentialCreationOptions(
+ body ?: throw ApiException("Empty response from the api call")
+ )
+ }
+ }
+ // [END android_identity_fido2_migration_register_request]
+
+ // [START android_identity_fido2_migration_create_passkey]
+ suspend fun createPasskey(
+ activity: Activity,
+ requestResult: JSONObject
+ ): CreatePublicKeyCredentialResponse? {
+ val request = CreatePublicKeyCredentialRequest(requestResult.toString())
+ var response: CreatePublicKeyCredentialResponse? = null
+ try {
+ response = credMan.createCredential(
+ request = request as CreateCredentialRequest,
+ context = activity
+ ) as CreatePublicKeyCredentialResponse
+ } catch (e: CreateCredentialException) {
+
+ showErrorAlert(activity, e)
+
+ return null
+ }
+ return response
+ }
+ // [END android_identity_fido2_migration_create_passkey]
+
+ // [START android_identity_fido2_migration_auth_with_passkeys]
+ /**
+ * @param sessionId The session ID to be used for the sign-in.
+ * @param credentialId The credential ID of this device.
+ * @return a JSON object.
+ */
+ suspend fun signinRequest(): ApiResult {
+ val call = client.newCall(
+ Builder().url(
+ buildString {
+ append("$BASE_URL/signinRequest")
+ }
+ ).method("POST", jsonRequestBody {})
+ .build()
+ )
+ val response = call.await()
+ return response.result("Error calling /signinRequest") {
+ parsePublicKeyCredentialRequestOptions(
+ body ?: throw ApiException("Empty response from /signinRequest")
+ )
+ }
+ }
+
+ /**
+ * @param sessionId The session ID to be used for the sign-in.
+ * @param response The JSONObject for signInResponse.
+ * @param credentialId id/rawId.
+ * @return A list of all the credentials registered on the server,
+ * including the newly-registered one.
+ */
+ suspend fun signinResponse(
+ sessionId: String,
+ response: JSONObject,
+ credentialId: String
+ ): ApiResult {
+
+ val call = client.newCall(
+ Builder().url("https://github.com/$BASE_URL/signinResponse")
+ .addHeader("Cookie", formatCookie(sessionId))
+ .method(
+ "POST",
+ jsonRequestBody {
+ name("id").value(credentialId)
+ name("type").value(PUBLIC_KEY.toString())
+ name("rawId").value(credentialId)
+ name("response").objectValue {
+ name("clientDataJSON").value(
+ response.getString("clientDataJSON")
+ )
+ name("authenticatorData").value(
+ response.getString("authenticatorData")
+ )
+ name("signature").value(
+ response.getString("signature")
+ )
+ name("userHandle").value(
+ response.getString("userHandle")
+ )
+ }
+ }
+ ).build()
+ )
+ val apiResponse = call.await()
+ return apiResponse.result("Error calling /signingResponse") {
+ }
+ }
+ // [END android_identity_fido2_migration_auth_with_passkeys]
+
+ // [START android_identity_fido2_migration_get_passkeys]
+ suspend fun getPasskey(
+ activity: Activity,
+ creationResult: JSONObject
+ ): GetCredentialResponse? {
+ Toast.makeText(
+ activity,
+ "Fetching previously stored credentials",
+ Toast.LENGTH_SHORT
+ )
+ .show()
+ var result: GetCredentialResponse? = null
+ try {
+ val request = GetCredentialRequest(
+ listOf(
+ GetPublicKeyCredentialOption(
+ creationResult.toString(),
+ null
+ ),
+ GetPasswordOption()
+ )
+ )
+ result = credMan.getCredential(activity, request)
+ if (result.credential is PublicKeyCredential) {
+ val publicKeycredential = result.credential as PublicKeyCredential
+ Log.i("TAG", "Passkey ${publicKeycredential.authenticationResponseJson}")
+ return result
+ }
+ } catch (e: Exception) {
+ showErrorAlert(activity, e)
+ }
+ return result
+ }
+ // [END android_identity_fido2_migration_get_passkeys]
+
+ private fun showErrorAlert(
+ activity: Activity,
+ e: Exception
+ ) {}
+
+ private fun jsonRequestBody(body: JsonWriter.() -> Unit): RequestBody {
+ val output = StringWriter()
+ JsonWriter(output).use { writer ->
+ writer.beginObject()
+ writer.body()
+ writer.endObject()
+ }
+ return output.toString().toRequestBody(JSON)
+ }
+
+ private fun JsonWriter.objectValue(body: JsonWriter.() -> Unit) {
+ beginObject()
+ body()
+ endObject()
+ }
+
+ private fun formatCookie(sessionId: String): String {
+ return ""
+ }
+
+ private fun parsePublicKeyCredentialCreationOptions(body: ResponseBody): JSONObject {
+ return JSONObject()
+ }
+
+ private fun parsePublicKeyCredentialRequestOptions(body: ResponseBody): JSONObject {
+ return JSONObject()
+ }
+
+ private fun Response.result(errorMessage: String, data: Response.() -> T): ApiResult {
+ return Success()
+ }
+}
+
+sealed class ApiResult {
+ class Success : ApiResult()
+}
+
+class ApiException(message: String) : RuntimeException(message)
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MainActivity.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MainActivity.kt
new file mode 100644
index 000000000..58fae77dc
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MainActivity.kt
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+
+class MainActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MyCredentialProviderService.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MyCredentialProviderService.kt
new file mode 100644
index 000000000..77db763d3
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/MyCredentialProviderService.kt
@@ -0,0 +1,327 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.annotation.SuppressLint
+import android.app.PendingIntent
+import android.content.Intent
+import android.graphics.drawable.Icon
+import android.os.Build.VERSION_CODES
+import android.os.Bundle
+import android.os.CancellationSignal
+import android.os.OutcomeReceiver
+import android.util.Log
+import androidx.annotation.RequiresApi
+import androidx.credentials.exceptions.ClearCredentialException
+import androidx.credentials.exceptions.CreateCredentialException
+import androidx.credentials.exceptions.CreateCredentialUnknownException
+import androidx.credentials.exceptions.GetCredentialException
+import androidx.credentials.exceptions.GetCredentialUnknownException
+import androidx.credentials.provider.AuthenticationAction
+import androidx.credentials.provider.BeginCreateCredentialRequest
+import androidx.credentials.provider.BeginCreateCredentialResponse
+import androidx.credentials.provider.BeginCreatePublicKeyCredentialRequest
+import androidx.credentials.provider.BeginGetCredentialRequest
+import androidx.credentials.provider.BeginGetCredentialResponse
+import androidx.credentials.provider.BeginGetPasswordOption
+import androidx.credentials.provider.BeginGetPublicKeyCredentialOption
+import androidx.credentials.provider.CallingAppInfo
+import androidx.credentials.provider.CreateEntry
+import androidx.credentials.provider.CredentialEntry
+import androidx.credentials.provider.CredentialProviderService
+import androidx.credentials.provider.PasswordCredentialEntry
+import androidx.credentials.provider.ProviderClearCredentialStateRequest
+import androidx.credentials.provider.PublicKeyCredentialEntry
+import androidx.credentials.webauthn.PublicKeyCredentialRequestOptions
+
+@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
+class MyCredentialProviderService : CredentialProviderService() {
+ private val PERSONAL_ACCOUNT_ID: String = ""
+ private val FAMILY_ACCOUNT_ID: String = ""
+ private val CREATE_PASSKEY_INTENT: String = ""
+ private val PACKAGE_NAME: String = ""
+ private val EXTRA_KEY_ACCOUNT_ID: String = ""
+ private val UNIQUE_REQ_CODE: Int = 1
+ private val UNLOCK_INTENT: String = ""
+ private val UNIQUE_REQUEST_CODE: Int = 0
+ private val TAG: String = ""
+ private val GET_PASSWORD_INTENT: String = ""
+
+ // [START android_identity_credential_provider_passkey_creation]
+ override fun onBeginCreateCredentialRequest(
+ request: BeginCreateCredentialRequest,
+ cancellationSignal: CancellationSignal,
+ callback: OutcomeReceiver,
+ ) {
+ val response: BeginCreateCredentialResponse? = processCreateCredentialRequest(request)
+ if (response != null) {
+ callback.onResult(response)
+ } else {
+ callback.onError(CreateCredentialUnknownException())
+ }
+ }
+
+ fun processCreateCredentialRequest(request: BeginCreateCredentialRequest): BeginCreateCredentialResponse? {
+ when (request) {
+ is BeginCreatePublicKeyCredentialRequest -> {
+ // Request is passkey type
+ return handleCreatePasskeyQuery(request)
+ }
+ }
+ // Request not supported
+ return null
+ }
+
+ private fun handleCreatePasskeyQuery(
+ request: BeginCreatePublicKeyCredentialRequest
+ ): BeginCreateCredentialResponse {
+
+ // Adding two create entries - one for storing credentials to the 'Personal'
+ // account, and one for storing them to the 'Family' account. These
+ // accounts are local to this sample app only.
+ val createEntries: MutableList = mutableListOf()
+ createEntries.add(
+ CreateEntry(
+ PERSONAL_ACCOUNT_ID,
+ createNewPendingIntent(PERSONAL_ACCOUNT_ID, CREATE_PASSKEY_INTENT)
+ )
+ )
+
+ createEntries.add(
+ CreateEntry(
+ FAMILY_ACCOUNT_ID,
+ createNewPendingIntent(FAMILY_ACCOUNT_ID, CREATE_PASSKEY_INTENT)
+ )
+ )
+
+ return BeginCreateCredentialResponse(createEntries)
+ }
+
+ private fun createNewPendingIntent(accountId: String, action: String): PendingIntent {
+ val intent = Intent(action).setPackage(PACKAGE_NAME)
+
+ // Add your local account ID as an extra to the intent, so that when
+ // user selects this entry, the credential can be saved to this
+ // account
+ intent.putExtra(EXTRA_KEY_ACCOUNT_ID, accountId)
+
+ return PendingIntent.getActivity(
+ applicationContext, UNIQUE_REQ_CODE,
+ intent,
+ (
+ PendingIntent.FLAG_MUTABLE
+ or PendingIntent.FLAG_UPDATE_CURRENT
+ )
+ )
+ }
+ // [END android_identity_credential_provider_passkey_creation]
+
+ private lateinit var response: BeginGetCredentialResponse
+
+ // [START android_identity_credential_provider_sign_in]
+ private val unlockEntryTitle = "Authenticate to continue"
+
+ override fun onBeginGetCredentialRequest(
+ request: BeginGetCredentialRequest,
+ cancellationSignal: CancellationSignal,
+ callback: OutcomeReceiver,
+ ) {
+ if (isAppLocked()) {
+ callback.onResult(
+ BeginGetCredentialResponse(
+ authenticationActions = mutableListOf(
+ AuthenticationAction(
+ unlockEntryTitle, createUnlockPendingIntent()
+ )
+ )
+ )
+ )
+ return
+ }
+ try {
+ response = processGetCredentialRequest(request)
+ callback.onResult(response)
+ } catch (e: GetCredentialException) {
+ callback.onError(GetCredentialUnknownException())
+ }
+ }
+ // [END android_identity_credential_provider_sign_in]
+
+ // [START android_identity_credential_provider_unlock_pending_intent]
+ private fun createUnlockPendingIntent(): PendingIntent {
+ val intent = Intent(UNLOCK_INTENT).setPackage(PACKAGE_NAME)
+ return PendingIntent.getActivity(
+ applicationContext, UNIQUE_REQUEST_CODE, intent,
+ (
+ PendingIntent.FLAG_MUTABLE
+ or PendingIntent.FLAG_UPDATE_CURRENT
+ )
+ )
+ }
+ // [END android_identity_credential_provider_unlock_pending_intent]
+
+ // [START android_identity_credential_provider_process_get_credential_request]
+ companion object {
+ // These intent actions are specified for corresponding activities
+ // that are to be invoked through the PendingIntent(s)
+ private const val GET_PASSKEY_INTENT_ACTION = "PACKAGE_NAME.GET_PASSKEY"
+ private const val GET_PASSWORD_INTENT_ACTION = "PACKAGE_NAME.GET_PASSWORD"
+ }
+
+ fun processGetCredentialRequest(
+ request: BeginGetCredentialRequest
+ ): BeginGetCredentialResponse {
+ val callingPackageInfo = request.callingAppInfo
+ val callingPackageName = callingPackageInfo?.packageName.orEmpty()
+ val credentialEntries: MutableList = mutableListOf()
+
+ for (option in request.beginGetCredentialOptions) {
+ when (option) {
+ is BeginGetPasswordOption -> {
+ credentialEntries.addAll(
+ populatePasswordData(
+ callingPackageName,
+ option
+ )
+ )
+ }
+ is BeginGetPublicKeyCredentialOption -> {
+ credentialEntries.addAll(
+ populatePasskeyData(
+ callingPackageInfo,
+ option
+ )
+ )
+ } else -> {
+ Log.i(TAG, "Request not supported")
+ }
+ }
+ }
+ return BeginGetCredentialResponse(credentialEntries)
+ }
+ // [END android_identity_credential_provider_process_get_credential_request]
+
+ @SuppressLint("RestrictedApi")
+ // [START android_identity_credential_provider_populate_pkpw_data]
+ private fun populatePasskeyData(
+ callingAppInfo: CallingAppInfo?,
+ option: BeginGetPublicKeyCredentialOption
+ ): List {
+ val passkeyEntries: MutableList = mutableListOf()
+ val request = PublicKeyCredentialRequestOptions(option.requestJson)
+ // Get your credentials from database where you saved during creation flow
+ val creds = getCredentialsFromInternalDb(request.rpId)
+ val passkeys = creds.passkeys
+ for (passkey in passkeys) {
+ val data = Bundle()
+ data.putString("credId", passkey.credId)
+ passkeyEntries.add(
+ PublicKeyCredentialEntry(
+ context = applicationContext,
+ username = passkey.username,
+ pendingIntent = createNewPendingIntent(
+ GET_PASSKEY_INTENT_ACTION,
+ data
+ ),
+ beginGetPublicKeyCredentialOption = option,
+ displayName = passkey.displayName,
+ icon = passkey.icon
+ )
+ )
+ }
+ return passkeyEntries
+ }
+
+ // Fetch password credentials and create password entries to populate to the user
+ private fun populatePasswordData(
+ callingPackage: String,
+ option: BeginGetPasswordOption
+ ): List {
+ val passwordEntries: MutableList = mutableListOf()
+
+ // Get your password credentials from database where you saved during
+ // creation flow
+ val creds = getCredentialsFromInternalDb(callingPackage)
+ val passwords = creds.passwords
+ for (password in passwords) {
+ passwordEntries.add(
+ PasswordCredentialEntry(
+ context = applicationContext,
+ username = password.username,
+ pendingIntent = createNewPendingIntent(
+ GET_PASSWORD_INTENT
+ ),
+ beginGetPasswordOption = option,
+ displayName = password.username,
+ icon = password.icon
+ )
+ )
+ }
+ return passwordEntries
+ }
+
+ private fun createNewPendingIntent(
+ action: String,
+ extra: Bundle? = null
+ ): PendingIntent {
+ val intent = Intent(action).setPackage(PACKAGE_NAME)
+ if (extra != null) {
+ intent.putExtra("CREDENTIAL_DATA", extra)
+ }
+
+ return PendingIntent.getActivity(
+ applicationContext, UNIQUE_REQUEST_CODE, intent,
+ (PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
+ )
+ }
+ // [END android_identity_credential_provider_populate_pkpw_data]
+
+ // [START android_identity_credential_provider_clear_credential]
+ override fun onClearCredentialStateRequest(
+ request: ProviderClearCredentialStateRequest,
+ cancellationSignal: CancellationSignal,
+ callback: OutcomeReceiver
+ ) {
+ // Delete any maintained state as appropriate.
+ }
+ // [END android_identity_credential_provider_clear_credential]
+
+ private fun isAppLocked(): Boolean {
+ return true
+ }
+
+ private fun getCredentialsFromInternalDb(rpId: String): Creds {
+ return Creds()
+ }
+}
+
+data class Creds(
+ val passkeys: List = listOf(),
+ val passwords: List = listOf()
+)
+
+data class Passkey(
+ val credId: String = "",
+ val username: String = "",
+ val displayName: String = "",
+ val icon: Icon
+)
+
+data class Password(
+ val username: String = "",
+ val icon: Icon
+)
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyAndPasswordFunctions.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyAndPasswordFunctions.kt
new file mode 100644
index 000000000..747f72c15
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyAndPasswordFunctions.kt
@@ -0,0 +1,331 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.content.Context
+import android.os.Build
+import android.os.Bundle
+import android.util.Log
+import androidx.annotation.RequiresApi
+import androidx.appcompat.widget.AppCompatEditText
+import androidx.credentials.CreatePasswordRequest
+import androidx.credentials.CreatePublicKeyCredentialRequest
+import androidx.credentials.CredentialManager
+import androidx.credentials.CustomCredential
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.GetPasswordOption
+import androidx.credentials.GetPublicKeyCredentialOption
+import androidx.credentials.PasswordCredential
+import androidx.credentials.PendingGetCredentialRequest
+import androidx.credentials.PublicKeyCredential
+import androidx.credentials.exceptions.CreateCredentialCancellationException
+import androidx.credentials.exceptions.CreateCredentialCustomException
+import androidx.credentials.exceptions.CreateCredentialException
+import androidx.credentials.exceptions.CreateCredentialInterruptedException
+import androidx.credentials.exceptions.CreateCredentialProviderConfigurationException
+import androidx.credentials.exceptions.GetCredentialException
+import androidx.credentials.exceptions.publickeycredential.CreatePublicKeyCredentialDomException
+import androidx.credentials.pendingGetCredentialRequest
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.runBlocking
+import org.json.JSONObject
+
+class PasskeyAndPasswordFunctions(
+ context: Context,
+) {
+ // [START android_identity_initialize_credman]
+ // Use your app or activity context to instantiate a client instance of
+ // CredentialManager.
+ private val credentialManager = CredentialManager.create(context)
+ // [END android_identity_initialize_credman]
+ private val activityContext = context
+
+ // Placeholder for TAG log value.
+ val TAG = ""
+ /**
+ * Retrieves a passkey from the credential manager.
+ *
+ * @param creationResult The result of the passkey creation operation.
+ * @param context The activity context from the Composable, to be used in Credential Manager APIs
+ * @return The [GetCredentialResponse] object containing the passkey, or null if an error occurred.
+ */
+ @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+ fun signInFlow(
+ creationResult: JSONObject
+ ) {
+ val requestJson = creationResult.toString()
+ // [START android_identity_get_password_passkey_options]
+ // Retrieves the user's saved password for your app from their
+ // password provider.
+ val getPasswordOption = GetPasswordOption()
+
+ // Get passkey from the user's public key credential provider.
+ val getPublicKeyCredentialOption = GetPublicKeyCredentialOption(
+ requestJson = requestJson
+ )
+ // [END android_identity_get_password_passkey_options]
+ var result: GetCredentialResponse
+ // [START android_identity_get_credential_request]
+ val credentialRequest = GetCredentialRequest(
+ listOf(getPasswordOption, getPublicKeyCredentialOption),
+ )
+ // [END android_identity_get_credential_request]
+ runBlocking {
+ // getPrepareCredential request
+ // [START android_identity_prepare_get_credential]
+ coroutineScope {
+ val response = credentialManager.prepareGetCredential(
+ GetCredentialRequest(
+ listOf(
+ getPublicKeyCredentialOption,
+ getPasswordOption
+ )
+ )
+ )
+ }
+ // [END android_identity_prepare_get_credential]
+ // getCredential request without handling exception.
+ // [START android_identity_launch_sign_in_flow_1]
+ coroutineScope {
+ try {
+ result = credentialManager.getCredential(
+ // Use an activity-based context to avoid undefined system UI
+ // launching behavior.
+ context = activityContext,
+ request = credentialRequest
+ )
+ handleSignIn(result)
+ } catch (e: GetCredentialException) {
+ // Handle failure
+ }
+ }
+ // [END android_identity_launch_sign_in_flow_1]
+ // getCredential request adding some exception handling.
+ // [START android_identity_handle_exceptions_no_credential]
+ coroutineScope {
+ try {
+ result = credentialManager.getCredential(
+ context = activityContext,
+ request = credentialRequest
+ )
+ } catch (e: GetCredentialException) {
+ Log.e("CredentialManager", "No credential available", e)
+ }
+ }
+ // [END android_identity_handle_exceptions_no_credential]
+ }
+ }
+
+ fun autofillImplementation(
+ requestJson: String
+ ) {
+ // [START android_identity_autofill_construct_request]
+ // Retrieves the user's saved password for your app.
+ val getPasswordOption = GetPasswordOption()
+
+ // Get a passkey from the user's public key credential provider.
+ val getPublicKeyCredentialOption = GetPublicKeyCredentialOption(
+ requestJson = requestJson
+ )
+
+ val getCredRequest = GetCredentialRequest(
+ listOf(getPasswordOption, getPublicKeyCredentialOption)
+ )
+ // [END android_identity_autofill_construct_request]
+
+ runBlocking {
+ // [START android_identity_autofill_get_credential_api]
+ coroutineScope {
+ try {
+ val result = credentialManager.getCredential(
+ context = activityContext, // Use an activity-based context.
+ request = getCredRequest
+ )
+ handleSignIn(result)
+ } catch (e: GetCredentialException) {
+ handleFailure(e)
+ }
+ }
+ // [END android_identity_autofill_get_credential_api]
+ }
+
+ val usernameEditText: androidx.appcompat.widget.AppCompatEditText = AppCompatEditText(activityContext)
+ val passwordEditText: androidx.appcompat.widget.AppCompatEditText = AppCompatEditText(activityContext)
+
+ // [START android_identity_autofill_enable_edit_text]
+ usernameEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
+ getCredRequest
+ ) { response ->
+ handleSignIn(response)
+ }
+
+ passwordEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
+ getCredRequest
+ ) { response ->
+ handleSignIn(response)
+ }
+ // [END android_identity_autofill_enable_edit_text]
+ }
+
+ // [START android_identity_launch_sign_in_flow_2]
+ fun handleSignIn(result: GetCredentialResponse) {
+ // Handle the successfully returned credential.
+ val credential = result.credential
+
+ when (credential) {
+ is PublicKeyCredential -> {
+ val responseJson = credential.authenticationResponseJson
+ // Share responseJson i.e. a GetCredentialResponse on your server to
+ // validate and authenticate
+ }
+
+ is PasswordCredential -> {
+ val username = credential.id
+ val password = credential.password
+ // Use id and password to send to your server to validate
+ // and authenticate
+ }
+
+ is CustomCredential -> {
+ // If you are also using any external sign-in libraries, parse them
+ // here with the utility functions provided.
+ if (credential.type == ExampleCustomCredential.TYPE) {
+ try {
+ val ExampleCustomCredential =
+ ExampleCustomCredential.createFrom(credential.data)
+ // Extract the required credentials and complete the authentication as per
+ // the federated sign in or any external sign in library flow
+ } catch (e: ExampleCustomCredential.ExampleCustomCredentialParsingException) {
+ // Unlikely to happen. If it does, you likely need to update the dependency
+ // version of your external sign-in library.
+ Log.e(TAG, "Failed to parse an ExampleCustomCredential", e)
+ }
+ } else {
+ // Catch any unrecognized custom credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+ else -> {
+ // Catch any unrecognized credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+ }
+ // [END android_identity_launch_sign_in_flow_2]
+
+ // [START android_identity_create_passkey]
+ suspend fun createPasskey(requestJson: String, preferImmediatelyAvailableCredentials: Boolean) {
+ val createPublicKeyCredentialRequest = CreatePublicKeyCredentialRequest(
+ // Contains the request in JSON format. Uses the standard WebAuthn
+ // web JSON spec.
+ requestJson = requestJson,
+ // Defines whether you prefer to use only immediately available
+ // credentials, not hybrid credentials, to fulfill this request.
+ // This value is false by default.
+ preferImmediatelyAvailableCredentials = preferImmediatelyAvailableCredentials,
+ )
+
+ // Execute CreateCredentialRequest asynchronously to register credentials
+ // for a user account. Handle success and failure cases with the result and
+ // exceptions, respectively.
+ coroutineScope {
+ try {
+ val result = credentialManager.createCredential(
+ // Use an activity-based context to avoid undefined system
+ // UI launching behavior
+ context = activityContext,
+ request = createPublicKeyCredentialRequest,
+ )
+ // Handle passkey creation result
+ } catch (e: CreateCredentialException) {
+ handleFailure(e)
+ }
+ }
+ }
+ // [END android_identity_create_passkey]
+
+ // [START android_identity_handle_create_passkey_failure]
+ fun handleFailure(e: CreateCredentialException) {
+ when (e) {
+ is CreatePublicKeyCredentialDomException -> {
+ // Handle the passkey DOM errors thrown according to the
+ // WebAuthn spec.
+ }
+ is CreateCredentialCancellationException -> {
+ // The user intentionally canceled the operation and chose not
+ // to register the credential.
+ }
+ is CreateCredentialInterruptedException -> {
+ // Retry-able error. Consider retrying the call.
+ }
+ is CreateCredentialProviderConfigurationException -> {
+ // Your app is missing the provider configuration dependency.
+ // Most likely, you're missing the
+ // "credentials-play-services-auth" module.
+ }
+ is CreateCredentialCustomException -> {
+ // You have encountered an error from a 3rd-party SDK. If you
+ // make the API call with a request object that's a subclass of
+ // CreateCustomCredentialRequest using a 3rd-party SDK, then you
+ // should check for any custom exception type constants within
+ // that SDK to match with e.type. Otherwise, drop or log the
+ // exception.
+ }
+ else -> Log.w(TAG, "Unexpected exception type ${e::class.java.name}")
+ }
+ }
+ // [END android_identity_handle_create_passkey_failure]
+
+ fun handleFailure(e: GetCredentialException) { }
+
+ // [START android_identity_register_password]
+ suspend fun registerPassword(username: String, password: String) {
+ // Initialize a CreatePasswordRequest object.
+ val createPasswordRequest =
+ CreatePasswordRequest(id = username, password = password)
+
+ // Create credential and handle result.
+ coroutineScope {
+ try {
+ val result =
+ credentialManager.createCredential(
+ // Use an activity based context to avoid undefined
+ // system UI launching behavior.
+ activityContext,
+ createPasswordRequest
+ )
+ // Handle register password result
+ } catch (e: CreateCredentialException) {
+ handleFailure(e)
+ }
+ }
+ }
+ // [END android_identity_register_password]
+}
+
+sealed class ExampleCustomCredential {
+ class ExampleCustomCredentialParsingException : Throwable()
+
+ companion object {
+ fun createFrom(data: Bundle): PublicKeyCredential {
+ return PublicKeyCredential("")
+ }
+
+ const val TYPE: String = ""
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyWebListener.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyWebListener.kt
new file mode 100644
index 000000000..f13052b49
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/PasskeyWebListener.kt
@@ -0,0 +1,256 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.app.Activity
+import android.net.Uri
+import android.util.Log
+import android.webkit.WebView
+import android.widget.Toast
+import androidx.annotation.UiThread
+import androidx.credentials.PublicKeyCredential
+import androidx.credentials.exceptions.CreateCredentialException
+import androidx.credentials.exceptions.GetCredentialException
+import androidx.webkit.JavaScriptReplyProxy
+import androidx.webkit.WebMessageCompat
+import androidx.webkit.WebViewCompat
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.launch
+import org.json.JSONArray
+import org.json.JSONObject
+
+// Placeholder for TAG log value.
+const val TAG = ""
+
+// This class is mostly copied from https://github.com/android/identity-samples/blob/main/WebView/CredentialManagerWebView/PasskeyWebListener.kt.
+
+// [START android_identity_create_listener_passkeys]
+// The class talking to Javascript should inherit:
+class PasskeyWebListener(
+ private val activity: Activity,
+ private val coroutineScope: CoroutineScope,
+ private val credentialManagerHandler: CredentialManagerHandler
+) : WebViewCompat.WebMessageListener {
+ /** havePendingRequest is true if there is an outstanding WebAuthn request.
+ There is only ever one request outstanding at a time. */
+ private var havePendingRequest = false
+
+ /** pendingRequestIsDoomed is true if the WebView has navigated since
+ starting a request. The FIDO module cannot be canceled, but the response
+ will never be delivered in this case. */
+ private var pendingRequestIsDoomed = false
+
+ /** replyChannel is the port that the page is listening for a response on.
+ It is valid if havePendingRequest is true. */
+ private var replyChannel: ReplyChannel? = null
+
+ /**
+ * Called by the page during a WebAuthn request.
+ *
+ * @param view Creates the WebView.
+ * @param message The message sent from the client using injected JavaScript.
+ * @param sourceOrigin The origin of the HTTPS request. Should not be null.
+ * @param isMainFrame Should be set to true. Embedded frames are not
+ supported.
+ * @param replyProxy Passed in by JavaScript. Allows replying when wrapped in
+ the Channel.
+ * @return The message response.
+ */
+ @UiThread
+ override fun onPostMessage(
+ view: WebView,
+ message: WebMessageCompat,
+ sourceOrigin: Uri,
+ isMainFrame: Boolean,
+ replyProxy: JavaScriptReplyProxy,
+ ) {
+ val messageData = message.data ?: return
+ onRequest(
+ messageData,
+ sourceOrigin,
+ isMainFrame,
+ JavaScriptReplyChannel(replyProxy)
+ )
+ }
+
+ private fun onRequest(
+ msg: String,
+ sourceOrigin: Uri,
+ isMainFrame: Boolean,
+ reply: ReplyChannel,
+ ) {
+ msg?.let {
+ val jsonObj = JSONObject(msg)
+ val type = jsonObj.getString(TYPE_KEY)
+ val message = jsonObj.getString(REQUEST_KEY)
+
+ if (havePendingRequest) {
+ postErrorMessage(reply, "The request already in progress", type)
+ return
+ }
+
+ replyChannel = reply
+ if (!isMainFrame) {
+ reportFailure("Requests from subframes are not supported", type)
+ return
+ }
+ val originScheme = sourceOrigin.scheme
+ if (originScheme == null || originScheme.lowercase() != "https") {
+ reportFailure("WebAuthn not permitted for current URL", type)
+ return
+ }
+
+ // Verify that origin belongs to your website,
+ // it's because the unknown origin may gain credential info.
+ // if (isUnknownOrigin(originScheme)) {
+ // return
+ // }
+
+ havePendingRequest = true
+ pendingRequestIsDoomed = false
+
+ // Use a temporary "replyCurrent" variable to send the data back, while
+ // resetting the main "replyChannel" variable to null so it’s ready for
+ // the next request.
+ val replyCurrent = replyChannel
+ if (replyCurrent == null) {
+ Log.i(TAG, "The reply channel was null, cannot continue")
+ return
+ }
+
+ when (type) {
+ CREATE_UNIQUE_KEY ->
+ this.coroutineScope.launch {
+ handleCreateFlow(credentialManagerHandler, message, replyCurrent)
+ }
+
+ GET_UNIQUE_KEY -> this.coroutineScope.launch {
+ handleGetFlow(credentialManagerHandler, message, replyCurrent)
+ }
+
+ else -> Log.i(TAG, "Incorrect request json")
+ }
+ }
+ }
+
+ private suspend fun handleCreateFlow(
+ credentialManagerHandler: CredentialManagerHandler,
+ message: String,
+ reply: ReplyChannel,
+ ) {
+ try {
+ havePendingRequest = false
+ pendingRequestIsDoomed = false
+ val response = credentialManagerHandler.createPasskey(message)
+ val successArray = ArrayList()
+ successArray.add("success")
+ successArray.add(JSONObject(response.registrationResponseJson))
+ successArray.add(CREATE_UNIQUE_KEY)
+ reply.send(JSONArray(successArray).toString())
+ replyChannel = null // setting initial replyChannel for the next request
+ } catch (e: CreateCredentialException) {
+ reportFailure(
+ "Error: ${e.errorMessage} w type: ${e.type} w obj: $e",
+ CREATE_UNIQUE_KEY
+ )
+ } catch (t: Throwable) {
+ reportFailure("Error: ${t.message}", CREATE_UNIQUE_KEY)
+ }
+ }
+
+ companion object {
+ /** INTERFACE_NAME is the name of the MessagePort that must be injected into pages. */
+ const val INTERFACE_NAME = "__webauthn_interface__"
+ const val TYPE_KEY = "type"
+ const val REQUEST_KEY = "request"
+ const val CREATE_UNIQUE_KEY = "create"
+ const val GET_UNIQUE_KEY = "get"
+ /** INJECTED_VAL is the minified version of the JavaScript code described at this class
+ * heading. The non minified form is found at credmanweb/javascript/encode.js.*/
+ const val INJECTED_VAL = """
+ var __webauthn_interface__,__webauthn_hooks__;!function(e){console.log("In the hook."),__webauthn_interface__.addEventListener("message",function e(n){var r=JSON.parse(n.data),t=r[2];"get"===t?o(r):"create"===t?u(r):console.log("Incorrect response format for reply")});var n=null,r=null,t=null,a=null;function o(e){if(null!==n&&null!==t){if("success"!=e[0]){var r=t;n=null,t=null,r(new DOMException(e[1],"NotAllowedError"));return}var a=i(e[1]),o=n;n=null,t=null,o(a)}}function l(e){var n=e.length%4;return Uint8Array.from(atob(e.replace(/-/g,"+").replace(/_/g,"/").padEnd(e.length+(0===n?0:4-n),"=")),function(e){return e.charCodeAt(0)}).buffer}function s(e){return btoa(Array.from(new Uint8Array(e),function(e){return String.fromCharCode(e)}).join("")).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+${'$'}/,"")}function u(e){if(null===r||null===a){console.log("Here: "+r+" and reject: "+a);return}if(console.log("Output back: "+e),"success"!=e[0]){var n=a;r=null,a=null,n(new DOMException(e[1],"NotAllowedError"));return}var t=i(e[1]),o=r;r=null,a=null,o(t)}function i(e){return console.log("Here is the response from credential manager: "+e),e.rawId=l(e.rawId),e.response.clientDataJSON=l(e.response.clientDataJSON),e.response.hasOwnProperty("attestationObject")&&(e.response.attestationObject=l(e.response.attestationObject)),e.response.hasOwnProperty("authenticatorData")&&(e.response.authenticatorData=l(e.response.authenticatorData)),e.response.hasOwnProperty("signature")&&(e.response.signature=l(e.response.signature)),e.response.hasOwnProperty("userHandle")&&(e.response.userHandle=l(e.response.userHandle)),e.getClientExtensionResults=function e(){return{}},e}e.create=function n(t){if(!("publicKey"in t))return e.originalCreateFunction(t);var o=new Promise(function(e,n){r=e,a=n}),l=t.publicKey;if(l.hasOwnProperty("challenge")){var u=s(l.challenge);l.challenge=u}if(l.hasOwnProperty("user")&&l.user.hasOwnProperty("id")){var i=s(l.user.id);l.user.id=i}var c=JSON.stringify({type:"create",request:l});return __webauthn_interface__.postMessage(c),o},e.get=function r(a){if(!("publicKey"in a))return e.originalGetFunction(a);var o=new Promise(function(e,r){n=e,t=r}),l=a.publicKey;if(l.hasOwnProperty("challenge")){var u=s(l.challenge);l.challenge=u}var i=JSON.stringify({type:"get",request:l});return __webauthn_interface__.postMessage(i),o},e.onReplyGet=o,e.CM_base64url_decode=l,e.CM_base64url_encode=s,e.onReplyCreate=u}(__webauthn_hooks__||(__webauthn_hooks__={})),__webauthn_hooks__.originalGetFunction=navigator.credentials.get,__webauthn_hooks__.originalCreateFunction=navigator.credentials.create,navigator.credentials.get=__webauthn_hooks__.get,navigator.credentials.create=__webauthn_hooks__.create,window.PublicKeyCredential=function(){},window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable=function(){return Promise.resolve(!1)};
+ """
+ }
+ // [END android_identity_create_listener_passkeys]
+
+ // Handles the get flow in a less error-prone way
+ private suspend fun handleGetFlow(
+ credentialManagerHandler: CredentialManagerHandler,
+ message: String,
+ reply: ReplyChannel,
+ ) {
+ try {
+ havePendingRequest = false
+ pendingRequestIsDoomed = false
+ val r = credentialManagerHandler.getPasskey(message)
+ val successArray = ArrayList()
+ successArray.add("success")
+ successArray.add(
+ JSONObject(
+ (r.credential as PublicKeyCredential).authenticationResponseJson
+ )
+ )
+ successArray.add(GET_UNIQUE_KEY)
+ reply.send(JSONArray(successArray).toString())
+ replyChannel = null // setting initial replyChannel for next request given temp 'reply'
+ } catch (e: GetCredentialException) {
+ reportFailure("Error: ${e.errorMessage} w type: ${e.type} w obj: $e", GET_UNIQUE_KEY)
+ } catch (t: Throwable) {
+ reportFailure("Error: ${t.message}", GET_UNIQUE_KEY)
+ }
+ }
+
+ /** Sends an error result to the page. */
+ private fun reportFailure(message: String, type: String) {
+ havePendingRequest = false
+ pendingRequestIsDoomed = false
+ val reply: ReplyChannel = replyChannel!! // verifies non null by throwing NPE
+ replyChannel = null
+ postErrorMessage(reply, message, type)
+ }
+
+ private fun postErrorMessage(reply: ReplyChannel, errorMessage: String, type: String) {
+ Log.i(TAG, "Sending error message back to the page via replyChannel $errorMessage")
+ val array: MutableList = ArrayList()
+ array.add("error")
+ array.add(errorMessage)
+ array.add(type)
+ reply.send(JSONArray(array).toString())
+ var toastMsg = errorMessage
+ Toast.makeText(this.activity.applicationContext, toastMsg, Toast.LENGTH_SHORT).show()
+ }
+
+ // [START android_identity_javascript_reply_channel]
+ // The setup for the reply channel allows communication with JavaScript.
+ private class JavaScriptReplyChannel(private val reply: JavaScriptReplyProxy) :
+ ReplyChannel {
+ override fun send(message: String?) {
+ try {
+ reply.postMessage(message!!)
+ } catch (t: Throwable) {
+ Log.i(TAG, "Reply failure due to: " + t.message)
+ }
+ }
+ }
+
+ // ReplyChannel is the interface where replies to the embedded site are
+ // sent. This allows for testing since AndroidX bans mocking its objects.
+ interface ReplyChannel {
+ fun send(message: String?)
+ }
+ // [END android_identity_javascript_reply_channel]
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/RestoreCredentialsFunctions.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/RestoreCredentialsFunctions.kt
new file mode 100644
index 000000000..8ce9700e7
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/RestoreCredentialsFunctions.kt
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.content.Context
+import androidx.credentials.ClearCredentialStateRequest
+import androidx.credentials.ClearCredentialStateRequest.Companion.TYPE_CLEAR_RESTORE_CREDENTIAL
+import androidx.credentials.CreateRestoreCredentialRequest
+import androidx.credentials.CredentialManager
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetRestoreCredentialOption
+
+class RestoreCredentialsFunctions(
+ private val context: Context,
+ private val credentialManager: CredentialManager,
+) {
+ suspend fun createRestoreKey(
+ createRestoreRequest: CreateRestoreCredentialRequest
+ ) {
+ // [START android_identity_restore_cred_create]
+ val credentialManager = CredentialManager.create(context)
+
+ // On a successful authentication create a Restore Key
+ // Pass in the context and CreateRestoreCredentialRequest object
+ val response = credentialManager.createCredential(context, createRestoreRequest)
+ // [END android_identity_restore_cred_create]
+ }
+
+ suspend fun getRestoreKey(
+ fetchAuthenticationJson: () -> String,
+ ) {
+ // [START android_identity_restore_cred_get]
+ // Fetch the Authentication JSON from server
+ val authenticationJson = fetchAuthenticationJson()
+
+ // Create the GetRestoreCredentialRequest object
+ val options = GetRestoreCredentialOption(authenticationJson)
+ val getRequest = GetCredentialRequest(listOf(options))
+
+ // The restore key can be fetched in two scenarios to
+ // 1. On the first launch of app on the device, fetch the Restore Key
+ // 2. In the onRestore callback (if the app implements the Backup Agent)
+ val response = credentialManager.getCredential(context, getRequest)
+ // [END android_identity_restore_cred_get]
+ }
+
+ suspend fun deleteRestoreKey() {
+ // [START android_identity_restore_cred_delete]
+ // Create a ClearCredentialStateRequest object
+ val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)
+
+ // On user log-out, clear the restore key
+ val response = credentialManager.clearCredentialState(clearRequest)
+ // [END android_identity_restore_cred_delete]
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SignInWithGoogleFunctions.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SignInWithGoogleFunctions.kt
new file mode 100644
index 000000000..28c58ef25
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SignInWithGoogleFunctions.kt
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.content.Context
+import android.util.Log
+import androidx.credentials.CredentialManager
+import androidx.credentials.CustomCredential
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.PasswordCredential
+import androidx.credentials.PublicKeyCredential
+import androidx.credentials.exceptions.GetCredentialException
+import com.google.android.libraries.identity.googleid.GetGoogleIdOption
+import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
+import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
+import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
+import kotlinx.coroutines.coroutineScope
+
+const val WEB_CLIENT_ID = ""
+class SignInWithGoogleFunctions(
+ context: Context,
+) {
+ private val credentialManager = CredentialManager.create(context)
+ private val activityContext = context
+ // Placeholder for TAG log value.
+ val TAG = ""
+
+ fun createGoogleIdOption(nonce: String): GetGoogleIdOption {
+ // [START android_identity_siwg_instantiate_request]
+ val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
+ .setFilterByAuthorizedAccounts(true)
+ .setServerClientId(WEB_CLIENT_ID)
+ .setAutoSelectEnabled(true)
+ // nonce string to use when generating a Google ID token
+ .setNonce(nonce)
+ .build()
+ // [END android_identity_siwg_instantiate_request]
+
+ return googleIdOption
+ }
+
+ private val googleIdOption = createGoogleIdOption("")
+
+ suspend fun signInUser() {
+ // [START android_identity_siwg_signin_flow_create_request]
+ val request: GetCredentialRequest = GetCredentialRequest.Builder()
+ .addCredentialOption(googleIdOption)
+ .build()
+
+ coroutineScope {
+ try {
+ val result = credentialManager.getCredential(
+ request = request,
+ context = activityContext,
+ )
+ handleSignIn(result)
+ } catch (e: GetCredentialException) {
+ // Handle failure
+ }
+ }
+ // [END android_identity_siwg_signin_flow_create_request]
+ }
+
+ // [START android_identity_siwg_signin_flow_handle_signin]
+ fun handleSignIn(result: GetCredentialResponse) {
+ // Handle the successfully returned credential.
+ val credential = result.credential
+ val responseJson: String
+
+ when (credential) {
+
+ // Passkey credential
+ is PublicKeyCredential -> {
+ // Share responseJson such as a GetCredentialResponse to your server to validate and
+ // authenticate
+ responseJson = credential.authenticationResponseJson
+ }
+
+ // Password credential
+ is PasswordCredential -> {
+ // Send ID and password to your server to validate and authenticate.
+ val username = credential.id
+ val password = credential.password
+ }
+
+ // GoogleIdToken credential
+ is CustomCredential -> {
+ if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
+ try {
+ // Use googleIdTokenCredential and extract the ID to validate and
+ // authenticate on your server.
+ val googleIdTokenCredential = GoogleIdTokenCredential
+ .createFrom(credential.data)
+ // You can use the members of googleIdTokenCredential directly for UX
+ // purposes, but don't use them to store or control access to user
+ // data. For that you first need to validate the token:
+ // pass googleIdTokenCredential.getIdToken() to the backend server.
+ // see [validation instructions](https://developers.google.com/identity/gsi/web/guides/verify-google-id-token)
+ } catch (e: GoogleIdTokenParsingException) {
+ Log.e(TAG, "Received an invalid google id token response", e)
+ }
+ } else {
+ // Catch any unrecognized custom credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+
+ else -> {
+ // Catch any unrecognized credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+ }
+ // [END android_identity_siwg_signin_flow_handle_signin]
+
+ fun createGoogleSignInWithGoogleOption(nonce: String): GetSignInWithGoogleOption {
+ // [START android_identity_siwg_get_siwg_option]
+ val signInWithGoogleOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder(
+ serverClientId = WEB_CLIENT_ID
+ ).setNonce(nonce)
+ .build()
+ // [END android_identity_siwg_get_siwg_option]
+
+ return signInWithGoogleOption
+ }
+
+ // [START android_identity_handle_siwg_option]
+ fun handleSignInWithGoogleOption(result: GetCredentialResponse) {
+ // Handle the successfully returned credential.
+ val credential = result.credential
+
+ when (credential) {
+ is CustomCredential -> {
+ if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
+ try {
+ // Use googleIdTokenCredential and extract id to validate and
+ // authenticate on your server.
+ val googleIdTokenCredential = GoogleIdTokenCredential
+ .createFrom(credential.data)
+ } catch (e: GoogleIdTokenParsingException) {
+ Log.e(TAG, "Received an invalid google id token response", e)
+ }
+ } else {
+ // Catch any unrecognized credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+
+ else -> {
+ // Catch any unrecognized credential type here.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+ }
+ // [END android_identity_handle_siwg_option]
+
+ fun googleIdOptionFalseFilter() {
+ // [START android_identity_siwg_instantiate_request_2]
+ val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
+ .setFilterByAuthorizedAccounts(false)
+ .setServerClientId(WEB_CLIENT_ID)
+ .build()
+ // [END android_identity_siwg_instantiate_request_2]
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SingleTap.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SingleTap.kt
new file mode 100644
index 000000000..b61ddbe5c
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SingleTap.kt
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.os.Build.VERSION_CODES
+import android.util.Log
+import androidx.activity.ComponentActivity
+import androidx.annotation.RequiresApi
+import androidx.credentials.CreatePublicKeyCredentialRequest
+import androidx.credentials.provider.BeginCreatePublicKeyCredentialRequest
+import androidx.credentials.provider.BeginGetPublicKeyCredentialOption
+import androidx.credentials.provider.BiometricPromptData
+import androidx.credentials.provider.CallingAppInfo
+import androidx.credentials.provider.PendingIntentHandler
+
+class SingleTap : ComponentActivity() {
+ private val x: Any? = null
+ private val TAG: String = ""
+
+ private fun passkeyCreation(
+ request: BeginCreatePublicKeyCredentialRequest,
+ passwordCount: Int,
+ passkeyCount: Int
+ ) {
+ val option = null
+ val origin = null
+ val responseBuilder = null
+ val autoSelectEnabled = null
+ val allowedAuthenticator = 0
+
+ val y =
+ // [START android_identity_single_tap_set_biometric_prompt_data]
+ PublicKeyCredentialEntry(
+ // other properties...
+
+ biometricPromptData = BiometricPromptData(
+ allowedAuthenticators = allowedAuthenticator
+ )
+ )
+ // [END android_identity_single_tap_set_biometric_prompt_data]
+
+ when (x) {
+ // [START android_identity_single_tap_pk_creation]
+ is BeginCreatePublicKeyCredentialRequest -> {
+ Log.i(TAG, "Request is passkey type")
+ return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
+ }
+ // [END android_identity_single_tap_pk_creation]
+
+ // [START android_identity_single_tap_pk_flow]
+ is BeginGetPublicKeyCredentialOption -> {
+ // ... other logic
+
+ populatePasskeyData(
+ origin,
+ option,
+ responseBuilder,
+ autoSelectEnabled,
+ allowedAuthenticator
+ )
+
+ // ... other logic as needed
+ }
+ // [END android_identity_single_tap_pk_flow]
+ }
+ }
+
+ private fun handleCreatePasskeyQuery(
+ request: BeginCreatePublicKeyCredentialRequest,
+ passwordCount: Int,
+ passkeyCount: Int
+ ) {
+ val allowedAuthenticator = 0
+
+ // [START android_identity_single_tap_create_entry]
+ val createEntry = CreateEntry(
+ // Additional properties...
+ biometricPromptData = BiometricPromptData(
+ allowedAuthenticators = allowedAuthenticator
+ ),
+ )
+ // [END android_identity_single_tap_create_entry]
+ }
+
+ @RequiresApi(VERSION_CODES.M)
+ private fun handleCredentialEntrySelection(
+ accountId: String = "",
+ createPasskey: (String, CallingAppInfo, ByteArray?, String) -> Unit
+ ) {
+ // [START android_identity_single_tap_handle_credential_entry]
+ val createRequest = PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
+ if (createRequest == null) {
+ Log.i(TAG, "request is null")
+ setUpFailureResponseAndFinish("Unable to extract request from intent")
+ return
+ }
+ // Other logic...
+
+ val biometricPromptResult = createRequest.biometricPromptResult
+
+ // Add your logic based on what needs to be done
+ // after getting biometrics
+
+ if (createRequest.callingRequest is CreatePublicKeyCredentialRequest) {
+ val publicKeyRequest: CreatePublicKeyCredentialRequest =
+ createRequest.callingRequest as CreatePublicKeyCredentialRequest
+
+ if (biometricPromptResult == null) {
+ // Do your own authentication flow, if needed
+ } else if (biometricPromptResult.isSuccessful) {
+ createPasskey(
+ publicKeyRequest.requestJson,
+ createRequest.callingAppInfo,
+ publicKeyRequest.clientDataHash,
+ accountId
+ )
+ } else {
+ val error = biometricPromptResult.authenticationError
+ // Process the error
+ }
+
+ // Other logic...
+ }
+ // [END android_identity_single_tap_handle_credential_entry]
+ }
+
+ @RequiresApi(VERSION_CODES.M)
+ private fun retrieveProviderGetCredentialRequest(
+ validatePasskey: (String, String, String, String, String, String, String) -> Unit,
+ publicKeyRequest: CreatePublicKeyCredentialRequest,
+ origin: String,
+ uid: String,
+ passkey: PK,
+ credId: String,
+ privateKey: String,
+ ) {
+ // [START android_identity_single_tap_get_cred_request]
+ val getRequest =
+ PendingIntentHandler.retrieveProviderGetCredentialRequest(intent)
+
+ if (getRequest == null) {
+ Log.i(TAG, "request is null")
+ setUpFailureResponseAndFinish("Unable to extract request from intent")
+ return
+ }
+
+ // Other logic...
+
+ val biometricPromptResult = getRequest.biometricPromptResult
+
+ // Add your logic based on what needs to be done
+ // after getting biometrics
+
+ if (biometricPromptResult == null) {
+ // Do your own authentication flow, if necessary
+ } else if (biometricPromptResult.isSuccessful) {
+
+ Log.i(TAG, "The response from the biometricPromptResult was ${biometricPromptResult.authenticationResult?.authenticationType}")
+
+ validatePasskey(
+ publicKeyRequest.requestJson,
+ origin,
+ packageName,
+ uid,
+ passkey.username,
+ credId,
+ privateKey
+ )
+ } else {
+ val error = biometricPromptResult.authenticationError
+ // Process the error
+ }
+
+ // Other logic...
+ // [END android_identity_single_tap_get_cred_request]
+ }
+
+ private fun CreateEntry(biometricPromptData: BiometricPromptData) {}
+
+ private fun PublicKeyCredentialEntry(biometricPromptData: BiometricPromptData) {}
+
+ private fun populatePasskeyData(
+ origin: Any?,
+ option: Any?,
+ responseBuilder: Any?,
+ autoSelectEnabled: Any?,
+ allowedAuthenticator: Any?
+ ) {}
+
+ private fun setUpFailureResponseAndFinish(str: String) {}
+}
+
+data class PK(
+ val username: String,
+)
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SmartLockToCredMan.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SmartLockToCredMan.kt
new file mode 100644
index 000000000..f37f6d9da
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/SmartLockToCredMan.kt
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.content.Context
+import android.util.Log
+import androidx.credentials.CredentialManager
+import androidx.credentials.GetCredentialRequest
+import androidx.credentials.GetCredentialResponse
+import androidx.credentials.GetPasswordOption
+import androidx.credentials.PasswordCredential
+import androidx.credentials.exceptions.GetCredentialCancellationException
+import androidx.credentials.exceptions.GetCredentialCustomException
+import androidx.credentials.exceptions.GetCredentialException
+import androidx.credentials.exceptions.GetCredentialInterruptedException
+import androidx.credentials.exceptions.GetCredentialProviderConfigurationException
+import androidx.credentials.exceptions.GetCredentialUnknownException
+import androidx.credentials.exceptions.GetCredentialUnsupportedException
+import androidx.credentials.exceptions.NoCredentialException
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.launch
+
+class SmartLockToCredMan(
+ private val credentialManager: CredentialManager,
+ private val activityContext: Context,
+ private val coroutineScope: CoroutineScope,
+) {
+ // [START android_identity_init_password_option]
+ // Retrieves the user's saved password for your app from their
+ // password provider.
+ val getPasswordOption = GetPasswordOption()
+ // [END android_identity_init_password_option]
+
+ // [START android_identity_get_cred_request]
+ val getCredRequest = GetCredentialRequest(
+ listOf(getPasswordOption)
+ )
+ // [END android_identity_get_cred_request]
+
+ val TAG: String = "tag"
+
+ // [START android_identity_launch_sign_in_flow]
+ fun launchSignInFlow() {
+ coroutineScope.launch {
+ try {
+ // Attempt to retrieve the credential from the Credential Manager.
+ val result = credentialManager.getCredential(
+ // Use an activity-based context to avoid undefined system UI
+ // launching behavior.
+ context = activityContext,
+ request = getCredRequest
+ )
+
+ // Process the successfully retrieved credential.
+ handleSignIn(result)
+ } catch (e: GetCredentialException) {
+ // Handle any errors that occur during the credential retrieval
+ // process.
+ handleFailure(e)
+ }
+ }
+ }
+
+ private fun handleSignIn(result: GetCredentialResponse) {
+ // Extract the credential from the response.
+ val credential = result.credential
+
+ // Determine the type of credential and handle it accordingly.
+ when (credential) {
+ is PasswordCredential -> {
+ val username = credential.id
+ val password = credential.password
+
+ // Use the extracted username and password to perform
+ // authentication.
+ }
+
+ else -> {
+ // Handle unrecognized credential types.
+ Log.e(TAG, "Unexpected type of credential")
+ }
+ }
+ }
+
+ private fun handleFailure(e: GetCredentialException) {
+ // Handle specific credential retrieval errors.
+ when (e) {
+ is GetCredentialCancellationException -> {
+ /* This exception is thrown when the user intentionally cancels
+ the credential retrieval operation. Update the application's state
+ accordingly. */
+ }
+
+ is GetCredentialCustomException -> {
+ /* This exception is thrown when a custom error occurs during the
+ credential retrieval flow. Refer to the documentation of the
+ third-party SDK used to create the GetCredentialRequest for
+ handling this exception. */
+ }
+
+ is GetCredentialInterruptedException -> {
+ /* This exception is thrown when an interruption occurs during the
+ credential retrieval flow. Determine whether to retry the
+ operation or proceed with an alternative authentication method. */
+ }
+
+ is GetCredentialProviderConfigurationException -> {
+ /* This exception is thrown when there is a mismatch in
+ configurations for the credential provider. Verify that the
+ provider dependency is included in the manifest and that the
+ required system services are enabled. */
+ }
+
+ is GetCredentialUnknownException -> {
+ /* This exception is thrown when the credential retrieval
+ operation fails without providing any additional details. Handle
+ the error appropriately based on the application's context. */
+ }
+
+ is GetCredentialUnsupportedException -> {
+ /* This exception is thrown when the device does not support the
+ Credential Manager feature. Inform the user that credential-based
+ authentication is unavailable and guide them to an alternative
+ authentication method. */
+ }
+
+ is NoCredentialException -> {
+ /* This exception is thrown when there are no viable credentials
+ available for the user. Prompt the user to sign up for an account
+ or provide an alternative authentication method. Upon successful
+ authentication, store the login information using
+ androidx.credentials.CredentialManager.createCredential to
+ facilitate easier sign-in the next time. */
+ }
+
+ else -> {
+ // Handle unexpected exceptions.
+ Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
+ }
+ }
+ }
+ // [END android_identity_launch_sign_in_flow]
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/WebViewMainActivity.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/WebViewMainActivity.kt
new file mode 100644
index 000000000..935da4971
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/WebViewMainActivity.kt
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager
+
+import android.graphics.Bitmap
+import android.os.Bundle
+import android.webkit.WebView
+import android.webkit.WebViewClient
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.viewinterop.AndroidView
+import androidx.webkit.WebViewCompat
+import androidx.webkit.WebViewFeature
+import kotlinx.coroutines.CoroutineScope
+
+class WebViewMainActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // [START android_identity_initialize_the_webview]
+ val credentialManagerHandler = CredentialManagerHandler(this)
+
+ setContent {
+ val coroutineScope = rememberCoroutineScope()
+ AndroidView(
+ factory = {
+ WebView(it).apply {
+ settings.javaScriptEnabled = true
+
+ // Test URL:
+ val url = "https://passkeys-codelab.glitch.me/"
+ val listenerSupported = WebViewFeature.isFeatureSupported(
+ WebViewFeature.WEB_MESSAGE_LISTENER
+ )
+ if (listenerSupported) {
+ // Inject local JavaScript that calls Credential Manager.
+ hookWebAuthnWithListener(
+ this, this@WebViewMainActivity,
+ coroutineScope, credentialManagerHandler
+ )
+ } else {
+ // Fallback routine for unsupported API levels.
+ }
+ loadUrl(url)
+ }
+ }
+ )
+ }
+ // [END android_identity_initialize_the_webview]
+ }
+
+ /**
+ * Connects the local app logic with the web page via injection of javascript through a
+ * WebListener. Handles ensuring the [PasskeyWebListener] is hooked up to the webView page
+ * if compatible.
+ */
+ fun hookWebAuthnWithListener(
+ webView: WebView,
+ activity: WebViewMainActivity,
+ coroutineScope: CoroutineScope,
+ credentialManagerHandler: CredentialManagerHandler
+ ) {
+ // [START android_identity_create_webview_object]
+ val passkeyWebListener = PasskeyWebListener(activity, coroutineScope, credentialManagerHandler)
+
+ val webViewClient = object : WebViewClient() {
+ override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
+ super.onPageStarted(view, url, favicon)
+ webView.evaluateJavascript(PasskeyWebListener.INJECTED_VAL, null)
+ }
+ }
+
+ webView.webViewClient = webViewClient
+ // [END android_identity_create_webview_object]
+
+ // [START android_identity_set_web]
+ val rules = setOf("*")
+ if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
+ WebViewCompat.addWebMessageListener(
+ webView, PasskeyWebListener.INTERFACE_NAME,
+ rules, passkeyWebListener
+ )
+ }
+ // [END android_identity_set_web]
+ }
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Color.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Color.kt
new file mode 100644
index 000000000..027a2e3fd
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Color.kt
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager.ui.theme
+
+import androidx.compose.ui.graphics.Color
+
+val Purple80 = Color(0xFFD0BCFF)
+val PurpleGrey80 = Color(0xFFCCC2DC)
+val Pink80 = Color(0xFFEFB8C8)
+
+val Purple40 = Color(0xFF6650a4)
+val PurpleGrey40 = Color(0xFF625b71)
+val Pink40 = Color(0xFF7D5260)
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Theme.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Theme.kt
new file mode 100644
index 000000000..aa8223784
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Theme.kt
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager.ui.theme
+
+import android.os.Build
+import androidx.compose.foundation.isSystemInDarkTheme
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.darkColorScheme
+import androidx.compose.material3.dynamicDarkColorScheme
+import androidx.compose.material3.dynamicLightColorScheme
+import androidx.compose.material3.lightColorScheme
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.platform.LocalContext
+
+private val DarkColorScheme = darkColorScheme(
+ primary = Purple80,
+ secondary = PurpleGrey80,
+ tertiary = Pink80
+)
+
+private val LightColorScheme = lightColorScheme(
+ primary = Purple40,
+ secondary = PurpleGrey40,
+ tertiary = Pink40
+)
+
+@Composable
+fun SnippetsTheme(
+ darkTheme: Boolean = isSystemInDarkTheme(),
+ // Dynamic color is available on Android 12+
+ dynamicColor: Boolean = true,
+ content: @Composable () -> Unit
+) {
+ val colorScheme = when {
+ dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
+ val context = LocalContext.current
+ if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
+ }
+
+ darkTheme -> DarkColorScheme
+ else -> LightColorScheme
+ }
+
+ MaterialTheme(
+ colorScheme = colorScheme,
+ typography = Typography,
+ content = content
+ )
+}
diff --git a/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Type.kt b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Type.kt
new file mode 100644
index 000000000..5e0eaf6b2
--- /dev/null
+++ b/identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/ui/theme/Type.kt
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.identity.credentialmanager.ui.theme
+
+import androidx.compose.material3.Typography
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.font.FontFamily
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.sp
+
+// Set of Material typography styles to start with
+val Typography = Typography(
+ bodyLarge = TextStyle(
+ fontFamily = FontFamily.Default,
+ fontWeight = FontWeight.Normal,
+ fontSize = 16.sp,
+ lineHeight = 24.sp,
+ letterSpacing = 0.5.sp
+ )
+)
diff --git a/identity/credentialmanager/src/main/jsonSnippets.json b/identity/credentialmanager/src/main/jsonSnippets.json
new file mode 100644
index 000000000..9cbccd4e1
--- /dev/null
+++ b/identity/credentialmanager/src/main/jsonSnippets.json
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+{
+ "snippets": [
+ {
+ "DigitalAssetLinking":
+ // Digital asset linking
+ // [START android_identity_assetlinks_json]
+ [
+ {
+ "relation": [
+ "delegate_permission/common.handle_all_urls",
+ "delegate_permission/common.get_login_creds"
+ ],
+ "target": {
+ "namespace": "android_app",
+ "package_name": "com.example.android",
+ "sha256_cert_fingerprints": [
+ SHA_HEX_VALUE
+ ]
+ }
+ }
+ ]
+ // [END android_identity_assetlinks_json]
+ },
+
+ {
+ "FormatJsonRequestPasskey":
+ // JSON request format
+ // [START android_identity_format_json_request_passkey]
+ {
+ "challenge": "T1xCsnxM2DNL2KdK5CLa6fMhD7OBqho6syzInk_n-Uo",
+ "allowCredentials": [],
+ "timeout": 1800000,
+ "userVerification": "required",
+ "rpId": "https://passkeys-codelab.glitch.me/"
+ }
+ // [END android_identity_format_json_request_passkey]
+ },
+
+ {
+ "FormatJsonResponsePasskey":
+ // JSON response format
+ // [START android_identity_format_json_response_passkey]
+ {
+ "id": "KEDetxZcUfinhVi6Za5nZQ",
+ "type": "public-key",
+ "rawId": "KEDetxZcUfinhVi6Za5nZQ",
+ "response": {
+ "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiVDF4Q3NueE0yRE5MMktkSzVDTGE2Zk1oRDdPQnFobzZzeXpJbmtfbi1VbyIsIm9yaWdpbiI6ImFuZHJvaWQ6YXBrLWtleS1oYXNoOk1MTHpEdll4UTRFS1R3QzZVNlpWVnJGUXRIOEdjVi0xZDQ0NEZLOUh2YUkiLCJhbmRyb2lkUGFja2FnZU5hbWUiOiJjb20uZ29vZ2xlLmNyZWRlbnRpYWxtYW5hZ2VyLnNhbXBsZSJ9",
+ "authenticatorData": "j5r_fLFhV-qdmGEwiukwD5E_5ama9g0hzXgN8thcFGQdAAAAAA",
+ "signature": "MEUCIQCO1Cm4SA2xiG5FdKDHCJorueiS04wCsqHhiRDbbgITYAIgMKMFirgC2SSFmxrh7z9PzUqr0bK1HZ6Zn8vZVhETnyQ",
+ "userHandle": "2HzoHm_hY0CjuEESY9tY6-3SdjmNHOoNqaPDcZGzsr0"
+ }
+ }
+ // [END android_identity_format_json_response_passkey]
+ },
+ {
+ "CreatePasskeyJsonRequest":
+ // Json request for creating a passkey
+ // [START android_identity_create_passkey_request_json]
+ {
+ "challenge": "abc123",
+ "rp": {
+ "name": "Credential Manager example",
+ "id": "credential-manager-test.example.com"
+ },
+ "user": {
+ "id": "def456",
+ "name": "helloandroid@gmail.com",
+ "displayName": "helloandroid@gmail.com"
+ },
+ "pubKeyCredParams": [
+ {
+ "type": "public-key",
+ "alg": -7
+ },
+ {
+ "type": "public-key",
+ "alg": -257
+ }
+ ],
+ "timeout": 1800000,
+ "attestation": "none",
+ "excludeCredentials": [
+ {
+ "id": "ghi789",
+ "type": "public-key"
+ },
+ {
+ "id": "jkl012",
+ "type": "public-key"
+ }
+ ],
+ "authenticatorSelection": {
+ "authenticatorAttachment": "platform",
+ "requireResidentKey": true,
+ "residentKey": "required",
+ "userVerification": "required"
+ }
+ }
+ // [END android_identity_create_passkey_request_json]
+ },
+ {
+ "CreatePasskeyHandleJsonResponse":
+ // Json response when creating a passkey
+ // [START android_identity_create_passkey_response_json]
+ {
+ "id": "KEDetxZcUfinhVi6Za5nZQ",
+ "type": "public-key",
+ "rawId": "KEDetxZcUfinhVi6Za5nZQ",
+ "response": {
+ "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoibmhrUVhmRTU5SmI5N1Z5eU5Ka3ZEaVh1Y01Fdmx0ZHV2Y3JEbUdyT0RIWSIsIm9yaWdpbiI6ImFuZHJvaWQ6YXBrLWtleS1oYXNoOk1MTHpEdll4UTRFS1R3QzZVNlpWVnJGUXRIOEdjVi0xZDQ0NEZLOUh2YUkiLCJhbmRyb2lkUGFja2FnZU5hbWUiOiJjb20uZ29vZ2xlLmNyZWRlbnRpYWxtYW5hZ2VyLnNhbXBsZSJ9",
+ "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViUj5r_fLFhV-qdmGEwiukwD5E_5ama9g0hzXgN8thcFGRdAAAAAAAAAAAAAAAAAAAAAAAAAAAAEChA3rcWXFH4p4VYumWuZ2WlAQIDJiABIVgg4RqZaJyaC24Pf4tT-8ONIZ5_Elddf3dNotGOx81jj3siWCAWXS6Lz70hvC2g8hwoLllOwlsbYatNkO2uYFO-eJID6A"
+ }
+ }
+ // [END android_identity_create_passkey_response_json]
+ }
+
+ ]
+}
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/othersnippets b/identity/credentialmanager/src/main/othersnippets
new file mode 100644
index 000000000..3a82843c6
--- /dev/null
+++ b/identity/credentialmanager/src/main/othersnippets
@@ -0,0 +1,18 @@
+// [START android_identity_apk_key_hash]
+android:apk-key-hash:
+// [END android_identity_apk_key_hash]
+
+// [START android_identity_keytool_sign]
+keytool -list -keystore
+// [END android_identity_keytool_sign]
+
+// [START android_identity_fingerprint_decode_python]
+import binascii
+import base64
+fingerprint = '91:F7:CB:F9:D6:81:53:1B:C7:A5:8F:B8:33:CC:A1:4D:AB:ED:E5:09:C5'
+print("android:apk-key-hash:" + base64.urlsafe_b64encode(binascii.a2b_hex(fingerprint.replace(':', ''))).decode('utf8').replace('=', ''))
+// [END android_identity_fingerprint_decode_python]
+
+// [START android_identity_fingerprint_decoded]
+android:apk-key-hash:kffL-daBUxvHpY-4M8yhTavt5QnFEI2LsexohxrGPYU
+// [END android_identity_fingerprint_decoded]
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/drawable-v24/ic_launcher_foreground.xml b/identity/credentialmanager/src/main/res/drawable-v24/ic_launcher_foreground.xml
new file mode 100644
index 000000000..2b068d114
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/drawable-v24/ic_launcher_foreground.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/drawable/ic_launcher_background.xml b/identity/credentialmanager/src/main/res/drawable/ic_launcher_background.xml
new file mode 100644
index 000000000..07d5da9cb
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/drawable/ic_launcher_background.xml
@@ -0,0 +1,170 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/identity/credentialmanager/src/main/res/layout-v34/xmlsnippets.xml b/identity/credentialmanager/src/main/res/layout-v34/xmlsnippets.xml
new file mode 100644
index 000000000..9be5cf21f
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/layout-v34/xmlsnippets.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
new file mode 100644
index 000000000..6f3b755bf
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
new file mode 100644
index 000000000..6f3b755bf
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher.webp b/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher.webp
new file mode 100644
index 000000000..c209e78ec
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher_round.webp
new file mode 100644
index 000000000..b2dfe3d1b
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher.webp b/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher.webp
new file mode 100644
index 000000000..4f0f1d64e
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher_round.webp
new file mode 100644
index 000000000..62b611da0
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher.webp b/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher.webp
new file mode 100644
index 000000000..948a3070f
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
new file mode 100644
index 000000000..1b9a6956b
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher.webp
new file mode 100644
index 000000000..28d4b77f9
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
new file mode 100644
index 000000000..9287f5083
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
new file mode 100644
index 000000000..aa7d6427e
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ
diff --git a/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
new file mode 100644
index 000000000..9126ae37c
Binary files /dev/null and b/identity/credentialmanager/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ
diff --git a/identity/credentialmanager/src/main/res/values/colors.xml b/identity/credentialmanager/src/main/res/values/colors.xml
new file mode 100644
index 000000000..f8c6127d3
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/values/colors.xml
@@ -0,0 +1,10 @@
+
+
+ #FFBB86FC
+ #FF6200EE
+ #FF3700B3
+ #FF03DAC5
+ #FF018786
+ #FF000000
+ #FFFFFFFF
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/values/strings.xml b/identity/credentialmanager/src/main/res/values/strings.xml
new file mode 100644
index 000000000..8f5fb8e80
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/values/strings.xml
@@ -0,0 +1,10 @@
+
+ credentialmanager
+ // [START android_identity_assetlinks_app_association]
+
+ [{
+ \"include\": \"https://signin.example.com/.well-known/assetlinks.json/"
+ }]
+
+ // [END android_identity_assetlinks_app_association]
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/values/themes.xml b/identity/credentialmanager/src/main/res/values/themes.xml
new file mode 100644
index 000000000..65078ebe0
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/values/themes.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/identity/credentialmanager/src/main/res/xml/provider.xml b/identity/credentialmanager/src/main/res/xml/provider.xml
new file mode 100644
index 000000000..81bdbd01d
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/xml/provider.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
diff --git a/identity/credentialmanager/src/main/res/xml/provider_settings.xml b/identity/credentialmanager/src/main/res/xml/provider_settings.xml
new file mode 100644
index 000000000..698ba5f6c
--- /dev/null
+++ b/identity/credentialmanager/src/main/res/xml/provider_settings.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
diff --git a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/HomeViewModel.kt b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/HomeViewModel.kt
index 9dea9a720..0b804c985 100644
--- a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/HomeViewModel.kt
+++ b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/HomeViewModel.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/Repository.kt b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/Repository.kt
index babf205c6..0d6252d77 100644
--- a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/Repository.kt
+++ b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/Repository.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package com.example.android.coroutines.testing
+import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -23,7 +24,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
-import java.util.concurrent.atomic.AtomicBoolean
// [START coroutine_test_repo_dispatcher_injection]
// Example class demonstrating dispatcher use cases
diff --git a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserRepository.kt b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserRepository.kt
index b53dee883..26b888543 100644
--- a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserRepository.kt
+++ b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserRepository.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserState.kt b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserState.kt
index e06b7240c..7ac89ce50 100644
--- a/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserState.kt
+++ b/kotlin/src/main/kotlin/com/example/android/coroutines/testing/UserState.kt
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.example.android.coroutines.testing.scope
import kotlinx.coroutines.CoroutineScope
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/CreatingYourOwn.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/CreatingYourOwn.kt
index 727a7c8a7..05a6794c0 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/CreatingYourOwn.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/CreatingYourOwn.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatcherTypesTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatcherTypesTest.kt
index bbe6b117e..81f206ad1 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatcherTypesTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatcherTypesTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatchersOutsideTests.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatchersOutsideTests.kt
index 2f3228ccf..c586f2570 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatchersOutsideTests.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/DispatchersOutsideTests.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@ import org.junit.runner.RunWith
// Helper function to let code below compile
private fun ExampleRepository(): Repository = Repository(Dispatchers.IO)
-
// [START coroutine_test_repo_with_rule_blank]
class ExampleRepository(private val ioDispatcher: CoroutineDispatcher) { /* ... */ }
@@ -89,5 +88,3 @@ class DispatchersOutsideTests {
}
// [END coroutine_test_repo_without_rule]
}
-
-
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTest.kt
index d4efc2e58..b588e90ca 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTestUsingRule.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTestUsingRule.kt
index 54a5a9bdd..b7b926edb 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTestUsingRule.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/HomeViewModelTestUsingRule.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/RepositoryTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/RepositoryTest.kt
index ec0a47c49..5cd4b2bab 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/RepositoryTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/RepositoryTest.kt
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.example.android.coroutines.testing
import kotlinx.coroutines.test.StandardTestDispatcher
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/StandardTestDispatcherTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/StandardTestDispatcherTest.kt
index bf749c095..da07e4936 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/StandardTestDispatcherTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/StandardTestDispatcherTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,4 +51,4 @@ class StandardTestDispatcherTest_Fixed {
assertEquals(listOf("Alice", "Bob"), userRepo.getAllUsers()) // ✅ Passes
}
// [END coroutine_test_standard_fixed]
-}
\ No newline at end of file
+}
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/SuspendingFunctionTests.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/SuspendingFunctionTests.kt
index 90e0ab79f..2dc83b6a3 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/SuspendingFunctionTests.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/SuspendingFunctionTests.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UnconfinedTestDispatcherTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UnconfinedTestDispatcherTest.kt
index ae488974a..b2230c034 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UnconfinedTestDispatcherTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UnconfinedTestDispatcherTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Google LLC
+ * Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UserStateTest.kt b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UserStateTest.kt
index e2c625e4e..3b2ec514b 100644
--- a/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UserStateTest.kt
+++ b/kotlin/src/test/kotlin/com/example/android/coroutines/testing/UserStateTest.kt
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
@file:OptIn(ExperimentalCoroutinesApi::class)
package com.example.android.coroutines.testing
diff --git a/misc/build.gradle.kts b/misc/build.gradle.kts
index de867bb88..cb0905420 100644
--- a/misc/build.gradle.kts
+++ b/misc/build.gradle.kts
@@ -57,14 +57,24 @@ dependencies {
implementation(libs.androidx.compose.ui.util)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
+ implementation(libs.androidx.media3.common)
+ implementation(libs.androidx.media3.exoplayer)
+ implementation(libs.androidx.tracing)
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.kotlinx.serialization.json)
ksp(libs.hilt.compiler)
+ implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.lifecycle.runtime)
+ implementation(libs.androidx.window)
+ implementation(libs.androidx.startup.runtime)
+ implementation(libs.androidx.window.java)
+ implementation(libs.appcompat)
testImplementation(libs.junit)
+ testImplementation(kotlin("test"))
+ androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
diff --git a/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestJavaSnippets.java b/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestJavaSnippets.java
new file mode 100644
index 000000000..ba249b0e6
--- /dev/null
+++ b/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestJavaSnippets.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets;
+
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.test.core.app.ActivityScenario;
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+import org.junit.Rule;
+import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+
+public class DeviceCompatibilityModeTestJavaSnippets {
+
+ // [START android_device_compatibility_mode_assert_isLetterboxed_java]
+ @Rule
+ public ActivityScenarioRule rule = new ActivityScenarioRule<>(MainActivity.class);
+
+ @Test
+ public void activity_launched_notLetterBoxed() {
+ try (ActivityScenario scenario =
+ ActivityScenario.launch(MainActivity.class)) {
+ scenario.onActivity( activity -> {
+ assertFalse(activity.isLetterboxed());
+ });
+ }
+ }
+ // [END android_device_compatibility_mode_assert_isLetterboxed_java]
+
+
+ // Class used by snippets.
+
+ class MainActivity extends AppCompatActivity {
+ public boolean isLetterboxed() {
+ return true;
+ }
+ }
+}
diff --git a/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestKotlinSnippets.kt b/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestKotlinSnippets.kt
new file mode 100644
index 000000000..65f9a7f1f
--- /dev/null
+++ b/misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestKotlinSnippets.kt
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets
+
+import androidx.appcompat.app.AppCompatActivity
+import androidx.test.ext.junit.rules.ActivityScenarioRule
+import org.junit.Assert.assertFalse
+import org.junit.Rule
+import org.junit.Test
+
+class DeviceCompatibilityModeTestKotlinSnippets {
+
+ // [START android_device_compatibility_mode_assert_isLetterboxed_kotlin]
+ @get:Rule
+ val activityRule = ActivityScenarioRule(MainActivity::class.java)
+
+ @Test
+ fun activity_launched_notLetterBoxed() {
+ activityRule.scenario.onActivity {
+ assertFalse(it.isLetterboxed())
+ }
+ }
+ // [END android_device_compatibility_mode_assert_isLetterboxed_kotlin]
+
+ // Class used by snippets.
+
+ class MainActivity : AppCompatActivity() {
+ fun isLetterboxed(): Boolean {
+ return true
+ }
+ }
+}
diff --git a/misc/src/main/AndroidManifest.xml b/misc/src/main/AndroidManifest.xml
index 0a7a5c6f0..2ad7db1e7 100644
--- a/misc/src/main/AndroidManifest.xml
+++ b/misc/src/main/AndroidManifest.xml
@@ -45,6 +45,17 @@
+
+
+
+
-
\ No newline at end of file
+
diff --git a/misc/src/main/java/com/example/snippets/ActivityEmbeddingJavaSnippets.java b/misc/src/main/java/com/example/snippets/ActivityEmbeddingJavaSnippets.java
new file mode 100644
index 000000000..a0ed0595b
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/ActivityEmbeddingJavaSnippets.java
@@ -0,0 +1,395 @@
+package com.example.snippets;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.os.Build.VERSION_CODES;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.OptIn;
+import androidx.annotation.RequiresApi;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.content.ContextCompat;
+import androidx.startup.Initializer;
+import androidx.window.WindowSdkExtensions;
+import androidx.window.core.ExperimentalWindowApi;
+import androidx.window.embedding.ActivityEmbeddingController;
+import androidx.window.embedding.ActivityFilter;
+import androidx.window.embedding.ActivityRule;
+import androidx.window.embedding.DividerAttributes;
+import androidx.window.embedding.EmbeddingAspectRatio;
+import androidx.window.embedding.RuleController;
+import androidx.window.embedding.SplitAttributes;
+import androidx.window.embedding.SplitAttributes.SplitType;
+import androidx.window.embedding.SplitController;
+import androidx.window.embedding.SplitPairFilter;
+import androidx.window.embedding.SplitPairRule;
+import androidx.window.embedding.SplitPinRule;
+import androidx.window.embedding.SplitPlaceholderRule;
+import androidx.window.embedding.SplitRule;
+import androidx.window.java.embedding.SplitControllerCallbackAdapter;
+import androidx.window.layout.FoldingFeature;
+import androidx.window.layout.WindowMetrics;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class ActivityEmbeddingJavaSnippets {
+
+ static class SplitAttributesCalculatorSnippetsActivity extends AppCompatActivity {
+
+ @RequiresApi(api=VERSION_CODES.N)
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // [START android_activity_embedding_split_attributes_calculator_java]
+ if (WindowSdkExtensions.getInstance().getExtensionVersion() >= 2) {
+ SplitController.getInstance(this).setSplitAttributesCalculator(params -> {
+ Configuration parentConfiguration = params.getParentConfiguration();
+ SplitAttributes.Builder builder = new SplitAttributes.Builder();
+ if (parentConfiguration.screenWidthDp >= 840) {
+ // Side-by-side dual-pane layout for wide displays.
+ return builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LOCALE)
+ .build();
+ } else if (parentConfiguration.screenHeightDp >= 600) {
+ // Horizontal split for tall displays.
+ return builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.BOTTOM_TO_TOP)
+ .build();
+ } else {
+ // Fallback to expand the secondary container.
+ return builder
+ .setSplitType(SplitType.SPLIT_TYPE_EXPAND)
+ .build();
+ }
+ });
+ }
+ // [END android_activity_embedding_split_attributes_calculator_java]
+
+ // [START android_activity_embedding_split_attributes_calculator_tabletop_java]
+ if (WindowSdkExtensions.getInstance().getExtensionVersion() >= 2) {
+ SplitController.getInstance(this).setSplitAttributesCalculator(params -> {
+ String tag = params.getSplitRuleTag();
+ WindowMetrics parentWindowMetrics = params.getParentWindowMetrics();
+ Configuration parentConfiguration = params.getParentConfiguration();
+ List foldingFeatures =
+ params.getParentWindowLayoutInfo().getDisplayFeatures().stream().filter(
+ item -> item instanceof FoldingFeature)
+ .map(item -> (FoldingFeature) item)
+ .collect(Collectors.toList());
+ FoldingFeature feature = foldingFeatures.size() == 1 ? foldingFeatures.get(0) : null;
+ SplitAttributes.Builder builder = new SplitAttributes.Builder();
+ builder.setSplitType(SplitType.SPLIT_TYPE_HINGE);
+ if (feature != null && feature.isSeparating()) {
+ // Horizontal slit for tabletop posture.
+ return builder
+ .setSplitType(SplitType.SPLIT_TYPE_HINGE)
+ .setLayoutDirection(
+ feature.getOrientation() == FoldingFeature.Orientation.HORIZONTAL
+ ? SplitAttributes.LayoutDirection.BOTTOM_TO_TOP
+ : SplitAttributes.LayoutDirection.LOCALE)
+ .build();
+ }
+ else if (parentConfiguration.screenWidthDp >= 840) {
+ // Side-by-side dual-pane layout for wide displays.
+ return builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LOCALE)
+ .build();
+ } else {
+ // No split for tall displays.
+ return builder
+ .setSplitType(SplitType.SPLIT_TYPE_EXPAND)
+ .build();
+ }
+ });
+ }
+ // [END android_activity_embedding_split_attributes_calculator_tabletop_java]
+
+ }
+ }
+
+ static class SplitRuleSnippetsActivity extends AppCompatActivity {
+
+ private Context context;
+
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // [START android_activity_embedding_splitPairFilter_java]
+ SplitPairFilter splitPairFilter = new SplitPairFilter(
+ new ComponentName(this, ListActivity.class),
+ new ComponentName(this, DetailActivity.class),
+ null
+ );
+ // [END android_activity_embedding_splitPairFilter_java]
+
+ // [START android_activity_embedding_filterSet_java]
+ Set filterSet = new HashSet<>();
+ filterSet.add(splitPairFilter);
+ // [END android_activity_embedding_filterSet_java]
+
+ // [START android_activity_embedding_splitAttributes_java]
+ SplitAttributes splitAttributes = new SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.33f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT)
+ .build();
+ // [END android_activity_embedding_splitAttributes_java]
+
+ // [START android_activity_embedding_splitPairRule_java]
+ SplitPairRule splitPairRule = new SplitPairRule.Builder(filterSet)
+ .setDefaultSplitAttributes(splitAttributes)
+ .setMinWidthDp(840)
+ .setMinSmallestWidthDp(600)
+ .setMaxAspectRatioInPortrait(EmbeddingAspectRatio.ratio(1.5f))
+ .setFinishPrimaryWithSecondary(SplitRule.FinishBehavior.NEVER)
+ .setFinishSecondaryWithPrimary(SplitRule.FinishBehavior.ALWAYS)
+ .setClearTop(false)
+ .build();
+ // [END android_activity_embedding_splitPairRule_java]
+
+ // [START android_activity_embedding_ruleController_java]
+ RuleController ruleController = RuleController.getInstance(this);
+ ruleController.addRule(splitPairRule);
+ // [END android_activity_embedding_ruleController_java]
+
+ // [START android_activity_embedding_placeholderActivityFilter_java]
+ ActivityFilter placeholderActivityFilter = new ActivityFilter(
+ new ComponentName(this, ListActivity.class),
+ null
+ );
+ // [END android_activity_embedding_placeholderActivityFilter_java]
+
+ // [START android_activity_embedding_placeholderActivityFilterSet_java]
+ Set placeholderActivityFilterSet = new HashSet<>();
+ placeholderActivityFilterSet.add(placeholderActivityFilter);
+ // [END android_activity_embedding_placeholderActivityFilterSet_java]
+
+ // [START android_activity_embedding_splitPlaceholderRule_java]
+ SplitPlaceholderRule splitPlaceholderRule = new SplitPlaceholderRule.Builder(
+ placeholderActivityFilterSet,
+ new Intent(this, PlaceholderActivity.class)
+ ).setDefaultSplitAttributes(splitAttributes)
+ .setMinWidthDp(840)
+ .setMinSmallestWidthDp(600)
+ .setMaxAspectRatioInPortrait(EmbeddingAspectRatio.ratio(1.5f))
+ .setFinishPrimaryWithPlaceholder(SplitRule.FinishBehavior.ALWAYS)
+ .setSticky(false)
+ .build();
+ // [END android_activity_embedding_splitPlaceholderRule_java]
+
+ // [START android_activity_embedding_addRuleSplitPlaceholderRule_java]
+ ruleController.addRule(splitPlaceholderRule);
+ // [END android_activity_embedding_addRuleSplitPlaceholderRule_java]
+
+ // [START android_activity_embedding_expandedActivityFilter_java]
+ ActivityFilter expandedActivityFilter = new ActivityFilter(
+ new ComponentName(this, ExpandedActivity.class),
+ null
+ );
+ // [END android_activity_embedding_expandedActivityFilter_java]
+
+ // [START android_activity_embedding_expandedActivityFilterSet_java]
+ Set expandedActivityFilterSet = new HashSet<>();
+ expandedActivityFilterSet.add(expandedActivityFilter);
+ // [END android_activity_embedding_expandedActivityFilterSet_java]
+
+ // [START android_activity_embedding_activityRule_java]
+ ActivityRule activityRule = new ActivityRule.Builder(
+ expandedActivityFilterSet
+ ).setAlwaysExpand(true)
+ .build();
+ // [END android_activity_embedding_activityRule_java]
+
+ // [START android_activity_embedding_addRuleActivityRule_java]
+ ruleController.addRule(activityRule);
+ // [END android_activity_embedding_addRuleActivityRule_java]
+
+ }
+
+
+ // [START android_activity_embedding_isActivityEmbedded_java]
+ boolean isActivityEmbedded(Activity activity) {
+ return ActivityEmbeddingController.getInstance(context).isActivityEmbedded(activity);
+ }
+ // [END android_activity_embedding_isActivityEmbedded_java]
+
+ }
+
+ static class SplitAttributesBuilderSnippetsActivity extends AppCompatActivity {
+
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // [START android_activity_embedding_splitAttributesBuilder_java]
+ SplitAttributes.Builder splitAttributesBuilder = new SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.33f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT);
+
+ if (WindowSdkExtensions.getInstance().getExtensionVersion() >= 6) {
+ splitAttributesBuilder.setDividerAttributes(
+ new DividerAttributes.DraggableDividerAttributes.Builder()
+ .setColor(ContextCompat.getColor(this, R.color.divider_color))
+ .setWidthDp(4)
+ .setDragRange(DividerAttributes.DragRange.DRAG_RANGE_SYSTEM_DEFAULT)
+ .build()
+ );
+ }
+ SplitAttributes _splitAttributes = splitAttributesBuilder.build();
+ // [END android_activity_embedding_splitAttributesBuilder_java]
+
+ }
+ }
+
+ /** @noinspection InnerClassMayBeStatic */
+ // [START android_activity_embedding_DetailActivity_class_java]
+ public class DetailActivity extends AppCompatActivity {
+ void onOpenSubdetail() {
+ startActivity(new Intent(this, SubdetailActivity.class));
+ }
+ }
+ // [END android_activity_embedding_DetailActivity_class_java]
+
+
+ /** @noinspection InnerClassMayBeStatic */
+ // [START android_activity_embedding_SplitInitializer_class_java]
+ public class SplitInitializer implements Initializer {
+
+ @NonNull
+ @Override
+ public RuleController create(@NonNull Context context) {
+ RuleController ruleController = RuleController.getInstance(context);
+ ruleController.setRules(
+ RuleController.parseRules(context, R.xml.main_split_config)
+ );
+ return ruleController;
+ }
+
+ @NonNull
+ @Override
+ public List>> dependencies() {
+ return Collections.emptyList();
+ }
+ }
+ // [END android_activity_embedding_SplitInitializer_class_java]
+
+
+ /**
+ * Function used by snippet.
+ */
+ private Class> classForItem(int item) { return Class.class; }
+
+ /** @noinspection InnerClassMayBeStatic */
+ // [START android_activity_embedding_MenuActivity_class_java]
+ public class MenuActivity extends AppCompatActivity{
+ void onMenuItemSelected(int selectedMenuItem) {
+ startActivity(new Intent(this, classForItem(selectedMenuItem)));
+ }
+ }
+ // [END android_activity_embedding_MenuActivity_class_java]
+
+
+ /** @noinspection InnerClassMayBeStatic */
+ // [START android_activity_embedding_B_class_java]
+ public class B extends AppCompatActivity{
+ void onOpenC() {
+ startActivity(new Intent(this, C.class));
+ }
+ }
+ // [END android_activity_embedding_B_class_java]
+
+
+ static class RuleControllerSnippetsActivity extends Activity {
+
+ private Set filterSet = new HashSet<>();
+
+ // [START android_activity_embedding_onCreate_RuleController_java]
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ RuleController.getInstance(this)
+ .addRule(new SplitPairRule.Builder(filterSet).build());
+ startActivity(new Intent(this, DetailActivity.class));
+ }
+ // [END android_activity_embedding_onCreate_RuleController_java]
+
+ }
+
+
+ static class SplitDeviceActivity extends AppCompatActivity {
+
+ @OptIn(markerClass = ExperimentalWindowApi.class)
+ // [START android_activity_embedding_onCreate_SplitControllerCallbackAdapter_java]
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ new SplitControllerCallbackAdapter(SplitController.getInstance(this))
+ .addSplitListener(
+ this,
+ Runnable::run,
+ splitInfoList -> {
+ View layout = getLayoutInflater().inflate(R.layout.activity_main, null);
+ layout.findViewById(R.id.infoButton).setVisibility(
+ splitInfoList.isEmpty() ? View.VISIBLE : View.GONE);
+ });
+ }
+ // [END android_activity_embedding_onCreate_SplitControllerCallbackAdapter_java]
+
+ }
+
+ static class ActivityPinningSnippetsActivity extends Activity {
+
+ @SuppressLint("RequiresWindowSdk")
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ // [START android_activity_embedding_pinButton_java]
+ Button pinButton = findViewById(R.id.pinButton);
+ pinButton.setOnClickListener( (view) -> {
+ SplitAttributes splitAttributes = new SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.66f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT)
+ .build();
+
+ SplitPinRule pinSplitRule = new SplitPinRule.Builder()
+ .setSticky(true)
+ .setDefaultSplitAttributes(splitAttributes)
+ .build();
+
+ SplitController.getInstance(getApplicationContext())
+ .pinTopActivityStack(getTaskId(), pinSplitRule);
+ });
+ // [END android_activity_embedding_pinButton_java]
+
+ // [START android_activity_embedding_getSplitSupportStatus_java]
+ if (SplitController.getInstance(this).getSplitSupportStatus() ==
+ SplitController.SplitSupportStatus.SPLIT_AVAILABLE) {
+ // Device supports split activity features.
+ }
+ // [END android_activity_embedding_getSplitSupportStatus_java]
+
+ }
+ }
+
+
+ // Classes used by snippets.
+ static class ListActivity {}
+ static class SubdetailActivity {}
+ static class PlaceholderActivity {}
+ static class ExpandedActivity {}
+ static class C {}
+
+}
diff --git a/misc/src/main/java/com/example/snippets/ActivityEmbeddingKotlinSnippets.kt b/misc/src/main/java/com/example/snippets/ActivityEmbeddingKotlinSnippets.kt
new file mode 100644
index 000000000..d00308286
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/ActivityEmbeddingKotlinSnippets.kt
@@ -0,0 +1,372 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets
+
+import android.annotation.SuppressLint
+import android.app.Activity
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.os.Build.VERSION_CODES
+import android.os.Bundle
+import android.view.View
+import android.widget.Button
+import androidx.annotation.RequiresApi
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
+import androidx.startup.Initializer
+import androidx.window.WindowSdkExtensions
+import androidx.window.embedding.ActivityEmbeddingController
+import androidx.window.embedding.ActivityFilter
+import androidx.window.embedding.ActivityRule
+import androidx.window.embedding.DividerAttributes
+import androidx.window.embedding.EmbeddingAspectRatio
+import androidx.window.embedding.RuleController
+import androidx.window.embedding.SplitAttributes
+import androidx.window.embedding.SplitAttributes.SplitType.Companion.SPLIT_TYPE_EXPAND
+import androidx.window.embedding.SplitAttributes.SplitType.Companion.SPLIT_TYPE_HINGE
+import androidx.window.embedding.SplitController
+import androidx.window.embedding.SplitPairFilter
+import androidx.window.embedding.SplitPairRule
+import androidx.window.embedding.SplitPinRule
+import androidx.window.embedding.SplitPlaceholderRule
+import androidx.window.embedding.SplitRule
+import androidx.window.layout.FoldingFeature
+import kotlinx.coroutines.launch
+
+class ActivityEmbeddingKotlinSnippets {
+
+ class SplitAttributesCalculatorSnippetsActivity : AppCompatActivity() {
+
+ @SuppressLint("RequiresWindowSdk")
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // [START android_activity_embedding_split_attributes_calculator_kotlin]
+ if (WindowSdkExtensions.getInstance().extensionVersion >= 2) {
+ SplitController.getInstance(this).setSplitAttributesCalculator { params ->
+ val parentConfiguration = params.parentConfiguration
+ val builder = SplitAttributes.Builder()
+ return@setSplitAttributesCalculator if (parentConfiguration.screenWidthDp >= 840) {
+ // Side-by-side dual-pane layout for wide displays.
+ builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LOCALE)
+ .build()
+ } else if (parentConfiguration.screenHeightDp >= 600) {
+ // Horizontal split for tall displays.
+ builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.BOTTOM_TO_TOP)
+ .build()
+ } else {
+ // Fallback to expand the secondary container.
+ builder
+ .setSplitType(SPLIT_TYPE_EXPAND)
+ .build()
+ }
+ }
+ }
+ // [END android_activity_embedding_split_attributes_calculator_kotlin]
+
+ // [START android_activity_embedding_split_attributes_calculator_tabletop_kotlin]
+ if (WindowSdkExtensions.getInstance().extensionVersion >= 2) {
+ SplitController.getInstance(this).setSplitAttributesCalculator { params ->
+ val tag = params.splitRuleTag
+ val parentWindowMetrics = params.parentWindowMetrics
+ val parentConfiguration = params.parentConfiguration
+ val foldingFeatures =
+ params.parentWindowLayoutInfo.displayFeatures.filterIsInstance()
+ val feature = if (foldingFeatures.size == 1) foldingFeatures[0] else null
+ val builder = SplitAttributes.Builder()
+ builder.setSplitType(SPLIT_TYPE_HINGE)
+ return@setSplitAttributesCalculator if (feature?.isSeparating == true) {
+ // Horizontal split for tabletop posture.
+ builder
+ .setSplitType(SPLIT_TYPE_HINGE)
+ .setLayoutDirection(
+ if (feature.orientation == FoldingFeature.Orientation.HORIZONTAL) {
+ SplitAttributes.LayoutDirection.BOTTOM_TO_TOP
+ } else {
+ SplitAttributes.LayoutDirection.LOCALE
+ }
+ )
+ .build()
+ } else if (parentConfiguration.screenWidthDp >= 840) {
+ // Side-by-side dual-pane layout for wide displays.
+ builder
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LOCALE)
+ .build()
+ } else {
+ // No split for tall displays.
+ builder
+ .setSplitType(SPLIT_TYPE_EXPAND)
+ .build()
+ }
+ }
+ }
+ // [END android_activity_embedding_split_attributes_calculator_tabletop_kotlin]
+ }
+ }
+
+ class SplitRuleSnippetsActivity : AppCompatActivity() {
+
+ private val context = this
+
+ @RequiresApi(api = VERSION_CODES.N)
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // [START android_activity_embedding_splitPairFilter_kotlin]
+ val splitPairFilter = SplitPairFilter(
+ ComponentName(this, ListActivity::class.java),
+ ComponentName(this, DetailActivity::class.java),
+ null
+ )
+ // [END android_activity_embedding_splitPairFilter_kotlin]
+
+ // [START android_activity_embedding_filterSet_kotlin]
+ val filterSet = setOf(splitPairFilter)
+ // [END android_activity_embedding_filterSet_kotlin]
+
+ // [START android_activity_embedding_splitAttributes_kotlin]
+ val splitAttributes: SplitAttributes = SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.33f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT)
+ .build()
+ // [END android_activity_embedding_splitAttributes_kotlin]
+
+ // [START android_activity_embedding_splitPairRule_kotlin]
+ val splitPairRule = SplitPairRule.Builder(filterSet)
+ .setDefaultSplitAttributes(splitAttributes)
+ .setMinWidthDp(840)
+ .setMinSmallestWidthDp(600)
+ .setMaxAspectRatioInPortrait(EmbeddingAspectRatio.ratio(1.5f))
+ .setFinishPrimaryWithSecondary(SplitRule.FinishBehavior.NEVER)
+ .setFinishSecondaryWithPrimary(SplitRule.FinishBehavior.ALWAYS)
+ .setClearTop(false)
+ .build()
+ // [END android_activity_embedding_splitPairRule_kotlin]
+
+ // [START android_activity_embedding_ruleController_kotlin]
+ val ruleController = RuleController.getInstance(this)
+ ruleController.addRule(splitPairRule)
+ // [END android_activity_embedding_ruleController_kotlin]
+
+ // [START android_activity_embedding_placeholderActivityFilter_kotlin]
+ val placeholderActivityFilter = ActivityFilter(
+ ComponentName(this, ListActivity::class.java),
+ null
+ )
+ // [END android_activity_embedding_placeholderActivityFilter_kotlin]
+
+ // [START android_activity_embedding_placeholderActivityFilterSet_kotlin]
+ val placeholderActivityFilterSet = setOf(placeholderActivityFilter)
+ // [END android_activity_embedding_placeholderActivityFilterSet_kotlin]
+
+ // [START android_activity_embedding_splitPlaceholderRule_kotlin]
+ val splitPlaceholderRule = SplitPlaceholderRule.Builder(
+ placeholderActivityFilterSet,
+ Intent(context, PlaceholderActivity::class.java)
+ ).setDefaultSplitAttributes(splitAttributes)
+ .setMinWidthDp(840)
+ .setMinSmallestWidthDp(600)
+ .setMaxAspectRatioInPortrait(EmbeddingAspectRatio.ratio(1.5f))
+ .setFinishPrimaryWithPlaceholder(SplitRule.FinishBehavior.ALWAYS)
+ .setSticky(false)
+ .build()
+ // [END android_activity_embedding_splitPlaceholderRule_kotlin]
+
+ // [START android_activity_embedding_addRuleSplitPlaceholderRule_kotlin]
+ ruleController.addRule(splitPlaceholderRule)
+ // [END android_activity_embedding_addRuleSplitPlaceholderRule_kotlin]
+
+ // [START android_activity_embedding_expandedActivityFilter_kotlin]
+ val expandedActivityFilter = ActivityFilter(
+ ComponentName(this, ExpandedActivity::class.java),
+ null
+ )
+ // [END android_activity_embedding_expandedActivityFilter_kotlin]
+
+ // [START android_activity_embedding_expandedActivityFilterSet_kotlin]
+ val expandedActivityFilterSet = setOf(expandedActivityFilter)
+ // [END android_activity_embedding_expandedActivityFilterSet_kotlin]
+
+ // [START android_activity_embedding_activityRule_kotlin]
+ val activityRule = ActivityRule.Builder(expandedActivityFilterSet)
+ .setAlwaysExpand(true)
+ .build()
+ // [END android_activity_embedding_activityRule_kotlin]
+
+ // [START android_activity_embedding_addRuleActivityRule_kotlin]
+ ruleController.addRule(activityRule)
+ // [END android_activity_embedding_addRuleActivityRule_kotlin]
+ }
+
+ // [START android_activity_embedding_isActivityEmbedded_kotlin]
+ fun isActivityEmbedded(activity: Activity): Boolean {
+ return ActivityEmbeddingController.getInstance(this).isActivityEmbedded(activity)
+ }
+ // [END android_activity_embedding_isActivityEmbedded_kotlin]
+ }
+
+ class SplitAttributesBuilderSnippetsActivity : AppCompatActivity() {
+
+ @SuppressLint("RequiresWindowSdk")
+ @RequiresApi(VERSION_CODES.M)
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // [START android_activity_embedding_splitAttributesBuilder_kotlin]
+ val splitAttributesBuilder: SplitAttributes.Builder = SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.33f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT)
+
+ if (WindowSdkExtensions.getInstance().extensionVersion >= 6) {
+ splitAttributesBuilder.setDividerAttributes(
+ DividerAttributes.DraggableDividerAttributes.Builder()
+ .setColor(getColor(R.color.divider_color))
+ .setWidthDp(4)
+ .setDragRange(DividerAttributes.DragRange.DRAG_RANGE_SYSTEM_DEFAULT)
+ .build()
+ )
+ }
+ val splitAttributes: SplitAttributes = splitAttributesBuilder.build()
+ // [END android_activity_embedding_splitAttributesBuilder_kotlin]
+ }
+ }
+
+ // [START android_activity_embedding_DetailActivity_class_kotlin]
+ class DetailActivity : AppCompatActivity() {
+ fun onOpenSubdetail() {
+ startActivity(Intent(this, SubdetailActivity::class.java))
+ }
+ }
+ // [END android_activity_embedding_DetailActivity_class_kotlin]
+
+ // [START android_activity_embedding_SplitInitializer_class_kotlin]
+ class SplitInitializer : Initializer {
+
+ override fun create(context: Context): RuleController {
+ return RuleController.getInstance(context).apply {
+ setRules(RuleController.parseRules(context, R.xml.main_split_config))
+ }
+ }
+
+ override fun dependencies(): List>> {
+ return emptyList()
+ }
+ }
+ // [END android_activity_embedding_SplitInitializer_class_kotlin]
+
+ /**
+ * Function used by snippet.
+ */
+ fun classForItem(item: Int): Class<*> { return Class::class.java }
+
+ // [START android_activity_embedding_MenuActivity_class_kotlin]
+ inner class MenuActivity : AppCompatActivity() {
+ fun onMenuItemSelected(selectedMenuItem: Int) {
+ startActivity(Intent(this, classForItem(selectedMenuItem)))
+ }
+ }
+ // [END android_activity_embedding_MenuActivity_class_kotlin]
+
+ // [START android_activity_embedding_B_class_kotlin]
+ class B : AppCompatActivity() {
+ fun onOpenC() {
+ startActivity(Intent(this, C::class.java))
+ }
+ }
+ // [END android_activity_embedding_B_class_kotlin]
+
+ class RuleControllerSnippetsActivity : Activity() {
+
+ private val filterSet = HashSet()
+
+ // [START android_activity_embedding_onCreate_RuleController_kotlin]
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ RuleController.getInstance(this)
+ .addRule(SplitPairRule.Builder(filterSet).build())
+ startActivity(Intent(this, DetailActivity::class.java))
+ }
+ // [END android_activity_embedding_onCreate_RuleController_kotlin]
+ }
+
+ class SplitDeviceActivity : AppCompatActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ val splitController = SplitController.getInstance(this)
+ // [START android_activity_embedding_onCreate_SplitControllerCallbackAdapter_kotlin]
+ val layout = layoutInflater.inflate(R.layout.activity_main, null)
+ val view = layout.findViewById(R.id.infoButton)
+ lifecycleScope.launch {
+ repeatOnLifecycle(Lifecycle.State.STARTED) {
+ splitController.splitInfoList(this@SplitDeviceActivity) // The activity instance.
+ .collect { list ->
+ view.visibility = if (list.isEmpty()) View.VISIBLE else View.GONE
+ }
+ }
+ }
+ // [END android_activity_embedding_onCreate_SplitControllerCallbackAdapter_kotlin]
+ }
+ }
+
+ class ActivityPinningSnippetsActivity : AppCompatActivity() {
+ @SuppressLint("RequiresWindowSdk")
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.activity_main)
+
+ // [START android_activity_embedding_pinButton_kotlin]
+ val pinButton: Button = findViewById(R.id.pinButton)
+ pinButton.setOnClickListener {
+ val splitAttributes: SplitAttributes = SplitAttributes.Builder()
+ .setSplitType(SplitAttributes.SplitType.ratio(0.66f))
+ .setLayoutDirection(SplitAttributes.LayoutDirection.LEFT_TO_RIGHT)
+ .build()
+
+ val pinSplitRule = SplitPinRule.Builder()
+ .setSticky(true)
+ .setDefaultSplitAttributes(splitAttributes)
+ .build()
+
+ SplitController.getInstance(applicationContext)
+ .pinTopActivityStack(taskId, pinSplitRule)
+ }
+ // [END android_activity_embedding_pinButton_kotlin]
+
+ // [START android_activity_embedding_getSplitSupportStatus_kotlin]
+ if (SplitController.getInstance(this).splitSupportStatus ==
+ SplitController.SplitSupportStatus.SPLIT_AVAILABLE
+ ) {
+ // Device supports split activity features.
+ }
+ // [END android_activity_embedding_getSplitSupportStatus_kotlin]
+ }
+ }
+
+ // Classes used by snippets.
+ class ListActivity
+ class SubdetailActivity
+ class PlaceholderActivity
+ class ExpandedActivity
+ class C
+}
diff --git a/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeJavaSnippets.java b/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeJavaSnippets.java
new file mode 100644
index 000000000..d5e3a3622
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeJavaSnippets.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets;
+
+import android.graphics.Rect;
+import android.os.Build.VERSION_CODES;
+import androidx.annotation.RequiresApi;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.window.layout.WindowMetricsCalculator;
+
+public class DeviceCompatibilityModeJavaSnippets {
+
+ @RequiresApi(api=VERSION_CODES.N)
+ // [START android_device_compatibility_mode_isLetterboxed_java]
+ public boolean isLetterboxed(AppCompatActivity activity) {
+ if (activity.isInMultiWindowMode()) {
+ return false;
+ }
+
+ WindowMetricsCalculator wmc = WindowMetricsCalculator.getOrCreate();
+ Rect currentBounds = wmc.computeCurrentWindowMetrics(activity).getBounds();
+ Rect maxBounds = wmc.computeMaximumWindowMetrics(activity).getBounds();
+
+ boolean isScreenPortrait = maxBounds.height() > maxBounds.width();
+
+ return (isScreenPortrait)
+ ? currentBounds.height() < maxBounds.height()
+ : currentBounds.width() < maxBounds.width();
+ }
+ // [END android_device_compatibility_mode_isLetterboxed_java]
+
+}
diff --git a/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeKotlinSnippets.kt b/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeKotlinSnippets.kt
new file mode 100644
index 000000000..7bc6a1971
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/DeviceCompatibilityModeKotlinSnippets.kt
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets
+
+import android.os.Build
+import android.os.Bundle
+import androidx.annotation.RequiresApi
+import androidx.appcompat.app.AppCompatActivity
+import androidx.window.layout.WindowMetricsCalculator
+
+class DeviceCompatibilityModeKotlinSnippets : AppCompatActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ }
+
+ @RequiresApi(Build.VERSION_CODES.N)
+ // [START android_device_compatibility_mode_isLetterboxed_kotlin]
+ fun isLetterboxed(activity: AppCompatActivity): Boolean {
+ if (isInMultiWindowMode) return false
+
+ val wmc = WindowMetricsCalculator.getOrCreate()
+ val currentBounds = wmc.computeCurrentWindowMetrics(this).bounds
+ val maxBounds = wmc.computeMaximumWindowMetrics(this).bounds
+
+ val isScreenPortrait = maxBounds.height() > maxBounds.width()
+
+ return if (isScreenPortrait) {
+ currentBounds.height() < maxBounds.height()
+ } else {
+ currentBounds.width() < maxBounds.width()
+ }
+ }
+ // [END android_device_compatibility_mode_isLetterboxed_kotlin]
+}
diff --git a/misc/src/main/java/com/example/snippets/PreloadManagerKotlinSnippets.kt b/misc/src/main/java/com/example/snippets/PreloadManagerKotlinSnippets.kt
new file mode 100644
index 000000000..c120e824f
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/PreloadManagerKotlinSnippets.kt
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets
+
+import android.os.Bundle
+import androidx.annotation.OptIn
+import androidx.appcompat.app.AppCompatActivity
+import androidx.media3.common.C
+import androidx.media3.common.MediaItem
+import androidx.media3.common.util.UnstableApi
+import androidx.media3.exoplayer.ExoPlayer
+import androidx.media3.exoplayer.source.preload.DefaultPreloadManager
+import androidx.media3.exoplayer.source.preload.TargetPreloadStatusControl
+import java.lang.Math.abs
+
+// constants to make the code snippets work
+const val currentPlayingIndex = 10
+
+@UnstableApi
+// [START android_defaultpreloadmanager_MyTargetPreloadStatusControl]
+class MyTargetPreloadStatusControl(
+ currentPlayingIndex: Int = C.INDEX_UNSET
+) : TargetPreloadStatusControl {
+
+ override fun getTargetPreloadStatus(index: Int): DefaultPreloadManager.PreloadStatus? {
+ if (index - currentPlayingIndex == 1) { // next track
+ // return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and
+ // suggest loading 3000ms from the default start position
+ return DefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)
+ } else if (index - currentPlayingIndex == -1) { // previous track
+ // return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and
+ // suggest loading 3000ms from the default start position
+ return DefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)
+ } else if (abs(index - currentPlayingIndex) == 2) {
+ // return a PreloadStatus that is labelled by STAGE_TRACKS_SELECTED
+ return DefaultPreloadManager.PreloadStatus.TRACKS_SELECTED
+ } else if (abs(index - currentPlayingIndex) <= 4) {
+ // return a PreloadStatus that is labelled by STAGE_SOURCE_PREPARED
+ return DefaultPreloadManager.PreloadStatus.SOURCE_PREPARED
+ }
+ return null
+ }
+}
+// [END android_defaultpreloadmanager_MyTargetPreloadStatusControl]
+
+class PreloadManagerSnippetsKotlin {
+
+ class PreloadSnippetsActivity : AppCompatActivity() {
+ private val context = this
+
+ @OptIn(UnstableApi::class)
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // [START android_defaultpreloadmanager_createPLM]
+ val targetPreloadStatusControl = MyTargetPreloadStatusControl()
+ val preloadManagerBuilder =
+ DefaultPreloadManager.Builder(context, targetPreloadStatusControl)
+ val preloadManager = preloadManagerBuilder.build()
+ // [END android_defaultpreloadmanager_createPLM]
+
+ val player = preloadManagerBuilder.buildExoPlayer()
+
+ // [START android_defaultpreloadmanager_addMedia]
+ val initialMediaItems = pullMediaItemsFromService(/* count= */ 20)
+ for (index in 0 until initialMediaItems.size) {
+ preloadManager.add(initialMediaItems.get(index), /* rankingData= */ index)
+ }
+ // items aren't actually loaded yet! need to call invalidate() after this
+ // [END android_defaultpreloadmanager_addMedia]
+
+ // [START android_defaultpreloadmanager_invalidate]
+ preloadManager.invalidate()
+ // [END android_defaultpreloadmanager_invalidate]
+ }
+
+ @OptIn(UnstableApi::class)
+ private fun fetchMedia(
+ preloadManager: DefaultPreloadManager,
+ mediaItem: MediaItem,
+ player: ExoPlayer,
+ currentIndex: Int
+ ) {
+ // [START android_defaultpreloadmanager_getAndPlayMedia]
+ // When a media item is about to display on the screen
+ val mediaSource = preloadManager.getMediaSource(mediaItem)
+ if (mediaSource != null) {
+ player.setMediaSource(mediaSource)
+ } else {
+ // If mediaSource is null, that mediaItem hasn't been added to the preload manager
+ // yet. So, send it directly to the player when it's about to play
+ player.setMediaItem(mediaItem)
+ }
+ player.prepare()
+
+ // When the media item is displaying at the center of the screen
+ player.play()
+ preloadManager.setCurrentPlayingIndex(currentIndex)
+
+ // Need to call invalidate() to update the priorities
+ preloadManager.invalidate()
+ // [END android_defaultpreloadmanager_getAndPlayMedia]
+ }
+
+ @OptIn(UnstableApi::class)
+ private fun removeMedia(mediaItem: MediaItem, preloadManager: DefaultPreloadManager) {
+ // [START android_defaultpreloadmanager_removeItem]
+ preloadManager.remove(mediaItem)
+ // [END android_defaultpreloadmanager_removeItem]
+ }
+
+ @OptIn(UnstableApi::class)
+ private fun releasePLM(preloadManager: DefaultPreloadManager) {
+ // [START android_defaultpreloadmanager_releasePLM]
+ preloadManager.release()
+ // [END android_defaultpreloadmanager_releasePLM]
+ }
+
+ // dummy methods to support the code snippets
+ private fun pullMediaItemsFromService(count: Int): List {
+ return listOf()
+ }
+ }
+}
diff --git a/misc/src/main/java/com/example/snippets/backgroundwork/MyException.java b/misc/src/main/java/com/example/snippets/backgroundwork/MyException.java
new file mode 100644
index 000000000..04d08bfe8
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/backgroundwork/MyException.java
@@ -0,0 +1,13 @@
+package com.example.snippets.backgroundwork;
+
+/**
+ * Placeholder exception used by wake lock docs.
+ *
+ * Existing wake lock code snippets inclde a method that throws "MyException", I need to define
+ * it for the code snippets to use.
+ */
+public class MyException extends RuntimeException {
+ public MyException(String message) {
+ super(message);
+ }
+}
diff --git a/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java b/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java
new file mode 100644
index 000000000..7b9a9e682
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java
@@ -0,0 +1,39 @@
+package com.example.snippets.backgroundwork;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.os.PowerManager;
+
+import androidx.annotation.Nullable;
+
+public class WakeLockSnippetsJava extends Activity {
+
+ PowerManager.WakeLock wakeLock;
+
+ @Override
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
+
+ // [START android_backgroundwork_wakelock_create_java]
+ PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
+ PowerManager.WakeLock wakeLock =
+ powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag");
+ wakeLock.acquire();
+ // [END android_backgroundwork_wakelock_create_java]
+
+ super.onCreate(savedInstanceState);
+ }
+
+ // [START android_backgroundwork_wakelock_release_java]
+ void doSomethingAndRelease() throws MyException {
+ try {
+ wakeLock.acquire();
+ doTheWork();
+ } finally {
+ wakeLock.release();
+ }
+ }
+ // [END android_backgroundwork_wakelock_release_java]
+
+ private void doTheWork() {
+ }
+}
diff --git a/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt b/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt
new file mode 100644
index 000000000..18ead1619
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets.backgroundwork
+
+import android.app.Activity
+import android.content.Context
+import android.os.Bundle
+import android.os.PowerManager
+
+// Snippets for doc page go here
+class WakeLockSnippetsKotlin : Activity() {
+
+ // [START android_backgroundwork_wakelock_create_kotlin]
+ val wakeLock: PowerManager.WakeLock =
+ (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
+ newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag").apply {
+ acquire()
+ }
+ }
+ // [END android_backgroundwork_wakelock_create_kotlin]
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+
+ super.onCreate(savedInstanceState)
+ }
+
+ // [START android_backgroundwork_wakelock_release_kotlin]
+ @Throws(MyException::class)
+ fun doSomethingAndRelease() {
+ wakeLock.apply {
+ try {
+ acquire()
+ doTheWork()
+ } finally {
+ release()
+ }
+ }
+ }
+ // [END android_backgroundwork_wakelock_release_kotlin]
+
+ private fun doTheWork() {
+ }
+}
diff --git a/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivity.kt b/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivity.kt
new file mode 100644
index 000000000..674f8a42e
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivity.kt
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets.location
+
+import android.Manifest
+import android.os.Build
+import androidx.activity.ComponentActivity
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.annotation.RequiresApi
+
+// Assuming this function is inside a ComponentActivity or a subclass of it
+class LocationPermissionsActivity : ComponentActivity() {
+
+ // [START android_location_requestpermissions_kotlin]
+ @RequiresApi(Build.VERSION_CODES.N)
+ fun requestPermissions() {
+ val locationPermissionRequest = registerForActivityResult(
+ ActivityResultContracts.RequestMultiplePermissions()
+ ) { permissions ->
+ when {
+ permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
+ // Precise location access granted.
+ }
+ permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
+ // Only approximate location access granted.
+ }
+ else -> {
+ // No location access granted.
+ }
+ }
+ }
+
+ // Before you perform the actual permission request, check whether your app
+ // already has the permissions, and whether your app needs to show a permission
+ // rationale dialog. For more details, see Request permissions:
+ // https://developer.android.com/training/permissions/requesting#request-permission
+ locationPermissionRequest.launch(
+ arrayOf(
+ Manifest.permission.ACCESS_FINE_LOCATION,
+ Manifest.permission.ACCESS_COARSE_LOCATION
+ )
+ )
+ }
+ // [END android_location_requestpermissions_kotlin]
+}
diff --git a/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivityJava.java b/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivityJava.java
new file mode 100644
index 000000000..8d20ad5d3
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/location/LocationPermissionsActivityJava.java
@@ -0,0 +1,51 @@
+package com.example.snippets.location;
+
+import android.Manifest;
+import android.os.Build;
+import android.os.Bundle;
+import androidx.activity.ComponentActivity;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
+import java.util.Map;
+
+public class LocationPermissionsActivityJava extends ComponentActivity {
+
+ // [START android_location_requestpermissions_java]
+ private void requestPermissions() {
+
+ ActivityResultLauncher locationPermissionRequest =
+ registerForActivityResult(new ActivityResultContracts
+ .RequestMultiplePermissions(), result -> {
+
+ Boolean fineLocationGranted = null;
+ Boolean coarseLocationGranted = null;
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ fineLocationGranted = result.getOrDefault(
+ Manifest.permission.ACCESS_FINE_LOCATION, false);
+ coarseLocationGranted = result.getOrDefault(
+ Manifest.permission.ACCESS_COARSE_LOCATION,false);
+ }
+
+ if (fineLocationGranted != null && fineLocationGranted) {
+ // Precise location access granted.
+ } else if (coarseLocationGranted != null && coarseLocationGranted) {
+ // Only approximate location access granted.
+ } else {
+ // No location access granted.
+ }
+ }
+ );
+
+ // ...
+
+ // Before you perform the actual permission request, check whether your app
+ // already has the permissions, and whether your app needs to show a permission
+ // rationale dialog. For more details, see Request permissions.
+ locationPermissionRequest.launch(new String[] {
+ Manifest.permission.ACCESS_FINE_LOCATION,
+ Manifest.permission.ACCESS_COARSE_LOCATION
+ });
+ }
+ // [END android_location_requestpermissions_java]
+}
\ No newline at end of file
diff --git a/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java
new file mode 100644
index 000000000..4dae4d353
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java
@@ -0,0 +1,73 @@
+package com.example.snippets.profiling;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import java.util.function.Consumer;
+import java.util.concurrent.Executor;
+import android.os.ProfilingResult;
+import java.util.concurrent.Executors;
+import android.os.CancellationSignal;
+import androidx.tracing.Trace;
+import androidx.core.os.Profiling;
+import androidx.core.os.SystemTraceRequestBuilder;
+import androidx.core.os.BufferFillPolicy;
+
+public class ProfilingManagerJavaSnippets {
+ public class MainActivityJava extends Activity {
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ sampleRecordSystemTrace();
+ }
+
+ // [START android_profiling_manager_record_system_trace_java]
+ void heavyOperation() {
+ // Computations you want to profile
+ }
+
+ void sampleRecordSystemTrace() {
+ Executor mainExecutor = Executors.newSingleThreadExecutor();
+ Consumer resultCallback =
+ new Consumer() {
+ @Override
+ public void accept(ProfilingResult profilingResult) {
+ if (profilingResult.getErrorCode() == ProfilingResult.ERROR_NONE) {
+ Log.d(
+ "ProfileTest",
+ "Received profiling result file=" + profilingResult.getResultFilePath());
+ } else {
+ Log.e(
+ "ProfileTest",
+ "Profiling failed errorcode="
+
+ + profilingResult.getErrorCode()
+ + " errormsg="
+ + profilingResult.getErrorMessage());
+ }
+ }
+ };
+ CancellationSignal stopSignal = new CancellationSignal();
+
+ SystemTraceRequestBuilder requestBuilder = new SystemTraceRequestBuilder();
+ requestBuilder.setCancellationSignal(stopSignal);
+ requestBuilder.setTag("FOO");
+ requestBuilder.setDurationMs(60000);
+ requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER);
+ requestBuilder.setBufferSizeKb(20971520);
+ Profiling.requestProfiling(getApplicationContext(), requestBuilder.build(), mainExecutor,
+ resultCallback);
+
+ // Wait some time for profiling to start.
+
+ Trace.beginSection("MyApp:HeavyOperation");
+ heavyOperation();
+ Trace.endSection();
+
+ // Once the interesting code section is profiled, stop profile
+ stopSignal.cancel();
+ }
+ // [END android_profiling_manager_record_system_trace_java]
+ }
+}
diff --git a/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt
new file mode 100644
index 000000000..f3f0ad99c
--- /dev/null
+++ b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.snippets.profiling
+
+import android.app.Activity
+import android.os.Build
+import android.os.Bundle
+import android.os.CancellationSignal
+import android.os.ProfilingResult
+import android.util.Log
+import androidx.annotation.RequiresApi
+import androidx.core.os.BufferFillPolicy
+import androidx.core.os.SystemTraceRequestBuilder
+import androidx.core.os.requestProfiling
+import androidx.tracing.Trace
+import java.util.concurrent.Executor
+import java.util.function.Consumer
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.asExecutor
+
+class ProfilingManagerKotlinSnippets {
+ class MainActivity : Activity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ sampleRecordSystemTrace()
+ }
+
+ // [START android_profiling_manager_record_system_trace_kotlin]
+ @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
+ fun sampleRecordSystemTrace() {
+ val mainExecutor: Executor =
+ Dispatchers.IO.asExecutor() // Your choice of executor for the callback to occur on.
+ val resultCallback = Consumer { profilingResult ->
+ if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) {
+ Log.d(
+ "ProfileTest",
+ "Received profiling result file=" + profilingResult.resultFilePath
+ )
+ } else {
+ Log.e(
+ "ProfileTest",
+ "Profiling failed errorcode=" + profilingResult.errorCode + " errormsg=" + profilingResult.errorMessage
+ )
+ }
+ }
+ val stopSignal = CancellationSignal()
+
+ val requestBuilder = SystemTraceRequestBuilder()
+ requestBuilder.setCancellationSignal(stopSignal)
+ requestBuilder.setTag("FOO") // Caller supplied tag for identification
+ requestBuilder.setDurationMs(60000)
+ requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER)
+ requestBuilder.setBufferSizeKb(20971520)
+ requestProfiling(applicationContext, requestBuilder.build(), mainExecutor, resultCallback)
+
+ // Wait some time for profiling to start.
+
+ Trace.beginSection("MyApp:HeavyOperation")
+ heavyOperation()
+ Trace.endSection()
+
+ // Once the interesting code section is profiled, stop profile
+ stopSignal.cancel()
+ }
+
+ fun heavyOperation() {
+ // Computations you want to profile
+ }
+ // [END android_profiling_manager_record_system_trace_kotlin]
+ }
+}
diff --git a/misc/src/main/res/layout/activity_main.xml b/misc/src/main/res/layout/activity_main.xml
new file mode 100644
index 000000000..59cbc1b45
--- /dev/null
+++ b/misc/src/main/res/layout/activity_main.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
diff --git a/misc/src/main/res/values/colors.xml b/misc/src/main/res/values/colors.xml
index f8c6127d3..61944aef2 100644
--- a/misc/src/main/res/values/colors.xml
+++ b/misc/src/main/res/values/colors.xml
@@ -7,4 +7,6 @@
#FF018786
#FF000000
#FFFFFFFF
-
\ No newline at end of file
+
+ #FFFFAA00
+
diff --git a/misc/src/main/res/xml/main_split_config.xml b/misc/src/main/res/xml/main_split_config.xml
new file mode 100644
index 000000000..2113dc80d
--- /dev/null
+++ b/misc/src/main/res/xml/main_split_config.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 65b99c5b1..316110b4e 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -7,6 +7,9 @@ pluginManagement {
mavenCentral()
}
}
+plugins {
+ id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
+}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
@@ -14,7 +17,12 @@ dependencyResolutionManagement {
println("https://androidx.dev/snapshots/builds/$it/artifacts/repository/")
maven { url = uri("https://androidx.dev/snapshots/builds/$it/artifacts/repository/") }
}
-
+ maven {
+ url = uri("https://jitpack.io/")
+ content {
+ includeGroup("com.github.xgouchet")
+ }
+ }
google()
mavenCentral()
}
@@ -27,5 +35,8 @@ include(
":compose:snippets",
":wear",
":views",
- ":misc"
+ ":misc",
+ ":identity:credentialmanager",
+ ":xr",
+ ":watchfacepush:validator"
)
diff --git a/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt b/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt
index cd6ac4855..0499d116a 100644
--- a/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt
+++ b/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt
@@ -27,11 +27,10 @@ import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.compose
import com.example.example.snippet.views.R
-class ExampleAppWidget:GlanceAppWidget() {
+class ExampleAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
TODO("Not yet implemented")
}
-
}
private object GeneratedPreviewWithoutGlance {
@@ -51,7 +50,7 @@ private object GeneratedPreviewWithoutGlance {
// [END android_view_appwidget_generatedpreview_with_remoteview]
}
- suspend fun MyGlanceWidgetPreview() {
+ suspend fun MyGlanceWidgetPreview() {
// [START android_view_appwidget_generatedpreview_with_glance]
AppWidgetManager.getInstance(appContext).setWidgetPreview(
ComponentName(
@@ -68,6 +67,4 @@ private object GeneratedPreviewWithoutGlance {
}
}
-class ExampleAppWidgetReceiver: AppWidgetProvider() {
-
-}
+class ExampleAppWidgetReceiver : AppWidgetProvider()
diff --git a/views/src/main/java/insets/SystemBarProtectionSnippet.kt b/views/src/main/java/insets/SystemBarProtectionSnippet.kt
new file mode 100644
index 000000000..c21011b44
--- /dev/null
+++ b/views/src/main/java/insets/SystemBarProtectionSnippet.kt
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package insets
+
+import android.graphics.Color
+import android.os.Bundle
+import android.view.View
+import androidx.activity.enableEdgeToEdge
+import androidx.appcompat.app.AppCompatActivity
+import androidx.core.view.ViewCompat
+import androidx.core.view.WindowInsetsCompat
+import androidx.core.view.insets.GradientProtection
+import androidx.core.view.insets.ProtectionLayout
+import com.example.example.snippet.views.R
+
+class SystemBarProtectionSnippet : AppCompatActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.system_bar_protection)
+
+ enableEdgeToEdge()
+
+ ViewCompat.setOnApplyWindowInsetsListener(
+ findViewById(R.id.item_list)
+ ) { v: View, insets: WindowInsetsCompat ->
+ val innerPadding = insets.getInsets(
+ WindowInsetsCompat.Type.systemBars() or
+ WindowInsetsCompat.Type.displayCutout()
+ )
+ v.setPadding(
+ innerPadding.left,
+ innerPadding.top,
+ innerPadding.right,
+ innerPadding.bottom
+ )
+ insets
+ }
+
+ val red = 52
+ val green = 168
+ val blue = 83
+ val paneBackgroundColor = Color.rgb(red, green, blue)
+ // [START android_system_bar_protection_kotlin]
+ findViewById(R.id.list_protection)
+ .setProtections(
+ listOf(
+ GradientProtection(
+ WindowInsetsCompat.Side.TOP,
+ // Ideally, this is the pane's background color
+ paneBackgroundColor
+ )
+ )
+ )
+ // [END android_system_bar_protection_kotlin]
+ }
+}
diff --git a/views/src/main/res/layout/system_bar_protection.xml b/views/src/main/res/layout/system_bar_protection.xml
new file mode 100644
index 000000000..f70ccd21f
--- /dev/null
+++ b/views/src/main/res/layout/system_bar_protection.xml
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/watchfacepush/validator/.gitignore b/watchfacepush/validator/.gitignore
new file mode 100644
index 000000000..42afabfd2
--- /dev/null
+++ b/watchfacepush/validator/.gitignore
@@ -0,0 +1 @@
+/build
\ No newline at end of file
diff --git a/watchfacepush/validator/README.md b/watchfacepush/validator/README.md
new file mode 100644
index 000000000..e11373aa7
--- /dev/null
+++ b/watchfacepush/validator/README.md
@@ -0,0 +1 @@
+This is a sample project that contains the code snippets seen on https://developer.android.com/training/wearables/watch-face-push
diff --git a/watchfacepush/validator/build.gradle.kts b/watchfacepush/validator/build.gradle.kts
new file mode 100644
index 000000000..0b9289a5c
--- /dev/null
+++ b/watchfacepush/validator/build.gradle.kts
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+group = "com.example.validator"
+version = "1.0"
+
+plugins {
+ kotlin("jvm")
+ application
+}
+
+application {
+ mainClass.set("com.example.validator.Main")
+}
+
+sourceSets {
+ named("main") {
+ java {
+ srcDir("src/main/java")
+ }
+ }
+}
+
+dependencies {
+ implementation(libs.validator.push)
+}
\ No newline at end of file
diff --git a/watchfacepush/validator/consumer-rules.pro b/watchfacepush/validator/consumer-rules.pro
new file mode 100644
index 000000000..e69de29bb
diff --git a/watchfacepush/validator/proguard-rules.pro b/watchfacepush/validator/proguard-rules.pro
new file mode 100644
index 000000000..481bb4348
--- /dev/null
+++ b/watchfacepush/validator/proguard-rules.pro
@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile
\ No newline at end of file
diff --git a/watchfacepush/validator/src/main/kotlin/com/example/validator/Main.kt b/watchfacepush/validator/src/main/kotlin/com/example/validator/Main.kt
new file mode 100644
index 000000000..c1aa20ef0
--- /dev/null
+++ b/watchfacepush/validator/src/main/kotlin/com/example/validator/Main.kt
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.validator
+
+import com.google.android.wearable.watchface.validator.client.DwfValidatorFactory
+import java.io.File
+import java.io.FileOutputStream
+import kotlin.system.exitProcess
+
+class Main {
+ companion object {
+ @JvmStatic
+ fun main(args: Array) {
+ println("Watch Face Push validator test program")
+ performValidation()
+ }
+ }
+}
+
+private fun performValidation() {
+ val watchFaceFile = obtainTempWatchFaceFile()
+ val appPackageName = "com.example.validator"
+
+ // [START android_examples_wfp_validation]
+ val validator = DwfValidatorFactory.create()
+ val result = validator.validate(watchFaceFile, appPackageName)
+
+ if (result.failures().isEmpty()) {
+ val token = result.validationToken()
+ println("Validation token: $token")
+
+ // Validation success - continue with the token
+ // ...
+ } else {
+ // There were failures, handle them accordingly - validation has failed.
+ result.failures().forEach { failure ->
+ println("FAILURE: ${failure.name()}: ${failure.failureMessage()}")
+ // ...
+ }
+ }
+ // [END android_examples_wfp_validation]
+}
+
+private fun obtainTempWatchFaceFile(): File {
+ val resourceName = "watchface.apk"
+
+ val inputStream = object {}.javaClass.classLoader.getResourceAsStream(resourceName)
+
+ if (inputStream == null) {
+ println("Error: Cannot find resource '$resourceName'")
+ exitProcess(1)
+ }
+
+ val tempFile = File.createTempFile("validator-", ".apk")
+ tempFile.deleteOnExit()
+
+ FileOutputStream(tempFile).use { fos ->
+ inputStream.copyTo(fos)
+ }
+ return tempFile
+}
diff --git a/watchfacepush/validator/src/main/resources/watchface.apk b/watchfacepush/validator/src/main/resources/watchface.apk
new file mode 100644
index 000000000..d9bd51c3f
Binary files /dev/null and b/watchfacepush/validator/src/main/resources/watchface.apk differ
diff --git a/wear/build.gradle.kts b/wear/build.gradle.kts
index 154e7d37e..d88421c1e 100644
--- a/wear/build.gradle.kts
+++ b/wear/build.gradle.kts
@@ -6,12 +6,12 @@ plugins {
android {
namespace = "com.example.wear"
- compileSdk = 35
+ compileSdk = 36
defaultConfig {
applicationId = "com.example.wear"
minSdk = 26
- targetSdk = 33
+ targetSdk = 36
versionCode = 1
versionName = "1.0"
vectorDrawables {
@@ -46,9 +46,13 @@ android {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
+ kotlinOptions {
+ jvmTarget = "17"
+ }
}
dependencies {
+ implementation(libs.androidx.core.ktx)
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
androidTestImplementation(composeBom)
@@ -57,8 +61,11 @@ dependencies {
implementation(libs.play.services.wearable)
implementation(libs.androidx.tiles)
implementation(libs.androidx.wear)
+ implementation(libs.androidx.wear.ongoing)
+ implementation(libs.androidx.lifecycle.service)
implementation(libs.androidx.protolayout)
implementation(libs.androidx.protolayout.material)
+ implementation(libs.androidx.protolayout.material3)
implementation(libs.androidx.protolayout.expression)
debugImplementation(libs.androidx.tiles.renderer)
testImplementation(libs.androidx.tiles.testing)
@@ -69,15 +76,20 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.tooling.preview)
- implementation(libs.compose.material)
+ implementation(libs.androidx.fragment.ktx)
+ implementation(libs.wear.compose.material)
+ implementation(libs.wear.compose.material3)
implementation(libs.compose.foundation)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.core.splashscreen)
implementation(libs.horologist.compose.layout)
implementation(libs.horologist.compose.material)
implementation(libs.androidx.material.icons.core)
+ implementation(libs.androidx.watchface.complications.data.source.ktx)
+
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
+ debugImplementation(libs.androidx.compose.ui.tooling.preview)
debugImplementation(libs.androidx.compose.ui.test.manifest)
testImplementation(libs.junit)
diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml
index 84c4785a2..aaad81bba 100644
--- a/wear/src/main/AndroidManifest.xml
+++ b/wear/src/main/AndroidManifest.xml
@@ -2,6 +2,9 @@
+
+
+
@@ -24,7 +27,7 @@
android:value="true" />
@@ -35,6 +38,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnActivity.kt b/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnActivity.kt
new file mode 100644
index 000000000..17b18d8af
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnActivity.kt
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.alwayson
+
+import android.Manifest
+import android.content.pm.PackageManager
+import android.os.Build
+import android.os.Bundle
+import android.os.SystemClock
+import android.util.Log
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.PaddingValues
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.produceState
+import androidx.compose.runtime.saveable.rememberSaveable
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.core.content.ContextCompat
+import androidx.wear.compose.material3.MaterialTheme
+import androidx.wear.compose.material3.SwitchButton
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.material3.dynamicColorScheme
+import androidx.wear.tooling.preview.devices.WearDevices
+import com.google.android.horologist.compose.ambient.AmbientAware
+import com.google.android.horologist.compose.ambient.AmbientState
+import kotlinx.coroutines.delay
+
+private const val TAG = "AlwaysOnActivity"
+
+class AlwaysOnActivity : ComponentActivity() {
+ private val requestPermissionLauncher =
+ registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
+ if (isGranted) {
+ Log.d(TAG, "POST_NOTIFICATIONS permission granted")
+ } else {
+ Log.w(TAG, "POST_NOTIFICATIONS permission denied")
+ }
+ }
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ Log.d(TAG, "onCreate: Activity created")
+
+ setTheme(android.R.style.Theme_DeviceDefault)
+
+ // Check and request notification permission
+ checkAndRequestNotificationPermission()
+
+ setContent { WearApp() }
+ }
+
+ private fun checkAndRequestNotificationPermission() {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ when {
+ ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
+ PackageManager.PERMISSION_GRANTED -> {
+ Log.d(TAG, "POST_NOTIFICATIONS permission already granted")
+ }
+ shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
+ Log.d(TAG, "Should show permission rationale")
+ // You could show a dialog here explaining why the permission is needed
+ requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
+ }
+ else -> {
+ Log.d(TAG, "Requesting POST_NOTIFICATIONS permission")
+ requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
+ }
+ }
+ }
+ }
+}
+
+@Composable
+// [START android_wear_ongoing_activity_elapsedtime]
+fun ElapsedTime(ambientState: AmbientState) {
+ // [START_EXCLUDE]
+ val startTimeMs = rememberSaveable { SystemClock.elapsedRealtime() }
+
+ val elapsedMs by
+ produceState(initialValue = 0L, key1 = startTimeMs) {
+ while (true) { // time doesn't stop!
+ value = SystemClock.elapsedRealtime() - startTimeMs
+ // In ambient mode, update every minute instead of every second
+ val updateInterval = if (ambientState.isAmbient) 60_000L else 1_000L
+ delay(updateInterval - (value % updateInterval))
+ }
+ }
+
+ val totalSeconds = elapsedMs / 1_000L
+ val minutes = totalSeconds / 60
+ val seconds = totalSeconds % 60
+
+ // [END_EXCLUDE]
+ val timeText =
+ if (ambientState.isAmbient) {
+ // Show "mm:--" format in ambient mode
+ "%02d:--".format(minutes)
+ } else {
+ // Show full "mm:ss" format in interactive mode
+ "%02d:%02d".format(minutes, seconds)
+ }
+
+ Text(text = timeText, style = MaterialTheme.typography.numeralMedium)
+}
+// [END android_wear_ongoing_activity_elapsedtime]
+
+@Preview(
+ device = WearDevices.LARGE_ROUND,
+ backgroundColor = 0xff000000,
+ showBackground = true,
+ group = "Devices - Large Round",
+ showSystemUi = true,
+)
+@Composable
+fun WearApp() {
+ val context = LocalContext.current
+ var isOngoingActivity by rememberSaveable { mutableStateOf(AlwaysOnService.isRunning) }
+ MaterialTheme(
+ colorScheme = dynamicColorScheme(LocalContext.current) ?: MaterialTheme.colorScheme
+ ) {
+ // [START android_wear_ongoing_activity_ambientaware]
+ AmbientAware { ambientState ->
+ // [START_EXCLUDE]
+ Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
+ Column(horizontalAlignment = Alignment.CenterHorizontally) {
+ Text(text = "Elapsed Time", style = MaterialTheme.typography.titleLarge)
+ Spacer(modifier = Modifier.height(8.dp))
+ // [END_EXCLUDE]
+ ElapsedTime(ambientState = ambientState)
+ // [START_EXCLUDE]
+ Spacer(modifier = Modifier.height(8.dp))
+ SwitchButton(
+ checked = isOngoingActivity,
+ onCheckedChange = { newState ->
+ Log.d(TAG, "Switch button changed: $newState")
+ isOngoingActivity = newState
+
+ if (newState) {
+ Log.d(TAG, "Starting AlwaysOnService")
+ AlwaysOnService.startService(context)
+ } else {
+ Log.d(TAG, "Stopping AlwaysOnService")
+ AlwaysOnService.stopService(context)
+ }
+ },
+ contentPadding = PaddingValues(horizontal = 8.dp, vertical = 4.dp),
+ ) {
+ Text(
+ text = "Ongoing Activity",
+ style = MaterialTheme.typography.bodyExtraSmall,
+ )
+ }
+ }
+ }
+ // [END_EXCLUDE]
+ }
+ // [END android_wear_ongoing_activity_ambientaware]
+ }
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnService.kt b/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnService.kt
new file mode 100644
index 000000000..59ed0f8af
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnService.kt
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.alwayson
+
+import android.app.Notification
+import android.app.NotificationChannel
+import android.app.NotificationManager
+import android.app.PendingIntent
+import android.content.Context
+import android.content.Intent
+import android.util.Log
+import androidx.core.app.NotificationCompat
+import androidx.core.content.getSystemService
+import androidx.lifecycle.LifecycleService
+import androidx.wear.ongoing.OngoingActivity
+import androidx.wear.ongoing.Status
+import com.example.wear.R
+
+class AlwaysOnService : LifecycleService() {
+
+ private val notificationManager by lazy { getSystemService() }
+
+ companion object {
+ private const val TAG = "AlwaysOnService"
+ private const val NOTIFICATION_ID = 1001
+ private const val CHANNEL_ID = "always_on_service_channel"
+ private const val CHANNEL_NAME = "Always On Service"
+ @Volatile
+ var isRunning = false
+ private set
+
+ fun startService(context: Context) {
+ Log.d(TAG, "Starting AlwaysOnService")
+ val intent = Intent(context, AlwaysOnService::class.java)
+ context.startForegroundService(intent)
+ }
+
+ fun stopService(context: Context) {
+ Log.d(TAG, "Stopping AlwaysOnService")
+ context.stopService(Intent(context, AlwaysOnService::class.java))
+ }
+ }
+
+ override fun onCreate() {
+ super.onCreate()
+ Log.d(TAG, "onCreate: Service created")
+ isRunning = true
+ createNotificationChannel()
+ }
+
+ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
+ super.onStartCommand(intent, flags, startId)
+ Log.d(TAG, "onStartCommand: Service started with startId: $startId")
+
+ // Create and start foreground notification
+ val notification = createNotification()
+ startForeground(NOTIFICATION_ID, notification)
+
+ Log.d(TAG, "onStartCommand: Service is now running as foreground service")
+
+ return START_STICKY
+ }
+
+ override fun onDestroy() {
+ Log.d(TAG, "onDestroy: Service destroyed")
+ isRunning = false
+ super.onDestroy()
+ }
+
+ private fun createNotificationChannel() {
+ val channel =
+ NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
+ .apply {
+ description = "Always On Service notification channel"
+ setShowBadge(false)
+ }
+
+ notificationManager?.createNotificationChannel(channel)
+ Log.d(TAG, "createNotificationChannel: Notification channel created")
+ }
+
+ // [START android_wear_ongoing_activity_create_notification]
+ private fun createNotification(): Notification {
+ val activityIntent =
+ Intent(this, AlwaysOnActivity::class.java).apply {
+ flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
+ }
+
+ val pendingIntent =
+ PendingIntent.getActivity(
+ this,
+ 0,
+ activityIntent,
+ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
+ )
+
+ val notificationBuilder =
+ NotificationCompat.Builder(this, CHANNEL_ID)
+ // ...
+ // [START_EXCLUDE]
+ .setContentTitle("Always On Service")
+ .setContentText("Service is running in background")
+ .setSmallIcon(R.drawable.animated_walk)
+ .setContentIntent(pendingIntent)
+ .setCategory(NotificationCompat.CATEGORY_STOPWATCH)
+ .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
+ // [END_EXCLUDE]
+ .setOngoing(true)
+
+ // [START_EXCLUDE]
+ // Create an Ongoing Activity
+ val ongoingActivityStatus = Status.Builder().addTemplate("Stopwatch running").build()
+ // [END_EXCLUDE]
+
+ val ongoingActivity =
+ OngoingActivity.Builder(applicationContext, NOTIFICATION_ID, notificationBuilder)
+ // ...
+ // [START_EXCLUDE]
+ .setStaticIcon(R.drawable.ic_walk)
+ .setAnimatedIcon(R.drawable.animated_walk)
+ .setStatus(ongoingActivityStatus)
+ // [END_EXCLUDE]
+ .setTouchIntent(pendingIntent)
+ .build()
+
+ ongoingActivity.apply(applicationContext)
+
+ return notificationBuilder.build()
+ }
+ // [END android_wear_ongoing_activity_create_notification]
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/complication/ConfigurationActivity.kt b/wear/src/main/java/com/example/wear/snippets/complication/ConfigurationActivity.kt
new file mode 100644
index 000000000..551588854
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/complication/ConfigurationActivity.kt
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.complication
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.Text
+import androidx.wear.watchface.complications.datasource.ComplicationDataSourceService.Companion.EXTRA_CONFIG_COMPLICATION_ID
+import androidx.wear.watchface.complications.datasource.ComplicationDataSourceService.Companion.EXTRA_CONFIG_COMPLICATION_TYPE
+import androidx.wear.watchface.complications.datasource.ComplicationDataSourceService.Companion.EXTRA_CONFIG_DATA_SOURCE_COMPONENT
+
+class ConfigurationActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ // [START android_wear_complication_configuration_intent]
+ // Keys defined on ComplicationDataSourceService
+ val id = intent.getIntExtra(EXTRA_CONFIG_COMPLICATION_ID, -1)
+ val type = intent.getIntExtra(EXTRA_CONFIG_COMPLICATION_TYPE, -1)
+ val source = intent.getStringExtra(EXTRA_CONFIG_DATA_SOURCE_COMPONENT)
+ // [END android_wear_complication_configuration_intent]
+ setContent {
+ ComplicationConfig(
+ id = id,
+ type = type,
+ source = source
+ )
+ }
+ }
+
+ @Composable
+ fun ComplicationConfig(
+ modifier: Modifier = Modifier,
+ id: Int,
+ type: Int,
+ source: String?
+ ) {
+ Column(
+ modifier = Modifier.fillMaxSize(),
+ verticalArrangement = Arrangement.SpaceEvenly,
+ horizontalAlignment = Alignment.CenterHorizontally
+ ) {
+ Text("ID: $id")
+ Text("Type: $type")
+ Text("Source: $source")
+ Spacer(modifier = Modifier.height(4.dp))
+ Button(onClick = {
+ // [START android_wear_complication_configuration_finish]
+ setResult(RESULT_OK) // Or RESULT_CANCELLED to cancel configuration
+ finish()
+ // [END android_wear_complication_configuration_finish]
+ }) {
+ Text("Done!")
+ }
+ }
+ }
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/complication/MyComplicationDataSourceService.kt b/wear/src/main/java/com/example/wear/snippets/complication/MyComplicationDataSourceService.kt
new file mode 100644
index 000000000..ce08ea6ff
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/complication/MyComplicationDataSourceService.kt
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.complication
+
+import androidx.wear.watchface.complications.data.ComplicationData
+import androidx.wear.watchface.complications.data.ComplicationType
+import androidx.wear.watchface.complications.data.PlainComplicationText
+import androidx.wear.watchface.complications.data.ShortTextComplicationData
+import androidx.wear.watchface.complications.datasource.ComplicationRequest
+import androidx.wear.watchface.complications.datasource.SuspendingComplicationDataSourceService
+
+// [START android_wear_complication]
+class MyComplicationDataSourceService : SuspendingComplicationDataSourceService() {
+ override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationData? {
+ // Retrieve latest info for inclusion in the data
+ val text = getLatestData()
+ return shortTextComplicationData(text)
+ }
+
+ override fun getPreviewData(type: ComplicationType): ComplicationData? {
+ return shortTextComplicationData("Event 1")
+ }
+
+ private fun shortTextComplicationData(text: String) =
+ ShortTextComplicationData.Builder(
+ text = PlainComplicationText.Builder(text).build(),
+ contentDescription = PlainComplicationText.Builder(text).build()
+ )
+ // Add further optional details here such as icon, tap action, title etc
+ .build()
+
+ // [START_EXCLUDE]
+ private fun getLatestData() = "Test"
+ // [END_EXCLUDE]
+}
+// [END android_wear_complication]
diff --git a/wear/src/main/java/com/example/wear/snippets/complication/MyTimelineComplicationDataSourceService.kt b/wear/src/main/java/com/example/wear/snippets/complication/MyTimelineComplicationDataSourceService.kt
new file mode 100644
index 000000000..00b6daea3
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/complication/MyTimelineComplicationDataSourceService.kt
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.complication
+
+import androidx.wear.watchface.complications.data.ComplicationData
+import androidx.wear.watchface.complications.data.ComplicationType
+import androidx.wear.watchface.complications.data.NoDataComplicationData
+import androidx.wear.watchface.complications.data.PlainComplicationText
+import androidx.wear.watchface.complications.data.ShortTextComplicationData
+import androidx.wear.watchface.complications.datasource.ComplicationDataTimeline
+import androidx.wear.watchface.complications.datasource.ComplicationRequest
+import androidx.wear.watchface.complications.datasource.SuspendingTimelineComplicationDataSourceService
+import androidx.wear.watchface.complications.datasource.TimeInterval
+import androidx.wear.watchface.complications.datasource.TimelineEntry
+import java.time.Instant
+import java.time.temporal.ChronoUnit
+
+data class CalendarEntry(
+ val start: Instant,
+ val end: Instant,
+ val name: String
+)
+
+// [START android_wear_timeline_complication]
+class MyTimelineComplicationDataSourceService : SuspendingTimelineComplicationDataSourceService() {
+ override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationDataTimeline? {
+ if (request.complicationType != ComplicationType.SHORT_TEXT) {
+ return ComplicationDataTimeline(
+ defaultComplicationData = NoDataComplicationData(),
+ timelineEntries = emptyList()
+ )
+ }
+ // Retrieve list of events from your own datasource / database.
+ val events = getCalendarEvents()
+ return ComplicationDataTimeline(
+ defaultComplicationData = shortTextComplicationData("No event"),
+ timelineEntries = events.map {
+ TimelineEntry(
+ validity = TimeInterval(it.start, it.end),
+ complicationData = shortTextComplicationData(it.name)
+ )
+ }
+ )
+ }
+
+ override fun getPreviewData(type: ComplicationType): ComplicationData? {
+ return shortTextComplicationData("Event 1")
+ }
+
+ private fun shortTextComplicationData(text: String) =
+ ShortTextComplicationData.Builder(
+ text = PlainComplicationText.Builder(text).build(),
+ contentDescription = PlainComplicationText.Builder(text).build()
+ )
+ // Add further optional details here such as icon, tap action, title etc
+ .build()
+
+ // [START_EXCLUDE]
+ private fun getCalendarEvents(): List {
+ val now = Instant.now()
+ return listOf(
+ CalendarEntry(now, now.plus(1, ChronoUnit.HOURS), "Event 1"),
+ CalendarEntry(now.plus(2, ChronoUnit.HOURS), now.plus(3, ChronoUnit.HOURS), "Event 2"),
+ CalendarEntry(now.plus(4, ChronoUnit.HOURS), now.plus(5, ChronoUnit.HOURS), "Event 3"),
+ )
+ }
+ // [END_EXCLUDE]
+}
+// [END android_wear_timeline_complication]
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/MainActivity.kt b/wear/src/main/java/com/example/wear/snippets/m3/MainActivity.kt
new file mode 100644
index 000000000..52e9c2eb7
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/MainActivity.kt
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.runtime.Composable
+import com.example.wear.snippets.m3.list.ComposeList
+
+class MainActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ setContent {
+ WearApp()
+ }
+ }
+}
+
+@Composable
+fun WearApp() {
+ // insert here the snippet you want to test
+ ComposeList()
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/list/List.kt b/wear/src/main/java/com/example/wear/snippets/m3/list/List.kt
new file mode 100644
index 000000000..29b0f8fd5
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/list/List.kt
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.list
+
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.Build
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalConfiguration
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
+import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.Icon
+import androidx.wear.compose.material3.ListHeader
+import androidx.wear.compose.material3.ScreenScaffold
+import androidx.wear.compose.material3.SurfaceTransformation
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.material3.lazy.rememberTransformationSpec
+import androidx.wear.compose.material3.lazy.transformedHeight
+import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
+import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
+import com.google.android.horologist.annotations.ExperimentalHorologistApi
+import com.google.android.horologist.compose.layout.ColumnItemType
+import com.google.android.horologist.compose.layout.ScalingLazyColumn
+import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults
+import com.google.android.horologist.compose.layout.ScalingLazyColumnState
+import com.google.android.horologist.compose.layout.ScreenScaffold
+import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
+import com.google.android.horologist.compose.layout.rememberResponsiveColumnState
+import com.google.android.horologist.compose.material.Button
+import com.google.android.horologist.compose.material.ListHeaderDefaults.firstItemPadding
+import com.google.android.horologist.compose.material.ResponsiveListHeader
+
+@Composable
+fun ComposeList() {
+ // [START android_wear_list]
+ val columnState = rememberTransformingLazyColumnState()
+ val contentPadding = rememberResponsiveColumnPadding(
+ first = ColumnItemType.ListHeader,
+ last = ColumnItemType.Button,
+ )
+ val transformationSpec = rememberTransformationSpec()
+ ScreenScaffold(
+ scrollState = columnState,
+ contentPadding = contentPadding
+ ) { contentPadding ->
+ TransformingLazyColumn(
+ state = columnState,
+ contentPadding = contentPadding
+ ) {
+ item {
+ ListHeader(
+ modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
+ transformation = SurfaceTransformation(transformationSpec)
+ ) {
+ Text(text = "Header")
+ }
+ }
+ // ... other items
+ item {
+ Button(
+ modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
+ transformation = SurfaceTransformation(transformationSpec),
+ onClick = { /* ... */ },
+ icon = {
+ Icon(
+ imageVector = Icons.Default.Build,
+ contentDescription = "build",
+ )
+ },
+ ) {
+ Text(
+ text = "Build",
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ )
+ }
+ }
+ }
+ }
+ // [END android_wear_list]
+}
+
+@OptIn(ExperimentalHorologistApi::class)
+@Composable
+fun SnapAndFlingComposeList() {
+ // [START android_wear_snap]
+ val columnState = rememberResponsiveColumnState(
+ // ...
+ // [START_EXCLUDE]
+ contentPadding = ScalingLazyColumnDefaults.padding(
+ first = ScalingLazyColumnDefaults.ItemType.Text,
+ last = ScalingLazyColumnDefaults.ItemType.SingleButton
+ ),
+ // [END_EXCLUDE]
+ rotaryMode = ScalingLazyColumnState.RotaryMode.Snap
+ )
+ ScreenScaffold(scrollState = columnState) {
+ ScalingLazyColumn(
+ columnState = columnState
+ ) {
+ // ...
+ // [START_EXCLUDE]
+ item {
+ ResponsiveListHeader(contentPadding = firstItemPadding()) {
+ androidx.wear.compose.material.Text(text = "Header")
+ }
+ }
+ // ... other items
+ item {
+ Button(
+ imageVector = Icons.Default.Build,
+ contentDescription = "Example Button",
+ onClick = { }
+ )
+ }
+ // [END_EXCLUDE]
+ }
+ }
+ // [END android_wear_snap]
+}
+
+// [START android_wear_list_breakpoint]
+const val LARGE_DISPLAY_BREAKPOINT = 225
+
+@Composable
+fun isLargeDisplay() =
+ LocalConfiguration.current.screenWidthDp >= LARGE_DISPLAY_BREAKPOINT
+
+// [START_EXCLUDE]
+@Composable
+fun breakpointDemo() {
+ // [END_EXCLUDE]
+// ... use in your Composables:
+ if (isLargeDisplay()) {
+ // Show additional content.
+ } else {
+ // Show content only for smaller displays.
+ }
+ // [START_EXCLUDE]
+}
+// [END_EXCLUDE]
+// [END android_wear_list_breakpoint]
+
+// [START android_wear_list_preview]
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun ComposeListPreview() {
+ ComposeList()
+}
+// [END android_wear_list_preview]
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun SnapAndFlingComposeListPreview() {
+ SnapAndFlingComposeList()
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/navigation/Navigation.kt b/wear/src/main/java/com/example/wear/snippets/m3/navigation/Navigation.kt
new file mode 100644
index 000000000..c595c54ef
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/navigation/Navigation.kt
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.navigation
+
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.style.TextAlign
+import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
+import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
+import androidx.wear.compose.material3.AppScaffold
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.ListHeader
+import androidx.wear.compose.material3.ScreenScaffold
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.navigation.SwipeDismissableNavHost
+import androidx.wear.compose.navigation.composable
+import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
+import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
+import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
+import com.example.wear.R
+import com.google.android.horologist.compose.layout.ColumnItemType
+import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
+
+@Composable
+fun navigation() {
+ // [START android_wear_navigation]
+ AppScaffold {
+ val navController = rememberSwipeDismissableNavController()
+ SwipeDismissableNavHost(
+ navController = navController,
+ startDestination = "message_list"
+ ) {
+ composable("message_list") {
+ MessageList(onMessageClick = { id ->
+ navController.navigate("message_detail/$id")
+ })
+ }
+ composable("message_detail/{id}") {
+ MessageDetail(id = it.arguments?.getString("id")!!)
+ }
+ }
+ }
+}
+
+// Implementation of one of the screens in the navigation
+@Composable
+fun MessageDetail(id: String) {
+ // .. Screen level content goes here
+ val scrollState = rememberTransformingLazyColumnState()
+
+ val padding = rememberResponsiveColumnPadding(
+ first = ColumnItemType.BodyText
+ )
+
+ ScreenScaffold(
+ scrollState = scrollState,
+ contentPadding = padding
+ ) {
+ // Screen content goes here
+ // [END android_wear_navigation]
+ TransformingLazyColumn(state = scrollState) {
+ item {
+ Text(
+ text = id,
+ textAlign = TextAlign.Center,
+ modifier = Modifier.fillMaxSize()
+ )
+ }
+ }
+ }
+}
+
+@Composable
+fun MessageList(onMessageClick: (String) -> Unit) {
+ val scrollState = rememberTransformingLazyColumnState()
+
+ val padding = rememberResponsiveColumnPadding(
+ first = ColumnItemType.ListHeader,
+ last = ColumnItemType.Button
+ )
+
+ ScreenScaffold(scrollState = scrollState, contentPadding = padding) { contentPadding ->
+ TransformingLazyColumn(
+ state = scrollState,
+ contentPadding = contentPadding
+ ) {
+ item {
+ ListHeader() {
+ Text(text = stringResource(R.string.message_list))
+ }
+ }
+ item {
+ Button(
+ onClick = { onMessageClick("message1") },
+ modifier = Modifier.fillMaxWidth()
+ ) {
+ Text(text = "Message 1")
+ }
+ }
+ item {
+ Button(
+ onClick = { onMessageClick("message2") },
+ modifier = Modifier.fillMaxWidth()
+ ) {
+ Text(text = "Message 2")
+ }
+ }
+ }
+ }
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun MessageDetailPreview() {
+ MessageDetail("test")
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun MessageListPreview() {
+ MessageList(onMessageClick = {})
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt b/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt
new file mode 100644
index 000000000..4cff56480
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt
@@ -0,0 +1,235 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.rotary
+
+import android.view.MotionEvent
+import androidx.compose.foundation.focusable
+import androidx.compose.foundation.gestures.scrollBy
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.width
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.derivedStateOf
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.focus.FocusRequester
+import androidx.compose.ui.focus.focusRequester
+import androidx.compose.ui.input.pointer.pointerInteropFilter
+import androidx.compose.ui.input.rotary.onRotaryScrollEvent
+import androidx.compose.ui.unit.dp
+import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
+import androidx.wear.compose.foundation.lazy.ScalingLazyColumnDefaults
+import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
+import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.ButtonDefaults
+import androidx.wear.compose.material3.ListHeader
+import androidx.wear.compose.material3.MaterialTheme
+import androidx.wear.compose.material3.Picker
+import androidx.wear.compose.material3.ScreenScaffold
+import androidx.wear.compose.material3.ScrollIndicator
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.material3.rememberPickerState
+import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
+import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
+import kotlinx.coroutines.launch
+
+@Composable
+fun TimePicker() {
+ val textStyle = MaterialTheme.typography.displayMedium
+
+ // [START android_wear_rotary_input_picker]
+ var selectedColumn by remember { mutableIntStateOf(0) }
+
+ val hoursFocusRequester = remember { FocusRequester() }
+ val minutesRequester = remember { FocusRequester() }
+ // [START_EXCLUDE]
+ val coroutineScope = rememberCoroutineScope()
+
+ @Composable
+ fun Option(column: Int, text: String) = Box(modifier = Modifier.fillMaxSize()) {
+ Text(
+ text = text, style = textStyle,
+ color = if (selectedColumn == column) MaterialTheme.colorScheme.secondary
+ else MaterialTheme.colorScheme.onBackground,
+ modifier = Modifier
+ .pointerInteropFilter {
+ if (it.action == MotionEvent.ACTION_DOWN) selectedColumn = column
+ true
+ }
+ )
+ }
+ // [END_EXCLUDE]
+ ScreenScaffold(modifier = Modifier.fillMaxSize()) {
+ Row(
+ // [START_EXCLUDE]
+ modifier = Modifier.fillMaxSize(),
+ verticalAlignment = Alignment.CenterVertically,
+ horizontalArrangement = Arrangement.Center,
+ // [END_EXCLUDE]
+ // ...
+ ) {
+ // [START_EXCLUDE]
+ val hourState = rememberPickerState(
+ initialNumberOfOptions = 12,
+ initiallySelectedIndex = 5
+ )
+ val hourContentDescription by remember {
+ derivedStateOf { "${hourState.selectedOptionIndex + 1 } hours" }
+ }
+ // [END_EXCLUDE]
+ Picker(
+ readOnly = selectedColumn != 0,
+ modifier = Modifier.size(64.dp, 100.dp)
+ .onRotaryScrollEvent {
+ coroutineScope.launch {
+ hourState.scrollBy(it.verticalScrollPixels)
+ }
+ true
+ }
+ .focusRequester(hoursFocusRequester)
+ .focusable(),
+ onSelected = { selectedColumn = 0 },
+ // ...
+ // [START_EXCLUDE]
+ state = hourState,
+ contentDescription = { hourContentDescription },
+ option = { hour: Int -> Option(0, "%2d".format(hour + 1)) }
+ // [END_EXCLUDE]
+ )
+ // [START_EXCLUDE]
+ Spacer(Modifier.width(8.dp))
+ Text(text = ":", style = textStyle, color = MaterialTheme.colorScheme.onBackground)
+ Spacer(Modifier.width(8.dp))
+ val minuteState =
+ rememberPickerState(initialNumberOfOptions = 60, initiallySelectedIndex = 0)
+ val minuteContentDescription by remember {
+ derivedStateOf { "${minuteState.selectedOptionIndex} minutes" }
+ }
+ // [END_EXCLUDE]
+ Picker(
+ readOnly = selectedColumn != 1,
+ modifier = Modifier.size(64.dp, 100.dp)
+ .onRotaryScrollEvent {
+ coroutineScope.launch {
+ minuteState.scrollBy(it.verticalScrollPixels)
+ }
+ true
+ }
+ .focusRequester(minutesRequester)
+ .focusable(),
+ onSelected = { selectedColumn = 1 },
+ // ...
+ // [START_EXCLUDE]
+ state = minuteState,
+ contentDescription = { minuteContentDescription },
+ option = { minute: Int -> Option(1, "%02d".format(minute)) }
+ // [END_EXCLUDE]
+ )
+ LaunchedEffect(selectedColumn) {
+ listOf(
+ hoursFocusRequester,
+ minutesRequester
+ )[selectedColumn]
+ .requestFocus()
+ }
+ }
+ }
+ // [END android_wear_rotary_input_picker]
+}
+
+@Composable
+fun SnapScrollableScreen() {
+ // This sample doesn't add a Time Text at the top of the screen.
+ // If using Time Text, add padding to ensure content does not overlap with Time Text.
+ // [START android_wear_rotary_input_snap_fling]
+ val listState = rememberScalingLazyListState()
+ ScreenScaffold(
+ scrollState = listState,
+ scrollIndicator = {
+ ScrollIndicator(state = listState)
+ }
+ ) {
+
+ val state = rememberScalingLazyListState()
+ ScalingLazyColumn(
+ modifier = Modifier.fillMaxWidth(),
+ state = state,
+ flingBehavior = ScalingLazyColumnDefaults.snapFlingBehavior(state = state)
+ ) {
+ // Content goes here
+ // [START_EXCLUDE]
+ item { ListHeader { Text(text = "List Header") } }
+ items(20) {
+ Button(
+ onClick = {},
+ label = { Text("List item $it") },
+ colors = ButtonDefaults.filledTonalButtonColors()
+ )
+ }
+ // [END_EXCLUDE]
+ }
+ }
+ // [END android_wear_rotary_input_snap_fling]
+}
+
+@Composable
+fun PositionScrollIndicator() {
+ // [START android_wear_rotary_position_indicator]
+ val listState = rememberTransformingLazyColumnState()
+ ScreenScaffold(
+ scrollState = listState,
+ scrollIndicator = {
+ ScrollIndicator(state = listState)
+ }
+ ) {
+ // ...
+ }
+ // [END android_wear_rotary_position_indicator]
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun TimePickerPreview() {
+ TimePicker()
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun SnapScrollableScreenPreview() {
+ SnapScrollableScreen()
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun PositionScrollIndicatorPreview() {
+ PositionScrollIndicator()
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/tile/Interaction.kt b/wear/src/main/java/com/example/wear/snippets/m3/tile/Interaction.kt
new file mode 100644
index 000000000..29d63d219
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/tile/Interaction.kt
@@ -0,0 +1,273 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.tile
+
+import android.content.ComponentName
+import android.content.Intent
+import androidx.core.app.TaskStackBuilder
+import androidx.core.net.toUri
+import androidx.wear.protolayout.ActionBuilders
+import androidx.wear.protolayout.ActionBuilders.launchAction
+import androidx.wear.protolayout.LayoutElementBuilders
+import androidx.wear.protolayout.ResourceBuilders.Resources
+import androidx.wear.protolayout.TimelineBuilders.Timeline
+import androidx.wear.protolayout.expression.dynamicDataMapOf
+import androidx.wear.protolayout.expression.intAppDataKey
+import androidx.wear.protolayout.expression.mapTo
+import androidx.wear.protolayout.expression.stringAppDataKey
+import androidx.wear.protolayout.material3.MaterialScope
+import androidx.wear.protolayout.material3.Typography.BODY_LARGE
+import androidx.wear.protolayout.material3.materialScope
+import androidx.wear.protolayout.material3.primaryLayout
+import androidx.wear.protolayout.material3.text
+import androidx.wear.protolayout.material3.textButton
+import androidx.wear.protolayout.modifiers.clickable
+import androidx.wear.protolayout.modifiers.loadAction
+import androidx.wear.protolayout.types.layoutString
+import androidx.wear.tiles.RequestBuilders
+import androidx.wear.tiles.RequestBuilders.ResourcesRequest
+import androidx.wear.tiles.TileBuilders.Tile
+import androidx.wear.tiles.TileService
+import com.google.common.util.concurrent.Futures
+import com.google.common.util.concurrent.ListenableFuture
+import java.util.Locale
+import kotlin.random.Random
+
+private const val RESOURCES_VERSION = "1"
+
+abstract class BaseTileService : TileService() {
+
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture =
+ Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ materialScope(this, requestParams.deviceConfiguration) {
+ tileLayout(requestParams)
+ }
+ )
+ )
+ .build()
+ )
+
+ override fun onTileResourcesRequest(
+ requestParams: ResourcesRequest
+ ): ListenableFuture =
+ Futures.immediateFuture(
+ Resources.Builder().setVersion(requestParams.version).build()
+ )
+
+ abstract fun MaterialScope.tileLayout(
+ requestParams: RequestBuilders.TileRequest
+ ): LayoutElementBuilders.LayoutElement
+}
+
+class HelloTileService : BaseTileService() {
+ override fun MaterialScope.tileLayout(
+ requestParams: RequestBuilders.TileRequest
+ ) = primaryLayout(mainSlot = { text("Hello, World!".layoutString) })
+}
+
+class InteractionRefresh : BaseTileService() {
+ override fun MaterialScope.tileLayout(
+ requestParams: RequestBuilders.TileRequest
+ ) =
+ primaryLayout(
+ // Output a debug code so we can see the layout changing
+ titleSlot = {
+ text(
+ String.format(
+ Locale.ENGLISH,
+ "Debug %06d",
+ Random.nextInt(0, 1_000_000),
+ )
+ .layoutString
+ )
+ },
+ mainSlot = {
+ // [START android_wear_m3_interaction_refresh]
+ textButton(
+ onClick = clickable(loadAction()),
+ labelContent = { text("Refresh".layoutString) },
+ )
+ // [END android_wear_m3_interaction_refresh]
+ },
+ )
+}
+
+class InteractionDeepLink : TileService() {
+
+ // [START android_wear_m3_interaction_deeplink_tile]
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ val lastClickableId = requestParams.currentState.lastClickableId
+ if (lastClickableId == "foo") {
+ TaskStackBuilder.create(this)
+ .addNextIntentWithParentStack(
+ Intent(
+ Intent.ACTION_VIEW,
+ "googleandroidsnippets://app/message_detail/1".toUri(),
+ this,
+ TileActivity::class.java,
+ )
+ )
+ .startActivities()
+ }
+ // ... User didn't tap a button (either first load or tapped somewhere else)
+ // [START_EXCLUDE]
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ materialScope(this, requestParams.deviceConfiguration) {
+ tileLayout(requestParams)
+ }
+ )
+ )
+ .build()
+ )
+ // [END_EXCLUDE]
+ }
+
+ // [END android_wear_m3_interaction_deeplink_tile]
+
+ override fun onTileResourcesRequest(
+ requestParams: ResourcesRequest
+ ): ListenableFuture =
+ Futures.immediateFuture(
+ Resources.Builder().setVersion(requestParams.version).build()
+ )
+
+ fun MaterialScope.tileLayout(requestParams: RequestBuilders.TileRequest) =
+ primaryLayout(
+ mainSlot = {
+ // [START android_wear_m3_interaction_deeplink_layout]
+ textButton(
+ labelContent = {
+ text("Deep Link me!".layoutString, typography = BODY_LARGE)
+ },
+ onClick = clickable(id = "foo", action = loadAction()),
+ )
+ // [END android_wear_m3_interaction_deeplink_layout]
+ }
+ )
+}
+
+class InteractionLoadAction : BaseTileService() {
+
+ // [START android_wear_m3_interaction_loadaction_request]
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+
+ // When triggered by loadAction(), "name" will be "Javier", and "age" will
+ // be 37.
+ with(requestParams.currentState.stateMap) {
+ val name = this[stringAppDataKey("name")]
+ val age = this[intAppDataKey("age")]
+ }
+
+ // [START_EXCLUDE]
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ materialScope(this, requestParams.deviceConfiguration) {
+ tileLayout(requestParams)
+ }
+ )
+ )
+ .build()
+ )
+ // [END_EXCLUDE]
+ }
+ // [END android_wear_m3_interaction_loadaction_request]
+
+ override fun MaterialScope.tileLayout(
+ requestParams: RequestBuilders.TileRequest
+ ) =
+ primaryLayout(
+ // Output a debug code so we can verify that the reload happens
+ titleSlot = {
+ text(
+ String.format(
+ Locale.ENGLISH,
+ "Debug %06d",
+ Random.nextInt(0, 1_000_000),
+ )
+ .layoutString
+ )
+ },
+ mainSlot = {
+ // [START android_wear_m3_interaction_loadaction_layout]
+ textButton(
+ labelContent = {
+ text("loadAction()".layoutString, typography = BODY_LARGE)
+ },
+ onClick =
+ clickable(
+ action =
+ loadAction(
+ dynamicDataMapOf(
+ stringAppDataKey("name") mapTo "Javier",
+ intAppDataKey("age") mapTo 37,
+ )
+ )
+ ),
+ )
+ // [END android_wear_m3_interaction_loadaction_layout]
+ },
+ )
+}
+
+class InteractionLaunchAction : BaseTileService() {
+
+ override fun MaterialScope.tileLayout(
+ requestParams: RequestBuilders.TileRequest
+ ) =
+ primaryLayout(
+ mainSlot = {
+ // [START android_wear_m3_interactions_launchaction]
+ textButton(
+ labelContent = {
+ text("launchAction()".layoutString, typography = BODY_LARGE)
+ },
+ onClick =
+ clickable(
+ action =
+ launchAction(
+ ComponentName(
+ "com.example.wear",
+ "com.example.wear.snippets.m3.tile.TileActivity",
+ ),
+ mapOf(
+ "name" to ActionBuilders.stringExtra("Bartholomew"),
+ "age" to ActionBuilders.intExtra(21),
+ ),
+ )
+ ),
+ )
+ // [END android_wear_m3_interactions_launchaction]
+ }
+ )
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/tile/Tile.kt b/wear/src/main/java/com/example/wear/snippets/m3/tile/Tile.kt
new file mode 100644
index 000000000..2ad9812e2
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/tile/Tile.kt
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.tile
+
+import androidx.wear.protolayout.ResourceBuilders.Resources
+import androidx.wear.protolayout.TimelineBuilders.Timeline
+import androidx.wear.protolayout.material3.Typography.BODY_LARGE
+import androidx.wear.protolayout.material3.materialScope
+import androidx.wear.protolayout.material3.primaryLayout
+import androidx.wear.protolayout.material3.text
+import androidx.wear.protolayout.types.layoutString
+import androidx.wear.tiles.RequestBuilders
+import androidx.wear.tiles.RequestBuilders.ResourcesRequest
+import androidx.wear.tiles.TileBuilders.Tile
+import androidx.wear.tiles.TileService
+import com.google.common.util.concurrent.Futures
+
+private const val RESOURCES_VERSION = "1"
+
+// [START android_wear_m3_tile_mytileservice]
+class MyTileService : TileService() {
+
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest) =
+ Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ materialScope(this, requestParams.deviceConfiguration) {
+ primaryLayout(
+ mainSlot = {
+ text("Hello, World!".layoutString, typography = BODY_LARGE)
+ }
+ )
+ }
+ )
+ )
+ .build()
+ )
+
+ override fun onTileResourcesRequest(requestParams: ResourcesRequest) =
+ Futures.immediateFuture(
+ Resources.Builder().setVersion(RESOURCES_VERSION).build()
+ )
+}
+// [END android_wear_m3_tile_mytileservice]
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/tile/TileActivity.kt b/wear/src/main/java/com/example/wear/snippets/m3/tile/TileActivity.kt
new file mode 100644
index 000000000..e9d4771f0
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/tile/TileActivity.kt
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.tile
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.style.TextAlign
+import androidx.navigation.navDeepLink
+import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
+import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
+import androidx.wear.compose.material3.AppScaffold
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.ListHeader
+import androidx.wear.compose.material3.ScreenScaffold
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.navigation.SwipeDismissableNavHost
+import androidx.wear.compose.navigation.composable
+import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
+import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
+import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
+import com.example.wear.R
+import com.google.android.horologist.compose.layout.ColumnItemType
+import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
+
+class TileActivity : ComponentActivity() {
+ // [START android_wear_m3_interactions_launchaction_activity]
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ // When this activity is launched from the tile InteractionLaunchAction,
+ // "name" will be "Bartholomew" and "age" will be 21
+ val name = intent.getStringExtra("name")
+ val age = intent.getStringExtra("age")
+
+ // [START_EXCLUDE]
+ setContent { MainContent() }
+ // [END_EXCLUDE]
+ }
+ // [END android_wear_m3_interactions_launchaction_activity]
+}
+
+@Composable
+fun MainContent() {
+ // [START android_wear_m3_interaction_deeplink_activity]
+ AppScaffold {
+ val navController = rememberSwipeDismissableNavController()
+ SwipeDismissableNavHost(
+ navController = navController,
+ startDestination = "message_list",
+ ) {
+ // [START_EXCLUDE]
+ composable(
+ route = "message_list",
+ deepLinks =
+ listOf(
+ navDeepLink {
+ uriPattern = "googleandroidsnippets://app/message_list"
+ }
+ ),
+ ) {
+ MessageList(
+ onMessageClick = { id ->
+ navController.navigate("message_detail/$id")
+ }
+ )
+ }
+ // [END_EXCLUDE]
+ composable(
+ route = "message_detail/{id}",
+ deepLinks =
+ listOf(
+ navDeepLink {
+ uriPattern = "googleandroidsnippets://app/message_detail/{id}"
+ }
+ ),
+ ) {
+ val id = it.arguments?.getString("id") ?: "0"
+ MessageDetails(details = "message $id")
+ }
+ }
+ }
+ // [END android_wear_m3_interaction_deeplink_activity]
+}
+
+// Implementation of one of the screens in the navigation
+@Composable
+fun MessageDetails(details: String) {
+ val scrollState = rememberTransformingLazyColumnState()
+
+ val padding = rememberResponsiveColumnPadding(first = ColumnItemType.BodyText)
+
+ ScreenScaffold(scrollState = scrollState, contentPadding = padding) {
+ scaffoldPaddingValues ->
+ TransformingLazyColumn(
+ state = scrollState,
+ contentPadding = scaffoldPaddingValues,
+ ) {
+ item {
+ ListHeader() { Text(text = stringResource(R.string.message_detail)) }
+ }
+ item {
+ Text(
+ text = details,
+ textAlign = TextAlign.Center,
+ modifier = Modifier.fillMaxSize(),
+ )
+ }
+ }
+ }
+}
+
+@Composable
+fun MessageList(onMessageClick: (String) -> Unit) {
+ val scrollState = rememberTransformingLazyColumnState()
+
+ val padding =
+ rememberResponsiveColumnPadding(
+ first = ColumnItemType.ListHeader,
+ last = ColumnItemType.Button,
+ )
+
+ ScreenScaffold(scrollState = scrollState, contentPadding = padding) {
+ contentPadding ->
+ TransformingLazyColumn(
+ state = scrollState,
+ contentPadding = contentPadding,
+ ) {
+ item {
+ ListHeader() { Text(text = stringResource(R.string.message_list)) }
+ }
+ item {
+ Button(
+ onClick = { onMessageClick("message1") },
+ modifier = Modifier.fillMaxWidth(),
+ ) {
+ Text(text = "Message 1")
+ }
+ }
+ item {
+ Button(
+ onClick = { onMessageClick("message2") },
+ modifier = Modifier.fillMaxWidth(),
+ ) {
+ Text(text = "Message 2")
+ }
+ }
+ }
+ }
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun MessageDetailPreview() {
+ MessageDetails("message 7")
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun MessageListPreview() {
+ MessageList(onMessageClick = {})
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/m3/voiceinput/VoiceInputScreen.kt b/wear/src/main/java/com/example/wear/snippets/m3/voiceinput/VoiceInputScreen.kt
new file mode 100644
index 000000000..d926487c5
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/m3/voiceinput/VoiceInputScreen.kt
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.m3.voiceinput
+
+import android.content.Intent
+import android.speech.RecognizerIntent
+import androidx.activity.compose.rememberLauncherForActivityResult
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.stringResource
+import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
+import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
+import androidx.wear.compose.material3.AppScaffold
+import androidx.wear.compose.material3.Button
+import androidx.wear.compose.material3.ScreenScaffold
+import androidx.wear.compose.material3.Text
+import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
+import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
+import com.example.wear.R
+import com.google.android.horologist.compose.layout.ColumnItemType
+import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
+
+/**
+ * Shows voice input option
+ */
+@Composable
+fun VoiceInputScreen() {
+ AppScaffold {
+ // [START android_wear_voice_input]
+ var textForVoiceInput by remember { mutableStateOf("") }
+
+ val voiceLauncher =
+ rememberLauncherForActivityResult(
+ ActivityResultContracts.StartActivityForResult()
+ ) { activityResult ->
+ // This is where you process the intent and extract the speech text from the intent.
+ activityResult.data?.let { data ->
+ val results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
+ textForVoiceInput = results?.get(0) ?: "None"
+ }
+ }
+
+ val scrollState = rememberTransformingLazyColumnState()
+ ScreenScaffold(
+ scrollState = scrollState,
+ contentPadding = rememberResponsiveColumnPadding(
+ first = ColumnItemType.Button
+ )
+ ) { contentPadding ->
+ TransformingLazyColumn(
+ contentPadding = contentPadding,
+ state = scrollState,
+ ) {
+ item {
+ // Create an intent that can start the Speech Recognizer activity
+ val voiceIntent: Intent =
+ Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
+ putExtra(
+ RecognizerIntent.EXTRA_LANGUAGE_MODEL,
+ RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
+ )
+
+ putExtra(
+ RecognizerIntent.EXTRA_PROMPT,
+ stringResource(R.string.voice_text_entry_label)
+ )
+ }
+ // Invoke the process from a Button
+ Button(
+ onClick = {
+ voiceLauncher.launch(voiceIntent)
+ },
+ label = { Text(stringResource(R.string.voice_input_label)) },
+ secondaryLabel = { Text(textForVoiceInput) },
+ modifier = Modifier.fillMaxWidth()
+ )
+ }
+ }
+ }
+ // [END android_wear_voice_input]
+ }
+}
+
+@WearPreviewDevices
+@WearPreviewFontScales
+@Composable
+fun VoiceInputScreenPreview() {
+ VoiceInputScreen()
+}
diff --git a/wear/src/main/java/com/example/wear/snippets/navigation/Navigation.kt b/wear/src/main/java/com/example/wear/snippets/navigation/Navigation.kt
index 42507078c..ed75220ab 100644
--- a/wear/src/main/java/com/example/wear/snippets/navigation/Navigation.kt
+++ b/wear/src/main/java/com/example/wear/snippets/navigation/Navigation.kt
@@ -26,6 +26,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
+import androidx.wear.compose.foundation.rememberActiveFocusRequester
+import androidx.wear.compose.foundation.rotary.RotaryScrollableDefaults.behavior
+import androidx.wear.compose.foundation.rotary.rotaryScrollable
import androidx.wear.compose.material.Text
import androidx.wear.compose.navigation.SwipeDismissableNavHost
import androidx.wear.compose.navigation.composable
@@ -43,7 +46,6 @@ import com.google.android.horologist.compose.layout.rememberResponsiveColumnStat
import com.google.android.horologist.compose.material.Chip
import com.google.android.horologist.compose.material.ListHeaderDefaults.firstItemPadding
import com.google.android.horologist.compose.material.ResponsiveListHeader
-import com.google.android.horologist.compose.rotaryinput.rotaryWithScroll
@Composable
fun navigation() {
@@ -81,12 +83,16 @@ fun MessageDetail(id: String) {
first = ItemType.Text,
last = ItemType.Text
)()
+ val focusRequester = rememberActiveFocusRequester()
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(scrollState)
- .rotaryWithScroll(scrollState)
- .padding(padding),
+ .padding(padding)
+ .rotaryScrollable(
+ behavior = behavior(scrollableState = scrollState),
+ focusRequester = focusRequester,
+ ),
verticalArrangement = Arrangement.Center
) {
Text(
diff --git a/wear/src/main/java/com/example/wear/snippets/tile/Animations.kt b/wear/src/main/java/com/example/wear/snippets/tile/Animations.kt
new file mode 100644
index 000000000..bb53c5ca5
--- /dev/null
+++ b/wear/src/main/java/com/example/wear/snippets/tile/Animations.kt
@@ -0,0 +1,364 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.wear.snippets.tile
+
+import android.annotation.SuppressLint
+import androidx.annotation.OptIn
+import androidx.wear.protolayout.DeviceParametersBuilders
+import androidx.wear.protolayout.DimensionBuilders.degrees
+import androidx.wear.protolayout.DimensionBuilders.dp
+import androidx.wear.protolayout.LayoutElementBuilders
+import androidx.wear.protolayout.LayoutElementBuilders.Arc
+import androidx.wear.protolayout.LayoutElementBuilders.ArcLine
+import androidx.wear.protolayout.ModifiersBuilders
+import androidx.wear.protolayout.ModifiersBuilders.AnimatedVisibility
+import androidx.wear.protolayout.ModifiersBuilders.DefaultContentTransitions
+import androidx.wear.protolayout.ModifiersBuilders.Modifiers
+import androidx.wear.protolayout.ResourceBuilders
+import androidx.wear.protolayout.ResourceBuilders.Resources
+import androidx.wear.protolayout.TimelineBuilders.Timeline
+import androidx.wear.protolayout.TriggerBuilders.createOnVisibleTrigger
+import androidx.wear.protolayout.TypeBuilders.FloatProp
+import androidx.wear.protolayout.expression.AnimationParameterBuilders.AnimationParameters
+import androidx.wear.protolayout.expression.AnimationParameterBuilders.AnimationSpec
+import androidx.wear.protolayout.expression.DynamicBuilders.DynamicFloat
+import androidx.wear.protolayout.expression.ProtoLayoutExperimental
+import androidx.wear.protolayout.material.CircularProgressIndicator
+import androidx.wear.protolayout.material.Text
+import androidx.wear.protolayout.material.layouts.EdgeContentLayout
+import androidx.wear.tiles.RequestBuilders
+import androidx.wear.tiles.RequestBuilders.ResourcesRequest
+import androidx.wear.tiles.TileBuilders.Tile
+import androidx.wear.tiles.TileService
+import com.example.wear.R
+import com.google.common.util.concurrent.Futures
+import com.google.common.util.concurrent.ListenableFuture
+
+// In a real implementation, this needs to change whenever the resources
+// change, to ensure the updated resources are loaded.
+private const val RESOURCES_VERSION = "1"
+private const val someTileText = "Hello"
+private val deviceParameters = DeviceParametersBuilders.DeviceParameters.Builder().build()
+
+private fun getTileTextToShow(): String {
+ return "Some text"
+}
+
+/** Demonstrates a sweep transition animation on a [CircularProgressIndicator]. */
+class AnimationSweepTransition : TileService() {
+ // [START android_wear_tile_animations_sweep_transition]
+ private var startValue = 15f
+ private var endValue = 105f
+ private val animationDurationInMillis = 2000L // 2 seconds
+
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture {
+ val circularProgressIndicator =
+ CircularProgressIndicator.Builder()
+ .setProgress(
+ FloatProp.Builder(/* static value */ 0.25f)
+ .setDynamicValue(
+ // Or you can use some other dynamic object, for example
+ // from the platform and then at the end of expression
+ // add animate().
+ DynamicFloat.animate(
+ startValue,
+ endValue,
+ AnimationSpec.Builder()
+ .setAnimationParameters(
+ AnimationParameters.Builder()
+ .setDurationMillis(animationDurationInMillis)
+ .build()
+ )
+ .build(),
+ )
+ )
+ .build()
+ )
+ .build()
+
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(Timeline.fromLayoutElement(circularProgressIndicator))
+ .build()
+ )
+ }
+ // [END android_wear_tile_animations_sweep_transition]
+}
+
+/** Demonstrates setting the growth direction of an [Arc] and [ArcLine]. */
+@SuppressLint("RestrictedApi")
+class AnimationArcDirection : TileService() {
+ // [START android_wear_tile_animations_set_arc_direction]
+ public override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ EdgeContentLayout.Builder(deviceParameters)
+ .setResponsiveContentInsetEnabled(true)
+ .setEdgeContent(
+ Arc.Builder()
+ // Arc should always grow clockwise.
+ .setArcDirection(LayoutElementBuilders.ARC_DIRECTION_CLOCKWISE)
+ .addContent(
+ ArcLine.Builder()
+ // Set color, length, thickness, and more.
+ // Arc should always grow clockwise.
+ .setArcDirection(
+ LayoutElementBuilders.ARC_DIRECTION_CLOCKWISE
+ )
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ }
+ // [END android_wear_tile_animations_set_arc_direction]
+}
+
+/** Demonstrates smooth fade-in and fade-out transitions. */
+class AnimationFadeTransition : TileService() {
+
+ @OptIn(ProtoLayoutExperimental::class)
+ // [START android_wear_tile_animations_fade]
+ public override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ // Assumes that you've defined a custom helper method called
+ // getTileTextToShow().
+ val tileText = getTileTextToShow()
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(this, tileText)
+ .setModifiers(
+ Modifiers.Builder()
+ .setContentUpdateAnimation(
+ AnimatedVisibility.Builder()
+ .setEnterTransition(DefaultContentTransitions.fadeIn())
+ .setExitTransition(DefaultContentTransitions.fadeOut())
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ }
+ // [END android_wear_tile_animations_fade]
+}
+
+/** Demonstrates smooth slide-in and slide-out transitions. */
+class AnimationSlideTransition : TileService() {
+ @OptIn(ProtoLayoutExperimental::class)
+ // [START android_wear_tile_animations_slide]
+ public override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ // Assumes that you've defined a custom helper method called
+ // getTileTextToShow().
+ val tileText = getTileTextToShow()
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(this, tileText)
+ .setModifiers(
+ Modifiers.Builder()
+ .setContentUpdateAnimation(
+ AnimatedVisibility.Builder()
+ .setEnterTransition(
+ DefaultContentTransitions.slideIn(
+ ModifiersBuilders.SLIDE_DIRECTION_LEFT_TO_RIGHT
+ )
+ )
+ .setExitTransition(
+ DefaultContentTransitions.slideOut(
+ ModifiersBuilders.SLIDE_DIRECTION_LEFT_TO_RIGHT
+ )
+ )
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ }
+ // [END android_wear_tile_animations_slide]
+}
+
+/** Demonstrates a rotation transformation. */
+class AnimationRotation : TileService() {
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture {
+ // [START android_wear_tile_animations_rotation]
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(this, someTileText)
+ .setModifiers(
+ Modifiers.Builder()
+ .setTransformation(
+ ModifiersBuilders.Transformation.Builder()
+ // Set the pivot point 50 dp from the left edge
+ // and 100 dp from the top edge of the screen.
+ .setPivotX(dp(50f))
+ .setPivotY(dp(100f))
+ // Rotate the element 45 degrees clockwise.
+ .setRotation(degrees(45f))
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ // [END android_wear_tile_animations_rotation]
+ }
+}
+
+/** Demonstrates a scaling transformation. */
+class AnimationScaling : TileService() {
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture {
+ // [START android_wear_tile_animations_scaling]
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(this, someTileText)
+ .setModifiers(
+ Modifiers.Builder()
+ .setTransformation(
+ ModifiersBuilders.Transformation.Builder()
+ // Set the pivot point 50 dp from the left edge
+ // and 100 dp from the top edge of the screen.
+ .setPivotX(dp(50f))
+ .setPivotY(dp(100f))
+ // Shrink the element by a scale factor
+ // of 0.5 horizontally and 0.75 vertically.
+ .setScaleX(FloatProp.Builder(0.5f).build())
+ .setScaleY(
+ FloatProp.Builder(0.75f).build()
+ )
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ // [END android_wear_tile_animations_scaling]
+ }
+}
+
+/** Demonstrates a geometric translation. */
+class AnimationGeometricTranslation : TileService() {
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture {
+ // [START android_wear_tile_animations_geometric_translation]
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(this, someTileText)
+ .setModifiers(
+ Modifiers.Builder()
+ .setTransformation(
+ ModifiersBuilders.Transformation.Builder()
+ // Translate (move) the element 60 dp to the right
+ // and 80 dp down.
+ .setTranslationX(dp(60f))
+ .setTranslationY(dp(80f))
+ .build()
+ )
+ .build()
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ // [END android_wear_tile_animations_geometric_translation]
+ }
+}
+
+// [START android_wear_tile_animations_lottie]
+class LottieAnimation : TileService() {
+
+ val lottieResourceId = "lottie_animation"
+
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture {
+
+ val layout =
+ LayoutElementBuilders.Image.Builder()
+ .setWidth(dp(150f))
+ .setHeight(dp(150f))
+ .setResourceId(lottieResourceId)
+ .build()
+
+ return Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(Timeline.fromLayoutElement(layout))
+ .build()
+ )
+ }
+
+ override fun onTileResourcesRequest(
+ requestParams: ResourcesRequest
+ ): ListenableFuture {
+
+ val lottieImage =
+ ResourceBuilders.ImageResource.Builder()
+ .setAndroidLottieResourceByResId(
+ ResourceBuilders.AndroidLottieResourceByResId.Builder(R.raw.lottie)
+ .setStartTrigger(createOnVisibleTrigger())
+ .build()
+ )
+ .build()
+
+ return Futures.immediateFuture(
+ Resources.Builder()
+ .setVersion(requestParams.version)
+ .addIdToImageMapping(lottieResourceId, lottieImage)
+ .build()
+ )
+ }
+}
+// [END android_wear_tile_animations_lottie]
diff --git a/wear/src/main/java/com/example/wear/snippets/tile/Tile.kt b/wear/src/main/java/com/example/wear/snippets/tile/Tile.kt
index 58cbaa758..9e2a9508e 100644
--- a/wear/src/main/java/com/example/wear/snippets/tile/Tile.kt
+++ b/wear/src/main/java/com/example/wear/snippets/tile/Tile.kt
@@ -16,9 +16,18 @@
package com.example.wear.snippets.tile
+import android.Manifest
+import android.content.Context
+import androidx.annotation.RequiresPermission
import androidx.wear.protolayout.ColorBuilders.argb
+import androidx.wear.protolayout.DimensionBuilders
+import androidx.wear.protolayout.LayoutElementBuilders
import androidx.wear.protolayout.ResourceBuilders.Resources
+import androidx.wear.protolayout.TimelineBuilders
import androidx.wear.protolayout.TimelineBuilders.Timeline
+import androidx.wear.protolayout.TypeBuilders
+import androidx.wear.protolayout.expression.DynamicBuilders
+import androidx.wear.protolayout.expression.PlatformHealthSources
import androidx.wear.protolayout.material.Text
import androidx.wear.protolayout.material.Typography
import androidx.wear.tiles.RequestBuilders
@@ -26,6 +35,7 @@ import androidx.wear.tiles.RequestBuilders.ResourcesRequest
import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TileService
import com.google.common.util.concurrent.Futures
+import com.google.common.util.concurrent.ListenableFuture
private const val RESOURCES_VERSION = "1"
@@ -48,10 +58,145 @@ class MyTileService : TileService() {
)
override fun onTileResourcesRequest(requestParams: ResourcesRequest) =
+ Futures.immediateFuture(Resources.Builder().setVersion(RESOURCES_VERSION).build())
+}
+
+// [END android_wear_tile_mytileservice]
+
+fun simpleLayout(context: Context) =
+ Text.Builder(context, "Hello World!")
+ .setTypography(Typography.TYPOGRAPHY_BODY1)
+ .setColor(argb(0xFFFFFFFF.toInt()))
+ .build()
+
+class PeriodicUpdatesSingleEntry : TileService() {
+ // [START android_wear_tile_periodic_single_entry]
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ val tile =
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ // We add a single timeline entry when our layout is fixed, and
+ // we don't know in advance when its contents might change.
+ .setTileTimeline(Timeline.fromLayoutElement(simpleLayout(this)))
+ .build()
+ return Futures.immediateFuture(tile)
+ }
+ // [END android_wear_tile_periodic_single_entry]
+}
+
+fun emptySpacer(): LayoutElementBuilders.LayoutElement {
+ return LayoutElementBuilders.Spacer.Builder()
+ .setWidth(DimensionBuilders.dp(0f))
+ .setHeight(DimensionBuilders.dp(0f))
+ .build()
+}
+
+fun getNoMeetingsLayout(): LayoutElementBuilders.Layout {
+ return LayoutElementBuilders.Layout.Builder().setRoot(emptySpacer()).build()
+}
+
+fun getMeetingLayout(meeting: Meeting): LayoutElementBuilders.Layout {
+ return LayoutElementBuilders.Layout.Builder().setRoot(emptySpacer()).build()
+}
+
+data class Meeting(val name: String, val dateTimeMillis: Long)
+
+object MeetingsRepo {
+ fun getMeetings(): List {
+ val now = System.currentTimeMillis()
+ return listOf(
+ Meeting("Meeting 1", now + 1 * 60 * 60 * 1000), // 1 hour from now
+ Meeting("Meeting 2", now + 3 * 60 * 60 * 1000), // 3 hours from now
+ )
+ }
+}
+
+class PeriodicUpdatesTimebound : TileService() {
+ // [START android_wear_tile_periodic_timebound]
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture {
+ val timeline = Timeline.Builder()
+
+ // Add fallback "no meetings" entry
+ // Use the version of TimelineEntry that's in androidx.wear.protolayout.
+ timeline.addTimelineEntry(
+ TimelineBuilders.TimelineEntry.Builder().setLayout(getNoMeetingsLayout()).build()
+ )
+
+ // Retrieve a list of scheduled meetings
+ val meetings = MeetingsRepo.getMeetings()
+ // Add a timeline entry for each meeting
+ meetings.forEach { meeting ->
+ timeline.addTimelineEntry(
+ TimelineBuilders.TimelineEntry.Builder()
+ .setLayout(getMeetingLayout(meeting))
+ .setValidity(
+ // The tile should disappear when the meeting begins
+ // Use the version of TimeInterval that's in
+ // androidx.wear.protolayout.
+ TimelineBuilders.TimeInterval.Builder()
+ .setEndMillis(meeting.dateTimeMillis)
+ .build()
+ )
+ .build()
+ )
+ }
+
+ val tile =
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setTileTimeline(timeline.build())
+ .build()
+ return Futures.immediateFuture(tile)
+ }
+ // [END android_wear_tile_periodic_timebound]
+}
+
+fun getWeatherLayout() = emptySpacer()
+
+class PeriodicUpdatesRefresh : TileService() {
+ // [START android_wear_tile_periodic_refresh]
+ override fun onTileRequest(
+ requestParams: RequestBuilders.TileRequest
+ ): ListenableFuture =
Futures.immediateFuture(
- Resources.Builder()
- .setVersion(RESOURCES_VERSION)
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setFreshnessIntervalMillis(60 * 60 * 1000) // 60 minutes
+ .setTileTimeline(Timeline.fromLayoutElement(getWeatherLayout()))
.build()
)
+ // [END android_wear_tile_periodic_refresh]
+}
+
+class DynamicHeartRate : TileService() {
+ @RequiresPermission(Manifest.permission.BODY_SENSORS)
+ // [START android_wear_tile_dynamic_heart_rate]
+ override fun onTileRequest(requestParams: RequestBuilders.TileRequest) =
+ Futures.immediateFuture(
+ Tile.Builder()
+ .setResourcesVersion(RESOURCES_VERSION)
+ .setFreshnessIntervalMillis(60 * 60 * 1000) // 60 minutes
+ .setTileTimeline(
+ Timeline.fromLayoutElement(
+ Text.Builder(
+ this,
+ TypeBuilders.StringProp.Builder("--")
+ .setDynamicValue(
+ PlatformHealthSources.heartRateBpm()
+ .format()
+ .concat(DynamicBuilders.DynamicString.constant(" bpm"))
+ )
+ .build(),
+ TypeBuilders.StringLayoutConstraint.Builder("000").build(),
+ )
+ .build()
+ )
+ )
+ .build()
+ )
+ // [END android_wear_tile_dynamic_heart_rate]
}
-// [END android_wear_tile_mytileservice]
diff --git a/wear/src/main/java/com/example/wear/snippets/voiceinput/VoiceInputScreen.kt b/wear/src/main/java/com/example/wear/snippets/voiceinput/VoiceInputScreen.kt
index fa80ab800..31bbde0e9 100644
--- a/wear/src/main/java/com/example/wear/snippets/voiceinput/VoiceInputScreen.kt
+++ b/wear/src/main/java/com/example/wear/snippets/voiceinput/VoiceInputScreen.kt
@@ -49,6 +49,9 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
+import androidx.wear.compose.foundation.rememberActiveFocusRequester
+import androidx.wear.compose.foundation.rotary.RotaryScrollableDefaults.behavior
+import androidx.wear.compose.foundation.rotary.rotaryScrollable
import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
import com.example.wear.R
@@ -58,7 +61,6 @@ import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults
import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults.ItemType
import com.google.android.horologist.compose.layout.ScreenScaffold
import com.google.android.horologist.compose.material.Chip
-import com.google.android.horologist.compose.rotaryinput.rotaryWithScroll
/**
* Shows voice input option
@@ -90,6 +92,7 @@ fun VoiceInputScreen() {
first = ItemType.Text,
last = ItemType.Chip
)()
+ val focusRequester = rememberActiveFocusRequester()
// [END_EXCLUDE]
Column(
// rest of implementation here
@@ -97,8 +100,11 @@ fun VoiceInputScreen() {
modifier = Modifier
.fillMaxSize()
.verticalScroll(scrollState)
- .rotaryWithScroll(scrollState)
- .padding(padding),
+ .padding(padding)
+ .rotaryScrollable(
+ behavior = behavior(scrollableState = scrollState),
+ focusRequester = focusRequester,
+ ),
verticalArrangement = Arrangement.Center
) {
// [END_EXCLUDE]
diff --git a/wear/src/main/res/drawable/animated_walk.xml b/wear/src/main/res/drawable/animated_walk.xml
new file mode 100644
index 000000000..e94991e07
--- /dev/null
+++ b/wear/src/main/res/drawable/animated_walk.xml
@@ -0,0 +1,682 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/wear/src/main/res/drawable/complication_icon.xml b/wear/src/main/res/drawable/complication_icon.xml
new file mode 100644
index 000000000..1793d6ba5
--- /dev/null
+++ b/wear/src/main/res/drawable/complication_icon.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
diff --git a/wear/src/main/res/drawable/ic_walk.xml b/wear/src/main/res/drawable/ic_walk.xml
new file mode 100644
index 000000000..6c226e943
--- /dev/null
+++ b/wear/src/main/res/drawable/ic_walk.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
diff --git a/wear/src/main/res/raw/lottie.json b/wear/src/main/res/raw/lottie.json
new file mode 100644
index 000000000..8725aba51
--- /dev/null
+++ b/wear/src/main/res/raw/lottie.json
@@ -0,0 +1,240 @@
+{
+ "v": "5.12.2",
+ "fr": 29.9700012207031,
+ "ip": 0,
+ "op": 149.000006068894,
+ "w": 256,
+ "h": 256,
+ "nm": "TestLotties",
+ "ddd": 0,
+ "assets": [],
+ "layers": [
+ {
+ "ddd": 0,
+ "ind": 1,
+ "ty": 4,
+ "nm": "Shape Layer 1",
+ "sr": 1,
+ "ks": {
+ "o": {
+ "a": 0,
+ "k": 100,
+ "ix": 11
+ },
+ "r": {
+ "a": 0,
+ "k": 0,
+ "ix": 10
+ },
+ "p": {
+ "a": 0,
+ "k": [128, 128, 0],
+ "ix": 2,
+ "l": 2
+ },
+ "a": {
+ "a": 0,
+ "k": [0, 0, 0],
+ "ix": 1,
+ "l": 2
+ },
+ "s": {
+ "a": 0,
+ "k": [100, 100, 100],
+ "ix": 6,
+ "l": 2
+ }
+ },
+ "ao": 0,
+ "shapes": [
+ {
+ "ty": "gr",
+ "it": [
+ {
+ "ty": "rc",
+ "d": 1,
+ "s": {
+ "a": 0,
+ "k": [147.598, 101.668],
+ "ix": 2
+ },
+ "p": {
+ "a": 0,
+ "k": [0, 0],
+ "ix": 3
+ },
+ "r": {
+ "a": 0,
+ "k": 0,
+ "ix": 4
+ },
+ "nm": "Rectangle Path 1",
+ "mn": "ADBE Vector Shape - Rect",
+ "hd": false
+ },
+ {
+ "ty": "fl",
+ "c": {
+ "a": 0,
+ "k": [0.373262771906, 0.896561446844, 0.9078125, 1],
+ "ix": 4
+ },
+ "o": {
+ "a": 0,
+ "k": 100,
+ "ix": 5
+ },
+ "r": 1,
+ "bm": 0,
+ "nm": "Fill 1",
+ "mn": "ADBE Vector Graphic - Fill",
+ "hd": false
+ },
+ {
+ "ty": "tr",
+ "p": {
+ "a": 1,
+ "k": [
+ {
+ "i": {
+ "x": 0.833,
+ "y": 0.833
+ },
+ "o": {
+ "x": 0.167,
+ "y": 0.167
+ },
+ "t": 89,
+ "s": [-3.201, -3.166],
+ "to": [0, 0],
+ "ti": [0, 0]
+ },
+ {
+ "t": 118.000004806239,
+ "s": [-61.201, 57.834]
+ }
+ ],
+ "ix": 2
+ },
+ "a": {
+ "a": 1,
+ "k": [
+ {
+ "i": {
+ "x": 0.833,
+ "y": 0.833
+ },
+ "o": {
+ "x": 0.167,
+ "y": 0.167
+ },
+ "t": 60,
+ "s": [26, 10],
+ "to": [0, 0],
+ "ti": [0, 0]
+ },
+ {
+ "t": 89.0000036250443,
+ "s": [1, 45]
+ }
+ ],
+ "ix": 1
+ },
+ "s": {
+ "a": 1,
+ "k": [
+ {
+ "i": {
+ "x": [0.833, 0.833],
+ "y": [0.833, 0.833]
+ },
+ "o": {
+ "x": [0.167, 0.167],
+ "y": [0.167, 0.167]
+ },
+ "t": 118,
+ "s": [57, 126]
+ },
+ {
+ "t": 146.000005946702,
+ "s": [107, 69]
+ }
+ ],
+ "ix": 3
+ },
+ "r": {
+ "a": 1,
+ "k": [
+ {
+ "i": {
+ "x": [0.833],
+ "y": [0.833]
+ },
+ "o": {
+ "x": [0.167],
+ "y": [0.167]
+ },
+ "t": 28,
+ "s": [18]
+ },
+ {
+ "t": 60.0000024438501,
+ "s": [60]
+ }
+ ],
+ "ix": 6
+ },
+ "o": {
+ "a": 1,
+ "k": [
+ {
+ "i": {
+ "x": [0.833],
+ "y": [0.833]
+ },
+ "o": {
+ "x": [0.167],
+ "y": [0.167]
+ },
+ "t": 0,
+ "s": [48]
+ },
+ {
+ "t": 28.0000011404634,
+ "s": [100]
+ }
+ ],
+ "ix": 7
+ },
+ "sk": {
+ "a": 0,
+ "k": 0,
+ "ix": 4
+ },
+ "sa": {
+ "a": 0,
+ "k": 0,
+ "ix": 5
+ },
+ "nm": "Transform"
+ }
+ ],
+ "nm": "Rectangle 1",
+ "np": 3,
+ "cix": 2,
+ "bm": 0,
+ "ix": 1,
+ "mn": "ADBE Vector Group",
+ "hd": false
+ }
+ ],
+ "ip": 0,
+ "op": 900.000036657751,
+ "st": 0,
+ "ct": 1,
+ "bm": 0
+ }
+ ],
+ "markers": [],
+ "props": {}
+}
diff --git a/wear/src/main/res/values/strings.xml b/wear/src/main/res/values/strings.xml
index fc59c67b8..19e308a68 100644
--- a/wear/src/main/res/values/strings.xml
+++ b/wear/src/main/res/values/strings.xml
@@ -3,6 +3,10 @@
Voice Input
Voice Text Entry
Message List
+ Message Detail
Hello Tile
Hello Tile Description
+ My Complication
+ My Timeline Complication
+ Configuration activity
\ No newline at end of file
diff --git a/xr/.gitignore b/xr/.gitignore
new file mode 100644
index 000000000..42afabfd2
--- /dev/null
+++ b/xr/.gitignore
@@ -0,0 +1 @@
+/build
\ No newline at end of file
diff --git a/xr/build.gradle.kts b/xr/build.gradle.kts
new file mode 100644
index 000000000..51c224f20
--- /dev/null
+++ b/xr/build.gradle.kts
@@ -0,0 +1,69 @@
+plugins {
+ alias(libs.plugins.android.application)
+ alias(libs.plugins.kotlin.android)
+ alias(libs.plugins.compose.compiler)
+}
+
+android {
+ namespace = "com.example.xr"
+ compileSdk = 35
+
+ defaultConfig {
+ applicationId = "com.example.xr"
+ minSdk = 34
+ targetSdk = 35
+ versionCode = 1
+ versionName = "1.0"
+ }
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_11
+ targetCompatibility = JavaVersion.VERSION_11
+ }
+ kotlin {
+ jvmToolchain(11)
+ }
+ buildFeatures {
+ compose = true
+ }
+ lint {
+ disable += "RestrictedApi"
+ }
+}
+
+dependencies {
+ implementation(libs.androidx.xr.arcore)
+ implementation(libs.androidx.xr.scenecore)
+ implementation(libs.androidx.xr.compose)
+
+ implementation(libs.androidx.activity.ktx)
+
+ implementation(libs.androidx.media3.exoplayer)
+
+ val composeBom = platform(libs.androidx.compose.bom)
+ implementation(composeBom)
+
+ implementation(libs.androidx.compose.ui)
+ implementation(libs.androidx.compose.ui.util)
+ implementation(libs.androidx.compose.ui.graphics)
+ implementation(libs.androidx.graphics.shapes)
+ implementation(libs.androidx.compose.ui.tooling.preview)
+ implementation(libs.androidx.compose.ui.viewbinding)
+ implementation(libs.androidx.paging.compose)
+ implementation(libs.androidx.compose.animation.graphics)
+
+ implementation(libs.androidx.compose.material3)
+ implementation(libs.androidx.compose.material3.adaptive)
+ implementation(libs.androidx.compose.material3.adaptive.layout)
+ implementation(libs.androidx.compose.material3.adaptive.navigation)
+ implementation(libs.androidx.compose.material3.adaptive.navigation.suite)
+ implementation(libs.androidx.compose.material)
+
+ implementation(libs.androidx.compose.runtime)
+ implementation(libs.androidx.compose.runtime.livedata)
+ implementation(libs.androidx.compose.material.iconsExtended)
+ implementation(libs.androidx.compose.material.ripple)
+ implementation(libs.androidx.constraintlayout.compose)
+
+ implementation(libs.androidx.activity.compose)
+ implementation(libs.androidx.appcompat)
+}
\ No newline at end of file
diff --git a/xr/src/main/AndroidManifest.xml b/xr/src/main/AndroidManifest.xml
new file mode 100644
index 000000000..6d6399c14
--- /dev/null
+++ b/xr/src/main/AndroidManifest.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt b/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt
new file mode 100644
index 000000000..e3d38c0f8
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/arcore/AnchorPersistence.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.arcore
+
+import androidx.xr.arcore.Anchor
+import androidx.xr.arcore.AnchorCreateSuccess
+import androidx.xr.runtime.Session
+import java.util.UUID
+
+private suspend fun persistAnchor(anchor: Anchor) {
+ // [START androidxr_arcore_anchor_persist]
+ val uuid = anchor.persist()
+ // [END androidxr_arcore_anchor_persist]
+}
+
+private fun loadAnchor(session: Session, uuid: UUID) {
+ // [START androidxr_arcore_anchor_load]
+ when (val result = Anchor.load(session, uuid)) {
+ is AnchorCreateSuccess -> {
+ // Loading was successful. The anchor is stored in result.anchor.
+ }
+ else -> {
+ // handle failure
+ }
+ }
+ // [END androidxr_arcore_anchor_load]
+}
+
+private fun unpersistAnchor(session: Session, uuid: UUID) {
+ // [START androidxr_arcore_anchor_unpersist]
+ Anchor.unpersist(session, uuid)
+ // [END androidxr_arcore_anchor_unpersist]
+}
+
+private fun getPersistedAnchorUuids(session: Session) {
+ // [START androidxr_arcore_anchor_get_uuids]
+ val uuids = Anchor.getPersistedAnchorUuids(session)
+ // [END androidxr_arcore_anchor_get_uuids]
+}
diff --git a/xr/src/main/java/com/example/xr/arcore/Anchors.kt b/xr/src/main/java/com/example/xr/arcore/Anchors.kt
new file mode 100644
index 000000000..56c7cb8d7
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/arcore/Anchors.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.arcore
+
+import androidx.xr.arcore.Anchor
+import androidx.xr.arcore.AnchorCreateSuccess
+import androidx.xr.arcore.Trackable
+import androidx.xr.runtime.Config
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
+import androidx.xr.runtime.SessionConfigureSuccess
+import androidx.xr.runtime.math.Pose
+import androidx.xr.scenecore.AnchorEntity
+import androidx.xr.scenecore.Entity
+import androidx.xr.scenecore.scene
+
+fun configureAnchoring(session: Session) {
+ // [START androidxr_arcore_anchoring_configure]
+ val newConfig = session.config.copy(
+ anchorPersistence = Config.AnchorPersistenceMode.LOCAL,
+ )
+ when (val result = session.configure(newConfig)) {
+ is SessionConfigureConfigurationNotSupported ->
+ TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
+ is SessionConfigureSuccess -> TODO(/* Success! */)
+ else ->
+ TODO(/* A different unhandled exception was thrown. */)
+ }
+ // [END androidxr_arcore_anchoring_configure]
+}
+
+private fun createAnchorAtPose(session: Session, pose: Pose) {
+ val pose = Pose()
+ // [START androidxr_arcore_anchor_create]
+ when (val result = Anchor.create(session, pose)) {
+ is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
+ else -> { /* handle failure */ }
+ }
+ // [END androidxr_arcore_anchor_create]
+}
+
+private fun createAnchorAtTrackable(trackable: Trackable<*>) {
+ val pose = Pose()
+ // [START androidxr_arcore_anchor_create_trackable]
+ when (val result = trackable.createAnchor(pose)) {
+ is AnchorCreateSuccess -> { /* anchor stored in `result.anchor`. */ }
+ else -> { /* handle failure */ }
+ }
+ // [END androidxr_arcore_anchor_create_trackable]
+}
+
+private fun attachEntityToAnchor(
+ session: Session,
+ entity: Entity,
+ anchor: Anchor
+) {
+ // [START androidxr_arcore_entity_tracks_anchor]
+ AnchorEntity.create(session, anchor).apply {
+ parent = session.scene.activitySpace
+ addChild(entity)
+ }
+ // [END androidxr_arcore_entity_tracks_anchor]
+}
diff --git a/xr/src/main/java/com/example/xr/arcore/Hands.kt b/xr/src/main/java/com/example/xr/arcore/Hands.kt
new file mode 100644
index 000000000..1507bed7c
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/arcore/Hands.kt
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.arcore
+
+import android.app.Activity
+import androidx.activity.ComponentActivity
+import androidx.lifecycle.lifecycleScope
+import androidx.xr.arcore.Hand
+import androidx.xr.runtime.Config
+import androidx.xr.runtime.HandJointType
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
+import androidx.xr.runtime.SessionConfigureSuccess
+import androidx.xr.runtime.math.Pose
+import androidx.xr.runtime.math.Quaternion
+import androidx.xr.runtime.math.Vector3
+import androidx.xr.runtime.math.toRadians
+import androidx.xr.scenecore.GltfModelEntity
+import androidx.xr.scenecore.scene
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.launch
+
+fun ComponentActivity.configureSession(session: Session) {
+ // [START androidxr_arcore_hand_configure]
+ val newConfig = session.config.copy(
+ handTracking = Config.HandTrackingMode.BOTH
+ )
+ when (val result = session.configure(newConfig)) {
+ is SessionConfigureConfigurationNotSupported ->
+ TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
+ is SessionConfigureSuccess -> TODO(/* Success! */)
+ else ->
+ TODO(/* A different unhandled exception was thrown. */)
+ }
+ // [END androidxr_arcore_hand_configure]
+}
+
+fun ComponentActivity.collectHands(session: Session) {
+ lifecycleScope.launch {
+ // [START androidxr_arcore_hand_collect]
+ Hand.left(session)?.state?.collect { handState -> // or Hand.right(session)
+ // Hand state has been updated.
+ // Use the state of hand joints to update an entity's position.
+ renderPlanetAtHandPalm(handState)
+ }
+ // [END androidxr_arcore_hand_collect]
+ }
+ lifecycleScope.launch {
+ Hand.right(session)?.state?.collect { rightHandState ->
+ renderPlanetAtFingerTip(rightHandState)
+ }
+ }
+}
+
+fun secondaryHandDetection(activity: Activity, session: Session) {
+ fun detectGesture(handState: Flow) {}
+ // [START androidxr_arcore_hand_handedness]
+ val handedness = Hand.getPrimaryHandSide(activity.contentResolver)
+ val secondaryHand = if (handedness == Hand.HandSide.LEFT) Hand.right(session) else Hand.left(session)
+ val handState = secondaryHand?.state ?: return
+ detectGesture(handState)
+ // [END androidxr_arcore_hand_handedness]
+}
+
+fun ComponentActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
+ val session: Session = null!!
+ val palmEntity: GltfModelEntity = null!!
+ // [START androidxr_arcore_hand_entityAtHandPalm]
+ val palmPose = leftHandState.handJoints[HandJointType.PALM] ?: return
+
+ // the down direction points in the same direction as the palm
+ val angle = Vector3.angleBetween(palmPose.rotation * Vector3.Down, Vector3.Up)
+ palmEntity.setEnabled(angle > Math.toRadians(40.0))
+
+ val transformedPose =
+ session.scene.perceptionSpace.transformPoseTo(
+ palmPose,
+ session.scene.activitySpace,
+ )
+ val newPosition = transformedPose.translation + transformedPose.down * 0.05f
+ palmEntity.setPose(Pose(newPosition, transformedPose.rotation))
+ // [END androidxr_arcore_hand_entityAtHandPalm]
+}
+
+fun ComponentActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
+ val session: Session = null!!
+ val indexFingerEntity: GltfModelEntity = null!!
+
+ // [START androidxr_arcore_hand_entityAtIndexFingerTip]
+ val tipPose = rightHandState.handJoints[HandJointType.INDEX_TIP] ?: return
+
+ // the forward direction points towards the finger tip.
+ val angle = Vector3.angleBetween(tipPose.rotation * Vector3.Forward, Vector3.Up)
+ indexFingerEntity.setEnabled(angle > Math.toRadians(40.0))
+
+ val transformedPose =
+ session.scene.perceptionSpace.transformPoseTo(
+ tipPose,
+ session.scene.activitySpace,
+ )
+ val position = transformedPose.translation + transformedPose.forward * 0.03f
+ val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up)
+ indexFingerEntity.setPose(Pose(position, rotation))
+ // [END androidxr_arcore_hand_entityAtIndexFingerTip]
+}
+
+private fun detectPinch(session: Session, handState: Hand.State): Boolean {
+ // [START androidxr_arcore_hand_pinch_gesture]
+ val thumbTip = handState.handJoints[HandJointType.THUMB_TIP] ?: return false
+ val thumbTipPose = session.scene.perceptionSpace.transformPoseTo(thumbTip, session.scene.activitySpace)
+ val indexTip = handState.handJoints[HandJointType.INDEX_TIP] ?: return false
+ val indexTipPose = session.scene.perceptionSpace.transformPoseTo(indexTip, session.scene.activitySpace)
+ return Vector3.distance(thumbTipPose.translation, indexTipPose.translation) < 0.05
+ // [END androidxr_arcore_hand_pinch_gesture]
+}
+
+private fun detectStop(session: Session, handState: Hand.State): Boolean {
+ // [START androidxr_arcore_hand_stop_gesture]
+ val threshold = toRadians(angleInDegrees = 30f)
+ fun pointingInSameDirection(joint1: HandJointType, joint2: HandJointType): Boolean {
+ val forward1 = handState.handJoints[joint1]?.forward ?: return false
+ val forward2 = handState.handJoints[joint2]?.forward ?: return false
+ return Vector3.angleBetween(forward1, forward2) < threshold
+ }
+ return pointingInSameDirection(HandJointType.INDEX_PROXIMAL, HandJointType.INDEX_TIP) &&
+ pointingInSameDirection(HandJointType.MIDDLE_PROXIMAL, HandJointType.MIDDLE_TIP) &&
+ pointingInSameDirection(HandJointType.RING_PROXIMAL, HandJointType.RING_TIP)
+ // [END androidxr_arcore_hand_stop_gesture]
+}
diff --git a/xr/src/main/java/com/example/xr/arcore/Planes.kt b/xr/src/main/java/com/example/xr/arcore/Planes.kt
new file mode 100644
index 000000000..b5017dcb7
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/arcore/Planes.kt
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.arcore
+
+import androidx.xr.arcore.Plane
+import androidx.xr.runtime.Config
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
+import androidx.xr.runtime.SessionConfigureSuccess
+import androidx.xr.runtime.math.Pose
+import androidx.xr.runtime.math.Ray
+import androidx.xr.scenecore.scene
+
+fun configurePlaneTracking(session: Session) {
+ // [START androidxr_arcore_planetracking_configure]
+ val newConfig = session.config.copy(
+ planeTracking = Config.PlaneTrackingMode.HORIZONTAL_AND_VERTICAL,
+ )
+ when (val result = session.configure(newConfig)) {
+ is SessionConfigureConfigurationNotSupported ->
+ TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
+ is SessionConfigureSuccess -> TODO(/* Success! */)
+ else ->
+ TODO(/* A different unhandled exception was thrown. */)
+ }
+ // [END androidxr_arcore_planetracking_configure]
+}
+
+private suspend fun subscribePlanes(session: Session) {
+ // [START androidxr_arcore_planes_subscribe]
+ Plane.subscribe(session).collect { planes ->
+ // Planes have changed; update plane rendering
+ }
+ // [END androidxr_arcore_planes_subscribe]
+}
+
+private fun hitTestTable(session: Session) {
+ val pose = session.scene.spatialUser.head?.transformPoseTo(Pose(), session.scene.perceptionSpace) ?: return
+ val ray = Ray(pose.translation, pose.forward)
+ // [START androidxr_arcore_hitTest]
+ val results = androidx.xr.arcore.hitTest(session, ray)
+ // When interested in the first Table hit:
+ val tableHit = results.firstOrNull {
+ val trackable = it.trackable
+ trackable is Plane && trackable.state.value.label == Plane.Label.TABLE
+ }
+ // [END androidxr_arcore_hitTest]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/Orbiter.kt b/xr/src/main/java/com/example/xr/compose/Orbiter.kt
new file mode 100644
index 000000000..f01c4cd1f
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/Orbiter.kt
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.CornerSize
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Surface
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.res.dimensionResource
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.xr.compose.spatial.ContentEdge
+import androidx.xr.compose.spatial.Orbiter
+import androidx.xr.compose.spatial.OrbiterOffsetType
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.SpatialRow
+import androidx.xr.compose.subspace.layout.SpatialRoundedCornerShape
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.movable
+import androidx.xr.compose.subspace.layout.resizable
+import androidx.xr.compose.subspace.layout.width
+import com.example.xr.R
+
+@Composable
+private fun OrbiterExampleSubspace() {
+ // [START androidxr_compose_OrbiterExampleSubspace]
+ Subspace {
+ SpatialPanel(
+ SubspaceModifier
+ .height(824.dp)
+ .width(1400.dp)
+ .movable()
+ .resizable()
+ ) {
+ SpatialPanelContent()
+ OrbiterExample()
+ }
+ }
+ // [END androidxr_compose_OrbiterExampleSubspace]
+}
+
+// [START androidxr_compose_OrbiterExample]
+@Composable
+fun OrbiterExample() {
+ Orbiter(
+ position = ContentEdge.Bottom,
+ offset = 96.dp,
+ alignment = Alignment.CenterHorizontally
+ ) {
+ Surface(Modifier.clip(CircleShape)) {
+ Row(
+ Modifier
+ .background(color = Color.Black)
+ .height(100.dp)
+ .width(600.dp),
+ horizontalArrangement = Arrangement.Center,
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ Text(
+ text = "Orbiter",
+ color = Color.White,
+ fontSize = 50.sp
+ )
+ }
+ }
+ }
+}
+// [END androidxr_compose_OrbiterExample]
+
+@Composable
+fun OrbiterAnchoringExample() {
+ // [START androidxr_compose_OrbiterAnchoringExample]
+ Subspace {
+ SpatialRow {
+ Orbiter(
+ position = ContentEdge.Top,
+ offset = 8.dp,
+ offsetType = OrbiterOffsetType.InnerEdge,
+ shape = SpatialRoundedCornerShape(size = CornerSize(50))
+ ) {
+ Text(
+ "Hello World!",
+ style = MaterialTheme.typography.titleMedium,
+ modifier = Modifier
+ .background(Color.White)
+ .padding(16.dp)
+ )
+ }
+ SpatialPanel(
+ SubspaceModifier
+ .height(824.dp)
+ .width(1400.dp)
+ ) {
+ Box(
+ modifier = Modifier
+ .background(Color.Red)
+ )
+ }
+ SpatialPanel(
+ SubspaceModifier
+ .height(824.dp)
+ .width(1400.dp)
+ ) {
+ Box(
+ modifier = Modifier
+ .background(Color.Blue)
+ )
+ }
+ }
+ }
+ // [END androidxr_compose_OrbiterAnchoringExample]
+}
+
+@Composable
+private fun NavigationRail() {}
+
+@Composable
+private fun Ui2DToOribiter() {
+ // [START androidxr_compose_orbiter_comparison]
+ // Previous approach
+ NavigationRail()
+
+ // New XR differentiated approach
+ Orbiter(
+ position = ContentEdge.Start,
+ offset = dimensionResource(R.dimen.start_orbiter_padding),
+ alignment = Alignment.Top
+ ) {
+ NavigationRail()
+ }
+ // [END androidxr_compose_orbiter_comparison]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt b/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt
new file mode 100644
index 000000000..b3af88c13
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.unit.dp
+import androidx.xr.compose.platform.LocalSpatialCapabilities
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.fillMaxHeight
+import androidx.xr.compose.subspace.layout.width
+
+@Composable
+private fun SupportingInfoPanel() {}
+
+@Composable
+private fun ButtonToPresentInfoModal() {}
+
+@Composable
+private fun SpatialCapabilitiesCheck() {
+ // [START androidxr_compose_checkSpatialCapabilities]
+ if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
+ SupportingInfoPanel()
+ } else {
+ ButtonToPresentInfoModal()
+ }
+
+ // Similar check for audio
+ val spatialAudioEnabled = LocalSpatialCapabilities.current.isSpatialAudioEnabled
+ // [END androidxr_compose_checkSpatialCapabilities]
+}
+
+@Composable
+private fun checkSpatialUiEnabled() {
+ // [START androidxr_compose_checkSpatialUiEnabled]
+ if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
+ Subspace {
+ SpatialPanel(
+ modifier = SubspaceModifier
+ .width(1488.dp)
+ .fillMaxHeight()
+ ) {
+ AppContent()
+ }
+ }
+ } else {
+ AppContent()
+ }
+ // [END androidxr_compose_checkSpatialUiEnabled]
+}
+
+@Composable
+private fun AppContent() {}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialDialog.kt b/xr/src/main/java/com/example/xr/compose/SpatialDialog.kt
new file mode 100644
index 000000000..f9455f26b
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialDialog.kt
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.width
+import androidx.compose.material3.Button
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.window.Dialog
+import androidx.xr.compose.spatial.SpatialDialog
+import androidx.xr.compose.spatial.SpatialDialogProperties
+import kotlinx.coroutines.delay
+
+// [START androidxr_compose_DelayedDialog]
+@Composable
+fun DelayedDialog() {
+ var showDialog by remember { mutableStateOf(false) }
+ LaunchedEffect(Unit) {
+ delay(3000)
+ showDialog = true
+ }
+ if (showDialog) {
+ SpatialDialog(
+ onDismissRequest = { showDialog = false },
+ SpatialDialogProperties(
+ dismissOnBackPress = true
+ )
+ ) {
+ Box(
+ Modifier
+ .height(150.dp)
+ .width(150.dp)
+ ) {
+ Button(onClick = { showDialog = false }) {
+ Text("OK")
+ }
+ }
+ }
+ }
+}
+// [END androidxr_compose_DelayedDialog]
+
+@Composable
+private fun MyDialogContent() {}
+@Composable
+private fun SpatialDialogComparison() {
+ val onDismissRequest: () -> Unit = {}
+ // [START androidxr_compose_spatialdialog_comparison]
+ // Previous approach
+ Dialog(
+ onDismissRequest = onDismissRequest
+ ) {
+ MyDialogContent()
+ }
+
+ // New XR differentiated approach
+ SpatialDialog(
+ onDismissRequest = onDismissRequest
+ ) {
+ MyDialogContent()
+ }
+ // [END androidxr_compose_spatialdialog_comparison]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialElevation.kt b/xr/src/main/java/com/example/xr/compose/SpatialElevation.kt
new file mode 100644
index 000000000..68fedfe3f
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialElevation.kt
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.runtime.Composable
+import androidx.xr.compose.spatial.SpatialElevation
+import androidx.xr.compose.spatial.SpatialElevationLevel
+
+@Composable
+private fun ComposableThatShouldElevateInXr() {}
+
+@Composable
+private fun SpatialElevationExample() {
+ // [START androidxr_compose_spatialelevation]
+ // Elevate an otherwise 2D Composable (signified here by ComposableThatShouldElevateInXr).
+ SpatialElevation(elevation = SpatialElevationLevel.Level4) {
+ ComposableThatShouldElevateInXr()
+ }
+ // [END androidxr_compose_spatialelevation]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialExternalSurface.kt b/xr/src/main/java/com/example/xr/compose/SpatialExternalSurface.kt
new file mode 100644
index 000000000..c42730758
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialExternalSurface.kt
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import android.content.ContentResolver
+import android.net.Uri
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.ExperimentalComposeApi
+import androidx.compose.runtime.remember
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.unit.dp
+import androidx.media3.common.C
+import androidx.media3.common.MediaItem
+import androidx.media3.exoplayer.ExoPlayer
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialExternalSurface
+import androidx.xr.compose.subspace.StereoMode
+import androidx.xr.compose.subspace.SurfaceProtection
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.width
+
+// [START androidxr_compose_SpatialExternalSurfaceStereo]
+@OptIn(ExperimentalComposeApi::class)
+@Composable
+fun SpatialExternalSurfaceContent() {
+ val context = LocalContext.current
+ Subspace {
+ SpatialExternalSurface(
+ modifier = SubspaceModifier
+ .width(1200.dp) // Default width is 400.dp if no width modifier is specified
+ .height(676.dp), // Default height is 400.dp if no height modifier is specified
+ // Use StereoMode.Mono, StereoMode.SideBySide, or StereoMode.TopBottom, depending
+ // upon which type of content you are rendering: monoscopic content, side-by-side stereo
+ // content, or top-bottom stereo content
+ stereoMode = StereoMode.SideBySide,
+ ) {
+ val exoPlayer = remember { ExoPlayer.Builder(context).build() }
+ val videoUri = Uri.Builder()
+ .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
+ // Represents a side-by-side stereo video, where each frame contains a pair of
+ // video frames arranged side-by-side. The frame on the left represents the left
+ // eye view, and the frame on the right represents the right eye view.
+ .path("sbs_video.mp4")
+ .build()
+ val mediaItem = MediaItem.fromUri(videoUri)
+
+ // onSurfaceCreated is invoked only one time, when the Surface is created
+ onSurfaceCreated { surface ->
+ exoPlayer.setVideoSurface(surface)
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+ }
+ // onSurfaceDestroyed is invoked when the SpatialExternalSurface composable and its
+ // associated Surface are destroyed
+ onSurfaceDestroyed { exoPlayer.release() }
+ }
+ }
+}
+// [END androidxr_compose_SpatialExternalSurfaceStereo]
+
+// [START androidxr_compose_SpatialExternalSurfaceDRM]
+@OptIn(ExperimentalComposeApi::class)
+@Composable
+fun DrmSpatialVideoPlayer() {
+ val context = LocalContext.current
+ Subspace {
+ SpatialExternalSurface(
+ modifier = SubspaceModifier
+ .width(1200.dp)
+ .height(676.dp),
+ stereoMode = StereoMode.SideBySide,
+ surfaceProtection = SurfaceProtection.Protected
+ ) {
+ val exoPlayer = remember { ExoPlayer.Builder(context).build() }
+
+ // Define the URI for your DRM-protected content and license server.
+ val videoUri = "https://your-content-provider.com/video.mpd"
+ val drmLicenseUrl = "https://your-license-server.com/license"
+
+ // Build a MediaItem with the necessary DRM configuration.
+ val mediaItem = MediaItem.Builder()
+ .setUri(videoUri)
+ .setDrmConfiguration(
+ MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
+ .setLicenseUri(drmLicenseUrl)
+ .build()
+ )
+ .build()
+
+ onSurfaceCreated { surface ->
+ // The created surface is secure and can be used by the player.
+ exoPlayer.setVideoSurface(surface)
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+ }
+
+ onSurfaceDestroyed { exoPlayer.release() }
+ }
+ }
+}
+// [END androidxr_compose_SpatialExternalSurfaceDRM]
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialLayout.kt b/xr/src/main/java/com/example/xr/compose/SpatialLayout.kt
new file mode 100644
index 000000000..b0099b00a
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialLayout.kt
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialColumn
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.SpatialRow
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.width
+
+@Composable
+private fun SpatialLayoutExampleSubspace() {
+ // [START androidxr_compose_SpatialLayoutExampleSubspace]
+ Subspace {
+ SpatialRow {
+ SpatialColumn {
+ SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
+ SpatialPanelContent("Top Left")
+ }
+ SpatialPanel(SubspaceModifier.height(200.dp).width(400.dp)) {
+ SpatialPanelContent("Middle Left")
+ }
+ SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
+ SpatialPanelContent("Bottom Left")
+ }
+ }
+ SpatialColumn {
+ SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
+ SpatialPanelContent("Top Right")
+ }
+ SpatialPanel(SubspaceModifier.height(200.dp).width(400.dp)) {
+ SpatialPanelContent("Middle Right")
+ }
+ SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
+ SpatialPanelContent("Bottom Right")
+ }
+ }
+ }
+ }
+ // [END androidxr_compose_SpatialLayoutExampleSubspace]
+}
+
+// [START androidxr_compose_SpatialLayoutExampleSpatialPanelContent]
+@Composable
+fun SpatialPanelContent(text: String) {
+ Column(
+ Modifier
+ .background(color = Color.Black)
+ .fillMaxSize(),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ verticalArrangement = Arrangement.Center
+ ) {
+ Text(
+ text = "Panel",
+ color = Color.White,
+ fontSize = 15.sp
+ )
+ Text(
+ text = text,
+ color = Color.White,
+ fontSize = 25.sp,
+ fontWeight = FontWeight.Bold
+ )
+ }
+}
+// [END androidxr_compose_SpatialLayoutExampleSpatialPanelContent]
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialPanel.kt b/xr/src/main/java/com/example/xr/compose/SpatialPanel.kt
new file mode 100644
index 000000000..c3a3a58ef
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialPanel.kt
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.width
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.xr.compose.platform.LocalSpatialCapabilities
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.movable
+import androidx.xr.compose.subspace.layout.resizable
+import androidx.xr.compose.subspace.layout.width
+
+@Composable
+private fun SpatialPanelExample() {
+ // [START androidxr_compose_SpatialPanel]
+ Subspace {
+ SpatialPanel(
+ SubspaceModifier
+ .height(824.dp)
+ .width(1400.dp)
+ .movable()
+ .resizable()
+ ) {
+ SpatialPanelContent()
+ }
+ }
+ // [END androidxr_compose_SpatialPanel]
+}
+
+// [START androidxr_compose_SpatialPanelContent]
+@Composable
+fun SpatialPanelContent() {
+ Box(
+ Modifier
+ .background(color = Color.Black)
+ .height(500.dp)
+ .width(500.dp),
+ contentAlignment = Alignment.Center
+ ) {
+ Text(
+ text = "Spatial Panel",
+ color = Color.White,
+ fontSize = 25.sp
+ )
+ }
+}
+// [END androidxr_compose_SpatialPanelContent]
+
+@Composable
+private fun AppContent() {}
+
+@Composable
+private fun ContentInSpatialPanel() {
+ // [START androidxr_compose_SpatialPanelAppContent]
+ if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
+ Subspace {
+ SpatialPanel(
+ SubspaceModifier
+ .resizable(true)
+ .movable(true)
+ ) {
+ AppContent()
+ }
+ }
+ } else {
+ AppContent()
+ }
+ // [END androidxr_compose_SpatialPanelAppContent]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialPopup.kt b/xr/src/main/java/com/example/xr/compose/SpatialPopup.kt
new file mode 100644
index 000000000..f42e1d1b2
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialPopup.kt
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.window.Popup
+import androidx.xr.compose.spatial.SpatialPopup
+
+@Composable
+private fun MyPopupContent() {}
+@Composable
+private fun SpatialPopupComparison() {
+ val onDismissRequest: () -> Unit = {}
+ // [START androidxr_compose_spatialpopup_comparison]
+ // Previous approach
+ Popup(onDismissRequest = onDismissRequest) {
+ MyPopupContent()
+ }
+
+ // New XR differentiated approach
+ SpatialPopup(onDismissRequest = onDismissRequest) {
+ MyPopupContent()
+ }
+ // [END androidxr_compose_spatialpopup_comparison]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/SpatialRow.kt b/xr/src/main/java/com/example/xr/compose/SpatialRow.kt
new file mode 100644
index 000000000..d138411ac
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/SpatialRow.kt
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.unit.dp
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.SpatialRow
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.width
+
+@Composable
+private fun SpatialRowExample() {
+ // [START androidxr_compose_SpatialRowExample]
+ SpatialRow {
+ SpatialPanel(
+ SubspaceModifier
+ .width(384.dp)
+ .height(592.dp)
+ ) {
+ StartSupportingPanelContent()
+ }
+ SpatialPanel(
+ SubspaceModifier
+ .height(824.dp)
+ .width(1400.dp)
+ ) {
+ App()
+ }
+ SpatialPanel(
+ SubspaceModifier
+ .width(288.dp)
+ .height(480.dp)
+ ) {
+ EndSupportingPanelContent()
+ }
+ }
+ // [END androidxr_compose_SpatialRowExample]
+}
+
+@Composable
+private fun App() { }
+
+@Composable
+private fun EndSupportingPanelContent() { }
+
+@Composable
+private fun StartSupportingPanelContent() { }
diff --git a/xr/src/main/java/com/example/xr/compose/Subspace.kt b/xr/src/main/java/com/example/xr/compose/Subspace.kt
new file mode 100644
index 000000000..2cbbe1021
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/Subspace.kt
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.enableEdgeToEdge
+import androidx.compose.foundation.layout.Row
+import androidx.compose.runtime.Composable
+import androidx.xr.compose.spatial.ApplicationSubspace
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialPanel
+
+private class SubspaceActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ enableEdgeToEdge()
+ // [START androidxr_compose_SubspaceSetContent]
+ setContent {
+ // This is a top-level subspace
+ ApplicationSubspace {
+ SpatialPanel {
+ MyComposable()
+ }
+ }
+ }
+ // [END androidxr_compose_SubspaceSetContent]
+ }
+}
+
+// [START androidxr_compose_SubspaceComponents]
+@Composable
+private fun MyComposable() {
+ Row {
+ PrimaryPane()
+ SecondaryPane()
+ }
+}
+
+@Composable
+private fun PrimaryPane() {
+ // This is a nested subspace, because PrimaryPane is in a SpatialPanel
+ // and that SpatialPanel is in a top-level Subspace
+ Subspace {
+ ObjectInAVolume(true)
+ }
+}
+// [END androidxr_compose_SubspaceComponents]
+
+@Composable
+private fun SecondaryPane() {}
diff --git a/xr/src/main/java/com/example/xr/compose/Views.kt b/xr/src/main/java/com/example/xr/compose/Views.kt
new file mode 100644
index 000000000..b567b2f09
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/Views.kt
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import android.content.Context
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.material3.Text
+import androidx.compose.ui.platform.ComposeView
+import androidx.compose.ui.platform.ViewCompositionStrategy
+import androidx.compose.ui.unit.dp
+import androidx.fragment.app.Fragment
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.depth
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.width
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.math.IntSize2d
+import androidx.xr.scenecore.PanelEntity
+import com.example.xr.R
+
+private class MyCustomView(context: Context) : View(context)
+
+private class ActivityWithSubspaceContent : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ // [START androidxr_compose_ActivityWithSubspaceContent]
+ setContent {
+ Subspace {
+ SpatialPanel(
+ modifier = SubspaceModifier.height(500.dp).width(500.dp).depth(25.dp)
+ ) { MyCustomView(this@ActivityWithSubspaceContent) }
+ }
+ }
+ // [END androidxr_compose_ActivityWithSubspaceContent]
+ }
+}
+
+private class FragmentWithComposeView() : Fragment() {
+ // [START androidxr_compose_FragmentWithComposeView]
+ override fun onCreateView(
+ inflater: LayoutInflater,
+ container: ViewGroup?,
+ savedInstanceState: Bundle?
+ ): View {
+ val view = inflater.inflate(R.layout.example_fragment, container, false)
+ view.findViewById(R.id.compose_view).apply {
+ // Dispose of the Composition when the view's LifecycleOwner
+ // is destroyed
+ setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
+ setContent {
+ // In Compose world
+ SpatialPanel(SubspaceModifier.height(500.dp).width(500.dp)) {
+ Text("Spatial Panel with Orbiter")
+ }
+ }
+ }
+ return view
+ }
+ // [END androidxr_compose_FragmentWithComposeView]
+}
+
+fun ComponentActivity.PanelEntityWithView(xrSession: Session) {
+ // [START androidxr_compose_PanelEntityWithView]
+ val panelContent = MyCustomView(this)
+ val panelEntity = PanelEntity.create(
+ session = xrSession,
+ view = panelContent,
+ pixelDimensions = IntSize2d(500, 500),
+ name = "panel entity"
+ )
+ // [END androidxr_compose_PanelEntityWithView]
+}
diff --git a/xr/src/main/java/com/example/xr/compose/Volume.kt b/xr/src/main/java/com/example/xr/compose/Volume.kt
new file mode 100644
index 000000000..cce37ca83
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/compose/Volume.kt
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.compose
+
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.xr.compose.platform.LocalSession
+import androidx.xr.compose.spatial.Subspace
+import androidx.xr.compose.subspace.ExperimentalSubspaceVolumeApi
+import androidx.xr.compose.subspace.SpatialPanel
+import androidx.xr.compose.subspace.Volume
+import androidx.xr.compose.subspace.layout.SubspaceModifier
+import androidx.xr.compose.subspace.layout.height
+import androidx.xr.compose.subspace.layout.movable
+import androidx.xr.compose.subspace.layout.offset
+import androidx.xr.compose.subspace.layout.resizable
+import androidx.xr.compose.subspace.layout.scale
+import androidx.xr.compose.subspace.layout.width
+import kotlinx.coroutines.launch
+
+@Composable
+private fun VolumeExample() {
+ // [START androidxr_compose_Volume]
+ Subspace {
+ SpatialPanel(
+ SubspaceModifier.height(1500.dp).width(1500.dp)
+ .resizable().movable()
+ ) {
+ ObjectInAVolume(true)
+ Box(
+ Modifier.fillMaxSize(),
+ contentAlignment = Alignment.Center
+ ) {
+ Text(
+ text = "Welcome",
+ fontSize = 50.sp,
+ )
+ }
+ }
+ }
+ // [END androidxr_compose_Volume]
+}
+
+// [START androidxr_compose_ObjectInAVolume]
+@OptIn(ExperimentalSubspaceVolumeApi::class)
+@Composable
+fun ObjectInAVolume(show3DObject: Boolean) {
+ // [START_EXCLUDE silent]
+ val volumeXOffset = 0.dp
+ val volumeYOffset = 0.dp
+ val volumeZOffset = 0.dp
+ // [END_EXCLUDE silent]
+ val session = checkNotNull(LocalSession.current)
+ val scope = rememberCoroutineScope()
+ if (show3DObject) {
+ Subspace {
+ Volume(
+ modifier = SubspaceModifier
+ .offset(volumeXOffset, volumeYOffset, volumeZOffset) // Relative position
+ .scale(1.2f) // Scale to 120% of the size
+
+ ) { parent ->
+ scope.launch {
+ // Load your 3D model here
+ }
+ }
+ }
+ }
+}
+// [END androidxr_compose_ObjectInAVolume]
diff --git a/xr/src/main/java/com/example/xr/misc/ModeTransition.kt b/xr/src/main/java/com/example/xr/misc/ModeTransition.kt
new file mode 100644
index 000000000..ea13d5868
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/misc/ModeTransition.kt
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.misc
+
+import androidx.compose.runtime.Composable
+import androidx.xr.compose.platform.LocalSpatialConfiguration
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.scene
+
+@Composable
+fun modeTransitionCompose() {
+ // [START androidxr_misc_modeTransitionCompose]
+ LocalSpatialConfiguration.current.requestHomeSpaceMode()
+ // or
+ LocalSpatialConfiguration.current.requestFullSpaceMode()
+ // [END androidxr_misc_modeTransitionCompose]
+}
+
+fun modeTransitionScenecore(xrSession: Session) {
+ // [START androidxr_misc_modeTransitionScenecore]
+ xrSession.scene.requestHomeSpaceMode()
+ // [END androidxr_misc_modeTransitionScenecore]
+}
diff --git a/xr/src/main/java/com/example/xr/runtime/Session.kt b/xr/src/main/java/com/example/xr/runtime/Session.kt
new file mode 100644
index 000000000..2a9f84ff1
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/runtime/Session.kt
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.runtime
+
+import androidx.activity.ComponentActivity
+import androidx.compose.runtime.Composable
+import androidx.xr.compose.platform.LocalSession
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.SessionCreateSuccess
+
+// [START androidxr_localsession]
+@Composable
+fun ComposableUsingSession() {
+ val session = LocalSession.current
+}
+// [END androidxr_localsession]
+
+fun ComponentActivity.createSession() {
+ // [START androidxr_session_create]
+ when (val result = Session.create(this)) {
+ is SessionCreateSuccess -> {
+ val xrSession = result.session
+ // ...
+ }
+ else ->
+ TODO(/* A different unhandled exception was thrown. */)
+ }
+ // [END androidxr_session_create]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/Entities.kt b/xr/src/main/java/com/example/xr/scenecore/Entities.kt
new file mode 100644
index 000000000..cf16f266e
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/Entities.kt
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import androidx.xr.runtime.math.Pose
+import androidx.xr.runtime.math.Quaternion
+import androidx.xr.runtime.math.Vector3
+import androidx.xr.scenecore.Entity
+
+private fun setPoseExample(entity: Entity) {
+ // [START androidxr_scenecore_entity_setPoseExample]
+ // Place the entity forward 2 meters
+ val newPosition = Vector3(0f, 0f, -2f)
+ // Rotate the entity by 180 degrees on the up axis (upside-down)
+ val newOrientation = Quaternion.fromEulerAngles(0f, 0f, 180f)
+ // Update the position and rotation on the entity
+ entity.setPose(Pose(newPosition, newOrientation))
+ // [END androidxr_scenecore_entity_setPoseExample]
+}
+
+private fun disableEntity(entity: Entity) {
+ // [START androidxr_scenecore_entity_setEnabled]
+ // Disable the entity.
+ entity.setEnabled(false)
+ // [END androidxr_scenecore_entity_setEnabled]
+}
+
+private fun entitySetScale(entity: Entity) {
+ // [START androidxr_scenecore_entity_entitySetScale]
+ // Double the size of the entity
+ entity.setScale(2f)
+ // [END androidxr_scenecore_entity_entitySetScale]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/Environments.kt b/xr/src/main/java/com/example/xr/scenecore/Environments.kt
new file mode 100644
index 000000000..5ed80d36f
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/Environments.kt
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import android.content.Context
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.ExrImage
+import androidx.xr.scenecore.GltfModel
+import androidx.xr.scenecore.SpatialEnvironment
+import androidx.xr.scenecore.scene
+import java.nio.file.Paths
+
+private class Environments(val session: Session) {
+ suspend fun loadEnvironmentGeometry(context: Context) {
+ // [START androidxr_scenecore_environment_loadEnvironmentGeometry]
+ val environmentGeometry = GltfModel.create(session, Paths.get("DayGeometry.glb"))
+ // [END androidxr_scenecore_environment_loadEnvironmentGeometry]
+ }
+
+ suspend fun loadEnvironmentSkybox() {
+ // [START androidxr_scenecore_environment_loadEnvironmentSkybox]
+ val lightingForSkybox = ExrImage.createFromZip(session, Paths.get("BlueSkyboxLighting.zip"))
+ // [END androidxr_scenecore_environment_loadEnvironmentSkybox]
+ }
+
+ fun setEnvironmentPreference(environmentGeometry: GltfModel, lightingForSkybox: ExrImage) {
+ // [START androidxr_scenecore_environment_setEnvironmentPreference]
+ val spatialEnvironmentPreference =
+ SpatialEnvironment.SpatialEnvironmentPreference(lightingForSkybox, environmentGeometry)
+ session.scene.spatialEnvironment.preferredSpatialEnvironment = spatialEnvironmentPreference
+ if (session.scene.spatialEnvironment.isPreferredSpatialEnvironmentActive) {
+ // The environment was successfully updated and is now visible, and any listeners
+ // specified using addOnSpatialEnvironmentChangedListener will be notified.
+ } else {
+ // The passthrough opacity preference was successfully set, but not
+ // immediately visible. The passthrough opacity change will be applied
+ // when the activity has the SPATIAL_CAPABILITY_APP_ENVIRONMENT capability.
+ // Then, any listeners specified using addOnSpatialEnvironmentChangedListener
+ // will be notified.
+ }
+ // [END androidxr_scenecore_environment_setEnvironmentPreference]
+ }
+
+ fun setPassthroughOpacityPreference() {
+ // [START androidxr_scenecore_environment_setPassthroughOpacityPreference]
+ session.scene.spatialEnvironment.preferredPassthroughOpacity = 1.0f
+ if (session.scene.spatialEnvironment.currentPassthroughOpacity == 1.0f) {
+ // The passthrough opacity request succeeded and should be visible now, and any listeners
+ // specified using addOnPassthroughOpacityChangedListener will be notified.
+ } else {
+ // The passthrough opacity preference was successfully set, but not
+ // immediately visible. The passthrough opacity change will be applied
+ // when the activity has the
+ // SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL capability.
+ // Then, any listeners specified using addOnPassthroughOpacityChangedListener
+ // will be notified.
+ }
+ // [END androidxr_scenecore_environment_setPassthroughOpacityPreference]
+ }
+
+ fun getCurrentPassthroughOpacity() {
+ // [START androidxr_scenecore_environment_getCurrentPassthroughOpacity]
+ val currentPassthroughOpacity = session.scene.spatialEnvironment.currentPassthroughOpacity
+ // [END androidxr_scenecore_environment_getCurrentPassthroughOpacity]
+ }
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/GltfEntity.kt b/xr/src/main/java/com/example/xr/scenecore/GltfEntity.kt
new file mode 100644
index 000000000..997436ddf
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/GltfEntity.kt
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import android.content.ActivityNotFoundException
+import android.content.Intent
+import android.net.Uri
+import androidx.activity.ComponentActivity
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.GltfModel
+import androidx.xr.scenecore.GltfModelEntity
+import androidx.xr.scenecore.SpatialCapabilities
+import androidx.xr.scenecore.scene
+import java.nio.file.Paths
+
+private suspend fun loadGltfFile(session: Session) {
+ // [START androidxr_scenecore_gltfmodel_create]
+ val gltfModel = GltfModel.create(session, Paths.get("models", "saturn_rings.glb"))
+ // [END androidxr_scenecore_gltfmodel_create]
+}
+
+private fun createModelEntity(session: Session, gltfModel: GltfModel) {
+ // [START androidxr_scenecore_gltfmodelentity_create]
+ if (session.scene.spatialCapabilities
+ .hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT)
+ ) {
+ val gltfEntity = GltfModelEntity.create(session, gltfModel)
+ }
+ // [END androidxr_scenecore_gltfmodelentity_create]
+}
+
+private fun animateEntity(gltfEntity: GltfModelEntity) {
+ // [START androidxr_scenecore_gltfmodelentity_animation]
+ gltfEntity.startAnimation(loop = true, animationName = "Walk")
+ // [END androidxr_scenecore_gltfmodelentity_animation]
+}
+
+private fun ComponentActivity.startSceneViewer() {
+ // [START androidxr_scenecore_sceneviewer]
+ val url =
+ "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Avocado/glTF/Avocado.gltf"
+ val sceneViewerIntent = Intent(Intent.ACTION_VIEW)
+ val intentUri =
+ Uri.parse("https://arvr.google.com/scene-viewer/1.2")
+ .buildUpon()
+ .appendQueryParameter("file", url)
+ .build()
+ sceneViewerIntent.setData(intentUri)
+ try {
+ startActivity(sceneViewerIntent)
+ } catch (e: ActivityNotFoundException) {
+ // There is no activity that could handle the intent.
+ }
+ // [END androidxr_scenecore_sceneviewer]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/InteractableComponent.kt b/xr/src/main/java/com/example/xr/scenecore/InteractableComponent.kt
new file mode 100644
index 000000000..a712d0dfe
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/InteractableComponent.kt
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.Entity
+import androidx.xr.scenecore.InputEvent
+import androidx.xr.scenecore.InteractableComponent
+import java.util.concurrent.Executors
+
+private fun interactableComponentExample(session: Session, entity: Entity) {
+ // [START androidxr_scenecore_interactableComponentExample]
+ val executor = Executors.newSingleThreadExecutor()
+ val interactableComponent = InteractableComponent.create(session, executor) {
+ // when the user disengages with the entity with their hands
+ if (it.source == InputEvent.Source.SOURCE_HANDS && it.action == InputEvent.Action.ACTION_UP) {
+ // increase size with right hand and decrease with left
+ if (it.pointerType == InputEvent.Pointer.POINTER_TYPE_RIGHT) {
+ entity.setScale(1.5f)
+ } else if (it.pointerType == InputEvent.Pointer.POINTER_TYPE_LEFT) {
+ entity.setScale(0.5f)
+ }
+ }
+ }
+ entity.addComponent(interactableComponent)
+ // [END androidxr_scenecore_interactableComponentExample]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/MovableComponent.kt b/xr/src/main/java/com/example/xr/scenecore/MovableComponent.kt
new file mode 100644
index 000000000..3c254e433
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/MovableComponent.kt
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.AnchorPlacement
+import androidx.xr.scenecore.Entity
+import androidx.xr.scenecore.MovableComponent
+import androidx.xr.scenecore.PlaneOrientation
+import androidx.xr.scenecore.PlaneSemanticType
+
+private fun createSystemMovable(session: Session, entity: Entity) {
+ // [START androidxr_scenecore_movableComponent_createSystemMovable]
+ val movableComponent = MovableComponent.createSystemMovable(session)
+ entity.addComponent(movableComponent)
+ // [END androidxr_scenecore_movableComponent_createSystemMovable]
+}
+
+private fun movableComponentAnchorExample(session: Session, entity: Entity) {
+ // [START androidxr_scenecore_movableComponent_anchorable]
+ val anchorPlacement = AnchorPlacement.createForPlanes(
+ anchorablePlaneOrientations = setOf(PlaneOrientation.VERTICAL),
+ anchorablePlaneSemanticTypes = setOf(PlaneSemanticType.FLOOR, PlaneSemanticType.TABLE)
+ )
+
+ val movableComponent = MovableComponent.createAnchorable(
+ session = session,
+ anchorPlacement = setOf(anchorPlacement)
+ )
+ entity.addComponent(movableComponent)
+ // [END androidxr_scenecore_movableComponent_anchorable]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/ResizableComponent.kt b/xr/src/main/java/com/example/xr/scenecore/ResizableComponent.kt
new file mode 100644
index 000000000..28b70027d
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/ResizableComponent.kt
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.math.FloatSize3d
+import androidx.xr.scenecore.ResizableComponent
+import androidx.xr.scenecore.ResizeEvent
+import androidx.xr.scenecore.SurfaceEntity
+import java.util.concurrent.Executor
+
+private fun resizableComponentExample(
+ session: Session,
+ surfaceEntity: SurfaceEntity,
+ executor: Executor
+) {
+ // [START androidxr_scenecore_resizableComponentExample]
+ val resizableComponent = ResizableComponent.create(session) { event ->
+ if (event.resizeState == ResizeEvent.ResizeState.RESIZE_STATE_END) {
+ // update the Entity to reflect the new size
+ surfaceEntity.canvasShape = SurfaceEntity.CanvasShape.Quad(event.newSize.width, event.newSize.height)
+ }
+ }
+ resizableComponent.minimumEntitySize = FloatSize3d(177f, 100f, 1f)
+ resizableComponent.fixedAspectRatio = 16f / 9f // Specify a 16:9 aspect ratio
+
+ surfaceEntity.addComponent(resizableComponent)
+ // [END androidxr_scenecore_resizableComponentExample]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/SpatialAudio.kt b/xr/src/main/java/com/example/xr/scenecore/SpatialAudio.kt
new file mode 100644
index 000000000..b68e67713
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/SpatialAudio.kt
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import android.content.Context
+import android.media.AudioAttributes
+import android.media.AudioAttributes.CONTENT_TYPE_SONIFICATION
+import android.media.AudioAttributes.USAGE_ASSISTANCE_SONIFICATION
+import android.media.MediaPlayer
+import android.media.SoundPool
+import androidx.annotation.OptIn
+import androidx.media3.common.C
+import androidx.media3.common.util.UnstableApi
+import androidx.media3.exoplayer.audio.AudioCapabilities
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.Entity
+import androidx.xr.scenecore.PointSourceParams
+import androidx.xr.scenecore.SoundFieldAttributes
+import androidx.xr.scenecore.SpatialCapabilities
+import androidx.xr.scenecore.SpatialMediaPlayer
+import androidx.xr.scenecore.SpatialSoundPool
+import androidx.xr.scenecore.SpatializerConstants
+import androidx.xr.scenecore.scene
+
+private fun playSpatialAudioAtEntity(session: Session, appContext: Context, entity: Entity) {
+ // [START androidxr_scenecore_playSpatialAudio]
+ // Check spatial capabilities before using spatial audio
+ if (session.scene.spatialCapabilities
+ .hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_SPATIAL_AUDIO)
+ ) { // The session has spatial audio capabilities
+ val maxVolume = 1F
+ val lowPriority = 0
+ val infiniteLoop = -1
+ val normalSpeed = 1F
+
+ val soundPool = SoundPool.Builder()
+ .setAudioAttributes(
+ AudioAttributes.Builder()
+ .setContentType(CONTENT_TYPE_SONIFICATION)
+ .setUsage(USAGE_ASSISTANCE_SONIFICATION)
+ .build()
+ )
+ .build()
+
+ val pointSource = PointSourceParams(entity)
+
+ val soundEffect = appContext.assets.openFd("sounds/tiger_16db.mp3")
+ val pointSoundId = soundPool.load(soundEffect, lowPriority)
+
+ soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
+ // wait for the sound file to be loaded into the soundPool
+ if (status == 0) {
+ SpatialSoundPool.play(
+ session = session,
+ soundPool = soundPool,
+ soundID = pointSoundId,
+ params = pointSource,
+ volume = maxVolume,
+ priority = lowPriority,
+ loop = infiniteLoop,
+ rate = normalSpeed
+ )
+ }
+ }
+ } else {
+ // The session does not have spatial audio capabilities
+ }
+ // [END androidxr_scenecore_playSpatialAudio]
+}
+
+private fun playSpatialAudioAtEntitySurround(session: Session, appContext: Context) {
+ // [START androidxr_scenecore_playSpatialAudioSurround]
+ // Check spatial capabilities before using spatial audio
+ if (session.scene.spatialCapabilities.hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_SPATIAL_AUDIO)) {
+ // The session has spatial audio capabilities
+
+ val pointSourceAttributes = PointSourceParams(session.scene.mainPanelEntity)
+
+ val mediaPlayer = MediaPlayer()
+
+ val fivePointOneAudio = appContext.assets.openFd("sounds/aac_51.ogg")
+ mediaPlayer.reset()
+ mediaPlayer.setDataSource(fivePointOneAudio)
+
+ val audioAttributes =
+ AudioAttributes.Builder()
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
+ .setUsage(AudioAttributes.USAGE_MEDIA)
+ .build()
+
+ SpatialMediaPlayer.setPointSourceParams(
+ session,
+ mediaPlayer,
+ pointSourceAttributes
+ )
+
+ mediaPlayer.setAudioAttributes(audioAttributes)
+ mediaPlayer.prepare()
+ mediaPlayer.start()
+ } else {
+ // The session does not have spatial audio capabilities
+ }
+ // [END androidxr_scenecore_playSpatialAudioSurround]
+}
+
+private fun playSpatialAudioAtEntityAmbionics(session: Session, appContext: Context) {
+ // [START androidxr_scenecore_playSpatialAudioAmbionics]
+ // Check spatial capabilities before using spatial audio
+ if (session.scene.spatialCapabilities.hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_SPATIAL_AUDIO)) {
+ // The session has spatial audio capabilities
+
+ val soundFieldAttributes =
+ SoundFieldAttributes(SpatializerConstants.AMBISONICS_ORDER_FIRST_ORDER)
+
+ val mediaPlayer = MediaPlayer()
+
+ val soundFieldAudio = appContext.assets.openFd("sounds/foa_basketball_16bit.wav")
+
+ mediaPlayer.reset()
+ mediaPlayer.setDataSource(soundFieldAudio)
+
+ val audioAttributes =
+ AudioAttributes.Builder()
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
+ .setUsage(AudioAttributes.USAGE_MEDIA)
+ .build()
+
+ SpatialMediaPlayer.setSoundFieldAttributes(
+ session,
+ mediaPlayer,
+ soundFieldAttributes
+ )
+
+ mediaPlayer.setAudioAttributes(audioAttributes)
+ mediaPlayer.prepare()
+ mediaPlayer.start()
+ } else {
+ // The session does not have spatial audio capabilities
+ }
+ // [END androidxr_scenecore_playSpatialAudioAmbionics]
+}
+
+@OptIn(UnstableApi::class)
+private fun detectSupport(context: Context) {
+ // [START androidxr_scenecore_dolby_detect_support]
+ val audioCapabilities = AudioCapabilities.getCapabilities(context, androidx.media3.common.AudioAttributes.DEFAULT, null)
+ if (audioCapabilities.supportsEncoding(C.ENCODING_AC3)) {
+ // Device supports playback of the Dolby Digital media format.
+ }
+ if (audioCapabilities.supportsEncoding(C.ENCODING_E_AC3)) {
+ // Device supports playback of the Dolby Digital Plus media format.
+ }
+ if (audioCapabilities.supportsEncoding(C.ENCODING_E_AC3_JOC)) {
+ // Device supports playback of the Dolby Digital Plus with Dolby Atmos media format.
+ }
+ // [END androidxr_scenecore_dolby_detect_support]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt b/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt
new file mode 100644
index 000000000..7fb784080
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import androidx.xr.runtime.Session
+import androidx.xr.scenecore.SpatialCapabilities
+import androidx.xr.scenecore.scene
+
+fun checkMultipleCapabilities(xrSession: Session) {
+ // [START androidxr_compose_checkMultipleCapabilities]
+ // Example 1: check if enabling passthrough mode is allowed
+ if (xrSession.scene.spatialCapabilities.hasCapability(
+ SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL
+ )
+ ) {
+ xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f
+ }
+ // Example 2: multiple capability flags can be checked simultaneously:
+ if (xrSession.scene.spatialCapabilities.hasCapability(
+ SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
+ SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT
+ )
+ ) {
+ // ...
+ }
+ // [END androidxr_compose_checkMultipleCapabilities]
+}
diff --git a/xr/src/main/java/com/example/xr/scenecore/SpatialVideo.kt b/xr/src/main/java/com/example/xr/scenecore/SpatialVideo.kt
new file mode 100644
index 000000000..ec733bb61
--- /dev/null
+++ b/xr/src/main/java/com/example/xr/scenecore/SpatialVideo.kt
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.xr.scenecore
+
+import android.content.ContentResolver
+import android.net.Uri
+import androidx.activity.ComponentActivity
+import androidx.lifecycle.lifecycleScope
+import androidx.media3.common.C
+import androidx.media3.common.MediaItem
+import androidx.media3.exoplayer.ExoPlayer
+import androidx.xr.runtime.Session
+import androidx.xr.runtime.math.Pose
+import androidx.xr.runtime.math.Vector3
+import androidx.xr.scenecore.SurfaceEntity
+import androidx.xr.scenecore.Texture
+import androidx.xr.scenecore.TextureSampler
+import androidx.xr.scenecore.scene
+import java.nio.file.Paths
+import kotlinx.coroutines.launch
+
+private fun ComponentActivity.surfaceEntityCreate(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityCreate]
+ val stereoSurfaceEntity = SurfaceEntity.create(
+ xrSession,
+ SurfaceEntity.StereoMode.SIDE_BY_SIDE,
+ Pose(Vector3(0.0f, 0.0f, -1.5f)),
+ SurfaceEntity.CanvasShape.Quad(1.0f, 1.0f)
+ )
+ val videoUri = Uri.Builder()
+ .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
+ .path("sbs_video.mp4")
+ .build()
+ val mediaItem = MediaItem.fromUri(videoUri)
+
+ val exoPlayer = ExoPlayer.Builder(this).build()
+ exoPlayer.setVideoSurface(stereoSurfaceEntity.getSurface())
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+ // [END androidxr_scenecore_surfaceEntityCreate]
+}
+
+private fun ComponentActivity.surfaceEntityCreateSbs(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityCreateSbs]
+ // Set up the surface for playing a 180° video on a hemisphere.
+ val hemisphereStereoSurfaceEntity =
+ SurfaceEntity.create(
+ xrSession,
+ SurfaceEntity.StereoMode.SIDE_BY_SIDE,
+ xrSession.scene.spatialUser.head?.transformPoseTo(
+ Pose.Identity,
+ xrSession.scene.activitySpace
+ )!!,
+ SurfaceEntity.CanvasShape.Vr180Hemisphere(1.0f),
+ )
+ // ... and use the surface for playing the media.
+ // [END androidxr_scenecore_surfaceEntityCreateSbs]
+}
+
+private fun ComponentActivity.surfaceEntityCreateTb(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityCreateTb]
+ // Set up the surface for playing a 360° video on a sphere.
+ val sphereStereoSurfaceEntity =
+ SurfaceEntity.create(
+ xrSession,
+ SurfaceEntity.StereoMode.TOP_BOTTOM,
+ xrSession.scene.spatialUser.head?.transformPoseTo(
+ Pose.Identity,
+ xrSession.scene.activitySpace
+ )!!,
+ SurfaceEntity.CanvasShape.Vr360Sphere(1.0f),
+ )
+ // ... and use the surface for playing the media.
+ // [END androidxr_scenecore_surfaceEntityCreateTb]
+}
+
+private fun ComponentActivity.surfaceEntityCreateMVHEVC(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityCreateMVHEVC]
+ // Create the SurfaceEntity with the StereoMode corresponding to the MV-HEVC content
+ val stereoSurfaceEntity = SurfaceEntity.create(
+ xrSession,
+ SurfaceEntity.StereoMode.MULTIVIEW_LEFT_PRIMARY,
+ Pose(Vector3(0.0f, 0.0f, -1.5f)),
+ SurfaceEntity.CanvasShape.Quad(1.0f, 1.0f)
+ )
+ val videoUri = Uri.Builder()
+ .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
+ .path("mvhevc_video.mp4")
+ .build()
+ val mediaItem = MediaItem.fromUri(videoUri)
+
+ val exoPlayer = ExoPlayer.Builder(this).build()
+ exoPlayer.setVideoSurface(stereoSurfaceEntity.getSurface())
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+ // [END androidxr_scenecore_surfaceEntityCreateMVHEVC]
+}
+
+private fun ComponentActivity.surfaceEntityCreateDRM(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityCreateDRM]
+ // Create a SurfaceEntity with DRM content
+
+ // Define the URI for your DRM-protected content and license server.
+ val videoUri = "https://your-content-provider.com/video.mpd"
+ val drmLicenseUrl = "https://your-license-server.com/license"
+
+ // Create the SurfaceEntity with the PROTECTED content security level.
+ val protectedSurfaceEntity = SurfaceEntity.create(
+ session = xrSession,
+ stereoMode = SurfaceEntity.StereoMode.SIDE_BY_SIDE,
+ pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
+ canvasShape = SurfaceEntity.CanvasShape.Quad(1.0f, 1.0f),
+ contentSecurityLevel = SurfaceEntity.ContentSecurityLevel.PROTECTED
+ )
+
+ // Build a MediaItem with the necessary DRM configuration.
+ val mediaItem = MediaItem.Builder()
+ .setUri(videoUri)
+ .setDrmConfiguration(
+ MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
+ .setLicenseUri(drmLicenseUrl)
+ .build()
+ )
+ .build()
+
+ // Initialize ExoPlayer and set the protected surface.
+ val exoPlayer = ExoPlayer.Builder(this).build()
+ exoPlayer.setVideoSurface(protectedSurfaceEntity.getSurface())
+
+ // Set the media item and start playback.
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+
+ // [END androidxr_scenecore_surfaceEntityCreateDRM]
+}
+
+private fun ComponentActivity.surfaceEntityHDR(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityHDR]
+ // Define the color properties for your HDR video. These values should be specific
+ // to your content.
+ val hdrMetadata = SurfaceEntity.ContentColorMetadata(
+ colorSpace = SurfaceEntity.ContentColorMetadata.ColorSpace.BT2020,
+ colorTransfer = SurfaceEntity.ContentColorMetadata.ColorTransfer.ST2084, // PQ
+ colorRange = SurfaceEntity.ContentColorMetadata.ColorRange.LIMITED,
+ maxCLL = 1000 // Example: 1000 nits
+ )
+
+ // Create a SurfaceEntity, passing the HDR metadata at creation time.
+ val hdrSurfaceEntity = SurfaceEntity.create(
+ session = xrSession,
+ stereoMode = SurfaceEntity.StereoMode.MONO,
+ pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
+ canvasShape = SurfaceEntity.CanvasShape.Quad(1.0f, 1.0f),
+ contentColorMetadata = hdrMetadata
+ )
+
+ // Initialize ExoPlayer and set the surface.
+ val exoPlayer = ExoPlayer.Builder(this).build()
+ exoPlayer.setVideoSurface(hdrSurfaceEntity.getSurface())
+
+ // Define the URI for your HDR content.
+ val videoUri = "https://your-content-provider.com/hdr_video.mp4"
+ val mediaItem = MediaItem.fromUri(videoUri)
+
+ // Set the media item and start playback.
+ exoPlayer.setMediaItem(mediaItem)
+ exoPlayer.prepare()
+ exoPlayer.play()
+ // [END androidxr_scenecore_surfaceEntityHDR]
+}
+
+private fun surfaceEntityEdgeFeathering(xrSession: Session) {
+ // [START androidxr_scenecore_surfaceEntityEdgeFeathering]
+ // Create a SurfaceEntity.
+ val surfaceEntity = SurfaceEntity.create(
+ session = xrSession,
+ pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
+ )
+
+ // Feather the edges of the surface.
+ surfaceEntity.edgeFeather =
+ SurfaceEntity.EdgeFeatheringParams.SmoothFeather(0.1f, 0.1f)
+ // [END androidxr_scenecore_surfaceEntityEdgeFeathering]
+}
+
+private fun surfaceEntityAlphaMasking(xrSession: Session, activity: ComponentActivity) {
+ // [START androidxr_scenecore_surfaceEntityAlphaMasking]
+ // Create a SurfaceEntity.
+ val surfaceEntity = SurfaceEntity.create(
+ session = xrSession,
+ pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
+ )
+
+ // Load the texture in a coroutine scope.
+ activity.lifecycleScope.launch {
+ val alphaMaskTexture =
+ Texture.create(
+ xrSession,
+ Paths.get("textures", "alpha_mask.png"),
+ TextureSampler.create()
+ )
+
+ // Apply the alpha mask.
+ surfaceEntity.primaryAlphaMaskTexture = alphaMaskTexture
+
+ // To remove the mask, set the property to null.
+ surfaceEntity.primaryAlphaMaskTexture = null
+ }
+ // [END androidxr_scenecore_surfaceEntityAlphaMasking]
+}
diff --git a/xr/src/main/res/layout/example_fragment.xml b/xr/src/main/res/layout/example_fragment.xml
new file mode 100644
index 000000000..13aa8cbbe
--- /dev/null
+++ b/xr/src/main/res/layout/example_fragment.xml
@@ -0,0 +1,9 @@
+
+
+
+
\ No newline at end of file
diff --git a/xr/src/main/res/values/dimens.xml b/xr/src/main/res/values/dimens.xml
new file mode 100644
index 000000000..ed1e9310d
--- /dev/null
+++ b/xr/src/main/res/values/dimens.xml
@@ -0,0 +1,5 @@
+
+
+ 8dp
+ 8dp
+
\ No newline at end of file