uimanager(refactor): replace autosuspend if check with noop

pull/1930/head
Qingping Hou 8 years ago
parent e3137134f8
commit 144fd170f1

@ -8,4 +8,4 @@ make all
retry_cmd 6 make testfront retry_cmd 6 make testfront
set +o pipefail set +o pipefail
luajit $(which luacheck) --no-color -q frontend | tee ./luacheck.out 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 unused_args = false
std = "luajit" std = "luajit"
-- ignore implicit self
self = false
globals = { globals = {
"G_reader_settings", "G_reader_settings",

@ -340,9 +340,12 @@ po:
$(MAKE) -i -C l10n bootstrap pull $(MAKE) -i -C l10n bootstrap pull
static-check: static-check:
@if which luacheck > /dev/null; then luacheck -q frontend; else \ @if which luacheck > /dev/null; then \
echo "[!] luacheck not found. "\ luacheck -q frontend; \
"you can install it with 'luarocks install luacheck'"; fi else \
echo "[!] luacheck not found. "\
"you can install it with 'luarocks install luacheck'"; \
fi
doc: doc:
make -C doc make -C doc

@ -7,6 +7,7 @@ local util = require("ffi/util")
local dbg = require("dbg") local dbg = require("dbg")
local _ = require("gettext") local _ = require("gettext")
local noop = function() end
local MILLION = 1000000 local MILLION = 1000000
-- there is only one instance of this -- there is only one instance of this
@ -46,17 +47,13 @@ function UIManager:init()
-- resume. -- resume.
self:_initAutoSuspend() self:_initAutoSuspend()
self.event_handlers["Suspend"] = function(input_event) self.event_handlers["Suspend"] = function(input_event)
if self:_autoSuspendEnabled() then self:_stopAutoSuspend()
self:unschedule(self.auto_suspend_action)
end
Device:onPowerEvent(input_event) Device:onPowerEvent(input_event)
end end
self.event_handlers["Resume"] = function(input_event) self.event_handlers["Resume"] = function(input_event)
Device:onPowerEvent(input_event) Device:onPowerEvent(input_event)
self:sendEvent(Event:new("Resume")) self:sendEvent(Event:new("Resume"))
if self:_autoSuspendEnabled() then self:_startAutoSuspend()
self:_startAutoSuspend()
end
end end
self.event_handlers["Light"] = function() self.event_handlers["Light"] = function()
Device:getPowerDevice():toggleFrontlight() Device:getPowerDevice():toggleFrontlight()
@ -585,9 +582,7 @@ function UIManager:handleInput()
-- delegate input_event to handler -- delegate input_event to handler
if input_event then if input_event then
if self:_autoSuspendEnabled() then self:_resetAutoSuspendTimer()
self.last_action_sec = util.gettime()
end
local handler = self.event_handlers[input_event] local handler = self.event_handlers[input_event]
if handler then if handler then
handler(input_event) handler(input_event)
@ -647,6 +642,10 @@ end
-- Kobo does not have an auto suspend function, so we implement it ourselves. -- Kobo does not have an auto suspend function, so we implement it ourselves.
function UIManager:_initAutoSuspend() 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") local sec = G_reader_settings:readSetting("auto_suspend_timeout_seconds")
if sec then if sec then
self.auto_suspend_sec = sec self.auto_suspend_sec = sec
@ -654,7 +653,8 @@ function UIManager:_initAutoSuspend()
-- default setting is 60 minutes -- default setting is 60 minutes
self.auto_suspend_sec = 60 * 60 self.auto_suspend_sec = 60 * 60
end end
if self:_autoSuspendEnabled() then
if isAutoSuspendEnabled() then
self.auto_suspend_action = function() self.auto_suspend_action = function()
local now = util.gettime() local now = util.gettime()
-- Do not repeat auto suspend procedure after suspend. -- Do not repeat auto suspend procedure after suspend.
@ -666,23 +666,32 @@ function UIManager:_initAutoSuspend()
self.auto_suspend_action) self.auto_suspend_action)
end end
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() self:_startAutoSuspend()
else
self._startAutoSuspend = noop
self._stopAutoSuspend = noop
end end
end end
function UIManager:_startAutoSuspend() UIManager._resetAutoSuspendTimer = noop
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:init() UIManager:init()
return UIManager 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() describe("UIManager spec", function()
local Device, UIManager, util
local noop = function() end 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() it("should consume due tasks", function()
local now = { util.gettime() } local now = { util.gettime() }
local future = { now[1] + 60000, now[2] } local future = { now[1] + 60000, now[2] }
@ -161,4 +163,32 @@ describe("UIManager spec", function()
UIManager:_checkTasks() UIManager:_checkTasks()
assert.is_true(UIManager._task_queue_dirty) assert.is_true(UIManager._task_queue_dirty)
end) 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) end)

Loading…
Cancel
Save