Skip to content

Commit da39e1f

Browse files
committed
Migrate snippets from "Bring your Android app into 3D with XR"
1 parent b1b4ef6 commit da39e1f

File tree

8 files changed

+249
-0
lines changed

8 files changed

+249
-0
lines changed

xr/src/main/java/com/example/xr/compose/Orbiter.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.compose.ui.Alignment
3333
import androidx.compose.ui.Modifier
3434
import androidx.compose.ui.draw.clip
3535
import androidx.compose.ui.graphics.Color
36+
import androidx.compose.ui.res.dimensionResource
3637
import androidx.compose.ui.unit.dp
3738
import androidx.compose.ui.unit.sp
3839
import androidx.xr.compose.spatial.EdgeOffset
@@ -47,6 +48,7 @@ import androidx.xr.compose.subspace.layout.height
4748
import androidx.xr.compose.subspace.layout.movable
4849
import androidx.xr.compose.subspace.layout.resizable
4950
import androidx.xr.compose.subspace.layout.width
51+
import com.example.xr.R
5052

5153
@Composable
5254
private fun OrbiterExampleSubspace() {
@@ -136,3 +138,23 @@ fun OrbiterAnchoringExample() {
136138
}
137139
// [END androidxr_compose_OrbiterAnchoringExample]
138140
}
141+
142+
@Composable
143+
private fun NavigationRail() {}
144+
145+
@Composable
146+
private fun Ui2DToOribiter() {
147+
// [START androidxr_compose_orbiter_comparison]
148+
// Previous approach
149+
NavigationRail()
150+
151+
// New XR differentiated approach
152+
Orbiter(
153+
position = OrbiterEdge.Start,
154+
offset = dimensionResource(R.dimen.start_orbiter_padding),
155+
alignment = Alignment.Top
156+
) {
157+
NavigationRail()
158+
}
159+
// [END androidxr_compose_orbiter_comparison]
160+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.xr.compose.platform.LocalSpatialCapabilities
21+
22+
@Composable
23+
private fun SupportingInfoPanel() {}
24+
25+
@Composable
26+
private fun ButtonToPresentInfoModal() {}
27+
28+
@Composable
29+
private fun SpatialCapabilitiesCheck() {
30+
// [START androidxr_compose_checkSpatialCapabilities]
31+
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
32+
SupportingInfoPanel()
33+
} else {
34+
ButtonToPresentInfoModal()
35+
}
36+
37+
// Similar check for audio
38+
val spatialAudioEnabled = LocalSpatialCapabilities.current.isSpatialAudioEnabled
39+
// [END androidxr_compose_checkSpatialCapabilities]
40+
}

