2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2016-06-23 17:50:24 +00:00
|
|
|
|
2014-01-04 13:38:07 +00:00
|
|
|
local BasePowerD = {
|
2017-03-24 06:36:15 +00:00
|
|
|
fl_min = 0, -- min frontlight intensity
|
|
|
|
fl_max = 10, -- max frontlight intensity
|
|
|
|
fl_intensity = nil, -- frontlight intensity
|
|
|
|
battCapacity = 0, -- battery capacity
|
|
|
|
device = nil, -- device object
|
2014-07-07 02:50:16 +00:00
|
|
|
|
2017-03-24 06:36:15 +00:00
|
|
|
last_capacity_pull_time = 0, -- timestamp of last pull
|
2017-06-14 17:32:16 +00:00
|
|
|
|
|
|
|
is_fl_on = false, -- whether the frontlight is on
|
2014-01-04 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function BasePowerD:new(o)
|
2016-02-16 07:10:07 +00:00
|
|
|
o = o or {}
|
2014-03-13 13:52:43 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
2017-06-14 17:32:16 +00:00
|
|
|
assert(o.fl_min < o.fl_max)
|
2014-03-13 13:52:43 +00:00
|
|
|
if o.init then o:init() end
|
2017-06-14 17:32:16 +00:00
|
|
|
if o.device and o.device.hasFrontlight() then
|
|
|
|
o.fl_intensity = o:frontlightIntensityHW()
|
|
|
|
o:_decideFrontlightState()
|
2017-10-08 14:40:24 +00:00
|
|
|
-- Note added by @Frenzie 2017-10-08
|
|
|
|
-- I believe this should be `if isKobo()`, or better yet that the entire
|
|
|
|
-- block should be moved to `KoboPowerD:init()` because afaik that is the
|
|
|
|
-- only platform where the system doesn't provide trustworthy frontlight
|
|
|
|
-- information. But to be absolutely sure that I don't break anything (and I
|
|
|
|
-- don't want to spend any time on this atm) I'm temporarily excluding only
|
|
|
|
-- Android where this behavior is known to be problematic.
|
|
|
|
-- See discussion in https://github.com/koreader/koreader/issues/3118#issuecomment-334995879
|
|
|
|
if not o.device:isAndroid() then
|
|
|
|
if o:isFrontlightOn() then
|
|
|
|
o:turnOnFrontlightHW()
|
|
|
|
else
|
|
|
|
o:turnOffFrontlightHW()
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
return o
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:init() end
|
2017-06-14 17:32:16 +00:00
|
|
|
function BasePowerD:setIntensityHW(intensity) end
|
2017-03-24 06:36:15 +00:00
|
|
|
function BasePowerD:getCapacityHW() return 0 end
|
|
|
|
function BasePowerD:isChargingHW() return false end
|
2017-06-14 17:32:16 +00:00
|
|
|
function BasePowerD:frontlightIntensityHW() return 0 end
|
2017-06-22 02:10:44 +00:00
|
|
|
function BasePowerD:isFrontlightOnHW() return self.fl_intensity > self.fl_min end
|
2017-06-20 02:00:36 +00:00
|
|
|
function BasePowerD:turnOffFrontlightHW() self:_setIntensity(self.fl_min) end
|
|
|
|
function BasePowerD:turnOnFrontlightHW() self:_setIntensity(self.fl_intensity) end
|
2016-02-26 09:46:23 +00:00
|
|
|
-- Anything needs to be done before do a real hardware suspend. Such as turn off
|
|
|
|
-- front light.
|
|
|
|
function BasePowerD:beforeSuspend() end
|
|
|
|
-- Anything needs to be done after do a real hardware resume. Such as resume
|
|
|
|
-- front light state.
|
|
|
|
function BasePowerD:afterResume() end
|
2014-01-04 13:38:07 +00:00
|
|
|
|
2017-06-14 17:32:16 +00:00
|
|
|
function BasePowerD:isFrontlightOn()
|
|
|
|
assert(self ~= nil)
|
|
|
|
return self.is_fl_on
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:_decideFrontlightState()
|
|
|
|
assert(self ~= nil)
|
|
|
|
assert(self.device.hasFrontlight())
|
2017-06-22 02:10:44 +00:00
|
|
|
self.is_fl_on = self:isFrontlightOnHW()
|
2017-06-14 17:32:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:isFrontlightOff()
|
|
|
|
return not self:isFrontlightOn()
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:frontlightIntensity()
|
|
|
|
assert(self ~= nil)
|
|
|
|
if not self.device.hasFrontlight() then return 0 end
|
|
|
|
if self:isFrontlightOff() then return 0 end
|
|
|
|
return self.fl_intensity
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:toggleFrontlight()
|
|
|
|
assert(self ~= nil)
|
|
|
|
if not self.device.hasFrontlight() then return false end
|
|
|
|
if self:isFrontlightOn() then
|
|
|
|
return self:turnOffFrontlight()
|
|
|
|
else
|
|
|
|
return self:turnOnFrontlight()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:turnOffFrontlight()
|
|
|
|
assert(self ~= nil)
|
|
|
|
if not self.device.hasFrontlight() then return end
|
|
|
|
if self:isFrontlightOff() then return false end
|
|
|
|
self.is_fl_on = false
|
2017-06-20 02:00:36 +00:00
|
|
|
self:turnOffFrontlightHW()
|
2017-06-14 17:32:16 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:turnOnFrontlight()
|
|
|
|
assert(self ~= nil)
|
|
|
|
if not self.device.hasFrontlight() then return end
|
|
|
|
if self:isFrontlightOn() then return false end
|
|
|
|
if self.fl_intensity == self.fl_min then return false end
|
|
|
|
self.is_fl_on = true
|
2017-06-20 02:00:36 +00:00
|
|
|
self:turnOnFrontlightHW()
|
2017-06-14 17:32:16 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-01-04 13:38:07 +00:00
|
|
|
function BasePowerD:read_int_file(file)
|
2014-07-15 08:45:09 +00:00
|
|
|
local fd = io.open(file, "r")
|
|
|
|
if fd then
|
|
|
|
local int = fd:read("*all"):match("%d+")
|
|
|
|
fd:close()
|
|
|
|
return int and tonumber(int) or 0
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
2015-01-17 15:54:49 +00:00
|
|
|
function BasePowerD:read_str_file(file)
|
|
|
|
local fd = io.open(file, "r")
|
|
|
|
if fd then
|
|
|
|
local str = fd:read("*all")
|
|
|
|
fd:close()
|
|
|
|
return str
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-25 08:54:41 +00:00
|
|
|
function BasePowerD:normalizeIntensity(intensity)
|
2014-03-13 13:52:43 +00:00
|
|
|
intensity = intensity < self.fl_min and self.fl_min or intensity
|
2016-02-25 08:54:41 +00:00
|
|
|
return intensity > self.fl_max and self.fl_max or intensity
|
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:setIntensity(intensity)
|
2017-06-14 17:32:16 +00:00
|
|
|
if not self.device.hasFrontlight() then return false end
|
2017-06-29 06:23:32 +00:00
|
|
|
if intensity == self:frontlightIntensity() then return false end
|
2016-03-02 06:06:21 +00:00
|
|
|
self.fl_intensity = self:normalizeIntensity(intensity)
|
2017-06-14 17:32:16 +00:00
|
|
|
self:_decideFrontlightState()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("set light intensity", self.fl_intensity)
|
2017-06-20 02:00:36 +00:00
|
|
|
self:_setIntensity(self.fl_intensity)
|
2017-06-14 17:32:16 +00:00
|
|
|
return true
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:getCapacity()
|
2017-03-24 06:36:15 +00:00
|
|
|
if os.time() - self.last_capacity_pull_time >= 60 then
|
|
|
|
self.battCapacity = self:getCapacityHW()
|
|
|
|
self.last_capacity_pull_time = os.time()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2017-03-24 06:36:15 +00:00
|
|
|
return self.battCapacity
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function BasePowerD:isCharging()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self:isChargingHW()
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
2017-06-20 02:00:36 +00:00
|
|
|
function BasePowerD:_setIntensity(intensity)
|
|
|
|
self:setIntensityHW(intensity)
|
|
|
|
-- BasePowerD is loaded before UIManager. So we cannot broadcast events before UIManager has
|
|
|
|
-- been loaded.
|
|
|
|
if package.loaded["ui/uimanager"] ~= nil then
|
|
|
|
local Event = require("ui/event")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
UIManager:broadcastEvent(Event:new("FrontlightStateChanged"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-04 13:38:07 +00:00
|
|
|
return BasePowerD
|