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.
here
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;
}
public void updateCurrentSteps(int newSteps, int timestamp) {
public void updateCurrentSteps(int stepsDelta, int timestamp) {
try {
if (steps == 0) {
steps = newSteps;
steps += stepsDelta;
lastTimestamp = timestamp;
if (newSteps > 0) {
initialSteps = newSteps;
}
// if (stepsDelta > 0) {
// initialSteps = stepsDelta;
// }
return;
}
if (newSteps >= steps) {
int stepsDelta = newSteps - steps;
int timeDelta = timestamp - lastTimestamp;
currentStepsPerMinute = calculateStepsPerMinute(stepsDelta, timeDelta);
if (currentStepsPerMinute > maxStepsPerMinute) {
maxStepsPerMinute = currentStepsPerMinute;
maxStepsResetCounter = 0;
}
steps = newSteps;
lastTimestamp = timestamp;
} else {
// TODO: handle new day?
int timeDelta = timestamp - lastTimestamp;
currentStepsPerMinute = calculateStepsPerMinute(stepsDelta, timeDelta);
if (currentStepsPerMinute > maxStepsPerMinute) {
maxStepsPerMinute = currentStepsPerMinute;
maxStepsResetCounter = 0;
}
steps += stepsDelta;
lastTimestamp = timestamp;
} catch (Exception 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);
MiBandActivitySample sample = createActivitySample(device, user, ts, provider);
sample.setHeartRate(getHeartrateBpm());
sample.setSteps(getSteps());
sample.setRawIntensity(ActivitySample.NOT_MEASURED);
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)
.putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
LOG.debug("Storing realtime sample: " + sample);
provider.addGBActivitySample(sample);
} catch (Exception 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 heartrateBpm;
private int lastSteps;
// subclasses may add more
private Timer realtimeStorageTimer;
@ -56,12 +57,22 @@ public abstract class RealtimeSamplesSupport {
return realtimeStorageTimer != null;
}
public void setSteps(int stepsPerMinute) {
public synchronized void setSteps(int stepsPerMinute) {
this.steps = stepsPerMinute;
}
public int getSteps() {
return steps;
public synchronized int getSteps() {
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) {
@ -77,7 +88,10 @@ public abstract class RealtimeSamplesSupport {
resetCurrentValues();
}
protected void resetCurrentValues() {
protected synchronized void resetCurrentValues() {
if (steps >= lastSteps) {
lastSteps = steps;
}
steps = ActivitySample.NOT_MEASURED;
heartrateBpm = ActivitySample.NOT_MEASURED;
}