From 5b383d5201f8e2321cc25d53cc1a433827139c38 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 22 Jun 2022 10:05:03 +0200 Subject: [PATCH] resolve ANR in AutomationPlugin --- .../general/automation/AutomationPlugin.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt index aaa0bf3363..fd941dd6ca 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt @@ -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 = 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 { val list = mutableListOf() - val iterator: MutableIterator = automationEvents.iterator() + val iterator = synchronized(this) { automationEvents.toMutableList().iterator() } while (iterator.hasNext()) { val event = iterator.next() if (event.userAction && event.isEnabled) list.add(event)