AutoSuspend: Unbreak scheduling & settings across instances

Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.

As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.

Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.

In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
reviewable/pr7635/r1
NiLuJe 3 years ago
parent 0d6be41c75
commit b0cc940776

@ -24,10 +24,11 @@ local default_auto_suspend_timeout_seconds = 60*60
local AutoSuspend = WidgetContainer:new{
name = "autosuspend",
is_doc_only = false,
autoshutdown_timeout_seconds = G_reader_settings:readSetting("autoshutdown_timeout_seconds") or default_autoshutdown_timeout_seconds,
auto_suspend_timeout_seconds = G_reader_settings:readSetting("auto_suspend_timeout_seconds") or default_auto_suspend_timeout_seconds,
last_action_tv = TimeVal:now(),
autoshutdown_timeout_seconds = default_autoshutdown_timeout_seconds,
auto_suspend_timeout_seconds = default_auto_suspend_timeout_seconds,
last_action_tv = TimeVal.zero,
standby_prevented = false,
task = nil,
}
function AutoSuspend:_enabled()
@ -67,18 +68,20 @@ function AutoSuspend:_schedule(shutdown_only)
else
if self:_enabled() and not shutdown_only then
logger.dbg("AutoSuspend: scheduling next suspend check in", delay_suspend)
UIManager:scheduleIn(delay_suspend, self._schedule, self, shutdown_only)
UIManager:scheduleIn(delay_suspend, self.task)
end
if self:_enabledShutdown() then
logger.dbg("AutoSuspend: scheduling next shutdown check in", delay_shutdown)
UIManager:scheduleIn(delay_shutdown, self._schedule, self, shutdown_only)
UIManager:scheduleIn(delay_shutdown, self.task)
end
end
end
function AutoSuspend:_unschedule()
logger.dbg("AutoSuspend: unschedule")
UIManager:unschedule(self._schedule)
if self.task then
logger.dbg("AutoSuspend: unschedule")
UIManager:unschedule(self.task)
end
end
function AutoSuspend:_start()
@ -103,8 +106,15 @@ end
function AutoSuspend:init()
logger.dbg("AutoSuspend: init")
if Device:isPocketBook() and not Device:canSuspend() then return end
self.autoshutdown_timeout_seconds = G_reader_settings:readSetting("autoshutdown_timeout_seconds") or default_autoshutdown_timeout_seconds
self.auto_suspend_timeout_seconds = G_reader_settings:readSetting("auto_suspend_timeout_seconds") or default_auto_suspend_timeout_seconds
UIManager.event_hook:registerWidget("InputEvent", self)
self:_unschedule()
-- We need an instance-specific function reference to schedule, because when going from FM to RD,
-- we instantiate the RD instance *before* tearing down the old FM instance.
self.task = function(shutdown_only)
self:_schedule(shutdown_only)
end
self:_start()
-- self.ui is nil in the testsuite
if not self.ui or not self.ui.menu then return end
@ -116,6 +126,7 @@ function AutoSuspend:onCloseWidget()
logger.dbg("AutoSuspend: onCloseWidget")
if Device:isPocketBook() and not Device:canSuspend() then return end
self:_unschedule()
self.task = nil
end
function AutoSuspend:onInputEvent()

Loading…
Cancel
Save