ReaderDeviceStatus: add high battery level alert (#8037)

reviewable/pr8071/r1
hius07 3 years ago committed by GitHub
parent 6fb52528cd
commit b622d6edd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,26 +14,38 @@ local ReaderDeviceStatus = InputContainer:new{
} }
function ReaderDeviceStatus:init() function ReaderDeviceStatus:init()
self.has_battery = powerd:getCapacity() > 0 or powerd:isCharging() if Device:hasBattery() then
if self.has_battery then
self.battery_interval = G_reader_settings:readSetting("device_status_battery_interval") or 10 self.battery_interval = G_reader_settings:readSetting("device_status_battery_interval") or 10
self.battery_threshold = G_reader_settings:readSetting("device_status_battery_threshold") or 20 self.battery_threshold = G_reader_settings:readSetting("device_status_battery_threshold") or 20
self.battery_threshold_high = G_reader_settings:readSetting("device_status_battery_threshold_high") or 100
self.checkLowBatteryLevel = function() self.checkLowBatteryLevel = function()
local is_charging = powerd:isCharging()
local battery_capacity = powerd:getCapacity() local battery_capacity = powerd:getCapacity()
if powerd:isCharging() then if powerd:getDismissBatteryStatus() == true then -- alerts dismissed
powerd:setDismissBatteryStatus(false) if (is_charging and battery_capacity <= self.battery_threshold_high) or
elseif powerd:getDismissBatteryStatus() ~= true and battery_capacity <= self.battery_threshold then (not is_charging and battery_capacity > self.battery_threshold) then
powerd:setDismissBatteryStatus(true) powerd:setDismissBatteryStatus(false)
UIManager:show(ConfirmBox:new { end
text = T(_("Low battery level: %1%\n\nDismiss low battery alarm?"), battery_capacity), else
ok_text = _("Dismiss"), if is_charging and battery_capacity > self.battery_threshold_high then
dismissable = false, UIManager:show(ConfirmBox:new {
cancel_callback = function() text = T(_("High battery level: %1%\n\nDismiss battery level alert?"), battery_capacity),
powerd:setDismissBatteryStatus(false) ok_text = _("Dismiss"),
end, dismissable = false,
}) ok_callback = function()
elseif powerd:getDismissBatteryStatus() and battery_capacity > self.battery_threshold then powerd:setDismissBatteryStatus(true)
powerd:setDismissBatteryStatus(false) end,
})
elseif not is_charging and battery_capacity <= self.battery_threshold then
UIManager:show(ConfirmBox:new {
text = T(_("Low battery level: %1%\n\nDismiss battery level alert?"), battery_capacity),
ok_text = _("Dismiss"),
dismissable = false,
ok_callback = function()
powerd:setDismissBatteryStatus(true)
end,
})
end
end end
UIManager:scheduleIn(self.battery_interval * 60, self.checkLowBatteryLevel) UIManager:scheduleIn(self.battery_interval * 60, self.checkLowBatteryLevel)
end end
@ -101,10 +113,10 @@ function ReaderDeviceStatus:addToMainMenu(menu_items)
text = _("Device status alerts"), text = _("Device status alerts"),
sub_item_table = {}, sub_item_table = {},
} }
if self.has_battery then if Device:hasBattery() then
table.insert(menu_items.device_status_alarm.sub_item_table, table.insert(menu_items.device_status_alarm.sub_item_table,
{ {
text = _("Low battery level"), text = _("Battery level"),
checked_func = function() checked_func = function()
return G_reader_settings:isTrue("device_status_battery_alarm") return G_reader_settings:isTrue("device_status_battery_alarm")
end, end,
@ -149,28 +161,44 @@ function ReaderDeviceStatus:addToMainMenu(menu_items)
table.insert(menu_items.device_status_alarm.sub_item_table, table.insert(menu_items.device_status_alarm.sub_item_table,
{ {
text_func = function() text_func = function()
return T(_("Threshold (%1%)"), self.battery_threshold) return T(_("Thresholds (%1% %2%)"), self.battery_threshold, self.battery_threshold_high)
end, end,
enabled_func = function() enabled_func = function()
return G_reader_settings:isTrue("device_status_battery_alarm") return G_reader_settings:isTrue("device_status_battery_alarm")
end, end,
keep_menu_open = true, keep_menu_open = true,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
UIManager:show(SpinWidget:new{ local DoubleSpinWidget = require("/ui/widget/doublespinwidget")
width = math.floor(Screen:getWidth() * 0.6), local thresholds_widget = DoubleSpinWidget:new{
value = self.battery_threshold, title_text = _("Battery level alert thresholds"),
value_min = 1, info_text = _([[
value_max = 99, Low level threshold is checked when the device is not charging.
default_value = 20, High level threshold is checked when the device is charging.]]),
value_hold_step = 5, width = math.floor(Screen:getWidth() * 0.8),
title_text = _("Battery alarm threshold"), left_text = _("Low"),
callback = function(spin) left_value = self.battery_threshold,
self.battery_threshold = spin.value left_min = 1,
left_max = self.battery_threshold_high,
left_default = 20,
left_hold_step = 5,
right_text = _("High"),
right_value = self.battery_threshold_high,
right_min = self.battery_threshold,
right_max = 100,
right_default = 100,
right_hold_step = 5,
default_values = true,
callback = function(left_value, right_value)
if not left_value then return end -- "Use defaults" pressed
self.battery_threshold = left_value
self.battery_threshold_high = right_value
G_reader_settings:saveSetting("device_status_battery_threshold", self.battery_threshold) G_reader_settings:saveSetting("device_status_battery_threshold", self.battery_threshold)
G_reader_settings:saveSetting("device_status_battery_threshold_high", self.battery_threshold_high)
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
powerd:setDismissBatteryStatus(false) powerd:setDismissBatteryStatus(false)
end, end,
}) }
UIManager:show(thresholds_widget)
end, end,
separator = true, separator = true,
}) })
@ -236,7 +264,7 @@ function ReaderDeviceStatus:addToMainMenu(menu_items)
default_value = 100, default_value = 100,
value_step = 5, value_step = 5,
value_hold_step = 10, value_hold_step = 10,
title_text = _("Memory alarm threshold"), title_text = _("Memory alert threshold"),
callback = function(spin) callback = function(spin)
self.memory_threshold = spin.value self.memory_threshold = spin.value
G_reader_settings:saveSetting("device_status_memory_threshold", self.memory_threshold) G_reader_settings:saveSetting("device_status_memory_threshold", self.memory_threshold)

Loading…
Cancel
Save