Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,54 @@ import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.service.chooser.ChooserResult
import android.service.chooser.ChooserResult.CHOOSER_RESULT_COPY
import android.service.chooser.ChooserResult.CHOOSER_RESULT_EDIT
import android.service.chooser.ChooserResult.CHOOSER_RESULT_SELECTED_COMPONENT
import android.service.chooser.ChooserResult.CHOOSER_RESULT_UNKNOWN
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.IntentCompat


private const val TAG = "ShareResultReceiver"

@RequiresApi(22)
class ShareResultReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
// This ComponentName represents the Activity that has received the data we shared.
val componentName: ComponentName? = IntentCompat.getParcelableExtra(
intent,
Intent.EXTRA_CHOSEN_COMPONENT,
ComponentName::class.java,
)
// ...
Log.d(TAG, "componentName: $componentName")
if (Build.VERSION.SDK_INT >= 35) {
val chooserResult: ChooserResult? = IntentCompat.getParcelableExtra(
intent,
Intent.EXTRA_CHOOSER_RESULT,
ChooserResult::class.java,
)
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")
}
Comment on lines +46 to +52
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")

} else {
// This ComponentName represents the Activity that has received the data we shared.
val componentName: ComponentName? = IntentCompat.getParcelableExtra(
intent,
Intent.EXTRA_CHOSEN_COMPONENT,
ComponentName::class.java,
)
Log.d(TAG, "componentName: $componentName")
}
}

private fun typeToString(type: Int): String {
return when (type) {
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
CHOOSER_RESULT_COPY -> "COPY"
CHOOSER_RESULT_EDIT -> "EDIT"
CHOOSER_RESULT_UNKNOWN -> "UNKNOWN"
else -> "UNKNOWN"
}
Comment on lines +65 to +71
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"
        }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun ShareSender() {
) {
val context = LocalContext.current
Title(text = "Send various data")
ShareButton(text = "Share plain text") { sharePlainText(context) }
ShareButton(text = "Share plain text (Demonstrates share callback)") { sharePlainText(context) }
ShareButton(text = "Share rich text") { shareRichText(context) }
ShareButton(text = "Share image") { shareImage(context) }
ShareButton(text = "Share multiple images") { shareMultipleImages(context) }
Expand Down Expand Up @@ -99,13 +99,30 @@ private val filenames = listOf(
"night_highrise.jpg",
)

/**
* Shares a text message and demonstrates how to set up a BroadcastReceiver to be
* notified who has received the data we shared. See [ShareResultReceiver].
*/
private fun sharePlainText(context: Context) {
context.startActivity(
ShareCompat.IntentBuilder(context)
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)

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,

Intent(context, ShareResultReceiver::class.java),
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
context.startActivity(
Intent.createChooser(share, null, pendingIntent.intentSender)
)
} else {
context.startActivity(ShareCompat.IntentBuilder(context)
.setType("text/plain")
.setText("Hello, world!")
.createChooserIntent(),
)
.createChooserIntent()
)
}
}

private fun shareRichText(context: Context) {
Expand Down
Loading