diff --git a/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt b/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt index b4286a6..2ab0969 100644 --- a/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt +++ b/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt @@ -193,11 +193,17 @@ fun fitBitmap(source: Bitmap, width: Int, height: Int): Bitmap { if (source.width == width && source.height == height) { return source } + if (source.width <= 0 || source.height <= 0 || width <= 0 || height <= 0) { + Log.w("WallpaperUtil", "Invalid dimensions for fitBitmap, returning source.") + return source + } + return try { val bitmap = createBitmap(width, height, source.config ?: Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) val scale = width.toFloat() / source.width - val yOffset = (height - source.height * scale) / 2f + val scaledSourceHeight = source.height * scale + val yOffset = (height - scaledSourceHeight) / 2f val matrix = Matrix().apply { postScale(scale, scale) postTranslate(0f, yOffset) @@ -227,7 +233,7 @@ fun fillBitmap(source: Bitmap, width: Int, height: Int): Bitmap { val xOffset: Float val yOffset: Float - if (sourceAspect > targetAspect) { + if (sourceAspect >= targetAspect) { scale = height.toFloat() / source.height.toFloat() xOffset = (width - source.width * scale) / 2f yOffset = 0f @@ -238,7 +244,7 @@ fun fillBitmap(source: Bitmap, width: Int, height: Int): Bitmap { } val matrix = Matrix().apply { - setScale(scale, scale) + postScale(scale, scale) postTranslate(xOffset, yOffset) } canvas.drawBitmap(source, matrix, SharedPaintFilterAntiAlias) @@ -257,12 +263,16 @@ fun stretchBitmap(source: Bitmap, width: Int, height: Int): Bitmap { return source } return try { + val bitmap = createBitmap(width, height, source.config ?: Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) val matrix = Matrix().apply { - setScale(width.toFloat() / source.width, height.toFloat() / source.height) - } - createBitmap(width, height, source.config ?: Bitmap.Config.ARGB_8888).apply { - Canvas(this).drawBitmap(source, matrix, SharedPaintFilterAntiAlias) + setScale( + width.toFloat() / source.width, + height.toFloat() / source.height + ) } + canvas.drawBitmap(source, matrix, SharedPaintFilterAntiAlias) + bitmap } catch (e: Exception) { Log.e("WallpaperUtil", "Error stretching bitmap: $e") source diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/data/data_source/AlbumDao.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/data/data_source/AlbumDao.kt index 211bbb5..0ffebce 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/data/data_source/AlbumDao.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/data/data_source/AlbumDao.kt @@ -26,9 +26,6 @@ interface AlbumDao { @Query("SELECT * FROM album WHERE selected = 1") fun getSelectedAlbums(): Flow> - /*@Query("UPDATE album SET selected = :selected WHERE initialAlbumName = :albumName") - suspend fun updateAlbumSelection(albumName: String, selected: Boolean)*/ - @Query("UPDATE album SET selected = :selected WHERE initialAlbumName = :albumName") suspend fun updateAlbumSelection(albumName: String, selected: Boolean) diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_alarmmanager/WallpaperAlarmSchedulerImpl.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_alarmmanager/WallpaperAlarmSchedulerImpl.kt index f1b7b32..2150bb6 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_alarmmanager/WallpaperAlarmSchedulerImpl.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_alarmmanager/WallpaperAlarmSchedulerImpl.kt @@ -1,10 +1,13 @@ package com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager +import android.Manifest import android.app.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Build +import android.util.Log +import androidx.annotation.RequiresPermission import com.anthonyla.paperize.core.Type import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.HomeWallpaperService import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.LockWallpaperService @@ -37,6 +40,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor( /** * Schedules the wallpaper alarm based on the origin and changeImmediate */ + @RequiresPermission(Manifest.permission.SCHEDULE_EXACT_ALARM) override suspend fun scheduleWallpaperAlarm( wallpaperAlarmItem: WallpaperAlarmItem, origin: Int?, @@ -132,6 +136,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor( /** * Schedules the wallpaper alarm based on type and time */ + @RequiresPermission(Manifest.permission.SCHEDULE_EXACT_ALARM) private fun scheduleWallpaper(wallpaperAlarmItem: WallpaperAlarmItem, type: Type, origin: Int? = null, firstLaunch: Boolean = false, homeNextTime: String? = null, lockNextTime: String? = null) { try { val nextTime = calculateNextAlarmTime(wallpaperAlarmItem, type, firstLaunch, homeNextTime ?: "", lockNextTime ?: "") @@ -146,6 +151,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor( scheduleExactAlarm(nextTime, intent) } catch (e: Exception) { + Log.e("WallpaperAlarmScheduler", "Error scheduling wallpaper alarm: ${e.message}") cancelWallpaperAlarm() } } @@ -226,6 +232,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor( } } + @RequiresPermission(Manifest.permission.SCHEDULE_EXACT_ALARM) private fun scheduleExactAlarm(nextTime: LocalDateTime, intent: Intent) { val pendingIntent = PendingIntent.getBroadcast( context, diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/HomeWallpaperService.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/HomeWallpaperService.kt index 260822d..b02b3f1 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/HomeWallpaperService.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/HomeWallpaperService.kt @@ -582,9 +582,7 @@ class HomeWallpaperService: Service() { processBitmap(size.width, size.height, bitmap, darken, darkenPercent, scaling, blur, blurPercent, vignette, vignettePercent, grayscale, grayscalePercent)?.let { image -> if (both) setWallpaperSafely(image, WallpaperManager.FLAG_LOCK, wallpaperManager) setWallpaperSafely(image, WallpaperManager.FLAG_SYSTEM, wallpaperManager) - image.recycle() } - bitmap.recycle() context.triggerWallpaperTaskerEvent() return true }