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

Code before:
                        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)
    }

    private fun handleRevampedNotificationOpen(intent: Intent, context: Context?) {
        if (context == null) return
        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)
        }
    }

    private fun tryLaunchIntent(intent: Intent, context: Context) {
        try {
            context.startActivity(intent.apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK })
        } catch (t: Throwable) {
            Timber.w("Failed to launch activity for intent $intent")
        }
    }

    companion object {

Review comment (bug):
⚠️ Same nullability pattern is used here. I know we have quite a bit of this in the legacy parts of the codebase, but we should really try to avoid it going forward. 

If a function or class can't operate with a `null` value, it probably shouldn't accept `null` in the first place. Instead, the caller should handle that case. Either by not calling the function or doing something else appropriate.

It's just too easy to accidentally pass in `null` and get no feedback about it, which can lead to subtle bugs that are hard to track down.

Code after:
            }
            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)
        }
    }

    private fun tryLaunchIntent(intent: Intent, context: Context) {
        try {
            context.startActivity(intent.apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK })
        } catch (t: Throwable) {
            Timber.w("Failed to launch activity for intent $intent")
        }
    }

    companion object {
        private const val ACTION_REVAMP_NOTIFICATION_OPENED = "au.com.shiftyjelly.pocketcasts.ACTION_REVAMPED_NOTIFICATION_OPENED"
        private const val EXTRA_REVAMPED_TYPE = "au.com.shiftyjelly.pocketcasts.extras.revamped.notification.type"
        private const val EXTRA_CATEGORY = "au.com.shiftyjelly.pocketcasts.extras.notification.category"
        private const val EXTRA_ORIGINAL_COMPONENT = "au.com.shiftyjelly.pocketcasts.extras.notification.component"
        private const val CATEGORY_DEEP_LINK = "DEEP_LINK"
        private const val CATEGORY_EPISODE = "ep"
        private const val CATEGORY_PODCAST = "po"

        fun NotificationType.toBroadcast(context: Context) = Intent(context, NotificationOpenReceiver::class.java).apply {
            action = ACTION_REVAMP_NOTIFICATION_OPENED
            putExtra(EXTRA_REVAMPED_TYPE, this@toBroadcast.subcategory)
        }

        fun toEpisodeIntentRelay(context: Context, intent: Intent) = Intent(context, NotificationOpenReceiver::class.java).copyCommonIntentProps(intent, CATEGORY_EPISODE)

        fun toPodcastIntentRelay(context: Context, intent: Intent) = Intent(context, NotificationOpenReceiver::class.java).copyCommonIntentProps(intent, CATEGORY_PODCAST)