Skip to content

Commit e0115c4

Browse files
fix: prevent FileProvider crash in root process on Samsung SDK 35
1 parent 0fdddd3 commit e0115c4

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
android:exported="false" />
150150

151151
<provider
152-
android:name="androidx.core.content.FileProvider"
152+
android:name=".util.SafeFileProvider"
153153
android:authorities="${applicationId}.fileprovider"
154154
android:exported="false"
155155
android:grantUriPermissions="true">
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.bmax.apatch.util
2+
3+
import android.app.Application
4+
import android.database.Cursor
5+
import android.database.MatrixCursor
6+
import android.net.Uri
7+
import android.os.ParcelFileDescriptor
8+
import androidx.core.content.FileProvider
9+
10+
class SafeFileProvider : FileProvider() {
11+
private var shouldInit = false
12+
13+
override fun onCreate(): Boolean {
14+
val processName = Application.getProcessName()
15+
shouldInit = !processName.endsWith(":root") && !processName.endsWith(":webui")
16+
if (!shouldInit) return false
17+
return try {
18+
super.onCreate()
19+
} catch (e: Exception) {
20+
false
21+
}
22+
}
23+
24+
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor {
25+
if (!shouldInit) return MatrixCursor(emptyArray())
26+
return try {
27+
super.query(uri, projection, selection, selectionArgs, sortOrder)
28+
} catch (e: Exception) {
29+
MatrixCursor(emptyArray())
30+
}
31+
}
32+
33+
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor {
34+
if (!shouldInit) throw java.io.FileNotFoundException("Not available in this process")
35+
return try {
36+
super.openFile(uri, mode) ?: throw java.io.FileNotFoundException("Null result")
37+
} catch (e: java.io.FileNotFoundException) {
38+
throw e
39+
} catch (e: Exception) {
40+
throw java.io.FileNotFoundException(e.message)
41+
}
42+
}
43+
44+
override fun getType(uri: Uri): String? {
45+
if (!shouldInit) return null
46+
return try {
47+
super.getType(uri)
48+
} catch (e: Exception) {
49+
null
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)