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 @@ -25,7 +25,6 @@ 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
Expand All @@ -43,13 +42,12 @@ class ShareResultReceiver : BroadcastReceiver() {
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")
}
chooserResult?.let {
Log.i(
TAG,
"Share callback: isShortcut: ${it.isShortcut}, type: ${typeToString(it.type)}, 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(
Expand All @@ -66,7 +64,6 @@ class ShareResultReceiver : BroadcastReceiver() {
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
CHOOSER_RESULT_COPY -> "COPY"
CHOOSER_RESULT_EDIT -> "EDIT"
CHOOSER_RESULT_UNKNOWN -> "UNKNOWN"
else -> "UNKNOWN"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ private val filenames = listOf(
"night_highrise.jpg",
)

private const val PLAIN_TEXT_CONTENT = "Hello, world!"
private const val PLAIN_TEXT_TYPE = "text/plain"
private const val SHARE_TEXT_REQUEST_CODE = 1234
Comment on lines +102 to +104
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It's great that you've extracted these magic values into constants. To improve consistency across the file, I'd suggest two further improvements:

  1. Use the new constants PLAIN_TEXT_TYPE and PLAIN_TEXT_CONTENT in the knowReceiver function, which currently uses the same hardcoded strings.
  2. The knowReceiver function also uses a magic number 1 for its PendingIntent request code. Consider extracting this into a named constant as well, similar to what you've done with SHARE_TEXT_REQUEST_CODE.


/**
* 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) {
if (Build.VERSION.SDK_INT >= 22) {
val share = Intent(Intent.ACTION_SEND)
share.setType("text/plain")
share.putExtra(Intent.EXTRA_TEXT,"Hello, world!")
share.setType(PLAIN_TEXT_TYPE)
share.putExtra(Intent.EXTRA_TEXT, PLAIN_TEXT_CONTENT)
val pendingIntent = PendingIntent.getBroadcast(
context, 1234,
context, SHARE_TEXT_REQUEST_CODE,
Intent(context, ShareResultReceiver::class.java),
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
Expand All @@ -118,8 +122,8 @@ private fun sharePlainText(context: Context) {
)
} else {
context.startActivity(ShareCompat.IntentBuilder(context)
.setType("text/plain")
.setText("Hello, world!")
.setType(PLAIN_TEXT_TYPE)
.setText(PLAIN_TEXT_CONTENT)
.createChooserIntent()
)
}
Expand Down
Loading