From 2a865fe498d7b9fa33870c0eb075eccb16a49e67 Mon Sep 17 00:00:00 2001 From: "Martin.JM" Date: Sat, 9 Nov 2024 18:27:14 +0100 Subject: [PATCH] Huawei: Fix SmartAlarm for Huawei Watch GT Linked to #4308. --- .../devices/huawei/HuaweiTLV.java | 24 +++++++++++++++++++ .../devices/huawei/packets/Alarms.java | 10 ++++---- .../devices/huawei/HuaweiWeatherManager.java | 1 + 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java index 687100fb2..4cd44bb92 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java @@ -223,10 +223,26 @@ public class HuaweiTLV { return getBytes(tag)[0]; } + public Byte getByte(int tag, Byte defaultValue) { + try { + return getByte(tag); + } catch (HuaweiPacket.MissingTagException e) { + return defaultValue; + } + } + public Boolean getBoolean(int tag) throws HuaweiPacket.MissingTagException { return getBytes(tag)[0] == 1; } + public Boolean getBoolean(int tag, Boolean defaultValue) { + try { + return getBoolean(tag); + } catch (HuaweiPacket.MissingTagException e) { + return defaultValue; + } + } + public Integer getInteger(int tag) throws HuaweiPacket.MissingTagException { return ByteBuffer.wrap(getBytes(tag)).getInt(); } @@ -243,6 +259,14 @@ public class HuaweiTLV { return ByteBuffer.wrap(getBytes(tag)).getShort(); } + public Short getShort(int tag, Short defaultValue) { + try { + return getShort(tag); + } catch (HuaweiPacket.MissingTagException e) { + return defaultValue; + } + } + public Long getLong(int tag) throws HuaweiPacket.MissingTagException { return ByteBuffer.wrap(getBytes(tag)).getLong(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/Alarms.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/Alarms.java index 788997f57..abb996fbc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/Alarms.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/Alarms.java @@ -92,11 +92,11 @@ public class Alarms { public SmartAlarm(HuaweiTLV tlv) throws ParseException { this.index = tlv.getByte(0x03); - this.status = tlv.getBoolean(0x04); - this.startHour = (byte) ((tlv.getShort(0x05) >> 8) & 0xFF); - this.startMinute = (byte) (tlv.getShort(0x05) & 0xFF); - this.repeat = tlv.getByte(0x06); - this.aheadTime = tlv.getByte(0x07); + this.status = tlv.getBoolean(0x04, false); + this.startHour = (byte) ((tlv.getShort(0x05, (short) 0) >> 8) & 0xFF); + this.startMinute = (byte) (tlv.getShort(0x05, (short) 0) & 0xFF); + this.repeat = tlv.getByte(0x06, (byte) 0); + this.aheadTime = tlv.getByte(0x07, (byte) 0); } public SmartAlarm(boolean status, byte startHour, byte startMinute, byte repeat, byte aheadTime) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWeatherManager.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWeatherManager.java index 1ce3b857c..0a40b78fc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWeatherManager.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWeatherManager.java @@ -277,6 +277,7 @@ public class HuaweiWeatherManager { if (response.getTlv().getInteger(0x7f, -1) == 0x000186AA) { // Send weather final ArrayList specs = new ArrayList<>(nodomain.freeyourgadget.gadgetbridge.model.Weather.getInstance().getWeatherSpecs()); + // TODO: could be empty, not really an issue but we need to check what to send back in that case this.sendWeather(specs.get(0)); return; }