@@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid
1919import android.app.Application
2020import android.content.pm.ApplicationInfo
2121import android.os.StrictMode
22+ import android.os.StrictMode.ThreadPolicy.Builder
2223import coil.ImageLoader
2324import coil.ImageLoaderFactory
2425import com.google.samples.apps.nowinandroid.sync.initializers.Sync
@@ -40,12 +41,7 @@ class NiaApplication : Application(), ImageLoaderFactory {
4041 override fun onCreate () {
4142 super .onCreate()
4243
43- // Kill NiA if there are main thread policy violations and log the offending call.
44- if (isDebuggable()) {
45- StrictMode .setThreadPolicy(
46- StrictMode .ThreadPolicy .Builder ().detectAll().penaltyLog().penaltyDeath().build(),
47- )
48- }
44+ setStrictModePolicy()
4945
5046 // Initialize Sync; the system responsible for keeping data in the app up to date.
5147 Sync .initialize(context = this )
@@ -60,4 +56,18 @@ class NiaApplication : Application(), ImageLoaderFactory {
6056 private fun isDebuggable (): Boolean {
6157 return 0 != applicationInfo.flags and ApplicationInfo .FLAG_DEBUGGABLE
6258 }
59+
60+ /* *
61+ * Set a thread policy that detects all potential problems on the main thread, such as network
62+ * and disk access.
63+ *
64+ * If a problem is found, the offending call will be logged and the application will be killed.
65+ */
66+ private fun setStrictModePolicy () {
67+ if (isDebuggable()) {
68+ StrictMode .setThreadPolicy(
69+ Builder ().detectAll().penaltyLog().penaltyDeath().build(),
70+ )
71+ }
72+ }
6373}
0 commit comments