From bb5e54d0cd0ea01eda9dffdb1b8fca7f91f1b1cc Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 17 Nov 2025 08:47:37 +0100 Subject: [PATCH 1/2] refactor(thumbnails): simplify doResizedImageInBackground logic Signed-off-by: alperozturk --- .../extensions/OwnCloudClientExtensions.kt | 7 ++ .../datamodel/ThumbnailsCacheManager.java | 106 +++++++++--------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/nextcloud/utils/extensions/OwnCloudClientExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/OwnCloudClientExtensions.kt index ee36c24b42e1..0a28f83f7616 100644 --- a/app/src/main/java/com/nextcloud/utils/extensions/OwnCloudClientExtensions.kt +++ b/app/src/main/java/com/nextcloud/utils/extensions/OwnCloudClientExtensions.kt @@ -20,3 +20,10 @@ fun OwnCloudClient.toNextcloudClient(context: Context): NextcloudClient = OwnClo context, isFollowRedirects ) + +fun OwnCloudClient.getPreviewEndpoint(localFileId: Long, x: Int, y: Int): String = baseUri + .toString() + + "/index.php/core/preview?fileId=" + + localFileId + + "&x=" + (x / 2) + "&y=" + (y / 2) + + "&a=1&mode=cover&forceIcon=0" 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..43f1faa5f657 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -38,6 +38,7 @@ import com.nextcloud.client.account.User; import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.utils.BitmapExtensionsKt; +import com.nextcloud.utils.extensions.OwnCloudClientExtensionsKt; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.lib.common.OwnCloudAccount; @@ -1404,78 +1405,71 @@ public static void clearCache() { private static Bitmap doResizedImageInBackground(OCFile file, FileDataStorageManager storageManager) { Bitmap thumbnail; - String imageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId(); // Check disk cache in background thread thumbnail = getBitmapFromDiskCache(imageKey); + if (thumbnail != null && !file.isUpdateThumbnailNeeded()) { + Log_OC.d(TAG, "Thumbnail found in cache"); + return thumbnail; + } - // Not found in disk cache - if (thumbnail == null || file.isUpdateThumbnailNeeded()) { - Point p = getScreenDimension(); - int pxW = p.x; - int pxH = p.y; + Point p = getScreenDimension(); + int pxW = p.x; + int pxH = p.y; - if (file.isDown()) { - Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getStoragePath(), pxW, pxH); + if (file.isDown()) { + Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getStoragePath(), pxW, pxH); + if (bitmap != null) { + if (PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { + bitmap = handlePNG(bitmap, pxW, pxH); + } + thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH); + file.setUpdateThumbnailNeeded(false); + } + } else if (mClient != null) { + GetMethod getMethod = null; - if (bitmap != null) { - // Handle PNG - if (PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { - bitmap = handlePNG(bitmap, pxW, pxH); - } + try { + String uri = OwnCloudClientExtensionsKt.getPreviewEndpoint(mClient, file.getLocalId(), pxW, pxH); + Log_OC.d(TAG, "generating resized image: " + file.getFileName() + " URI: " + uri); - thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH); + getMethod = new GetMethod(uri); + getMethod.getParams().setSoTimeout(READ_TIMEOUT); - file.setUpdateThumbnailNeeded(false); + int status = mClient.executeMethod(getMethod); + if (status == HttpStatus.SC_OK) { + try (InputStream inputStream = getMethod.getResponseBodyAsStream()) { + thumbnail = BitmapFactory.decodeStream(inputStream); + Log_OC.d(TAG, "resized image generated"); + } + } else { + mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); } - } else { - // Download thumbnail from server - if (mClient != null) { - GetMethod getMethod = null; - try { - String uri = mClient.getBaseUri() + "/index.php/core/preview?fileId=" - + file.getLocalId() - + "&x=" + (pxW / 2) + "&y=" + (pxH / 2) + "&a=1&mode=cover&forceIcon=0"; - Log_OC.d(TAG, "generate resized image: " + file.getFileName() + " URI: " + uri); - getMethod = new GetMethod(uri); - - int status = mClient.executeMethod(getMethod); - if (status == HttpStatus.SC_OK) { - InputStream inputStream = getMethod.getResponseBodyAsStream(); - thumbnail = BitmapFactory.decodeStream(inputStream); - } else { - mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); - } - - // Handle PNG - if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { - thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight()); - } - - // Add thumbnail to cache - if (thumbnail != null) { - Log_OC.d(TAG, "add resized image to cache: " + file.getFileName()); - addBitmapToCache(imageKey, thumbnail); - } + if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { + thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight()); + } - } catch (Exception e) { - Log_OC.d(TAG, e.getMessage(), e); - } finally { - if (getMethod != null) { - getMethod.releaseConnection(); - } + if (thumbnail != null) { + synchronized (mThumbnailsDiskCacheLock) { + addBitmapToCache(imageKey, thumbnail); } } + } catch (Exception e) { + Log_OC.e(TAG, "doResizedBitmap: ", e); + } finally { + if (getMethod != null) { + getMethod.releaseConnection(); + } } + } - // resized dimensions and set update thumbnail needed to false to prevent rendering loop - if (thumbnail != null) { - file.setImageDimension(new ImageDimension(thumbnail.getWidth(), thumbnail.getHeight())); - file.setUpdateThumbnailNeeded(false); - storageManager.saveFile(file); - } + // resized dimensions and set update thumbnail needed to false to prevent rendering loop + if (thumbnail != null) { + file.setImageDimension(new ImageDimension(thumbnail.getWidth(), thumbnail.getHeight())); + file.setUpdateThumbnailNeeded(false); + storageManager.saveFile(file); } return thumbnail; From cde23750e5b3e8ea9eefefab5b1d2aea8b6bd25d Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 17 Nov 2025 08:48:16 +0100 Subject: [PATCH 2/2] refactor(thumbnails): simplify doResizedImageInBackground logic Signed-off-by: alperozturk --- .../owncloud/android/datamodel/ThumbnailsCacheManager.java | 4 +--- 1 file changed, 1 insertion(+), 3 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 43f1faa5f657..3cce2dca5e8e 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -1452,9 +1452,7 @@ private static Bitmap doResizedImageInBackground(OCFile file, FileDataStorageMan } if (thumbnail != null) { - synchronized (mThumbnailsDiskCacheLock) { - addBitmapToCache(imageKey, thumbnail); - } + addBitmapToCache(imageKey, thumbnail); } } catch (Exception e) { Log_OC.e(TAG, "doResizedBitmap: ", e);