2021-09-25 09:02:10 +00:00
|
|
|
--[[--
|
|
|
|
Plugin for setting screen warmth based on the sun position and/or a time schedule
|
2021-11-01 17:59:15 +00:00
|
|
|
|
|
|
|
@module koplugin.autowarmth
|
|
|
|
--]]--
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2023-08-02 23:01:38 +00:00
|
|
|
local CheckButton = require("ui/widget/checkbutton")
|
2021-09-25 09:02:10 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2022-07-14 21:25:11 +00:00
|
|
|
local Device = require("device")
|
2021-12-05 08:52:02 +00:00
|
|
|
local DateTimeWidget = require("ui/widget/datetimewidget")
|
2021-09-25 09:02:10 +00:00
|
|
|
local DoubleSpinWidget = require("/ui/widget/doublespinwidget")
|
|
|
|
local DeviceListener = require("device/devicelistener")
|
|
|
|
local Dispatcher = require("dispatcher")
|
2022-07-14 21:25:11 +00:00
|
|
|
local Event = require("ui/event")
|
2021-09-25 09:02:10 +00:00
|
|
|
local FFIUtil = require("ffi/util")
|
2022-07-14 21:25:11 +00:00
|
|
|
local Font = require("ui/font")
|
2021-09-25 09:02:10 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
2024-06-01 06:42:09 +00:00
|
|
|
local Math = require("optmath")
|
2021-09-25 09:02:10 +00:00
|
|
|
local Notification = require("ui/widget/notification")
|
|
|
|
local SpinWidget = require("ui/widget/spinwidget")
|
|
|
|
local SunTime = require("suntime")
|
2021-10-03 08:40:46 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
2021-09-25 09:02:10 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
2022-03-29 20:59:10 +00:00
|
|
|
local logger = require("logger")
|
2021-09-25 09:02:10 +00:00
|
|
|
local _ = require("gettext")
|
2022-05-23 22:25:50 +00:00
|
|
|
local C_ = _.pgettext
|
2022-07-28 04:22:25 +00:00
|
|
|
local Powerd = Device.powerd
|
2021-09-25 09:02:10 +00:00
|
|
|
local T = FFIUtil.template
|
|
|
|
local Screen = require("device").screen
|
2022-11-17 04:53:35 +00:00
|
|
|
local datetime = require("datetime")
|
2021-09-25 09:02:10 +00:00
|
|
|
|
|
|
|
local activate_sun = 1
|
|
|
|
local activate_schedule = 2
|
|
|
|
local activate_closer_noon = 3
|
2021-10-26 07:54:21 +00:00
|
|
|
local activate_closer_midnight = 4
|
2021-09-25 09:02:10 +00:00
|
|
|
|
|
|
|
local midnight_index = 11
|
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local device_max_warmth = Device:hasNaturalLight() and Powerd.fl_warmth_max or 100
|
2022-10-10 20:21:27 +00:00
|
|
|
local device_warmth_fit_scale = device_max_warmth * (1/100)
|
2021-09-25 09:02:10 +00:00
|
|
|
|
|
|
|
local function frac(x)
|
|
|
|
return x - math.floor(x)
|
|
|
|
end
|
|
|
|
|
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 AutoWarmth = WidgetContainer:extend{
|
2021-09-25 09:02:10 +00:00
|
|
|
name = "autowarmth",
|
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
|
|
|
sched_times_s = nil, -- array
|
|
|
|
sched_warmths = nil, -- array
|
2022-11-04 08:31:06 +00:00
|
|
|
|
|
|
|
-- Static member that shall survive reloading of the plugin but not a restart
|
2023-06-13 05:51:31 +00:00
|
|
|
fl_user_toggle = false, -- true/false if someone (AutoWarmth, gesture ...) has toggled the frontlight
|
|
|
|
fl_turned_off = false, -- the frontlight toggle state
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function AutoWarmth:init()
|
|
|
|
self:onDispatcherRegisterActions()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
|
2021-10-03 08:28:09 +00:00
|
|
|
self.easy_mode = G_reader_settings:nilOrTrue("autowarmth_easy_mode")
|
|
|
|
self.activate = G_reader_settings:readSetting("autowarmth_activate") or 0
|
|
|
|
self.location = G_reader_settings:readSetting("autowarmth_location") or "Geysir"
|
|
|
|
self.latitude = G_reader_settings:readSetting("autowarmth_latitude") or 64.31 --great Geysir in Iceland
|
|
|
|
self.longitude = G_reader_settings:readSetting("autowarmth_longitude") or -20.30
|
|
|
|
self.altitude = G_reader_settings:readSetting("autowarmth_altitude") or 200
|
|
|
|
self.timezone = G_reader_settings:readSetting("autowarmth_timezone") or 0
|
2022-06-01 14:22:42 +00:00
|
|
|
self.scheduler_times = G_reader_settings:readSetting("autowarmth_scheduler_times")
|
|
|
|
or {0.0, 5.5, 6.0, 6.5, 7.0, 13.0, 21.5, 22.0, 22.5, 23.0, 24.0}
|
|
|
|
self.warmth = G_reader_settings:readSetting("autowarmth_warmth")
|
2021-10-03 08:28:09 +00:00
|
|
|
or { 90, 90, 80, 60, 20, 20, 20, 60, 80, 90, 90}
|
2022-11-11 22:47:51 +00:00
|
|
|
|
2022-07-28 12:57:02 +00:00
|
|
|
self.fl_off_during_day = G_reader_settings:readSetting("autowarmth_fl_off_during_day")
|
2022-11-11 22:47:51 +00:00
|
|
|
self.fl_off_during_day_offset_s = G_reader_settings:readSetting("autowarmth_fl_off_during_day_offset_s", 0)
|
|
|
|
if self.easy_mode then
|
|
|
|
self.fl_off_during_day_offset_s = 0
|
|
|
|
end
|
2021-10-03 08:28:09 +00:00
|
|
|
|
2022-09-11 17:31:36 +00:00
|
|
|
self.control_warmth = G_reader_settings:nilOrTrue("autowarmth_control_warmth")
|
|
|
|
self.control_nightmode = G_reader_settings:nilOrTrue("autowarmth_control_nightmode")
|
2023-06-09 18:57:49 +00:00
|
|
|
if not Device:hasNaturalLight() then
|
|
|
|
self.control_nightmode = true
|
|
|
|
elseif not self.control_warmth and not self.control_nightmode then
|
2022-09-11 17:31:36 +00:00
|
|
|
logger.dbg("AutoWarmth: autowarmth_control_warmth and autowarmth_control_nightmode are both false, set them to true")
|
|
|
|
self.control_warmth = true
|
|
|
|
self.control_nightmode = true
|
|
|
|
end
|
|
|
|
|
2022-11-02 13:29:36 +00:00
|
|
|
-- Fix entries not in ascending order (only happens by manual editing of settings.reader.lua)
|
2023-11-01 21:10:52 +00:00
|
|
|
local i = 1
|
|
|
|
|
|
|
|
-- Find first not disabled entry. (`<` is OK here.)
|
|
|
|
while i < midnight_index and not self.scheduler_times[i] do
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
while i < midnight_index do
|
|
|
|
local j = i + 1
|
|
|
|
-- Find next not disabled entry
|
|
|
|
while j <= midnight_index and not self.scheduler_times[j] do
|
|
|
|
j = j + 1
|
|
|
|
end
|
|
|
|
-- Fix the found the next not disabled entry if necessary.
|
|
|
|
if j <= midnight_index and self.scheduler_times[j] and
|
|
|
|
self.scheduler_times[i] > self.scheduler_times[j] then
|
|
|
|
|
|
|
|
self.scheduler_times[j] = self.scheduler_times[i]
|
2022-11-02 13:29:36 +00:00
|
|
|
logger.warn("AutoWarmth: scheduling times fixed.")
|
|
|
|
end
|
2023-11-01 21:10:52 +00:00
|
|
|
i = j
|
2022-11-02 13:29:36 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 09:02:10 +00:00
|
|
|
-- schedule recalculation shortly afer midnight
|
|
|
|
self:scheduleMidnightUpdate()
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:onDispatcherRegisterActions()
|
|
|
|
Dispatcher:registerAction("show_ephemeris",
|
|
|
|
{category="none", event="ShowEphemeris", title=_("Show ephemeris"), general=true})
|
|
|
|
Dispatcher:registerAction("auto_warmth_off",
|
|
|
|
{category="none", event="AutoWarmthOff", title=_("Auto warmth off"), screen=true})
|
2024-06-01 06:42:09 +00:00
|
|
|
Dispatcher:registerAction("auto_warmth_activate_sun",
|
|
|
|
{category="none", event="AutoWarmthMode", arg=activate_sun, title=_("Auto warmth use sun position"), screen=true})
|
|
|
|
Dispatcher:registerAction("auto_warmth_activate_schedule",
|
|
|
|
{category="none", event="AutoWarmthMode", arg=activate_schedule, title=_("Auto warmth use schedule"), screen=true})
|
|
|
|
Dispatcher:registerAction("auto_warmth_activate_closer_midnight",
|
|
|
|
{category="none", event="AutoWarmthMode", arg=activate_closer_midnight, title=_("Auto warmth use closer midnight"), screen=true})
|
|
|
|
Dispatcher:registerAction("auto_warmth_activate_closer_noon",
|
|
|
|
{category="none", event="AutoWarmthMode", arg=activate_closer_noon, title=_("Auto warmth use closer noon"), screen=true})
|
|
|
|
|
2021-09-25 09:02:10 +00:00
|
|
|
Dispatcher:registerAction("auto_warmth_cycle_trough",
|
|
|
|
{category="none", event="AutoWarmthMode", title=_("Auto warmth cycle through modes"), screen=true})
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:onShowEphemeris()
|
|
|
|
self:showTimesInfo(_("Information about the sun in"), true, activate_sun, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:onAutoWarmthOff()
|
2024-06-01 06:42:09 +00:00
|
|
|
self:onAutoWarmthMode(0)
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
2024-06-01 06:42:09 +00:00
|
|
|
-- select one mode of autowarmth directly
|
|
|
|
function AutoWarmth:onAutoWarmthMode(forced_method)
|
|
|
|
if forced_method then
|
|
|
|
self.activate = Math.clamp(forced_method, 0, activate_closer_midnight)
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
2024-06-01 06:42:09 +00:00
|
|
|
if self.activate > 0 then
|
|
|
|
self.activate = self.activate - 1
|
|
|
|
else
|
|
|
|
self.activate = activate_closer_midnight
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
local notify_text
|
|
|
|
if self.activate == 0 then
|
|
|
|
notify_text = _("Auto warmth turned off")
|
|
|
|
elseif self.activate == activate_sun then
|
|
|
|
notify_text = _("Auto warmth use sun position")
|
|
|
|
elseif self.activate == activate_schedule then
|
|
|
|
notify_text = _("Auto warmth use schedule")
|
|
|
|
elseif self.activate == activate_closer_midnight then
|
|
|
|
notify_text = _("Auto warmth use whatever is closer to midnight")
|
|
|
|
elseif self.activate == activate_closer_noon then
|
|
|
|
notify_text = _("Auto warmth use whatever is closer to noon")
|
|
|
|
end
|
|
|
|
G_reader_settings:saveSetting("autowarmth_activate", self.activate)
|
|
|
|
Notification:notify(notify_text)
|
|
|
|
self:scheduleMidnightUpdate()
|
|
|
|
end
|
|
|
|
|
2023-04-05 18:54:47 +00:00
|
|
|
function AutoWarmth:_onResume()
|
2023-06-13 05:51:31 +00:00
|
|
|
logger.dbg("AutoWarmth: onResume", AutoWarmth.fl_turned_off, AutoWarmth.fl_user_toggle)
|
2023-04-05 18:54:47 +00:00
|
|
|
|
2021-09-25 09:02:10 +00:00
|
|
|
local resume_date = os.date("*t")
|
|
|
|
|
2023-06-09 18:56:46 +00:00
|
|
|
if self.fl_off_during_day then
|
2023-06-13 05:51:31 +00:00
|
|
|
-- We handle the frontlight state ourself.
|
2023-06-09 18:56:46 +00:00
|
|
|
Powerd.fl_was_on = false
|
|
|
|
end
|
|
|
|
|
2021-09-25 09:02:10 +00:00
|
|
|
-- check if resume and suspend are done on the same day
|
|
|
|
if resume_date.day == SunTime.date.day and resume_date.month == SunTime.date.month
|
|
|
|
and resume_date.year == SunTime.date.year then
|
2022-12-02 20:44:45 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local now_s = SunTime:getTimeInSec(resume_date)
|
2022-12-02 20:44:45 +00:00
|
|
|
self.sched_warmth_index = self.sched_warmth_index - 1 -- scheduleNextWarmth will check this
|
2023-04-05 18:54:47 +00:00
|
|
|
self:scheduleNextWarmthChange(true)
|
2022-12-02 20:44:45 +00:00
|
|
|
self:scheduleToggleFrontlight(now_s) -- reset user toggles at sun set or sun rise
|
2023-06-13 05:51:31 +00:00
|
|
|
if AutoWarmth.fl_user_toggle then
|
|
|
|
self:setFrontlight(not AutoWarmth.fl_turned_off, true) -- keep user toggle state
|
|
|
|
else
|
|
|
|
self:toggleFrontlight(now_s) -- no user toggle
|
|
|
|
end
|
2022-07-28 04:22:25 +00:00
|
|
|
-- Reschedule 1sec after midnight
|
|
|
|
UIManager:scheduleIn(24*3600 + 1 - now_s, self.scheduleMidnightUpdate, self)
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
2023-04-05 18:54:47 +00:00
|
|
|
self:scheduleMidnightUpdate(true) -- resume is on the other day, do all calcs again
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
function AutoWarmth:_onSuspend()
|
2022-11-02 13:29:36 +00:00
|
|
|
logger.dbg("AutoWarmth: onSuspend")
|
2022-07-06 14:56:30 +00:00
|
|
|
UIManager:unschedule(self.scheduleMidnightUpdate)
|
|
|
|
UIManager:unschedule(self.setWarmth)
|
2022-11-11 22:47:51 +00:00
|
|
|
UIManager:unschedule(self.setFrontlight)
|
2023-06-09 18:56:46 +00:00
|
|
|
UIManager:unschedule(self.scheduleNextWarmthChange)
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
2022-11-03 05:32:47 +00:00
|
|
|
function AutoWarmth:_onToggleNightMode()
|
2022-11-04 08:31:06 +00:00
|
|
|
logger.dbg("AutoWarmth: onToggleNightMode")
|
2022-12-02 20:44:45 +00:00
|
|
|
if not self.hide_nightmode_warning then
|
2022-11-03 05:32:47 +00:00
|
|
|
local RadioButtonWidget = require("ui/widget/radiobuttonwidget")
|
|
|
|
local radio_buttons = {
|
|
|
|
{{
|
|
|
|
text = _("Show this warning again"),
|
|
|
|
provider = function() end
|
|
|
|
}},
|
|
|
|
{{
|
|
|
|
text = _("Hide the warning until the next book is opened"),
|
|
|
|
provider = function()
|
|
|
|
self.hide_nightmode_warning = true
|
|
|
|
end,
|
|
|
|
}},
|
|
|
|
{{
|
|
|
|
text = _("Disable AutoWarmth's nightmode control"),
|
|
|
|
provider = function()
|
|
|
|
self.control_nightmode = false
|
|
|
|
G_reader_settings:makeFalse("autowarmth_control_nightmode")
|
2023-06-09 18:57:49 +00:00
|
|
|
self:scheduleMidnightUpdate(true)
|
2022-11-03 05:32:47 +00:00
|
|
|
end,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
UIManager:show(RadioButtonWidget:new{
|
|
|
|
title_text = _("Night mode changed"),
|
|
|
|
info_text = _("The AutoWarmth plugin might change it again."),
|
|
|
|
width_factor = 0.9,
|
|
|
|
radio_buttons = radio_buttons,
|
|
|
|
callback = function(radio)
|
|
|
|
radio.provider()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:31:06 +00:00
|
|
|
function AutoWarmth:_onToggleFrontlight()
|
|
|
|
logger.dbg("AutoWarmth: onToggleFrontlight")
|
2023-06-13 05:51:31 +00:00
|
|
|
AutoWarmth.fl_user_toggle = true
|
|
|
|
AutoWarmth.fl_turned_off = not AutoWarmth.fl_turned_off
|
2022-11-04 08:31:06 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
function AutoWarmth:setEventHandlers()
|
|
|
|
self.onResume = self._onResume
|
|
|
|
self.onSuspend = self._onSuspend
|
2022-12-02 20:44:45 +00:00
|
|
|
if self.control_nightmode then
|
|
|
|
self.onToggleNightMode = self._onToggleNightMode
|
|
|
|
self.onSetNightMode = self._onToggleNightMode
|
2023-06-09 18:57:49 +00:00
|
|
|
else
|
|
|
|
self.onToggleNightMode = nil
|
|
|
|
self.onSetNightMode = nil
|
2022-12-02 20:44:45 +00:00
|
|
|
end
|
2022-11-04 08:31:06 +00:00
|
|
|
if self.fl_off_during_day then
|
|
|
|
self.onToggleFrontlight = self._onToggleFrontlight
|
|
|
|
end
|
2022-07-28 04:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:clearEventHandlers()
|
|
|
|
self.onResume = nil
|
|
|
|
self.onSuspend = nil
|
2022-11-03 05:32:47 +00:00
|
|
|
self.onToggleNightMode = nil
|
|
|
|
self.onSetNightMode = nil
|
2022-11-04 08:31:06 +00:00
|
|
|
self.onToggleFrontlight = nil
|
2022-07-28 04:22:25 +00:00
|
|
|
end
|
2022-07-06 14:56:30 +00:00
|
|
|
|
|
|
|
-- from_resume ... true if called from onResume
|
|
|
|
function AutoWarmth:scheduleMidnightUpdate(from_resume)
|
2022-03-29 20:59:10 +00:00
|
|
|
logger.dbg("AutoWarmth: scheduleMidnightUpdate")
|
2021-09-25 09:02:10 +00:00
|
|
|
-- first unschedule all old functions
|
2022-07-06 14:56:30 +00:00
|
|
|
UIManager:unschedule(self.scheduleMidnightUpdate)
|
2022-11-02 13:29:36 +00:00
|
|
|
UIManager:unschedule(self.setWarmth)
|
2022-11-11 22:47:51 +00:00
|
|
|
UIManager:unschedule(self.setFrontlight)
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2023-02-15 23:20:01 +00:00
|
|
|
-- Calculate current timezone of device, which might change due to daylight saving.
|
|
|
|
local timezone = SunTime:getTimezoneOffset()
|
|
|
|
if timezone ~= self.timezone then
|
|
|
|
G_reader_settings:saveSetting("autowarmth_timezone", self.timezone)
|
|
|
|
self.timezone = timezone
|
|
|
|
end
|
|
|
|
|
2022-07-06 14:56:30 +00:00
|
|
|
SunTime:setPosition(self.location, self.latitude, self.longitude, self.timezone, self.altitude, true)
|
2021-09-25 09:02:10 +00:00
|
|
|
SunTime:setAdvanced()
|
|
|
|
SunTime:setDate() -- today
|
2022-07-28 04:22:25 +00:00
|
|
|
SunTime:calculateTimes() -- calculates times in hours
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
self.sched_times_s = {}
|
2022-07-06 14:56:30 +00:00
|
|
|
self.sched_warmths = {}
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local function prepareSchedule(times_h, index1, index2)
|
|
|
|
local time1_h = times_h[index1]
|
|
|
|
if not time1_h then return end
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local time1_s = SunTime:getTimeInSec(time1_h)
|
|
|
|
self.sched_times_s[#self.sched_times_s + 1] = time1_s
|
|
|
|
self.sched_warmths[#self.sched_warmths + 1] = self.warmth[index1]
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local time2_h = times_h[index2]
|
|
|
|
if not time2_h then return end -- to near to the pole
|
2021-09-25 09:02:10 +00:00
|
|
|
local warmth_diff = math.min(self.warmth[index2], 100) - math.min(self.warmth[index1], 100)
|
2022-07-28 04:22:25 +00:00
|
|
|
local time_diff_s = SunTime:getTimeInSec(time2_h) - time1_s
|
|
|
|
if warmth_diff ~= 0 and time_diff_s > 0 then
|
|
|
|
local delta_t = time_diff_s / math.abs(warmth_diff) -- cannot be inf, no problem
|
2022-06-01 14:22:42 +00:00
|
|
|
local delta_w = warmth_diff > 0 and 1 or -1
|
2022-07-28 04:22:25 +00:00
|
|
|
for i = 1, math.abs(warmth_diff) - 1 do
|
2021-09-25 09:02:10 +00:00
|
|
|
local next_warmth = math.min(self.warmth[index1], 100) + delta_w * i
|
|
|
|
-- only apply warmth for steps the hardware has (e.g. Tolino has 0-10 hw steps
|
|
|
|
-- which map to warmth 0, 10, 20, 30 ... 100)
|
|
|
|
if frac(next_warmth * device_warmth_fit_scale) == 0 then
|
2022-07-28 04:22:25 +00:00
|
|
|
table.insert(self.sched_times_s, time1_s + delta_t * i)
|
2022-08-24 11:32:16 +00:00
|
|
|
table.insert(self.sched_warmths, next_warmth)
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.activate == activate_sun then
|
2022-07-28 04:22:25 +00:00
|
|
|
self.current_times_h = {unpack(SunTime.times, 1, midnight_index)}
|
2021-09-25 09:02:10 +00:00
|
|
|
elseif self.activate == activate_schedule then
|
2022-07-28 04:22:25 +00:00
|
|
|
self.current_times_h = {unpack(self.scheduler_times, 1, midnight_index)}
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
2022-07-28 04:22:25 +00:00
|
|
|
self.current_times_h = {unpack(SunTime.times, 1, midnight_index)}
|
2021-09-25 09:02:10 +00:00
|
|
|
if self.activate == activate_closer_noon then
|
|
|
|
for i = 1, midnight_index do
|
2022-07-28 04:22:25 +00:00
|
|
|
if not self.current_times_h[i] then
|
|
|
|
self.current_times_h[i] = self.scheduler_times[i]
|
2021-09-25 09:02:10 +00:00
|
|
|
elseif self.scheduler_times[i] and
|
2022-07-28 04:22:25 +00:00
|
|
|
math.abs(self.current_times_h[i]%24 - 12) > math.abs(self.scheduler_times[i]%24 - 12) then
|
|
|
|
self.current_times_h[i] = self.scheduler_times[i]
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- activate_closer_midnight
|
|
|
|
for i = 1, midnight_index do
|
2022-07-28 04:22:25 +00:00
|
|
|
if not self.current_times_h[i] then
|
|
|
|
self.current_times_h[i] = self.scheduler_times[i]
|
2021-09-25 09:02:10 +00:00
|
|
|
elseif self.scheduler_times[i] and
|
2022-07-28 04:22:25 +00:00
|
|
|
math.abs(self.current_times_h[i]%24 - 12) < math.abs(self.scheduler_times[i]%24 - 12) then
|
|
|
|
self.current_times_h[i] = self.scheduler_times[i]
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.easy_mode then
|
2022-12-02 20:44:45 +00:00
|
|
|
self.current_times_h[1] = nil -- Solar midnight prev. day
|
|
|
|
self.current_times_h[2] = nil -- Astronomical dawn
|
|
|
|
self.current_times_h[3] = nil -- Nautical dawn
|
|
|
|
self.current_times_h[6] = nil -- Solar noon
|
2023-06-09 18:56:46 +00:00
|
|
|
self.current_times_h[9] = nil -- Nautical dusk
|
|
|
|
self.current_times_h[10] = nil -- Astronomical dusk
|
2022-12-02 20:44:45 +00:00
|
|
|
self.current_times_h[11] = nil -- Solar midnight
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- here are dragons
|
2022-12-02 20:44:45 +00:00
|
|
|
local prev_index = 1
|
|
|
|
-- find first valid entry (~= nil)
|
|
|
|
while not self.current_times_h[prev_index] and prev_index <= midnight_index do
|
|
|
|
prev_index = prev_index + 1
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
2022-12-02 20:44:45 +00:00
|
|
|
local next_index
|
|
|
|
while prev_index <= midnight_index do
|
|
|
|
next_index = prev_index + 1
|
|
|
|
-- find next valid entry (~= nil)
|
|
|
|
while not self.current_times_h[next_index] and next_index <= midnight_index do
|
|
|
|
next_index = next_index + 1
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
2022-12-02 20:44:45 +00:00
|
|
|
-- now we have two valid indices: prev_index and next_index
|
|
|
|
prepareSchedule(self.current_times_h, prev_index, next_index)
|
|
|
|
-- move on prev_index to the next valid entry
|
|
|
|
prev_index = next_index
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
local now_s = SunTime:getTimeInSec()
|
|
|
|
|
|
|
|
-- Reschedule 1sec after midnight
|
|
|
|
UIManager:scheduleIn(24*3600 + 1 - now_s, self.scheduleMidnightUpdate, self)
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-07-28 04:22:25 +00:00
|
|
|
-- set event handlers
|
|
|
|
if self.activate ~= 0 then
|
|
|
|
self:setEventHandlers()
|
2022-12-02 20:44:45 +00:00
|
|
|
-- Schedule the first warmth change
|
|
|
|
self.sched_warmth_index = 1
|
|
|
|
self:scheduleNextWarmthChange(from_resume)
|
|
|
|
self:scheduleToggleFrontlight(now_s) -- reset user toggles at sun set or sun rise
|
2022-11-11 22:47:51 +00:00
|
|
|
self:toggleFrontlight(now_s)
|
2022-07-28 04:22:25 +00:00
|
|
|
else
|
|
|
|
self:clearEventHandlers()
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
2022-11-11 22:47:51 +00:00
|
|
|
function AutoWarmth:scheduleToggleFrontlight(now_s)
|
|
|
|
logger.dbg("AutoWarmth: scheduleToggleFrontlight")
|
2022-12-02 20:44:45 +00:00
|
|
|
|
|
|
|
UIManager:unschedule(self.setFrontlight)
|
|
|
|
|
2022-11-11 22:47:51 +00:00
|
|
|
if not self.fl_off_during_day then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Reset user fl toggles at sunset or sunrise with offset, as `scheduleNextWarmthChange` gets called only
|
|
|
|
-- on scheduled warmth changes.
|
2023-11-01 21:10:52 +00:00
|
|
|
local ss = self.current_times_h[7]
|
|
|
|
local sunset_in_s = ss and (ss * 3600 - self.fl_off_during_day_offset_s - now_s) or -1
|
2023-06-09 18:56:46 +00:00
|
|
|
if sunset_in_s >= 0 then -- first check if we are before sunset
|
2023-06-13 05:51:31 +00:00
|
|
|
UIManager:scheduleIn(sunset_in_s, self.setFrontlight, self, true, false)
|
2023-11-01 21:10:52 +00:00
|
|
|
end
|
|
|
|
local sr = self.current_times_h[5]
|
|
|
|
local sunrise_in_s = sr and (sr * 3600 + self.fl_off_during_day_offset_s - now_s) or -1
|
|
|
|
if sunrise_in_s >= 0 then -- second check if we are before sunrise
|
|
|
|
UIManager:scheduleIn(sunrise_in_s, self.setFrontlight, self, false, false)
|
2022-11-11 22:47:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:31:06 +00:00
|
|
|
-- turns frontlight on or off and notice AutoDim on turning it off
|
|
|
|
-- enable ... true to enable frontlight, false/nil to disable it
|
2023-06-13 05:51:31 +00:00
|
|
|
function AutoWarmth:setFrontlight(enable, keep_user_toggle)
|
|
|
|
logger.dbg("AutoWarmth: setFrontlight", enable, keep_user_toggle)
|
|
|
|
if not keep_user_toggle then
|
|
|
|
AutoWarmth.fl_user_toggle = false
|
|
|
|
end
|
|
|
|
if not self.fl_off_during_day then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-08-09 17:42:13 +00:00
|
|
|
UIManager:scheduleIn(0.01, function()
|
|
|
|
if enable then
|
|
|
|
Powerd:turnOnFrontlight()
|
|
|
|
AutoWarmth.fl_turned_off = false
|
|
|
|
else
|
|
|
|
Powerd:turnOffFrontlight()
|
|
|
|
AutoWarmth.fl_turned_off = true
|
|
|
|
UIManager:broadcastEvent(Event:new("FrontlightTurnedOff")) -- used e.g. in AutoDim
|
|
|
|
end
|
|
|
|
end)
|
2022-11-04 08:31:06 +00:00
|
|
|
end
|
|
|
|
|
2023-06-13 05:51:31 +00:00
|
|
|
-- toggles Frontlight on or off, depending on `now_s`
|
|
|
|
function AutoWarmth:toggleFrontlight(now_s)
|
|
|
|
logger.dbg("AutoWarmth: toggleFrontlight", now_s)
|
|
|
|
|
2022-11-11 22:47:51 +00:00
|
|
|
if not self.fl_off_during_day then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-06-09 18:56:46 +00:00
|
|
|
now_s = now_s or SunTime:getTimeInSec()
|
2023-11-01 21:10:52 +00:00
|
|
|
local sr = self.current_times_h[5]
|
|
|
|
local ss = self.current_times_h[7]
|
|
|
|
local sunrise_in_s = sr and (sr * 3600 + self.fl_off_during_day_offset_s - now_s) or 0
|
|
|
|
local sunset_in_s = sr and (ss * 3600 - self.fl_off_during_day_offset_s - now_s) or 0
|
2022-11-11 22:47:51 +00:00
|
|
|
|
2023-06-13 05:51:31 +00:00
|
|
|
self:setFrontlight(sunrise_in_s > 0 or sunset_in_s < 0)
|
2022-11-11 22:47:51 +00:00
|
|
|
end
|
|
|
|
|
2022-07-06 14:56:30 +00:00
|
|
|
-- schedules the next warmth change
|
|
|
|
-- search_pos ... start searching from that index
|
|
|
|
-- from_resume ... true if first call after resume
|
2022-12-02 20:44:45 +00:00
|
|
|
function AutoWarmth:scheduleNextWarmthChange(from_resume)
|
2022-07-28 04:22:25 +00:00
|
|
|
logger.dbg("AutoWarmth: scheduleNextWarmthChange")
|
2022-12-02 20:44:45 +00:00
|
|
|
UIManager:unschedule(self.scheduleNextWarmthChange)
|
2021-09-25 09:02:10 +00:00
|
|
|
|
2022-12-02 20:44:45 +00:00
|
|
|
if self.activate == 0 or #self.sched_warmths == 0 then
|
2022-07-06 14:56:30 +00:00
|
|
|
return
|
|
|
|
end
|
2021-10-28 20:13:41 +00:00
|
|
|
|
2022-12-02 20:44:45 +00:00
|
|
|
if self.sched_warmth_index < 1 then
|
|
|
|
self.sched_warmth_index = 1
|
|
|
|
end
|
2021-10-03 08:28:09 +00:00
|
|
|
|
2022-12-02 20:44:45 +00:00
|
|
|
-- `warmth_now` is the value which should be applied now.
|
|
|
|
-- `warmth_in_1p5_s` is valid `1.5` seconds after now for resume on some devices (KA1)
|
|
|
|
-- Most of the times this will be the same as `warmth_now`.
|
2021-10-26 07:54:21 +00:00
|
|
|
-- We need both, as we could have a very rapid change in warmth (depending on user settings)
|
|
|
|
-- or by chance a change in warmth very shortly after (a few ms) resume time.
|
2022-12-02 20:44:45 +00:00
|
|
|
-- Use the last (== solar midnight) warmth value, so that we have a valid value when resuming
|
|
|
|
-- before 24:00:00 but after true midnight. OK, this value is actually not quite the right one,
|
|
|
|
-- as it is calculated for the current day (and not the previous one), but this is for a corner case
|
2021-10-26 07:54:21 +00:00
|
|
|
-- and the error is small.
|
2022-12-02 20:44:45 +00:00
|
|
|
local warmth_now = self.sched_warmths[self.sched_warmth_index]
|
|
|
|
local warmth_in_1p5_s = warmth_now
|
|
|
|
local now_s = SunTime:getTimeInSec()
|
|
|
|
while self.sched_warmth_index <= #self.sched_warmths do
|
|
|
|
if self.sched_times_s[self.sched_warmth_index] > now_s then
|
2022-07-06 14:56:30 +00:00
|
|
|
break
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
2022-11-02 13:29:36 +00:00
|
|
|
|
2022-12-02 20:44:45 +00:00
|
|
|
-- update warmth_now
|
|
|
|
warmth_now = self.sched_warmths[self.sched_warmth_index]
|
|
|
|
local j = self.sched_warmth_index
|
|
|
|
if from_resume then
|
|
|
|
-- update warmth_in_1p5_s
|
|
|
|
while j <= #self.sched_warmths and self.sched_times_s[j] <= now_s + 1.5 do
|
|
|
|
-- Most times only one iteration through this loop
|
|
|
|
warmth_in_1p5_s = self.sched_warmths[j]
|
|
|
|
j = j + 1
|
|
|
|
end
|
2022-11-02 13:29:36 +00:00
|
|
|
end
|
2022-12-02 20:44:45 +00:00
|
|
|
-- It might be possible that self.sched_warmth_index gets > #self.sched_warmths
|
|
|
|
self.sched_warmth_index = self.sched_warmth_index + 1
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
2022-11-02 13:29:36 +00:00
|
|
|
|
2021-10-26 07:54:21 +00:00
|
|
|
-- update current warmth immediately
|
2022-12-02 20:44:45 +00:00
|
|
|
self:setWarmth(warmth_now, from_resume) -- force warmth, when from_resume
|
|
|
|
|
|
|
|
if self.sched_warmth_index <= #self.sched_warmths then -- and only then, schedule next warmth change
|
|
|
|
local next_sched_time_s = self.sched_times_s[self.sched_warmth_index] - now_s
|
|
|
|
UIManager:scheduleIn(next_sched_time_s, self.scheduleNextWarmthChange, self, false) -- no force warmth
|
2022-07-06 14:56:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if from_resume then
|
|
|
|
-- On some strange devices like KA1 setWarmth doesn't work right after a resume so
|
2022-12-02 20:44:45 +00:00
|
|
|
-- schedule setting of another valid warmth (=`warmth_in_1p5_s`) again (one time) in 1.5s.
|
2022-07-06 14:56:30 +00:00
|
|
|
-- On sane devices this schedule does no harm.
|
|
|
|
-- see https://github.com/koreader/koreader/issues/8363
|
2022-12-02 20:44:45 +00:00
|
|
|
UIManager:scheduleIn(1.5, self.setWarmth, self, warmth_in_1p5_s, true) -- force warmth one time
|
2022-07-06 14:56:30 +00:00
|
|
|
end
|
|
|
|
end
|
2021-10-26 07:54:21 +00:00
|
|
|
|
2022-07-06 14:56:30 +00:00
|
|
|
-- Set warmth and schedule the next warmth change
|
2022-12-02 20:44:45 +00:00
|
|
|
function AutoWarmth:setWarmth(val, force_warmth)
|
2023-08-02 23:01:38 +00:00
|
|
|
-- A value > 100 means to set night mode and set warmth to maximum.
|
|
|
|
-- We use an offset of 1000 to "flag", that night mode is on.
|
2022-07-06 14:56:30 +00:00
|
|
|
if val then
|
2024-04-17 17:23:12 +00:00
|
|
|
if self.control_nightmode then
|
|
|
|
DeviceListener:onSetNightMode(val > 100)
|
|
|
|
end
|
2022-07-06 14:56:30 +00:00
|
|
|
|
2022-12-02 20:44:45 +00:00
|
|
|
if self.control_warmth and Device:hasNaturalLight() then
|
2022-07-06 14:56:30 +00:00
|
|
|
val = math.min(val, 100) -- "mask" night mode
|
2022-07-28 04:22:25 +00:00
|
|
|
Powerd:setWarmth(val, force_warmth)
|
2022-07-06 14:56:30 +00:00
|
|
|
end
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:hoursToClock(hours)
|
|
|
|
if hours then
|
|
|
|
hours = hours % 24 * 3600 + 0.01 -- round up, due to reduced precision in settings.reader.lua
|
|
|
|
end
|
2022-11-17 04:53:35 +00:00
|
|
|
return datetime.secondsToClock(hours, self.easy_mode)
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:addToMainMenu(menu_items)
|
|
|
|
menu_items.autowarmth = {
|
2022-07-06 14:56:30 +00:00
|
|
|
text = Device:hasNaturalLight() and _("Auto warmth and night mode") or _("Auto night mode"),
|
2021-09-25 09:02:10 +00:00
|
|
|
checked_func = function() return self.activate ~= 0 end,
|
2022-12-02 20:44:45 +00:00
|
|
|
sub_item_table = self:getSubMenuItems(),
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function tidy_menu(menu, request)
|
|
|
|
for i = #menu, 1, -1 do
|
|
|
|
if menu[i].mode ~=nil then
|
|
|
|
if menu[i].mode ~= request then
|
2022-12-02 20:44:45 +00:00
|
|
|
table.remove(menu, i)
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
|
|
|
menu[i].mode = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
2022-07-14 21:25:11 +00:00
|
|
|
function AutoWarmth:updateItems(touchmenu_instance)
|
|
|
|
touchmenu_instance:updateItems()
|
|
|
|
UIManager:broadcastEvent(Event:new("UpdateFooter", self.view and self.view.footer_visible or false))
|
|
|
|
end
|
|
|
|
|
2021-09-25 09:02:10 +00:00
|
|
|
local about_text = _([[Set the frontlight warmth (if available) and night mode based on a time schedule or the sun's position.
|
|
|
|
|
|
|
|
There are three types of twilight:
|
|
|
|
|
|
|
|
• Civil: You can read a newspaper
|
|
|
|
• Nautical: You can see the first stars
|
|
|
|
• Astronomical: It is really dark
|
|
|
|
|
|
|
|
Custom warmth values can be set for every kind of twilight and sunrise, noon, sunset and midnight.
|
|
|
|
The screen warmth is continuously adjusted to the current time.
|
|
|
|
|
|
|
|
To use the sun's position, a geographical location must be entered. The calculations are very precise, with a deviation less than minute and a half.]])
|
|
|
|
function AutoWarmth:getSubMenuItems()
|
|
|
|
return {
|
|
|
|
{
|
2022-07-06 14:56:30 +00:00
|
|
|
text = Device:hasNaturalLight() and _("About auto warmth and night mode") or _("About auto night mode"),
|
2021-09-25 09:02:10 +00:00
|
|
|
callback = function()
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = about_text,
|
2022-06-01 14:22:42 +00:00
|
|
|
width = math.floor(Screen:getWidth() * 0.9),
|
2021-09-25 09:02:10 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
separator = true,
|
|
|
|
},
|
2021-10-21 19:47:11 +00:00
|
|
|
{
|
|
|
|
text = _("Activate"),
|
|
|
|
checked_func = function()
|
|
|
|
return self.activate ~= 0
|
|
|
|
end,
|
|
|
|
sub_item_table = self:getActivateMenu(),
|
|
|
|
},
|
2021-09-25 09:02:10 +00:00
|
|
|
{
|
|
|
|
text = _("Expert mode"),
|
|
|
|
checked_func = function()
|
|
|
|
return not self.easy_mode
|
|
|
|
end,
|
|
|
|
help_text = _("In the expert mode, different types of twilight can be used in addition to civil twilight."),
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
self.easy_mode = not self.easy_mode
|
|
|
|
G_reader_settings:saveSetting("autowarmth_easy_mode", self.easy_mode)
|
2022-11-11 22:47:51 +00:00
|
|
|
if self.easy_mode then
|
|
|
|
self.fl_off_during_day_offset_s = 0 -- don't store that value
|
|
|
|
else
|
|
|
|
self.fl_off_during_day_offset_s =
|
|
|
|
G_reader_settings:readSetting("autowarmth_fl_off_during_day_offset_s", 0)
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then
|
|
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
|
|
self:updateItems(touchmenu_instance)
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Location settings"),
|
|
|
|
sub_item_table = self:getLocationMenu(),
|
|
|
|
},
|
|
|
|
{
|
2021-10-21 19:47:11 +00:00
|
|
|
text = _("Fixed schedule settings"),
|
2021-10-18 19:15:12 +00:00
|
|
|
enabled_func = function()
|
2022-11-04 08:31:06 +00:00
|
|
|
return self.activate ~= activate_sun and self.activate ~= 0
|
2021-10-18 19:15:12 +00:00
|
|
|
end,
|
2021-09-25 09:02:10 +00:00
|
|
|
sub_item_table = self:getScheduleMenu(),
|
|
|
|
},
|
|
|
|
{
|
2021-10-18 19:15:12 +00:00
|
|
|
enabled_func = function()
|
2022-11-04 08:31:06 +00:00
|
|
|
return self.activate ~= 0
|
2021-10-18 19:15:12 +00:00
|
|
|
end,
|
2022-07-06 14:56:30 +00:00
|
|
|
text = Device:hasNaturalLight() and _("Warmth and night mode settings") or _("Night mode settings"),
|
2021-09-25 09:02:10 +00:00
|
|
|
sub_item_table = self:getWarmthMenu(),
|
2022-07-28 04:22:25 +00:00
|
|
|
},
|
2022-11-11 22:47:51 +00:00
|
|
|
self:getFlOffDuringDayMenu(),
|
|
|
|
self:getTimesMenu(_("Currently active parameters")),
|
|
|
|
self:getTimesMenu(_("Sun position information for"), true, activate_sun),
|
|
|
|
self:getTimesMenu(_("Fixed schedule information"), false, activate_schedule),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getFlOffDuringDayMenu()
|
|
|
|
return {
|
|
|
|
checked_func = function()
|
|
|
|
return self.fl_off_during_day
|
|
|
|
end,
|
|
|
|
text_func = function()
|
|
|
|
if self.fl_off_during_day and self.fl_off_during_day_offset_s ~= 0 and not self.easy_mode then
|
|
|
|
return T(_("Frontlight off during day: %1 min offset"), math.floor(self.fl_off_during_day_offset_s/60))
|
|
|
|
else
|
|
|
|
return _("Frontlight off during day")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
if self.easy_mode then
|
2022-07-28 04:22:25 +00:00
|
|
|
self.fl_off_during_day = not self.fl_off_during_day
|
|
|
|
G_reader_settings:saveSetting("autowarmth_fl_off_during_day", self.fl_off_during_day)
|
|
|
|
self:scheduleMidnightUpdate()
|
2023-06-13 05:51:31 +00:00
|
|
|
self:toggleFrontlight()
|
2022-11-11 22:47:51 +00:00
|
|
|
else
|
|
|
|
-- hard limit offset to 15 min after sunset and 30 mins before sunset; sunrise vice versa
|
|
|
|
UIManager:show(SpinWidget:new{
|
|
|
|
title_text = _("Offset for frontlight off"),
|
|
|
|
info_text = _[[At this time the frontlight will be turned
|
|
|
|
• off after sunrise and
|
|
|
|
• on before sunset.]],
|
|
|
|
ok_always_enabled = true,
|
|
|
|
-- read the saved setting, as this get's not overwritten by toggling easy_mode
|
|
|
|
value = G_reader_settings:readSetting("autowarmth_fl_off_during_day_offset_s", 0) * (1/60),
|
|
|
|
value_min = -15,
|
|
|
|
value_max = 30,
|
|
|
|
wrap = false,
|
|
|
|
value_step = 5,
|
|
|
|
value_hold_step = 10,
|
|
|
|
unit = "min",
|
|
|
|
ok_text = _("Set"),
|
|
|
|
callback = function(spin)
|
|
|
|
self.fl_off_during_day_offset_s = spin.value * 60
|
|
|
|
G_reader_settings:saveSetting("autowarmth_fl_off_during_day_offset_s",
|
|
|
|
self.fl_off_during_day_offset_s)
|
|
|
|
self.fl_off_during_day = true
|
|
|
|
G_reader_settings:saveSetting("autowarmth_fl_off_during_day", true)
|
|
|
|
self:scheduleMidnightUpdate()
|
2023-06-13 05:51:31 +00:00
|
|
|
self:toggleFrontlight()
|
2022-11-11 22:47:51 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
|
|
|
end,
|
|
|
|
extra_text = _("Disable"),
|
2023-06-09 18:57:49 +00:00
|
|
|
extra_callback = function()
|
2022-11-11 22:47:51 +00:00
|
|
|
self.fl_off_during_day = nil
|
|
|
|
G_reader_settings:saveSetting("autowarmth_fl_off_during_day", nil)
|
|
|
|
self:scheduleMidnightUpdate()
|
2023-06-13 05:51:31 +00:00
|
|
|
self:toggleFrontlight()
|
2022-11-11 22:47:51 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
|
|
|
end,
|
2022-11-04 08:31:06 +00:00
|
|
|
})
|
2022-11-11 22:47:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if touchmenu_instance then
|
|
|
|
self:updateItems(touchmenu_instance)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _([[This feature turns your front light on at sunset and off at sunrise according to the “Current Active Parameters” in this plugin.
|
|
|
|
|
|
|
|
You can override this change by manually turning the front light on/off. At the next sunrise/sunset, AutoWarmth will toggle again if needed.
|
|
|
|
|
|
|
|
For cloudy autumn days, the switch-on/off time can be shifted by an offset.]]),
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
separator = true,
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getActivateMenu()
|
2021-10-18 19:15:12 +00:00
|
|
|
local function getActivateMenuEntry(text, help_text, activator)
|
2021-09-25 09:02:10 +00:00
|
|
|
return {
|
|
|
|
text = text,
|
2021-10-18 19:15:12 +00:00
|
|
|
help_text = help_text,
|
2021-09-25 09:02:10 +00:00
|
|
|
checked_func = function() return self.activate == activator end,
|
|
|
|
callback = function()
|
2022-07-06 14:56:30 +00:00
|
|
|
self.activate = self.activate ~= activator and activator or 0
|
2021-09-25 09:02:10 +00:00
|
|
|
G_reader_settings:saveSetting("autowarmth_activate", self.activate)
|
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
UIManager:broadcastEvent(Event:new("UpdateFooter", self.view and self.view.footer_visible or false))
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2021-10-21 19:47:11 +00:00
|
|
|
getActivateMenuEntry(_("According to the sun's position"),
|
2021-10-18 19:15:12 +00:00
|
|
|
_("Only use the times calculated from the position of the sun."),
|
|
|
|
activate_sun),
|
2021-10-21 19:47:11 +00:00
|
|
|
getActivateMenuEntry(_("According to the fixed schedule"),
|
|
|
|
_("Only use the times from the fixed schedule."),
|
2021-10-18 19:15:12 +00:00
|
|
|
activate_schedule),
|
|
|
|
getActivateMenuEntry(_("Whatever is closer to noon"),
|
|
|
|
_("Use the times from the sun position or schedule that are closer to noon."),
|
|
|
|
activate_closer_noon),
|
|
|
|
getActivateMenuEntry(_("Whatever is closer to midnight"),
|
|
|
|
_("Use the times from the sun position or schedule that are closer to midnight."),
|
|
|
|
activate_closer_midnight),
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getLocationMenu()
|
|
|
|
return {{
|
|
|
|
text_func = function()
|
|
|
|
if self.location ~= "" then
|
|
|
|
return T(_("Location: %1"), self.location)
|
|
|
|
else
|
|
|
|
return _("Location")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
local location_name_dialog
|
|
|
|
location_name_dialog = InputDialog:new{
|
|
|
|
title = _("Location name"),
|
|
|
|
input = self.location,
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
2022-03-04 20:20:00 +00:00
|
|
|
id = "close",
|
2021-09-25 09:02:10 +00:00
|
|
|
callback = function()
|
|
|
|
UIManager:close(location_name_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("OK"),
|
|
|
|
callback = function()
|
|
|
|
self.location = location_name_dialog:getInputText()
|
|
|
|
G_reader_settings:saveSetting("autowarmth_location",
|
|
|
|
self.location)
|
|
|
|
UIManager:close(location_name_dialog)
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
},
|
2022-06-01 14:22:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-09-25 09:02:10 +00:00
|
|
|
UIManager:show(location_name_dialog)
|
|
|
|
location_name_dialog:onShowKeyboard()
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text_func = function()
|
2022-05-23 22:25:50 +00:00
|
|
|
return T(_("Coordinates: (%1°, %2°)"), self.latitude, self.longitude)
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
local location_widget = DoubleSpinWidget:new{
|
|
|
|
title_text = _("Set location"),
|
|
|
|
info_text = _("Enter decimal degrees, northern hemisphere and eastern length are '+'."),
|
|
|
|
left_text = _("Latitude"),
|
|
|
|
left_value = self.latitude,
|
|
|
|
left_min = -90,
|
|
|
|
left_max = 90,
|
|
|
|
left_step = 0.1,
|
|
|
|
left_hold_step = 5,
|
2021-12-01 11:42:54 +00:00
|
|
|
left_precision = "%0.2f",
|
2021-09-25 09:02:10 +00:00
|
|
|
right_text = _("Longitude"),
|
|
|
|
right_value = self.longitude,
|
|
|
|
right_min = -180,
|
|
|
|
right_max = 180,
|
|
|
|
right_step = 0.1,
|
|
|
|
right_hold_step = 5,
|
2021-12-01 11:42:54 +00:00
|
|
|
right_precision = "%0.2f",
|
2022-05-23 22:25:50 +00:00
|
|
|
unit = "°",
|
2021-09-25 09:02:10 +00:00
|
|
|
callback = function(lat, long)
|
|
|
|
self.latitude = lat
|
|
|
|
self.longitude = long
|
2023-02-15 23:20:01 +00:00
|
|
|
self.timezone = SunTime:getTimezoneOffset() -- use timezone of device
|
2021-09-25 09:02:10 +00:00
|
|
|
G_reader_settings:saveSetting("autowarmth_latitude", self.latitude)
|
|
|
|
G_reader_settings:saveSetting("autowarmth_longitude", self.longitude)
|
|
|
|
G_reader_settings:saveSetting("autowarmth_timezone", self.timezone)
|
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(location_widget)
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text_func = function()
|
2022-05-23 22:25:50 +00:00
|
|
|
return T(_("Altitude: %1 m"), self.altitude)
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
UIManager:show(SpinWidget:new{
|
|
|
|
title_text = _("Altitude"),
|
2021-12-05 08:52:02 +00:00
|
|
|
info_text = _("Enter the altitude in meters above sea level."),
|
2021-09-25 09:02:10 +00:00
|
|
|
value = self.altitude,
|
|
|
|
value_min = -100,
|
|
|
|
value_max = 15000, -- intercontinental flight
|
|
|
|
wrap = false,
|
|
|
|
value_step = 10,
|
|
|
|
value_hold_step = 100,
|
2022-05-23 22:25:50 +00:00
|
|
|
unit = C_("Length", "m"),
|
2021-09-25 09:02:10 +00:00
|
|
|
ok_text = _("Set"),
|
|
|
|
callback = function(spin)
|
|
|
|
self.altitude = spin.value
|
|
|
|
G_reader_settings:saveSetting("autowarmth_altitude", self.altitude)
|
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
extra_text = _("Default"),
|
|
|
|
extra_callback = function()
|
|
|
|
self.altitude = 200
|
|
|
|
G_reader_settings:saveSetting("autowarmth_altitude", self.altitude)
|
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
}}
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getScheduleMenu()
|
|
|
|
local function store_times(touchmenu_instance, new_time, num)
|
|
|
|
self.scheduler_times[num] = new_time
|
2022-08-24 11:32:16 +00:00
|
|
|
G_reader_settings:saveSetting("autowarmth_scheduler_times", self.scheduler_times)
|
2021-09-25 09:02:10 +00:00
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
2022-09-11 17:31:36 +00:00
|
|
|
-- mode == nil ... show always
|
2021-09-25 09:02:10 +00:00
|
|
|
-- == true ... easy mode
|
|
|
|
-- == false ... expert mode
|
|
|
|
local function getScheduleMenuEntry(text, num, mode)
|
|
|
|
return {
|
|
|
|
mode = mode,
|
|
|
|
text_func = function()
|
2022-09-16 18:23:41 +00:00
|
|
|
return T(_("%1: %2"), text,
|
2021-09-25 09:02:10 +00:00
|
|
|
self:hoursToClock(self.scheduler_times[num]))
|
|
|
|
end,
|
|
|
|
checked_func = function()
|
|
|
|
return self.scheduler_times[num] ~= nil
|
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
local hh = 12
|
|
|
|
local mm = 0
|
|
|
|
if self.scheduler_times[num] then
|
|
|
|
hh = math.floor(self.scheduler_times[num])
|
|
|
|
mm = math.floor(frac(self.scheduler_times[num]) * 60 + 0.5)
|
|
|
|
end
|
2021-12-05 08:52:02 +00:00
|
|
|
UIManager:show(DateTimeWidget:new{
|
2021-09-25 09:02:10 +00:00
|
|
|
title_text = _("Set time"),
|
2021-12-05 08:52:02 +00:00
|
|
|
info_text = _("Enter time in hours and minutes."),
|
|
|
|
hour = hh,
|
2022-08-24 11:32:16 +00:00
|
|
|
hour_min = -1,
|
|
|
|
hour_max = 24,
|
2021-12-05 08:52:02 +00:00
|
|
|
min = mm,
|
|
|
|
ok_text = _("Set time"),
|
|
|
|
callback = function(time)
|
2022-10-10 20:21:27 +00:00
|
|
|
local new_time = time.hour + time.min * (1/60)
|
2021-09-25 09:02:10 +00:00
|
|
|
local function get_valid_time(n, dir)
|
|
|
|
for i = n+dir, dir > 0 and midnight_index or 1, dir do
|
|
|
|
if self.scheduler_times[i] then
|
|
|
|
return self.scheduler_times[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return dir > 0 and 0 or 26
|
|
|
|
end
|
|
|
|
if num > 1 and new_time < get_valid_time(num, -1) then
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2022-06-01 14:22:42 +00:00
|
|
|
text = _("This time is before the previous time.\nAdjust the previous time?"),
|
2021-09-25 09:02:10 +00:00
|
|
|
ok_callback = function()
|
2022-07-28 04:22:25 +00:00
|
|
|
for i = num - 1, 1, -1 do
|
2021-09-25 09:02:10 +00:00
|
|
|
if self.scheduler_times[i] then
|
|
|
|
if new_time < self.scheduler_times[i] then
|
|
|
|
self.scheduler_times[i] = new_time
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
store_times(touchmenu_instance, new_time, num)
|
|
|
|
end,
|
|
|
|
})
|
2022-11-02 13:29:36 +00:00
|
|
|
elseif num < midnight_index and new_time > get_valid_time(num, 1) then
|
2021-09-25 09:02:10 +00:00
|
|
|
UIManager:show(ConfirmBox:new{
|
2022-06-01 14:22:42 +00:00
|
|
|
text = _("This time is after the subsequent time.\nAdjust the subsequent time?"),
|
2021-09-25 09:02:10 +00:00
|
|
|
ok_callback = function()
|
2022-11-02 13:29:36 +00:00
|
|
|
for i = num + 1, midnight_index do
|
2021-09-25 09:02:10 +00:00
|
|
|
if self.scheduler_times[i] then
|
|
|
|
if new_time > self.scheduler_times[i] then
|
|
|
|
self.scheduler_times[i] = new_time
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
store_times(touchmenu_instance, new_time, num)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
store_times(touchmenu_instance, new_time, num)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
extra_text = _("Invalidate"),
|
|
|
|
extra_callback = function()
|
|
|
|
store_times(touchmenu_instance, nil, num)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local retval = {
|
2022-08-24 11:32:16 +00:00
|
|
|
getScheduleMenuEntry(_("Solar midnight (previous day)"), 1, false),
|
2021-09-25 09:02:10 +00:00
|
|
|
getScheduleMenuEntry(_("Astronomical dawn"), 2, false),
|
|
|
|
getScheduleMenuEntry(_("Nautical dawn"), 3, false),
|
|
|
|
getScheduleMenuEntry(_("Civil dawn"), 4),
|
|
|
|
getScheduleMenuEntry(_("Sunrise"), 5),
|
|
|
|
getScheduleMenuEntry(_("Solar noon"), 6, false),
|
|
|
|
getScheduleMenuEntry(_("Sunset"), 7),
|
|
|
|
getScheduleMenuEntry(_("Civil dusk"), 8),
|
|
|
|
getScheduleMenuEntry(_("Nautical dusk"), 9, false),
|
|
|
|
getScheduleMenuEntry(_("Astronomical dusk"), 10, false),
|
2022-08-24 11:32:16 +00:00
|
|
|
getScheduleMenuEntry(_("Solar midnight"), 11, false),
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tidy_menu(retval, self.easy_mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getWarmthMenu()
|
2022-09-11 17:31:36 +00:00
|
|
|
-- mode == nil ... show always
|
2021-09-25 09:02:10 +00:00
|
|
|
-- == true ... easy mode
|
|
|
|
-- == false ... expert mode
|
|
|
|
local function getWarmthMenuEntry(text, num, mode)
|
|
|
|
return {
|
|
|
|
mode = mode,
|
|
|
|
text_func = function()
|
2022-09-11 17:31:36 +00:00
|
|
|
if Device:hasNaturalLight() and self.control_warmth then
|
2023-08-02 23:01:38 +00:00
|
|
|
if self.control_nightmode then
|
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
return T(_("%1: %2 %"), text, self.warmth[num])
|
|
|
|
else
|
|
|
|
return T(_("%1: 100 % + ☾"), text)
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
2023-08-02 23:01:38 +00:00
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
return T(_("%1: %2 %"), text, self.warmth[num])
|
|
|
|
else
|
|
|
|
return T(_("%1: %2 %"), text, math.max(self.warmth[num] - 1000, 0))
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
return T(_("%1: ☼"), text)
|
|
|
|
else
|
|
|
|
return T(_("%1: ☾"), text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
2022-09-11 17:31:36 +00:00
|
|
|
if Device:hasNaturalLight() and self.control_warmth then
|
2023-08-02 23:01:38 +00:00
|
|
|
local warmth_spinner = SpinWidget:new{
|
2021-09-25 09:02:10 +00:00
|
|
|
title_text = text,
|
2021-12-05 08:52:02 +00:00
|
|
|
info_text = _("Enter percentage of warmth."),
|
2023-08-02 23:01:38 +00:00
|
|
|
value = self.warmth[num] <= 100 and self.warmth[num] or math.max(self.warmth[num] - 1000, 0), -- mask nightmode
|
2021-09-25 09:02:10 +00:00
|
|
|
value_min = 0,
|
|
|
|
value_max = 100,
|
|
|
|
wrap = false,
|
|
|
|
value_step = math.floor(100 / device_max_warmth),
|
|
|
|
value_hold_step = 10,
|
2022-05-23 22:25:50 +00:00
|
|
|
unit = "%",
|
2021-09-25 09:02:10 +00:00
|
|
|
ok_text = _("Set"),
|
2023-08-02 23:01:38 +00:00
|
|
|
ok_always_enabled = true,
|
2021-09-25 09:02:10 +00:00
|
|
|
callback = function(spin)
|
|
|
|
self.warmth[num] = spin.value
|
2023-08-02 23:01:38 +00:00
|
|
|
if self.control_nightmode and self.night_mode_check_box.checked then
|
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
self.warmth[num] = self.warmth[num] + 1000 -- add night mode
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.warmth[num] > 100 then
|
|
|
|
self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.warmth[#self.warmth - num + 1] = self.warmth[num]
|
2021-09-25 09:02:10 +00:00
|
|
|
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
|
|
|
|
self:scheduleMidnightUpdate()
|
2022-07-14 21:25:11 +00:00
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
2023-08-02 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.control_nightmode then
|
|
|
|
self.night_mode_check_box = CheckButton:new{
|
|
|
|
text = _("Night mode"),
|
|
|
|
checked = self.warmth[num] > 100,
|
|
|
|
parent = warmth_spinner,
|
|
|
|
}
|
|
|
|
warmth_spinner:addWidget(self.night_mode_check_box)
|
|
|
|
end
|
|
|
|
UIManager:show(warmth_spinner)
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2022-09-11 17:31:36 +00:00
|
|
|
text = _("Night mode"),
|
2023-07-23 18:37:50 +00:00
|
|
|
ok_text = _("Turn on"),
|
2021-09-25 09:02:10 +00:00
|
|
|
ok_callback = function()
|
2023-08-02 23:01:38 +00:00
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
self.warmth[num] = self.warmth[num] + 1000
|
|
|
|
self.warmth[#self.warmth - num + 1] = self.warmth[num]
|
|
|
|
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
|
|
|
|
self:scheduleMidnightUpdate()
|
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
2023-07-23 18:37:50 +00:00
|
|
|
cancel_text = _("Turn off"),
|
2021-09-25 09:02:10 +00:00
|
|
|
cancel_callback = function()
|
2023-08-02 23:01:38 +00:00
|
|
|
if self.warmth[num] > 100 then
|
|
|
|
self.warmth[num] = math.max(self.warmth[num] - 1000, 0) -- delete night mode
|
|
|
|
self.warmth[#self.warmth - num + 1] = self.warmth[num]
|
|
|
|
G_reader_settings:saveSetting("autowarmth_warmth", self.warmth)
|
|
|
|
self:scheduleMidnightUpdate()
|
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
|
|
|
end
|
2021-09-25 09:02:10 +00:00
|
|
|
end,
|
|
|
|
other_buttons = {{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local retval = {
|
2022-09-11 17:31:36 +00:00
|
|
|
{
|
|
|
|
text_func = function()
|
|
|
|
if Device:hasNaturalLight() then
|
|
|
|
return T(_("Control: %1%2%3"), self.control_warmth and _("warmth") or "",
|
2023-01-05 12:46:52 +00:00
|
|
|
self.control_warmth and self.control_nightmode and T(" %1 ", _("and")) or "",
|
2022-09-11 17:31:36 +00:00
|
|
|
self.control_nightmode and _("night mode") or "")
|
|
|
|
else
|
|
|
|
return _("Control: night mode")
|
|
|
|
end
|
|
|
|
end,
|
2023-06-09 18:57:49 +00:00
|
|
|
checked_func = function()
|
|
|
|
if Device:hasNaturalLight() then
|
|
|
|
return self.control_nightmode or self.control_warmth
|
|
|
|
else
|
|
|
|
return self.control_nightmode
|
|
|
|
end
|
2022-09-11 17:31:36 +00:00
|
|
|
end,
|
2023-06-09 18:56:46 +00:00
|
|
|
hold_callback = function()
|
|
|
|
if Device:hasNaturalLight() then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Tapping here chooses between the different AutoWarmth modes: 'warmth only', 'warmth and night mode', 'night mode only'."),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Your device supports 'night mode' control only."),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
2022-09-11 17:31:36 +00:00
|
|
|
callback = function(touchmenu_instance)
|
2023-06-09 18:57:49 +00:00
|
|
|
if Device:hasNaturalLight() then
|
|
|
|
if self.control_warmth and self.control_nightmode then
|
|
|
|
self.control_warmth = true
|
|
|
|
self.control_nightmode = false
|
|
|
|
G_reader_settings:makeTrue("autowarmth_control_warmth")
|
|
|
|
G_reader_settings:makeFalse("autowarmth_control_nightmode")
|
|
|
|
elseif self.control_warmth and not self.control_nightmode then
|
|
|
|
self.control_warmth = false
|
|
|
|
self.control_nightmode = true
|
|
|
|
G_reader_settings:makeFalse("autowarmth_control_warmth")
|
|
|
|
G_reader_settings:makeTrue("autowarmth_control_nightmode")
|
|
|
|
else
|
|
|
|
self.control_warmth = true
|
|
|
|
self.control_nightmode = true
|
|
|
|
G_reader_settings:makeTrue("autowarmth_control_warmth")
|
|
|
|
G_reader_settings:makeTrue("autowarmth_control_nightmode")
|
|
|
|
end
|
2022-09-11 17:31:36 +00:00
|
|
|
else
|
2023-06-09 18:57:49 +00:00
|
|
|
self.control_nightmode = not self.control_nightmode
|
|
|
|
G_reader_settings:toggle("autowarmth_control_nightmode")
|
2022-09-11 17:31:36 +00:00
|
|
|
end
|
|
|
|
self:scheduleMidnightUpdate()
|
|
|
|
if touchmenu_instance then self:updateItems(touchmenu_instance) end
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
separator = true,
|
|
|
|
},
|
2021-09-25 09:02:10 +00:00
|
|
|
{
|
2022-07-06 14:56:30 +00:00
|
|
|
text = Device:hasNaturalLight() and _("Set warmth and night mode for:") or _("Set night mode for:"),
|
2022-12-29 07:44:37 +00:00
|
|
|
enabled = false,
|
2021-09-25 09:02:10 +00:00
|
|
|
},
|
|
|
|
getWarmthMenuEntry(_("Solar noon"), 6, false),
|
2021-10-21 19:47:11 +00:00
|
|
|
getWarmthMenuEntry(_("Sunset and sunrise"), 5),
|
2021-09-25 22:30:34 +00:00
|
|
|
getWarmthMenuEntry(_("Darkest time of civil twilight"), 4),
|
|
|
|
getWarmthMenuEntry(_("Darkest time of nautical twilight"), 3, false),
|
|
|
|
getWarmthMenuEntry(_("Darkest time of astronomical twilight"), 2, false),
|
|
|
|
getWarmthMenuEntry(_("Solar midnight"), 1, false),
|
2021-09-25 09:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tidy_menu(retval, self.easy_mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- title
|
|
|
|
-- location: add a location string
|
2022-07-28 04:22:25 +00:00
|
|
|
-- activator: nil .. current_times_h,
|
2021-09-25 09:02:10 +00:00
|
|
|
-- activate_sun .. sun times
|
|
|
|
-- activate_schedule .. scheduler times
|
|
|
|
-- request_easy: true if easy_mode should be used
|
|
|
|
function AutoWarmth:showTimesInfo(title, location, activator, request_easy)
|
|
|
|
local times
|
|
|
|
if not activator then
|
2022-07-28 04:22:25 +00:00
|
|
|
times = self.current_times_h
|
2021-09-25 09:02:10 +00:00
|
|
|
elseif activator == activate_sun then
|
|
|
|
times = SunTime.times
|
|
|
|
elseif activator == activate_schedule then
|
|
|
|
times = self.scheduler_times
|
|
|
|
end
|
|
|
|
|
|
|
|
-- text to show
|
|
|
|
-- t .. times
|
|
|
|
-- num .. index in times
|
2022-11-11 22:47:51 +00:00
|
|
|
local function info_line(indent, text, time, num, face, easy)
|
2021-10-03 08:40:46 +00:00
|
|
|
-- get width of space
|
|
|
|
local unit = " "
|
|
|
|
local tmp = TextWidget:new{
|
|
|
|
text = unit,
|
|
|
|
face = face,
|
|
|
|
}
|
|
|
|
local space_w = tmp:getSize().w
|
|
|
|
|
|
|
|
-- get width of text
|
|
|
|
unit = text
|
|
|
|
tmp = TextWidget:new{
|
|
|
|
text = unit,
|
|
|
|
face = face,
|
|
|
|
}
|
|
|
|
local text_w = tmp:getSize().w
|
|
|
|
tmp:free()
|
|
|
|
|
|
|
|
-- width of text in spaces
|
|
|
|
local str_len = math.floor(text_w / space_w + 0.5)
|
|
|
|
|
2021-09-25 22:30:34 +00:00
|
|
|
local tab_width = 18 - indent
|
2021-10-03 08:40:46 +00:00
|
|
|
local retval = string.rep(" ", indent) .. text .. string.rep(" ", tab_width - str_len)
|
2022-11-11 22:47:51 +00:00
|
|
|
.. self:hoursToClock(time)
|
2021-09-25 09:02:10 +00:00
|
|
|
if easy then
|
2022-11-11 22:47:51 +00:00
|
|
|
if time and num and self.current_times_h[num] and self.current_times_h[num] ~= time then
|
2021-09-25 09:02:10 +00:00
|
|
|
return text .. "\n"
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 22:47:51 +00:00
|
|
|
if not time then -- entry deactivated
|
2021-09-25 09:02:10 +00:00
|
|
|
return retval .. "\n"
|
2022-09-11 17:31:36 +00:00
|
|
|
elseif Device:hasNaturalLight() and self.control_warmth then
|
2022-11-11 22:47:51 +00:00
|
|
|
if self.current_times_h[num] == time then
|
2021-09-25 09:02:10 +00:00
|
|
|
if self.warmth[num] <= 100 then
|
2022-12-02 20:44:45 +00:00
|
|
|
return retval .. " (💡" .. self.warmth[num] .. "%)\n"
|
2021-09-25 09:02:10 +00:00
|
|
|
else
|
2022-09-11 17:31:36 +00:00
|
|
|
return retval .. " (💡100%" .. (self.control_nightmode and " + ☾" or "") .. ")\n"
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return retval .. "\n"
|
|
|
|
end
|
|
|
|
else
|
2022-11-11 22:47:51 +00:00
|
|
|
if self.current_times_h[num] == time then
|
2021-09-25 09:02:10 +00:00
|
|
|
if self.warmth[num] <= 100 then
|
|
|
|
return retval .. " (☼)\n"
|
|
|
|
else
|
|
|
|
return retval .. " (☾)\n"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return retval .. "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local location_string = ""
|
|
|
|
if location then
|
|
|
|
location_string = " " .. self:getLocationString()
|
|
|
|
end
|
|
|
|
|
2022-11-11 22:47:51 +00:00
|
|
|
local function add_line(indent, text, easy)
|
|
|
|
return easy and "" or (string.rep(" ", indent) .. text .. "\n")
|
2021-09-26 10:24:23 +00:00
|
|
|
end
|
|
|
|
|
2021-10-03 08:40:46 +00:00
|
|
|
local face = Font:getFace("scfont")
|
2021-09-25 09:02:10 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2021-10-03 08:40:46 +00:00
|
|
|
face = face,
|
2021-09-25 09:02:10 +00:00
|
|
|
width = math.floor(Screen:getWidth() * (self.easy_mode and 0.75 or 0.90)),
|
2022-06-01 14:22:42 +00:00
|
|
|
text = title .. location_string .. ":\n\n" ..
|
2022-11-11 22:47:51 +00:00
|
|
|
info_line(0, _("Solar midnight:"), times[1], 1, face, request_easy) ..
|
|
|
|
add_line(2, _("Dawn"), request_easy) ..
|
|
|
|
info_line(4, _("Astronomic:"), times[2], 2, face, request_easy) ..
|
|
|
|
info_line(4, _("Nautical:"), times[3], 3, face, request_easy)..
|
2021-09-26 10:24:23 +00:00
|
|
|
info_line(request_easy and 0 or 4,
|
2022-11-11 22:47:51 +00:00
|
|
|
request_easy and _("Twilight:") or _("Civil:"), times[4], 4, face) ..
|
|
|
|
add_line(2, _("Dawn"), request_easy) ..
|
|
|
|
info_line(0, _("Sunrise:"), times[5], 5, face) ..
|
2021-09-25 22:30:34 +00:00
|
|
|
"\n" ..
|
2022-11-11 22:47:51 +00:00
|
|
|
info_line(0, _("Solar noon:"), times[6], 6, face, request_easy) ..
|
|
|
|
add_line(0, "", request_easy) ..
|
|
|
|
info_line(0, _("Sunset:"), times[7], 7, face) ..
|
|
|
|
add_line(2, _("Dusk"), request_easy) ..
|
2021-09-26 10:24:23 +00:00
|
|
|
info_line(request_easy and 0 or 4,
|
2022-11-11 22:47:51 +00:00
|
|
|
request_easy and _("Twilight:") or _("Civil:"), times[8], 8, face) ..
|
|
|
|
info_line(4, _("Nautical:"), times[9], 9, face, request_easy) ..
|
|
|
|
info_line(4, _("Astronomic:"), times[10], 10, face, request_easy) ..
|
|
|
|
add_line(2, _("Dusk"), request_easy) ..
|
|
|
|
info_line(0, _("Solar midnight:"), times[midnight_index], midnight_index, face, request_easy) ..
|
|
|
|
-- add fl toggle
|
|
|
|
add_line(0, "", not self.fl_off_during_day) ..
|
|
|
|
add_line(0, _("Toggle frontlight off between"), not self.fl_off_during_day) ..
|
2022-12-02 20:44:45 +00:00
|
|
|
add_line(4, T(_("%1 and %2"),
|
2023-11-01 21:10:52 +00:00
|
|
|
times[5] and self:hoursToClock(times[5] + self.fl_off_during_day_offset_s * (1/3600)) or "",
|
|
|
|
times[7] and self:hoursToClock(times[7] - self.fl_off_during_day_offset_s * (1/3600)) or ""),
|
2022-11-11 22:47:51 +00:00
|
|
|
not self.fl_off_during_day),
|
2021-09-25 09:02:10 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
-- title
|
|
|
|
-- location: add a location string
|
2022-07-28 04:22:25 +00:00
|
|
|
-- activator: nil .. current_times_h,
|
2021-09-25 09:02:10 +00:00
|
|
|
-- activate_sun .. sun times
|
|
|
|
-- activate_schedule .. scheduler times
|
|
|
|
function AutoWarmth:getTimesMenu(title, location, activator)
|
|
|
|
return {
|
2021-10-18 19:15:12 +00:00
|
|
|
enabled_func = function()
|
|
|
|
-- always show sun position times so you can see ephemeris
|
|
|
|
return self.activate ~= 0 and (self.activate ~= activate_sun or activator == nil)
|
|
|
|
or activator == activate_sun
|
|
|
|
end,
|
2021-09-25 09:02:10 +00:00
|
|
|
text_func = function()
|
|
|
|
if location then
|
|
|
|
return title .. " " .. self:getLocationString()
|
|
|
|
end
|
|
|
|
return title
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
self:showTimesInfo(title, location, activator, self.easy_mode)
|
|
|
|
end,
|
|
|
|
keep_menu_open = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoWarmth:getLocationString()
|
|
|
|
if self.location ~= "" then
|
|
|
|
return self.location
|
|
|
|
else
|
2022-07-06 14:56:30 +00:00
|
|
|
return string.format("(%.2f°,%.2f°)", self.latitude, self.longitude)
|
2021-09-25 09:02:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return AutoWarmth
|