extracted method and improved code

Signed-off-by: camillo-positano <camillo.positano@hotmail.it>
This commit is contained in:
camillo-positano 2024-06-20 17:51:53 +02:00 committed by Alper Öztürk
parent d42198a96f
commit 92d641eabc

View file

@ -296,53 +296,60 @@ public final class ThumbnailsCacheManager {
if (file.getRemoteId() != null || file.isPreviewAvailable()) { if (file.getRemoteId() != null || file.isPreviewAvailable()) {
// Thumbnail in cache? // Thumbnail in cache?
thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId() ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId());
);
if (thumbnail != null && !file.isUpdateThumbnailNeeded()) { if (thumbnail != null && !file.isUpdateThumbnailNeeded())
Float size = (float) ThumbnailsCacheManager.getThumbnailDimension(); return getThumbnailFromCache(thumbnail);
// resized dimensions return getThumbnailFromServerAndAddToCache(thumbnail);
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;
}
} else {
try {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
MainApp.getAppContext());
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);
}
return thumbnail;
}
} }
Log_OC.d(TAG, "File cannot be previewed"); Log_OC.d(TAG, "File cannot be previewed");
return null; return null;
} }
@Nullable
private Bitmap getThumbnailFromServerAndAddToCache(Bitmap thumbnail) {
try {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
MainApp.getAppContext());
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);
}
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) { protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && imageViewReference != null) { if (bitmap != null && imageViewReference != null) {
final ImageView imageView = imageViewReference.get(); final ImageView imageView = imageViewReference.get();