1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Mi Band: fix live activity messing up stepcount #448

live samples now report relative steps, not absolute to the current
day's stepcount.

Also live samples' steps should NOT be added to the database since
they are already counted in the regular stepcount.
This commit is contained in:
cpfeiffer 2016-12-26 01:38:20 +01:00
parent 305bd7600c
commit 8719cadc43
3 changed files with 39 additions and 25 deletions

View File

@ -97,32 +97,26 @@ public class LiveActivityFragment extends AbstractChartFragment {
return maxStepsPerMinute; return maxStepsPerMinute;
} }
public void updateCurrentSteps(int newSteps, int timestamp) { public void updateCurrentSteps(int stepsDelta, int timestamp) {
try { try {
if (steps == 0) { if (steps == 0) {
steps = newSteps; steps += stepsDelta;
lastTimestamp = timestamp; lastTimestamp = timestamp;
if (newSteps > 0) { // if (stepsDelta > 0) {
initialSteps = newSteps; // initialSteps = stepsDelta;
} // }
return; return;
} }
if (newSteps >= steps) { int timeDelta = timestamp - lastTimestamp;
int stepsDelta = newSteps - steps; currentStepsPerMinute = calculateStepsPerMinute(stepsDelta, timeDelta);
int timeDelta = timestamp - lastTimestamp; if (currentStepsPerMinute > maxStepsPerMinute) {
currentStepsPerMinute = calculateStepsPerMinute(stepsDelta, timeDelta); maxStepsPerMinute = currentStepsPerMinute;
if (currentStepsPerMinute > maxStepsPerMinute) { maxStepsResetCounter = 0;
maxStepsPerMinute = currentStepsPerMinute;
maxStepsResetCounter = 0;
}
steps = newSteps;
lastTimestamp = timestamp;
} else {
// TODO: handle new day?
} }
steps += stepsDelta;
lastTimestamp = timestamp;
} catch (Exception ex) { } catch (Exception ex) {
GB.toast(LiveActivityFragment.this.getContext(), ex.getMessage(), Toast.LENGTH_SHORT, GB.ERROR, ex); GB.toast(LiveActivityFragment.this.getContext(), ex.getMessage(), Toast.LENGTH_SHORT, GB.ERROR, ex);
} }

View File

@ -950,16 +950,22 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
MiBandSampleProvider provider = new MiBandSampleProvider(gbDevice, session); MiBandSampleProvider provider = new MiBandSampleProvider(gbDevice, session);
MiBandActivitySample sample = createActivitySample(device, user, ts, provider); MiBandActivitySample sample = createActivitySample(device, user, ts, provider);
sample.setHeartRate(getHeartrateBpm()); sample.setHeartRate(getHeartrateBpm());
sample.setSteps(getSteps());
sample.setRawIntensity(ActivitySample.NOT_MEASURED); sample.setRawIntensity(ActivitySample.NOT_MEASURED);
sample.setRawKind(MiBandSampleProvider.TYPE_ACTIVITY); // to make it visible in the charts TODO: add a MANUAL kind for that? sample.setRawKind(MiBandSampleProvider.TYPE_ACTIVITY); // to make it visible in the charts TODO: add a MANUAL kind for that?
LOG.debug("Storing realtime sample: " + sample);
provider.addGBActivitySample(sample);
// set the steps only afterwards, since realtime steps are also recorded
// in the regular samples and we must not count them twice
// Note: we know that the DAO sample is never committed again, so we simply
// change the value here in memory.
sample.setSteps(getSteps());
Intent intent = new Intent(DeviceService.ACTION_REALTIME_SAMPLES) Intent intent = new Intent(DeviceService.ACTION_REALTIME_SAMPLES)
.putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample); .putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
LOG.debug("Storing realtime sample: " + sample);
provider.addGBActivitySample(sample);
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Unable to acquire db for saving realtime samples", e); LOG.warn("Unable to acquire db for saving realtime samples", e);
} }

View File

@ -22,6 +22,7 @@ public abstract class RealtimeSamplesSupport {
protected int steps; protected int steps;
protected int heartrateBpm; protected int heartrateBpm;
private int lastSteps;
// subclasses may add more // subclasses may add more
private Timer realtimeStorageTimer; private Timer realtimeStorageTimer;
@ -56,12 +57,22 @@ public abstract class RealtimeSamplesSupport {
return realtimeStorageTimer != null; return realtimeStorageTimer != null;
} }
public void setSteps(int stepsPerMinute) { public synchronized void setSteps(int stepsPerMinute) {
this.steps = stepsPerMinute; this.steps = stepsPerMinute;
} }
public int getSteps() { public synchronized int getSteps() {
return steps; if (lastSteps == 0) {
return 0; // wait until we have a delta between two samples
}
if (steps == ActivitySample.NOT_MEASURED) {
return 0;
}
int delta = steps - lastSteps;
if (delta < 0) {
return 0;
}
return delta;
} }
public void setHeartrateBpm(int hrBpm) { public void setHeartrateBpm(int hrBpm) {
@ -77,7 +88,10 @@ public abstract class RealtimeSamplesSupport {
resetCurrentValues(); resetCurrentValues();
} }
protected void resetCurrentValues() { protected synchronized void resetCurrentValues() {
if (steps >= lastSteps) {
lastSteps = steps;
}
steps = ActivitySample.NOT_MEASURED; steps = ActivitySample.NOT_MEASURED;
heartrateBpm = ActivitySample.NOT_MEASURED; heartrateBpm = ActivitySample.NOT_MEASURED;
} }