mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
feat(hud): add battery runtime estimate to DD overlay ( hud ) (#2573)
This commit is contained in:
1 parent
30d103280b
commit
f502b8b3e6
43 files changed
+380
-146
No files matched your search
@@ -11,6 +11,8 @@ import kotlin.math.abs
|
||||
class BatteryComponent(val dir: File, private val vpowerDir: File) : SessionPart() {
|
||||
@Volatile private var running = false
|
||||
private var thread: Thread? = null
|
||||
private val runtimeEstimator = BatteryRuntimeEstimator()
|
||||
private var lastDischarging: Boolean? = null
|
||||
|
||||
override fun start() {
|
||||
File(dir, "BAT0").deleteRecursively()
|
||||
@@ -49,11 +51,17 @@ class BatteryComponent(val dir: File, private val vpowerDir: File) : SessionPart
|
||||
val currentAvgUa = toMicroamps(bm?.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)?.takeIf { it != Long.MIN_VALUE && it != 0L } ?: 0L)
|
||||
.takeIf { it != 0L } ?: currentNowUa
|
||||
val discharging = status == BatteryManager.BATTERY_STATUS_DISCHARGING || status == BatteryManager.BATTERY_STATUS_NOT_CHARGING
|
||||
if (lastDischarging != null && lastDischarging != discharging) runtimeEstimator.reset()
|
||||
lastDischarging = discharging
|
||||
val currentForRuntimeUa = currentNowUa.takeIf { it != 0L } ?: currentAvgUa
|
||||
val chargeFullUah = if (chargeNowUah > 0 && pct > 0) chargeNowUah * 100 / pct else 0L
|
||||
val energyNowUwh = chargeNowUah * voltageUv / 1_000_000L
|
||||
val energyFullUwh = chargeFullUah * voltageUv / 1_000_000L
|
||||
val powerNowUw = abs(currentNowUa) * voltageUv / 1_000_000L
|
||||
val timeToEmptyS = if (discharging && chargeNowUah > 0 && abs(currentAvgUa) > 0) chargeNowUah * 3600L / abs(currentAvgUa) else 0L
|
||||
val runtimeHours = if (discharging)
|
||||
runtimeEstimator.estimateHours(chargeNowUah.toDouble(), currentForRuntimeUa.toDouble())
|
||||
else null
|
||||
val timeToEmptyS = BatteryRuntimeEstimator.secondsFromHours(runtimeHours)
|
||||
val charging = status == BatteryManager.BATTERY_STATUS_CHARGING
|
||||
val timeToFullS = if (charging && chargeFullUah > chargeNowUah && abs(currentAvgUa) > 0) (chargeFullUah - chargeNowUah) * 3600L / abs(currentAvgUa) else 0L
|
||||
val statusText = when (status) {
|
||||
@@ -126,9 +134,7 @@ class BatteryComponent(val dir: File, private val vpowerDir: File) : SessionPart
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SessionService"
|
||||
private const val MILLIAMP_CEILING = 10_000L
|
||||
|
||||
internal fun toMicroamps(raw: Long): Long = if (abs(raw) < MILLIAMP_CEILING) raw * 1000L else raw
|
||||
internal fun toMicroamps(raw: Long): Long = BatteryRuntimeEstimator.toMicroamps(raw)
|
||||
private const val PERIOD_MS = 5_000L
|
||||
private val VPOWER_FILES = listOf(
|
||||
"ac_status", "battery_percent", "battery_status",
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import java.util.ArrayDeque
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
class BatteryRuntimeEstimator {
|
||||
private val currentSamples = ArrayDeque<Double>()
|
||||
|
||||
@Synchronized
|
||||
fun reset() {
|
||||
currentSamples.clear()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun estimateHours(chargeNowUah: Double, currentSampleUa: Double): Double? {
|
||||
if (currentSampleUa.isFinite() && currentSampleUa != 0.0) {
|
||||
currentSamples.addLast(abs(currentSampleUa))
|
||||
while (currentSamples.size > MAX_SAMPLES) currentSamples.removeFirst()
|
||||
}
|
||||
if (!chargeNowUah.isFinite() || chargeNowUah <= 0.0 || currentSamples.isEmpty()) return null
|
||||
var sum = 0.0
|
||||
for (sample in currentSamples) sum += sample
|
||||
val averageCurrentUa = sum / currentSamples.size
|
||||
if (!averageCurrentUa.isFinite() || averageCurrentUa <= 0.0) return null
|
||||
val hours = chargeNowUah / averageCurrentUa
|
||||
return hours.takeIf { it.isFinite() && it > 0.0 }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MAX_SAMPLES = 25
|
||||
private const val MILLIAMP_CEILING = 10_000L
|
||||
|
||||
@JvmStatic
|
||||
fun toMicroamps(raw: Long): Long {
|
||||
if (raw == Long.MIN_VALUE) return 0L
|
||||
return if (abs(raw) < MILLIAMP_CEILING) raw * 1_000L else raw
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun chargeMicroampHoursFromEnergy(energyUwh: Double, voltageUv: Double): Double? {
|
||||
if (!energyUwh.isFinite() || energyUwh <= 0.0 || !voltageUv.isFinite() || voltageUv <= 0.0) return null
|
||||
return (energyUwh * 1_000_000.0 / voltageUv).takeIf { it.isFinite() && it > 0.0 }
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun currentMicroampsFromPower(powerUw: Double, voltageUv: Double): Double? {
|
||||
if (!powerUw.isFinite() || voltageUv <= 0.0 || !voltageUv.isFinite()) return null
|
||||
return (abs(powerUw) * 1_000_000.0 / voltageUv).takeIf { it.isFinite() && it > 0.0 }
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun secondsFromHours(hours: Double?): Long {
|
||||
if (hours == null || !hours.isFinite() || hours <= 0.0) return 0L
|
||||
val seconds = hours * 3_600.0
|
||||
return if (seconds.isFinite() && seconds < Long.MAX_VALUE.toDouble()) seconds.toLong() else 0L
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun formatDuration(hours: Double?): String? {
|
||||
if (hours == null || !hours.isFinite() || hours <= 0.0) return null
|
||||
val totalMinutes = (hours * 60.0).roundToLong().coerceAtLeast(1L)
|
||||
val wholeHours = totalMinutes / 60L
|
||||
val minutes = totalMinutes % 60L
|
||||
val hourUnit = if (wholeHours == 1L) "hr" else "hrs"
|
||||
val minuteUnit = if (minutes == 1L) "min" else "mins"
|
||||
return when {
|
||||
wholeHours > 0L && minutes > 0L -> "$wholeHours $hourUnit $minutes $minuteUnit"
|
||||
wholeHours > 0L -> "$wholeHours $hourUnit"
|
||||
else -> "$minutes $minuteUnit"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user