Skip to content

Update the device availability snippets#908

Merged
devbridie merged 3 commits into
android:mainfrom
trambui09:tb/device_availability_update
May 8, 2026
Merged

Update the device availability snippets#908
devbridie merged 3 commits into
android:mainfrom
trambui09:tb/device_availability_update

Conversation

@trambui09
Copy link
Copy Markdown
Contributor

Update the device availability snippets to check for projected device connection first before getting the lifecycle.

@trambui09 trambui09 marked this pull request as ready for review May 7, 2026 23:17
@trambui09 trambui09 requested a review from a team as a code owner May 7, 2026 23:17
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the collectDeviceLifecycle function to reactively monitor projected device connection states using flatMapLatest and isProjectedDeviceConnected. The changes include adding necessary API requirements and experimental annotations. Feedback focuses on improving the formatting of the @OptIn annotation and ensuring that CancellationException is not swallowed in the try-catch block, which is critical for proper coroutine cancellation behavior.

Comment thread xr/src/main/java/com/example/xr/runtime/DeviceLifecycle.kt
Comment on lines +52 to +54
} catch (e: Exception) {
flowOf(Lifecycle.State.DESTROYED)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Catching a broad Exception can inadvertently intercept CancellationException, which is essential for the proper functioning of Kotlin coroutines (e.g., when flatMapLatest cancels the previous block). It is recommended to explicitly rethrow CancellationException to maintain proper structured concurrency behavior. Additionally, if you know the specific exceptions thrown by the API (such as IllegalStateException), it is better to catch those specifically.

Suggested change
} catch (e: Exception) {
flowOf(Lifecycle.State.DESTROYED)
}
} catch (e: Exception) {
if (e is kotlinx.coroutines.CancellationException) throw e
flowOf(Lifecycle.State.DESTROYED)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done! catching an IllegalStateException now.

@devbridie devbridie force-pushed the tb/device_availability_update branch from 253d43e to 6af51be Compare May 8, 2026 10:57
@devbridie devbridie merged commit 637b9ee into android:main May 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants