From dfbaba4cb6902c19cc7570dd36e02edb96238485 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Tue, 22 Nov 2016 15:53:58 +0100 Subject: [PATCH] Make sure that the charts display the requested time range (instead of only the range with available samples) --- .../charts/AbstractChartFragment.java | 32 ++++++++++++++- .../charts/TrailingActivitySample.java | 39 +++++++++++++++++++ .../operations/FetchActivityOperation.java | 2 + 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TrailingActivitySample.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java index aafb0680..702243e9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java @@ -709,7 +709,10 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { } protected List getSamples(DBHandler db, GBDevice device) { - List samples = getSamples(db, device, getTSStart(), getTSEnd()); + int tsStart = getTSStart(); + int tsEnd = getTSEnd(); + List samples = (List) getSamples(db, device, tsStart, tsEnd); + ensureStartAndEndSamples(samples, tsStart, tsEnd); // List samples2 = new ArrayList<>(); // int min = Math.min(samples.size(), 10); // int min = Math.min(samples.size(), 10); @@ -720,6 +723,33 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { return samples; } + protected void ensureStartAndEndSamples(List samples, int tsStart, int tsEnd) { + if (samples == null || samples.isEmpty()) { + return; + } + ActivitySample lastSample = samples.get(samples.size() - 1); + if (lastSample.getTimestamp() < tsEnd) { + samples.add(createTrailingActivitySample(lastSample, tsEnd)); + } + + ActivitySample firstSample = samples.get(0); + if (firstSample.getTimestamp() > tsStart) { + samples.add(createTrailingActivitySample(firstSample, tsStart)); + } + } + + private ActivitySample createTrailingActivitySample(ActivitySample referenceSample, int timestamp) { + TrailingActivitySample sample = new TrailingActivitySample(); + if (referenceSample instanceof AbstractActivitySample) { + AbstractActivitySample reference = (AbstractActivitySample) referenceSample; + sample.setUserId(reference.getUserId()); + sample.setDeviceId(reference.getDeviceId()); + sample.setProvider(reference.getProvider()); + } + sample.setTimestamp(timestamp); + return sample; + } + private int getTSEnd() { return toTimestamp(getEndDate()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TrailingActivitySample.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TrailingActivitySample.java new file mode 100644 index 00000000..4c2fc77a --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TrailingActivitySample.java @@ -0,0 +1,39 @@ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; + +public class TrailingActivitySample extends AbstractActivitySample { + private int timestamp; + private long userId; + private long deviceId; + + @Override + public void setTimestamp(int timestamp) { + this.timestamp = timestamp; + } + + @Override + public void setUserId(long userId) { + this.userId = userId; + } + + @Override + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + + @Override + public long getDeviceId() { + return deviceId; + } + + @Override + public long getUserId() { + return userId; + } + + @Override + public int getTimestamp() { + return timestamp; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java index 5d8c2dba..26a46c84 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java @@ -236,12 +236,14 @@ public class FetchActivityOperation extends AbstractMiBand2Operation { DateFormat.getDateTimeInstance().format(startTimestamp.getTime())), Toast.LENGTH_LONG, GB.INFO); } else { LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value)); + handleActivityFetchFinish(); } } else if (value.length == 3) { if (Arrays.equals(MiBand2Service.RESPONSE_FINISH_SUCCESS, value)) { handleActivityFetchFinish(); } else { LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value)); + handleActivityFetchFinish(); } } }