From ee15eef6d1dae7a66c0496dc1f5b261c17076ff0 Mon Sep 17 00:00:00 2001 From: Tram Bui Date: Tue, 22 Sep 2026 15:09:23 -0700 Subject: [PATCH 1/2] update Block of content 2 to have a launch on glasses floating action button to launch projected activity --- app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 6 ++++ .../helloandroidxr/ui/HelloAndroidXRApp.kt | 35 ++++++++++++++++++- gradle/libs.versions.toml | 3 +- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 09aa707..b44e554 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -70,4 +70,5 @@ dependencies { implementation(libs.androidx.activity.compose) implementation(libs.androidx.compose.ui.tooling) + implementation(libs.androidx.xr.projected) } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 11a291c..5796516 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -44,6 +44,12 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt b/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt index 2cdea44..ff388db 100644 --- a/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt +++ b/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt @@ -48,6 +48,14 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import android.content.Intent +import android.os.Build +import androidx.compose.material3.ExtendedFloatingActionButton +import androidx.xr.projected.ProjectedContext +import androidx.xr.projected.experimental.ExperimentalProjectedApi +import com.example.helloandroidxr.ProjectedActivity +import kotlinx.coroutines.Dispatchers import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.res.dimensionResource @@ -404,6 +412,7 @@ private fun BlockOfContentOne( } } +@OptIn(ExperimentalProjectedApi::class) @Composable private fun BlockOfContentTwo( modifier: Modifier = Modifier, @@ -427,7 +436,31 @@ private fun BlockOfContentTwo( modifier = modifier ) } else { - TextPane(stringResource(R.string.block_of_content_2), modifier = modifier.fillMaxHeight()) + Box(modifier = modifier.fillMaxHeight()) { + TextPane(stringResource(R.string.block_of_content_2), modifier = Modifier.fillMaxSize()) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) { + val context = LocalContext.current + val isGlassesConnected by remember(context) { + ProjectedContext.isProjectedDeviceConnected(context, Dispatchers.Default) + }.collectAsState(initial = false) + + if (isGlassesConnected) { + ExtendedFloatingActionButton( + onClick = { + val options = ProjectedContext.createProjectedActivityOptions(context) + val intent = Intent(context, ProjectedActivity::class.java) + context.startActivity(intent, options.toBundle()) + }, + modifier = Modifier + .align(Alignment.BottomEnd) + .padding(16.dp) + ) { + Text("Launch on Glasses") + } + } + } + } } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9bfc45c..947c1dd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,6 +11,7 @@ activityCompose = "1.13.0" composeBom = "2026.08.00" material = "1.14.0" adaptiveAndroid = "1.3.0" +projected = "1.0.0-alpha12" [libraries] androidx-arcore = { module = "androidx.xr.arcore:arcore", version.ref = "arcore" } @@ -25,7 +26,7 @@ androidx-activity-compose = { group = "androidx.activity", name = "activity-comp androidx-compose-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "androidx-runtime" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-adaptive-android = { group = "androidx.compose.material3.adaptive", name = "adaptive-android", version.ref = "adaptiveAndroid" } - +androidx-xr-projected = { module = "androidx.xr.projected:projected", version.ref = "projected" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } From 9bcd903543a66b66f6cb29b806888db6d3076a5c Mon Sep 17 00:00:00 2001 From: Tram Bui Date: Wed, 23 Sep 2026 13:37:33 -0700 Subject: [PATCH 2/2] add in projected microphone support for verbal command to change the BugDroid's properties. --- app/src/main/AndroidManifest.xml | 1 + .../helloandroidxr/ProjectedActivity.kt | 215 ++++++++++++++++++ .../helloandroidxr/ui/HelloAndroidXRApp.kt | 25 +- .../viewmodel/BugdroidViewModel.kt | 5 + 4 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/example/helloandroidxr/ProjectedActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5796516..a8ad8a2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,7 @@ + + if (status == TextToSpeech.SUCCESS) { + tts?.language = Locale.US + speak("Welcome. Tap the glasses touchpad to give a command for changing the properties of the Bug Droid," + + "like its scale, rotation, offset, material color, and material properties.") + } + } + + speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this).apply { + setRecognitionListener(object : RecognitionListener { + override fun onReadyForSpeech(params: Bundle?) { + statusText = "Listening..." + } + + override fun onBeginningOfSpeech() {} + override fun onRmsChanged(rmsdB: Float) {} + override fun onBufferReceived(buffer: ByteArray?) {} + override fun onEndOfSpeech() { + isRecording = false + } + + override fun onError(error: Int) { + isRecording = false + statusText = "Error: $error. Tap to try again." + } + + override fun onResults(results: Bundle?) { + val matches = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION) + if (!matches.isNullOrEmpty()) { + val command = matches[0] + statusText = "Command captured: $command" + speak("Command has been captured. Tap to record a new command.") + CommandBroadcaster.postCommand(command) + } + isRecording = false + } + + override fun onPartialResults(partialResults: Bundle?) {} + override fun onEvent(eventType: Int, params: Bundle?) {} + }) + } + + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + showExitDialog = true + } + }) + + setContent { + HelloAndroidXRTheme { + Surface( + modifier = Modifier.fillMaxSize(), + color = MaterialTheme.colorScheme.background + ) { + Box( + modifier = Modifier + .fillMaxSize() + .clickable { + if (isRecording) { + stopListening() + } else { + startListening() + } + }, + contentAlignment = Alignment.Center + ) { + Column(horizontalAlignment = Alignment.CenterHorizontally) { + Text( + text = statusText, + style = MaterialTheme.typography.headlineMedium, + modifier = Modifier.padding(16.dp) + ) + if (isRecording) { + Text(text = "Tap to finish", style = MaterialTheme.typography.bodyLarge) + } + } + + if (showExitDialog) { + AlertDialog( + onDismissRequest = { showExitDialog = false }, + title = { Text("Exit") }, + text = { Text("Do you really want to exit the projected activity?") }, + confirmButton = { + TextButton(onClick = { + showExitDialog = false + finish() + }) { + Text("Yes") + } + }, + dismissButton = { + TextButton(onClick = { showExitDialog = false }) { + Text("No") + } + } + ) + } + } + } + } + } + } + + private fun startListening() { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + lifecycleScope.launch(Dispatchers.IO) { + ProjectedActivityCompat.requestPermissions(this@ProjectedActivity, arrayOf(Manifest.permission.RECORD_AUDIO), 101) + } + } else { + requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), 101) + } + return + } + + val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { + putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) + putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()) + } + speechRecognizer?.startListening(intent) + isRecording = true + } + + private fun stopListening() { + speechRecognizer?.stopListening() + isRecording = false + } + + private fun speak(text: String) { + tts?.speak(text, TextToSpeech.QUEUE_FLUSH, null, null) + } + + override fun onDestroy() { + super.onDestroy() + tts?.shutdown() + speechRecognizer?.destroy() + } +} + +object CommandBroadcaster { + private var callback: ((String) -> Unit)? = null + fun setCallback(cb: (String) -> Unit) { + callback = cb + } + fun postCommand(command: String) { + callback?.invoke(command) + } +} diff --git a/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt b/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt index ff388db..bcc4eab 100644 --- a/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt +++ b/app/src/main/java/com/example/helloandroidxr/ui/HelloAndroidXRApp.kt @@ -55,6 +55,7 @@ import androidx.compose.material3.ExtendedFloatingActionButton import androidx.xr.projected.ProjectedContext import androidx.xr.projected.experimental.ExperimentalProjectedApi import com.example.helloandroidxr.ProjectedActivity +import com.example.helloandroidxr.CommandBroadcaster import kotlinx.coroutines.Dispatchers import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip @@ -108,6 +109,12 @@ fun HelloAndroidXRApp() { } val uiState by viewModel.uiState.collectAsState() + + LaunchedEffect(Unit) { + CommandBroadcaster.setCallback { command -> + viewModel.updateCapturedCommand(command) + } + } if (LocalSpatialCapabilities.current.isSpatialUiEnabled) { SpatialLayout( primaryContent = { @@ -143,7 +150,8 @@ fun HelloAndroidXRApp() { modifier = Modifier.height(240.dp), showBugdroid = uiState.showBugdroid, onSliderGroupSelected = viewModel::updateShownSliderGroup, - onResetModel = viewModel::resetModel + onResetModel = viewModel::resetModel, + capturedCommand = uiState.capturedCommand ) BlockOfContentTwo( uiState = uiState, @@ -396,7 +404,8 @@ private fun BlockOfContentOne( modifier: Modifier = Modifier, showBugdroid: Boolean, onSliderGroupSelected: (SliderGroup) -> Unit, - onResetModel: () -> Unit + onResetModel: () -> Unit, + capturedCommand: String = "" ) { if (LocalSpatialCapabilities.current.isContent3dEnabled && showBugdroid) { BugdroidControls( @@ -408,7 +417,17 @@ private fun BlockOfContentOne( modifier = modifier ) } else { - TextPane(stringResource(R.string.block_of_content_1), modifier = modifier.fillMaxHeight()) + Column(modifier = modifier.fillMaxHeight()) { + TextPane(stringResource(R.string.block_of_content_1), modifier = Modifier.weight(1f)) + + if (capturedCommand.isNotEmpty()) { + Text( + text = "Last command: $capturedCommand", + style = MaterialTheme.typography.bodyMedium, + modifier = Modifier.padding(16.dp) + ) + } + } } } diff --git a/app/src/main/java/com/example/helloandroidxr/viewmodel/BugdroidViewModel.kt b/app/src/main/java/com/example/helloandroidxr/viewmodel/BugdroidViewModel.kt index f52a6ac..5edbb78 100644 --- a/app/src/main/java/com/example/helloandroidxr/viewmodel/BugdroidViewModel.kt +++ b/app/src/main/java/com/example/helloandroidxr/viewmodel/BugdroidViewModel.kt @@ -122,6 +122,7 @@ data class BugdroidUiState( val animateBugdroid: Boolean = false, val visibleSliderGroup: SliderGroup = SliderGroup.NONE, val modelTransform: ModelTransform = ModelTransform(), + val capturedCommand: String = "", ) class BugdroidViewModel : ViewModel() { @@ -135,6 +136,10 @@ class BugdroidViewModel : ViewModel() { private var gltfEntity: GltfModelEntity? = null private var pbrMaterial: KhronosPbrMaterial? = null + fun updateCapturedCommand(command: String) { + _uiState.update { it.copy(capturedCommand = command) } + } + fun initSession(session: Session) { if (this.xrSession == session) return this.xrSession = session