resolve ANR in AutomationPlugin

This commit is contained in:
Milos Kozak 2022-06-22 10:05:03 +02:00
parent 9b60655393
commit 5b383d5201

View file

@ -148,7 +148,7 @@ class AutomationPlugin @Inject constructor(
private fun storeToSP() {
val array = JSONArray()
val iterator = ArrayList(automationEvents).iterator()
val iterator = synchronized(this) { automationEvents.toMutableList().iterator() }
try {
while (iterator.hasNext()) {
val event = iterator.next()
@ -161,6 +161,7 @@ class AutomationPlugin @Inject constructor(
sp.putString(keyAutomationEvents, array.toString())
}
@Synchronized
private fun loadFromSP() {
automationEvents.clear()
val data = sp.getString(keyAutomationEvents, "")
@ -179,7 +180,7 @@ class AutomationPlugin @Inject constructor(
automationEvents.add(AutomationEvent(injector).fromJSON(event, 0))
}
@Synchronized internal fun processActions() {
internal fun processActions() {
var commonEventsEnabled = true
if (loop.isSuspended || !(loop as PluginBase).isEnabled()) {
aapsLogger.debug(LTag.AUTOMATION, "Loop deactivated")
@ -207,7 +208,7 @@ class AutomationPlugin @Inject constructor(
}
aapsLogger.debug(LTag.AUTOMATION, "processActions")
val iterator: MutableIterator<AutomationEvent> = automationEvents.iterator()
val iterator = synchronized(this) { automationEvents.toMutableList().iterator() }
while (iterator.hasNext()) {
val event = iterator.next()
if (event.isEnabled && !event.userAction && event.shouldRun())
@ -254,7 +255,7 @@ class AutomationPlugin @Inject constructor(
}
SystemClock.sleep(1100)
event.lastRun = dateUtil.now()
if (event.autoRemove) automationEvents.remove(event)
if (event.autoRemove) remove(event)
}
}
@ -299,9 +300,12 @@ class AutomationPlugin @Inject constructor(
}
@Synchronized
fun remove(event: AutomationEvent) {
automationEvents.remove(event)
}
fun at(index: Int) = automationEvents[index]
@Synchronized
fun size() = automationEvents.size
@Synchronized
@ -309,10 +313,9 @@ class AutomationPlugin @Inject constructor(
Collections.swap(automationEvents, fromPosition, toPosition)
}
@Synchronized
fun userEvents(): List<AutomationEvent> {
val list = mutableListOf<AutomationEvent>()
val iterator: MutableIterator<AutomationEvent> = automationEvents.iterator()
val iterator = synchronized(this) { automationEvents.toMutableList().iterator() }
while (iterator.hasNext()) {
val event = iterator.next()
if (event.userAction && event.isEnabled) list.add(event)