CommandQueue: prevent ANR

This commit is contained in:
Milos Kozak 2022-11-15 12:27:17 +01:00
parent c2cfbd1a2b
commit a5c06513e4

View file

@ -2,6 +2,8 @@ package info.nightscout.implementation.queue
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Handler
import android.os.HandlerThread
import android.os.SystemClock import android.os.SystemClock
import android.text.Spanned import android.text.Spanned
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -97,6 +99,7 @@ class CommandQueueImplementation @Inject constructor(
) : CommandQueue { ) : CommandQueue {
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private val queue = LinkedList<Command>() private val queue = LinkedList<Command>()
@Volatile private var thread: QueueThread? = null @Volatile private var thread: QueueThread? = null
@ -205,7 +208,7 @@ class CommandQueueImplementation @Inject constructor(
// After new command added to the queue // After new command added to the queue
// start thread again if not already running // start thread again if not already running
@Synchronized fun notifyAboutNewCommand() { @Synchronized fun notifyAboutNewCommand() = handler.post {
waitForFinishedThread() waitForFinishedThread()
if (thread == null || thread!!.state == Thread.State.TERMINATED) { if (thread == null || thread!!.state == Thread.State.TERMINATED) {
thread = QueueThread(this, context, aapsLogger, rxBus, activePlugin, rh, sp, androidPermission, config) thread = QueueThread(this, context, aapsLogger, rxBus, activePlugin, rh, sp, androidPermission, config)