2014-08-27 03:07:25 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2014-08-26 10:10:26 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-04-18 05:30:46 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = Device.screen
|
2014-09-17 10:24:33 +00:00
|
|
|
local DocSettings = require("docsettings")
|
2014-08-26 10:10:26 +00:00
|
|
|
local DEBUG = require("dbg")
|
|
|
|
local _ = require("gettext")
|
2014-08-22 07:05:00 +00:00
|
|
|
|
2014-08-26 10:10:26 +00:00
|
|
|
local Screensaver = {
|
2014-08-22 07:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 05:32:51 +00:00
|
|
|
local function createWidgetFromImage(image_widget)
|
|
|
|
if image_widget then
|
2016-04-15 20:21:39 +00:00
|
|
|
local AlphaContainer = require("ui/widget/container/alphacontainer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
return AlphaContainer:new{
|
|
|
|
alpha = 1,
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Screen:getSize(),
|
2016-04-18 05:32:51 +00:00
|
|
|
image_widget,
|
2014-08-27 03:07:25 +00:00
|
|
|
}
|
2016-04-15 20:21:39 +00:00
|
|
|
}
|
2014-08-22 07:05:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 20:21:39 +00:00
|
|
|
local function createWidgetFromFile(file)
|
2016-04-15 00:42:54 +00:00
|
|
|
if lfs.attributes(file, "mode") == "file" then
|
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
2016-04-15 20:21:39 +00:00
|
|
|
return createWidgetFromImage(
|
|
|
|
ImageWidget:new{
|
|
|
|
file = file,
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
autostretch = true,
|
|
|
|
})
|
2016-04-15 00:42:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function getRandomImage(dir)
|
|
|
|
if string.sub(dir, string.len(dir)) ~= "/" then
|
|
|
|
dir = dir .. "/"
|
|
|
|
end
|
2014-08-22 07:05:00 +00:00
|
|
|
local pics = {}
|
|
|
|
local i = 0
|
2014-08-26 10:10:26 +00:00
|
|
|
math.randomseed(os.time())
|
2014-08-22 07:05:00 +00:00
|
|
|
for entry in lfs.dir(dir) do
|
|
|
|
if lfs.attributes(dir .. entry, "mode") == "file" then
|
2016-04-15 20:21:39 +00:00
|
|
|
local extension =
|
|
|
|
string.lower(string.match(entry, ".+%.([^.]+)") or "")
|
|
|
|
if extension == "jpg"
|
|
|
|
or extension == "jpeg"
|
|
|
|
or extension == "png" then
|
2014-08-22 07:05:00 +00:00
|
|
|
i = i + 1
|
|
|
|
pics[i] = entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-15 20:21:39 +00:00
|
|
|
return createWidgetFromFile(dir .. pics[math.random(i)])
|
|
|
|
end
|
|
|
|
|
2016-08-01 01:26:26 +00:00
|
|
|
function Screensaver:isUsingBookCover()
|
|
|
|
-- this setting is on by default
|
|
|
|
return G_reader_settings:readSetting('use_lastfile_as_screensaver') ~= false
|
|
|
|
end
|
|
|
|
|
2016-04-15 20:21:39 +00:00
|
|
|
function Screensaver:getCoverImage(file)
|
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
|
|
|
local doc = DocumentRegistry:openDocument(file)
|
2016-08-01 01:26:26 +00:00
|
|
|
if not doc then return end
|
|
|
|
|
|
|
|
local image = doc:getCoverPageImage()
|
|
|
|
doc:close()
|
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
|
|
|
if image then
|
|
|
|
local img_widget = ImageWidget:new{
|
|
|
|
image = image,
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
autostretch = doc_settings:readSetting("proportional_screensaver"),
|
|
|
|
}
|
|
|
|
return createWidgetFromImage(img_widget)
|
2014-08-27 03:07:25 +00:00
|
|
|
end
|
2014-08-22 07:05:00 +00:00
|
|
|
end
|
|
|
|
|
2014-08-26 10:10:26 +00:00
|
|
|
function Screensaver:show()
|
|
|
|
DEBUG("show screensaver")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2016-08-01 01:26:26 +00:00
|
|
|
-- first check book cover image, on by default
|
2016-04-19 07:59:02 +00:00
|
|
|
local screen_saver_last_book =
|
2016-04-18 05:30:46 +00:00
|
|
|
G_reader_settings:readSetting("use_lastfile_as_screensaver")
|
|
|
|
if screen_saver_last_book == nil or screen_saver_last_book then
|
2014-08-27 03:07:25 +00:00
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
2014-11-05 06:02:12 +00:00
|
|
|
if lastfile then
|
2016-08-01 01:26:26 +00:00
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
|
|
|
local exclude = doc_settings:readSetting("exclude_screensaver")
|
2014-11-05 06:02:12 +00:00
|
|
|
if not exclude then
|
|
|
|
self.suspend_msg = self:getCoverImage(lastfile)
|
|
|
|
end
|
2014-09-17 10:24:33 +00:00
|
|
|
end
|
2014-08-29 20:10:20 +00:00
|
|
|
end
|
|
|
|
-- then screensaver directory or file image
|
|
|
|
if not self.suspend_msg then
|
2016-08-01 01:26:26 +00:00
|
|
|
-- FIXME: rename this config to screen_saver_path
|
2016-04-18 05:30:46 +00:00
|
|
|
local screen_saver_folder =
|
|
|
|
G_reader_settings:readSetting("screensaver_folder")
|
|
|
|
if screen_saver_folder == nil
|
|
|
|
and Device.internal_storage_mount_point ~= nil then
|
|
|
|
screen_saver_folder =
|
|
|
|
Device.internal_storage_mount_point .. "screensaver"
|
|
|
|
end
|
|
|
|
if screen_saver_folder then
|
|
|
|
local file = screen_saver_folder
|
2014-08-29 20:10:20 +00:00
|
|
|
if lfs.attributes(file, "mode") == "directory" then
|
2016-04-15 00:42:54 +00:00
|
|
|
self.suspend_msg = getRandomImage(file)
|
|
|
|
else
|
2016-04-15 20:21:39 +00:00
|
|
|
self.suspend_msg = createWidgetFromFile(file)
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- fallback to suspended message
|
|
|
|
if not self.suspend_msg then
|
|
|
|
self.suspend_msg = InfoMessage:new{ text = _("Suspended") }
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:show(self.suspend_msg)
|
|
|
|
else
|
|
|
|
-- refresh whole screen for other types
|
|
|
|
UIManager:show(self.suspend_msg, "full")
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screensaver:close()
|
|
|
|
DEBUG("close screensaver")
|
|
|
|
if self.suspend_msg then
|
|
|
|
UIManager:close(self.suspend_msg)
|
|
|
|
self.suspend_msg = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Screensaver
|