-
Notifications
You must be signed in to change notification settings - Fork 315
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
e8c29c4
35967d9
e995493
160ebff
3edddf4
7ab3b56
a55ef05
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,50 @@ | ||
| /* | ||
| * 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( | ||
| private val 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) { | ||
| controller.addPresentationModeChangedListener(executor, visualStateListener) | ||
| } | ||
|
|
||
| override fun onStop(owner: LifecycleOwner) { | ||
| // unregister to stop consuming values and prevent memory leaks. | ||
| controller.removePresentationModeChangedListener(visualStateListener) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| 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.experimental.ExperimentalProjectedApi | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| // [START androidxr_projected_ai_glasses_activity] | ||
| @OptIn(ExperimentalProjectedApi::class) | ||
| class GlassesMainActivity : ComponentActivity() { | ||
|
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. |
||
|
|
||
| private var projectedDisplayController: ProjectedDisplayController? = null | ||
| private var areVisualsOn by mutableStateOf(true) | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| lifecycleScope.launch { | ||
| // initialize the controller | ||
| val controller = ProjectedDisplayController.create(this@GlassesMainActivity) | ||
| projectedDisplayController = controller | ||
|
|
||
| // attach the observer to manage registration based on Activity visibility | ||
| val observer = GlassesLifecycleObserver( | ||
| context = this@GlassesMainActivity, | ||
| controller = controller, | ||
| onVisualsChanged = { visualsOn -> | ||
| areVisualsOn = visualsOn | ||
| } | ||
| ) | ||
| lifecycle.addObserver(observer) | ||
| } | ||
|
|
||
| setContent { | ||
| GlimmerTheme { | ||
| HomeScreen( | ||
| visualsOn = areVisualsOn, | ||
| onClose = { finish() } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onDestroy() { | ||
| super.onDestroy() | ||
| projectedDisplayController?.close() | ||
| } | ||
| } | ||
| // [END androidxr_projected_ai_glasses_activity] | ||
|
|
||
| // [START androidxr_projected_ai_glasses_activity_homescreen] | ||
| @Composable | ||
| fun HomeScreen( | ||
| visualsOn: Boolean, | ||
| modifier: Modifier = Modifier, | ||
| onClose: () -> Unit | ||
| ) { | ||
| Box( | ||
| modifier = modifier | ||
| .surface(focusable = false) | ||
| .fillMaxSize(), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Card( | ||
| title = { Text("Android XR") }, | ||
| action = { | ||
| Button(onClick = { | ||
| onClose() | ||
| }) { | ||
| Text("Close") | ||
| } | ||
| } | ||
| ) { | ||
| // UI dynamically updates based on the observer's state | ||
| if (visualsOn) { | ||
| Text("Hello, AI Glasses!") | ||
| } else { | ||
| Text("Display is off. Audio guidance active.") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END androidxr_projected_ai_glasses_activity_homescreen] | ||
| 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?