2
0
mirror of https://github.com/koreader/koreader synced 2024-11-04 12:00:25 +00:00

AutoWarmth: Fix frontlight flash after resume and weird true midnight settings (#9730)

This commit is contained in:
zwim 2022-11-02 14:29:36 +01:00 committed by GitHub
parent 4a355f789d
commit 629304adce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,6 +82,14 @@ function AutoWarmth:init()
self.control_nightmode = true
end
-- Fix entries not in ascending order (only happens by manual editing of settings.reader.lua)
for i = 1, #self.scheduler_times - 1 do
if self.scheduler_times[i] > self.scheduler_times[i + 1] then
self.scheduler_times[i + 1] = self.scheduler_times[i]
logger.warn("AutoWarmth: scheduling times fixed.")
end
end
-- schedule recalculation shortly afer midnight
self:scheduleMidnightUpdate()
end
@ -130,7 +138,6 @@ function AutoWarmth:onAutoWarmthMode()
end
function AutoWarmth:leavePowerSavingState(from_resume)
logger.dbg("AutoWarmth: onResume/onLeaveStandby")
local resume_date = os.date("*t")
-- check if resume and suspend are done on the same day
@ -146,14 +153,17 @@ function AutoWarmth:leavePowerSavingState(from_resume)
end
function AutoWarmth:_onResume()
logger.dbg("AutoWarmth: onResume")
self:leavePowerSavingState(true)
end
function AutoWarmth:_onLeaveStandby()
logger.dbg("AutoWarmth: onLeaveStandby")
self:leavePowerSavingState(false)
end
function AutoWarmth:_onSuspend()
logger.dbg("AutoWarmth: onSuspend")
UIManager:unschedule(self.scheduleMidnightUpdate)
UIManager:unschedule(self.setWarmth)
end
@ -179,7 +189,7 @@ function AutoWarmth:scheduleMidnightUpdate(from_resume)
logger.dbg("AutoWarmth: scheduleMidnightUpdate")
-- first unschedule all old functions
UIManager:unschedule(self.scheduleMidnightUpdate)
UIManager:unschedule(AutoWarmth.setWarmth)
UIManager:unschedule(self.setWarmth)
SunTime:setPosition(self.location, self.latitude, self.longitude, self.timezone, self.altitude, true)
SunTime:setAdvanced()
@ -291,7 +301,7 @@ end
-- from_resume ... true if first call after resume
function AutoWarmth:scheduleNextWarmthChange(time_s, search_pos, from_resume)
logger.dbg("AutoWarmth: scheduleNextWarmthChange")
UIManager:unschedule(AutoWarmth.setWarmth)
UIManager:unschedule(self.setWarmth)
if self.activate == 0 or #self.sched_warmths == 0 or search_pos > #self.sched_warmths then
return
@ -309,39 +319,40 @@ function AutoWarmth:scheduleNextWarmthChange(time_s, search_pos, from_resume)
-- before true midnight. OK, this value is actually not quite the right one, as it is calculated
-- for the current day (and not the previous one), but this is for a corner case
-- and the error is small.
local actual_warmth = self.sched_warmths[self.sched_warmth_index or #self.sched_warmths]
local actual_warmth = self.sched_warmths[self.sched_warmth_index]
local next_warmth = actual_warmth
for i = self.sched_warmth_index, #self.sched_warmths do
if self.sched_times_s[i] <= time_s then
actual_warmth = self.sched_warmths[i]
local j = i
while from_resume and j <= #self.sched_warmths and self.sched_times_s[j] <= time_s + delay_s do
-- Most times only one iteration through this loop
next_warmth = self.sched_warmths[j]
j = j + 1
end
else
self.sched_warmth_index = i
-- It might be possible that actual_warmth == self.sched_warmth[#self.sched_warmths]
-- in this case next self.sched_warmth_index should be #self.sched_warmths
self.sched_warmth_index = i
if self.sched_times_s[i] > time_s then
break
end
end
-- update current warmth immediately
self:setWarmth(actual_warmth, false) -- no setWarmth rescheduling, don't force warmth
if self.sched_warmth_index <= #self.sched_warmths then
local next_sched_time_s = self.sched_times_s[self.sched_warmth_index] - time_s
if next_sched_time_s > 0 then
-- This setWarmth will call scheduleNextWarmthChange which will schedule setWarmth again.
UIManager:scheduleIn(next_sched_time_s, self.setWarmth, self, self.sched_warmths[self.sched_warmth_index], true)
elseif self.sched_warmth_index < #self.sched_warmths then
-- If this really happens under strange conditions, wait until the next full minute to
-- minimize wakeups from standby.
UIManager:scheduleIn(61 - tonumber(os.date("%S")),
self.setWarmth, self, self.sched_warmths[self.sched_warmth_index], true)
else
logger.dbg("AutoWarmth: schedule is over, waiting for midnight update")
-- update actual_warmth and next_warmth
actual_warmth = self.sched_warmths[i]
local j = i
while from_resume and j <= #self.sched_warmths and self.sched_times_s[j] <= time_s + delay_s do
-- Most times only one iteration through this loop
next_warmth = self.sched_warmths[j]
j = j + 1
end
end
-- update current warmth immediately
self:setWarmth(actual_warmth, false) -- no setWarmth rescheduling, don't force warmth
local next_sched_time_s = self.sched_times_s[self.sched_warmth_index] - time_s
if next_sched_time_s <= 0 then
-- If this really happens under strange conditions (after the last scheduler entry
-- (true midnight) and before 00:00),
-- wait until the next full minute to minimize wakeups from standby.
next_sched_time_s = 61 - tonumber(os.date("%S"))
end
-- This setWarmth will call scheduleNextWarmthChange which will schedule setWarmth again.
UIManager:scheduleIn(next_sched_time_s,
self.setWarmth, self, self.sched_warmths[self.sched_warmth_index], true)
if from_resume then
-- On some strange devices like KA1 setWarmth doesn't work right after a resume so
-- schedule setting of another valid warmth (=`next_warmth`) again (one time).
@ -729,11 +740,11 @@ function AutoWarmth:getScheduleMenu()
store_times(touchmenu_instance, new_time, num)
end,
})
elseif num < 10 and new_time > get_valid_time(num, 1) then
elseif num < midnight_index and new_time > get_valid_time(num, 1) then
UIManager:show(ConfirmBox:new{
text = _("This time is after the subsequent time.\nAdjust the subsequent time?"),
ok_callback = function()
for i = num + 1, midnight_index - 1 do
for i = num + 1, midnight_index do
if self.scheduler_times[i] then
if new_time > self.scheduler_times[i] then
self.scheduler_times[i] = new_time