mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
[Android] misc fixes (#4478)
* use product as device model * print android version (codename + number) + api at launch * exit the application properly * fix fullscreen switching (and disable it on newer android versions) * gettext: lower log level for message: cannot open translation file * android common settings refactor
This commit is contained in:
committed by
Frans de Jonge
parent
81e160692d
commit
b15c2ed0c5
@@ -9,12 +9,12 @@ local function yes() return true end
|
||||
local function no() return false end
|
||||
|
||||
local Device = Generic:new{
|
||||
model = "Android",
|
||||
model = android.getProduct(),
|
||||
hasKeys = yes,
|
||||
hasDPad = no,
|
||||
isAndroid = yes,
|
||||
hasFrontlight = yes,
|
||||
firmware_rev = "none",
|
||||
firmware_rev = android.app.activity.sdkVersion,
|
||||
display_dpi = android.lib.AConfiguration_getDensity(android.app.config),
|
||||
hasClipboard = yes,
|
||||
hasColorScreen = yes,
|
||||
@@ -89,4 +89,38 @@ function Device:initNetworkManager(NetworkMgr)
|
||||
end
|
||||
end
|
||||
|
||||
function Device:exit()
|
||||
android.log_name = 'luajit-launcher'
|
||||
android.LOGI("Finishing luajit launcher main activity");
|
||||
android.lib.ANativeActivity_finish(android.app.activity)
|
||||
end
|
||||
|
||||
local function getCodename()
|
||||
local api = Device.firmware_rev
|
||||
local codename = nil
|
||||
|
||||
if api > 27 then
|
||||
codename = "Pie"
|
||||
elseif api == 27 or api == 26 then
|
||||
codename = "Oreo"
|
||||
elseif api == 25 or api == 24 then
|
||||
codename = "Nougat"
|
||||
elseif api == 23 then
|
||||
codename = "Marshmallow"
|
||||
elseif api == 22 or api == 21 then
|
||||
codename = "Lollipop"
|
||||
elseif api == 19 then
|
||||
codename = "KitKat"
|
||||
elseif api < 19 and api >= 16 then
|
||||
codename = "Jelly Bean"
|
||||
elseif api < 16 and api >= 14 then
|
||||
codename = "Ice Cream Sandwich"
|
||||
end
|
||||
|
||||
return codename or ""
|
||||
end
|
||||
|
||||
android.LOGI(string.format("Android %s - %s (API %d)",
|
||||
android.getVersion(), getCodename(), Device.firmware_rev))
|
||||
|
||||
return Device
|
||||
|
||||
@@ -51,7 +51,7 @@ function GetText_mt.__index.changeLang(new_lang)
|
||||
local po = io.open(file, "r")
|
||||
|
||||
if not po then
|
||||
logger.warn("cannot open translation file:", file)
|
||||
logger.dbg("cannot open translation file:", file)
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
@@ -123,9 +123,29 @@ if Screen.isColorScreen() then
|
||||
else
|
||||
common_settings.screen.sub_item_table[2].separator = true
|
||||
end
|
||||
|
||||
if Device:isAndroid() then
|
||||
table.insert(common_settings.screen.sub_item_table, require("ui/elements/screen_fullscreen_menu_table"))
|
||||
table.insert(common_settings.screen.sub_item_table, require("ui/elements/screen_keep_on_menu_table"))
|
||||
-- android common settings
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
if not isAndroid then return end
|
||||
|
||||
-- keep screen on
|
||||
table.insert(common_settings.screen.sub_item_table,
|
||||
{
|
||||
text = _("Keep screen on"),
|
||||
checked_func = function() return not G_reader_settings:isTrue("disable_android_wakelock") end,
|
||||
callback = function() require("ui/elements/screen_android"):toggleWakelock() end,
|
||||
})
|
||||
|
||||
-- fullscreen
|
||||
if Device.firmware_rev <= 16 then
|
||||
table.insert(common_settings.screen.sub_item_table,
|
||||
{
|
||||
text = _("Fullscreen"),
|
||||
checked_func = function() return android.isFullscreen() end,
|
||||
callback = function() require("ui/elements/screen_android"):toggleFullscreen() end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
if Device:hasKeys() then
|
||||
|
||||
52
frontend/ui/elements/screen_android.lua
Normal file
52
frontend/ui/elements/screen_android.lua
Normal file
@@ -0,0 +1,52 @@
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
local Device = require("device")
|
||||
local Geom = require("ui/geometry")
|
||||
local logger = require("logger")
|
||||
local Input = Device.input
|
||||
local Screen = Device.screen
|
||||
|
||||
if not isAndroid then return end
|
||||
|
||||
local ScreenHelper = {}
|
||||
|
||||
-- toggle android status bar visibility
|
||||
function ScreenHelper:toggleFullscreen()
|
||||
-- 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
|
||||
|
||||
-- toggle android wakelock support
|
||||
function ScreenHelper:toggleWakelock()
|
||||
local is_wakelock = not G_reader_settings:isTrue("disable_android_wakelock")
|
||||
android.setWakeLock(not is_wakelock)
|
||||
G_reader_settings:saveSetting("disable_android_wakelock", is_wakelock)
|
||||
end
|
||||
|
||||
return ScreenHelper
|
||||
@@ -1,47 +0,0 @@
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
local Device = require("device")
|
||||
local Geom = require("ui/geometry")
|
||||
local logger = require("logger")
|
||||
local _ = require("gettext")
|
||||
local Input = Device.input
|
||||
local Screen = Device.screen
|
||||
|
||||
if not isAndroid then return end
|
||||
|
||||
return {
|
||||
text = _("Fullscreen"),
|
||||
checked_func = function()
|
||||
return G_reader_settings:isFalse("disabled_fullscreen")
|
||||
end,
|
||||
callback = function()
|
||||
local disabled_fullscreen = G_reader_settings:isTrue("disabled_fullscreen")
|
||||
|
||||
logger.dbg("screen_fullscreen_menu_table.lua: Fullscreen swiching to: ", disabled_fullscreen)
|
||||
android.setFullscreen(disabled_fullscreen)
|
||||
|
||||
local status_bar_height = android.getStatusBarHeight()
|
||||
logger.dbg("screen_fullscreen_menu_table.lua: Status bar height: ", status_bar_height)
|
||||
local screen_width = android.getScreenWidth()
|
||||
logger.dbg("screen_fullscreen_menu_table.lua: Screen width: ", screen_width)
|
||||
local screen_height = android.getScreenHeight()
|
||||
logger.dbg("screen_fullscreen_menu_table.lua: Screen height: ", screen_height)
|
||||
|
||||
local new_height = screen_height - status_bar_height
|
||||
local viewport = Geom:new{x=0, y=status_bar_height, w=screen_width, h=new_height}
|
||||
|
||||
if not disabled_fullscreen and Screen.viewport then
|
||||
-- reset touchTranslate to normal
|
||||
Input:registerEventAdjustHook(
|
||||
Input.adjustTouchTranslate,
|
||||
{x = 0 + Screen.viewport.x, y = 0 + Screen.viewport.y})
|
||||
end
|
||||
Screen:setViewport(viewport)
|
||||
if disabled_fullscreen then
|
||||
Input:registerEventAdjustHook(
|
||||
Input.adjustTouchTranslate,
|
||||
{x = 0 - Screen.viewport.x, y = 0 - Screen.viewport.y})
|
||||
end
|
||||
|
||||
G_reader_settings:saveSetting("disabled_fullscreen", not disabled_fullscreen)
|
||||
end,
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
local _ = require("gettext")
|
||||
|
||||
if not isAndroid then return end
|
||||
|
||||
local function isWakeLock()
|
||||
return not G_reader_settings:isTrue("disable_android_wakelock")
|
||||
end
|
||||
|
||||
local function setWakeLock(enable)
|
||||
G_reader_settings:saveSetting("disable_android_wakelock", not enable)
|
||||
end
|
||||
|
||||
return {
|
||||
text = _("Keep screen on"),
|
||||
checked_func = function()
|
||||
return isWakeLock()
|
||||
end,
|
||||
callback = function()
|
||||
local current = isWakeLock()
|
||||
android.setWakeLock(not current)
|
||||
setWakeLock(not current)
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user