2017-06-14 17:32:16 +00:00
local Device = require ( " device " )
2017-09-05 19:30:55 +00:00
if not Device : isKindle ( ) or
2022-06-12 10:19:14 +00:00
( Device.model ~= " KindleVoyage " and
Device.model ~= " KindleOasis " and
Device.model ~= " KindleOasis2 " and
Device.model ~= " KindleOasis3 " ) then
2017-06-14 17:32:16 +00:00
return { disabled = true , }
end
2017-06-24 07:55:31 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2017-06-14 17:32:16 +00:00
local DataStorage = require ( " datastorage " )
local LuaSettings = require ( " luasettings " )
2017-07-28 14:48:19 +00:00
local PluginShare = require ( " pluginshare " )
2017-06-14 17:32:16 +00:00
local UIManager = require ( " ui/uimanager " )
local WidgetContainer = require ( " ui/widget/container/widgetcontainer " )
local logger = require ( " logger " )
2017-06-24 07:55:31 +00:00
local _ = require ( " gettext " )
local T = require ( " ffi/util " ) . template
2017-06-14 17:32:16 +00:00
local AutoFrontlight = {
settings = LuaSettings : open ( DataStorage : getSettingsDir ( ) .. " /autofrontlight.lua " ) ,
settings_id = 0 ,
enabled = false ,
2017-06-24 07:55:31 +00:00
last_brightness = - 1 ,
2017-06-14 17:32:16 +00:00
}
2017-07-28 14:48:19 +00:00
function AutoFrontlight : _schedule ( settings_id )
local enabled = function ( )
if not self.enabled then
logger.dbg ( " AutoFrontlight:_schedule() is disabled " )
return false
end
if settings_id ~= self.settings_id then
logger.dbg ( " AutoFrontlight:_schedule(): registered settings_id " ,
settings_id ,
" does not equal to current one " ,
self.settings_id )
return false
end
2017-06-24 07:55:31 +00:00
2017-07-28 14:48:19 +00:00
return true
end
2017-06-24 07:55:31 +00:00
2017-07-28 14:48:19 +00:00
table.insert ( PluginShare.backgroundJobs , {
when = 2 ,
repeated = enabled ,
executable = function ( )
if enabled ( ) then
self : _action ( )
end
end
} )
2020-08-31 14:48:29 +00:00
local Event = require ( " ui/event " )
UIManager : broadcastEvent ( Event : new ( " BackgroundJobsUpdated " ) )
2017-06-24 07:55:31 +00:00
end
function AutoFrontlight : _action ( )
2017-06-14 17:32:16 +00:00
logger.dbg ( " AutoFrontlight:_action() @ " , os.time ( ) )
2017-06-24 07:55:31 +00:00
local current_level = Device : ambientBrightnessLevel ( )
logger.dbg ( " AutoFrontlight:_action(): Retrieved ambient brightness level: " , current_level )
if self.last_brightness == current_level then
logger.dbg ( " AutoFrontlight:_action(): recorded brightness is same as current level " ,
self.last_brightness )
return
end
if current_level <= 1 then
2017-06-14 17:32:16 +00:00
logger.dbg ( " AutoFrontlight: going to turn on frontlight " )
Device : getPowerDevice ( ) : turnOnFrontlight ( )
2017-06-24 07:55:31 +00:00
elseif current_level >= 3 then
2017-06-14 17:32:16 +00:00
logger.dbg ( " AutoFrontlight: going to turn off frontlight " )
Device : getPowerDevice ( ) : turnOffFrontlight ( )
end
2017-06-24 07:55:31 +00:00
self.last_brightness = current_level
2017-06-14 17:32:16 +00:00
end
function AutoFrontlight : init ( )
2017-06-24 07:55:31 +00:00
self.enabled = self.settings : nilOrTrue ( " enable " )
self.settings_id = self.settings_id + 1
logger.dbg ( " AutoFrontlight:init() self.enabled: " , self.enabled , " with id " , self.settings_id )
self : _schedule ( self.settings_id )
end
function AutoFrontlight : flipSetting ( )
self.settings : flipNilOrTrue ( " enable " )
self : init ( )
end
function AutoFrontlight : onFlushSettings ( )
self.settings : flush ( )
2017-06-14 17:32:16 +00:00
end
AutoFrontlight : init ( )
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local AutoFrontlightWidget = WidgetContainer : extend {
2018-08-17 18:54:11 +00:00
name = " autofrontlight " ,
2017-06-14 17:32:16 +00:00
}
2017-06-24 07:55:31 +00:00
function AutoFrontlightWidget : init ( )
-- self.ui and self.ui.menu are nil in unittests.
if self.ui ~= nil and self.ui . menu ~= nil then
self.ui . menu : registerToMainMenu ( self )
end
end
function AutoFrontlightWidget : flipSetting ( )
AutoFrontlight : flipSetting ( )
end
-- For test only.
function AutoFrontlightWidget : deprecateLastTask ( )
logger.dbg ( " AutoFrontlightWidget:deprecateLastTask() @ " , AutoFrontlight.settings_id )
AutoFrontlight.settings_id = AutoFrontlight.settings_id + 1
end
function AutoFrontlightWidget : addToMainMenu ( menu_items )
menu_items.auto_frontlight = {
text = _ ( " Auto frontlight " ) ,
2023-02-28 07:19:17 +00:00
callback = function ( touchmenu_instance )
2017-06-24 07:55:31 +00:00
UIManager : show ( ConfirmBox : new {
text = T ( _ ( " Auto frontlight detects the brightness of the environment and automatically turn on and off the frontlight. \n Frontlight will be turned off to save battery in bright environment, and turned on in dark environment. \n Do you want to %1 it? " ) ,
AutoFrontlight.enabled and _ ( " disable " ) or _ ( " enable " ) ) ,
ok_text = AutoFrontlight.enabled and _ ( " Disable " ) or _ ( " Enable " ) ,
ok_callback = function ( )
self : flipSetting ( )
2023-02-28 07:19:17 +00:00
touchmenu_instance : updateItems ( )
2017-06-24 07:55:31 +00:00
end
} )
end ,
checked_func = function ( ) return AutoFrontlight.enabled end ,
}
end
function AutoFrontlightWidget : onFlushSettings ( )
AutoFrontlight : onFlushSettings ( )
end
2017-06-14 17:32:16 +00:00
return AutoFrontlightWidget