2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Fix low battery notification spam on Android 12

This commit is contained in:
Albert Vaca Cintora
2021-11-09 00:39:00 +01:00
parent 3fa5e041cf
commit 9d09e5cd22

View File

@@ -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);