mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
uimanager(refactor): replace autosuspend if check with noop
This commit is contained in:
@@ -8,4 +8,4 @@ make all
|
||||
retry_cmd 6 make testfront
|
||||
set +o pipefail
|
||||
luajit $(which luacheck) --no-color -q frontend | tee ./luacheck.out
|
||||
test $(grep Total ./luacheck.out | awk '{print $2}') -le 63
|
||||
test $(grep Total ./luacheck.out | awk '{print $2}') -le 59
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
unused_args = false
|
||||
std = "luajit"
|
||||
-- ignore implicit self
|
||||
self = false
|
||||
|
||||
globals = {
|
||||
"G_reader_settings",
|
||||
|
||||
9
Makefile
9
Makefile
@@ -340,9 +340,12 @@ po:
|
||||
$(MAKE) -i -C l10n bootstrap pull
|
||||
|
||||
static-check:
|
||||
@if which luacheck > /dev/null; then luacheck -q frontend; else \
|
||||
echo "[!] luacheck not found. "\
|
||||
"you can install it with 'luarocks install luacheck'"; fi
|
||||
@if which luacheck > /dev/null; then \
|
||||
luacheck -q frontend; \
|
||||
else \
|
||||
echo "[!] luacheck not found. "\
|
||||
"you can install it with 'luarocks install luacheck'"; \
|
||||
fi
|
||||
|
||||
doc:
|
||||
make -C doc
|
||||
|
||||
@@ -7,6 +7,7 @@ local util = require("ffi/util")
|
||||
local dbg = require("dbg")
|
||||
local _ = require("gettext")
|
||||
|
||||
local noop = function() end
|
||||
local MILLION = 1000000
|
||||
|
||||
-- there is only one instance of this
|
||||
@@ -46,17 +47,13 @@ function UIManager:init()
|
||||
-- resume.
|
||||
self:_initAutoSuspend()
|
||||
self.event_handlers["Suspend"] = function(input_event)
|
||||
if self:_autoSuspendEnabled() then
|
||||
self:unschedule(self.auto_suspend_action)
|
||||
end
|
||||
self:_stopAutoSuspend()
|
||||
Device:onPowerEvent(input_event)
|
||||
end
|
||||
self.event_handlers["Resume"] = function(input_event)
|
||||
Device:onPowerEvent(input_event)
|
||||
self:sendEvent(Event:new("Resume"))
|
||||
if self:_autoSuspendEnabled() then
|
||||
self:_startAutoSuspend()
|
||||
end
|
||||
self:_startAutoSuspend()
|
||||
end
|
||||
self.event_handlers["Light"] = function()
|
||||
Device:getPowerDevice():toggleFrontlight()
|
||||
@@ -585,9 +582,7 @@ function UIManager:handleInput()
|
||||
|
||||
-- delegate input_event to handler
|
||||
if input_event then
|
||||
if self:_autoSuspendEnabled() then
|
||||
self.last_action_sec = util.gettime()
|
||||
end
|
||||
self:_resetAutoSuspendTimer()
|
||||
local handler = self.event_handlers[input_event]
|
||||
if handler then
|
||||
handler(input_event)
|
||||
@@ -647,6 +642,10 @@ end
|
||||
|
||||
-- Kobo does not have an auto suspend function, so we implement it ourselves.
|
||||
function UIManager:_initAutoSuspend()
|
||||
local function isAutoSuspendEnabled()
|
||||
return Device:isKobo() and self.auto_suspend_sec > 0
|
||||
end
|
||||
|
||||
local sec = G_reader_settings:readSetting("auto_suspend_timeout_seconds")
|
||||
if sec then
|
||||
self.auto_suspend_sec = sec
|
||||
@@ -654,7 +653,8 @@ function UIManager:_initAutoSuspend()
|
||||
-- default setting is 60 minutes
|
||||
self.auto_suspend_sec = 60 * 60
|
||||
end
|
||||
if self:_autoSuspendEnabled() then
|
||||
|
||||
if isAutoSuspendEnabled() then
|
||||
self.auto_suspend_action = function()
|
||||
local now = util.gettime()
|
||||
-- Do not repeat auto suspend procedure after suspend.
|
||||
@@ -666,23 +666,32 @@ function UIManager:_initAutoSuspend()
|
||||
self.auto_suspend_action)
|
||||
end
|
||||
end
|
||||
|
||||
function UIManager:_startAutoSuspend()
|
||||
self.last_action_sec = util.gettime()
|
||||
self:nextTick(self.auto_suspend_action)
|
||||
end
|
||||
dbg:guard(UIManager, '_startAutoSuspend',
|
||||
function()
|
||||
assert(isAutoSuspendEnabled())
|
||||
end)
|
||||
|
||||
function UIManager:_stopAutoSuspend()
|
||||
self:unschedule(self.auto_suspend_action)
|
||||
end
|
||||
|
||||
function UIManager:_resetAutoSuspendTimer()
|
||||
self.last_action_sec = util.gettime()
|
||||
end
|
||||
|
||||
self:_startAutoSuspend()
|
||||
else
|
||||
self._startAutoSuspend = noop
|
||||
self._stopAutoSuspend = noop
|
||||
end
|
||||
end
|
||||
|
||||
function UIManager:_startAutoSuspend()
|
||||
self.last_action_sec = util.gettime()
|
||||
self:nextTick(self.auto_suspend_action)
|
||||
end
|
||||
dbg:guard(UIManager, '_startAutoSuspend',
|
||||
function(self)
|
||||
assert(self:_autoSuspendEnabled())
|
||||
end)
|
||||
|
||||
function UIManager:_autoSuspendEnabled()
|
||||
return Device:isKobo() and self.auto_suspend_sec > 0
|
||||
end
|
||||
UIManager._resetAutoSuspendTimer = noop
|
||||
|
||||
UIManager:init()
|
||||
return UIManager
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
require("commonrequire")
|
||||
local util = require("ffi/util")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local DEBUG = require("dbg")
|
||||
DEBUG:turnOn()
|
||||
|
||||
describe("UIManager spec", function()
|
||||
local Device, UIManager, util
|
||||
local noop = function() end
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
util = require("ffi/util")
|
||||
UIManager = require("ui/uimanager")
|
||||
Device = require("device")
|
||||
end)
|
||||
|
||||
it("should consume due tasks", function()
|
||||
local now = { util.gettime() }
|
||||
local future = { now[1] + 60000, now[2] }
|
||||
@@ -161,4 +163,32 @@ describe("UIManager spec", function()
|
||||
UIManager:_checkTasks()
|
||||
assert.is_true(UIManager._task_queue_dirty)
|
||||
end)
|
||||
|
||||
it("should setup auto suspend on kobo", function()
|
||||
local old_reset_timer = UIManager._resetAutoSuspendTimer
|
||||
local noop = old_reset_timer
|
||||
assert.falsy(UIManager._startAutoSuspend)
|
||||
assert.falsy(UIManager._stopAutoSuspend)
|
||||
assert.truthy(old_reset_timer)
|
||||
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 3600)
|
||||
|
||||
UIManager:quit()
|
||||
-- should skip on non-kobo devices
|
||||
UIManager:_initAutoSuspend()
|
||||
assert.is.same(noop, UIManager._startAutoSuspend)
|
||||
assert.is.same(noop, UIManager._stopAutoSuspend)
|
||||
assert.truthy(old_reset_timer)
|
||||
assert.is.same(#UIManager._task_queue, 0)
|
||||
-- now test kobo devices
|
||||
local old_is_kobo = Device.isKobo
|
||||
Device.isKobo = function() return true end
|
||||
UIManager:_initAutoSuspend()
|
||||
assert.truthy(UIManager._startAutoSuspend)
|
||||
assert.truthy(UIManager._stopAutoSuspend)
|
||||
assert.is_not.same(UIManager._resetAutoSuspendTimer, old_reset_timer)
|
||||
assert.is.same(#UIManager._task_queue, 1)
|
||||
assert.is.same(UIManager._task_queue[1].action,
|
||||
UIManager.auto_suspend_action)
|
||||
Device.isKobo = old_is_kobo
|
||||
end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user