From 2ef9128cf82c5b72457b79e501c21e8b3777e5e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hler?= Date: Fri, 20 Nov 2020 18:33:53 +0100 Subject: [PATCH] Move BCD functions to its own Util class --- .../watchxplus/WatchXPlusDeviceSupport.java | 43 +++++++------------ .../devices/watch9/Watch9DeviceSupport.java | 43 +++++++------------ .../gadgetbridge/util/BcdUtil.java | 17 ++++++++ 3 files changed, 49 insertions(+), 54 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BcdUtil.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/lenovo/watchxplus/WatchXPlusDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/lenovo/watchxplus/WatchXPlusDeviceSupport.java index bdfc6b7b6..98be5a1dd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/lenovo/watchxplus/WatchXPlusDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/lenovo/watchxplus/WatchXPlusDeviceSupport.java @@ -86,6 +86,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA import nodomain.freeyourgadget.gadgetbridge.service.devices.lenovo.operations.InitOperation; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; +import nodomain.freeyourgadget.gadgetbridge.util.BcdUtil; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -455,14 +456,14 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport { private void handleTime(byte[] time) { GregorianCalendar now = BLETypeConversions.createCalendar(); GregorianCalendar nowDevice = BLETypeConversions.createCalendar(); - int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + Conversion.fromBcd8(time[8]); + int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + BcdUtil.fromBcd8(time[8]); nowDevice.set(year, - Conversion.fromBcd8(time[9]) - 1, - Conversion.fromBcd8(time[10]), - Conversion.fromBcd8(time[11]), - Conversion.fromBcd8(time[12]), - Conversion.fromBcd8(time[13])); - nowDevice.set(Calendar.DAY_OF_WEEK, Conversion.fromBcd8(time[16]) + 1); + BcdUtil.fromBcd8(time[9]) - 1, + BcdUtil.fromBcd8(time[10]), + BcdUtil.fromBcd8(time[11]), + BcdUtil.fromBcd8(time[12]), + BcdUtil.fromBcd8(time[13])); + nowDevice.set(Calendar.DAY_OF_WEEK, BcdUtil.fromBcd8(time[16]) + 1); long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000; @@ -494,12 +495,12 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport { TransactionBuilder builder = performInitialized("setTime"); int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f); - byte[] time = new byte[]{Conversion.toBcd8(calendar.get(Calendar.YEAR) % 100), - Conversion.toBcd8(calendar.get(Calendar.MONTH) + 1), - Conversion.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), - Conversion.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), - Conversion.toBcd8(calendar.get(Calendar.MINUTE)), - Conversion.toBcd8(calendar.get(Calendar.SECOND)), + byte[] time = new byte[]{BcdUtil.toBcd8(calendar.get(Calendar.YEAR) % 100), + BcdUtil.toBcd8(calendar.get(Calendar.MONTH) + 1), + BcdUtil.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), + BcdUtil.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), + BcdUtil.toBcd8(calendar.get(Calendar.MINUTE)), + BcdUtil.toBcd8(calendar.get(Calendar.SECOND)), (byte) (timezoneOffsetMinutes / 60), (byte) timezoneOffsetIndustrialMinutes, (byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1) @@ -595,8 +596,8 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport { repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00); if (0 < index && index < 4) { byte[] alarmValue = new byte[]{(byte) index, - Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), - Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), + BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), + BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), repetitionMask, (byte) (alarm.getEnabled() ? 0x01 : 0x00), 0x00 // TODO: Unknown @@ -2179,18 +2180,6 @@ public class WatchXPlusDeviceSupport extends AbstractBTLEDeviceSupport { private static class Conversion { - static byte toBcd8(@IntRange(from = 0, to = 99) int value) { - int high = (value / 10) << 4; - int low = value % 10; - return (byte) (high | low); - } - - static int fromBcd8(byte value) { - int high = ((value & 0xF0) >> 4) * 10; - int low = value & 0x0F; - return high + low; - } - static byte[] toByteArr16(int value) { return new byte[]{(byte) (value >> 8), (byte) value}; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java index 8bbd0bbba..874c49646 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/watch9/Watch9DeviceSupport.java @@ -60,6 +60,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA import nodomain.freeyourgadget.gadgetbridge.service.devices.watch9.operations.InitOperation; import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; +import nodomain.freeyourgadget.gadgetbridge.util.BcdUtil; public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { @@ -238,14 +239,14 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { private void handleTime(byte[] time) { GregorianCalendar now = BLETypeConversions.createCalendar(); GregorianCalendar nowDevice = BLETypeConversions.createCalendar(); - int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + Conversion.fromBcd8(time[8]); + int year = (nowDevice.get(Calendar.YEAR) / 100) * 100 + BcdUtil.fromBcd8(time[8]); nowDevice.set(year, - Conversion.fromBcd8(time[9]) - 1, - Conversion.fromBcd8(time[10]), - Conversion.fromBcd8(time[11]), - Conversion.fromBcd8(time[12]), - Conversion.fromBcd8(time[13])); - nowDevice.set(Calendar.DAY_OF_WEEK, Conversion.fromBcd8(time[16]) + 1); + BcdUtil.fromBcd8(time[9]) - 1, + BcdUtil.fromBcd8(time[10]), + BcdUtil.fromBcd8(time[11]), + BcdUtil.fromBcd8(time[12]), + BcdUtil.fromBcd8(time[13])); + nowDevice.set(Calendar.DAY_OF_WEEK, BcdUtil.fromBcd8(time[16]) + 1); long timeDiff = (Math.abs(now.getTimeInMillis() - nowDevice.getTimeInMillis())) / 1000; if (10 < timeDiff && timeDiff < 120) { @@ -260,12 +261,12 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { TransactionBuilder builder = performInitialized("setTime"); int timezoneOffsetMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); int timezoneOffsetIndustrialMinutes = Math.round((Math.abs(timezoneOffsetMinutes) % 60) * 100f / 60f); - byte[] time = new byte[]{Conversion.toBcd8(calendar.get(Calendar.YEAR) % 100), - Conversion.toBcd8(calendar.get(Calendar.MONTH) + 1), - Conversion.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), - Conversion.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), - Conversion.toBcd8(calendar.get(Calendar.MINUTE)), - Conversion.toBcd8(calendar.get(Calendar.SECOND)), + byte[] time = new byte[]{BcdUtil.toBcd8(calendar.get(Calendar.YEAR) % 100), + BcdUtil.toBcd8(calendar.get(Calendar.MONTH) + 1), + BcdUtil.toBcd8(calendar.get(Calendar.DAY_OF_MONTH)), + BcdUtil.toBcd8(calendar.get(Calendar.HOUR_OF_DAY)), + BcdUtil.toBcd8(calendar.get(Calendar.MINUTE)), + BcdUtil.toBcd8(calendar.get(Calendar.SECOND)), (byte) (timezoneOffsetMinutes / 60), (byte) timezoneOffsetIndustrialMinutes, (byte) (calendar.get(Calendar.DAY_OF_WEEK) - 1) @@ -358,8 +359,8 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { repetitionMask |= (alarm.getRepetition(Alarm.ALARM_SUN) ? 0x01 : 0x00); if (0 < index && index < 4) { byte[] alarmValue = new byte[]{(byte) index, - Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), - Conversion.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), + BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.HOUR_OF_DAY)), + BcdUtil.toBcd8(AlarmUtils.toCalendar(alarm).get(Calendar.MINUTE)), repetitionMask, (byte) (alarm.getEnabled() ? 0x01 : 0x00), 0x00 // TODO: Unknown @@ -602,18 +603,6 @@ public class Watch9DeviceSupport extends AbstractBTLEDeviceSupport { } private static class Conversion { - static byte toBcd8(@IntRange(from = 0, to = 99) int value) { - int high = (value / 10) << 4; - int low = value % 10; - return (byte) (high | low); - } - - static int fromBcd8(byte value) { - int high = ((value & 0xF0) >> 4) * 10; - int low = value & 0x0F; - return high + low; - } - static byte[] toByteArr16(int value) { return new byte[]{(byte) (value >> 8), (byte) value}; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BcdUtil.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BcdUtil.java new file mode 100644 index 000000000..f3537b704 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BcdUtil.java @@ -0,0 +1,17 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +import androidx.annotation.IntRange; + +public class BcdUtil { + public static byte toBcd8(@IntRange(from = 0, to = 99) int value) { + int high = (value / 10) << 4; + int low = value % 10; + return (byte) (high | low); + } + + public static int fromBcd8(byte value) { + int high = ((value & 0xF0) >> 4) * 10; + int low = value & 0x0F; + return high + low; + } +}