1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-28 19:45:50 +01:00

Initial implementation of Activity list.

This commit is contained in:
vanous 2020-10-08 23:37:23 +02:00
parent 824784fd43
commit bd90b59f92
8 changed files with 408 additions and 12 deletions

View File

@ -0,0 +1,44 @@
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context;
import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.adapter.AbstractItemAdapter;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class ActivityListingAdapter extends AbstractItemAdapter<StepAnalysis.StepSession> {
public ActivityListingAdapter(Context context) {
super(context);
}
@Override
protected String getName(StepAnalysis.StepSession item) {
int activityKind = item.getActivityKind();
String activityKindLabel = ActivityKind.asString(activityKind, getContext());
Date start = item.getStepStart();
Date end = item.getStepEnd();
if (activityKind == ActivityKind.TYPE_UNKNOWN) {
return getContext().getString(R.string.chart_no_active_data);
}
return activityKindLabel + " " + DateTimeUtils.formatTime(start.getHours(), start.getMinutes()) + " - " + DateTimeUtils.formatTime(end.getHours(), end.getMinutes());
}
@Override
protected String getDetails(StepAnalysis.StepSession item) {
if (item.getActivityKind() == ActivityKind.TYPE_UNKNOWN) {
return getContext().getString(R.string.chart_get_active_and_synchronize);
}
return getContext().getString(R.string.steps) +": " + item.getSteps();
}
@Override
protected int getIcon(StepAnalysis.StepSession item) {
int activityKind = item.getActivityKind();
return ActivityKind.getIconId(activityKind);
}
}

View File

@ -0,0 +1,145 @@
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
Gobbetti, Dikay900, Pavel Elagin
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.github.mikephil.charting.charts.Chart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class ActivityListingChartFragment extends AbstractChartFragment {
protected static final Logger LOG = LoggerFactory.getLogger(ActivityListingChartFragment.class);
int tsDataFrom;
private View rootView;
private List<? extends ActivitySample> activitySamples;
private ActivityListingAdapter stepListAdapter;
private TextView stepsDateView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_steps_list, container, false);
ListView stepsList = rootView.findViewById(R.id.itemListView);
stepListAdapter = new ActivityListingAdapter(getContext());
stepsList.setAdapter(stepListAdapter);
stepsDateView = rootView.findViewById(R.id.stepsDateView);
//refresh();
return rootView;
}
@Override
public String getTitle() {
return "Steps list";
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ChartsHost.REFRESH)) {
// TODO: use LimitLines to visualize smart alarms?
//refresh();
} else {
super.onReceive(context, intent);
}
}
@Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
//trying to fit found peg into square hole of the Gb Charts fragment system
//get the data
activitySamples = getSamples(db, device);
return null;
}
@Override
protected void updateChartsnUIThread(ChartsData chartsData) {
//top displays selected date
stepsDateView.setText(DateTimeUtils.formatDate(new Date(tsDataFrom * 1000L)));
//calculate active sessions
StepAnalysis stepAnalysis = new StepAnalysis();
if (activitySamples != null) {
List<StepAnalysis.StepSession> stepSessions = stepAnalysis.calculateStepSessions(activitySamples);
if (stepSessions.toArray().length == 0) {
stepSessions = create_empty_record();
}
//push to the adapter
stepListAdapter.setItems(stepSessions, true);
}
}
@Override
protected void renderCharts() {
}
@Override
protected void setupLegend(Chart chart) {
}
@Override
protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
Calendar day = Calendar.getInstance();
day.setTimeInMillis(tsTo * 1000L); //we need today initially, which is the end of the time range
day.set(Calendar.HOUR_OF_DAY, 0); //and we set time for the start and end of the same day
day.set(Calendar.MINUTE, 0);
day.set(Calendar.SECOND, 0);
tsFrom = (int) (day.getTimeInMillis() / 1000);
tsTo = tsFrom + 24 * 60 * 60 - 1;
tsDataFrom = tsFrom;
return getAllSamples(db, device, tsFrom, tsTo);
}
private List<StepAnalysis.StepSession> create_empty_record() {
//have an "Unknown Activity" in the list in case there are no active sessions
List<StepAnalysis.StepSession> result = new ArrayList<>();
int tsTo = tsDataFrom + 24 * 60 * 60 - 1;
result.add(new StepAnalysis.StepSession(new Date(tsDataFrom * 1000L), new Date(tsTo * 1000L), 0, ActivityKind.TYPE_UNKNOWN));
return result;
}
}

