|
| 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