From 84e93a652bb425300c7f136a6a6c344c4bba1e7d Mon Sep 17 00:00:00 2001 From: Anthony La Date: Wed, 9 Oct 2024 22:13:32 -0700 Subject: [PATCH] Changed refresh alarm to be inexact to improve battery --- .../wallpaper/presentation/MainActivity.kt | 10 ++++- .../WallpaperAlarmSchedulerImpl.kt | 39 +++++-------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt index 5eeafc5..f833623 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt @@ -115,11 +115,17 @@ class MainActivity : ComponentActivity() { lockInterval = settingsDataStoreImpl.getInt(SettingsConstants.LOCK_WALLPAPER_CHANGE_INTERVAL) ?: SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT, setLock = settingsDataStoreImpl.getBoolean(SettingsConstants.ENABLE_LOCK_WALLPAPER) ?: false, setHome = settingsDataStoreImpl.getBoolean(SettingsConstants.ENABLE_HOME_WALLPAPER) ?: false, - scheduleSeparately = shouldScheduleSeparately + scheduleSeparately = shouldScheduleSeparately, + changeStartTime = settingsDataStoreImpl.getBoolean(SettingsConstants.CHANGE_START_TIME) ?: false, + startTime = Pair( + settingsDataStoreImpl.getInt(SettingsConstants.START_HOUR) ?: 0, + settingsDataStoreImpl.getInt(SettingsConstants.START_MINUTE) ?: 0 + ), ), origin = null, changeImmediate = true, - cancelImmediate = true + cancelImmediate = true, + firstLaunch = true ) settingsViewModel.onEvent(SettingsEvent.RefreshNextSetTime) val selectedState = wallpaperScreenViewModel.state.value 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 cc38812..87a7263 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 @@ -129,7 +129,7 @@ class WallpaperAlarmSchedulerImpl ( } val nextTime = when { wallpaperAlarmItem.changeStartTime && firstLaunch -> startTime - type == Type.LOCK -> startTime.plusMinutes(wallpaperAlarmItem.lockInterval.toLong()) + type == Type.LOCK && wallpaperAlarmItem.scheduleSeparately -> startTime.plusMinutes(wallpaperAlarmItem.lockInterval.toLong()) type == Type.HOME && wallpaperAlarmItem.scheduleSeparately -> startTime.plusMinutes(wallpaperAlarmItem.homeInterval.toLong()).plusSeconds(10) else -> startTime.plusMinutes(wallpaperAlarmItem.homeInterval.toLong()) } @@ -185,35 +185,16 @@ class WallpaperAlarmSchedulerImpl ( putExtra("refresh", true) } val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - if (!alarmManager.canScheduleExactAlarms()) { - cancelWallpaperAlarm() - } - else { - alarmManager.setExactAndAllowWhileIdle( - AlarmManager.RTC_WAKEUP, - nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000, - PendingIntent.getBroadcast( - context, - Type.REFRESH.ordinal, - intent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE - ) - ) - } - } - else { - alarmManager.setExactAndAllowWhileIdle( - AlarmManager.RTC_WAKEUP, - nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000, - PendingIntent.getBroadcast( - context, - Type.REFRESH.ordinal, - intent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE - ) + alarmManager.setAndAllowWhileIdle( + AlarmManager.RTC_WAKEUP, + nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000, + PendingIntent.getBroadcast( + context, + Type.REFRESH.ordinal, + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE ) - } + ) } /**