Several minor fixes (#3057)

1. Android.getScreenBrightness() is used in frontend/device/android/powerd.lua to retrieve the frontlight intensity.
2. kosync.koplugin won't save settings for whisper sync.
3. batterstat.koplugin won't work correctly if several on* events are fired.
pull/3060/head
Hzj_jie 7 years ago committed by Frans de Jonge
parent 0d38547990
commit a8513c95b6

@ -6,7 +6,8 @@ local AndroidPowerD = BasePowerD:new{
fl_intensity = 10,
}
function AndroidPowerD:init()
function AndroidPowerD:frontlightIntensityHW()
return android.getScreenBrightness()
end
function AndroidPowerD:setIntensityHW(intensity)

@ -168,28 +168,36 @@ end
function BatteryStat:onSuspend()
self:debugOutput("onSuspend")
self.was_suspending = false
self:accumulate()
if not self.was_suspending then
self:accumulate()
end
self.was_suspending = true
end
function BatteryStat:onResume()
self:debugOutput("onResume")
self.was_suspending = true
self:accumulate()
if self.was_suspending then
self:accumulate()
end
self.was_suspending = false
end
function BatteryStat:onCharging()
self:debugOutput("onCharging")
self.was_charging = false
self:reset(true, false)
self:accumulate()
if not self.was_charging then
self:reset(true, false)
self:accumulate()
end
self.was_charging = true
end
function BatteryStat:onNotCharging()
self:debugOutput("onNotCharging")
self.was_charging = true
self:reset(false, true)
self:accumulate()
if self.was_charging then
self:reset(false, true)
self:accumulate()
end
self.was_charging = false
end
function BatteryStat:showStatistics()
@ -277,6 +285,8 @@ local BatteryStatWidget = WidgetContainer:new{
}
function BatteryStatWidget:init()
-- self.ui is nil in test cases.
if not self.ui or not self.ui.menu then return end
self.ui.menu:registerToMainMenu(self)
end
@ -309,4 +319,9 @@ function BatteryStatWidget:onNotCharging()
BatteryStat:onNotCharging()
end
-- Test only
function BatteryStatWidget:stat()
return BatteryStat
end
return BatteryStatWidget

@ -131,7 +131,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_forward == SYNC_STRATEGY.WHISPER
end,
callback = function()
self.kosync_whisper_forward = SYNC_STRATEGY.WHISPER
self:setWhisperForward(SYNC_STRATEGY.WHISPER)
end,
},
{
@ -140,7 +140,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_forward == SYNC_STRATEGY.PROMPT
end,
callback = function()
self.kosync_whisper_forward = SYNC_STRATEGY.PROMPT
self:setWhisperForward(SYNC_STRATEGY.PROMPT)
end,
},
{
@ -149,7 +149,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_forward == SYNC_STRATEGY.DISABLE
end,
callback = function()
self.kosync_whisper_forward = SYNC_STRATEGY.DISABLE
self:setWhisperForward(SYNC_STRATEGY.DISABLE)
end,
},
{
@ -162,7 +162,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_backward == SYNC_STRATEGY.WHISPER
end,
callback = function()
self.kosync_whisper_backward = SYNC_STRATEGY.WHISPER
self:setWhisperBackward(SYNC_STRATEGY.WHISPER)
end,
},
{
@ -171,7 +171,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_backward == SYNC_STRATEGY.PROMPT
end,
callback = function()
self.kosync_whisper_backward = SYNC_STRATEGY.PROMPT
self:setWhisperBackward(SYNC_STRATEGY.PROMPT)
end,
},
{
@ -180,7 +180,7 @@ function KOSync:addToMainMenu(menu_items)
return self.kosync_whisper_backward == SYNC_STRATEGY.DISABLE
end,
callback = function()
self.kosync_whisper_backward = SYNC_STRATEGY.DISABLE
self:setWhisperBackward(SYNC_STRATEGY.DISABLE)
end,
},
},
@ -226,6 +226,16 @@ function KOSync:setCustomServer(server)
self:saveSettings()
end
function KOSync:setWhisperForward(strategy)
self.kosync_whisper_forward = strategy
self:saveSettings()
end
function KOSync:setWhisperBackward(strategy)
self.kosync_whisper_backward = strategy
self:saveSettings()
end
function KOSync:login()
if not NetworkMgr:isOnline() then
NetworkMgr:promptWifiOn()

@ -0,0 +1,185 @@
describe("BatteryState plugin tests", function()
local MockTime, module
local stat = function()
return module:new():stat()
end,
setup(function()
require("commonrequire")
package.unloadAll()
MockTime = require("mock_time")
MockTime:install()
end)
teardown(function()
MockTime:uninstall()
package.unloadAll()
end)
before_each(function()
module = dofile("plugins/batterystat.koplugin/main.lua")
end)
it("should record charging time", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onNotCharging()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & discharging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
end)
it("should record suspending time", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onResume()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
it("should not swap the state when several charging events fired", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(2, widget.charging.time)
end)
it("should not swap the state when several suspending events fired", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(3, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
end)
Loading…
Cancel
Save