From 4bfb7d46215cb92032ae54b7383819fdb9dbd8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Fri, 6 Sep 2024 22:32:32 +0100 Subject: [PATCH] Huami: Use activity summary timezone for details fetching (#3592) It looks like some watches will reply with a fetch timestamp in a different timezone than the request. When this happens, when we fetch the activity details, the watch will reply with the details from the wrong activity. Use the same timezone in the activity details request as the watch replies in the activity summary. --- .../operations/fetch/AbstractFetchOperation.java | 4 +++- .../fetch/FetchSportsDetailsOperation.java | 12 ++++++++---- .../fetch/FetchSportsSummaryOperation.java | 11 +++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/AbstractFetchOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/AbstractFetchOperation.java index 9a68eedab..1014d3b3c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/AbstractFetchOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/AbstractFetchOperation.java @@ -196,7 +196,9 @@ public abstract class AbstractFetchOperation extends AbstractHuamiOperation { buffer.write(value, 1, value.length - 1); // skip the counter } - protected void startFetching(final TransactionBuilder builder, final byte fetchType, final GregorianCalendar sinceWhen) { + protected void startFetching(final TransactionBuilder builder, final byte fetchType, final Calendar sinceWhen) { + LOG.debug("Requesting {} since {}", getName(), sinceWhen.getTime()); + final HuamiSupport support = getSupport(); byte[] fetchBytes = BLETypeConversions.join(new byte[]{ HuamiService.COMMAND_ACTIVITY_DATA_START_DATE, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsDetailsOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsDetailsOperation.java index f4663ffde..052d69fdd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsDetailsOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsDetailsOperation.java @@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Calendar; import java.util.GregorianCalendar; import androidx.annotation.NonNull; @@ -55,17 +56,20 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { private static final Logger LOG = LoggerFactory.getLogger(FetchSportsDetailsOperation.class); private final AbstractHuamiActivityDetailsParser detailsParser; private final BaseActivitySummary summary; + private final Calendar summarySyncTime; private final String lastSyncTimeKey; FetchSportsDetailsOperation(@NonNull BaseActivitySummary summary, @NonNull AbstractHuamiActivityDetailsParser detailsParser, @NonNull HuamiSupport support, + @NonNull Calendar summarySyncTime, @NonNull String lastSyncTimeKey, int fetchCount) { super(support); setName("fetching sport details"); this.summary = summary; this.detailsParser = detailsParser; + this.summarySyncTime = summarySyncTime; this.lastSyncTimeKey = lastSyncTimeKey; this.fetchCount = fetchCount; } @@ -77,9 +81,9 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { @Override protected void startFetching(TransactionBuilder builder) { - LOG.info("start " + getName()); - final GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); - startFetching(builder, HuamiFetchDataType.SPORTS_DETAILS.getCode(), sinceWhen); + LOG.info("start {}", getName()); + // Use the actual last time, including tz - #3592 + startFetching(builder, HuamiFetchDataType.SPORTS_DETAILS.getCode(), summarySyncTime); } @Override @@ -191,7 +195,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { @Override protected GregorianCalendar getLastSuccessfulSyncTime() { final GregorianCalendar calendar = BLETypeConversions.createCalendar(); - calendar.setTime(summary.getStartTime()); + calendar.setTime(summarySyncTime.getTime()); return calendar; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsSummaryOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsSummaryOperation.java index 4617930fb..a4a077c7a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsSummaryOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/operations/fetch/FetchSportsSummaryOperation.java @@ -60,7 +60,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { @Override protected void startFetching(TransactionBuilder builder) { - LOG.info("start" + getName()); + LOG.info("start {}", getName()); final GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); startFetching(builder, HuamiFetchDataType.SPORTS_SUMMARIES.getCode(), sinceWhen); } @@ -106,7 +106,14 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { } final AbstractHuamiActivityDetailsParser detailsParser = ((HuamiActivitySummaryParser) summaryParser).getDetailsParser(summary); - final FetchSportsDetailsOperation nextOperation = new FetchSportsDetailsOperation(summary, detailsParser, getSupport(), getLastSyncTimeKey(), fetchCount); + final FetchSportsDetailsOperation nextOperation = new FetchSportsDetailsOperation( + summary, + detailsParser, + getSupport(), + getLastStartTimestamp(), // we need the actual start timestamp, including tz - #3592 + getLastSyncTimeKey(), + fetchCount + ); getSupport().getFetchOperationQueue().add(0, nextOperation); return true;