1616
1717package com.example.xr.arcore
1818
19- import android.annotation.SuppressLint
20- import android.os.Bundle
2119import androidx.activity.ComponentActivity
2220import androidx.lifecycle.lifecycleScope
2321import androidx.xr.arcore.Hand
24- import androidx.xr.arcore.HandJointType
25- import androidx.xr.compose.platform.setSubspaceContent
22+ import androidx.xr.runtime.Config
23+ import androidx.xr.runtime.HandJointType
2624import androidx.xr.runtime.Session
25+ import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
26+ import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
27+ import androidx.xr.runtime.SessionConfigureSuccess
2728import androidx.xr.runtime.math.Pose
2829import androidx.xr.runtime.math.Quaternion
2930import androidx.xr.runtime.math.Vector3
30- import androidx.xr.scenecore.Entity
31- import androidx.xr.scenecore.GltfModel
3231import androidx.xr.scenecore.GltfModelEntity
33- import kotlinx.coroutines.guava.await
32+ import androidx.xr.scenecore.scene
3433import kotlinx.coroutines.launch
3534
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)
35+ @Suppress(" RestrictedApi" ) // b/416288516 - session.config and session.configure() are incorrectly restricted
36+ fun ComponentActivity.configureSession (session : Session ) {
37+ // [START androidxr_arcore_hand_configure]
38+ val newConfig = session.config.copy(
39+ handTracking = Config .HandTrackingMode .Enabled
40+ )
41+ when (val result = session.configure(newConfig)) {
42+ is SessionConfigureConfigurationNotSupported ->
43+ TODO (/* Some combinations of configurations are not valid. Handle this failure case. */ )
44+ is SessionConfigurePermissionsNotGranted ->
45+ TODO (/* The required permissions in result.permissions have not been granted. */ )
46+ is SessionConfigureSuccess -> TODO (/* Success! */ )
6847 }
48+ // [END androidxr_arcore_hand_configure]
6949}
7050
71- fun SampleHandsActivity .collectHands (session : Session ) {
51+ fun ComponentActivity .collectHands (session : Session ) {
7252 lifecycleScope.launch {
7353 // [START androidxr_arcore_hand_collect]
7454 Hand .left(session)?.state?.collect { handState -> // or Hand.right(session)
@@ -85,9 +65,9 @@ fun SampleHandsActivity.collectHands(session: Session) {
8565 }
8666}
8767
88- @SuppressLint( " RestrictedApi " ) // HandJointType is mistakenly @Restrict: b/397415504
89- fun SampleHandsActivity. renderPlanetAtHandPalm ( leftHandState : Hand . State ) {
90- val palmEntity = palmEntity ? : return
68+ fun ComponentActivity. renderPlanetAtHandPalm ( leftHandState : Hand . State ) {
69+ val session : Session = null !!
70+ val palmEntity: GltfModelEntity = null !!
9171 // [START androidxr_arcore_hand_entityAtHandPalm]
9272 val palmPose = leftHandState.handJoints[HandJointType .PALM ] ? : return
9373
@@ -96,18 +76,18 @@ fun SampleHandsActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
9676 palmEntity.setHidden(angle > Math .toRadians(40.0 ))
9777
9878 val transformedPose =
99- scenecoreSession .perceptionSpace.transformPoseTo(
79+ session.scene .perceptionSpace.transformPoseTo(
10080 palmPose,
101- scenecoreSession .activitySpace,
81+ session.scene .activitySpace,
10282 )
10383 val newPosition = transformedPose.translation + transformedPose.down * 0.05f
10484 palmEntity.setPose(Pose (newPosition, transformedPose.rotation))
10585 // [END androidxr_arcore_hand_entityAtHandPalm]
10686}
10787
108- @SuppressLint( " RestrictedApi " ) // HandJointType is mistakenly @Restrict: b/397415504
109- fun SampleHandsActivity. renderPlanetAtFingerTip ( rightHandState : Hand . State ) {
110- val indexFingerEntity = indexFingerEntity ? : return
88+ fun ComponentActivity. renderPlanetAtFingerTip ( rightHandState : Hand . State ) {
89+ val session : Session = null !!
90+ val indexFingerEntity: GltfModelEntity = null !!
11191
11292 // [START androidxr_arcore_hand_entityAtIndexFingerTip]
11393 val tipPose = rightHandState.handJoints[HandJointType .INDEX_TIP ] ? : return
@@ -117,9 +97,9 @@ fun SampleHandsActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
11797 indexFingerEntity.setHidden(angle > Math .toRadians(40.0 ))
11898
11999 val transformedPose =
120- scenecoreSession .perceptionSpace.transformPoseTo(
100+ session.scene .perceptionSpace.transformPoseTo(
121101 tipPose,
122- scenecoreSession .activitySpace,
102+ session.scene .activitySpace,
123103 )
124104 val position = transformedPose.translation + transformedPose.forward * 0.03f
125105 val rotation = Quaternion .fromLookTowards(transformedPose.up, Vector3 .Up )
0 commit comments