Skip to content

Commit

Permalink
Changed refresh alarm to be inexact to improve battery
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed Oct 10, 2024
1 parent 23a9f10 commit 84e93a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
)
}
)
}

/**
Expand Down

0 comments on commit 84e93a6

Please sign in to comment.