diff options
| author | 2023-05-18 00:01:15 +0000 | |
|---|---|---|
| committer | 2023-05-18 00:01:15 +0000 | |
| commit | fae8729f934bf68056685ac01b13bb1e89c25ff5 (patch) | |
| tree | 095558408fab031bb00af7b0c807564e7cc99945 /java/src | |
| parent | db9915fcda5211fcede89712be72848ed4c503bf (diff) | |
| parent | 9f16294a877aad18fa237d856bc5b9fa5085d7ce (diff) | |
Merge "Limit the number of parallel preview loadings in the image preview loader." into udc-dev am: 9f16294a87
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/IntentResolver/+/23268599
Change-Id: I71e06ee364773417456c3d099b5c96406dd126e7
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'java/src')
| -rw-r--r-- | java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt b/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt index 89b79a0a..22dd1125 100644 --- a/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt +++ b/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt @@ -26,28 +26,42 @@ import androidx.annotation.VisibleForTesting import androidx.collection.LruCache import androidx.lifecycle.Lifecycle import androidx.lifecycle.coroutineScope +import java.util.function.Consumer import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Deferred import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -import java.util.function.Consumer +import kotlinx.coroutines.sync.Semaphore private const val TAG = "ImagePreviewImageLoader" /** - * Implements preview image loading for the content preview UI. Provides requests deduplication and - * image caching. + * Implements preview image loading for the content preview UI. Provides requests deduplication, + * image caching, and a limit on the number of parallel loadings. */ @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) -class ImagePreviewImageLoader( +class ImagePreviewImageLoader +@VisibleForTesting +constructor( private val scope: CoroutineScope, thumbnailSize: Int, private val contentResolver: ContentResolver, cacheSize: Int, + // TODO: consider providing a scope with the dispatcher configured with + // [CoroutineDispatcher#limitedParallelism] instead + private val contentResolverSemaphore: Semaphore, ) : ImageLoader { + constructor( + scope: CoroutineScope, + thumbnailSize: Int, + contentResolver: ContentResolver, + cacheSize: Int, + maxSimultaneousRequests: Int = 4 + ) : this(scope, thumbnailSize, contentResolver, cacheSize, Semaphore(maxSimultaneousRequests)) + private val thumbnailSize: Size = Size(thumbnailSize, thumbnailSize) private val lock = Any() @@ -103,13 +117,16 @@ class ImagePreviewImageLoader( } } - private fun RequestRecord.loadBitmap() { + private suspend fun RequestRecord.loadBitmap() { + contentResolverSemaphore.acquire() val bitmap = try { contentResolver.loadThumbnail(uri, thumbnailSize, null) } catch (t: Throwable) { Log.d(TAG, "failed to load $uri preview", t) null + } finally { + contentResolverSemaphore.release() } complete(bitmap) } @@ -136,4 +153,4 @@ class ImagePreviewImageLoader( val deferred: CompletableDeferred<Bitmap?>, @GuardedBy("lock") var caching: Boolean ) -}
\ No newline at end of file +} |