Created
February 9, 2020 22:23
-
-
Save houssemzaier/cf69f9a614f9550408872349e5798e0b to your computer and use it in GitHub Desktop.
This solution gives the possibility to just hide the "android.os.strictmode.UntaggedSocketViolation" or just log it in a verbose lever, which I prefer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequiresApi(Build.VERSION_CODES.P) | |
private fun enabledStrictMode() { | |
StrictMode.setVmPolicy( | |
StrictMode.VmPolicy.Builder() | |
.detectAllExpect("android.os.StrictMode.onUntaggedSocket") | |
.build()) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequiresApi(Build.VERSION_CODES.P) | |
fun StrictMode.VmPolicy.Builder.detectAllExpect(ignoredViolationPackageName: String, justVerbose: Boolean = true): StrictMode.VmPolicy.Builder { | |
return detectAll() | |
.penaltyListener(Executors.newSingleThreadExecutor(), StrictMode.OnVmViolationListener | |
{ | |
it.filter(ignoredViolationPackageName, justVerbose) | |
}) | |
} | |
@RequiresApi(Build.VERSION_CODES.P) | |
private fun Violation.filter(ignoredViolationPackageName: String, justVerbose: Boolean) { | |
val violationPackageName = stackTrace[0].className | |
if (violationPackageName != ignoredViolationPackageName && justVerbose) { | |
Timber.v(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution does not "hide" the violation. It's only attaches the listener then prints another console log.