|
| 1 | +package com.flipcash.app.core.share |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.net.Uri |
| 6 | +import coil3.SingletonImageLoader |
| 7 | +import coil3.request.ImageRequest |
| 8 | +import coil3.request.SuccessResult |
| 9 | +import coil3.request.allowHardware |
| 10 | +import coil3.toBitmap |
| 11 | +import kotlinx.coroutines.withTimeoutOrNull |
| 12 | +import java.io.File |
| 13 | +import java.security.MessageDigest |
| 14 | +import kotlin.time.Duration.Companion.milliseconds |
| 15 | + |
| 16 | +/** |
| 17 | + * Puts a remote image on disk as a `content://` URI the Sharesheet can read, so a share can carry a |
| 18 | + * thumbnail of something the app only holds as a URL. |
| 19 | + * |
| 20 | + * The Sharesheet draws its preview from the intent's ClipData, and ClipData needs a URI a *different* |
| 21 | + * process can open — a Coil-cached bitmap in this process is not one. So the image is loaded through |
| 22 | + * the app's shared loader (cache-first, keyed on the durable blob id so it hits the rendition the |
| 23 | + * screen already downloaded) and written into the same `share_previews` cache the tip-code previews |
| 24 | + * use, which gets the FileProvider path and the pruning for free. |
| 25 | + * |
| 26 | + * Everything here is best-effort: a slow network, an expired download URL, or a full disk all return |
| 27 | + * null, and a share with no thumbnail is the same share. |
| 28 | + */ |
| 29 | +object SharePreviewImage { |
| 30 | + |
| 31 | + /** |
| 32 | + * Longest side to request. The Sharesheet's thumbnail slot is small, and the server ships a 320 |
| 33 | + * rendition — asking for more downloads a bigger image to draw it at the same size. |
| 34 | + */ |
| 35 | + const val TARGET_PX = 320 |
| 36 | + |
| 37 | + /** How long a share is willing to wait on the image before going without one. */ |
| 38 | + private const val FETCH_TIMEOUT_MS = 3_000L |
| 39 | + |
| 40 | + /** |
| 41 | + * Caches the image at [url], returning the URI to hand to `ClipData`, or null if it could not be |
| 42 | + * fetched or written. |
| 43 | + * |
| 44 | + * [cacheKey] is the rendition's stable identity ([com.flipcash.services.models.chat.MediaItem]'s |
| 45 | + * `cacheKeyForSize`), used both to hit Coil's caches and to name the file — the download URL is |
| 46 | + * re-minted on every fetch, so it names nothing stable. |
| 47 | + */ |
| 48 | + suspend fun cache(context: Context, url: String, cacheKey: String? = null): Uri? { |
| 49 | + val file = File(dir(context), "${signature(cacheKey ?: url)}.png") |
| 50 | + // Already written by an earlier share of the same rendition. Touch it so the pruner treats |
| 51 | + // it as recently used rather than as the oldest thing in the directory. |
| 52 | + if (file.exists() && file.length() > 0) { |
| 53 | + file.setLastModified(System.currentTimeMillis()) |
| 54 | + return uriFor(context, file) |
| 55 | + } |
| 56 | + |
| 57 | + val bitmap = load(context, url, cacheKey) ?: return null |
| 58 | + return runCatching { |
| 59 | + file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) } |
| 60 | + uriFor(context, file) |
| 61 | + }.getOrNull() |
| 62 | + } |
| 63 | + |
| 64 | + private suspend fun load(context: Context, url: String, cacheKey: String?): Bitmap? = |
| 65 | + withTimeoutOrNull(FETCH_TIMEOUT_MS.milliseconds) { |
| 66 | + runCatching { |
| 67 | + val request = ImageRequest.Builder(context) |
| 68 | + .data(url) |
| 69 | + .apply { cacheKey?.let { memoryCacheKey(it); diskCacheKey(it) } } |
| 70 | + // compress() cannot read a hardware bitmap. |
| 71 | + .allowHardware(false) |
| 72 | + .build() |
| 73 | + (SingletonImageLoader.get(context).execute(request) as? SuccessResult) |
| 74 | + ?.image?.toBitmap() |
| 75 | + }.getOrNull() |
| 76 | + } |
| 77 | + |
| 78 | + /** Shares the tip-code previews' directory, and with it their FileProvider path and pruning. */ |
| 79 | + private fun dir(context: Context): File = TipCodePreviewStorage.dir(context) |
| 80 | + |
| 81 | + private fun uriFor(context: Context, file: File): Uri = |
| 82 | + TipCodePreviewStorage.uriFor(context, file) |
| 83 | + |
| 84 | + private fun signature(of: String): String = |
| 85 | + MessageDigest.getInstance("SHA-256") |
| 86 | + .digest(of.toByteArray()) |
| 87 | + .take(8) |
| 88 | + .joinToString("") { "%02x".format(it) } |
| 89 | +} |
0 commit comments