Skip to content

Commit 7ae701f

Browse files
committed
Switch threads when necessary when pre-processing html.
1 parent 2f305d6 commit 7ae701f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package org.wordpress.aztec.placeholders
33
import android.graphics.Color
44
import android.graphics.Rect
55
import android.graphics.drawable.Drawable
6+
import android.os.Handler
7+
import android.os.Looper
68
import android.text.Editable
79
import android.text.Layout
810
import android.text.Spanned
@@ -33,6 +35,7 @@ import org.wordpress.aztec.spans.AztecMediaClickableSpan
3335
import org.xml.sax.Attributes
3436
import java.lang.ref.WeakReference
3537
import java.util.UUID
38+
import java.util.concurrent.CountDownLatch
3639
import kotlin.coroutines.CoroutineContext
3740

3841
/**
@@ -547,9 +550,20 @@ class ViewPlaceholderManager(
547550
}
548551

549552
override fun beforeHtmlProcessed(source: String): String {
550-
runBlocking {
551-
clearAllViews()
553+
// If on main thread, execute directly
554+
if (Looper.myLooper() == Looper.getMainLooper()) {
555+
runBlocking { clearAllViews() }
556+
return source
557+
}
558+
559+
// Otherwise, post to main thread and wait
560+
val latch = CountDownLatch(1)
561+
Handler(Looper.getMainLooper()).post {
562+
runBlocking { clearAllViews() }
563+
latch.countDown()
552564
}
565+
latch.await()
566+
553567
return source
554568
}
555569
}

0 commit comments

Comments
 (0)