xr/src/main/java/com/example/xr/compose/SpatialDialog.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.compose.runtime.remember
2929
import androidx.compose.runtime.setValue
3030
import androidx.compose.ui.Modifier
3131
import androidx.compose.ui.unit.dp
32+
import androidx.compose.ui.window.Dialog
3233
import androidx.xr.compose.spatial.SpatialDialog
3334
import androidx.xr.compose.spatial.SpatialDialogProperties
3435
import kotlinx.coroutines.delay
@@ -61,3 +62,25 @@ fun DelayedDialog() {
6162
}
6263
}
6364
// [END androidxr_compose_DelayedDialog]
65+
66+
@Composable
67+
private fun MyDialogContent() {}
68+
@Composable
69+
private fun SpatialDialogComparison() {
70+
val onDismissRequest: () -> Unit = {}
71+
// [START androidxr_compose_spatialdialog_comparison]
72+
// Previous approach
73+
Dialog(
74+
onDismissRequest = onDismissRequest
75+
) {
76+
MyDialogContent()
77+
}
78+
79+
// New XR differentiated approach
80+
SpatialDialog(
81+
onDismissRequest = onDismissRequest
82+
) {
83+
MyDialogContent()
84+
}
85+
// [END androidxr_compose_spatialdialog_comparison]
86+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.xr.compose.spatial.SpatialElevation
21+
import androidx.xr.compose.spatial.SpatialElevationLevel
22+
23+
@Composable
24+
private fun ComposableThatShouldElevateInXr() {}
25+
26+
@Composable
27+
private fun SpatialElevationExample() {
28+
// [START androidxr_compose_spatialelevation]
29+
// Elevate an otherwise 2D Composable (signified here by ComposableThatShouldElevateInXr).
30+
SpatialElevation(spatialElevationLevel = SpatialElevationLevel.Level4) {
31+
ComposableThatShouldElevateInXr()
32+
}
33+
// [END androidxr_compose_spatialelevation]
34+
}

xr/src/main/java/com/example/xr/compose/SpatialPanel.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.Modifier
2727
import androidx.compose.ui.graphics.Color
2828
import androidx.compose.ui.unit.dp
2929
import androidx.compose.ui.unit.sp
30+
import androidx.xr.compose.platform.LocalSpatialCapabilities
3031
import androidx.xr.compose.spatial.Subspace
3132
import androidx.xr.compose.subspace.SpatialPanel
3233
import androidx.xr.compose.subspace.layout.SubspaceModifier
@@ -70,3 +71,25 @@ fun SpatialPanelContent() {
7071
}
7172
}
7273
// [END androidxr_compose_SpatialPanelContent]
74+
75+
@Composable
76+
private fun AppContent() {}
77+
78+
@Composable
79+
private fun ContentInSpatialPanel() {
80+
// [START androidxr_compose_SpatialPanelAppContent]
81+
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
82+
Subspace {
83+
SpatialPanel(
84+
SubspaceModifier
85+
.resizable(true)
86+
.movable(true)
87+
) {
88+
AppContent()
89+
}
90+
}
91+
} else {
92+
AppContent()
93+
}
94+
// [END androidxr_compose_SpatialPanelAppContent]
95+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.ui.window.Popup
21+
import androidx.xr.compose.spatial.SpatialPopup
22+
23+
@Composable
24+
private fun MyPopupContent() {}
25+
@Composable
26+
private fun SpatialPopupComparison() {
27+
val onDismissRequest: () -> Unit = {}
28+
// [START androidxr_compose_spatialpopup_comparison]
29+
// Previous approach
30+
Popup(onDismissRequest = onDismissRequest) {
31+
MyPopupContent()
32+
}
33+
34+
// New XR differentiated approach
35+
SpatialPopup(onDismissRequest = onDismissRequest) {
36+
MyPopupContent()
37+
}
38+
// [END androidxr_compose_spatialpopup_comparison]
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.ui.unit.dp
21+
import androidx.xr.compose.subspace.SpatialPanel
22+
import androidx.xr.compose.subspace.SpatialRow
23+
import androidx.xr.compose.subspace.layout.SubspaceModifier
24+
import androidx.xr.compose.subspace.layout.height
25+
import androidx.xr.compose.subspace.layout.width
26+
27+
@Composable
28+
private fun SpatialRowExample() {
29+
// [START androidxr_compose_SpatialRowExample]
30+
SpatialRow(curveRadius = 825.dp) {
31+
SpatialPanel(
32+
SubspaceModifier
33+
.width(384.dp)
34+
.height(592.dp)
35+
) {
36+
StartSupportingPanelContent()
37+
}
38+
SpatialPanel(
39+
SubspaceModifier
40+
.height(824.dp)
41+
.width(1400.dp)
42+
) {
43+
App()
44+
}
45+
SpatialPanel(
46+
SubspaceModifier
47+
.width(288.dp)
48+
.height(480.dp)
49+
) {
50+
EndSupportingPanelContent()
51+
}
52+
}
53+
// [END androidxr_compose_SpatialRowExample]
54+
}
55+
56+
@Composable
57+
private fun App() { }
58+
59+
@Composable
60+
private fun EndSupportingPanelContent() { }
61+
62+
@Composable
63+
private fun StartSupportingPanelContent() { }

xr/src/main/res/values/dimens.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="start_orbiter_padding">8dp</dimen>
4+
<dimen name="top_orbiter_padding">8dp</dimen>
5+
</resources>

0 commit comments

Comments
 (0)