|
| 1 | +package ly.count.android.demo |
| 2 | + |
| 3 | +import android.os.Build |
| 4 | +import android.os.StrictMode |
| 5 | +import android.os.StrictMode.ThreadPolicy.Builder |
| 6 | +import android.os.StrictMode.VmPolicy |
| 7 | +import android.os.strictmode.UntaggedSocketViolation |
| 8 | +import android.os.strictmode.Violation |
| 9 | +import android.util.Log |
| 10 | +import java.util.concurrent.Executors |
| 11 | + |
| 12 | +object StrictModeConfigurator { |
| 13 | + |
| 14 | + private val penaltyExecutor by lazy { Executors.newSingleThreadExecutor() } |
| 15 | + |
| 16 | + private val threadPolicy: StrictMode.ThreadPolicy |
| 17 | + get() = Builder() |
| 18 | + .detectAll() |
| 19 | + .apply { |
| 20 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 21 | + penaltyListener(penaltyExecutor) { violation -> |
| 22 | + val knownIssue = knownThreadViolations.any { it(violation) } |
| 23 | + if (!knownIssue) Log.w("StrictMode", null, violation) |
| 24 | + } |
| 25 | + } else { |
| 26 | + penaltyLog() |
| 27 | + } |
| 28 | + } |
| 29 | + .penaltyDeathOnNetwork() |
| 30 | + .build() |
| 31 | + |
| 32 | + private val knownThreadViolations: List<Violation.() -> Boolean> by lazy { |
| 33 | + listOf( |
| 34 | + // add known violations if any |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + private val vmPolicy: VmPolicy |
| 39 | + get() = VmPolicy.Builder() |
| 40 | + .apply { |
| 41 | + detectActivityLeaks() |
| 42 | + detectLeakedSqlLiteObjects() |
| 43 | + detectLeakedClosableObjects() |
| 44 | + detectLeakedRegistrationObjects() |
| 45 | + detectFileUriExposure() |
| 46 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 47 | + detectCleartextNetwork() |
| 48 | + } |
| 49 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 50 | + detectContentUriWithoutPermission() |
| 51 | + detectUntaggedSockets() // okhttp "issue" |
| 52 | + } |
| 53 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 54 | + detectCredentialProtectedWhileLocked() |
| 55 | + } |
| 56 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 57 | + detectIncorrectContextUse() // countly has known issue |
| 58 | + detectUnsafeIntentLaunch() |
| 59 | + } |
| 60 | + } |
| 61 | + .apply { |
| 62 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 63 | + penaltyListener(penaltyExecutor) { violation -> |
| 64 | + val knownIssue = knownVmViolations.any { it(violation) } |
| 65 | + if (!knownIssue) Log.w("StrictMode", null, violation) |
| 66 | + } |
| 67 | + } else { |
| 68 | + penaltyLog() |
| 69 | + } |
| 70 | + } |
| 71 | + .penaltyDeathOnFileUriExposure() |
| 72 | + .build() |
| 73 | + |
| 74 | + private val knownVmViolations: List<Violation.() -> Boolean> by lazy { |
| 75 | + listOfNotNull( |
| 76 | + { |
| 77 | + this is UntaggedSocketViolation && stackTrace.any { |
| 78 | + it.className.contains("ImmediateRequestMaker") || it.className.contains("ConnectionProcessor") // countly |
| 79 | + } |
| 80 | + }, |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + @JvmStatic |
| 85 | + fun configure() { |
| 86 | + StrictMode.setThreadPolicy(threadPolicy) |
| 87 | + StrictMode.setVmPolicy(vmPolicy) |
| 88 | + } |
| 89 | +} |
0 commit comments