diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java index d12c5debb..46260f990 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java @@ -414,6 +414,8 @@ public class HuaweiPacket { return new DeviceConfig.SupportedServices.Response(paramsProvider).fromPacket(this); case DeviceConfig.SupportedCommands.id: return new DeviceConfig.SupportedCommands.Response(paramsProvider).fromPacket(this); + case DeviceConfig.TimeRequest.id: + return new DeviceConfig.TimeRequest.Response(paramsProvider).fromPacket(this); case DeviceConfig.ProductInfo.id: return new DeviceConfig.ProductInfo.Response(paramsProvider).fromPacket(this); case DeviceConfig.BondParams.id: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java index 30f6b98aa..ec0234a1e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java @@ -400,6 +400,19 @@ public class DeviceConfig { this(paramsProvider, Calendar.getInstance()); } + public static class Response extends HuaweiPacket { + public int deviceTime; + + public Response(ParamsProvider paramsProvider) { + super(paramsProvider); + } + + @Override + public void parseTlv() throws ParseException { + this.deviceTime = this.tlv.getInteger(0x01); + } + } + // TODO: implement parsing this request for the log parser support } 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 d373f1cc0..3418b1117 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 @@ -550,7 +550,6 @@ public class HuaweiSupportProvider { editor.putString(DeviceSettingsPreferenceConst.PREF_ACTIVATE_DISPLAY_ON_LIFT, "p_on"); editor.apply(); } - onSetTime(); getBatteryLevel(); sendUserInfo(); if (isBLE()) { @@ -1363,7 +1362,7 @@ public class HuaweiSupportProvider { } private Request setTime() { - SetTimeRequest setTimeReq = new SetTimeRequest(this); + SetTimeRequest setTimeReq = new SetTimeRequest(this, true); return setTimeReq; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SetTimeRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SetTimeRequest.java index 5b1b721af..e33ccd98e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SetTimeRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SetTimeRequest.java @@ -27,11 +27,13 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupport public class SetTimeRequest extends Request { private static final Logger LOG = LoggerFactory.getLogger(SetTimeRequest.class); + private boolean checkTime; - public SetTimeRequest(HuaweiSupportProvider support) { + public SetTimeRequest(HuaweiSupportProvider support, boolean syncTime) { super(support); this.serviceId = DeviceConfig.id; this.commandId = DeviceConfig.TimeRequest.id; + this.checkTime = syncTime; } @Override @@ -44,7 +46,19 @@ public class SetTimeRequest extends Request { } @Override - protected void processResponse() { + protected void processResponse() throws ResponseTypeMismatchException { LOG.debug("handle Set Time"); + if (!(receivedPacket instanceof DeviceConfig.TimeRequest.Response)) + throw new ResponseTypeMismatchException(receivedPacket, DeviceConfig.TimeRequest.Response.class); + + int deviceTime = ((DeviceConfig.TimeRequest.Response) receivedPacket).deviceTime; + int systemTime = (int) (System.currentTimeMillis() / 1000); + int diff = Math.abs(systemTime - deviceTime); + LOG.debug("systemTime: {} deviceTime: {} diff: {}",systemTime, deviceTime, diff); + if (this.checkTime && diff > 0) { + SetTimeRequest setTimeReq = new SetTimeRequest(supportProvider, false); + setTimeReq.nextRequest(this.nextRequest); + nextRequest(setTimeReq); + } } }