From 43f14b313fe1fb967670ba0cb036356b2f8f7992 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Tue, 1 Feb 2022 21:15:49 +0100 Subject: [PATCH] AltStatusBar: take the PowerCover into account in the battery level (#8741) Don't show [+] in top status line when device is charging from a power cover, but the sum of both battery levels. --- .../reader/modules/readercoptlistener.lua | 6 +++--- .../apps/reader/modules/readerrolling.lua | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/frontend/apps/reader/modules/readercoptlistener.lua b/frontend/apps/reader/modules/readercoptlistener.lua index ff8858a0f..c5fdbf69c 100644 --- a/frontend/apps/reader/modules/readercoptlistener.lua +++ b/frontend/apps/reader/modules/readercoptlistener.lua @@ -50,14 +50,14 @@ function ReaderCoptListener:onReadSettings(config) local status_line = config:readSetting("copt_status_line") or G_reader_settings:readSetting("copt_status_line", 1) self.ui:handleEvent(Event:new("SetStatusLine", status_line)) - self.old_battery_level = Device:getPowerDevice():getCapacity() + self.old_battery_level = self.ui.rolling:updateBatteryState() -- Have this ready in case auto-refresh is enabled, now or later self.headerRefresh = function() -- Only draw it if the header is shown... if self.document.configurable.status_line == 0 and self.view.view_mode == "page" then -- ...and something has changed - local new_battery_level = Device:getPowerDevice():getCapacity() + local new_battery_level = self.ui.rolling:updateBatteryState() if self.clock == 1 or (self.battery == 1 and new_battery_level ~= self.old_battery_level) then self.old_battery_level = new_battery_level self:updateHeader() @@ -92,7 +92,6 @@ end function ReaderCoptListener:updateHeader() -- Have crengine display accurate time and battery on its next drawing - self.ui.rolling:updateBatteryState() self.ui.document:resetBufferCache() -- be sure next repaint is a redrawing -- Force a refresh if we're not hidden behind another widget if UIManager:getTopWidget() == "ReaderUI" then @@ -165,6 +164,7 @@ function ReaderCoptListener:setAndSave(setting, property, value) self.ui.document._document:setIntProperty(property, value) G_reader_settings:saveSetting(setting, value) -- Have crengine redraw it (even if hidden by the menu at this time) + self.ui.rolling:updateBatteryState() self:updateHeader() -- And see if we should auto-refresh self:rescheduleHeaderRefreshIfNeeded() diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index 2fa882f4a..d4307d946 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -1106,12 +1106,30 @@ function ReaderRolling:updateBatteryState() if self.view.view_mode == "page" and self.cre_top_bar_enabled then logger.dbg("update battery state") local powerd = Device:getPowerDevice() + local main_batt_lvl = powerd:getCapacity() -- -1 is CR_BATTERY_STATE_CHARGING @ crengine/crengine/include/lvdocview.h - local state = powerd:isCharging() and -1 or powerd:getCapacity() + local state = powerd:isCharging() and -1 or main_batt_lvl + if powerd.device:hasAuxBattery() and powerd:isAuxBatteryConnected() and + not powerd:isAuxCharging() then + -- The first few reads after connecting to the PowerCover may fail, so default to zero + local aux_batt_lvl = powerd:getAuxCapacity() + -- If aux_battery not charging, but present -> don't show '[ + ]' in header + -- but show the average (as both battery have the same maximum capacity). + if G_reader_settings:readSetting("cre_header_battery_percent") ~= 0 then + -- if percentage is wanted, show the total capacity of reader plus power-cover + state = main_batt_lvl + aux_batt_lvl + else + -- if icon is wanted, show the total average capacity of reader and power-cover + -- (as this is the shows graphically what capacity is left in total) + state = math.floor((main_batt_lvl + aux_batt_lvl) / 2) + end + end if state then self.ui.document:setBatteryState(state) end + return state end + return 0 end function ReaderRolling:handleEngineCallback(ev, ...)