tweak timeouts, add haptic feedback support

Co-Authored-By: Frans de Jonge <fransdejonge@gmail.com>
pull/5463/head
Martín Fernández 5 years ago
parent e131443834
commit b75fec4971

@ -70,6 +70,7 @@ local Device = Generic:new{
canRestart = no, canRestart = no,
firmware_rev = android.app.activity.sdkVersion, firmware_rev = android.app.activity.sdkVersion,
display_dpi = android.lib.AConfiguration_getDensity(android.app.config), display_dpi = android.lib.AConfiguration_getDensity(android.app.config),
isHapticFeedbackEnabled = yes,
hasClipboard = yes, hasClipboard = yes,
hasOTAUpdates = canUpdateApk, hasOTAUpdates = canUpdateApk,
canOpenLink = yes, canOpenLink = yes,
@ -169,19 +170,17 @@ function Device:init()
self.isTouchDevice = yes self.isTouchDevice = yes
end end
-- check if we enabled support for custom timeouts (including wakelocks) -- check if we use custom timeouts
if android.needsWakelocks() then if android.needsWakelocks() then
android.setScreenOffTimeout(-1) android.timeout.set(C.AKEEP_SCREEN_ON_ENABLED)
else else
local timeout = G_reader_settings:readSetting("android_screen_timeout") local timeout = G_reader_settings:readSetting("android_screen_timeout")
if timeout and timeout > 0 then if timeout then
-- set a custom timeout if we already have write settings permission. if timeout == C.AKEEP_SCREEN_ON_ENABLED
-- do not attempt to request permissions here. or (timeout > C.AKEEP_SCREEN_ON_DISABLED
if android.canWriteSettings() then and android.settings.canWrite()) then
android.setScreenOffTimeout(timeout) android.timeout.set(timeout)
end end
elseif timeout and timeout == -1 then
android.setScreenOffTimeout(timeout)
end end
end end
@ -225,6 +224,10 @@ function Device:initNetworkManager(NetworkMgr)
end end
end end
function Device:performHapticFeedback(type)
android.hapticFeedback(C["AHAPTIC_"..type])
end
function Device:retrieveNetworkInfo() function Device:retrieveNetworkInfo()
local ssid, ip, gw = android.getNetworkInfo() local ssid, ip, gw = android.getNetworkInfo()
if ip == "0" or gw == "0" then if ip == "0" or gw == "0" then

@ -1,6 +1,7 @@
local isAndroid, android = pcall(require, "android") local isAndroid, android = pcall(require, "android")
local Device = require("device") local Device = require("device")
local Geom = require("ui/geometry") local Geom = require("ui/geometry")
local ffi = require("ffi")
local logger = require("logger") local logger = require("logger")
local _ = require("gettext") local _ = require("gettext")
local Input = Device.input local Input = Device.input
@ -9,20 +10,19 @@ local T = require("ffi/util").template
if not isAndroid then return end if not isAndroid then return end
local system = ffi.C.AKEEP_SCREEN_ON_DISABLED
local screenOn = ffi.C.AKEEP_SCREEN_ON_ENABLED
local needs_wakelocks = android.needsWakelocks()
-- custom timeouts (in milliseconds) -- custom timeouts (in milliseconds)
local timeout_custom1 = 30 * 1000 local timeout_custom1 = 2 * 60 * 1000
local timeout_custom2 = 60 * 1000 local timeout_custom2 = 5 * 60 * 1000
local timeout_custom3 = 2 * 60 * 1000 local timeout_custom3 = 10 * 60 * 1000
local timeout_custom4 = 5 * 60 * 1000 local timeout_custom4 = 15 * 60 * 1000
local timeout_custom5 = 10 * 60 * 1000 local timeout_custom5 = 20 * 60 * 1000
local timeout_custom6 = 15 * 60 * 1000 local timeout_custom6 = 25 * 60 * 1000
local timeout_custom7 = 30 * 60 * 1000 local timeout_custom7 = 30 * 60 * 1000
local timeout_system = 0 -- same as system (do nothing!)
local timeout_disabled = -1 -- disable timeout using wakelocks
local can_modify_timeout = not android.needsWakelocks()
local function humanReadableTimeout(timeout) local function humanReadableTimeout(timeout)
local sec = timeout / 1000 local sec = timeout / 1000
if sec >= 120 then if sec >= 120 then
@ -32,13 +32,35 @@ local function humanReadableTimeout(timeout)
end end
end end
local function canModifyTimeout(timeout)
if needs_wakelocks then return false end
if timeout == system or timeout == screenOn then
return true
else
return android.settings.canWrite()
end
end
local function timeoutEquals(timeout) local function timeoutEquals(timeout)
return timeout == android.getScreenOffTimeout() return timeout == android.timeout.get()
end end
local function saveAndApplyTimeout(timeout) local function saveAndApplyTimeout(timeout)
G_reader_settings:saveSetting("android_screen_timeout", timeout) G_reader_settings:saveSetting("android_screen_timeout", timeout)
android.setScreenOffTimeout(timeout) android.timeout.set(timeout)
end
local function requestWriteSettings()
local UIManager = require("ui/uimanager")
local ConfirmBox = require("ui/widget/confirmbox")
UIManager:show(ConfirmBox:new{
text = _("Allow KOReader to modify system settings?\n\nYou will be prompted with a permission management screen. You'll need to give KOReader permission and then restart the program."),
ok_text = _("Allow"),
ok_callback = function()
UIManager:scheduleIn(1, function() UIManager:quit() end)
android.settings.requestWritePermission()
end,
})
end end
local ScreenHelper = {} local ScreenHelper = {}
@ -93,66 +115,77 @@ end
-- timeout menu table -- timeout menu table
function ScreenHelper:getTimeoutMenuTable() function ScreenHelper:getTimeoutMenuTable()
return { local t = {
text = _("Screen Timeout"),
sub_item_table = {
{ {
text = _("Use system settings"), text = _("Use system settings"),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(system) end,
checked_func = function() return timeoutEquals(timeout_system) end, checked_func = function() return timeoutEquals(system) end,
callback = function() saveAndApplyTimeout(timeout_system) end callback = function() saveAndApplyTimeout(system) end
}, },
{ {
text = humanReadableTimeout(timeout_custom1), text = humanReadableTimeout(timeout_custom1),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom1) end,
checked_func = function() return timeoutEquals(timeout_custom1) end, checked_func = function() return timeoutEquals(timeout_custom1) end,
callback = function() saveAndApplyTimeout(timeout_custom1) end callback = function() saveAndApplyTimeout(timeout_custom1) end
}, },
{ {
text = humanReadableTimeout(timeout_custom2), text = humanReadableTimeout(timeout_custom2),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom2) end,
checked_func = function() return timeoutEquals(timeout_custom2) end, checked_func = function() return timeoutEquals(timeout_custom2) end,
callback = function() saveAndApplyTimeout(timeout_custom2) end callback = function() saveAndApplyTimeout(timeout_custom2) end
}, },
{ {
text = humanReadableTimeout(timeout_custom3), text = humanReadableTimeout(timeout_custom3),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom3) end,
checked_func = function() return timeoutEquals(timeout_custom3) end, checked_func = function() return timeoutEquals(timeout_custom3) end,
callback = function() saveAndApplyTimeout(timeout_custom3) end callback = function() saveAndApplyTimeout(timeout_custom3) end
}, },
{ {
text = humanReadableTimeout(timeout_custom4), text = humanReadableTimeout(timeout_custom4),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom4) end,
checked_func = function() return timeoutEquals(timeout_custom4) end, checked_func = function() return timeoutEquals(timeout_custom4) end,
callback = function() saveAndApplyTimeout(timeout_custom4) end callback = function() saveAndApplyTimeout(timeout_custom4) end
}, },
{ {
text = humanReadableTimeout(timeout_custom5), text = humanReadableTimeout(timeout_custom5),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom5) end,
checked_func = function() return timeoutEquals(timeout_custom5) end, checked_func = function() return timeoutEquals(timeout_custom5) end,
callback = function() saveAndApplyTimeout(timeout_custom5) end callback = function() saveAndApplyTimeout(timeout_custom5) end
}, },
{ {
text = humanReadableTimeout(timeout_custom6), text = humanReadableTimeout(timeout_custom6),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom6) end,
checked_func = function() return timeoutEquals(timeout_custom6) end, checked_func = function() return timeoutEquals(timeout_custom6) end,
callback = function() saveAndApplyTimeout(timeout_custom6) end callback = function() saveAndApplyTimeout(timeout_custom6) end
}, },
{ {
text = humanReadableTimeout(timeout_custom7), text = humanReadableTimeout(timeout_custom7),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(timeout_custom7) end,
checked_func = function() return timeoutEquals(timeout_custom7) end, checked_func = function() return timeoutEquals(timeout_custom7) end,
callback = function() saveAndApplyTimeout(timeout_custom7) end callback = function() saveAndApplyTimeout(timeout_custom7) end
}, },
{ {
text = _("Keep screen on"), text = _("Keep screen on"),
enabled_func = function() return can_modify_timeout end, enabled_func = function() return canModifyTimeout(screenOn) end,
checked_func = function() return timeoutEquals(timeout_disabled) end, checked_func = function() return timeoutEquals(screenOn) end,
callback = function() saveAndApplyTimeout(timeout_disabled) end callback = function() saveAndApplyTimeout(screenOn) end
}, },
} }
if not android.settings.canWrite() then
table.insert(t, 1, {
text = _("Allow system settings override"),
enabled_func = function() return not android.settings.canWrite() end,
checked_func = function() return android.settings.canWrite() end,
callback = function() requestWriteSettings() end,
separator = true,
})
end
return {
text = _("Screen Timeout"),
sub_item_table = t
} }
end end
return ScreenHelper return ScreenHelper

Loading…
Cancel
Save