Skip to content

Update the device availability snippets#908

Open
trambui09 wants to merge 3 commits intoandroid:mainfrom
trambui09:tb/device_availability_update
Open

Update the device availability snippets#908
trambui09 wants to merge 3 commits intoandroid: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 on lines +33 to +37
@OptIn(
ExperimentalXrDeviceLifecycleApi::class, ExperimentalProjectedApi::class,
ExperimentalCoroutinesApi::class,

)
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

The @OptIn annotation formatting can be improved by removing the unnecessary empty line and consistently placing each experimental API on its own line for better readability.

Suggested change
@OptIn(
ExperimentalXrDeviceLifecycleApi::class, ExperimentalProjectedApi::class,
ExperimentalCoroutinesApi::class,
)
@OptIn(
ExperimentalXrDeviceLifecycleApi::class,
ExperimentalProjectedApi::class,
ExperimentalCoroutinesApi::class
)

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!

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.

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.

1 participant