Make the powered off state customizable

This changes the behavior of the powered off transition phase for the
Kobo devices: instead of displaying a confirmation dialog, a screensaver
is used after a 2 seconds press delay.

Users can specify separate directories/files/messages for the powered
off and sleeping states through the `path` and `message` attributes of
the `{poweroff,suspend}_screensaver` settings.

Fixes #2327 and closes #2306.
pull/2358/head
Bastien Dejean 8 years ago committed by Qingping Hou
parent 2599c39d42
commit 64bc5cd63c

@ -1,5 +1,6 @@
local Event = require("ui/event") local Event = require("ui/event")
local DEBUG = require("dbg") local DEBUG = require("dbg")
local _ = require("gettext")
local function yes() return true end local function yes() return true end
local function no() return false end local function no() return false end
@ -137,7 +138,7 @@ function Device:onPowerEvent(ev)
-- always suspend in portrait mode -- always suspend in portrait mode
self.orig_rotation_mode = self.screen:getRotationMode() self.orig_rotation_mode = self.screen:getRotationMode()
self.screen:setRotationMode(0) self.screen:setRotationMode(0)
require("ui/screensaver"):show() require("ui/screensaver"):show("suspend", _("Sleeping"))
self.screen:refreshFull() self.screen:refreshFull()
self.screen_saver_mode = true self.screen_saver_mode = true
UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend) UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend)

@ -4,7 +4,6 @@ local Device = require("device")
local Screen = Device.screen local Screen = Device.screen
local DocSettings = require("docsettings") local DocSettings = require("docsettings")
local DEBUG = require("dbg") local DEBUG = require("dbg")
local _ = require("gettext")
local Screensaver = { local Screensaver = {
} }
@ -88,56 +87,60 @@ function Screensaver:getCoverImage(file)
end end
end end
function Screensaver:show() function Screensaver:show(kind, default_msg)
DEBUG("show screensaver") DEBUG("show screensaver")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local screensaver_settings = G_reader_settings:readSetting(kind .. "_screensaver") or {}
-- first check book cover image, on by default -- first check book cover image, on by default
local screen_saver_last_book = local screensaver_last_book =
G_reader_settings:readSetting("use_lastfile_as_screensaver") G_reader_settings:readSetting("use_lastfile_as_screensaver")
if screen_saver_last_book == nil or screen_saver_last_book then if screensaver_last_book == nil or screensaver_last_book then
local lastfile = G_reader_settings:readSetting("lastfile") local lastfile = G_reader_settings:readSetting("lastfile")
if lastfile then if lastfile then
local doc_settings = DocSettings:open(lastfile) local doc_settings = DocSettings:open(lastfile)
local exclude = doc_settings:readSetting("exclude_screensaver") local exclude = doc_settings:readSetting("exclude_screensaver")
if not exclude then if not exclude then
self.suspend_msg = self:getCoverImage(lastfile) self.left_msg = self:getCoverImage(lastfile)
end end
end end
end end
-- then screensaver directory or file image -- then screensaver directory or file image
if not self.suspend_msg then if not self.left_msg then
-- FIXME: rename this config to screen_saver_path -- FIXME: rename screensaver_folder to screensaver_path
local screen_saver_folder = local screensaver_path = screensaver_settings.path or
G_reader_settings:readSetting("screensaver_folder") G_reader_settings:readSetting("screensaver_folder")
if screen_saver_folder == nil if screensaver_path == nil
and Device.internal_storage_mount_point ~= nil then and Device.internal_storage_mount_point ~= nil then
screen_saver_folder = screensaver_path =
Device.internal_storage_mount_point .. "screensaver" Device.internal_storage_mount_point .. "screensaver"
end end
if screen_saver_folder then if screensaver_path then
local file = screen_saver_folder local mode = lfs.attributes(screensaver_path, "mode")
if lfs.attributes(file, "mode") == "directory" then if mode ~= nil then
self.suspend_msg = getRandomImage(file) if mode == "directory" then
else self.left_msg = getRandomImage(screensaver_path)
self.suspend_msg = createWidgetFromFile(file) else
self.left_msg = createWidgetFromFile(screensaver_path)
end
end end
end end
end end
-- fallback to suspended message -- fallback to message box
if not self.suspend_msg then if not self.left_msg then
self.suspend_msg = InfoMessage:new{ text = _("Suspended") } local msg = screensaver_settings.message or default_msg
UIManager:show(self.suspend_msg) self.left_msg = InfoMessage:new{ text = msg }
UIManager:show(self.left_msg)
else else
-- refresh whole screen for other types -- refresh whole screen for other types
UIManager:show(self.suspend_msg, "full") UIManager:show(self.left_msg, "full")
end end
end end
function Screensaver:close() function Screensaver:close()
DEBUG("close screensaver") DEBUG("close screensaver")
if self.suspend_msg then if self.left_msg then
UIManager:close(self.suspend_msg) UIManager:close(self.left_msg)
self.suspend_msg = nil self.left_msg = nil
end end
end end

@ -27,7 +27,7 @@ local UIManager = {
_zeromqs = {}, _zeromqs = {},
_refresh_stack = {}, _refresh_stack = {},
_refresh_func_stack = {}, _refresh_func_stack = {},
_power_ev_handled = false, _entered_poweroff_stage = false,
} }
function UIManager:init() function UIManager:init()
@ -42,6 +42,16 @@ function UIManager:init()
Device:onPowerEvent(input_event) Device:onPowerEvent(input_event)
end, end,
} }
self.poweroff_action = function()
self._entered_poweroff_stage = true;
Screen:setRotationMode(0)
require("ui/screensaver"):show("poweroff", _("Powered off"))
Screen:refreshFull()
UIManager:nextTick(function()
self:broadcastEvent(Event:new("Close"))
Device:powerOff()
end)
end
if Device:isKobo() then if Device:isKobo() then
-- We do not want auto suspend procedure to waste battery during -- We do not want auto suspend procedure to waste battery during
-- suspend. So let's unschedule it when suspending, and restart it after -- suspend. So let's unschedule it when suspending, and restart it after
@ -57,33 +67,12 @@ function UIManager:init()
self:_startAutoSuspend() self:_startAutoSuspend()
end end
self.event_handlers["PowerPress"] = function() self.event_handlers["PowerPress"] = function()
self._power_ev_handled = false UIManager:scheduleIn(2, self.poweroff_action)
local showPowerOffDialog = function()
if self._power_ev_handled then return end
self._power_ev_handled = true
local ConfirmBox = require("ui/widget/confirmbox")
UIManager:show(ConfirmBox:new{
text = _("Power off?"),
ok_callback = function()
local InfoMessage = require("ui/widget/infomessage")
UIManager:show(InfoMessage:new{
text = _("Powered off."),
})
-- The message can fail to render if this is executed directly
UIManager:scheduleIn(0.1, function()
self:broadcastEvent(Event:new("Close"))
Device:powerOff()
end)
end,
})
end
UIManager:scheduleIn(3, showPowerOffDialog)
end end
self.event_handlers["PowerRelease"] = function() self.event_handlers["PowerRelease"] = function()
if not self._power_ev_handled then if not self._entered_poweroff_stage then
self._power_ev_handled = true UIManager:unschedule(self.poweroff_action)
self.event_handlers["Suspend"]() self.event_handlers["Suspend"]()
end end
end end
if not G_reader_settings:readSetting("ignore_power_sleepcover") then if not G_reader_settings:readSetting("ignore_power_sleepcover") then

Loading…
Cancel
Save