Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ public GalleryImageGenerationTask(
this.backgroundColor = backgroundColor;
}

public String getImageKey() {
return imageKey;
}

public void setListener(GalleryListener listener) {
this.listener = listener;
}
Expand All @@ -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);

Expand All @@ -357,13 +369,21 @@ protected Bitmap doInBackground(Object... params) {

@Nullable
private Bitmap getThumbnailFromServerAndAddToCache(Bitmap thumbnail) {
if (isCancelled()) {
return null;
}

try {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
MainApp.getAppContext());

thumbnail = doResizedImageInBackground(file, storageManager);
newImage = true;

if (isCancelled()) {
return null;
}

if (MimeTypeUtil.isVideo(file) && thumbnail != null) {
thumbnail = addVideoOverlay(thumbnail, MainApp.getAppContext());
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -207,7 +212,6 @@ class OCFileListDelegate(
})

thumbnailView.setImageDrawable(asyncDrawable)

asyncGalleryTasks.add(task)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, file)
} catch (e: IllegalArgumentException) {
Expand Down
Loading