-
Notifications
You must be signed in to change notification settings - Fork 325
Add AI glasses projected snippets #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| } |
| 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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
<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>
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
| 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) } | ||
| } | ||
| } |
| 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 | ||
| ) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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?