Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ wearToolingPreview = "1.0.0"
webkit = "1.14.0"
wearPhoneInteractions = "1.1.0"
wearRemoteInteractions = "1.1.0"
xrGlimmer = "1.0.0-alpha03"
xrProjected = "1.0.0-alpha03"

[libraries]
accompanist-adaptive = "com.google.accompanist:accompanist-adaptive:0.37.3"
Expand Down Expand Up @@ -241,6 +243,8 @@ wear-compose-material = { module = "androidx.wear.compose:compose-material", ver
wear-compose-material3 = { module = "androidx.wear.compose:compose-material3", version.ref = "wearComposeMaterial3" }
androidx-wear-phone-interactions = { group = "androidx.wear", name = "wear-phone-interactions", version.ref = "wearPhoneInteractions" }
androidx-wear-remote-interactions = { group = "androidx.wear", name = "wear-remote-interactions", version.ref = "wearRemoteInteractions" }
androidx-glimmer = { group = "androidx.xr.glimmer", name = "glimmer", version.ref = "xrGlimmer" }
androidx-projected = { group = "androidx.xr.projected", name = "projected", version.ref = "xrProjected" }

[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down
4 changes: 3 additions & 1 deletion xr/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ dependencies {
implementation(libs.androidx.activity.ktx)

implementation(libs.androidx.media3.exoplayer)
implementation(libs.androidx.glimmer)
implementation(libs.androidx.projected)

val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
Expand Down Expand Up @@ -68,4 +70,4 @@ dependencies {

implementation(libs.androidx.activity.compose)
implementation(libs.androidx.appcompat)
}
}
19 changes: 18 additions & 1 deletion xr/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@

<application
android:label="XR"
tools:ignore="MissingApplicationIcon" />
tools:ignore="MissingApplicationIcon">
<activity
android:name="com.example.xr.projected.GlassesMainActivity"
android:exported="true"
android:requiredDisplayCategory="@string/display_category_xr_projected">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name="com.example.xr.projected.PhoneMainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you have two activities with the Main intent filter?

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.projected

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.xr.projected.ProjectedDisplayController
import androidx.xr.projected.ProjectedDisplayController.PresentationMode
import androidx.xr.projected.experimental.ExperimentalProjectedApi
import java.util.function.Consumer

@OptIn(ExperimentalProjectedApi::class)
class GlassesLifecycleObserver(
context: Context,
private val controller: ProjectedDisplayController,
private val onVisualsChanged: (Boolean) -> Unit
) : DefaultLifecycleObserver {

private val executor = ContextCompat.getMainExecutor(context)

private val visualStateListener = Consumer<ProjectedDisplayController.PresentationModeFlags> { flags ->
val visualsOn = flags.hasPresentationMode(PresentationMode.VISUALS_ON)
onVisualsChanged(visualsOn)
}

override fun onStart(owner: LifecycleOwner) {
// Register when the Activity is visible
controller.addPresentationModeChangedListener(executor, visualStateListener)
}

override fun onStop(owner: LifecycleOwner) {
// Unregister when the Activity is hidden to save battery and prevent leaks
controller.removePresentationModeChangedListener(visualStateListener)
}
}
128 changes: 128 additions & 0 deletions xr/src/main/java/com/example/xr/projected/GlassesMainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.projected

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.xr.glimmer.Button
import androidx.xr.glimmer.Card
import androidx.xr.glimmer.GlimmerTheme
import androidx.xr.glimmer.Text
import androidx.xr.glimmer.surface
import androidx.xr.projected.ProjectedDisplayController
import androidx.xr.projected.ProjectedDeviceController
import androidx.xr.projected.ProjectedDeviceController.Capability.Companion.CAPABILITY_VISUAL_UI
import androidx.xr.projected.experimental.ExperimentalProjectedApi
import kotlinx.coroutines.launch

// [START androidxr_projected_ai_glasses_activity]
@OptIn(ExperimentalProjectedApi::class)
class GlassesMainActivity : ComponentActivity() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

GlassesMainActivity is not declared in xr/src/main/AndroidManifest.xml. All activities must be declared in the manifest file. For a projected activity, it should be declared with android:theme="@android:style/Theme.NoDisplay" and an intent filter for androidx.xr.projected.ACTION_BIND_PROJECTED_SERVICE. For example:

<activity
    android:name=".projected.GlassesMainActivity"
    android:exported="true"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="androidx.xr.projected.ACTION_BIND_PROJECTED_SERVICE" />
    </intent-filter>
</activity>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you already have the code for an activity, do you want to add these activities to the manifest file for the xr module? If not, that's okay too, not a blocking comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. This is important! And will be part of the snippets too. Added

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to add the region tags in the manifest file?

Would it be?


[END androidxr_projected_ai_glasses_activity_manifest] -->```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah per our snippets criteria, you can actually omit the region tags from the manifest and leave that hardcoded in the documentation pages. It would get more complicated as more docs need to reference parts of the manifest file and there's no way to have overlapping region tags.


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel: GlassesViewModel by viewModels()

lifecycleScope.launch {
// [START androidxr_projected_device_capabilities_check]
// Check device capabilities
val projectedDeviceController = ProjectedDeviceController.create(this@GlassesMainActivity)
val isVisualSupported = projectedDeviceController.capabilities.contains(CAPABILITY_VISUAL_UI)

viewModel.setVisualUiSupported(isVisualSupported)
// [END androidxr_projected_device_capabilities_check]

val displayController = ProjectedDisplayController.create(this@GlassesMainActivity)
val observer = GlassesLifecycleObserver(
context = this@GlassesMainActivity,
controller = displayController,
onVisualsChanged = viewModel::updateVisuals
)
lifecycle.addObserver(observer)


// Cleanup observer to close the display controller
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
displayController.close()
}
})
}

setContent {
// [required] Use collectAsStateWithLifecycle for safe collection
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

GlimmerTheme {
HomeScreen(
visualsOn = uiState.areVisualsOn,
isVisualUiSupported = uiState.isVisualUiSupported,
onClose = { finish() }
)
}
}
}
}
// [END androidxr_projected_ai_glasses_activity]

// [START androidxr_projected_ai_glasses_activity_homescreen]
@Composable
fun HomeScreen(
visualsOn: Boolean,
isVisualUiSupported: Boolean,
onClose: () -> Unit,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.surface(focusable = false)
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
if (isVisualUiSupported) {
Card(
title = { Text("Android XR") },
action = {
Button(onClick = onClose) {
Text("Close")
}
}
) {
if (visualsOn) {
Text("Hello, AI Glasses!")
} else {
Text("Display is off. Audio guidance active.")
}
}
} else {
Text("Audio Guidance Mode Active")
}
}
}
// [END androidxr_projected_ai_glasses_activity_homescreen]
40 changes: 40 additions & 0 deletions xr/src/main/java/com/example/xr/projected/GlassesViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.projected

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

data class GlassesUiState(
val areVisualsOn: Boolean = true,
val isVisualUiSupported: Boolean = false
)
class GlassesViewModel : ViewModel() {
private val _uiState = MutableStateFlow(GlassesUiState())
val uiState: StateFlow<GlassesUiState> = _uiState.asStateFlow()

fun setVisualUiSupported(isSupported: Boolean) {
_uiState.update { it.copy(isVisualUiSupported = isSupported) }
}

fun updateVisuals(visualsOn: Boolean) {
_uiState.update { it.copy(areVisualsOn = visualsOn) }
}
}
111 changes: 111 additions & 0 deletions xr/src/main/java/com/example/xr/projected/PhoneMainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.projected

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.xr.projected.ProjectedContext
import androidx.xr.projected.experimental.ExperimentalProjectedApi

class PhoneMainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.BAKLAVA)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MaterialTheme {
ConnectionScreen()
}
}
}
}

@RequiresApi(Build.VERSION_CODES.BAKLAVA)
@OptIn(ExperimentalProjectedApi::class)
@Composable
fun ConnectionScreen() {
val context = LocalContext.current
Scaffold { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Hello AI Glasses",
style = MaterialTheme.typography.titleLarge
)
Spacer(modifier = Modifier.height(32.dp))
val scope = rememberCoroutineScope()
val isGlassesConnected by ProjectedContext.isProjectedDeviceConnected(
context,
scope.coroutineContext
).collectAsStateWithLifecycle(initialValue = false)
Button(
onClick = {
// [START androidxr_projected_start_glasses_activity]

val options = ProjectedContext.createProjectedActivityOptions(context)
val intent = Intent(context, GlassesMainActivity::class.java)
context.startActivity(intent, options.toBundle())

// [END androidxr_projected_start_glasses_activity]
},
colors = ButtonDefaults.buttonColors(
containerColor = if (isGlassesConnected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.error
),
enabled = isGlassesConnected
) {
Text(
text = "Launch",
style = MaterialTheme.typography.headlineMedium
)
}
Spacer(modifier = Modifier.height(32.dp))
Text(
text = "Status: " + if (isGlassesConnected) "Connected" else "Disconnected",
style = MaterialTheme.typography.titleMedium
)
}
}
}
Loading