diff --git a/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt b/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt index a31d59f..a875897 100644 --- a/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt +++ b/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt @@ -199,28 +199,46 @@ fun retrieveBitmap( val result = ImageDecoder.decodeBitmap(source) { decoder, info, _ -> val srcWidth = info.size.width val srcHeight = info.size.height - val (targetWidth, targetHeight) = when (scaling) { + + when (scaling) { ScalingType.FILL -> { + // Scale to fill, then crop to canvas during decode. + // setCrop avoids allocating the full fill-scale bitmap (which can be + // much taller/wider than the canvas) — major OOM prevention. val scale = maxOf(width.toFloat() / srcWidth, height.toFloat() / srcHeight) - Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt()) + val targetW = (srcWidth * scale).fastRoundToInt() + val targetH = (srcHeight * scale).fastRoundToInt() + decoder.setTargetSize(targetW, targetH) + if (targetW > width || targetH > height) { + val cropX = ((targetW - width) / 2).coerceAtLeast(0) + val cropY = ((targetH - height) / 2).coerceAtLeast(0) + decoder.setCrop(android.graphics.Rect( + cropX, cropY, cropX + width, cropY + height + )) + } } ScalingType.FIT -> { val scale = minOf(width.toFloat() / srcWidth, height.toFloat() / srcHeight) - Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt()) + decoder.setTargetSize( + (srcWidth * scale).fastRoundToInt(), + (srcHeight * scale).fastRoundToInt() + ) } - ScalingType.STRETCH -> Pair(width, height) + ScalingType.STRETCH -> decoder.setTargetSize(width, height) ScalingType.NONE -> { val maxWidth = width * 2 val maxHeight = height * 2 if (srcWidth > maxWidth || srcHeight > maxHeight) { val scale = minOf(maxWidth.toFloat() / srcWidth, maxHeight.toFloat() / srcHeight) - Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt()) - } else { - Pair(srcWidth, srcHeight) + decoder.setTargetSize( + (srcWidth * scale).fastRoundToInt(), + (srcHeight * scale).fastRoundToInt() + ) } + // else: decode at original resolution } } - decoder.setTargetSize(targetWidth, targetHeight) + decoder.isMutableRequired = true decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE } diff --git a/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt b/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt index a35fa7a..7f76de4 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt @@ -188,36 +188,34 @@ class WallpaperChangeService : Service() { Log.w(TAG, "Failed to sync LOCK queue in BOTH mode", e) } - // Render a separate bitmap for the lock screen at physical - // screen dimensions. The HOME bitmap was sized for the - // launcher's parallax canvas which is typically wider than - // the screen; reusing it for LOCK would let Android - // center-crop it, defeating FIT/NONE scaling modes. - val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK) - lockResult.onSuccess { lockBitmap -> - try { - wallpaperManager.setBitmap( - lockBitmap, null, true, WallpaperManager.FLAG_LOCK - ) - Log.d(TAG, "Lock wallpaper set separately in BOTH mode") - } catch (e: Exception) { - Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e) - } finally { - lockBitmap.recycle() - } - }.onError { - // Fallback: use the HOME bitmap for LOCK (old behavior) - Log.w(TAG, "Lock rerender failed in BOTH mode, using HOME bitmap") - wallpaperManager.setBitmap( - bitmap, null, true, WallpaperManager.FLAG_LOCK - ) - } } catch (e: Exception) { Log.e(TAG, "Error setting wallpaper for both screens", e) } finally { - // Always recycle bitmap to prevent memory leaks + // Recycle HOME bitmap BEFORE rendering LOCK to avoid + // holding both in memory (~48MB + ~16MB). bitmap.recycle() } + + // Render a separate bitmap for the lock screen at physical + // screen dimensions. The HOME bitmap was sized for the + // launcher's parallax canvas which is typically wider than + // the screen; reusing it for LOCK would let Android + // center-crop it, defeating FIT/NONE scaling modes. + val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK) + lockResult.onSuccess { lockBitmap -> + try { + wallpaperManager.setBitmap( + lockBitmap, null, true, WallpaperManager.FLAG_LOCK + ) + Log.d(TAG, "Lock wallpaper set separately in BOTH mode") + } catch (e: Exception) { + Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e) + } finally { + lockBitmap.recycle() + } + }.onError { error -> + Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}") + } }.onError { error -> if (error is EmptyAlbumException) { handleEmptyAlbumError(ScreenType.BOTH) diff --git a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt index 1029170..ea563cf 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt @@ -143,32 +143,32 @@ class WallpaperChangeWorker @AssistedInject constructor( Log.w(TAG, "Failed to sync LOCK queue in BOTH mode", e) } - // Render a separate bitmap for the lock screen at physical - // screen dimensions (no parallax on lock screen). - val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK) - lockResult.onSuccess { lockBitmap -> - try { - wallpaperManager.setBitmap( - lockBitmap, null, true, WallpaperManager.FLAG_LOCK - ) - Log.d(TAG, "Lock wallpaper set separately in BOTH mode") - } catch (e: Exception) { - Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e) - } finally { - lockBitmap.recycle() - } - }.onError { - Log.w(TAG, "Lock rerender failed in BOTH mode, using HOME bitmap") - wallpaperManager.setBitmap( - bitmap, null, true, WallpaperManager.FLAG_LOCK - ) - } } catch (e: Exception) { Log.e(TAG, "Error setting wallpaper for both screens", e) throw e } finally { + // Recycle HOME bitmap BEFORE rendering LOCK to avoid + // holding both in memory simultaneously. bitmap.recycle() } + + // Render a separate bitmap for the lock screen at physical + // screen dimensions (no parallax on lock screen). + val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK) + lockResult.onSuccess { lockBitmap -> + try { + wallpaperManager.setBitmap( + lockBitmap, null, true, WallpaperManager.FLAG_LOCK + ) + Log.d(TAG, "Lock wallpaper set separately in BOTH mode") + } catch (e: Exception) { + Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e) + } finally { + lockBitmap.recycle() + } + }.onError { error -> + Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}") + } }.onError { error -> if (error is EmptyAlbumException) { Log.w(TAG, "Album is empty for BOTH screens — disabling changer")