2020-09-15 18:39:32 +00:00
local FFIUtil = require ( " ffi/util " )
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
2019-07-05 20:57:58 +00:00
local Geom = require ( " ui/geometry " )
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 " )
2020-06-25 19:33:51 +00:00
local util = require ( " util " )
2019-02-06 14:51:50 +00:00
local _ = require ( " gettext " )
2020-09-15 18:39:32 +00:00
local T = FFIUtil.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-06-18 23:57:52 +00:00
local function getCodename ( )
local api = android.app . activity.sdkVersion
local codename = " "
2021-05-08 21:02:35 +00:00
if api > 30 then
codename = " S "
elseif api == 30 then
2019-12-10 21:26:45 +00:00
codename = " R "
elseif api == 29 then
codename = " Q "
elseif api == 28 then
2019-06-18 23:57:52 +00:00
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
end
2020-08-27 19:41:16 +00:00
-- thirdparty app support
local external = require ( " device/thirdparty " ) : new {
dicts = {
{ " Aard2 " , " Aard2 " , false , " itkach.aard2 " , " aard2 " } ,
{ " Alpus " , " Alpus " , false , " com.ngcomputing.fora.android " , " search " } ,
{ " ColorDict " , " ColorDict " , false , " com.socialnmobile.colordict " , " colordict " } ,
{ " Eudic " , " Eudic " , false , " com.eusoft.eudic " , " send " } ,
2020-09-19 18:25:51 +00:00
{ " EudicPlay " , " Eudic (Google Play) " , false , " com.qianyan.eudic " , " send " } ,
2020-08-27 19:41:16 +00:00
{ " Fora " , " Fora Dict " , false , " com.ngc.fora " , " search " } ,
2020-09-19 18:25:51 +00:00
{ " ForaPro " , " Fora Dict Pro " , false , " com.ngc.fora.android " , " search " } ,
2020-08-27 19:41:16 +00:00
{ " GoldenFree " , " GoldenDict Free " , false , " mobi.goldendict.android.free " , " send " } ,
{ " GoldenPro " , " GoldenDict Pro " , false , " mobi.goldendict.android " , " send " } ,
{ " Kiwix " , " Kiwix " , false , " org.kiwix.kiwixmobile " , " text " } ,
2021-03-06 18:32:01 +00:00
{ " LookUp " , " Look Up " , false , " gaurav.lookup " , " send " } ,
{ " LookUpPro " , " Look Up Pro " , false , " gaurav.lookuppro " , " send " } ,
2020-08-27 19:41:16 +00:00
{ " Mdict " , " Mdict " , false , " cn.mdict " , " send " } ,
{ " QuickDic " , " QuickDic " , false , " de.reimardoeffinger.quickdic " , " quickdic " } ,
} ,
check = function ( self , app )
return android.isPackageEnabled ( app )
end ,
}
2019-07-08 12:19:36 +00:00
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 ,
2020-06-19 07:41:50 +00:00
hasExitOptions = 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 ,
2021-08-13 08:57:37 +00:00
hasFrontlight = android.hasLights ,
2020-08-02 20:27:49 +00:00
hasNaturalLight = android.isWarmthDevice ,
2019-08-15 12:49:15 +00:00
canRestart = no ,
2020-06-19 07:41:50 +00:00
canSuspend = no ,
2019-01-17 20:44:15 +00:00
firmware_rev = android.app . activity.sdkVersion ,
2020-06-19 07:41:50 +00:00
home_dir = android.getExternalStoragePath ( ) ,
2017-09-23 21:58:34 +00:00
display_dpi = android.lib . AConfiguration_getDensity ( android.app . config ) ,
2019-10-08 16:15:43 +00:00
isHapticFeedbackEnabled = yes ,
2018-02-16 20:44:10 +00:00
hasClipboard = yes ,
2021-05-08 21:02:35 +00:00
hasOTAUpdates = android.ota . isEnabled ,
2021-05-10 18:36:22 +00:00
hasOTARunning = function ( ) return android.ota . isRunning end ,
2020-09-28 23:06:41 +00:00
hasFastWifiStatusQuery = yes ,
2021-04-19 07:04:31 +00:00
hasSystemFonts = yes ,
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
2021-06-07 15:13:34 +00:00
return android.openLink ( link )
2019-03-20 16:28:19 +00:00
end ,
2019-12-21 14:59:23 +00:00
canImportFiles = function ( ) return android.app . activity.sdkVersion >= 19 end ,
2020-11-27 19:51:40 +00:00
hasExternalSD = function ( ) return android.getExternalSdPath ( ) end ,
2019-12-21 14:59:23 +00:00
importFile = function ( path ) android.importFile ( path ) end ,
2020-01-05 11:56:01 +00:00
canShareText = yes ,
doShareText = function ( text ) android.sendText ( text ) end ,
2019-12-21 14:59:23 +00:00
2019-07-08 12:19:36 +00:00
canExternalDictLookup = yes ,
2020-08-27 19:41:16 +00:00
getExternalDictLookupList = function ( ) return external.dicts end ,
2019-07-08 12:19:36 +00:00
doExternalDictLookup = function ( self , text , method , callback )
2020-08-27 19:41:16 +00:00
external.when_back_callback = callback
local _ , app , action = external : checkMethod ( " dict " , method )
2020-11-18 15:43:20 +00:00
if action then
2020-08-27 19:41:16 +00:00
android.dictLookup ( text , app , action )
2019-07-08 12:19:36 +00:00
end
end ,
2020-12-24 01:37:03 +00:00
-- Android is very finicky, and the LuaJIT allocator has a tremendously hard time getting the system
-- to allocate memory for it in the very specific memory range it requires to store generated code (mcode).
-- Failure to do so at runtime (mcode_alloc) will *tank* performance
-- (much worse than if the JIT were simply disabled).
-- The first line of defense is left to android-luajit-launcher,
-- which will try to grab the full mcode region in one go right at startup.
-- The second line of defense is *this* flag, which disables the JIT in a few code-hungry modules,
-- but not necessarily performance critical ones, to hopefully limit the amount of mcode memory
-- required for those modules where it *really* matters (e.g., ffi/blitbuffer.lua).
-- This is also why we try to actually avoid entering actual loops in the Lua blitter on Android,
-- and instead attempt to do most of everything via the C implementation.
2021-01-02 02:00:39 +00:00
-- NOTE: Since https://github.com/koreader/android-luajit-launcher/pull/283, we've patched LuaJIT
-- to ensure that the initial mcode alloc works, and sticks around, which is why this is no longer enabled.
should_restrict_JIT = false ,
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 )
2021-05-31 20:19:24 +00:00
local Event = require ( " ui/event " )
2020-07-08 12:42:19 +00:00
local UIManager = require ( " ui/uimanager " )
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
2021-08-12 22:46:59 +00:00
UIManager : broadcastEvent ( Event : new ( " FlushSettings " ) )
2020-10-22 18:18:36 +00:00
elseif ev.code == C.APP_CMD_DESTROY then
UIManager : quit ( )
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 ( )
2021-04-15 15:34:24 +00:00
elseif ev.code == C.APP_CMD_LOST_FOCUS
or ev.code == C.APP_CMD_TERM_WINDOW then
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
this.device . input : resetState ( )
2020-07-08 12:42:19 +00:00
elseif ev.code == C.APP_CMD_CONFIG_CHANGED then
-- orientation and size changes
if android.screen . width ~= android.getScreenWidth ( )
or android.screen . height ~= android.getScreenHeight ( ) then
this.device . screen : resize ( )
local new_size = this.device . screen : getSize ( )
logger.info ( " Resizing screen to " , new_size )
2020-10-13 00:03:20 +00:00
local FileManager = require ( " apps/filemanager/filemanager " )
2020-07-08 12:42:19 +00:00
UIManager : broadcastEvent ( Event : new ( " SetDimensions " , new_size ) )
UIManager : broadcastEvent ( Event : new ( " ScreenResize " , new_size ) )
UIManager : broadcastEvent ( Event : new ( " RedrawCurrentPage " ) )
2020-10-13 00:03:20 +00:00
if FileManager.instance then
FileManager.instance : reinit ( FileManager.instance . path ,
FileManager.instance . focused_file )
UIManager : setDirty ( FileManager.instance . banner , function ( )
return " ui " , FileManager.instance . banner.dimen
end )
end
2020-07-08 12:42:19 +00:00
end
-- to-do: keyboard connected, disconnected
2021-05-31 18:34:26 +00:00
elseif ev.code == C.APP_CMD_RESUME then
2021-06-20 13:42:00 +00:00
if not android.prop . brokenLifecycle then
UIManager : broadcastEvent ( Event : new ( " Resume " ) )
end
2020-08-27 19:41:16 +00:00
if external.when_back_callback then
external.when_back_callback ( )
external.when_back_callback = nil
2019-07-08 12:19:36 +00:00
end
2021-05-08 21:02:35 +00:00
if android.ota . isPending then
UIManager : scheduleIn ( 0.1 , self.install )
2019-12-21 14:59:23 +00:00
else
2021-05-08 21:02:35 +00:00
local new_file = android.getIntent ( )
if new_file ~= nil and lfs.attributes ( new_file , " mode " ) == " file " then
-- we cannot blit to a window here since we have no focus yet.
local InfoMessage = require ( " ui/widget/infomessage " )
local BD = require ( " ui/bidi " )
UIManager : scheduleIn ( 0.1 , function ( )
UIManager : show ( InfoMessage : new {
text = T ( _ ( " Opening file '%1'. " ) , BD.filepath ( new_file ) ) ,
timeout = 0.0 ,
} )
2019-12-21 14:59:23 +00:00
end )
2021-05-08 21:02:35 +00:00
UIManager : scheduleIn ( 0.2 , function ( )
require ( " apps/reader/readerui " ) : doShowReader ( new_file )
end )
else
-- check if we're resuming from importing content.
local content_path = android.getLastImportedPath ( )
if content_path ~= nil then
local FileManager = require ( " apps/filemanager/filemanager " )
UIManager : scheduleIn ( 0.5 , function ( )
if FileManager.instance then
FileManager.instance : onRefresh ( )
else
FileManager : showFiles ( content_path )
end
end )
end
2019-12-21 14:59:23 +00:00
end
2019-01-06 17:14:06 +00:00
end
2021-05-31 20:19:24 +00:00
elseif ev.code == C.APP_CMD_PAUSE then
2021-06-20 13:42:00 +00:00
if not android.prop . brokenLifecycle then
UIManager : broadcastEvent ( Event : new ( " Suspend " ) )
end
2021-04-04 10:17:09 +00:00
elseif ev.code == C.AEVENT_POWER_CONNECTED then
UIManager : broadcastEvent ( Event : new ( " Charging " ) )
elseif ev.code == C.AEVENT_POWER_DISCONNECTED then
UIManager : broadcastEvent ( Event : new ( " NotCharging " ) )
2021-05-08 21:02:35 +00:00
elseif ev.code == C.AEVENT_DOWNLOAD_COMPLETE then
2021-05-10 18:36:22 +00:00
android.ota . isRunning = false
2021-05-08 21:02:35 +00:00
if android.isResumed ( ) then
self : install ( )
else
android.ota . isPending = true
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-10-08 16:15:43 +00:00
-- check if we use custom timeouts
2019-07-26 21:04:20 +00:00
if android.needsWakelocks ( ) then
2019-10-08 16:15:43 +00:00
android.timeout . set ( C.AKEEP_SCREEN_ON_ENABLED )
2019-07-26 21:04:20 +00:00
else
local timeout = G_reader_settings : readSetting ( " android_screen_timeout " )
2019-10-08 16:15:43 +00:00
if timeout then
if timeout == C.AKEEP_SCREEN_ON_ENABLED
2020-11-04 18:15:07 +00:00
or timeout > C.AKEEP_SCREEN_ON_DISABLED
and android.settings . hasPermission ( " settings " )
then
2019-10-08 16:15:43 +00:00
android.timeout . set ( timeout )
2019-08-14 11:40:42 +00:00
end
2019-07-26 21:04:20 +00:00
end
2019-01-10 02:11:06 +00:00
end
2019-07-05 20:57:58 +00:00
-- check if we disable fullscreen support
if G_reader_settings : isTrue ( " disable_android_fullscreen " ) then
self : toggleFullscreen ( )
end
2019-08-01 16:27:24 +00:00
2020-05-05 09:18:18 +00:00
-- check if we allow haptic feedback in spite of system settings
if G_reader_settings : isTrue ( " haptic_feedback_override " ) then
android.setHapticOverride ( true )
end
2019-08-01 16:27:24 +00:00
-- check if we ignore volume keys and then they're forwarded to system services.
if G_reader_settings : isTrue ( " android_ignore_volume_keys " ) then
2020-05-05 09:18:18 +00:00
android.setVolumeKeysIgnored ( true )
2019-08-01 16:27:24 +00:00
end
2019-07-05 20:57:58 +00:00
2020-06-15 05:43:37 +00:00
-- check if we ignore the back button completely
if G_reader_settings : isTrue ( " android_ignore_back_button " ) then
android.setBackButtonIgnored ( true )
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 )
2020-02-07 15:36:17 +00:00
android.openWifiSettings ( )
2017-10-21 20:27:09 +00:00
end
2019-02-17 15:22:41 +00:00
function NetworkMgr : turnOffWifi ( complete_callback )
2020-02-07 15:36:17 +00:00
android.openWifiSettings ( )
2017-10-21 20:27:09 +00:00
end
2020-02-07 15:36:17 +00:00
function NetworkMgr : openSettings ( )
android.openWifiSettings ( )
end
function NetworkMgr : isWifiOn ( )
local ok = android.getNetworkInfo ( )
ok = tonumber ( ok )
if not ok then return false end
return ok == 1
2017-10-28 15:51:34 +00:00
end
2017-10-21 20:27:09 +00:00
end
2019-10-08 16:15:43 +00:00
function Device : performHapticFeedback ( type )
android.hapticFeedback ( C [ " AHAPTIC_ " .. type ] )
end
2020-06-13 10:57:08 +00:00
function Device : setIgnoreInput ( enable )
android.setIgnoreInput ( enable )
end
2019-02-06 14:51:50 +00:00
function Device : retrieveNetworkInfo ( )
2020-02-07 15:36:17 +00:00
local ok , type = android.getNetworkInfo ( )
ok , type = tonumber ( ok ) , tonumber ( type )
if not ok or not type or type == C.ANETWORK_NONE then
2019-02-06 14:51:50 +00:00
return _ ( " Not connected " )
else
2020-02-07 15:36:17 +00:00
if type == C.ANETWORK_WIFI then
return _ ( " Connected to Wi-Fi " )
elseif type == C.ANETWORK_MOBILE then
return _ ( " Connected to mobile data network " )
elseif type == C.ANETWORK_ETHERNET then
return _ ( " Connected to Ethernet " )
elseif type == C.ANETWORK_BLUETOOTH then
return _ ( " Connected to Bluetooth " )
elseif type == C.ANETWORK_VPN then
return _ ( " Connected to VPN " )
end
return _ ( " Unknown connection " )
2019-02-06 14:51:50 +00:00
end
2019-07-05 20:57:58 +00:00
end
function Device : setViewport ( x , y , w , h )
logger.info ( string.format ( " Switching viewport to new geometry [x=%d,y=%d,w=%d,h=%d] " , x , y , w , h ) )
local viewport = Geom : new { x = x , y = y , w = w , h = h }
self.screen : setViewport ( viewport )
end
function Device : toggleFullscreen ( )
local api = android.app . activity.sdkVersion
if api >= 19 then
logger.dbg ( " ignoring fullscreen toggle, reason: always in immersive mode " )
elseif api < 19 and api >= 17 then
local width = android.getScreenWidth ( )
local height = android.getScreenHeight ( )
2021-01-13 20:43:46 +00:00
-- NOTE: Since we don't do HW rotation here, this should always match width
local available_width = android.getScreenAvailableWidth ( )
2019-07-05 20:57:58 +00:00
local available_height = android.getScreenAvailableHeight ( )
local is_fullscreen = android.isFullscreen ( )
android.setFullscreen ( not is_fullscreen )
G_reader_settings : saveSetting ( " disable_android_fullscreen " , is_fullscreen )
is_fullscreen = android.isFullscreen ( )
if is_fullscreen then
self : setViewport ( 0 , 0 , width , height )
else
2021-01-13 20:43:46 +00:00
self : setViewport ( 0 , 0 , available_width , available_height )
2019-07-05 20:57:58 +00:00
end
else
logger.dbg ( " ignoring fullscreen toggle, reason: legacy api " .. api )
end
2019-02-06 14:51:50 +00:00
end
2019-06-18 23:57:52 +00:00
function Device : info ( )
local is_eink , eink_platform = android.isEink ( )
2020-07-08 12:42:19 +00:00
local product_type = android.getPlatformName ( )
2019-01-17 20:44:15 +00:00
2019-06-18 23:57:52 +00:00
local common_text = T ( _ ( " %1 \n \n OS: Android %2, api %3 \n Build flavor: %4 \n " ) ,
android.prop . product , getCodename ( ) , Device.firmware_rev , android.prop . flavor )
2019-01-17 20:44:15 +00:00
2020-07-08 12:42:19 +00:00
local platform_text = " "
if product_type ~= " android " then
2020-07-08 15:55:17 +00:00
platform_text = " \n " .. T ( _ ( " Device type: %1 " ) , product_type ) .. " \n "
2020-07-08 12:42:19 +00:00
end
2019-06-18 23:57:52 +00:00
local eink_text = " "
if is_eink then
2020-07-08 15:55:17 +00:00
eink_text = " \n " .. T ( _ ( " E-ink display supported. \n Platform: %1 " ) , eink_platform ) .. " \n "
2019-06-18 23:57:52 +00:00
end
local wakelocks_text = " "
if android.needsWakelocks ( ) then
2020-07-08 15:55:17 +00:00
wakelocks_text = " \n " .. _ ( " This device needs CPU, screen and touchscreen always on. \n Screen timeout will be ignored while the app is in the foreground! " ) .. " \n "
2019-01-17 20:44:15 +00:00
end
2020-07-08 12:42:19 +00:00
return common_text .. platform_text .. eink_text .. wakelocks_text
2019-06-18 23:57:52 +00:00
end
2020-01-04 19:53:49 +00:00
function Device : epdTest ( )
android.einkTest ( )
end
2019-06-18 23:57:52 +00:00
function Device : exit ( )
android.LOGI ( string.format ( " Stopping %s main activity " , android.prop . name ) ) ;
android.lib . ANativeActivity_finish ( android.app . activity )
2019-01-17 20:44:15 +00:00
end
2020-06-25 19:33:51 +00:00
function Device : canExecuteScript ( file )
local file_ext = string.lower ( util.getFileNameSuffix ( file ) )
if android.prop . flavor ~= " fdroid " and file_ext == " sh " then
return true
end
end
2020-11-10 14:00:56 +00:00
function Device : isValidPath ( path )
2021-04-22 06:38:49 +00:00
-- the fast check
if android.isPathInsideSandbox ( path ) then
return true
end
-- the thorough check
local real_ext_storage = FFIUtil.realpath ( android.getExternalStoragePath ( ) )
local real_path = FFIUtil.realpath ( path )
if real_path then
return real_path : sub ( 1 , # real_ext_storage ) == real_ext_storage
else
return false
end
2020-11-10 14:00:56 +00:00
end
2021-04-15 15:34:24 +00:00
function Device : showLightDialog ( )
-- Delay it until next tick so that the event loop gets a chance to drain the input queue,
-- and consume the APP_CMD_LOST_FOCUS event.
-- This helps prevent ANRs on Tolino (c.f., #6583 & #7552).
local UIManager = require ( " ui/uimanager " )
UIManager : nextTick ( function ( ) self : _showLightDialog ( ) end )
2020-08-28 11:40:35 +00:00
end
2021-04-15 15:34:24 +00:00
function Device : _showLightDialog ( )
2020-08-02 20:27:49 +00:00
local title = android.isEink ( ) and _ ( " Frontlight settings " ) or _ ( " Light settings " )
android.lights . showDialog ( title , _ ( " Brightness " ) , _ ( " Warmth " ) , _ ( " OK " ) , _ ( " Cancel " ) )
2020-08-28 11:40:35 +00:00
2020-08-02 20:27:49 +00:00
local action = android.lights . dialogState ( )
if action == C.ALIGHTS_DIALOG_OK then
self.powerd . fl_intensity = self.powerd : frontlightIntensityHW ( )
logger.dbg ( " Dialog OK, brightness: " .. self.powerd . fl_intensity )
if android.isWarmthDevice ( ) then
self.powerd . fl_warmth = self.powerd : getWarmth ( )
logger.dbg ( " Dialog OK, warmth: " .. self.powerd . fl_warmth )
end
2020-08-29 14:46:59 +00:00
local Event = require ( " ui/event " )
local UIManager = require ( " ui/uimanager " )
UIManager : broadcastEvent ( Event : new ( " FrontlightStateChanged " ) )
2020-08-02 20:27:49 +00:00
elseif action == C.ALIGHTS_DIALOG_CANCEL then
logger.dbg ( " Dialog Cancel, brightness: " .. self.powerd . fl_intensity )
self.powerd : setIntensityHW ( self.powerd . fl_intensity )
if android.isWarmthDevice ( ) then
logger.dbg ( " Dialog Cancel, warmth: " .. self.powerd . fl_warmth )
self.powerd : setWarmth ( self.powerd . fl_warmth )
end
end
end
2020-12-10 15:59:14 +00:00
function Device : untar ( archive , extract_to )
return android.untar ( archive , extract_to )
end
2021-05-15 23:27:14 +00:00
function Device : download ( link , name , ok_text )
local ConfirmBox = require ( " ui/widget/confirmbox " )
local InfoMessage = require ( " ui/widget/infomessage " )
2021-05-31 20:19:24 +00:00
local UIManager = require ( " ui/uimanager " )
2021-05-15 23:27:14 +00:00
local ok = android.download ( link , name )
if ok == C.ADOWNLOAD_EXISTS then
self : install ( )
elseif ok == C.ADOWNLOAD_OK then
android.ota . isRunning = true
UIManager : show ( InfoMessage : new {
text = ok_text ,
timeout = 3 ,
} )
elseif ok == C.ADOWNLOAD_FAILED then
UIManager : show ( ConfirmBox : new {
text = _ ( " Your device seems to be unable to download packages. \n Retry using the browser? " ) ,
ok_text = _ ( " Retry " ) ,
ok_callback = function ( ) self : openLink ( link ) end ,
} )
end
end
2021-05-08 21:02:35 +00:00
function Device : install ( )
2021-05-31 18:34:26 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2021-05-31 20:19:24 +00:00
local Event = require ( " ui/event " )
local UIManager = require ( " ui/uimanager " )
2021-05-08 21:02:35 +00:00
UIManager : show ( ConfirmBox : new {
2021-05-09 07:17:20 +00:00
text = _ ( " Update is ready. Install it now? " ) ,
2021-05-08 21:02:35 +00:00
ok_text = _ ( " Install " ) ,
ok_callback = function ( )
2021-08-12 22:46:59 +00:00
UIManager : broadcastEvent ( Event : new ( " FlushSettings " ) )
UIManager : tickAfterNext ( function ( )
android.ota . install ( )
android.ota . isPending = false
end )
2021-05-08 21:02:35 +00:00
end ,
} )
end
2021-04-22 06:38:49 +00:00
-- todo: Wouldn't we like an android.deviceIdentifier() method, so we can use better default paths?
function Device : getDefaultCoverPath ( )
if android.prop . product == " ntx_6sl " then -- Tolino HD4 and other
return android.getExternalStoragePath ( ) .. " /suspend_others.jpg "
else
return android.getExternalStoragePath ( ) .. " /cover.jpg "
end
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