photos: extract WallpaperViewModel bitmap decoder seam
- Commit
910f11fdf58b34ac247f5bdf47ac8ed78a2bcf09- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 13:15 UTC (29 days ago)
- Parents
04d6809
Behavior-preserving testability seam: WallpaperViewModel.load decoded inline via ImageDecoder; that logic now lives in decodeWithImageDecoder behind an internal bitmapDecoder property defaulting to it, so tests can substitute a decoder (Robolectric's ImageDecoder shadow NPEs). No behavior change: the default path is byte-for-byte the previous decode, including the 4096px cap and software allocator. (cherry picked from commit 363ee3e65fc8d8bffeb0fc0e0b931e811280e592)
Diff
photos/src/main/java/com/vayunmathur/photos/util/WallpaperHelper.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/WallpaperHelper.kt b/photos/src/main/java/com/vayunmathur/photos/util/WallpaperHelper.ktindex 6a4bfa36d7..c2f8d23aee 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/WallpaperHelper.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/WallpaperHelper.kt@@ -39,6 +39,14 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio private val _loadState = MutableStateFlow(WallpaperLoadState.Idle) val loadState: StateFlow<WallpaperLoadState> = _loadState.asStateFlow() + /**+ * Test seam: decodes [uri] into a bitmap. Production uses [ImageDecoder]+ * (see [decodeWithImageDecoder]); Robolectric's ImageDecoder shadow is+ * broken, so tests substitute a decoder here. Behavior-preserving: the+ * default is exactly the previous inline decode.+ */+ internal var bitmapDecoder: (Context, android.net.Uri) -> Bitmap = ::decodeWithImageDecoder+ fun load(uriString: String) { if (_loadState.value == WallpaperLoadState.Loading) return _loadState.value = WallpaperLoadState.Loading@@ -46,19 +54,7 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio viewModelScope.launch(Dispatchers.IO) { val prev = _bitmap.value try {- val uri = uriString.toUri()- val source = ImageDecoder.createSource(ctx.contentResolver, uri)- val bmp = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->- val w = info.size.width- val h = info.size.height- // Cap at 4096 on the longest edge to avoid OOM while keeping quality- val maxEdge = 4096- if (w > maxEdge || h > maxEdge) {- val scale = maxEdge.toFloat() / max(w, h)- decoder.setTargetSize((w * scale).roundToInt(), (h * scale).roundToInt())- }- decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE- }+ val bmp = bitmapDecoder(ctx, uriString.toUri()) val argb = if (bmp.config == Bitmap.Config.ARGB_8888) bmp else bmp.copy(Bitmap.Config.ARGB_8888, true).also { if (bmp !== it) runCatching { bmp.recycle() }@@ -83,6 +79,21 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio _bitmap.value = null } + private fun decodeWithImageDecoder(context: Context, uri: android.net.Uri): Bitmap {+ val source = ImageDecoder.createSource(context.contentResolver, uri)+ return ImageDecoder.decodeBitmap(source) { decoder, info, _ ->+ val w = info.size.width+ val h = info.size.height+ // Cap at 4096 on the longest edge to avoid OOM while keeping quality+ val maxEdge = 4096+ if (w > maxEdge || h > maxEdge) {+ val scale = maxEdge.toFloat() / max(w, h)+ decoder.setTargetSize((w * scale).roundToInt(), (h * scale).roundToInt())+ }+ decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE+ }+ }+ companion object { private const val TAG = "WallpaperViewModel" }