-
Notifications
You must be signed in to change notification settings - Fork 469
Add share callback example #367
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
Conversation
Change-Id: I6cf5ce5174173c3134c2d29a9fb4f116616a492a
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.
Summary of Changes
Hello @alabiaga, 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 an updated example for handling share callbacks in Android applications. By leveraging the ChooserResult object, available from API Level 35, the example now provides more granular information about the user's share selection, such as the specific app component chosen or if a shortcut was utilized. This enhancement offers developers a clearer understanding of share sheet interactions, improving the overall sharing experience.
Highlights
- Enhanced Share Callback: Implements the use of Android's ChooserResult object (API Level 35+) to provide detailed feedback on share interactions, including whether a shortcut was used and the selected component.
- Conditional API Handling: The ShareResultReceiver now includes conditional logic to process ChooserResult for newer Android versions while maintaining compatibility with older APIs using Intent.EXTRA_CHOSEN_COMPONENT.
- Updated Share Example: The "Share plain text" example in ShareSender.kt has been updated to demonstrate how to register a BroadcastReceiver to receive these share callbacks.
Using Gemini Code Assist
The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request adds an example for the new share callback mechanism using ChooserResult available from API level 35. The changes correctly demonstrate how to receive information about the share action. I've provided a few suggestions to improve code quality by using more idiomatic Kotlin, removing redundancy, and avoiding magic numbers and duplicated strings. Overall, this is a great addition to the sample.
| if (chooserResult != null) { | ||
| Log.i(TAG, "isShortcut: ${chooserResult.isShortcut}") | ||
| Log.i(TAG, "type: ${typeToString(chooserResult.type)}") | ||
| Log.i(TAG, "componentName: ${chooserResult.selectedComponent}") | ||
| } else { | ||
| Log.i(TAG, "chooserResult is null") | ||
| } |
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.
To make the code more concise and idiomatic, you can use the let scope function with an elvis operator to handle the nullable chooserResult.
chooserResult?.let {
Log.i(TAG, "isShortcut: ${it.isShortcut}")
Log.i(TAG, "type: ${typeToString(it.type)}")
Log.i(TAG, "componentName: ${it.selectedComponent}")
} ?: Log.i(TAG, "chooserResult is null")| return when (type) { | ||
| CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT" | ||
| CHOOSER_RESULT_COPY -> "COPY" | ||
| CHOOSER_RESULT_EDIT -> "EDIT" | ||
| CHOOSER_RESULT_UNKNOWN -> "UNKNOWN" | ||
| else -> "UNKNOWN" | ||
| } |
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.
The when expression can be simplified. The CHOOSER_RESULT_UNKNOWN case is redundant because the else branch already handles it and any other unknown values by returning "UNKNOWN". Removing the explicit case makes the code more concise.
return when (type) {
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
CHOOSER_RESULT_COPY -> "COPY"
CHOOSER_RESULT_EDIT -> "EDIT"
else -> "UNKNOWN"
}| if (Build.VERSION.SDK_INT >= 22) { | ||
| val share = Intent(Intent.ACTION_SEND) | ||
| share.setType("text/plain") | ||
| share.putExtra(Intent.EXTRA_TEXT,"Hello, world!") |
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.
The string "Hello, world!" is hardcoded here and also in the else branch on line 122. To follow the DRY principle and improve maintainability, extract this string into a constant (e.g. private const val PLAIN_TEXT_CONTENT = "Hello, world!") and use it in both places. You will need to update line 122 as well.
| share.putExtra(Intent.EXTRA_TEXT,"Hello, world!") | |
| share.putExtra(Intent.EXTRA_TEXT, PLAIN_TEXT_CONTENT) |
| share.setType("text/plain") | ||
| share.putExtra(Intent.EXTRA_TEXT,"Hello, world!") | ||
| val pendingIntent = PendingIntent.getBroadcast( | ||
| context, 1234, |
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.
Change-Id: I6cf5ce5174173c3134c2d29a9fb4f116616a492a
Use the ChooserResult object introduced in API Level 35 for demonstrating getting information on sharing
https://developer.android.com/training/sharing/send#share-interaction-data
The documentation will be updated w/ this new info.