Repository: Automattic/pocket-casts-android
File: modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/notification/NotificationOpenReceiver.kt (Kotlin)

Code before:

@AndroidEntryPoint
class NotificationOpenReceiver : BroadcastReceiver() {
    @Inject
    lateinit var analyticsTracker: AnalyticsTracker

    override fun onReceive(context: Context?, intent: Intent?) {
        when (intent?.action) {
            ACTION_REVAMP_NOTIFICATION_OPENED -> handleRevampedNotificationOpen(intent, context)
            else -> handleRelayedNotificationOpen(intent ?: Intent(), context)
        }
    }

    private fun handleRelayedNotificationOpen(intent: Intent, context: Context?) {
        val category = intent.getStringExtra(EXTRA_CATEGORY)
        if (context == null || category == null) return

        analyticsTracker.track(
            event = AnalyticsEvent.NOTIFICATION_OPENED,
            properties = buildMap {
                put("category", category)
            },
        )

        val originalIntent = Intent().apply {
            intent.extras?.keySet()?.forEach {
                when (it) {
                    EXTRA_ORIGINAL_ACTION -> {
                        action = intent.getStringExtra(it)
                        intent.removeExtra(it)
                    }

                    EXTRA_ORIGINAL_FLAGS -> {
                        flags = intent.getIntExtra(it, 0)
                        intent.removeExtra(it)
                    }

                    EXTRA_ORIGINAL_COMPONENT -> {
                        component =
                            @Suppress("DEPRECATION")
                            intent.getParcelableExtra(it)
                        intent.removeExtra(it)
                    }

                    EXTRA_CATEGORY -> intent.removeExtra(it)
                    else -> Unit
                }
            }
            putExtras(intent.extras ?: Bundle())
        }
        tryLaunchIntent(originalIntent, context)

Review comment (suggestion):
❓ I'm a bit unclear on what this code is doing. Is it copying the original `intent` and then replacing `action`, `flags`, and `component` with the ones from the extras? If that's the goal, I think there might be a simpler way to express it. But I might be misunderstanding the intention here.


```kotlin
Intent(intent).apply {
    action = intent.getStringExtra(EXTRA_ORIGINAL_ACTION) ?: action
    flags = intent.getIntExtra(EXTRA_ORIGINAL_FLAGS, flags)
    component = IntentCompat.getParcelableExtra(intent, EXTRA_ORIGINAL_COMPONENT, ComponentName::class.java) ?: component
    removeExtra(EXTRA_CATEGORY)
}
```

Code after:
import timber.log.Timber

@AndroidEntryPoint
class NotificationOpenReceiver : BroadcastReceiver() {
    @Inject
    lateinit var analyticsTracker: AnalyticsTracker

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            ACTION_REVAMP_NOTIFICATION_OPENED -> handleRevampedNotificationOpen(intent, context)
            else -> handleRelayedNotificationOpen(intent, context)
        }
    }

    private fun handleRelayedNotificationOpen(intent: Intent, context: Context) {
        val category = intent.getStringExtra(EXTRA_CATEGORY) ?: return

        analyticsTracker.track(
            event = AnalyticsEvent.NOTIFICATION_OPENED,
            properties = mapOf(
                "category" to category,
            ),
        )

        val originalIntent = Intent().apply {
            flags = intent.flags
            action = intent.action
            IntentCompat.getParcelableExtra(intent, EXTRA_ORIGINAL_COMPONENT, ComponentName::class.java)?.let {
                component = it
            }
            putExtras(intent)
            removeExtra(EXTRA_CATEGORY)
            removeExtra(EXTRA_ORIGINAL_COMPONENT)
        }
        tryLaunchIntent(originalIntent, context)
    }

    private fun handleRevampedNotificationOpen(intent: Intent, context: Context) {
        val subCategory = intent.getStringExtra(EXTRA_REVAMPED_TYPE) ?: return

        val notificationType = NotificationType.fromSubCategory(subCategory)
        notificationType?.let {
            analyticsTracker.track(
                event = AnalyticsEvent.NOTIFICATION_OPENED,
                properties = mapOf(
                    "type" to it.analyticsType,
                    "category" to CATEGORY_DEEP_LINK,
                ),
            )
            val target = it.toIntent(context)
            tryLaunchIntent(target, context)