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

Code before:

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTracker
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
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 ?: 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)

Review comment (suggestion):
⚠️ Is there a reason we're accepting a nullable `Context?` here if we just return early when it's `null`? It might make more sense to require a non-null `Context` instead.

Code after:

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.core.content.IntentCompat
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTracker
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
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)