View File

@ -355,14 +355,16 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
case 0:
return new ActivitySleepChartFragment();
case 1:
return new SleepChartFragment();
return new ActivityListingChartFragment();
case 2:
return new WeekSleepChartFragment();
return new SleepChartFragment();
case 3:
return new WeekStepsChartFragment();
return new WeekSleepChartFragment();
case 4:
return new SpeedZonesFragment();
return new WeekStepsChartFragment();
case 5:
return new SpeedZonesFragment();
case 6:
return new LiveActivityFragment();
}
return null;
@ -373,9 +375,9 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
// Show 5 or 6 total pages.
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(mGBDevice);
if (coordinator.supportsRealtimeData()) {
return 6;
return 7;
}
return 5;
return 6;
}
private String getSleepTitle() {
@ -402,14 +404,16 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
case 0:
return getString(R.string.activity_sleepchart_activity_and_sleep);
case 1:
return getString(R.string.sleepchart_your_sleep);
return getString(R.string.charts_activity_list);
case 2:
return getSleepTitle();
return getString(R.string.sleepchart_your_sleep);
case 3:
return getStepsTitle();
return getSleepTitle();
case 4:
return getString(R.string.stats_title);
return getStepsTitle();
case 5:
return getString(R.string.stats_title);
case 6:
return getString(R.string.liveactivity_live_activity);
}
return super.getPageTitle(position);

View File

