From ce6bc3df8d7d7b663a2691e114da1ea06bc264e2 Mon Sep 17 00:00:00 2001 From: Hzj_jie Date: Tue, 27 Dec 2016 02:03:11 -0800 Subject: [PATCH] Implement keepalive plugin (#2456) --- frontend/pluginloader.lua | 4 +-- plugins/hello.koplugin/main.lua | 2 +- plugins/keepalive.koplugin/main.lua | 51 +++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 plugins/keepalive.koplugin/main.lua diff --git a/frontend/pluginloader.lua b/frontend/pluginloader.lua index 7716812fb..9457b7724 100644 --- a/frontend/pluginloader.lua +++ b/frontend/pluginloader.lua @@ -20,10 +20,10 @@ function PluginLoader:loadPlugins() package.path = path.."/?.lua;"..package.path package.cpath = path.."/lib/?.so;"..package.cpath local ok, plugin_module = pcall(dofile, mainfile) - if not ok then + if not ok or not plugin_module then DEBUG("Error when loading", mainfile, plugin_module) end - if not plugin_module.disabled and ok then + if ok and plugin_module and not plugin_module.disabled then package.path = package_path package.cpath = package_cpath plugin_module.path = path diff --git a/plugins/hello.koplugin/main.lua b/plugins/hello.koplugin/main.lua index 690e8061b..aeb5426f0 100644 --- a/plugins/hello.koplugin/main.lua +++ b/plugins/hello.koplugin/main.lua @@ -16,7 +16,7 @@ end function Hello:addToMainMenu(tab_item_table) table.insert(tab_item_table.plugins, { text = _("Hello World"), - callback_func = function() + callback = function() UIManager:show(InfoMessage:new{ text = _("Hello, docless plugin world"), }) diff --git a/plugins/keepalive.koplugin/main.lua b/plugins/keepalive.koplugin/main.lua new file mode 100644 index 000000000..22d5bd808 --- /dev/null +++ b/plugins/keepalive.koplugin/main.lua @@ -0,0 +1,51 @@ + +local ConfirmBox = require("ui/widget/confirmbox") +local Device = require("device") +local UIManager = require("ui/uimanager") +local WidgetContainer = require("ui/widget/container/widgetcontainer") +local _ = require("gettext") + +local function showConfirmBox(disable) + UIManager:show(ConfirmBox:new{ + text = _("The system won't sleep when this message is showing.\nPress \"Stay Alive\" if you prefer to keep system on even after closing this notification. *It will drain the battery.*\n\nIf for any reasons KOReader died before \"Close\" is pressed, please start and close KeepAlive plugin again to ensure settings are reset."), + ok_text = _("Close"), + ok_callback = disable, + cancel_text = _("Stay Alive"), + }) +end + +local menuItem = { + text = _("Keep Alive"), +} + +if Device:isKobo() then + disable = function() UIManager:_startAutoSuspend() end + menuItem.callback = function() + UIManager:_stopAutoSuspend() + showConfirmBox(disable) + end +elseif Device:isKindle() then + disable = function() + os.execute("lipc-set-prop com.lab126.powerd preventScreenSaver 0") + end + menuItem.callback = function() + os.execute("lipc-set-prop com.lab126.powerd preventScreenSaver 1") + showConfirmBox(disable) + end +else + return { disabled = true, } +end + +local KeepAlive = WidgetContainer:new{ + name = "keepalive", +} + +function KeepAlive:init() + self.ui.menu:registerToMainMenu(self) +end + +function KeepAlive:addToMainMenu(tab_item_table) + table.insert(tab_item_table.plugins, menuItem) +end + +return KeepAlive