From 0c2a686833bcb5e2490ae7f9a62bb6bc4b4a0114 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Mon, 16 Nov 2026 14:23:54 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=B0=EA=B2=BD=ED=99=94=EB=A9=B4=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EB=94=94=EC=BD=94=EB=94=A9=20?= =?UTF-8?q?=EC=8B=9C=20=EB=8B=A4=EC=9A=B4=EC=83=98=ED=94=8C=EB=A7=81(inSam?= =?UTF-8?q?pleSize)=20=EB=B0=8F=20=EB=B9=84=ED=8A=B8=EB=A7=B5=20=ED=95=B4?= =?UTF-8?q?=EC=A0=9C=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ForeGroundService, Appearances: 원본 해상도 전체 디코딩 대신 목표 화면 해상도 기준 inSampleSize 계산 후 디코딩 - WallpaperManager.setBitmap() 적용 완료 후 임시 비트맵 즉시 recycle() 처리 --- .../launcher/helpers/ForeGroundService.kt | 21 ++++++++++++++++--- .../launcher/settings/childs/Appearances.kt | 21 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt b/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt index d4222912..eb08d045 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt @@ -204,13 +204,25 @@ class WallpaperAutoChangeWorker(private val context: Context, params: WorkerPara return Result.failure() } - // 2. 필터링된 이미지 중 랜덤 선택 및 비트맵 로드 + // 2. 필터링된 이미지 중 랜덤 선택 및 다운샘플링 비트맵 로드 val randomImage = validImages.random() - val originalBitmap = BitmapFactory.decodeFile(randomImage.absolutePath) ?: return Result.failure() + val boundsOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true } + BitmapFactory.decodeFile(randomImage.absolutePath, boundsOptions) + var inSampleSize = 1 + if (boundsOptions.outHeight > minHeight || boundsOptions.outWidth > minWidth) { + val halfHeight = boundsOptions.outHeight / 2 + val halfWidth = boundsOptions.outWidth / 2 + while ((halfHeight / inSampleSize) >= minHeight && (halfWidth / inSampleSize) >= minWidth) { + inSampleSize *= 2 + } + } + val decodeOptions = BitmapFactory.Options().apply { this.inSampleSize = inSampleSize } + val originalBitmap = BitmapFactory.decodeFile(randomImage.absolutePath, decodeOptions) ?: return Result.failure() + var finalBitmap: Bitmap? = null return try { // 3. 비율 유지하며 Center Crop 처리 (기존 로직 유지) - val finalBitmap = Bitmap.createBitmap(minWidth, minHeight, Bitmap.Config.ARGB_8888) + finalBitmap = Bitmap.createBitmap(minWidth, minHeight, Bitmap.Config.ARGB_8888) val canvas = Canvas(finalBitmap) val scale = Math.max(minWidth.toFloat() / originalBitmap.width, minHeight.toFloat() / originalBitmap.height) @@ -227,6 +239,9 @@ class WallpaperAutoChangeWorker(private val context: Context, params: WorkerPara } catch (e: Exception) { e.printStackTrace() Result.retry() + } finally { + if (!originalBitmap.isRecycled) originalBitmap.recycle() + if (finalBitmap != null && !finalBitmap.isRecycled) finalBitmap.recycle() } } } diff --git a/app/src/main/kotlin/bums/lunatic/launcher/settings/childs/Appearances.kt b/app/src/main/kotlin/bums/lunatic/launcher/settings/childs/Appearances.kt index 5bedcffc..2a862ced 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/settings/childs/Appearances.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/settings/childs/Appearances.kt @@ -178,12 +178,27 @@ internal class Appearances : BottomSheetDialogFragment() { val index = cursor!!.getColumnIndex(projection[0]) val filePath = cursor.getString(index) cursor.close() - val bitmap = BitmapFactory.decodeFile(filePath) + val wm = WallpaperManager.getInstance(requireContext()) + val reqWidth = wm.desiredMinimumWidth.takeIf { it > 0 } ?: 1080 + val reqHeight = wm.desiredMinimumHeight.takeIf { it > 0 } ?: 1920 + val boundsOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true } + BitmapFactory.decodeFile(filePath, boundsOptions) + var inSampleSize = 1 + if (boundsOptions.outHeight > reqHeight || boundsOptions.outWidth > reqWidth) { + val halfHeight = boundsOptions.outHeight / 2 + val halfWidth = boundsOptions.outWidth / 2 + while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { + inSampleSize *= 2 + } + } + val bitmap = BitmapFactory.decodeFile(filePath, BitmapFactory.Options().apply { + this.inSampleSize = inSampleSize + }) val matrix = Matrix() matrix.postRotate(0F) try { if (bitmap != null) { - WallpaperManager.getInstance(requireContext()).setBitmap(bitmap) + wm.setBitmap(bitmap) Toast.makeText(requireContext(), requireActivity().getString(R.string.wallpaper_change_success), Toast.LENGTH_SHORT).show() } else { @@ -194,6 +209,8 @@ internal class Appearances : BottomSheetDialogFragment() { e.printStackTrace() Toast.makeText(requireContext(), requireActivity().getString(R.string.something_went_wrong), Toast.LENGTH_SHORT).show() + } finally { + if (bitmap != null && !bitmap.isRecycled) bitmap.recycle() } } catch (e: Exception) { e.printStackTrace()