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

Improve Activity List processing

This commit is contained in:
vanous 2020-10-11 09:24:57 +02:00
parent d6bed776c5
commit 0b7d37c7eb
4 changed files with 93 additions and 45 deletions

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context; import android.content.Context;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.adapter.AbstractItemAdapter; import nodomain.freeyourgadget.gadgetbridge.adapter.AbstractItemAdapter;
@ -18,20 +19,30 @@ public class ActivityListingAdapter extends AbstractItemAdapter<StepAnalysis.Ste
protected String getName(StepAnalysis.StepSession item) { protected String getName(StepAnalysis.StepSession item) {
int activityKind = item.getActivityKind(); int activityKind = item.getActivityKind();
String activityKindLabel = ActivityKind.asString(activityKind, getContext()); String activityKindLabel = ActivityKind.asString(activityKind, getContext());
Date start = item.getStepStart(); Date startTime = item.getStepStart();
Date end = item.getStepEnd(); Date endTime = item.getStepEnd();
String fromTime = DateTimeUtils.formatTime(startTime.getHours(), startTime.getMinutes());
String toTime = DateTimeUtils.formatTime(endTime.getHours(), endTime.getMinutes());
String duration = DateTimeUtils.formatDurationHoursMinutes(endTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS);
if (activityKind == ActivityKind.TYPE_UNKNOWN) { if (activityKind == ActivityKind.TYPE_UNKNOWN) {
return getContext().getString(R.string.chart_no_active_data); return getContext().getString(R.string.chart_no_active_data);
} }
return activityKindLabel + " " + DateTimeUtils.formatTime(start.getHours(), start.getMinutes()) + " - " + DateTimeUtils.formatTime(end.getHours(), end.getMinutes()); return activityKindLabel + " " + duration + " (" + fromTime + " - " + toTime + ")";
} }
@Override @Override
protected String getDetails(StepAnalysis.StepSession item) { protected String getDetails(StepAnalysis.StepSession item) {
String heartRate = "";
if (item.getActivityKind() == ActivityKind.TYPE_UNKNOWN) { if (item.getActivityKind() == ActivityKind.TYPE_UNKNOWN) {
return getContext().getString(R.string.chart_get_active_and_synchronize); return getContext().getString(R.string.chart_get_active_and_synchronize);
} }
return getContext().getString(R.string.steps) +": " + item.getSteps(); if (item.getHeartRateAverage() > 50) {
heartRate = " ❤️ " + item.getHeartRateAverage();
}
return "👣 " + item.getSteps() + heartRate;
} }
@Override @Override

View File

@ -127,9 +127,7 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
tsFrom = (int) (day.getTimeInMillis() / 1000); tsFrom = (int) (day.getTimeInMillis() / 1000);
tsTo = tsFrom + 24 * 60 * 60 - 1; tsTo = tsFrom + 24 * 60 * 60 - 1;
tsDataFrom = tsFrom; tsDataFrom = tsFrom;
return getAllSamples(db, device, tsFrom, tsTo); return getAllSamples(db, device, tsFrom, tsTo);
} }
@ -137,7 +135,7 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
//have an "Unknown Activity" in the list in case there are no active sessions //have an "Unknown Activity" in the list in case there are no active sessions
List<StepAnalysis.StepSession> result = new ArrayList<>(); List<StepAnalysis.StepSession> result = new ArrayList<>();
int tsTo = tsDataFrom + 24 * 60 * 60 - 1; int tsTo = tsDataFrom + 24 * 60 * 60 - 1;
result.add(new StepAnalysis.StepSession(new Date(tsDataFrom * 1000L), new Date(tsTo * 1000L), 0, ActivityKind.TYPE_UNKNOWN)); result.add(new StepAnalysis.StepSession(new Date(tsDataFrom * 1000L), new Date(tsTo * 1000L), 0, 0, ActivityKind.TYPE_UNKNOWN));
return result; return result;
} }

View File

