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()