handle plugged state correctly

This commit is contained in:
Milos Kozak 2023-02-18 17:11:22 +01:00
parent e5a9cc5a10
commit 36bbb0754c

View file

@ -32,12 +32,15 @@ class ChargingStateReceiver : DaggerBroadcastReceiver() {
var batteryLevel = 0
val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
val plugged = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1
if (level != -1 && scale != -1)
batteryLevel = (level.toFloat() / scale.toFloat() * 100.0f).toInt()
// Status
val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL
// Plugged
val isCharging: Boolean =
plugged == BatteryManager.BATTERY_PLUGGED_AC ||
plugged == BatteryManager.BATTERY_PLUGGED_USB ||
plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS ||
plugged == BatteryManager.BATTERY_PLUGGED_DOCK
return EventChargingState(isCharging, batteryLevel).also { receiverStatusStore.lastChargingEvent = it }
}