@ -29,81 +29,110 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public class StepAnalysis { public class StepAnalysis {
protected static final Logger LOG = LoggerFactory.getLogger(StepAnalysis.class); protected static final Logger LOG = LoggerFactory.getLogger(StepAnalysis.class);
private static final long MIN_SESSION_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_min_session_length", 10); private final int MIN_SESSION_STEPS = 100;
private static final long MAX_IDLE_PHASE_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_max_idle_phase_length", 5);
private static final long MIN_STEPS_PER_MINUTE = GBApplication.getPrefs().getInt("chart_list_min_steps_per_minute", 80);
private static final long MIN_STEPS_PER_MINUTE_FOR_RUN = GBApplication.getPrefs().getInt("chart_list_min_steps_per_minute_for_run", 120);
public List<StepSession> calculateStepSessions(List<? extends ActivitySample> samples) { public List<StepSession> calculateStepSessions(List<? extends ActivitySample> samples) {
List<StepSession> result = new ArrayList<>(); List<StepSession> result = new ArrayList<>();
final int MIN_SESSION_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_min_session_length", 5);
final int MAX_IDLE_PHASE_LENGTH = 60 * GBApplication.getPrefs().getInt("chart_list_max_idle_phase_length", 5);
final int MIN_STEPS_PER_MINUTE = GBApplication.getPrefs().getInt("chart_list_min_steps_per_minute", 40);
ActivitySample previousSample = null; ActivitySample previousSample = null;
Date stepStart = null; Date stepStart = null;
Date stepEnd = null; Date stepEnd = null;
int activeSteps = 0; int activeSteps = 0;
int stepsBetwenActivities = 0; int heartRateForAverage = 0;
long durationSinceLastActiveStep = 0; int heartRateToAdd = 0;
int activeSamplesForAverage = 0;
int activeSamplesToAdd = 0;
int stepsBetweenActivities = 0;
int heartRateBetweenActivities = 0;
int durationSinceLastActiveStep = 0;
int activityKind; int activityKind;
for (ActivitySample sample : samples) { for (ActivitySample sample : samples) {
if (isStep(sample)) { //TODO we could improve/extend this to other activities as well, if in database if (isStep(sample)) { //TODO we could improve/extend this to other activities as well, if in database
if (sample.getHeartRate() != 255 && sample.getHeartRate() != -1) {
heartRateToAdd = sample.getHeartRate();
activeSamplesToAdd = 1;
} else {
heartRateToAdd = 0;
activeSamplesToAdd = 0;
}
if (stepStart == null) { if (stepStart == null) {
if (sample.getSteps() > MIN_STEPS_PER_MINUTE) { //active step
stepStart = getDateFromSample(sample); stepStart = getDateFromSample(sample);
activeSteps = sample.getSteps(); activeSteps = sample.getSteps();
heartRateForAverage = heartRateToAdd;
activeSamplesForAverage = activeSamplesToAdd;
durationSinceLastActiveStep = 0; durationSinceLastActiveStep = 0;
stepsBetwenActivities = 0; stepsBetweenActivities = 0;
} heartRateBetweenActivities = 0;
previousSample = null;
} }
if (previousSample != null) { if (previousSample != null) {
long durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp(); int durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
activeSamplesForAverage += activeSamplesToAdd;
if (sample.getSteps() > MIN_STEPS_PER_MINUTE) { if (sample.getSteps() > MIN_STEPS_PER_MINUTE) {
activeSteps += sample.getSteps() + stepsBetwenActivities; activeSteps += sample.getSteps() + stepsBetweenActivities;
stepsBetwenActivities = 0; heartRateForAverage += heartRateToAdd + heartRateBetweenActivities;
stepsBetweenActivities = 0;
heartRateBetweenActivities = 0;
durationSinceLastActiveStep = 0; durationSinceLastActiveStep = 0;
} else { } else {
stepsBetwenActivities += sample.getSteps(); stepsBetweenActivities += sample.getSteps();
heartRateBetweenActivities += heartRateToAdd;
durationSinceLastActiveStep += durationSinceLastSample; durationSinceLastActiveStep += durationSinceLastSample;
} }
if (stepStart != null && durationSinceLastActiveStep >= MAX_IDLE_PHASE_LENGTH) { if (durationSinceLastActiveStep >= MAX_IDLE_PHASE_LENGTH) {
long current = getDateFromSample(sample).getTime() / 1000;
long ending = stepStart.getTime() / 1000;
long session_length = current - ending;
if (session_length > MIN_SESSION_LENGTH) { int current = sample.getTimestamp();
stepEnd = getDateFromSample(sample); int starting = (int) (stepStart.getTime() / 1000);
activityKind = detect_activity_from_steps_per_minute(session_length, activeSteps); int session_length = current - starting - durationSinceLastActiveStep;
result.add(new StepSession(stepStart, stepEnd, activeSteps, activityKind)); int heartRateAverage = activeSamplesForAverage > 0 ? heartRateForAverage / activeSamplesForAverage : 0;
stepStart = null;
if (session_length >= MIN_SESSION_LENGTH) {
stepEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L);
activityKind = detect_activity(session_length, activeSteps, heartRateAverage);
result.add(new StepSession(stepStart, stepEnd, activeSteps, heartRateAverage, activityKind));
} }
stepStart = null;
} }
} }
previousSample = sample; previousSample = sample;
} }
} }
//make sure we save the last portion of the data as well //make sure we show the last portion of the data as well in case no further activity is recorded yet
if (stepStart != null && previousSample != null) { if (stepStart != null && previousSample != null) {
long current = getDateFromSample(previousSample).getTime() / 1000; int current = previousSample.getTimestamp();
long ending = stepStart.getTime() / 1000; int starting = (int) (stepStart.getTime() / 1000);
long session_length = current - ending; int session_length = current - starting - durationSinceLastActiveStep;
int heartRateAverage = activeSamplesForAverage > 0 ? heartRateForAverage / activeSamplesForAverage : 0;
if (session_length > MIN_SESSION_LENGTH) { if (session_length > MIN_SESSION_LENGTH && activeSteps > MIN_SESSION_STEPS) {
stepEnd = getDateFromSample(previousSample); stepEnd = getDateFromSample(previousSample);
activityKind = detect_activity_from_steps_per_minute(session_length, activeSteps); activityKind = detect_activity(session_length, activeSteps, heartRateAverage);
result.add(new StepSession(stepStart, stepEnd, activeSteps, activityKind)); result.add(new StepSession(stepStart, stepEnd, activeSteps, heartRateAverage, activityKind));
} }
} }
return result; return result;
} }
private int detect_activity_from_steps_per_minute(long session_length, int activeSteps) { private int detect_activity(int session_length, int activeSteps, int heartRateAverage) {
long spm = activeSteps / (session_length / 60); final int MIN_STEPS_PER_MINUTE_FOR_RUN = GBApplication.getPrefs().getInt("chart_list_min_steps_per_minute_for_run", 120);
int spm = (int) (activeSteps / (session_length / 60));
if (spm > MIN_STEPS_PER_MINUTE_FOR_RUN) { if (spm > MIN_STEPS_PER_MINUTE_FOR_RUN) {
return ActivityKind.TYPE_RUNNING; return ActivityKind.TYPE_RUNNING;
} }
if (activeSteps > 200) {
return ActivityKind.TYPE_WALKING; return ActivityKind.TYPE_WALKING;
} }
if (heartRateAverage > 90) {
return ActivityKind.TYPE_EXERCISE;
}
return ActivityKind.TYPE_ACTIVITY;
}
private boolean isStep(ActivitySample sample) { private boolean isStep(ActivitySample sample) {
return sample.getKind() == ActivityKind.TYPE_WALKING || sample.getKind() == ActivityKind.TYPE_RUNNING || sample.getKind() == ActivityKind.TYPE_ACTIVITY; return sample.getKind() == ActivityKind.TYPE_WALKING || sample.getKind() == ActivityKind.TYPE_RUNNING || sample.getKind() == ActivityKind.TYPE_ACTIVITY;
@ -117,14 +146,16 @@ public class StepAnalysis {
private final Date stepStart; private final Date stepStart;
private final Date stepEnd; private final Date stepEnd;
private final int steps; private final int steps;
private final int heartRateAverage;
private final int activityKind; private final int activityKind;
StepSession(Date stepStart, StepSession(Date stepStart,
Date stepEnd, Date stepEnd,
int steps, int activityKind) { int steps, int heartRateAverage, int activityKind) {
this.stepStart = stepStart; this.stepStart = stepStart;
this.stepEnd = stepEnd; this.stepEnd = stepEnd;
this.steps = steps; this.steps = steps;
this.heartRateAverage = heartRateAverage;
this.activityKind = activityKind; this.activityKind = activityKind;
} }
@ -136,10 +167,14 @@ public class StepAnalysis {
return stepEnd; return stepEnd;
} }
public long getSteps() { public int getSteps() {
return steps; return steps;
} }
public int getHeartRateAverage() {
return heartRateAverage;
}
public int getActivityKind() { public int getActivityKind() {
return activityKind; return activityKind;
} }

View File

@ -59,24 +59,28 @@
android:title="@string/charts_activity_list"> android:title="@string/charts_activity_list">
<EditTextPreference <EditTextPreference
android:defaultValue="5"
android:inputType="number" android:inputType="number"
android:key="chart_list_min_session_length" android:key="chart_list_min_session_length"
android:maxLength="2" android:maxLength="2"
android:title="@string/activity_prefs_chart_min_session_length" /> android:title="@string/activity_prefs_chart_min_session_length" />
<EditTextPreference <EditTextPreference
android:defaultValue="5"
android:inputType="number" android:inputType="number"
android:key="chart_list_max_idle_phase_length" android:key="chart_list_max_idle_phase_length"
android:maxLength="2" android:maxLength="2"
android:title="@string/activity_prefs_chart_max_idle_phase_length" /> android:title="@string/activity_prefs_chart_max_idle_phase_length" />
<EditTextPreference <EditTextPreference
android:defaultValue="40"
android:inputType="number" android:inputType="number"
android:key="chart_list_min_steps_per_minute" android:key="chart_list_min_steps_per_minute"
android:maxLength="3" android:maxLength="3"
android:title="@string/activity_prefs_chart_min_steps_per_minute" /> android:title="@string/activity_prefs_chart_min_steps_per_minute" />
<EditTextPreference <EditTextPreference
android:defaultValue="120"
android:inputType="number" android:inputType="number"
android:key="chart_list_min_steps_per_minute_for_run" android:key="chart_list_min_steps_per_minute_for_run"
android:maxLength="3" android:maxLength="3"