Ignore invalid config when updating
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user