1414 * limitations under the License.
1515 */
1616
17+ @file:SuppressLint(" RestrictedApi" ) // Required for SNAPSHOT build. Remove after the release.
1718package com.example.xr.arcore
1819
1920import android.annotation.SuppressLint
20- import android.os.Bundle
2121import androidx.activity.ComponentActivity
2222import androidx.lifecycle.lifecycleScope
2323import 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
2626import 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
2731import androidx.xr.runtime.math.Pose
2832import androidx.xr.runtime.math.Quaternion
2933import 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
3435import 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 )
0 commit comments