PM-10878: Access parcelable data in a safe manor across SDK versions (#3727)

This commit is contained in:
David Perez 2024-08-14 10:28:01 -05:00 committed by GitHub
parent 2bed4986a1
commit 499bc20850
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,37 +3,24 @@
package com.x8bit.bitwarden.data.platform.util
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.core.content.IntentCompat
import androidx.core.os.BundleCompat
import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage
/**
* A means of retrieving a [Parcelable] from an [Intent] using the given [name] in a manner that
* is safe across SDK versions.
*/
inline fun <reified T> Intent.getSafeParcelableExtra(name: String): T? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableExtra(
name,
T::class.java,
)
} else {
@Suppress("DEPRECATION")
getParcelableExtra(name)
}
inline fun <reified T> Intent.getSafeParcelableExtra(
name: String,
): T? = IntentCompat.getParcelableExtra(this, name, T::class.java)
/**
* A means of retrieving a [Parcelable] from a [Bundle] using the given [name] in a manner that
* is safe across SDK versions.
*/
inline fun <reified T> Bundle.getSafeParcelableExtra(name: String): T? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelable(
name,
T::class.java,
)
} else {
@Suppress("DEPRECATION")
getParcelable(name)
}
inline fun <reified T> Bundle.getSafeParcelableExtra(
name: String,
): T? = BundleCompat.getParcelable(this, name, T::class.java)