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

Code before:
package au.com.shiftyjelly.pocketcasts.repositories.notification

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


Review comment (suggestion):
ℹ️ Both `context` and `intent` cannot be `null` in the `BroadcastReceiver`. They just aren't marked with any annotations in the platform code.

Code after:
package au.com.shiftyjelly.pocketcasts.repositories.notification

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)