Skip to content

Commit b78993d

Browse files
devbridieamyZepp
andauthored
Update XR snippets to be compatible with alpha05 versions of the libraries (#571)
* Migrate to snapshot 13623196 (alpha05) * Update to snapshot 13663278 * Update to snapshot 13681046 * Update to snapshot 13693757 * Remove guava dependency * Spotless apply * Update to snapshot 13740187 * Add snippets for xr DRM * Update to snapshot 13790637 * Manual unrestrict on 13790637. * Use ExoPlayer to detect compatibility * Fix duplicated libs in toml * Add snippets for HDR and Material improvements * Update to public alpha05 --------- Co-authored-by: azeppenfeld <[email protected]>
1 parent 341dbd4 commit b78993d

21 files changed

+386
-162
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ androidx-test-junit = "1.2.1"
2828
androidx-window = "1.5.0-alpha02"
2929
androidx-window-core = "1.5.0-alpha02"
3030
androidx-window-java = "1.5.0-alpha02"
31-
# @keep
32-
androidx-xr = "1.0.0-alpha03"
33-
# @keep
34-
androidx-xr-arcore = "1.0.0-alpha04"
35-
androidx-xr-compose = "1.0.0-alpha04"
36-
androidx-xr-scenecore = "1.0.0-alpha04"
31+
androidx-xr-arcore = "1.0.0-alpha05"
32+
androidx-xr-scenecore = "1.0.0-alpha05"
33+
androidx-xr-compose = "1.0.0-alpha05"
3734
androidxHiltNavigationCompose = "1.2.0"
3835
appcompat = "1.7.1"
3936
coil = "2.7.0"

xr/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ android {
2525
buildFeatures {
2626
compose = true
2727
}
28+
lint {
29+
disable += "RestrictedApi"
30+
}
2831
}
2932

3033
dependencies {
@@ -33,8 +36,6 @@ dependencies {
3336
implementation(libs.androidx.xr.compose)
3437

3538
implementation(libs.androidx.activity.ktx)
36-
implementation(libs.guava)
37-
implementation(libs.kotlinx.coroutines.guava)
3839

3940
implementation(libs.androidx.media3.exoplayer)
4041

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,23 @@ import androidx.xr.arcore.Trackable
2222
import androidx.xr.runtime.Config
2323
import androidx.xr.runtime.Session
2424
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
25-
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
2625
import androidx.xr.runtime.SessionConfigureSuccess
2726
import androidx.xr.runtime.math.Pose
2827
import androidx.xr.scenecore.AnchorEntity
2928
import androidx.xr.scenecore.Entity
3029
import androidx.xr.scenecore.scene
3130

32-
@Suppress("RestrictedApi") // b/416288516 - session.config and session.configure() are incorrectly restricted
3331
fun configureAnchoring(session: Session) {
3432
// [START androidxr_arcore_anchoring_configure]
3533
val newConfig = session.config.copy(
36-
anchorPersistence = Config.AnchorPersistenceMode.Enabled,
34+
anchorPersistence = Config.AnchorPersistenceMode.LOCAL,
3735
)
3836
when (val result = session.configure(newConfig)) {
3937
is SessionConfigureConfigurationNotSupported ->
4038
TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
41-
is SessionConfigurePermissionsNotGranted ->
42-
TODO(/* The required permissions in result.permissions have not been granted. */)
4339
is SessionConfigureSuccess -> TODO(/* Success! */)
40+
else ->
41+
TODO(/* A different unhandled exception was thrown. */)
4442
}
4543
// [END androidxr_arcore_anchoring_configure]
4644
}
@@ -72,7 +70,7 @@ private fun attachEntityToAnchor(
7270
) {
7371
// [START androidxr_arcore_entity_tracks_anchor]
7472
AnchorEntity.create(session, anchor).apply {
75-
setParent(session.scene.activitySpace)
73+
parent = session.scene.activitySpace
7674
addChild(entity)
7775
}
7876
// [END androidxr_arcore_entity_tracks_anchor]

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import androidx.xr.runtime.Config
2424
import androidx.xr.runtime.HandJointType
2525
import androidx.xr.runtime.Session
2626
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
27-
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
2827
import androidx.xr.runtime.SessionConfigureSuccess
2928
import androidx.xr.runtime.math.Pose
3029
import androidx.xr.runtime.math.Quaternion
@@ -35,18 +34,17 @@ import androidx.xr.scenecore.scene
3534
import kotlinx.coroutines.flow.Flow
3635
import kotlinx.coroutines.launch
3736

38-
@Suppress("RestrictedApi") // b/416288516 - session.config and session.configure() are incorrectly restricted
3937
fun ComponentActivity.configureSession(session: Session) {
4038
// [START androidxr_arcore_hand_configure]
4139
val newConfig = session.config.copy(
42-
handTracking = Config.HandTrackingMode.Enabled
40+
handTracking = Config.HandTrackingMode.BOTH
4341
)
4442
when (val result = session.configure(newConfig)) {
4543
is SessionConfigureConfigurationNotSupported ->
4644
TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
47-
is SessionConfigurePermissionsNotGranted ->
48-
TODO(/* The required permissions in result.permissions have not been granted. */)
4945
is SessionConfigureSuccess -> TODO(/* Success! */)
46+
else ->
47+
TODO(/* A different unhandled exception was thrown. */)
5048
}
5149
// [END androidxr_arcore_hand_configure]
5250
}
@@ -71,8 +69,8 @@ fun ComponentActivity.collectHands(session: Session) {
7169
fun secondaryHandDetection(activity: Activity, session: Session) {
7270
fun detectGesture(handState: Flow<Hand.State>) {}
7371
// [START androidxr_arcore_hand_handedness]
74-
val handedness = Hand.getHandedness(activity.contentResolver)
75-
val secondaryHand = if (handedness == Hand.Handedness.LEFT) Hand.right(session) else Hand.left(session)
72+
val handedness = Hand.getPrimaryHandSide(activity.contentResolver)
73+
val secondaryHand = if (handedness == Hand.HandSide.LEFT) Hand.right(session) else Hand.left(session)
7674
val handState = secondaryHand?.state ?: return
7775
detectGesture(handState)
7876
// [END androidxr_arcore_hand_handedness]
@@ -86,7 +84,7 @@ fun ComponentActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) {
8684

8785
// the down direction points in the same direction as the palm
8886
val angle = Vector3.angleBetween(palmPose.rotation * Vector3.Down, Vector3.Up)
89-
palmEntity.setHidden(angle > Math.toRadians(40.0))
87+
palmEntity.setEnabled(angle > Math.toRadians(40.0))
9088

9189
val transformedPose =
9290
session.scene.perceptionSpace.transformPoseTo(
@@ -107,7 +105,7 @@ fun ComponentActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
107105

108106
// the forward direction points towards the finger tip.
109107
val angle = Vector3.angleBetween(tipPose.rotation * Vector3.Forward, Vector3.Up)
110-
indexFingerEntity.setHidden(angle > Math.toRadians(40.0))
108+
indexFingerEntity.setEnabled(angle > Math.toRadians(40.0))
111109

112110
val transformedPose =
113111
session.scene.perceptionSpace.transformPoseTo(

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@ import androidx.xr.arcore.Plane
2020
import androidx.xr.runtime.Config
2121
import androidx.xr.runtime.Session
2222
import androidx.xr.runtime.SessionConfigureConfigurationNotSupported
23-
import androidx.xr.runtime.SessionConfigurePermissionsNotGranted
2423
import androidx.xr.runtime.SessionConfigureSuccess
2524
import androidx.xr.runtime.math.Pose
2625
import androidx.xr.runtime.math.Ray
2726
import androidx.xr.scenecore.scene
2827

29-
@Suppress("RestrictedApi") // b/416288516 - session.config and session.configure() are incorrectly restricted
3028
fun configurePlaneTracking(session: Session) {
3129
// [START androidxr_arcore_planetracking_configure]
3230
val newConfig = session.config.copy(
33-
planeTracking = Config.PlaneTrackingMode.HorizontalAndVertical,
31+
planeTracking = Config.PlaneTrackingMode.HORIZONTAL_AND_VERTICAL,
3432
)
3533
when (val result = session.configure(newConfig)) {
3634
is SessionConfigureConfigurationNotSupported ->
3735
TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
38-
is SessionConfigurePermissionsNotGranted ->
39-
TODO(/* The required permissions in result.permissions have not been granted. */)
4036
is SessionConfigureSuccess -> TODO(/* Success! */)
37+
else ->
38+
TODO(/* A different unhandled exception was thrown. */)
4139
}
4240
// [END androidxr_arcore_planetracking_configure]
4341
}
@@ -58,7 +56,7 @@ private fun hitTestTable(session: Session) {
5856
// When interested in the first Table hit:
5957
val tableHit = results.firstOrNull {
6058
val trackable = it.trackable
61-
trackable is Plane && trackable.state.value.label == Plane.Label.Table
59+
trackable is Plane && trackable.state.value.label == Plane.Label.TABLE
6260
}
6361
// [END androidxr_arcore_hitTest]
6462
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import androidx.compose.foundation.layout.padding
2525
import androidx.compose.foundation.layout.width
2626
import androidx.compose.foundation.shape.CircleShape
2727
import androidx.compose.foundation.shape.CornerSize
28-
import androidx.compose.material.MaterialTheme
29-
import androidx.compose.material.Surface
28+
import androidx.compose.material3.MaterialTheme
29+
import androidx.compose.material3.Surface
3030
import androidx.compose.material3.Text
3131
import androidx.compose.runtime.Composable
3232
import androidx.compose.ui.Alignment
@@ -36,9 +36,9 @@ import androidx.compose.ui.graphics.Color
3636
import androidx.compose.ui.res.dimensionResource
3737
import androidx.compose.ui.unit.dp
3838
import androidx.compose.ui.unit.sp
39-
import androidx.xr.compose.spatial.EdgeOffset
39+
import androidx.xr.compose.spatial.ContentEdge
4040
import androidx.xr.compose.spatial.Orbiter
41-
import androidx.xr.compose.spatial.OrbiterEdge
41+
import androidx.xr.compose.spatial.OrbiterOffsetType
4242
import androidx.xr.compose.spatial.Subspace
4343
import androidx.xr.compose.subspace.SpatialPanel
4444
import androidx.xr.compose.subspace.SpatialRow
@@ -72,7 +72,7 @@ private fun OrbiterExampleSubspace() {
7272
@Composable
7373
fun OrbiterExample() {
7474
Orbiter(
75-
position = OrbiterEdge.Bottom,
75+
position = ContentEdge.Bottom,
7676
offset = 96.dp,
7777
alignment = Alignment.CenterHorizontally
7878
) {
@@ -102,13 +102,14 @@ fun OrbiterAnchoringExample() {
102102
Subspace {
103103
SpatialRow {
104104
Orbiter(
105-
position = OrbiterEdge.Top,
106-
offset = EdgeOffset.inner(8.dp),
105+
position = ContentEdge.Top,
106+
offset = 8.dp,
107+
offsetType = OrbiterOffsetType.InnerEdge,
107108
shape = SpatialRoundedCornerShape(size = CornerSize(50))
108109
) {
109110
Text(
110111
"Hello World!",
111-
style = MaterialTheme.typography.h2,
112+
style = MaterialTheme.typography.titleMedium,
112113
modifier = Modifier
113114
.background(Color.White)
114115
.padding(16.dp)
@@ -150,7 +151,7 @@ private fun Ui2DToOribiter() {
150151

151152
// New XR differentiated approach
152153
Orbiter(
153-
position = OrbiterEdge.Start,
154+
position = ContentEdge.Start,
154155
offset = dimensionResource(R.dimen.start_orbiter_padding),
155156
alignment = Alignment.Top
156157
) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private fun ComposableThatShouldElevateInXr() {}
2727
private fun SpatialElevationExample() {
2828
// [START androidxr_compose_spatialelevation]
2929
// Elevate an otherwise 2D Composable (signified here by ComposableThatShouldElevateInXr).
30-
SpatialElevation(spatialElevationLevel = SpatialElevationLevel.Level4) {
30+
SpatialElevation(elevation = SpatialElevationLevel.Level4) {
3131
ComposableThatShouldElevateInXr()
3232
}
3333
// [END androidxr_compose_spatialelevation]

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ package com.example.xr.compose
1919
import android.content.ContentResolver
2020
import android.net.Uri
2121
import androidx.compose.runtime.Composable
22+
import androidx.compose.runtime.ExperimentalComposeApi
2223
import androidx.compose.runtime.remember
2324
import androidx.compose.ui.platform.LocalContext
2425
import androidx.compose.ui.unit.dp
26+
import androidx.media3.common.C
2527
import androidx.media3.common.MediaItem
2628
import androidx.media3.exoplayer.ExoPlayer
2729
import androidx.xr.compose.spatial.Subspace
2830
import androidx.xr.compose.subspace.SpatialExternalSurface
2931
import androidx.xr.compose.subspace.StereoMode
32+
import androidx.xr.compose.subspace.SurfaceProtection
3033
import androidx.xr.compose.subspace.layout.SubspaceModifier
3134
import androidx.xr.compose.subspace.layout.height
3235
import androidx.xr.compose.subspace.layout.width
3336

3437
// [START androidxr_compose_SpatialExternalSurfaceStereo]
38+
@OptIn(ExperimentalComposeApi::class)
3539
@Composable
3640
fun SpatialExternalSurfaceContent() {
3741
val context = LocalContext.current
@@ -69,3 +73,46 @@ fun SpatialExternalSurfaceContent() {
6973
}
7074
}
7175
// [END androidxr_compose_SpatialExternalSurfaceStereo]
76+
77+
// [START androidxr_compose_SpatialExternalSurfaceDRM]
78+
@OptIn(ExperimentalComposeApi::class)
79+
@Composable
80+
fun DrmSpatialVideoPlayer() {
81+
val context = LocalContext.current
82+
Subspace {
83+
SpatialExternalSurface(
84+
modifier = SubspaceModifier
85+
.width(1200.dp)
86+
.height(676.dp),
87+
stereoMode = StereoMode.SideBySide,
88+
surfaceProtection = SurfaceProtection.Protected
89+
) {
90+
val exoPlayer = remember { ExoPlayer.Builder(context).build() }
91+
92+
// Define the URI for your DRM-protected content and license server.
93+
val videoUri = "https://your-content-provider.com/video.mpd"
94+
val drmLicenseUrl = "https://your-license-server.com/license"
95+
96+
// Build a MediaItem with the necessary DRM configuration.
97+
val mediaItem = MediaItem.Builder()
98+
.setUri(videoUri)
99+
.setDrmConfiguration(
100+
MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
101+
.setLicenseUri(drmLicenseUrl)
102+
.build()
103+
)
104+
.build()
105+
106+
onSurfaceCreated { surface ->
107+
// The created surface is secure and can be used by the player.
108+
exoPlayer.setVideoSurface(surface)
109+
exoPlayer.setMediaItem(mediaItem)
110+
exoPlayer.prepare()
111+
exoPlayer.play()
112+
}
113+
114+
onSurfaceDestroyed { exoPlayer.release() }
115+
}
116+
}
117+
}
118+
// [END androidxr_compose_SpatialExternalSurfaceDRM]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import androidx.xr.compose.subspace.layout.depth
3535
import androidx.xr.compose.subspace.layout.height
3636
import androidx.xr.compose.subspace.layout.width
3737
import androidx.xr.runtime.Session
38+
import androidx.xr.runtime.math.IntSize2d
3839
import androidx.xr.scenecore.PanelEntity
39-
import androidx.xr.scenecore.PixelDimensions
4040
import com.example.xr.R
4141

4242
private class MyCustomView(context: Context) : View(context)
@@ -86,7 +86,7 @@ fun ComponentActivity.PanelEntityWithView(xrSession: Session) {
8686
val panelEntity = PanelEntity.create(
8787
session = xrSession,
8888
view = panelContent,
89-
pixelDimensions = PixelDimensions(500, 500),
89+
pixelDimensions = IntSize2d(500, 500),
9090
name = "panel entity"
9191
)
9292
// [END androidxr_compose_PanelEntityWithView]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
2727
import androidx.compose.ui.unit.sp
2828
import androidx.xr.compose.platform.LocalSession
2929
import androidx.xr.compose.spatial.Subspace
30+
import androidx.xr.compose.subspace.ExperimentalSubspaceVolumeApi
3031
import androidx.xr.compose.subspace.SpatialPanel
3132
import androidx.xr.compose.subspace.Volume
3233
import androidx.xr.compose.subspace.layout.SubspaceModifier
@@ -62,6 +63,7 @@ private fun VolumeExample() {
6263
}
6364

6465
// [START androidxr_compose_ObjectInAVolume]
66+
@OptIn(ExperimentalSubspaceVolumeApi::class)
6567
@Composable
6668
fun ObjectInAVolume(show3DObject: Boolean) {
6769
// [START_EXCLUDE silent]

0 commit comments

Comments
 (0)