Ignore invalid config when updating

This commit is contained in:
Mygod
2021-10-10 17:23:55 -04:00
parent aee1a45eba
commit aeef16eb51

View File

@@ -44,31 +44,31 @@ class BootReceiver : BroadcastReceiver() {
private const val FILENAME = "bootconfig" private const val FILENAME = "bootconfig"
private val configFile by lazy { File(app.deviceStorage.noBackupFilesDir, FILENAME) } private val configFile by lazy { File(app.deviceStorage.noBackupFilesDir, FILENAME) }
private var config: Config? private val config: Config? get() = try {
get() = try {
DataInputStream(configFile.inputStream()).use { it.readBytes().toParcelable() } DataInputStream(configFile.inputStream()).use { it.readBytes().toParcelable() }
} catch (_: FileNotFoundException) { } catch (_: FileNotFoundException) {
null null
} }
set(value) = DataOutputStream(configFile.outputStream()).use { it.write(value.toByteArray()) } private fun updateConfig(work: Config.() -> Unit) = synchronized(BootReceiver) {
val config = try {
config
} catch (e: Exception) {
Timber.w(e)
null
} ?: Config()
config.work()
DataOutputStream(configFile.outputStream()).use { it.write(config.toByteArray()) }
config
}
fun add(key: String, value: Startable) = try { fun add(key: String, value: Startable) = try {
synchronized(BootReceiver) { updateConfig { startables[key] = value }
val c = config ?: Config()
c.startables[key] = value
config = c
}
onConfigUpdated(true) onConfigUpdated(true)
} catch (e: Exception) { } catch (e: Exception) {
Timber.w(e) Timber.w(e)
} }
fun delete(key: String) = try { fun delete(key: String) = try {
onConfigUpdated(synchronized(BootReceiver) { onConfigUpdated(updateConfig { startables.remove(key) }.startables.isNotEmpty())
val c = config ?: Config()
c.startables.remove(key)
config = c
c
}.startables.isNotEmpty())
} catch (e: Exception) { } catch (e: Exception) {
Timber.w(e) Timber.w(e)
} }