Skip to content

Commit ad87397

Browse files
Merge pull request #16581 from nextcloud/fix/accessing-active-jobs-of-gallery
fix(gallery): image job cancellation
2 parents 6577f80 + 071bca2 commit ad87397

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

app/src/main/java/com/nextcloud/client/jobs/gallery/GalleryImageGenerationJob.kt

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager
2121
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
2222
import com.owncloud.android.lib.common.utils.Log_OC
2323
import com.owncloud.android.utils.MimeTypeUtil
24-
import kotlinx.coroutines.CoroutineScope
2524
import kotlinx.coroutines.Dispatchers
2625
import kotlinx.coroutines.Job
2726
import kotlinx.coroutines.sync.Semaphore
2827
import kotlinx.coroutines.sync.withPermit
2928
import kotlinx.coroutines.withContext
29+
import java.util.Collections
3030
import java.util.WeakHashMap
3131

3232
class 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
}

app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.Dispatchers
4343
import kotlinx.coroutines.SupervisorJob
4444
import kotlinx.coroutines.cancel
45+
import kotlinx.coroutines.job
4546
import kotlinx.coroutines.launch
4647
import kotlinx.coroutines.withContext
4748

@@ -152,7 +153,8 @@ class OCFileListDelegate(
152153
}
153154
)
154155
} finally {
155-
GalleryImageGenerationJob.removeActiveJob(imageView, this)
156+
val currentJob = coroutineContext.job
157+
GalleryImageGenerationJob.removeActiveJob(imageView, currentJob)
156158
}
157159
}
158160

@@ -418,10 +420,15 @@ class OCFileListDelegate(
418420
showShareAvatar = bool
419421
}
420422

423+
@Suppress("TooGenericExceptionCaught")
421424
fun cleanup() {
422425
ioScope.cancel()
423426

424-
GalleryImageGenerationJob.cancelAllActiveJobs()
427+
try {
428+
GalleryImageGenerationJob.cancelAllActiveJobs()
429+
} catch (e: Exception) {
430+
Log_OC.e(TAG, "exception: ", e)
431+
}
425432

426433
// cancel async tasks from ThumbnailsCacheManager
427434
cancelAllPendingTasks()

0 commit comments

Comments
 (0)