Android 15 strict mode update for unsafe intents (#1582)

* Android 15 strict mode update for unsafe intents

* Use AndroidApiVersion API

* Update AndroidApiVersion API with new versions

Rename T to Tiramisu

* Ktlint warning fix

---------

Co-authored-by: Honza <rychnovsky.honza@gmail.com>
This commit is contained in:
Milan 2024-09-13 11:34:28 +02:00 committed by GitHub
parent b8ae29a17f
commit a2a54921f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 19 additions and 10 deletions

View File

@ -26,7 +26,7 @@ class Components {
}
private fun PackageManager.getProviderInfoCompat(componentName: ComponentName) =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
getProviderInfo(componentName, PackageManager.ComponentInfoFlags.of(0))
} else {
@Suppress("Deprecation")
@ -34,7 +34,7 @@ private fun PackageManager.getProviderInfoCompat(componentName: ComponentName) =
}
private fun PackageManager.getReceiverInfoCompat(componentName: ComponentName) =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
getReceiverInfo(componentName, PackageManager.ComponentInfoFlags.of(0))
} else {
@Suppress("Deprecation")

View File

@ -17,7 +17,7 @@ class VersionCodeCompatTest {
PackageInfo().apply {
@Suppress("Deprecation")
versionCode = expectedVersionCode.toInt()
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
longVersionCode = expectedVersionCode
}
}

View File

@ -46,7 +46,13 @@ object AndroidApiVersion {
val isAtLeastS = isAtLeast(Build.VERSION_CODES.S)
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
val isAtLeastT = isAtLeast(Build.VERSION_CODES.TIRAMISU)
val isAtLeastTiramisu = isAtLeast(Build.VERSION_CODES.TIRAMISU)
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
val isAtLeastUpsideDownCake = isAtLeast(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.VANILLA_ICE_CREAM)
val isAtLeastVanillaIceCream = isAtLeast(Build.VERSION_CODES.VANILLA_ICE_CREAM)
val isPreview = 0 != Build.VERSION.PREVIEW_SDK_INT
}

View File

@ -12,7 +12,7 @@ import kotlinx.coroutines.runBlocking
object ClipboardManagerUtil {
private val extraIsSensitive: String
get() =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
ClipDescription.EXTRA_IS_SENSITIVE
} else {
"android.content.extra.IS_SENSITIVE"
@ -32,7 +32,7 @@ object ClipboardManagerUtil {
putBoolean(extraIsSensitive, true)
}
}
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
// API 33 and later implement their system Toast UI.
clipboardManager.setPrimaryClip(data)
} else {

View File

@ -11,7 +11,7 @@ fun PackageManager.getPackageInfoCompat(
packageName: String,
flags: Long
): PackageInfo =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
getPackageInfoTPlus(packageName, flags)
} else {
getPackageInfoLegacy(packageName, flags)
@ -21,7 +21,7 @@ suspend fun PackageManager.getPackageInfoCompatSuspend(
packageName: String,
flags: Long
): PackageInfo =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
withContext(Dispatchers.IO) { getPackageInfoTPlus(packageName, flags) }
} else {
withContext(Dispatchers.IO) { getPackageInfoLegacy(packageName, flags) }

View File

@ -26,6 +26,9 @@ object StrictModeCompat {
// Don't enable missing network tags, because those are noisy.
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder().apply {
if (AndroidApiVersion.isAtLeastS) {
detectUnsafeIntentLaunch()
}
detectActivityLeaks()
detectCleartextNetwork()
detectContentUriWithoutPermission()

View File

@ -27,7 +27,7 @@ open class AbstractProcessNameContentProvider : ContentProvider() {
super.attachInfo(context, info)
val processName: String =
if (AndroidApiVersion.isAtLeastT) {
if (AndroidApiVersion.isAtLeastTiramisu) {
getProcessNameTPlus()
} else if (AndroidApiVersion.isAtLeastP) {
getProcessNamePPlus()

View File

@ -55,7 +55,7 @@ object ProcessNameCompat {
* due to some race conditions in Android.
*/
private fun searchForProcessName(context: Context): String? {
return if (AndroidApiVersion.isAtLeastT) {
return if (AndroidApiVersion.isAtLeastTiramisu) {
getProcessNameTPlus()
} else if (AndroidApiVersion.isAtLeastP) {
getProcessNamePPlus()