Skip to content

Conversation

@alabiaga
Copy link
Member

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.

Change-Id: I6cf5ce5174173c3134c2d29a9fb4f116616a492a
Copy link
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.

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

  1. 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.

@alabiaga alabiaga self-assigned this Sep 15, 2025
Copy link
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 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.

Comment on lines +46 to +52
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")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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")

Comment on lines +65 to +71
return when (type) {
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
CHOOSER_RESULT_COPY -> "COPY"
CHOOSER_RESULT_EDIT -> "EDIT"
CHOOSER_RESULT_UNKNOWN -> "UNKNOWN"
else -> "UNKNOWN"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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!")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Avoid using magic numbers for request codes. Extract 1234 into a named constant at the file level (e.g., private const val SHARE_TEXT_REQUEST_CODE = 1234) to improve code clarity and maintainability.

Suggested change
context, 1234,
context, SHARE_TEXT_REQUEST_CODE,

@alabiaga
Copy link
Member Author

build fails because of #366. I'll fix since @yrezgui is away.

@alabiaga alabiaga merged commit bea98fa into android:main Sep 15, 2025
2 of 3 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