|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.client.jobs.gallery |
| 9 | + |
| 10 | +import android.graphics.Bitmap |
| 11 | +import android.widget.ImageView |
| 12 | +import androidx.core.content.ContextCompat |
| 13 | +import com.nextcloud.client.account.User |
| 14 | +import com.nextcloud.utils.OCFileUtils |
| 15 | +import com.nextcloud.utils.allocationKilobyte |
| 16 | +import com.owncloud.android.MainApp |
| 17 | +import com.owncloud.android.R |
| 18 | +import com.owncloud.android.datamodel.FileDataStorageManager |
| 19 | +import com.owncloud.android.datamodel.OCFile |
| 20 | +import com.owncloud.android.datamodel.ThumbnailsCacheManager |
| 21 | +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory |
| 22 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 23 | +import com.owncloud.android.utils.MimeTypeUtil |
| 24 | +import kotlinx.coroutines.Dispatchers |
| 25 | +import kotlinx.coroutines.sync.Semaphore |
| 26 | +import kotlinx.coroutines.sync.withPermit |
| 27 | +import kotlinx.coroutines.withContext |
| 28 | + |
| 29 | +class GalleryImageGenerationJob(private val user: User, private val storageManager: FileDataStorageManager) { |
| 30 | + companion object { |
| 31 | + private const val TAG = "GalleryImageGenerationJob" |
| 32 | + private val semaphore = Semaphore( |
| 33 | + maxOf( |
| 34 | + 3, |
| 35 | + Runtime.getRuntime().availableProcessors() / 2 |
| 36 | + ) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + @Suppress("TooGenericExceptionCaught") |
| 41 | + suspend fun run( |
| 42 | + file: OCFile, |
| 43 | + imageView: ImageView, |
| 44 | + imageDimension: Pair<Int, Int>, |
| 45 | + listener: GalleryImageGenerationListener |
| 46 | + ) { |
| 47 | + try { |
| 48 | + var newImage = false |
| 49 | + |
| 50 | + if (file.remoteId == null && !file.isPreviewAvailable) { |
| 51 | + listener.onError() |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + val bitmap: Bitmap? = getBitmap(imageView, file, imageDimension, onThumbnailGeneration = { |
| 56 | + newImage = true |
| 57 | + }) |
| 58 | + |
| 59 | + if (bitmap == null) { |
| 60 | + listener.onError() |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + setThumbnail(bitmap, file, imageView, newImage, listener) |
| 65 | + } catch (e: Exception) { |
| 66 | + Log_OC.e(TAG, "gallery image generation job: ", e) |
| 67 | + withContext(Dispatchers.Main) { |
| 68 | + listener.onError() |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private suspend fun getBitmap( |
| 74 | + imageView: ImageView, |
| 75 | + file: OCFile, |
| 76 | + imageDimension: Pair<Int, Int>, |
| 77 | + onThumbnailGeneration: () -> Unit |
| 78 | + ): Bitmap? = withContext(Dispatchers.IO) { |
| 79 | + if (file.remoteId == null && !file.isPreviewAvailable) { |
| 80 | + Log_OC.w(TAG, "file has no remoteId and no preview") |
| 81 | + return@withContext null |
| 82 | + } |
| 83 | + |
| 84 | + val key = file.remoteId |
| 85 | + val cachedThumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( |
| 86 | + ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.remoteId |
| 87 | + ) |
| 88 | + if (cachedThumbnail != null && !file.isUpdateThumbnailNeeded) { |
| 89 | + Log_OC.d(TAG, "cached thumbnail is used for: ${file.fileName}") |
| 90 | + return@withContext getThumbnailFromCache(file, cachedThumbnail, key) |
| 91 | + } |
| 92 | + |
| 93 | + Log_OC.d(TAG, "generating new thumbnail for: ${file.fileName}") |
| 94 | + |
| 95 | + // only add placeholder if new thumbnail will be generated because cached image will appear so quickly |
| 96 | + withContext(Dispatchers.Main) { |
| 97 | + val placeholderDrawable = OCFileUtils.getMediaPlaceholder(file, imageDimension) |
| 98 | + imageView.setImageDrawable(placeholderDrawable) |
| 99 | + } |
| 100 | + |
| 101 | + onThumbnailGeneration() |
| 102 | + semaphore.withPermit { |
| 103 | + return@withContext getThumbnailFromServerAndAddToCache(file, cachedThumbnail) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private suspend fun setThumbnail( |
| 108 | + bitmap: Bitmap, |
| 109 | + file: OCFile, |
| 110 | + imageView: ImageView, |
| 111 | + newImage: Boolean, |
| 112 | + listener: GalleryImageGenerationListener |
| 113 | + ) = withContext(Dispatchers.Main) { |
| 114 | + val tagId = file.fileId.toString() |
| 115 | + if (imageView.tag?.toString() != tagId) return@withContext |
| 116 | + |
| 117 | + if ("image/png".equals(file.mimeType, ignoreCase = true)) { |
| 118 | + imageView.setBackgroundColor( |
| 119 | + ContextCompat.getColor( |
| 120 | + MainApp.getAppContext(), |
| 121 | + R.color.bg_default |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + if (newImage) { |
| 127 | + listener.onNewGalleryImage() |
| 128 | + } |
| 129 | + |
| 130 | + imageView.setImageBitmap(bitmap) |
| 131 | + imageView.invalidate() |
| 132 | + listener.onSuccess() |
| 133 | + } |
| 134 | + |
| 135 | + private fun getThumbnailFromCache(file: OCFile, thumbnail: Bitmap, key: String): Bitmap { |
| 136 | + var result = thumbnail |
| 137 | + if (MimeTypeUtil.isVideo(file)) { |
| 138 | + result = ThumbnailsCacheManager.addVideoOverlay(thumbnail, MainApp.getAppContext()) |
| 139 | + } |
| 140 | + |
| 141 | + if (thumbnail.allocationKilobyte() > ThumbnailsCacheManager.THUMBNAIL_SIZE_IN_KB) { |
| 142 | + result = ThumbnailsCacheManager.getScaledThumbnailAfterSave(result, key) |
| 143 | + } |
| 144 | + |
| 145 | + return result |
| 146 | + } |
| 147 | + |
| 148 | + @Suppress("DEPRECATION", "TooGenericExceptionCaught") |
| 149 | + private suspend fun getThumbnailFromServerAndAddToCache(file: OCFile, thumbnail: Bitmap?): Bitmap? { |
| 150 | + var thumbnail = thumbnail |
| 151 | + try { |
| 152 | + val client = withContext(Dispatchers.IO) { |
| 153 | + OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor( |
| 154 | + user.toOwnCloudAccount(), |
| 155 | + MainApp.getAppContext() |
| 156 | + ) |
| 157 | + } |
| 158 | + ThumbnailsCacheManager.setClient(client) |
| 159 | + thumbnail = ThumbnailsCacheManager.doResizedImageInBackground(file, storageManager) |
| 160 | + |
| 161 | + if (MimeTypeUtil.isVideo(file) && thumbnail != null) { |
| 162 | + thumbnail = ThumbnailsCacheManager.addVideoOverlay(thumbnail, MainApp.getAppContext()) |
| 163 | + } |
| 164 | + } catch (t: Throwable) { |
| 165 | + Log_OC.e(TAG, "Generation of gallery image for $file failed", t) |
| 166 | + } |
| 167 | + |
| 168 | + return thumbnail |
| 169 | + } |
| 170 | +} |
0 commit comments