2
0
mirror of https://github.com/koreader/koreader synced 2024-11-11 19:11:14 +00:00
koreader/frontend/device/android/powerd.lua
NiLuJe 7e98b9de4b
PM: Minor refactor to suspend/resume code flow (#10426)
Make sure we only send Suspend/Resume events when we *actually* suspend/resume. This is done via the Device `_beforeSuspend`/`_afterResume` methods, and those were called by the *input handlers*, not the PM logic; which means they would fire, while the PM logic could actually take a smarter decision and *not* do what the event just sent implied ;).

(i.e., sleep with a cover -> suspend + actual suspend, OK; but if you then resume with a button -> input assumes resume, but PM will actually suspend again!).

Existing design issue made more apparent by #9448 ;).

Also fixes/generalizes a few corner-cases related to screen_saver_lock handling (e.g., don't allow USBMS during a lock).

And deal with the fallout of the main change to the Kobo frontlight ramp behavior ;).
2023-05-18 23:13:43 +02:00

91 lines
2.5 KiB
Lua

local BasePowerD = require("device/generic/powerd")
local Event = require("ui/event")
local UIManager
local _, android = pcall(require, "android")
local AndroidPowerD = BasePowerD:new{
fl_min = 0,
fl_max = 100,
}
-- Let the footer know of the change
local function broadcastLightChanges()
if UIManager then
UIManager:broadcastEvent(Event:new("FrontlightStateChanged"))
end
end
function AndroidPowerD:frontlightIntensityHW()
return math.floor(android.getScreenBrightness() / self.bright_diff * self.fl_max)
end
function AndroidPowerD:setIntensityHW(intensity)
-- if frontlight switch was toggled of, turn it on
android.enableFrontlightSwitch()
self.fl_intensity = intensity
android.setScreenBrightness(math.floor(intensity * self.bright_diff / self.fl_max))
end
function AndroidPowerD:init()
local min_bright = android.getScreenMinBrightness()
self.bright_diff = android.getScreenMaxBrightness() - min_bright
-- if necessary scale fl_min:
-- do not use fl_min==0 if getScreenMinBrightness!=0,
-- because intenstiy==0 would mean to use system intensity
if min_bright ~= self.fl_min then
self.fl_min = math.ceil(min_bright * self.bright_diff / self.fl_max)
end
if self.device:hasNaturalLight() then
self.fl_warmth_min = android.getScreenMinWarmth()
self.fl_warmth_max = android.getScreenMaxWarmth()
self.warm_diff = self.fl_warmth_max - self.fl_warmth_min
end
end
function AndroidPowerD:setWarmthHW(warmth)
android.setScreenWarmth(warmth)
end
function AndroidPowerD:frontlightWarmthHW()
return android.getScreenWarmth() * self.warm_diff
end
function AndroidPowerD:getCapacityHW()
return android.getBatteryLevel()
end
function AndroidPowerD:isChargingHW()
return android.isCharging()
end
function AndroidPowerD:turnOffFrontlightHW()
if not self:isFrontlightOnHW() then
return
end
android.setScreenBrightness(self.fl_min)
self.is_fl_on = false
broadcastLightChanges()
end
function AndroidPowerD:turnOnFrontlightHW()
if self:isFrontlightOn() and self:isFrontlightOnHW() then
return
end
-- on devices with a software frontlight switch (e.g Tolinos), enable it
android.enableFrontlightSwitch()
android.setScreenBrightness(math.floor(self.fl_intensity * self.bright_diff / self.fl_max))
self.is_fl_on = true
broadcastLightChanges()
end
function AndroidPowerD:UIManagerReadyHW(uimgr)
UIManager = uimgr
end
return AndroidPowerD