2014-10-30 18:42:18 +00:00
|
|
|
local Generic = require("device/generic/device")
|
2019-02-06 14:51:50 +00:00
|
|
|
local A, android = pcall(require, "android") -- luacheck: ignore
|
2014-10-30 18:42:18 +00:00
|
|
|
local ffi = require("ffi")
|
2018-06-02 16:10:55 +00:00
|
|
|
local C = ffi.C
|
2019-01-06 17:14:06 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2019-02-06 14:51:50 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local T = require("ffi/util").template
|
2014-10-30 18:42:18 +00:00
|
|
|
|
|
|
|
local function yes() return true end
|
2015-09-27 14:29:59 +00:00
|
|
|
local function no() return false end
|
2014-10-30 18:42:18 +00:00
|
|
|
|
2019-03-03 00:07:56 +00:00
|
|
|
local function canUpdateApk()
|
|
|
|
-- disable updates on fdroid builds, since they manage their own repo.
|
2019-04-17 18:04:07 +00:00
|
|
|
return (android.prop.flavor ~= "fdroid")
|
2019-03-03 00:07:56 +00:00
|
|
|
end
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = Generic:new{
|
2019-04-17 18:04:07 +00:00
|
|
|
isAndroid = yes,
|
|
|
|
model = android.prop.product,
|
2015-03-29 00:59:51 +00:00
|
|
|
hasKeys = yes,
|
2015-09-27 14:29:59 +00:00
|
|
|
hasDPad = no,
|
2019-02-08 09:45:09 +00:00
|
|
|
hasEinkScreen = function() return android.isEink() end,
|
2019-03-03 14:48:23 +00:00
|
|
|
hasColorScreen = function() return not android.isEink() end,
|
2016-06-23 17:50:24 +00:00
|
|
|
hasFrontlight = yes,
|
2019-01-17 20:44:15 +00:00
|
|
|
firmware_rev = android.app.activity.sdkVersion,
|
2017-09-23 21:58:34 +00:00
|
|
|
display_dpi = android.lib.AConfiguration_getDensity(android.app.config),
|
2018-02-16 20:44:10 +00:00
|
|
|
hasClipboard = yes,
|
2019-03-03 00:07:56 +00:00
|
|
|
hasOTAUpdates = canUpdateApk,
|
2019-03-31 17:19:07 +00:00
|
|
|
canOpenLink = yes,
|
2019-03-20 16:28:19 +00:00
|
|
|
openLink = function(self, link)
|
|
|
|
if not link or type(link) ~= "string" then return end
|
|
|
|
return android.openLink(link) == 0
|
|
|
|
end,
|
2019-02-18 16:01:00 +00:00
|
|
|
--[[
|
|
|
|
Disable jit on some modules on android to make koreader on Android more stable.
|
|
|
|
|
|
|
|
The strategy here is that we only use precious mcode memory (jitting)
|
|
|
|
on deep loops like the several blitting methods in blitbuffer.lua and
|
|
|
|
the pixel-copying methods in mupdf.lua. So that a small amount of mcode
|
|
|
|
memory (64KB) allocated when koreader is launched in the android.lua
|
|
|
|
is enough for the program and it won't need to jit other parts of lua
|
|
|
|
code and thus won't allocate mcode memory any more which by our
|
|
|
|
observation will be harder and harder as we run koreader.
|
|
|
|
]]--
|
|
|
|
should_restrict_JIT = true,
|
2014-10-30 18:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Device:init()
|
2016-12-29 08:10:38 +00:00
|
|
|
self.screen = require("ffi/framebuffer_android"):new{device = self, debug = logger.dbg}
|
2014-10-30 18:42:18 +00:00
|
|
|
self.powerd = require("device/android/powerd"):new{device = self}
|
|
|
|
self.input = require("device/input"):new{
|
|
|
|
device = self,
|
|
|
|
event_map = require("device/android/event_map"),
|
2016-04-03 04:52:30 +00:00
|
|
|
handleMiscEv = function(this, ev)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("Android application event", ev.code)
|
2018-06-02 16:10:55 +00:00
|
|
|
if ev.code == C.APP_CMD_SAVE_STATE then
|
2014-10-30 18:42:18 +00:00
|
|
|
return "SaveState"
|
2019-01-10 02:11:06 +00:00
|
|
|
elseif ev.code == C.APP_CMD_GAINED_FOCUS
|
|
|
|
or ev.code == C.APP_CMD_INIT_WINDOW
|
|
|
|
or ev.code == C.APP_CMD_WINDOW_REDRAW_NEEDED then
|
2019-03-05 11:25:04 +00:00
|
|
|
this.device.screen:_updateWindow()
|
2019-01-06 17:14:06 +00:00
|
|
|
elseif ev.code == C.APP_CMD_RESUME then
|
|
|
|
local new_file = android.getIntent()
|
|
|
|
if new_file ~= nil and lfs.attributes(new_file, "mode") == "file" then
|
2019-03-05 18:22:45 +00:00
|
|
|
-- we cannot blit to a window here since we have no focus yet.
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Opening file '%1'."), new_file),
|
|
|
|
timeout = 0.0,
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
UIManager:scheduleIn(0.2, function()
|
|
|
|
require("apps/reader/readerui"):doShowReader(new_file)
|
|
|
|
end)
|
2019-01-06 17:14:06 +00:00
|
|
|
end
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
end,
|
2018-02-16 20:44:10 +00:00
|
|
|
hasClipboardText = function()
|
|
|
|
return android.hasClipboardText()
|
|
|
|
end,
|
|
|
|
getClipboardText = function()
|
|
|
|
return android.getClipboardText()
|
|
|
|
end,
|
|
|
|
setClipboardText = function(text)
|
|
|
|
return android.setClipboardText(text)
|
|
|
|
end,
|
2014-10-30 18:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- check if we have a keyboard
|
2017-09-23 21:58:34 +00:00
|
|
|
if android.lib.AConfiguration_getKeyboard(android.app.config)
|
2018-06-02 16:10:55 +00:00
|
|
|
== C.ACONFIGURATION_KEYBOARD_QWERTY
|
2014-10-30 18:42:18 +00:00
|
|
|
then
|
|
|
|
self.hasKeyboard = yes
|
|
|
|
end
|
|
|
|
-- check if we have a touchscreen
|
2017-09-23 21:58:34 +00:00
|
|
|
if android.lib.AConfiguration_getTouchscreen(android.app.config)
|
2018-06-02 16:10:55 +00:00
|
|
|
~= C.ACONFIGURATION_TOUCHSCREEN_NOTOUCH
|
2014-10-30 18:42:18 +00:00
|
|
|
then
|
|
|
|
self.isTouchDevice = yes
|
|
|
|
end
|
|
|
|
|
2019-01-21 21:44:32 +00:00
|
|
|
-- check if we enabled support for wakelocks
|
|
|
|
if G_reader_settings:isTrue("enable_android_wakelock") then
|
|
|
|
android.setWakeLock(true)
|
2019-01-10 02:11:06 +00:00
|
|
|
end
|
|
|
|
|
2014-11-24 08:52:01 +00:00
|
|
|
Generic.init(self)
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
|
2017-10-21 20:27:09 +00:00
|
|
|
function Device:initNetworkManager(NetworkMgr)
|
2019-02-17 15:22:41 +00:00
|
|
|
function NetworkMgr:turnOnWifi(complete_callback)
|
2017-10-21 20:27:09 +00:00
|
|
|
android.setWifiEnabled(true)
|
2019-02-17 15:22:41 +00:00
|
|
|
if complete_callback then
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
UIManager:scheduleIn(1, complete_callback)
|
|
|
|
end
|
2017-10-21 20:27:09 +00:00
|
|
|
end
|
|
|
|
|
2019-02-17 15:22:41 +00:00
|
|
|
function NetworkMgr:turnOffWifi(complete_callback)
|
2017-10-21 20:27:09 +00:00
|
|
|
android.setWifiEnabled(false)
|
2019-02-17 15:22:41 +00:00
|
|
|
if complete_callback then
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
UIManager:scheduleIn(1, complete_callback)
|
|
|
|
end
|
2017-10-21 20:27:09 +00:00
|
|
|
end
|
2017-10-28 15:51:34 +00:00
|
|
|
NetworkMgr.isWifiOn = function()
|
|
|
|
return android.isWifiEnabled()
|
|
|
|
end
|
2017-10-21 20:27:09 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 14:51:50 +00:00
|
|
|
function Device:retrieveNetworkInfo()
|
|
|
|
local ssid, ip, gw = android.getNetworkInfo()
|
2019-02-07 14:57:44 +00:00
|
|
|
if ip == "0" or gw == "0" then
|
2019-02-06 14:51:50 +00:00
|
|
|
return _("Not connected")
|
|
|
|
else
|
|
|
|
return T(_("Connected to %1\n IP address: %2\n gateway: %3"), ssid, ip, gw)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-17 20:44:15 +00:00
|
|
|
function Device:exit()
|
2019-04-17 18:04:07 +00:00
|
|
|
android.LOGI(string.format("Stopping %s main activity", android.prop.name));
|
2019-01-17 20:44:15 +00:00
|
|
|
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
|
|
|
|
|
2019-03-03 00:07:56 +00:00
|
|
|
android.LOGI(string.format("Android %s - %s (API %d) - flavor: %s",
|
2019-04-17 18:04:07 +00:00
|
|
|
android.prop.version, getCodename(), Device.firmware_rev, android.prop.flavor))
|
2019-01-17 20:44:15 +00:00
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
return Device
|