mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
[feat] Add NetworkManager:beforeWifiAction() to enable optional auto-connect (#3482)
This commit is contained in:
@@ -4,8 +4,9 @@ local Device = require("device")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local LuaSettings = require("luasettings")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local ffiutil = require("ffi/util")
|
||||
local _ = require("gettext")
|
||||
local T = require("ffi/util").template
|
||||
local T = ffiutil.template
|
||||
|
||||
local NetworkMgr = {}
|
||||
|
||||
@@ -60,6 +61,34 @@ function NetworkMgr:promptWifiOff(complete_callback)
|
||||
})
|
||||
end
|
||||
|
||||
function NetworkMgr:turnOnWifiAndWaitForConnection(callback)
|
||||
NetworkMgr:turnOnWifi()
|
||||
local timeout = 30
|
||||
local retry_count = 0
|
||||
local info = InfoMessage:new{ text = T(_("Enabling Wi-Fi. Waiting for Internet connection…\nTimeout %1 seconds."), timeout)}
|
||||
UIManager:show(info)
|
||||
UIManager:forceRePaint()
|
||||
while not NetworkMgr:isOnline() and retry_count < timeout do
|
||||
ffiutil.sleep(1)
|
||||
retry_count = retry_count + 1
|
||||
end
|
||||
UIManager:close(info)
|
||||
if retry_count == timeout then
|
||||
UIManager:show(InfoMessage:new{ text = _("Error connecting to the network") })
|
||||
return
|
||||
end
|
||||
if callback then callback() end
|
||||
end
|
||||
|
||||
function NetworkMgr:beforeWifiAction(callback)
|
||||
local wifi_enable_action = G_reader_settings:readSetting("wifi_enable_action")
|
||||
if wifi_enable_action == "turn_on" then
|
||||
NetworkMgr:turnOnWifiAndWaitForConnection(callback)
|
||||
else
|
||||
NetworkMgr:promptWifiOn(callback)
|
||||
end
|
||||
end
|
||||
|
||||
function NetworkMgr:isConnected()
|
||||
if Device:isAndroid() then
|
||||
return self:isWifiOn()
|
||||
@@ -183,12 +212,45 @@ function NetworkMgr:getInfoMenuTable()
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
function NetworkMgr:getBeforeWifiActionMenuTable()
|
||||
local wifi_enable_action_setting = G_reader_settings:readSetting("wifi_enable_action") or "prompt"
|
||||
local wifi_enable_actions = {
|
||||
turn_on = {_("turn on"), _("Turn on (experimental)")},
|
||||
prompt = {_("prompt"), _("Prompt")},
|
||||
}
|
||||
local action_table = function(wifi_enable_action)
|
||||
return {
|
||||
text = wifi_enable_actions[wifi_enable_action][2],
|
||||
checked_func = function()
|
||||
return wifi_enable_action_setting == wifi_enable_action
|
||||
end,
|
||||
callback = function()
|
||||
wifi_enable_action_setting = wifi_enable_action
|
||||
G_reader_settings:saveSetting("wifi_enable_action", wifi_enable_action)
|
||||
end,
|
||||
}
|
||||
end
|
||||
return {
|
||||
text_func = function()
|
||||
return T(_("Action when Wi-Fi is off: %1"),
|
||||
wifi_enable_actions[wifi_enable_action_setting][1]
|
||||
)
|
||||
end,
|
||||
sub_item_table = {
|
||||
action_table("turn_on"),
|
||||
action_table("prompt"),
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
function NetworkMgr:getMenuTable()
|
||||
return {
|
||||
self:getWifiMenuTable(),
|
||||
self:getProxyMenuTable(),
|
||||
self:getRestoreMenuTable(),
|
||||
self:getInfoMenuTable(),
|
||||
self:getBeforeWifiActionMenuTable(),
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -66,15 +66,22 @@ function NewsDownloader:init()
|
||||
self.ui.menu:registerToMainMenu(self)
|
||||
end
|
||||
|
||||
|
||||
function NewsDownloader:addToMainMenu(menu_items)
|
||||
self:lazyInitialization()
|
||||
|
||||
menu_items.news_downloader = {
|
||||
text = _("News (RSS/Atom) downloader"),
|
||||
sub_item_table = {
|
||||
{
|
||||
text = _("Download news"),
|
||||
callback = function() self:loadConfigAndProcessFeeds() end,
|
||||
callback = function()
|
||||
if not NetworkMgr:isOnline() then
|
||||
wifi_enabled_before_action = false
|
||||
NetworkMgr:beforeWifiAction(self.loadConfigAndProcessFeeds)
|
||||
else
|
||||
self:loadConfigAndProcessFeeds()
|
||||
end
|
||||
end,
|
||||
},
|
||||
{
|
||||
text = _("Go to news folder"),
|
||||
@@ -97,12 +104,17 @@ function NewsDownloader:addToMainMenu(menu_items)
|
||||
},
|
||||
{
|
||||
text = _("Settings"),
|
||||
callback = function()
|
||||
UIManager:show(InfoMessage:new{
|
||||
text = T(_("To change feed (Atom/RSS) sources please manually edit the configuration file:\n%1\n\nIt is very simple and contains comments as well as sample configuration."),
|
||||
feed_config_path)
|
||||
})
|
||||
end,
|
||||
sub_item_table = {
|
||||
{
|
||||
text = _("Change feeds configuration"),
|
||||
callback = function()
|
||||
UIManager:show(InfoMessage:new{
|
||||
text = T(_("To change feed (Atom/RSS) sources please manually edit the configuration file:\n%1\n\nIt is very simple and contains comments as well as sample configuration."),
|
||||
feed_config_path)
|
||||
})
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
text = _("Help"),
|
||||
@@ -143,11 +155,6 @@ function NewsDownloader:lazyInitialization()
|
||||
end
|
||||
|
||||
function NewsDownloader:loadConfigAndProcessFeeds()
|
||||
if not NetworkMgr:isOnline() then
|
||||
wifi_enabled_before_action = false
|
||||
NetworkMgr:promptWifiOn()
|
||||
return
|
||||
end
|
||||
local info = InfoMessage:new{ text = _("Loading news feed config…") }
|
||||
UIManager:show(info)
|
||||
logger.dbg("force repaint due to upcoming blocking calls")
|
||||
@@ -177,7 +184,7 @@ function NewsDownloader:loadConfigAndProcessFeeds()
|
||||
UIManager:show(info)
|
||||
-- processFeedSource is a blocking call, so manually force a UI refresh beforehand
|
||||
UIManager:forceRePaint()
|
||||
self:processFeedSource(url, tonumber(limit), unsupported_feeds_urls, download_full_article)
|
||||
NewsDownloader:processFeedSource(url, tonumber(limit), unsupported_feeds_urls, download_full_article)
|
||||
UIManager:close(info)
|
||||
else
|
||||
logger.warn('NewsDownloader: invalid feed config entry', feed)
|
||||
|
||||
Reference in New Issue
Block a user