Skip to content

Commit 534f985

Browse files
committed
Migrate existing snippets to alpha04
1 parent b1b4ef6 commit 534f985

File tree

8 files changed

+175
-88
lines changed

8 files changed

+175
-88
lines changed

gradle/libs.versions.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ playServicesWearable = "19.0.0"
5656
protolayout = "1.2.1"
5757
recyclerview = "1.4.0"
5858
# @keep
59-
androidx-xr = "1.0.0-alpha02"
59+
androidx-xr-arcore = "1.0.0-SNAPSHOT"
60+
androidx-xr-scenecore = "1.0.0-SNAPSHOT"
61+
androidx-xr-compose = "1.0.0-SNAPSHOT"
6062
targetSdk = "34"
6163
tiles = "1.4.1"
6264
version-catalog-update = "0.8.5"
@@ -139,9 +141,9 @@ androidx-window = { module = "androidx.window:window", version.ref = "androidx-w
139141
androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window-core" }
140142
androidx-window-java = {module = "androidx.window:window-java", version.ref = "androidx-window-java" }
141143
androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.10.0"
142-
androidx-xr-arcore = { module = "androidx.xr.arcore:arcore", version.ref = "androidx-xr" }
143-
androidx-xr-compose = { module = "androidx.xr.compose:compose", version.ref = "androidx-xr" }
144-
androidx-xr-scenecore = { module = "androidx.xr.scenecore:scenecore", version.ref = "androidx-xr" }
144+
androidx-xr-arcore = { module = "androidx.xr.arcore:arcore", version.ref = "androidx-xr-arcore" }
145+
androidx-xr-compose = { module = "androidx.xr.compose:compose", version.ref = "androidx-xr-compose" }
146+
androidx-xr-scenecore = { module = "androidx.xr.scenecore:scenecore", version.ref = "androidx-xr-scenecore" }
145147
android-identity-googleid = {module = "com.google.android.libraries.identity.googleid:googleid", version.ref = "android-googleid"}
146148
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
147149
coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
val snapshotVersion : String? = System.getenv("COMPOSE_SNAPSHOT_ID")
1+
val snapshotVersion : String? = "13391174"
22

33
pluginManagement {
44
repositories {

xr/build.gradle.kts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ android {
2828
}
2929

3030
dependencies {
31-
implementation(libs.androidx.xr.arcore)
32-
implementation(libs.androidx.xr.scenecore)
33-
implementation(libs.androidx.xr.compose)
31+
implementation(libs.androidx.xr.arcore) {
32+
exclude(module = "impress")
33+
}
34+
implementation(libs.androidx.xr.scenecore) {
35+
exclude(module = "impress")
36+
}
37+
implementation(libs.androidx.xr.compose) {
38+
exclude(module = "impress")
39+
}
40+
41+
implementation("com.google.ar:impress:0.0.2")
3442

3543
implementation(libs.androidx.activity.ktx)
3644
implementation(libs.guava)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:SuppressLint("RestrictedApi") // Required for SNAPSHOT build. Remove after the release.
18+
19+
package com.example.xr.arcore
20+
21+
import android.annotation.SuppressLint
22+
import androidx.xr.runtime.PlaneTrackingMode
23+
import androidx.xr.runtime.Session
24+
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
25+
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
26+
import androidx.xr.runtime.SessionConfigureSuccess
27+
28+
fun configureAnchoring(session: Session) {
29+
// [START androidxr_arcore_anchoring_configure]
30+
val newConfig = session.config.copy(
31+
planeTracking = PlaneTrackingMode.HorizontalAndVertical,
32+
)
33+
when (val result = session.configure(newConfig)) {
34+
is SessionConfigureConfigurationNotSupported ->
35+
TODO(/* Some combinations of configurations are not valid. Handle this failure case.*/)
36+
is SessionConfigurePermissionsNotGranted ->
37+
TODO(/* The required permissions in result.permissions have not been granted. */)
38+
is SessionConfigureSuccess -> TODO(/* Success! */)
39+
}
40+
// [END androidxr_arcore_anchoring_configure]
41+
}

xr/src/main/java/com/example/xr/arcore/Hands.kt

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,42 @@
1414
* limitations under the License.
1515
*/
1616

17+
@file:SuppressLint("RestrictedApi") // Required for SNAPSHOT build. Remove after the release.
1718
package com.example.xr.arcore
1819

1920
import android.annotation.SuppressLint
20-
import android.os.Bundle
2121
import androidx.activity.ComponentActivity
2222
import androidx.lifecycle.lifecycleScope
2323
import androidx.xr.arcore.Hand
24-
import androidx.xr.arcore.HandJointType
25-
import androidx.xr.compose.platform.setSubspaceContent
24+
import androidx.xr.runtime.HandJointType
25+
import androidx.xr.runtime.HandTrackingMode
2626
import androidx.xr.runtime.Session
27+
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
28+
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
29+
import androidx.xr.runtime.SessionConfigureSuccess
30+
import androidx.xr.runtime.internal.GltfEntity
2731
import androidx.xr.runtime.math.Pose
2832
import androidx.xr.runtime.math.Quaternion
2933
import androidx.xr.runtime.math.Vector3
30-
import androidx.xr.scenecore.Entity
31-
import androidx.xr.scenecore.GltfModel
32-
import androidx.xr.scenecore.GltfModelEntity
33-
import kotlinx.coroutines.guava.await
34+
import androidx.xr.scenecore.scene
3435
import kotlinx.coroutines.launch
3536

36-
class SampleHandsActivity : ComponentActivity() {
37-
lateinit var session: Session
38-
lateinit var scenecoreSession: androidx.xr.scenecore.Session
39-
lateinit var sessionHelper: SessionLifecycleHelper
40-
41-
var palmEntity: Entity? = null
42-
var indexFingerEntity: Entity? = null
43-
44-
override fun onCreate(savedInstanceState: Bundle?) {
45-
super.onCreate(savedInstanceState)
46-
setSubspaceContent { }
47-
48-
scenecoreSession = androidx.xr.scenecore.Session.create(this@SampleHandsActivity)
49-
lifecycleScope.launch {
50-
val model = GltfModel.create(scenecoreSession, "models/saturn_rings.glb").await()
51-
palmEntity = GltfModelEntity.create(scenecoreSession, model).apply {
52-
setScale(0.3f)
53-
setHidden(true)
54-
}
55-
indexFingerEntity = GltfModelEntity.create(scenecoreSession, model).apply {
56-
setScale(0.2f)
57-
setHidden(true)
58-
}
59-
}
60-
61-
sessionHelper = SessionLifecycleHelper(
62-
onCreateCallback = { session = it },
63-
onResumeCallback = {
64-
collectHands(session)
65-
}
66-
)
67-
lifecycle.addObserver(sessionHelper)
37+
fun ComponentActivity.configureSession(session: Session) {
38+
// [START androidxr_arcore_hand_configure]
39+
val newConfig = session.config.copy(
40+
handTracking = HandTrackingMode.Enabled
41+
)
42+
when (val result = session.configure(newConfig)) {
43+
is SessionConfigureConfigurationNotSupported ->
44+
TODO(/* Some combinations of configurations are not valid. Handle this failure case.*/)
45+
is SessionConfigurePermissionsNotGranted ->
46+
TODO(/* The required permissions in result.permissions have not been granted. */)
47+
is SessionConfigureSuccess -> TODO(/* Success! */)
6848
}
49+
// [END androidxr_arcore_hand_configure]
6950
}
7051

71-
fun SampleHandsActivity.collectHands(session: Session) {
52+
fun ComponentActivity.collectHands(session: Session) {
7253
lifecycleScope.launch {
7354
// [START androidxr_arcore_hand_collect]
7455
Hand.left(session)?.state?.collect { handState -> // or Hand.right(session)
@@ -85,9 +66,9 @@ fun SampleHandsActivity.collectHands(session: Session) {
8566
}
8667
}
8768

88-
@SuppressLint("RestrictedApi") // HandJointType is mistakenly @Restrict: b/397415504
89-
fun SampleHandsActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
90-
val palmEntity = palmEntity ?: return
69+
fun ComponentActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
70+
val session: Session = null!!
71+
val palmEntity: GltfEntity = null!!
9172
// [START androidxr_arcore_hand_entityAtHandPalm]
9273
val palmPose = leftHandState.handJoints[HandJointType.PALM] ?: return
9374

@@ -96,18 +77,18 @@ fun SampleHandsActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
9677
palmEntity.setHidden(angle > Math.toRadians(40.0))
9778

9879
val transformedPose =
99-
scenecoreSession.perceptionSpace.transformPoseTo(
80+
session.scene.perceptionSpace.transformPoseTo(
10081
palmPose,
101-
scenecoreSession.activitySpace,
82+
session.scene.activitySpace,
10283
)
10384
val newPosition = transformedPose.translation + transformedPose.down * 0.05f
10485
palmEntity.setPose(Pose(newPosition, transformedPose.rotation))
10586
// [END androidxr_arcore_hand_entityAtHandPalm]
10687
}
10788

108-
@SuppressLint("RestrictedApi") // HandJointType is mistakenly @Restrict: b/397415504
109-
fun SampleHandsActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
110-
val indexFingerEntity = indexFingerEntity ?: return
89+
fun ComponentActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
90+
val session: Session = null!!
91+
val indexFingerEntity: GltfEntity = null!!
11192

11293
// [START androidxr_arcore_hand_entityAtIndexFingerTip]
11394
val tipPose = rightHandState.handJoints[HandJointType.INDEX_TIP] ?: return
@@ -117,9 +98,9 @@ fun SampleHandsActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
11798
indexFingerEntity.setHidden(angle > Math.toRadians(40.0))
11899

119100
val transformedPose =
120-
scenecoreSession.perceptionSpace.transformPoseTo(
101+
session.scene.perceptionSpace.transformPoseTo(
121102
tipPose,
122-
scenecoreSession.activitySpace,
103+
session.scene.activitySpace,
123104
)
124105
val position = transformedPose.translation + transformedPose.forward * 0.03f
125106
val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:SuppressLint("RestrictedApi") // Required for SNAPSHOT build. Remove after the release.
18+
19+
package com.example.xr.arcore
20+
21+
import android.annotation.SuppressLint
22+
import androidx.xr.runtime.PlaneTrackingMode
23+
import androidx.xr.runtime.Session
24+
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
25+
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
26+
import androidx.xr.runtime.SessionConfigureSuccess
27+
28+
fun configurePlaneTracking(session: Session) {
29+
// [START androidxr_arcore_planetracking_configure]
30+
val newConfig = session.config.copy(
31+
planeTracking = PlaneTrackingMode.HorizontalAndVertical,
32+
)
33+
when (val result = session.configure(newConfig)) {
34+
is SessionConfigureConfigurationNotSupported ->
35+
TODO(/* Some combinations of configurations are not valid. Handle this failure case.*/)
36+
is SessionConfigurePermissionsNotGranted ->
37+
TODO(/* The required permissions in result.permissions have not been granted. */)
38+
is SessionConfigureSuccess -> TODO(/* Success! */)
39+
}
40+
// [END androidxr_arcore_planetracking_configure]
41+
}

xr/src/main/java/com/example/xr/arcore/SessionLifecycleHelper.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.runtime
18+
19+
import android.app.Activity
20+
import androidx.compose.runtime.Composable
21+
import androidx.xr.compose.platform.LocalSession
22+
import androidx.xr.runtime.Session
23+
import androidx.xr.runtime.SessionCreatePermissionsNotGranted
24+
import androidx.xr.runtime.SessionCreateSuccess
25+
26+
// [START androidxr_localsession]
27+
@Composable
28+
fun ComposableUsingSession() {
29+
val session = LocalSession.current
30+
}
31+
// [END androidxr_localsession]
32+
33+
fun Activity.createSession() {
34+
// [START androidxr_session_create]
35+
when (val result = Session.create(this)) {
36+
is SessionCreateSuccess -> {
37+
val xrSession = result.session
38+
// ...
39+
}
40+
is SessionCreatePermissionsNotGranted ->
41+
TODO(/* The required permissions in result.permissions have not been granted. */)
42+
}
43+
// [END androidxr_session_create]
44+
}

0 commit comments

Comments
 (0)