From a5c06513e4a50c0c91051c082a99a5b77ce3bf21 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 15 Nov 2022 12:27:17 +0100 Subject: [PATCH] CommandQueue: prevent ANR --- .../implementation/queue/CommandQueueImplementation.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt b/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt index 75c90ed955..097cde704e 100644 --- a/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt +++ b/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt @@ -2,6 +2,8 @@ package info.nightscout.implementation.queue import android.content.Context import android.content.Intent +import android.os.Handler +import android.os.HandlerThread import android.os.SystemClock import android.text.Spanned import androidx.appcompat.app.AppCompatActivity @@ -97,6 +99,7 @@ class CommandQueueImplementation @Inject constructor( ) : CommandQueue { private val disposable = CompositeDisposable() + private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper) private val queue = LinkedList() @Volatile private var thread: QueueThread? = null @@ -205,7 +208,7 @@ class CommandQueueImplementation @Inject constructor( // After new command added to the queue // start thread again if not already running - @Synchronized fun notifyAboutNewCommand() { + @Synchronized fun notifyAboutNewCommand() = handler.post { waitForFinishedThread() if (thread == null || thread!!.state == Thread.State.TERMINATED) { thread = QueueThread(this, context, aapsLogger, rxBus, activePlugin, rh, sp, androidPermission, config)