feat(hud): add battery runtime estimate to DD overlay ( hud ) (#2573)

This commit is contained in:
LunaDL authored and GitHub committed 2026-11-13 21:41:44 -05:00
1 parent 30d103280b
commit f502b8b3e6
43 files changed
+380 -146

No files matched your search

@@ -37,12 +37,14 @@ class MangoReadingsTest {
assertEquals(100, m.cpu(stat)!!.percent)
}
private fun battery(status: String, chargeNow: Long, chargeFull: Long, powerUw: Long) {
private fun battery(status: String, chargeNow: Long, chargeFull: Long, powerUw: Long, currentUa: Long = 1_000_000L) {
val dir = File(tmp.root, "sys/power_supply/BAT1").apply { mkdirs() }
File(dir, "status").writeText("$status\n")
File(dir, "charge_now").writeText("$chargeNow\n")
File(dir, "charge_full").writeText("$chargeFull\n")
File(dir, "power_now").writeText("$powerUw\n")
File(dir, "current_now").writeText("$currentUa\n")
File(dir, "voltage_now").writeText("4000000\n")
}
@Test fun batteryHidesWattsWhileChargingAsMangoHudDoes() {
@@ -50,13 +52,42 @@ class MangoReadingsTest {
val discharging = readings().battery()!!
assertEquals(94, discharging.percent)
assertEquals(8.76f, discharging.watts, 0.001f)
assertEquals("3 hrs 37 mins", discharging.runtimeText)
assertFalse(discharging.charging)
battery("Charging", 3_622_942, 3_854_193, 8_760_000)
val charging = readings().battery()!!
assertEquals(0f, charging.watts, 0f)
assertEquals("Charging", charging.runtimeText)
assertTrue(charging.charging)
}
@Test fun dualBatteryRuntimeSumsBothPacksAndBothCurrents() {
battery("Discharging", 2_000_000, 3_000_000, 2_000_000, 500_000)
val second = File(tmp.root, "sys/power_supply/BAT2").apply { mkdirs() }
File(second, "status").writeText("Discharging\n")
File(second, "charge_now").writeText("2000000\n")
File(second, "charge_full").writeText("3000000\n")
File(second, "power_now").writeText("2000000\n")
File(second, "current_now").writeText("500000\n")
File(second, "voltage_now").writeText("4000000\n")
val result = readings().battery()!!
assertEquals(67, result.percent)
assertEquals("4 hrs", result.runtimeText)
assertEquals(4f, result.watts, 0.001f)
}
@Test fun energyOnlyBatteryConvertsEnergyToChargeBeforeEstimatingRuntime() {
val dir = File(tmp.root, "sys/power_supply/BAT1").apply { mkdirs() }
File(dir, "status").writeText("Discharging\n")
File(dir, "energy_now").writeText("4000000\n")
File(dir, "energy_full").writeText("5000000\n")
File(dir, "current_now").writeText("1000000\n")
File(dir, "voltage_now").writeText("4000000\n")
val result = readings().battery()!!
assertEquals(80, result.percent)
assertEquals("1 hr", result.runtimeText)
}
@Test fun noBatteryFolderMeansNoReading() {
assertNull(readings().battery())
}
@@ -0,0 +1,25 @@
package com.droiddeck.launcher.session
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Test
class BatteryRuntimeEstimatorTest {
@Test fun estimateUsesChargeDividedByRollingMeanCurrent() {
val estimator = BatteryRuntimeEstimator()
assertEquals(1.0, estimator.estimateHours(1_000_000.0, 1_000_000.0)!!, 0.0001)
assertEquals(1.3333, estimator.estimateHours(2_000_000.0, 2_000_000.0)!!, 0.001)
}
@Test fun energyAndPowerFallbacksKeepMicroUnitsConsistent() {
assertEquals(1_000_000.0, BatteryRuntimeEstimator.chargeMicroampHoursFromEnergy(4_000_000.0, 4_000_000.0)!!, 0.0)
assertEquals(1_000_000.0, BatteryRuntimeEstimator.currentMicroampsFromPower(4_000_000.0, 4_000_000.0)!!, 0.0)
}
@Test fun durationFormattingUsesHoursAndMinutes() {
assertEquals("2 hrs 15 mins", BatteryRuntimeEstimator.formatDuration(2.25))
assertEquals("1 hr", BatteryRuntimeEstimator.formatDuration(1.0))
assertEquals("5 mins", BatteryRuntimeEstimator.formatDuration(5.0 / 60.0))
assertNotNull(BatteryRuntimeEstimator.formatDuration(0.01))
}
}