@ -0,0 +1,148 @@
/* Copyright (C) 2019-2020 Q-er
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public class StepAnalysis {
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 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) {
List<StepSession> result = new ArrayList<>();
ActivitySample previousSample = null;
Date stepStart = null;
Date stepEnd = null;
int activeSteps = 0;
int stepsBetwenActivities = 0;
long durationSinceLastActiveStep = 0;
int activityKind;
for (ActivitySample sample : samples) {
if (isStep(sample)) { //TODO we could improve/extend this to other activities as well, if in database
if (stepStart == null) {
if (sample.getSteps() > MIN_STEPS_PER_MINUTE) { //active step
stepStart = getDateFromSample(sample);
activeSteps = sample.getSteps();
durationSinceLastActiveStep = 0;
stepsBetwenActivities = 0;
}
}
if (previousSample != null) {
long durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
if (sample.getSteps() > MIN_STEPS_PER_MINUTE) {
activeSteps += sample.getSteps() + stepsBetwenActivities;
stepsBetwenActivities = 0;
durationSinceLastActiveStep = 0;
} else {
stepsBetwenActivities += sample.getSteps();
durationSinceLastActiveStep += durationSinceLastSample;
}
if (stepStart != null && 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) {
stepEnd = getDateFromSample(sample);
activityKind = detect_activity_from_steps_per_minute(session_length, activeSteps);
result.add(new StepSession(stepStart, stepEnd, activeSteps, activityKind));
stepStart = null;
}
}
}
previousSample = sample;
}
}
//make sure we save the last portion of the data as well
if (stepStart != null && previousSample != null) {
long current = getDateFromSample(previousSample).getTime() / 1000;
long ending = stepStart.getTime() / 1000;
long session_length = current - ending;
if (session_length > MIN_SESSION_LENGTH) {
stepEnd = getDateFromSample(previousSample);
activityKind = detect_activity_from_steps_per_minute(session_length, activeSteps);
result.add(new StepSession(stepStart, stepEnd, activeSteps, activityKind));
}
}
return result;
}
private int detect_activity_from_steps_per_minute(long session_length, int activeSteps) {
long spm = activeSteps / (session_length / 60);
if (spm > MIN_STEPS_PER_MINUTE_FOR_RUN) {
return ActivityKind.TYPE_RUNNING;
}
return ActivityKind.TYPE_WALKING;
}
private boolean isStep(ActivitySample sample) {
return sample.getKind() == ActivityKind.TYPE_WALKING || sample.getKind() == ActivityKind.TYPE_RUNNING || sample.getKind() == ActivityKind.TYPE_ACTIVITY;
}
private Date getDateFromSample(ActivitySample sample) {
return new Date(sample.getTimestamp() * 1000L);
}
public static class StepSession {
private final Date stepStart;
private final Date stepEnd;
private final int steps;
private final int activityKind;
StepSession(Date stepStart,
Date stepEnd,
int steps, int activityKind) {
this.stepStart = stepStart;
this.stepEnd = stepEnd;
this.steps = steps;
this.activityKind = activityKind;
}
public Date getStepStart() {
return stepStart;
}
public Date getStepEnd() {
return stepEnd;
}
public long getSteps() {
return steps;
}
public int getActivityKind() {
return activityKind;
}
}
}

View File

@ -0,0 +1,21 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity$PlaceholderFragment">
<TextView
android:id="@+id/stepsDateView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold" />
<ListView
android:id="@+id/itemListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -418,6 +418,8 @@
<string name="user_feedback_miband_set_alarms_failed">There was an error setting the alarms, please try again.</string>
<string name="user_feedback_miband_set_alarms_ok">Alarms sent to device.</string>
<string name="chart_no_data_synchronize">No data. Synchronize device?</string>
<string name="chart_no_active_data">No activities detected.</string>
<string name="chart_get_active_and_synchronize">Do some activity and synchronize device.</string>
<string name="user_feedback_miband_activity_data_transfer">About to transfer %1$s of data starting from %2$s</string>
<string name="miband_prefs_fitness_goal">Daily step target</string>
<string name="dbaccess_error_executing">Error executing \'%1$s\'</string>
@ -439,6 +441,7 @@
<string name="weeksleepchart_today_sleep_description">Sleep today, target: %1$s</string>
<string name="weekstepschart_steps_a_week">Steps per week</string>
<string name="activity_sleepchart_activity_and_sleep">Activity</string>
<string name="charts_activity_list">Activity list</string>
<string name="lack_of_sleep">Lack of sleep: %1$s</string>
<string name="overslept">Overslept: %1$s</string>
<string name="updating_firmware">Flashing firmware…</string>
@ -596,6 +599,10 @@
<string name="pref_title_chart_sleep_rolling_24_hour">Sleep range</string>
<string name="pref_chart_sleep_rolling_24_on">Past 24 hours</string>
<string name="pref_chart_sleep_rolling_24_off">Noon to noon</string>
<string name="activity_prefs_chart_min_steps_per_minute_for_run">Minimal steps per minute to detect run</string>
<string name="activity_prefs_chart_min_steps_per_minute">Minimal steps per minute to detect activity</string>
<string name="activity_prefs_chart_max_idle_phase_length">Pause length to separate activities (minutes)</string>
<string name="activity_prefs_chart_min_session_length">Minimal activity length (minutes)</string>
<string name="authenticating">Authenticating</string>
<string name="authentication_required">Authentication required</string>
<string name="activity_prefs_sleep_duration">Preferred sleep duration in hours</string>

View File

@ -53,7 +53,34 @@
android:summaryOff="@string/pref_charts_range_off"
android:summaryOn="@string/pref_charts_range_on"
android:title="@string/pref_title_charts_range" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_charts_activity_list"
android:title="@string/charts_activity_list">
<EditTextPreference
android:inputType="number"
android:key="chart_list_min_session_length"
android:maxLength="2"
android:title="@string/activity_prefs_chart_min_session_length" />
<EditTextPreference
android:inputType="number"
android:key="chart_list_max_idle_phase_length"
android:maxLength="2"
android:title="@string/activity_prefs_chart_max_idle_phase_length" />
<EditTextPreference
android:inputType="number"
android:key="chart_list_min_steps_per_minute"
android:maxLength="3"
android:title="@string/activity_prefs_chart_min_steps_per_minute" />
<EditTextPreference
android:inputType="number"
android:key="chart_list_min_steps_per_minute_for_run"
android:maxLength="3"
android:title="@string/activity_prefs_chart_min_steps_per_minute_for_run" />
</PreferenceCategory>
</PreferenceScreen>