From 0a9e60959d0a940fa0ebc05184085b656328880c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Apr 2026 15:25:52 +0000 Subject: [PATCH] chore: Add StrictMode monitoring in debug and production builds Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/ee83859b-f72b-476f-a4e7-749697608dd6 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> chore: Move setupStrictMode() before super.onCreate() to catch init violations Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/ee83859b-f72b-476f-a4e7-749697608dd6 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> Move setupStrictMode() after super.onCreate() to align with Android docs Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/d25cd2c0-71f9-42fc-a82c-eb9dd8f05e87 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> feat: report main-thread StrictMode violations to Sentry in production Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/b72f6b85-c9bc-43a7-b490-8e497775d3e0 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> refactor: replace Build.VERSION_CODES.P with literal value 28 Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/a93ed231-7036-42cc-9572-5bbfde807284 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> refactor: static import SDK_INT, invert DEBUG condition Agent-Logs-Url: https://github.com/Infomaniak/android-authenticator/sessions/805018c9-2fef-40ab-b0bb-ba0b92aaa158 Co-authored-by: tevincent <149579879+tevincent@users.noreply.github.com> --- .../com/infomaniak/auth/MainApplication.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/app/src/main/kotlin/com/infomaniak/auth/MainApplication.kt b/app/src/main/kotlin/com/infomaniak/auth/MainApplication.kt index 2970c9ce..02150020 100644 --- a/app/src/main/kotlin/com/infomaniak/auth/MainApplication.kt +++ b/app/src/main/kotlin/com/infomaniak/auth/MainApplication.kt @@ -18,6 +18,9 @@ package com.infomaniak.auth import android.app.Application +import android.os.Build.VERSION.SDK_INT +import android.os.StrictMode +import androidx.annotation.RequiresApi import androidx.hilt.work.HiltWorkerFactory import androidx.work.Configuration import com.infomaniak.auth.data.preferences.SentryPreferences @@ -30,6 +33,7 @@ import com.infomaniak.core.network.ApiEnvironment import com.infomaniak.core.network.NetworkConfiguration import com.infomaniak.core.sentry.SentryConfig.configureSentry import dagger.hilt.android.HiltAndroidApp +import io.sentry.Sentry import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -64,6 +68,7 @@ open class MainApplication : Application(), Configuration.Provider { override fun onCreate() { super.onCreate() + if (BuildConfig.DEBUG) setupStrictMode() else setupProductionThreadMonitoring() notificationUtils.initNotificationChannel() userDataCleanableList = listOf(DeviceInfoUpdateManager) @@ -78,4 +83,41 @@ open class MainApplication : Application(), Configuration.Provider { var userDataCleanableList: List = emptyList() protected set } + + private fun setupStrictMode() { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy.Builder() + .detectAll() + .penaltyFlashScreen() + .penaltyLog() + .build() + ) + StrictMode.setVmPolicy( + StrictMode.VmPolicy.Builder() + .detectAll() + .penaltyLog() + .build() + ) + } + + private fun setupProductionThreadMonitoring() { + if (SDK_INT >= 28) { + setupProductionThreadMonitoringApi28() + } + } + + @RequiresApi(28) + private fun setupProductionThreadMonitoringApi28() { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy.Builder() + .detectDiskReads() + .detectDiskWrites() + .detectNetwork() + .detectCustomSlowCalls() + .penaltyListener(mainExecutor) { violation -> + Sentry.captureException(violation) + } + .build() + ) + } }