2022-05-31 09:38:02 -07:00
|
|
|
package co.electriccoin.zcash.spackle
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
import android.os.StrictMode
|
|
|
|
|
2022-05-31 09:38:02 -07:00
|
|
|
object StrictModeCompat {
|
2021-09-14 14:02:06 -07:00
|
|
|
|
2022-08-09 09:39:48 -07:00
|
|
|
fun enableStrictMode(isCrashOnViolation: Boolean) {
|
|
|
|
configureStrictMode(isCrashOnViolation)
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
// Workaround for Android bug
|
|
|
|
// https://issuetracker.google.com/issues/36951662
|
|
|
|
// Not needed if target O_MR1 and running on O_MR1
|
|
|
|
// Don't really need to check target, because of Google Play enforcement on targetSdkVersion for app updates
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O_MR1) {
|
2022-08-09 09:39:48 -07:00
|
|
|
Handler(Looper.getMainLooper()).postAtFrontOfQueue {
|
|
|
|
configureStrictMode(isCrashOnViolation)
|
|
|
|
}
|
2021-09-14 14:02:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
2022-08-09 09:39:48 -07:00
|
|
|
private fun configureStrictMode(isCrashOnViolation: Boolean) {
|
2021-09-14 14:02:06 -07:00
|
|
|
StrictMode.enableDefaults()
|
|
|
|
|
|
|
|
StrictMode.setThreadPolicy(
|
|
|
|
StrictMode.ThreadPolicy.Builder().apply {
|
|
|
|
detectAll()
|
2022-08-09 09:39:48 -07:00
|
|
|
if (isCrashOnViolation) {
|
|
|
|
penaltyDeath()
|
|
|
|
} else {
|
|
|
|
penaltyLog()
|
|
|
|
}
|
2021-09-14 14:02:06 -07:00
|
|
|
}.build()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Don't enable missing network tags, because those are noisy.
|
2022-05-31 09:38:02 -07:00
|
|
|
if (AndroidApiVersion.isAtLeastO) {
|
2021-09-14 14:02:06 -07:00
|
|
|
StrictMode.setVmPolicy(
|
|
|
|
StrictMode.VmPolicy.Builder().apply {
|
|
|
|
detectActivityLeaks()
|
|
|
|
detectCleartextNetwork()
|
|
|
|
detectContentUriWithoutPermission()
|
|
|
|
detectFileUriExposure()
|
|
|
|
detectLeakedClosableObjects()
|
|
|
|
detectLeakedRegistrationObjects()
|
|
|
|
detectLeakedSqlLiteObjects()
|
2022-05-31 09:38:02 -07:00
|
|
|
if (AndroidApiVersion.isAtLeastP) {
|
2021-09-14 14:02:06 -07:00
|
|
|
// Disable because this is mostly flagging Android X and Play Services
|
|
|
|
// builder.detectNonSdkApiUsage();
|
|
|
|
}
|
2022-08-09 09:39:48 -07:00
|
|
|
|
|
|
|
if (isCrashOnViolation) {
|
|
|
|
penaltyDeath()
|
|
|
|
} else {
|
|
|
|
penaltyLog()
|
|
|
|
}
|
2021-09-14 14:02:06 -07:00
|
|
|
}.build()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
StrictMode.setVmPolicy(
|
|
|
|
StrictMode.VmPolicy.Builder().apply {
|
|
|
|
detectAll()
|
2022-08-09 09:39:48 -07:00
|
|
|
if (isCrashOnViolation) {
|
|
|
|
penaltyDeath()
|
|
|
|
} else {
|
|
|
|
penaltyLog()
|
|
|
|
}
|
2021-09-14 14:02:06 -07:00
|
|
|
}.build()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|