From 6b9f7ccefdd546e90e06246081baad6fe1f09dc3 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 17 Nov 2025 08:16:21 +0100 Subject: [PATCH] fix: cancel potential gallery image generation task Signed-off-by: alperozturk --- .../datamodel/ThumbnailsCacheManager.java | 45 +++++++++++++++++++ .../android/ui/adapter/OCFileListDelegate.kt | 16 ++++--- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index 4cea79a61845..ecc70b733b6e 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -325,6 +325,10 @@ public GalleryImageGenerationTask( this.backgroundColor = backgroundColor; } + public String getImageKey() { + return imageKey; + } + public void setListener(GalleryListener listener) { this.listener = listener; } @@ -340,11 +344,19 @@ protected Bitmap doInBackground(Object... params) { file = (OCFile) params[0]; + if (isCancelled()) { + return null; + } + if (file.getRemoteId() != null || file.isPreviewAvailable()) { // Thumbnail in cache? thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId()); + if (isCancelled()) { + return null; + } + if (thumbnail != null && !file.isUpdateThumbnailNeeded()) return getThumbnailFromCache(thumbnail); @@ -357,6 +369,10 @@ protected Bitmap doInBackground(Object... params) { @Nullable private Bitmap getThumbnailFromServerAndAddToCache(Bitmap thumbnail) { + if (isCancelled()) { + return null; + } + try { mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(), MainApp.getAppContext()); @@ -364,6 +380,10 @@ private Bitmap getThumbnailFromServerAndAddToCache(Bitmap thumbnail) { thumbnail = doResizedImageInBackground(file, storageManager); newImage = true; + if (isCancelled()) { + return null; + } + if (MimeTypeUtil.isVideo(file) && thumbnail != null) { thumbnail = addVideoOverlay(thumbnail, MainApp.getAppContext()); } @@ -416,6 +436,13 @@ private Bitmap getScaledThumbnailAfterSave(Bitmap thumbnail) { } protected void onPostExecute(Bitmap bitmap) { + if (isCancelled()) { + if (asyncTasks != null) { + asyncTasks.remove(this); + } + return; + } + if (bitmap != null && imageViewReference.get() != null) { final ImageView imageView = imageViewReference.get(); final GalleryImageGenerationTask bitmapWorkerTask = getGalleryImageGenerationTask(imageView); @@ -1164,6 +1191,24 @@ Drawable doAvatarInBackground() { } } + public static boolean cancelPotentialGalleryWork(OCFile file, ImageView imageView) { + final GalleryImageGenerationTask galleryTask = getGalleryImageGenerationTask(imageView); + + if (galleryTask != null) { + final String taskImageKey = galleryTask.imageKey; + if (taskImageKey == null || file.getRemoteId() == null || + !taskImageKey.equals(file.getRemoteId())) { + // Cancel previous task + galleryTask.cancel(true); + Log_OC.v(TAG, "Cancelled generation of gallery image for a reused imageView"); + } else { + return false; + } + } + + return true; + } + public static boolean cancelPotentialThumbnailWork(Object file, ImageView imageView) { final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView); diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt index 247641c500cd..1a4254e3f099 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt +++ b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListDelegate.kt @@ -160,14 +160,19 @@ class OCFileListDelegate( galleryRowHolder: GalleryRowHolder, width: Int ) { - if (!ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, thumbnailView)) { - Log_OC.d(tag, "setGalleryImage.cancelPotentialThumbnailWork()") + if (!ThumbnailsCacheManager.cancelPotentialGalleryWork(file, thumbnailView)) { + Log_OC.d(tag, "setGalleryImage.cancelPotentialGalleryWork()") return } - for (task in asyncTasks) { - if (file.remoteId != null && task.imageKey != null && file.remoteId == task.imageKey) { - return + val iterator = asyncGalleryTasks.iterator() + while (iterator.hasNext()) { + val task = iterator.next() + if (file.remoteId != null && file.remoteId == task.imageKey) { + task.cancel(true) + iterator.remove() + Log_OC.d(tag, "Cancelled duplicate gallery task for: ${file.remoteId}") + break } } @@ -207,7 +212,6 @@ class OCFileListDelegate( }) thumbnailView.setImageDrawable(asyncDrawable) - asyncGalleryTasks.add(task) task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, file) } catch (e: IllegalArgumentException) {