2014-07-17 13:00:45 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2016-06-26 00:53:08 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2017-04-29 14:30:16 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local LuaSettings = require("luasettings")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-07-17 13:00:45 +00:00
|
|
|
local _ = require("gettext")
|
2017-04-29 14:30:16 +00:00
|
|
|
local T = require("ffi/util").template
|
2014-08-18 22:48:06 +00:00
|
|
|
|
2016-06-12 18:50:30 +00:00
|
|
|
local NetworkMgr = {}
|
2015-02-01 07:16:27 +00:00
|
|
|
|
2016-09-12 18:49:48 +00:00
|
|
|
function NetworkMgr:readNWSettings()
|
2016-06-26 00:53:08 +00:00
|
|
|
self.nw_settings = LuaSettings:open(DataStorage:getSettingsDir().."/network.lua")
|
2016-09-12 18:49:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:init()
|
2016-09-01 07:05:40 +00:00
|
|
|
self.wifi_was_on = G_reader_settings:isTrue("wifi_was_on")
|
2016-09-12 18:49:48 +00:00
|
|
|
if self.wifi_was_on and G_reader_settings:nilOrTrue("auto_restore_wifi") then
|
|
|
|
self:restoreWifiAsync()
|
|
|
|
end
|
2016-06-26 00:53:08 +00:00
|
|
|
end
|
2014-07-17 13:00:45 +00:00
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
-- Following methods are Device specific which need to be initialized in
|
|
|
|
-- Device:initNetworkManager. Some of them can be set by calling
|
|
|
|
-- NetworkMgr:setWirelessBackend
|
|
|
|
function NetworkMgr:turnOnWifi() end
|
2016-06-12 18:50:30 +00:00
|
|
|
function NetworkMgr:turnOffWifi() end
|
2016-06-26 00:53:08 +00:00
|
|
|
function NetworkMgr:getNetworkList() end
|
|
|
|
function NetworkMgr:getCurrentNetwork() end
|
|
|
|
function NetworkMgr:authenticateNetwork() end
|
|
|
|
function NetworkMgr:disconnectNetwork() end
|
|
|
|
function NetworkMgr:obtainIP() end
|
|
|
|
function NetworkMgr:releaseIP() end
|
2016-09-01 07:05:40 +00:00
|
|
|
-- This function should unblockly call both turnOnWifi() and obtainIP().
|
|
|
|
function NetworkMgr:restoreWifiAsync() end
|
2016-06-26 00:53:08 +00:00
|
|
|
-- End of device specific methods
|
2014-07-17 13:00:45 +00:00
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
function NetworkMgr:promptWifiOn(complete_callback)
|
2014-07-17 13:00:45 +00:00
|
|
|
UIManager:show(ConfirmBox:new{
|
2016-03-10 15:26:57 +00:00
|
|
|
text = _("Do you want to turn on Wi-Fi?"),
|
2017-04-04 13:31:13 +00:00
|
|
|
ok_text = _("Turn on"),
|
2014-07-17 13:00:45 +00:00
|
|
|
ok_callback = function()
|
2016-09-01 07:05:40 +00:00
|
|
|
self.wifi_was_on = true
|
|
|
|
G_reader_settings:saveSetting("wifi_was_on", true)
|
2016-06-26 00:53:08 +00:00
|
|
|
self:turnOnWifi(complete_callback)
|
2014-07-17 13:00:45 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
function NetworkMgr:promptWifiOff(complete_callback)
|
2014-07-17 13:00:45 +00:00
|
|
|
UIManager:show(ConfirmBox:new{
|
2016-03-10 15:26:57 +00:00
|
|
|
text = _("Do you want to turn off Wi-Fi?"),
|
2017-04-04 13:31:13 +00:00
|
|
|
ok_text = _("Turn off"),
|
2014-07-17 13:00:45 +00:00
|
|
|
ok_callback = function()
|
2016-09-01 07:05:40 +00:00
|
|
|
self.wifi_was_on = false
|
|
|
|
G_reader_settings:saveSetting("wifi_was_on", false)
|
2016-06-26 00:53:08 +00:00
|
|
|
self:turnOffWifi(complete_callback)
|
2014-07-17 13:00:45 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2016-11-28 00:40:47 +00:00
|
|
|
function NetworkMgr:isOnline()
|
2015-01-19 16:07:36 +00:00
|
|
|
local socket = require("socket")
|
2015-04-15 05:34:13 +00:00
|
|
|
return socket.dns.toip("www.example.com") ~= nil
|
2014-08-24 12:34:38 +00:00
|
|
|
end
|
|
|
|
|
2014-11-11 04:12:34 +00:00
|
|
|
function NetworkMgr:setHTTPProxy(proxy)
|
|
|
|
local http = require("socket.http")
|
|
|
|
http.PROXY = proxy
|
|
|
|
if proxy then
|
|
|
|
G_reader_settings:saveSetting("http_proxy", proxy)
|
|
|
|
G_reader_settings:saveSetting("http_proxy_enabled", true)
|
|
|
|
else
|
|
|
|
G_reader_settings:saveSetting("http_proxy_enabled", false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:getWifiMenuTable()
|
|
|
|
return {
|
2016-03-10 15:26:57 +00:00
|
|
|
text = _("Wi-Fi connection"),
|
2014-11-11 04:12:34 +00:00
|
|
|
enabled_func = function() return Device:isKindle() or Device:isKobo() end,
|
2016-11-28 00:40:47 +00:00
|
|
|
checked_func = function() return NetworkMgr:isOnline() end,
|
2016-06-26 00:53:08 +00:00
|
|
|
callback = function(menu)
|
2016-11-28 00:40:47 +00:00
|
|
|
local wifi_status = NetworkMgr:isOnline()
|
2016-06-26 00:53:08 +00:00
|
|
|
local complete_callback = function()
|
|
|
|
-- notify touch menu to update item check state
|
|
|
|
menu:updateItems()
|
2016-10-23 22:27:39 +00:00
|
|
|
local Event = require("ui/event")
|
|
|
|
-- if wifi was on, this callback will only be executed when the network has been
|
|
|
|
-- disconnected.
|
|
|
|
if wifi_status then
|
|
|
|
UIManager:broadcastEvent(Event:new("NetworkDisconnected"))
|
|
|
|
else
|
|
|
|
UIManager:broadcastEvent(Event:new("NetworkConnected"))
|
|
|
|
end
|
2016-06-26 00:53:08 +00:00
|
|
|
end
|
2016-10-23 22:27:39 +00:00
|
|
|
if wifi_status then
|
2016-06-26 00:53:08 +00:00
|
|
|
NetworkMgr:promptWifiOff(complete_callback)
|
2014-11-11 04:12:34 +00:00
|
|
|
else
|
2016-06-26 00:53:08 +00:00
|
|
|
NetworkMgr:promptWifiOn(complete_callback)
|
2014-11-11 04:12:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:getProxyMenuTable()
|
|
|
|
local proxy_enabled = function()
|
|
|
|
return G_reader_settings:readSetting("http_proxy_enabled")
|
|
|
|
end
|
|
|
|
local proxy = function()
|
|
|
|
return G_reader_settings:readSetting("http_proxy")
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
text_func = function()
|
2014-11-28 21:38:54 +00:00
|
|
|
return T(_("HTTP proxy %1"), (proxy_enabled() and proxy() or ""))
|
2014-11-11 04:12:34 +00:00
|
|
|
end,
|
|
|
|
checked_func = function() return proxy_enabled() end,
|
|
|
|
callback = function()
|
|
|
|
if not proxy_enabled() and proxy() then
|
|
|
|
NetworkMgr:setHTTPProxy(proxy())
|
|
|
|
elseif proxy_enabled() then
|
|
|
|
NetworkMgr:setHTTPProxy(nil)
|
|
|
|
end
|
|
|
|
if not proxy() then
|
|
|
|
UIManager:show(InfoMessage:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = _("Tip:\nLong press on this menu entry to configure HTTP proxy."),
|
2014-11-11 04:12:34 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
hold_input = {
|
2014-11-28 13:10:37 +00:00
|
|
|
title = _("Enter proxy address"),
|
2014-11-11 04:12:34 +00:00
|
|
|
type = "text",
|
|
|
|
hint = proxy() or "",
|
|
|
|
callback = function(input)
|
|
|
|
if input ~= "" then
|
|
|
|
NetworkMgr:setHTTPProxy(input)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-09-01 07:05:40 +00:00
|
|
|
function NetworkMgr:getRestoreMenuTable()
|
|
|
|
return {
|
|
|
|
text = _("Automatically restore Wi-Fi connection after resume"),
|
|
|
|
checked_func = function() return G_reader_settings:nilOrTrue("auto_restore_wifi") end,
|
|
|
|
enabled_func = function() return Device:isKobo() end,
|
|
|
|
callback = function(menu) G_reader_settings:flipNilOrTrue("auto_restore_wifi") end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-07-27 04:32:13 +00:00
|
|
|
function NetworkMgr:getInfoMenuTable()
|
|
|
|
return {
|
|
|
|
text = _("Network info"),
|
2016-11-28 00:40:47 +00:00
|
|
|
-- TODO: also show network info when device is authenticated to router but offline
|
|
|
|
enabled_func = function() return self:isOnline() end,
|
2016-07-27 04:32:13 +00:00
|
|
|
callback = function(menu)
|
|
|
|
if Device.retrieveNetworkInfo then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = Device:retrieveNetworkInfo(),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2017-04-04 13:31:13 +00:00
|
|
|
text = _("Could not retrieve network info."),
|
2016-07-27 04:32:13 +00:00
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-09-01 07:05:40 +00:00
|
|
|
function NetworkMgr:getMenuTable()
|
|
|
|
return {
|
|
|
|
self:getWifiMenuTable(),
|
|
|
|
self:getProxyMenuTable(),
|
|
|
|
self:getRestoreMenuTable(),
|
|
|
|
self:getInfoMenuTable(),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
function NetworkMgr:showNetworkMenu(complete_callback)
|
|
|
|
local info = InfoMessage:new{text = _("Scanning…")}
|
|
|
|
UIManager:show(info)
|
|
|
|
UIManager:nextTick(function()
|
2016-10-18 05:02:06 +00:00
|
|
|
local network_list, err = self:getNetworkList()
|
2016-06-26 00:53:08 +00:00
|
|
|
UIManager:close(info)
|
2016-10-18 05:02:06 +00:00
|
|
|
if network_list == nil then
|
|
|
|
UIManager:show(InfoMessage:new{text = err})
|
|
|
|
return
|
|
|
|
end
|
2016-06-26 00:53:08 +00:00
|
|
|
UIManager:show(require("ui/widget/networksetting"):new{
|
|
|
|
network_list = network_list,
|
|
|
|
connect_callback = complete_callback,
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:saveNetwork(setting)
|
2016-09-12 18:49:48 +00:00
|
|
|
if not self.nw_settings then self:readNWSettings() end
|
2017-02-27 04:51:04 +00:00
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
self.nw_settings:saveSetting(setting.ssid, {
|
|
|
|
ssid = setting.ssid,
|
|
|
|
password = setting.password,
|
2017-02-27 04:51:04 +00:00
|
|
|
psk = setting.psk,
|
2016-06-26 00:53:08 +00:00
|
|
|
flags = setting.flags,
|
|
|
|
})
|
|
|
|
self.nw_settings:flush()
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:deleteNetwork(setting)
|
2016-09-12 18:49:48 +00:00
|
|
|
if not self.nw_settings then self:readNWSettings() end
|
2016-06-26 00:53:08 +00:00
|
|
|
self.nw_settings:delSetting(setting.ssid)
|
|
|
|
self.nw_settings:flush()
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:getAllSavedNetworks()
|
2016-09-12 18:49:48 +00:00
|
|
|
if not self.nw_settings then self:readNWSettings() end
|
2016-06-26 00:53:08 +00:00
|
|
|
return self.nw_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:setWirelessBackend(name, options)
|
|
|
|
require("ui/network/"..name).init(self, options)
|
|
|
|
end
|
|
|
|
|
2014-10-05 08:04:49 +00:00
|
|
|
-- set network proxy if global variable NETWORK_PROXY is defined
|
|
|
|
if NETWORK_PROXY then
|
2014-11-11 04:12:34 +00:00
|
|
|
NetworkMgr:setHTTPProxy(NETWORK_PROXY)
|
2014-10-05 08:04:49 +00:00
|
|
|
end
|
|
|
|
|
2016-06-12 18:50:30 +00:00
|
|
|
Device:initNetworkManager(NetworkMgr)
|
2016-09-12 18:49:48 +00:00
|
|
|
NetworkMgr:init()
|
2016-06-12 18:50:30 +00:00
|
|
|
|
2014-07-17 13:00:45 +00:00
|
|
|
return NetworkMgr
|