@@ -21,12 +21,12 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager
2121import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
2222import com.owncloud.android.lib.common.utils.Log_OC
2323import com.owncloud.android.utils.MimeTypeUtil
24- import kotlinx.coroutines.CoroutineScope
2524import kotlinx.coroutines.Dispatchers
2625import kotlinx.coroutines.Job
2726import kotlinx.coroutines.sync.Semaphore
2827import kotlinx.coroutines.sync.withPermit
2928import kotlinx.coroutines.withContext
29+ import java.util.Collections
3030import java.util.WeakHashMap
3131
3232class GalleryImageGenerationJob (private val user : User , private val storageManager : FileDataStorageManager ) {
@@ -38,35 +38,48 @@ class GalleryImageGenerationJob(private val user: User, private val storageManag
3838 Runtime .getRuntime().availableProcessors() / 2
3939 )
4040 )
41- private val activeJobs = WeakHashMap <ImageView , Job >()
41+ private val activeJobs = Collections .synchronizedMap( WeakHashMap <ImageView , Job >() )
4242
4343 fun cancelAllActiveJobs () {
44- val entries = activeJobs.entries.toList()
45- for ((_, job) in entries) {
44+ val jobsToCancel = synchronized(activeJobs) {
45+ val list = activeJobs.values.toList()
46+ activeJobs.clear()
47+ list
48+ }
49+ for (job in jobsToCancel) {
4650 job.cancel()
4751 }
48- activeJobs.clear()
4952 }
5053
51- fun removeActiveJob (imageView : ImageView , job : CoroutineScope ) {
52- if (isActiveJob(imageView, job)) {
53- removeJob(imageView)
54+ fun removeActiveJob (imageView : ImageView , job : Job ) {
55+ synchronized(activeJobs) {
56+ if (isActiveJob(imageView, job)) {
57+ removeJob(imageView)
58+ }
5459 }
5560 }
5661
57- fun isActiveJob (imageView : ImageView , job : CoroutineScope ): Boolean = activeJobs[imageView] == = job
62+ fun isActiveJob (imageView : ImageView , job : Job ): Boolean = synchronized(activeJobs) {
63+ activeJobs[imageView] == = job
64+ }
5865
5966 fun storeJob (job : Job , imageView : ImageView ) {
60- activeJobs[imageView] = job
67+ synchronized(activeJobs) {
68+ activeJobs[imageView] = job
69+ }
6170 }
6271
6372 fun cancelPreviousJob (imageView : ImageView ) {
64- activeJobs[imageView]?.cancel()
65- removeJob(imageView)
73+ synchronized(activeJobs) {
74+ activeJobs[imageView]?.cancel()
75+ activeJobs.remove(imageView)
76+ }
6677 }
6778
6879 fun removeJob (imageView : ImageView ) {
69- activeJobs.remove(imageView)
80+ synchronized(activeJobs) {
81+ activeJobs.remove(imageView)
82+ }
7083 }
7184 }
7285
@@ -95,8 +108,7 @@ class GalleryImageGenerationJob(private val user: User, private val storageManag
95108 }
96109
97110 setThumbnail(bitmap, file, imageView, newImage, listener)
98- } catch (e: Exception ) {
99- Log_OC .e(TAG , " gallery image generation job: " , e)
111+ } catch (_: Exception ) {
100112 withContext(Dispatchers .Main ) {
101113 listener.onError()
102114 }
0 commit comments