diff --git a/frontend/ui/network/manager.lua b/frontend/ui/network/manager.lua index 5185d6b75..1bf4c68c2 100644 --- a/frontend/ui/network/manager.lua +++ b/frontend/ui/network/manager.lua @@ -5,6 +5,7 @@ local Device = require("device") local Event = require("ui/event") local InfoMessage = require("ui/widget/infomessage") local LuaSettings = require("luasettings") +local MultiConfirmBox = require("ui/widget/multiconfirmbox") local UIManager = require("ui/uimanager") local ffiutil = require("ffi/util") local logger = require("logger") @@ -135,6 +136,24 @@ function NetworkMgr:promptWifiOff(complete_callback) }) end +function NetworkMgr:promptWifi(complete_callback) + UIManager:show(MultiConfirmBox:new{ + text = _("Wi-Fi is enabled, but you're currently not connected to a network.\nHow would you like to proceed?"), + choice1_text = _("Turn Wi-Fi off"), + choice1_callback = function() + self.wifi_was_on = false + G_reader_settings:saveSetting("wifi_was_on", false) + self:turnOffWifi(complete_callback) + end, + choice2_text = _("Connect"), + choice2_callback = function() + self.wifi_was_on = true + G_reader_settings:saveSetting("wifi_was_on", true) + self:turnOnWifi(complete_callback) + end, + }) +end + function NetworkMgr:turnOnWifiAndWaitForConnection(callback) local info = InfoMessage:new{ text = _("Connecting to Wi-Fi…") } UIManager:show(info) @@ -335,13 +354,15 @@ function NetworkMgr:getWifiToggleMenuTable() enabled_func = function() return Device:hasWifiToggle() end, checked_func = function() return NetworkMgr:isWifiOn() end, callback = function(touchmenu_instance) - local wifi_status = NetworkMgr:isWifiOn() and NetworkMgr:isConnected() + local is_wifi_on = NetworkMgr:isWifiOn() + local is_connected = NetworkMgr:isConnected() + local fully_connected = is_wifi_on and is_connected local complete_callback = function() - -- notify touch menu to update item check state + -- Notify TouchMenu to update item check state touchmenu_instance:updateItems() - -- if wifi was on, this callback will only be executed when the network has been - -- disconnected. - if wifi_status then + -- If Wi-Fi was on when the menu was shown, this means the tap meant to turn the Wi-Fi *off*, + -- as such, this callback will only be executed *after* the network has been disconnected. + if fully_connected then UIManager:broadcastEvent(Event:new("NetworkDisconnected")) else -- On hasWifiManager devices that play with kernel modules directly, @@ -351,6 +372,9 @@ function NetworkMgr:getWifiToggleMenuTable() UIManager:broadcastEvent(Event:new("NetworkConnected")) elseif NetworkMgr:isWifiOn() and not NetworkMgr:isConnected() then -- Don't leave Wi-Fi in an inconsistent state if the connection failed. + -- NOTE: Keep in mind that NetworkSetting only runs this callback on *successful* connections! + -- (It's called connect_callback there). + -- This makes this branch somewhat hard to reach, which is why it gets a dedicated prompt below... self.wifi_was_on = false G_reader_settings:saveSetting("wifi_was_on", false) -- NOTE: We're limiting this to only a few platforms, as it might be actually harmful on some devices. @@ -372,8 +396,10 @@ function NetworkMgr:getWifiToggleMenuTable() end end end - if wifi_status then + if fully_connected then NetworkMgr:promptWifiOff(complete_callback) + elseif is_wifi_on and not is_connected then + NetworkMgr:promptWifi(complete_callback) else NetworkMgr:promptWifiOn(complete_callback) end @@ -574,6 +600,8 @@ function NetworkMgr:showNetworkMenu(complete_callback) return end end + -- NOTE: Also supports a disconnect_callback, should we use it for something? + -- Tearing down Wi-Fi completely when tapping "disconnect" would feel a bit harsh, though... UIManager:show(require("ui/widget/networksetting"):new{ network_list = network_list, connect_callback = complete_callback, diff --git a/frontend/ui/widget/networksetting.lua b/frontend/ui/widget/networksetting.lua index b150c53a9..94eb69405 100644 --- a/frontend/ui/widget/networksetting.lua +++ b/frontend/ui/widget/networksetting.lua @@ -237,6 +237,7 @@ function NetworkItem:connect() end -- Do what it says on the tin, and only trigger the connect_callback on a *successful* connect. + -- NOTE: This callback comes from NetworkManager, where it's named complete_callback. if success and self.setting_ui.connect_callback then self.setting_ui.connect_callback() end