Support strict intent matching (#5483)

* Enable some StrictMode flags in debug

* Enable intentMatchingFlags flag

* Add a flag to disable StrictMode

* Enable strict mode for threading but log only
This commit is contained in:
Timothy
2025-07-08 08:02:33 +02:00
committed by GitHub
parent a07cba508a
commit 713cc947f8
5 changed files with 56 additions and 1 deletions

View File

@ -71,7 +71,8 @@
android:networkSecurityConfig="@xml/network_security_config"
android:enableOnBackInvokedCallback="true"
android:localeConfig="@xml/locales_config"
tools:ignore="GoogleAppIndexingWarning"
android:intentMatchingFlags="enforceIntentFilter"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
tools:targetApi="tiramisu">
<!-- Start things like SensorWorker on device boot -->

View File

@ -11,6 +11,7 @@ import android.net.wifi.WifiManager
import android.nfc.NfcAdapter
import android.os.Build
import android.os.PowerManager
import android.os.StrictMode
import android.telephony.TelephonyManager
import androidx.core.content.ContextCompat
import coil3.ImageLoader
@ -63,6 +64,29 @@ open class HomeAssistantApplication : Application(), SingletonImageLoader.Factor
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
BuildConfig.DEBUG &&
!BuildConfig.NO_STRICT_MODE
) {
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectIncorrectContextUse()
.detectUnsafeIntentLaunch()
.detectLeakedRegistrationObjects()
.penaltyLog()
.penaltyDeath()
.build(),
)
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build(),
)
}
// We should initialize the logger as early as possible in the lifecycle of the application
Timber.plant(Timber.DebugTree())

View File

@ -46,6 +46,9 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
versionName = project.version.toString()
versionCode = System.getenv("VERSION_CODE")?.toIntOrNull() ?: 1
val noStrictMode = project.findProperty("noStrictMode")?.toString()?.ifEmpty { "true" }?.toBoolean() ?: false
buildConfigField("Boolean", "NO_STRICT_MODE", noStrictMode.toString())
}
buildFeatures {

View File

@ -34,3 +34,6 @@ android.experimental.enableScreenshotTest=true
# Uncomment to disable leak canary
#noLeakCanary=true
# Uncomment to disable Strict Mode
#noStrictMode=true

View File

@ -10,6 +10,7 @@ import android.net.wifi.WifiManager
import android.nfc.NfcAdapter
import android.os.Build
import android.os.PowerManager
import android.os.StrictMode
import androidx.core.content.ContextCompat
import dagger.hilt.android.HiltAndroidApp
import io.homeassistant.companion.android.common.data.keychain.KeyChainRepository
@ -35,6 +36,29 @@ open class HomeAssistantApplication : Application() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
BuildConfig.DEBUG &&
!BuildConfig.NO_STRICT_MODE
) {
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectIncorrectContextUse()
.detectUnsafeIntentLaunch()
.detectLeakedRegistrationObjects()
.penaltyLog()
.penaltyDeath()
.build(),
)
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build(),
)
}
// We should initialize the logger as early as possible in the lifecycle of the application
Timber.plant(Timber.DebugTree())