From e8035622b3baa2aeb792df938a2bbd23e02ee41b Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Mon, 20 Nov 2023 09:07:30 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Enable=20the=20thread=20police=20?= =?UTF-8?q?=F0=9F=91=AE=20for=20debug=20builds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * App will now crash if someone does funky stuff on the main thread. Change-Id: I9026c100705f2fec6963a1d888b40906186f9d28 --- .../samples/apps/nowinandroid/NiaApplication.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt index 8e3ad814a..775cfe8ee 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt @@ -17,6 +17,8 @@ package com.google.samples.apps.nowinandroid import android.app.Application +import android.content.pm.ApplicationInfo +import android.os.StrictMode import coil.ImageLoader import coil.ImageLoaderFactory import com.google.samples.apps.nowinandroid.sync.initializers.Sync @@ -37,10 +39,25 @@ class NiaApplication : Application(), ImageLoaderFactory { override fun onCreate() { super.onCreate() + + // Kill NiA if there are main thread policy violations and log the offending call. + if (isDebuggable()) { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build(), + ) + } + // Initialize Sync; the system responsible for keeping data in the app up to date. Sync.initialize(context = this) profileVerifierLogger() } override fun newImageLoader(): ImageLoader = imageLoader.get() + + /** + * Check if the application is debuggable. + */ + private fun isDebuggable(): Boolean { + return 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE + } } From 225f05e69701091cb21c555a646c63132c6442e2 Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Wed, 11 Dec 2024 13:41:47 +0100 Subject: [PATCH 2/3] Update app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt Co-authored-by: Don Turner --- .../com/google/samples/apps/nowinandroid/NiaApplication.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt index 775cfe8ee..507a8fe32 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt @@ -55,7 +55,7 @@ class NiaApplication : Application(), ImageLoaderFactory { override fun newImageLoader(): ImageLoader = imageLoader.get() /** - * Check if the application is debuggable. + * Return true if the application is debuggable. */ private fun isDebuggable(): Boolean { return 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE From 27a05c416e5f4e8be48e3a727bd9949de2204910 Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Wed, 11 Dec 2024 13:45:45 +0100 Subject: [PATCH 3/3] Extract strict mode policy into function Change-Id: If170b2b05859ebfca7bc91ccc790be5b43a1b772 --- .../apps/nowinandroid/NiaApplication.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt index 507a8fe32..77f72e5fc 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt @@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid import android.app.Application import android.content.pm.ApplicationInfo import android.os.StrictMode +import android.os.StrictMode.ThreadPolicy.Builder import coil.ImageLoader import coil.ImageLoaderFactory import com.google.samples.apps.nowinandroid.sync.initializers.Sync @@ -40,12 +41,7 @@ class NiaApplication : Application(), ImageLoaderFactory { override fun onCreate() { super.onCreate() - // Kill NiA if there are main thread policy violations and log the offending call. - if (isDebuggable()) { - StrictMode.setThreadPolicy( - StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build(), - ) - } + setStrictModePolicy() // Initialize Sync; the system responsible for keeping data in the app up to date. Sync.initialize(context = this) @@ -60,4 +56,18 @@ class NiaApplication : Application(), ImageLoaderFactory { private fun isDebuggable(): Boolean { return 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE } + + /** + * Set a thread policy that detects all potential problems on the main thread, such as network + * and disk access. + * + * If a problem is found, the offending call will be logged and the application will be killed. + */ + private fun setStrictModePolicy() { + if (isDebuggable()) { + StrictMode.setThreadPolicy( + Builder().detectAll().penaltyLog().penaltyDeath().build(), + ) + } + } }