From 9d09e5cd22b10fd7e37edacbe5dd10591159e6e6 Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Tue, 9 Nov 2021 00:39:00 +0100 Subject: [PATCH] Fix low battery notification spam on Android 12 --- .../Plugins/BatteryPlugin/BatteryPlugin.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java b/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java index 2c6f6935..7d4f5a65 100644 --- a/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java @@ -49,6 +49,9 @@ public class BatteryPlugin extends Plugin { } private final BroadcastReceiver receiver = new BroadcastReceiver() { + + boolean wasLowBattery = false; // will trigger a low battery notification when the device is connected + @Override public void onReceive(Context context, Intent batteryIntent) { @@ -58,8 +61,16 @@ public class BatteryPlugin extends Plugin { int currentCharge = (level == -1) ? batteryInfo.getInt("currentCharge") : level * 100 / scale; boolean isCharging = (plugged == -1) ? batteryInfo.getBoolean("isCharging") : (0 != plugged); - boolean lowBattery = Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction()); - int thresholdEvent = lowBattery ? THRESHOLD_EVENT_BATTERY_LOW : THRESHOLD_EVENT_NONE; + + int thresholdEvent = THRESHOLD_EVENT_NONE; + if (Intent.ACTION_BATTERY_OKAY.equals(batteryIntent.getAction())) { + wasLowBattery = false; + } else if (Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction())) { + if (!wasLowBattery && !isCharging) { + thresholdEvent = THRESHOLD_EVENT_BATTERY_LOW; + } + wasLowBattery = true; + } if (isCharging != batteryInfo.getBoolean("isCharging") || currentCharge != batteryInfo.getInt("currentCharge") @@ -80,6 +91,7 @@ public class BatteryPlugin extends Plugin { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); intentFilter.addAction(Intent.ACTION_BATTERY_LOW); + intentFilter.addAction(Intent.ACTION_BATTERY_OKAY); Intent currentState = context.registerReceiver(receiver, intentFilter); receiver.onReceive(context, currentState);