From 3852fcd756d31ed0b98f4f42cb8810e4e047315f Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 19 Sep 2015 21:54:01 +0200 Subject: [PATCH] Reuse MiBandDateConverter --- .../devices/miband/MiBandDateConverter.java | 38 ++++++++++++++----- .../service/devices/miband/BatteryInfo.java | 2 +- .../operations/FetchActivityOperation.java | 13 +------ 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandDateConverter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandDateConverter.java index b356a207f..8663921b1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandDateConverter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandDateConverter.java @@ -4,24 +4,44 @@ import java.util.Calendar; import java.util.GregorianCalendar; public class MiBandDateConverter { + /** + * Creates a calendar object representing the current date and time. + */ + public static GregorianCalendar createCalendar() { + return new GregorianCalendar(); + } + /** * uses the standard algorithm to convert bytes received from the MiBand to a Calendar object * @param value * @return */ public static GregorianCalendar rawBytesToCalendar(byte[] value) { - GregorianCalendar timestamp = new GregorianCalendar(); - if (value.length == 6) { - timestamp.set(Calendar.YEAR, (2000 + value[0])); - timestamp.set(Calendar.MONTH, value[1]); - timestamp.set(Calendar.DATE, value[2]); - timestamp.set(Calendar.HOUR_OF_DAY, value[3]); - timestamp.set(Calendar.MINUTE, value[4]); - timestamp.set(Calendar.SECOND, value[5]); + return rawBytesToCalendar(value, 0); + } + return createCalendar(); + } + + /** + * uses the standard algorithm to convert bytes received from the MiBand to a Calendar object + * @param value + * @return + */ + public static GregorianCalendar rawBytesToCalendar(byte[] value, int offset) { + if (value.length - offset >= 6) { + GregorianCalendar timestamp = new GregorianCalendar( + value[offset] + 2000, + value[offset + 1], + value[offset + 2], + value[offset + 3], + value[offset + 4], + value[offset + 5]); + + return timestamp; } - return timestamp; + return createCalendar(); } /** diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/BatteryInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/BatteryInfo.java index fa7d6e488..1bb6a33cf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/BatteryInfo.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/BatteryInfo.java @@ -43,7 +43,7 @@ public class BatteryInfo extends AbstractInfo { } public GregorianCalendar getLastChargeTime() { - GregorianCalendar lastCharge = new GregorianCalendar(); + GregorianCalendar lastCharge = MiBandDateConverter.createCalendar(); if (mData.length >= 10) { lastCharge = MiBandDateConverter.rawBytesToCalendar(new byte[]{ diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java index 90ac55c24..1047274e7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java @@ -107,7 +107,7 @@ public class FetchActivityOperation extends AbstractBTLEOperation // byte 0 is the data type: 1 means that each minute is represented by a triplet of bytes int dataType = value[0]; // byte 1 to 6 represent a timestamp - GregorianCalendar timestamp = parseTimestamp(value, 1); + GregorianCalendar timestamp = MiBandDateConverter.rawBytesToCalendar(value, 1); // counter of all data held by the band int totalDataToRead = (value[7] & 0xff) | ((value[8] & 0xff) << 8); @@ -273,15 +273,4 @@ public class FetchActivityOperation extends AbstractBTLEOperation LOG.error("Unable to send ack to MI", ex); } } - - private GregorianCalendar parseTimestamp(byte[] value, int offset) { - GregorianCalendar timestamp = new GregorianCalendar( - value[offset] + 2000, - value[offset + 1], - value[offset + 2], - value[offset + 3], - value[offset + 4], - value[offset + 5]); - return timestamp; - } }