diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java index f4b90c710..aba72e415 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java @@ -238,4 +238,9 @@ public abstract class HuaweiBRCoordinator extends AbstractBLClassicDeviceCoordin public Class getDeviceSupportClass() { return HuaweiBRSupport.class; } + + @Override + public boolean addBatteryPollingSettings() { + return true; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java index 87f4c4697..cd641c16a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java @@ -241,4 +241,9 @@ public abstract class HuaweiLECoordinator extends AbstractBLEDeviceCoordinator i public Class getDeviceSupportClass() { return HuaweiLESupport.class; } + + @Override + public boolean addBatteryPollingSettings() { + return true; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java index 6d45b488e..b753a73c0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java @@ -22,6 +22,7 @@ import android.content.SharedPreferences; import android.location.Location; import android.net.Uri; import android.os.Handler; +import android.os.Looper; import android.widget.Toast; import androidx.annotation.NonNull; @@ -173,6 +174,12 @@ public class HuaweiSupportProvider { private Context context; private HuaweiCoordinatorSupplier.HuaweiDeviceType huaweiType; + private final Handler handler = new Handler(Looper.getMainLooper()); + private final Runnable batteryRunner = () -> { + LOG.info("Running retrieving battery through runner."); + getBatteryLevel(); + }; + private boolean firstConnection = false; protected byte protocolVersion; public String deviceMac; //get it from GB @@ -1012,6 +1019,18 @@ public class HuaweiSupportProvider { // But it will disappear after reconnection - until it is enabled again GB.toast(context, context.getString(R.string.toast_setting_requires_reconnect), Toast.LENGTH_SHORT, GB.INFO); } + case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE: + if (!GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE, false)) { + stopBatteryRunnerDelayed(); + break; + } + // Fall through if enabled + case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_INTERVAL: + if (!startBatteryRunnerDelayed()) { + GB.toast(context, R.string.battery_polling_failed_start, Toast.LENGTH_SHORT, GB.ERROR); + LOG.error("Failed to start the battery polling"); + } + break; case ActivityUser.PREF_USER_WEIGHT_KG: case ActivityUser.PREF_USER_HEIGHT_CM: case ActivityUser.PREF_USER_GENDER: @@ -1050,7 +1069,7 @@ public class HuaweiSupportProvider { LOG.warn("Recorded data type {} not implemented yet.", dataTypes); } - // Get the battery level, as that isn't shared nicely for now + // Get the battery level as well getBatteryLevel(); // Get the alarms as they cannot be retrieved on opening the alarm window @@ -1621,6 +1640,7 @@ public class HuaweiSupportProvider { public void getBatteryLevel() { try { + stopBatteryRunnerDelayed(); GetBatteryLevelRequest batteryLevelReq = new GetBatteryLevelRequest(this); batteryLevelReq.doPerform(); } catch (IOException e) { @@ -2022,4 +2042,21 @@ public class HuaweiSupportProvider { } } } + + public boolean startBatteryRunnerDelayed() { + String interval_minutes = GBApplication.getDeviceSpecificSharedPrefs(deviceMac).getString(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_INTERVAL, "15"); + int interval = Integer.parseInt(interval_minutes) * 60 * 1000; + LOG.debug("Starting battery runner delayed by {} ({} minutes)", interval, interval_minutes); + handler.removeCallbacks(batteryRunner); + return handler.postDelayed(batteryRunner, interval); + } + + public void stopBatteryRunnerDelayed() { + LOG.debug("Stopping battery runner delayed"); + handler.removeCallbacks(batteryRunner); + } + + public void dispose() { + stopBatteryRunnerDelayed(); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetBatteryLevelRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetBatteryLevelRequest.java index 8b988e07f..f08723fc2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetBatteryLevelRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetBatteryLevelRequest.java @@ -16,15 +16,21 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests; +import android.widget.Toast; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig; import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider; +import nodomain.freeyourgadget.gadgetbridge.util.GB; public class GetBatteryLevelRequest extends Request { private static final Logger LOG = LoggerFactory.getLogger(GetBatteryLevelRequest.class); @@ -57,5 +63,12 @@ public class GetBatteryLevelRequest extends Request { GBDeviceEventBatteryInfo batteryInfo = new GBDeviceEventBatteryInfo(); batteryInfo.level = (int)batteryLevel & 0xff; this.supportProvider.evaluateGBDeviceEvent(batteryInfo); + + if (GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE, false)) { + if (!this.supportProvider.startBatteryRunnerDelayed()) { + GB.toast(getContext(), R.string.battery_polling_failed_start, Toast.LENGTH_SHORT, GB.ERROR); + LOG.error("Failed to start the battery polling"); + } + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5083857a9..785e57fe7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2924,6 +2924,7 @@ Enable battery polling Battery polling interval every %1$s minutes + Failed to start the battery polling None AGPS %1$d URL