Skip to content

Commit f7130b1

Browse files
authored
Add snippets from Android XR spotlight week that hadn't made it into main (#785)
* Add head tracking snippets * Add depth map snippets * Add face tracking snippets * Add panel move example * Rebase to January Jetpack release * Update DepthMaps.kt
1 parent 6a3ccb3 commit f7130b1

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

xr/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<manifest xmlns:tools="http://schemas.android.com/tools"
18-
xmlns:android="http://schemas.android.com/apk/res/android">
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools">
19+
20+
<uses-permission android:name="android.permission.HEAD_TRACKING" />
1921

2022
<application
2123
android:label="XR"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.arcore
18+
19+
import androidx.xr.arcore.DepthMap
20+
import androidx.xr.runtime.Config
21+
import androidx.xr.runtime.Session
22+
import androidx.xr.runtime.SessionConfigureSuccess
23+
24+
private fun configureDepthEstimation(session: Session) {
25+
// [START androidxr_arcore_depthmaps_configure]
26+
val newConfig = session.config.copy(
27+
depthEstimation = Config.DepthEstimationMode.SMOOTH_ONLY,
28+
)
29+
when (val result = session.configure(newConfig)) {
30+
is SessionConfigureSuccess -> TODO(/* Success! */)
31+
else ->
32+
TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */)
33+
}
34+
// [END androidxr_arcore_depthmaps_configure]
35+
}
36+
37+
@Suppress("UnusedVariable")
38+
private suspend fun getDepthMap(session: Session) {
39+
// [START androidxr_arcore_depthmaps_obtain_depth_map]
40+
val depthMap = DepthMap.left(session) ?: return
41+
// [END androidxr_arcore_depthmaps_obtain_depth_map]
42+
43+
// [START androidxr_arcore_depthmaps_calculate_results]
44+
depthMap.state.collect { depthMap ->
45+
// example coordinates
46+
val x = 200
47+
val y = 350
48+
val index = x + y * depthMap.width
49+
val result = depthMap.smoothDepthMap?.get(index)
50+
val confidence = depthMap.smoothConfidenceMap?.get(index)
51+
}
52+
// [END androidxr_arcore_depthmaps_calculate_results]
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.arcore
18+
19+
import androidx.xr.arcore.Face
20+
import androidx.xr.arcore.FaceBlendShapeType
21+
import androidx.xr.arcore.FaceConfidenceRegion
22+
import androidx.xr.runtime.Config
23+
import androidx.xr.runtime.Session
24+
import androidx.xr.runtime.SessionConfigureSuccess
25+
import androidx.xr.runtime.TrackingState
26+
27+
private fun configureFaceTracking(session: Session) {
28+
// [START androidxr_arcore_faceTracking_configure]
29+
val newConfig = session.config.copy(
30+
faceTracking = Config.FaceTrackingMode.USER,
31+
)
32+
when (val result = session.configure(newConfig)) {
33+
is SessionConfigureSuccess -> TODO(/* Success! */)
34+
else ->
35+
TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */)
36+
}
37+
// [END androidxr_arcore_faceTracking_configure]
38+
}
39+
40+
@Suppress("UnusedVariable")
41+
private suspend fun getUserFace(session: Session) {
42+
// [START androidxr_arcore_faceTracking_getFace]
43+
val face = Face.getUserFace(session) ?: return
44+
face.state.collect { state ->
45+
if (state.trackingState != TrackingState.TRACKING) return@collect
46+
47+
val confidence = state.getConfidence(FaceConfidenceRegion.FACE_CONFIDENCE_REGION_LOWER)
48+
val blendShapeValue = state.blendShapes[FaceBlendShapeType.FACE_BLEND_SHAPE_TYPE_LIPS_TOWARD]
49+
}
50+
// [END androidxr_arcore_faceTracking_getFace]
51+
}

0 commit comments

Comments
 (0)