Skip to content

Commit 27a05c4

Browse files
Extract strict mode policy into function
Change-Id: If170b2b05859ebfca7bc91ccc790be5b43a1b772
1 parent 225f05e commit 27a05c4

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid
1919
import android.app.Application
2020
import android.content.pm.ApplicationInfo
2121
import android.os.StrictMode
22+
import android.os.StrictMode.ThreadPolicy.Builder
2223
import coil.ImageLoader
2324
import coil.ImageLoaderFactory
2425
import 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

Comments
 (0)