Skip to content

Commit 016f3bf

Browse files
committed
Fix: Read the charger-maximum extras by their literal keys
BatteryManager.EXTRA_MAX_CHARGING_CURRENT/VOLTAGE are @hide constants: BatteryService puts both into ACTION_BATTERY_CHANGED, but the symbols are absent from the public SDK's android.jar, so referencing them fails to compile. The extras themselves are readable (only the constants are missing), so the reader uses the stable AOSP string keys instead. No reflection is involved.
1 parent ac3f158 commit 016f3bf

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

app/src/main/java/eu/darken/amply/battery/core/BatteryReader.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class BatteryReader @Inject constructor(
5151
currentNowMicroamps = manager.propertyOrAbsent(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW),
5252
chargeCounterMicroampHours = manager.propertyOrAbsent(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER),
5353
cycleCount = cycleCount(battery),
54-
maxChargingCurrentMicroamps = battery.getIntExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, ABSENT),
55-
maxChargingVoltageMicrovolts = battery.getIntExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, ABSENT),
54+
maxChargingCurrentMicroamps = battery.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, ABSENT),
55+
maxChargingVoltageMicrovolts = battery.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, ABSENT),
5656
)
5757
}
5858

@@ -68,5 +68,11 @@ class BatteryReader @Inject constructor(
6868

6969
// BatteryManager.EXTRA_CYCLE_COUNT — inlined to avoid a hard API-34 symbol reference.
7070
const val EXTRA_CYCLE_COUNT = "android.os.extra.CYCLE_COUNT"
71+
72+
// BatteryManager.EXTRA_MAX_CHARGING_CURRENT/VOLTAGE are @hide: BatteryService puts them into
73+
// ACTION_BATTERY_CHANGED, but the constants are absent from the public SDK. The literal keys
74+
// are stable AOSP identifiers.
75+
const val EXTRA_MAX_CHARGING_CURRENT = "max_charging_current"
76+
const val EXTRA_MAX_CHARGING_VOLTAGE = "max_charging_voltage"
7177
}
7278
}

app/src/main/java/eu/darken/amply/battery/core/BatteryReadout.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ data class BatteryReadout(
2424
val chargeCounterMicroampHours: Int? = null,
2525
val cycleCount: Int? = null,
2626
/**
27-
* Charger-advertised maximum, not a measurement: what the connected supply says it can deliver
28-
* ([android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT] / `EXTRA_MAX_CHARGING_VOLTAGE`). Only
29-
* meaningful while something is connected, and never a substitute for the measured draw.
27+
* Charger-advertised maximum, not a measurement: what the connected supply says it can deliver.
28+
* Only meaningful while something is connected, and never a substitute for the measured draw.
29+
*
30+
* Sourced from the `max_charging_current` / `max_charging_voltage` extras of
31+
* [android.content.Intent.ACTION_BATTERY_CHANGED]. The framework populates them, but their
32+
* `BatteryManager` constants are `@hide` and absent from the public SDK, so `BatteryReader` reads
33+
* the literal AOSP keys.
3034
*/
3135
val maxChargingCurrentMicroamps: Int? = null,
3236
val maxChargingVoltageMicrovolts: Int? = null,

0 commit comments

Comments
 (0)