Skip to content

Commit

Permalink
extracted method and improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
camillo-positano committed Jun 20, 2024
1 parent 6c63228 commit 1e3902c
Showing 1 changed file with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,51 +296,58 @@ protected Bitmap doInBackground(Object... params) {
if (file.getRemoteId() != null || file.isPreviewAvailable()) {
// Thumbnail in cache?
thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId()
);

if (thumbnail != null && !file.isUpdateThumbnailNeeded()) {
Float size = (float) ThumbnailsCacheManager.getThumbnailDimension();

// resized dimensions
ImageDimension imageDimension = file.getImageDimension();
if (imageDimension == null ||
imageDimension.getWidth() != size ||
imageDimension.getHeight() != size) {
file.setImageDimension(new ImageDimension(thumbnail.getWidth(), thumbnail.getHeight()));
storageManager.saveFile(file);
}
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId());

if (MimeTypeUtil.isVideo(file)) {
return ThumbnailsCacheManager.addVideoOverlay(thumbnail, MainApp.getAppContext());
} else {
return thumbnail;
}
} else {
try {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
MainApp.getAppContext());
if (thumbnail != null && !file.isUpdateThumbnailNeeded())
return getThumbnailFromCache(thumbnail);

thumbnail = doResizedImageInBackground(file, storageManager);
newImage = true;
return getThumbnailFromServerAndAddToCache(thumbnail);
}

if (MimeTypeUtil.isVideo(file) && thumbnail != null) {
thumbnail = addVideoOverlay(thumbnail, MainApp.getAppContext());
}
Log_OC.d(TAG, "File cannot be previewed");
return null;
}

} catch (OutOfMemoryError oome) {
Log_OC.e(TAG, "Out of memory");
} catch (Throwable t) {
// the app should never break due to a problem with thumbnails
Log_OC.e(TAG, "Generation of gallery image for " + file + " failed", t);
}
@Nullable
private Bitmap getThumbnailFromServerAndAddToCache(Bitmap thumbnail) {
try {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
MainApp.getAppContext());

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

if (MimeTypeUtil.isVideo(file) && thumbnail != null) {
thumbnail = addVideoOverlay(thumbnail, MainApp.getAppContext());
}

} catch (OutOfMemoryError oome) {
Log_OC.e(TAG, "Out of memory");
} catch (Throwable t) {
// the app should never break due to a problem with thumbnails
Log_OC.e(TAG, "Generation of gallery image for " + file + " failed", t);
}

Log_OC.d(TAG, "File cannot be previewed");
return null;
return thumbnail;
}

private Bitmap getThumbnailFromCache(Bitmap thumbnail) {
float size = (float) ThumbnailsCacheManager.getThumbnailDimension();

// resized dimensions
ImageDimension imageDimension = file.getImageDimension();
if (imageDimension == null ||
imageDimension.getWidth() != size ||
imageDimension.getHeight() != size) {
file.setImageDimension(new ImageDimension(thumbnail.getWidth(), thumbnail.getHeight()));
storageManager.saveFile(file);
}

if (MimeTypeUtil.isVideo(file)) {
return ThumbnailsCacheManager.addVideoOverlay(thumbnail, MainApp.getAppContext());
} else {
return thumbnail;
}
}

protected void onPostExecute(Bitmap bitmap) {
Expand Down

0 comments on commit 1e3902c

Please sign in to comment.