|
| 1 | +package io.github.acedroidx.frp |
| 2 | + |
| 3 | +import android.content.ContentProvider |
| 4 | +import android.content.ContentValues |
| 5 | +import android.content.UriMatcher |
| 6 | +import android.database.Cursor |
| 7 | +import android.database.MatrixCursor |
| 8 | +import android.net.Uri |
| 9 | +import android.os.ParcelFileDescriptor |
| 10 | +import java.io.File |
| 11 | +import java.io.FileNotFoundException |
| 12 | + |
| 13 | +class FrpConfigProvider : ContentProvider() { |
| 14 | + companion object { |
| 15 | + const val AUTHORITY = "io.github.acedroidx.frp.config" |
| 16 | + private const val CODE_CONFIGS = 1 |
| 17 | + private const val CODE_CONFIG_ITEM = 2 |
| 18 | + |
| 19 | + private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply { |
| 20 | + // 根路径列出全部配置:content://io.github.acedroidx.frp.config |
| 21 | + addURI(AUTHORITY, null, CODE_CONFIGS) |
| 22 | + // 单个配置:content://io.github.acedroidx.frp.config/{type}/{name} |
| 23 | + addURI(AUTHORITY, "*/*", CODE_CONFIG_ITEM) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + override fun onCreate(): Boolean = true |
| 28 | + |
| 29 | + override fun query( |
| 30 | + uri: Uri, |
| 31 | + projection: Array<out String>?, |
| 32 | + selection: String?, |
| 33 | + selectionArgs: Array<out String>?, |
| 34 | + sortOrder: String? |
| 35 | + ): Cursor? { |
| 36 | + val match = uriMatcher.match(uri) |
| 37 | + val context = context ?: return null |
| 38 | + val prefs = context.getSharedPreferences("data", android.content.Context.MODE_PRIVATE) |
| 39 | + |
| 40 | + // 仅在允许读取时暴露内容,避免敏感配置泄漏 |
| 41 | + if (!prefs.getBoolean(PreferencesKey.ALLOW_CONFIG_READ, false)) { |
| 42 | + throw SecurityException("Config read not allowed") |
| 43 | + } |
| 44 | + |
| 45 | + val cursor = MatrixCursor(arrayOf("_id", "type", "name")) |
| 46 | + when (match) { |
| 47 | + CODE_CONFIGS -> { |
| 48 | + var id = 0L |
| 49 | + FrpType.entries.forEach { type -> |
| 50 | + val names = type.getDir(context).list()?.toList() ?: emptyList() |
| 51 | + names.forEach { name -> |
| 52 | + cursor.addRow(arrayOf(id++, type.typeName, name)) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + CODE_CONFIG_ITEM -> { |
| 58 | + val type = uri.pathSegments.getOrNull(0) |
| 59 | + val name = uri.pathSegments.getOrNull(1) |
| 60 | + if (type.isNullOrBlank() || name.isNullOrBlank()) { |
| 61 | + return cursor |
| 62 | + } |
| 63 | + val frpType = AutoStartHelper.parseType(type) |
| 64 | + if (frpType != null) { |
| 65 | + val file = File(frpType.getDir(context), name) |
| 66 | + if (file.exists()) { |
| 67 | + cursor.addRow(arrayOf(0L, frpType.typeName, name)) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + else -> throw FileNotFoundException("Unknown URI: $uri") |
| 73 | + } |
| 74 | + return cursor |
| 75 | + } |
| 76 | + |
| 77 | + override fun getType(uri: Uri): String? { |
| 78 | + return when (uriMatcher.match(uri)) { |
| 79 | + CODE_CONFIGS -> "vnd.android.cursor.dir/vnd.io.github.acedroidx.frp.config" |
| 80 | + CODE_CONFIG_ITEM -> "text/plain" |
| 81 | + else -> null |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? { |
| 86 | + val context = context ?: throw FileNotFoundException("No context") |
| 87 | + val prefs = context.getSharedPreferences("data", android.content.Context.MODE_PRIVATE) |
| 88 | + val match = uriMatcher.match(uri) |
| 89 | + if (match != CODE_CONFIG_ITEM) { |
| 90 | + throw FileNotFoundException("Unsupported uri: $uri") |
| 91 | + } |
| 92 | + |
| 93 | + val type = uri.pathSegments.getOrNull(0) |
| 94 | + val name = uri.pathSegments.getOrNull(1) |
| 95 | + val frpType = |
| 96 | + AutoStartHelper.parseType(type) ?: throw FileNotFoundException("Invalid type: $type") |
| 97 | + if (name.isNullOrBlank()) throw FileNotFoundException("Invalid name") |
| 98 | + |
| 99 | + val modeBits = ParcelFileDescriptor.parseMode(mode) |
| 100 | + val isWrite = mode.contains("w") || mode.contains("a") || mode.contains("t") |
| 101 | + // 根据模式与开关判断是否允许读/写 |
| 102 | + if (isWrite && !prefs.getBoolean(PreferencesKey.ALLOW_CONFIG_WRITE, false)) { |
| 103 | + throw SecurityException("Config write not allowed") |
| 104 | + } |
| 105 | + if (!isWrite && !prefs.getBoolean(PreferencesKey.ALLOW_CONFIG_READ, false)) { |
| 106 | + throw SecurityException("Config read not allowed") |
| 107 | + } |
| 108 | + |
| 109 | + val dir = frpType.getDir(context) |
| 110 | + if (!dir.exists()) { |
| 111 | + dir.mkdirs() |
| 112 | + } |
| 113 | + val file = File(dir, name) |
| 114 | + if (!isWrite && !file.exists()) { |
| 115 | + throw FileNotFoundException("File not found") |
| 116 | + } |
| 117 | + return ParcelFileDescriptor.open(file, modeBits) |
| 118 | + } |
| 119 | + |
| 120 | + override fun insert(uri: Uri, values: ContentValues?): Uri? { |
| 121 | + throw UnsupportedOperationException("Insert not supported") |
| 122 | + } |
| 123 | + |
| 124 | + override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int { |
| 125 | + throw UnsupportedOperationException("Delete not supported") |
| 126 | + } |
| 127 | + |
| 128 | + override fun update( |
| 129 | + uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>? |
| 130 | + ): Int { |
| 131 | + throw UnsupportedOperationException("Update not supported") |
| 132 | + } |
| 133 | +} |
0 commit comments