2019-01-17 20:44:15 +00:00
|
|
|
local isAndroid, android = pcall(require, "android")
|
|
|
|
local Device = require("device")
|
|
|
|
local Geom = require("ui/geometry")
|
2019-10-08 16:15:43 +00:00
|
|
|
local ffi = require("ffi")
|
2019-01-17 20:44:15 +00:00
|
|
|
local logger = require("logger")
|
2019-07-05 20:57:58 +00:00
|
|
|
local _ = require("gettext")
|
2019-01-17 20:44:15 +00:00
|
|
|
local Input = Device.input
|
2021-01-16 20:40:00 +00:00
|
|
|
local N_ = _.ngettext
|
2019-01-17 20:44:15 +00:00
|
|
|
local Screen = Device.screen
|
2019-07-26 21:04:20 +00:00
|
|
|
local T = require("ffi/util").template
|
2019-01-17 20:44:15 +00:00
|
|
|
|
|
|
|
if not isAndroid then return end
|
|
|
|
|
2019-10-08 16:15:43 +00:00
|
|
|
local system = ffi.C.AKEEP_SCREEN_ON_DISABLED
|
|
|
|
local screenOn = ffi.C.AKEEP_SCREEN_ON_ENABLED
|
|
|
|
local needs_wakelocks = android.needsWakelocks()
|
|
|
|
|
2019-07-26 21:04:20 +00:00
|
|
|
-- custom timeouts (in milliseconds)
|
2019-10-08 16:15:43 +00:00
|
|
|
local timeout_custom1 = 2 * 60 * 1000
|
|
|
|
local timeout_custom2 = 5 * 60 * 1000
|
|
|
|
local timeout_custom3 = 10 * 60 * 1000
|
|
|
|
local timeout_custom4 = 15 * 60 * 1000
|
|
|
|
local timeout_custom5 = 20 * 60 * 1000
|
|
|
|
local timeout_custom6 = 25 * 60 * 1000
|
2019-07-26 21:04:20 +00:00
|
|
|
local timeout_custom7 = 30 * 60 * 1000
|
|
|
|
|
|
|
|
local function humanReadableTimeout(timeout)
|
|
|
|
local sec = timeout / 1000
|
2021-01-16 20:40:00 +00:00
|
|
|
if sec >= 60 then
|
|
|
|
return T(N_("1 minute", "%1 minutes", sec), sec / 60)
|
2019-07-26 21:04:20 +00:00
|
|
|
else
|
2021-01-16 20:40:00 +00:00
|
|
|
return T(N_("1 second", "%1 seconds", sec), sec)
|
2019-07-26 21:04:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-08 16:15:43 +00:00
|
|
|
local function canModifyTimeout(timeout)
|
|
|
|
if needs_wakelocks then return false end
|
|
|
|
if timeout == system or timeout == screenOn then
|
|
|
|
return true
|
|
|
|
else
|
2020-11-04 18:15:07 +00:00
|
|
|
return android.settings.hasPermission("settings")
|
2019-10-08 16:15:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 21:04:20 +00:00
|
|
|
local function timeoutEquals(timeout)
|
2019-10-08 16:15:43 +00:00
|
|
|
return timeout == android.timeout.get()
|
2019-07-26 21:04:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function saveAndApplyTimeout(timeout)
|
|
|
|
G_reader_settings:saveSetting("android_screen_timeout", timeout)
|
2019-10-08 16:15:43 +00:00
|
|
|
android.timeout.set(timeout)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function requestWriteSettings()
|
2020-11-04 18:15:07 +00:00
|
|
|
local text = _([[
|
|
|
|
Allow KOReader to modify system settings?
|
|
|
|
|
|
|
|
You will be prompted with a permission management screen. You'll need to give KOReader permission and then restart the program.]])
|
|
|
|
|
|
|
|
android.settings.requestPermission("settings", text, _("Allow"), _("Cancel"))
|
2019-07-26 21:04:20 +00:00
|
|
|
end
|
|
|
|
|
2019-01-17 20:44:15 +00:00
|
|
|
local ScreenHelper = {}
|
|
|
|
|
|
|
|
function ScreenHelper:toggleFullscreen()
|
2019-07-05 20:57:58 +00:00
|
|
|
local api = Device.firmware_rev
|
|
|
|
|
|
|
|
if api < 19 and api >= 17 then
|
|
|
|
Device:toggleFullscreen()
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("This will take effect on next restart.")
|
|
|
|
})
|
|
|
|
elseif api < 17 then
|
|
|
|
self:toggleFullscreenLegacy()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- toggle android status bar visibility -- Legacy function for Apis 14 - 16
|
|
|
|
function ScreenHelper:toggleFullscreenLegacy()
|
2019-01-17 20:44:15 +00:00
|
|
|
-- toggle android status bar visibility
|
|
|
|
local is_fullscreen = android.isFullscreen()
|
|
|
|
android.setFullscreen(not is_fullscreen)
|
|
|
|
logger.dbg(string.format("requesting fullscreen: %s", not is_fullscreen))
|
|
|
|
|
|
|
|
local screen_width = android.getScreenWidth()
|
|
|
|
local screen_height = android.getScreenHeight()
|
|
|
|
local status_bar_height = android.getStatusBarHeight()
|
|
|
|
local new_height = screen_height - status_bar_height
|
|
|
|
|
|
|
|
if not is_fullscreen and Screen.viewport then
|
|
|
|
status_bar_height = 0
|
|
|
|
-- reset touchTranslate to normal
|
|
|
|
Input:registerEventAdjustHook(
|
|
|
|
Input.adjustTouchTranslate,
|
|
|
|
{x = 0 + Screen.viewport.x, y = 0 + Screen.viewport.y})
|
|
|
|
end
|
|
|
|
|
|
|
|
local viewport = Geom:new{x=0, y=status_bar_height, w=screen_width, h=new_height}
|
|
|
|
logger.info(string.format("Switching viewport to new geometry [x=%d,y=%d,w=%d,h=%d]",
|
|
|
|
0, status_bar_height, screen_width, new_height))
|
|
|
|
|
|
|
|
Screen:setViewport(viewport)
|
|
|
|
|
|
|
|
if is_fullscreen and Screen.viewport then
|
|
|
|
Input:registerEventAdjustHook(
|
|
|
|
Input.adjustTouchTranslate,
|
|
|
|
{x = 0 - Screen.viewport.x, y = 0 - Screen.viewport.y})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 21:04:20 +00:00
|
|
|
-- timeout menu table
|
|
|
|
function ScreenHelper:getTimeoutMenuTable()
|
2019-10-08 16:15:43 +00:00
|
|
|
local t = {
|
2019-07-26 21:04:20 +00:00
|
|
|
{
|
|
|
|
text = _("Use system settings"),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(system) end,
|
|
|
|
checked_func = function() return timeoutEquals(system) end,
|
|
|
|
callback = function() saveAndApplyTimeout(system) end
|
2019-07-26 21:04:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom1),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom1) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom1) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom1) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom2),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom2) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom2) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom2) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom3),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom3) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom3) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom3) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom4),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom4) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom4) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom4) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom5),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom5) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom5) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom5) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom6),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom6) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom6) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom6) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = humanReadableTimeout(timeout_custom7),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(timeout_custom7) end,
|
2019-07-26 21:04:20 +00:00
|
|
|
checked_func = function() return timeoutEquals(timeout_custom7) end,
|
|
|
|
callback = function() saveAndApplyTimeout(timeout_custom7) end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Keep screen on"),
|
2019-10-08 16:15:43 +00:00
|
|
|
enabled_func = function() return canModifyTimeout(screenOn) end,
|
|
|
|
checked_func = function() return timeoutEquals(screenOn) end,
|
|
|
|
callback = function() saveAndApplyTimeout(screenOn) end
|
2019-07-26 21:04:20 +00:00
|
|
|
},
|
|
|
|
}
|
2019-10-08 16:15:43 +00:00
|
|
|
|
2020-11-04 18:15:07 +00:00
|
|
|
if not android.settings.hasPermission("settings") then
|
2019-10-08 16:15:43 +00:00
|
|
|
table.insert(t, 1, {
|
|
|
|
text = _("Allow system settings override"),
|
2020-11-04 18:15:07 +00:00
|
|
|
enabled_func = function() return not android.settings.hasPermission("settings") end,
|
|
|
|
checked_func = function() return android.settings.hasPermission("settings") end,
|
2019-10-08 16:15:43 +00:00
|
|
|
callback = function() requestWriteSettings() end,
|
|
|
|
separator = true,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
text = _("Screen Timeout"),
|
|
|
|
sub_item_table = t
|
2019-07-26 21:04:20 +00:00
|
|
|
}
|
2019-01-17 20:44:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ScreenHelper
|