Ignore invalid battery level updates

This commit is contained in:
José Rebelo
2025-04-08 22:35:48 +01:00
parent 85b9028893
commit 181d00fd08
2 changed files with 19 additions and 9 deletions
@@ -20,6 +20,8 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
import android.content.Context;
import androidx.annotation.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,9 +56,18 @@ public class GBDeviceEventBatteryInfo extends GBDeviceEvent {
return numCharges != -1 && lastChargeTime != null;
}
@NonNull
@Override
public String toString() {
return super.toString() + "index: " + batteryIndex + ", level: " + level;
}
@Override
public void evaluate(final Context context, final GBDevice device) {
LOG.info("Got BATTERY_INFO device event");
if ((level < 0 || level >= 100) && level != GBDevice.BATTERY_UNKNOWN) {
LOG.error("Battery level must be within range 0-100: {}", level);
return;
}
// for devices that do not report charging, but the level just increased
final boolean levelJustIncreased = device.getBatteryLevel(this.batteryIndex) != GBDevice.BATTERY_UNKNOWN &&
@@ -38,7 +38,6 @@ import java.util.HashMap;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
@@ -101,7 +100,7 @@ public class GBDevice implements Parcelable {
private int mNotificationIconDisconnected = R.drawable.ic_notification_disconnected;
private int mNotificationIconLowBattery = R.drawable.ic_notification_low_battery;
public static enum DeviceUpdateSubject {
public enum DeviceUpdateSubject {
UNKNOWN,
NOTHING,
CONNECTION_STATE,
@@ -241,7 +240,7 @@ public class GBDevice implements Parcelable {
}
public String getAliasOrName() {
if (mAlias != null && !mAlias.equals("")) {
if (mAlias != null && !mAlias.isEmpty()) {
return mAlias;
}
return mName;
@@ -280,7 +279,6 @@ public class GBDevice implements Parcelable {
/**
* Sets the second firmware version (HR or GPS or other component)
* @param firmwareVersion2
*/
public void setFirmwareVersion2(String firmwareVersion2) {
mFirmwareVersion2 = firmwareVersion2;
@@ -560,7 +558,7 @@ public class GBDevice implements Parcelable {
if ((batteryLevel >= 0 && batteryLevel <= 100) || batteryLevel == BATTERY_UNKNOWN) {
mBatteryLevel[index] = batteryLevel;
} else {
LOG.error("Battery level musts be within range 0-100: " + batteryLevel);
LOG.error("Battery level must be within range 0-100: {}", batteryLevel);
}
}
@@ -573,7 +571,7 @@ public class GBDevice implements Parcelable {
if (batteryVoltage >= 0 || batteryVoltage == BATTERY_UNKNOWN) {
mBatteryVoltage[index] = batteryVoltage;
} else {
LOG.error("Battery voltage must be > 0: " + batteryVoltage);
LOG.error("Battery voltage must be > 0: {}", batteryVoltage);
}
}
@@ -651,7 +649,7 @@ public class GBDevice implements Parcelable {
}
public boolean hasDeviceInfos() {
return getDeviceInfos().size() > 0;
return !getDeviceInfos().isEmpty();
}
public ItemWithDetails getDeviceInfo(String name) {
@@ -730,7 +728,8 @@ public class GBDevice implements Parcelable {
INITIALIZED(R.string.initialized, R.string.connected);
private int stringId, simpleStringId;
private final int stringId;
private final int simpleStringId;
State(int stringId, int simpleStringId) {
this.stringId = stringId;