From ff3fb338ab4e50fe6c5ca59581a584955b153334 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Tue, 27 Sep 2022 13:00:04 -0700 Subject: [PATCH] Use coroutine instead of Handler Summary: ^ Reviewed By: LukeDefeo Differential Revision: D39848640 fbshipit-source-id: 31e9ecc6e52105c61c8765d5d8b4c2c493026416 --- .../flipper/plugins/uidebugger/common/BitmapPool.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/BitmapPool.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/BitmapPool.kt index fa7ec04eb..d4d21d2a7 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/BitmapPool.kt +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/BitmapPool.kt @@ -8,8 +8,9 @@ package com.facebook.flipper.plugins.uidebugger.common import android.graphics.Bitmap -import android.os.Handler -import android.os.Looper +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch /** BitmapPool is intended to be used on the main thread. In other words, it is not thread-safe. */ class BitmapPool(private val config: Bitmap.Config = Bitmap.Config.RGB_565) { @@ -19,7 +20,7 @@ class BitmapPool(private val config: Bitmap.Config = Bitmap.Config.RGB_565) { fun readyForReuse() } - private var handler: Handler = Handler(Looper.getMainLooper()) + val mainScope = CoroutineScope(Dispatchers.Main) private val container: MutableMap> = mutableMapOf() private var isRecycled = false @@ -54,7 +55,8 @@ class BitmapPool(private val config: Bitmap.Config = Bitmap.Config.RGB_565) { inner class LeasedBitmap(override val bitmap: Bitmap) : ReusableBitmap { override fun readyForReuse() { val key = generateKey(bitmap.width, bitmap.height) - handler.post { + + mainScope.launch { if (isRecycled) { bitmap.recycle() } else {