Conversation
Adds a new ButtonExample composable to the compose/snippets module. Region-Tag: android_compose_snippets_button_example
|
Here is the summary of changes. You are about to add 5 region tags.
This comment is generated by snippet-bot.
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces new code snippets demonstrating synchronous and asynchronous Digital Rights Management (DRM) handling with Android's MediaPlayer in both Kotlin and Java. It also sets up a new Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds new media DRM snippets in both Kotlin and Java, along with a placeholder for a Compose Button snippet. My review focuses on improving the correctness and robustness of these snippets. The main issues identified are uninitialized variables that would lead to runtime crashes, missing exception handling for I/O and DRM operations, and resource leaks from not releasing MediaPlayer instances. I've provided suggestions to make the snippets functional and safe. Additionally, the new Compose snippet is currently empty, and I've suggested a basic implementation to make it useful.
| fun syncDRM() { | ||
| // [START android_mediaplayer_drm_sync] | ||
| mediaPlayer?.apply { | ||
| setDataSource("https://example.com/video.mp4") | ||
| setOnDrmConfigHelper { _ -> /* configuration */ } // optional, for custom configuration | ||
| prepare() | ||
| drmInfo?.also { | ||
| prepareDrm(UUID.randomUUID()) | ||
| getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null) | ||
| provideKeyResponse(byteArrayOf(), byteArrayOf()) | ||
| } | ||
|
|
||
| // MediaPlayer is now ready to use | ||
| start() | ||
| // ...play/pause/resume... | ||
| stop() | ||
| releaseDrm() | ||
| } | ||
| // [END android_mediaplayer_drm_sync] | ||
| } |
There was a problem hiding this comment.
The syncDRM function has several critical issues:
- The
mediaPlayerproperty is never initialized, so the code insidemediaPlayer?.applywill never execute, making the snippet non-functional. - Methods like
setDataSource()andprepare()can throw exceptions which are not handled. - The
MediaPlayeris not released withrelease()after use, which will cause a resource leak.releaseDrm()is called, butrelease()is also necessary.
I suggest refactoring the function to be self-contained and robust by initializing the MediaPlayer locally and using try-catch-finally for exception handling and resource management.
fun syncDRM() {
// [START android_mediaplayer_drm_sync]
val mediaPlayer = MediaPlayer()
try {
mediaPlayer.apply {
setDataSource("https://example.com/video.mp4")
setOnDrmConfigHelper { _ -> /* configuration */ } // optional, for custom configuration
prepare()
drmInfo?.also {
try {
prepareDrm(UUID.randomUUID())
getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null)
provideKeyResponse(byteArrayOf(), byteArrayOf())
} catch (e: Exception) {
e.printStackTrace()
}
}
// MediaPlayer is now ready to use
start()
// ...play/pause/resume...
stop()
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
mediaPlayer.releaseDrm()
mediaPlayer.release()
}
// [END android_mediaplayer_drm_sync]
}| public void syncDRM() { | ||
| // [START android_mediaplayer_drm_sync_java] | ||
| try { | ||
| mediaPlayer.setDataSource("https://example.com/video.mp4"); | ||
| mediaPlayer.setOnDrmConfigHelper(null); // optional, for custom configuration | ||
| mediaPlayer.prepare(); | ||
| if (mediaPlayer.getDrmInfo() != null) { | ||
| mediaPlayer.prepareDrm(UUID.randomUUID()); | ||
| mediaPlayer.getKeyRequest(null, null, null, 0, null); | ||
| mediaPlayer.provideKeyResponse(null, null); | ||
| } | ||
|
|
||
| // MediaPlayer is now ready to use | ||
| mediaPlayer.start(); | ||
| // ...play/pause/resume... | ||
| mediaPlayer.stop(); | ||
| mediaPlayer.releaseDrm(); | ||
| } catch (Exception e) { | ||
| // Handle exceptions | ||
| } | ||
| // [END android_mediaplayer_drm_sync_java] | ||
| } |
There was a problem hiding this comment.
This function has two critical issues:
- The
mediaPlayerfield is used without being initialized, which will cause aNullPointerExceptionas soon asmediaPlayer.setDataSource()is called. - The
MediaPlayerresource is not released withmediaPlayer.release(). This will lead to a resource leak.release()should be called in afinallyblock to ensure it executes even if exceptions occur.
I recommend initializing MediaPlayer within the method and using a finally block for cleanup.
public void syncDRM() {
// [START android_mediaplayer_drm_sync_java]
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://example.com/video.mp4");
mediaPlayer.setOnDrmConfigHelper(null); // optional, for custom configuration
mediaPlayer.prepare();
if (mediaPlayer.getDrmInfo() != null) {
mediaPlayer.prepareDrm(UUID.randomUUID());
mediaPlayer.getKeyRequest(null, null, null, 0, null);
mediaPlayer.provideKeyResponse(null, null);
}
// MediaPlayer is now ready to use
mediaPlayer.start();
// ...play/pause/resume...
mediaPlayer.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
mediaPlayer.releaseDrm();
mediaPlayer.release();
}
// [END android_mediaplayer_drm_sync_java]
}| mediaPlayer.apply { | ||
| setOnPreparedListener(this@MyDrmActivity) | ||
| setOnDrmInfoListener(this@MyDrmActivity) | ||
| setDataSource("https://example.com/video.mp4") | ||
| prepareAsync() | ||
| } |
There was a problem hiding this comment.
The setDataSource() method can throw an IOException among other exceptions. This is unhandled and could crash the application. The corresponding Java snippet includes a try-catch block, and this Kotlin version should as well for robustness.
| mediaPlayer.apply { | |
| setOnPreparedListener(this@MyDrmActivity) | |
| setOnDrmInfoListener(this@MyDrmActivity) | |
| setDataSource("https://example.com/video.mp4") | |
| prepareAsync() | |
| } | |
| try { | |
| mediaPlayer.apply { | |
| setOnPreparedListener(this@MyDrmActivity) | |
| setOnDrmInfoListener(this@MyDrmActivity) | |
| setDataSource("https://example.com/video.mp4") | |
| prepareAsync() | |
| } | |
| } catch (e: Exception) { | |
| e.printStackTrace() | |
| } |
| mediaPlayer.apply { | ||
| prepareDrm(UUID.randomUUID()) | ||
| getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null) | ||
| provideKeyResponse(byteArrayOf(), byteArrayOf()) | ||
| } |
There was a problem hiding this comment.
The DRM methods prepareDrm(), getKeyRequest(), and provideKeyResponse() can all throw exceptions (e.g., UnsupportedSchemeException, NotProvisionedException). These should be handled in a try-catch block to prevent crashes.
try {
mediaPlayer.apply {
prepareDrm(UUID.randomUUID())
getKeyRequest(byteArrayOf(), byteArrayOf(), "", 0, null)
provideKeyResponse(byteArrayOf(), byteArrayOf())
}
} catch (e: Exception) {
e.printStackTrace()
}| fun ButtonExample() { | ||
|
|
||
| } |
There was a problem hiding this comment.
The ButtonExample function is currently empty. To make this snippet useful, it should contain a basic implementation of a Button. I've suggested an example using fully qualified names to avoid needing to add imports. You may need to add a dependency on androidx.compose.material3 if it's not already present.
Additionally, the file is missing a newline character at the end, which is a common convention.
fun ButtonExample() {
androidx.compose.material3.Button(onClick = { /* Do something */ }) {
androidx.compose.material3.Text("Button")
}
}| mediaPlayer.setDataSource("https://example.com/video.mp4"); | ||
| mediaPlayer.prepareAsync(); | ||
| } catch (Exception e) { | ||
| // Handle exceptions |
| mediaPlayer.getKeyRequest(null, null, null, 0, null); | ||
| mediaPlayer.provideKeyResponse(null, null); | ||
| } catch (Exception e) { | ||
| // Handle exceptions |
Test PR for Kat's skills.md.