Repository: square/leakcanary
Issue: v2.7 causes a android.os.strictmode.DiskWriteViolation crash on API 30

### Description
LeakCanary is causing a `DiskWriteViolation` when StrickMode is enable on API 30 and 31 (on lower API levels does not seem to happen).
The problem happens only on the very first boot of the application. Subsequent starts do not cause the `DiskWriteViolation` and LeakCanary seems to work fine.

### Steps to Reproduce
A reproduction project is available here: https://github.com/leinardi/AndroidTemplateProject/tree/leakcanary

1. Start an Emulator with API level 30 or 31
2. Remove any previous installation of the repro app (or wipe the data of the app)
3. Start the repro app

**Expected behavior:**
LeakCanary should not cause any StrickMode violation.

### Version Information

* LeakCanary version: 2.7
* Android OS version: API 30 and 31
* Gradle version: 7.3

### Additional Information
Logcat on API 30:
```
2021-11-14 11:54:05.214 3443-3443/com.leinardi.template D/StrictMode: StrictMode policy violation; ~duration=1507 ms: android.os.strictmode.DiskWriteViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1552)
        at libcore.io.BlockGuardOs.mkdir(BlockGuardOs.java:235)
        at libcore.io.ForwardingOs.mkdir(ForwardingOs.java:156)
        at android.system.Os.mkdir(Os.java:377)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:684)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:672)
        at android.app.ContextImpl.getFilesDir(ContextImpl.java:717)
        at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:253)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.parsePathStrategy(LeakCanaryFileProvider.kt:480)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.getPathStrategy(LeakCanaryFileProvider.kt:428)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.access$getPathStrategy(LeakCanaryFileProvider.kt:366)
        at leakcanary.internal.LeakCanaryFileProvider.attachInfo(LeakCanaryFileProvider.kt:77)
        at android.app.ActivityThread.installProvider(ActivityThread.java:7239)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697)
        at android.app.ActivityThread.access$1300(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-11-14 11:54:05.214 3443-3443/com.leinardi.template D/AndroidRuntime: Shutting down VM
2021-11-14 11:54:05.215 3443-3443/com.leinardi.template E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.leinardi.template, PID: 3443
    java.lang.RuntimeException: StrictMode ThreadPolicy violation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1813)
        at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0$StrictMode$AndroidBlockGuardPolicy(StrictMode.java:1727)
        at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.os.strictmode.DiskWriteViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1552)
        at libcore.io.BlockGuardOs.mkdir(BlockGuardOs.java:235)
        at libcore.io.ForwardingOs.mkdir(ForwardingOs.java:156)
        at android.system.Os.mkdir(Os.java:377)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:684)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:672)
        at android.app.ContextImpl.getFilesDir(ContextImpl.java:717)
        at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:253)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.parsePathStrategy(LeakCanaryFileProvider.kt:480)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.getPathStrategy(LeakCanaryFileProvider.kt:428)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.access$getPathStrategy(LeakCanaryFileProvider.kt:366)
        at leakcanary.internal.LeakCanaryFileProvider.attachInfo(LeakCanaryFileProvider.kt:77)
        at android.app.ActivityThread.installProvider(ActivityThread.java:7239)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697)
        at android.app.ActivityThread.access$1300(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
```
Logcat on API 31:
```
2021-11-13 23:58:45.719 7972-7972/? D/StrictMode: StrictMode policy violation; ~duration=1506 ms: android.os.strictmode.DiskWriteViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1615)
        at libcore.io.BlockGuardOs.mkdir(BlockGuardOs.java:254)
        at libcore.io.ForwardingOs.mkdir(ForwardingOs.java:494)
        at android.system.Os.mkdir(Os.java:466)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:746)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:734)
        at android.app.ContextImpl.getFilesDir(ContextImpl.java:779)
        at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:261)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.parsePathStrategy(LeakCanaryFileProvider.kt:480)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.getPathStrategy(LeakCanaryFileProvider.kt:428)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.access$getPathStrategy(LeakCanaryFileProvider.kt:366)
        at leakcanary.internal.LeakCanaryFileProvider.attachInfo(LeakCanaryFileProvider.kt:77)
        at android.app.ActivityThread.installProvider(ActivityThread.java:7425)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6942)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6713)
        at android.app.ActivityThread.access$1500(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2057)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2021-11-13 23:58:45.719 7972-7972/? D/AndroidRuntime: Shutting down VM
2021-11-13 23:58:45.720 7972-7972/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.leinardi.template, PID: 7972
    java.lang.RuntimeException: StrictMode ThreadPolicy violation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1876)
        at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0$StrictMode$AndroidBlockGuardPolicy(StrictMode.java:1790)
        at android.os.StrictMode$AndroidBlockGuardPolicy$$ExternalSyntheticLambda0.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: android.os.strictmode.DiskWriteViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1615)
        at libcore.io.BlockGuardOs.mkdir(BlockGuardOs.java:254)
        at libcore.io.ForwardingOs.mkdir(ForwardingOs.java:494)
        at android.system.Os.mkdir(Os.java:466)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:746)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:734)
        at android.app.ContextImpl.getFilesDir(ContextImpl.java:779)
        at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:261)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.parsePathStrategy(LeakCanaryFileProvider.kt:480)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.getPathStrategy(LeakCanaryFileProvider.kt:428)
        at leakcanary.internal.LeakCanaryFileProvider$Companion.access$getPathStrategy(LeakCanaryFileProvider.kt:366)
        at leakcanary.internal.LeakCanaryFileProvider.attachInfo(LeakCanaryFileProvider.kt:77)
        at android.app.ActivityThread.installProvider(ActivityThread.java:7425)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6942)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6713)
        at android.app.ActivityThread.access$1500(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2057)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7842) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
```

Changed files:
leakcanary-android-core/src/main/java/leakcanary/internal/LeakCanaryFileProvider.kt

diff --git a/leakcanary-android-core/src/main/java/leakcanary/internal/LeakCanaryFileProvider.kt b/leakcanary-android-core/src/main/java/leakcanary/internal/LeakCanaryFileProvider.kt
index 052574763..43d796015 100644
--- a/leakcanary-android-core/src/main/java/leakcanary/internal/LeakCanaryFileProvider.kt
+++ b/leakcanary-android-core/src/main/java/leakcanary/internal/LeakCanaryFileProvider.kt
@@ -27,6 +27,7 @@ import android.net.Uri
 import android.os.Build
 import android.os.Environment
 import android.os.ParcelFileDescriptor
+import android.os.StrictMode
 import android.provider.OpenableColumns
 import android.text.TextUtils
 import android.webkit.MimeTypeMap
@@ -424,8 +425,15 @@ internal class LeakCanaryFileProvider : ContentProvider() {
       synchronized(sCache) {
         strat = sCache[authority]
         if (strat == null) {
+          // Minimal "fix" for https://github.com/square/leakcanary/issues/2202
           try {
-            strat = parsePathStrategy(context, authority)
+            val previousPolicy = StrictMode.getThreadPolicy()
+            try {
+              StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().build())
+              strat = parsePathStrategy(context, authority)
+            } finally {
+              StrictMode.setThreadPolicy(previousPolicy)
+            }
           } catch (e: IOException) {
             throw IllegalArgumentException(
               "Failed to parse $META_DATA_FILE_PROVIDER_PATHS meta-data", e