1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +01:00

Adds ongoing activity snackbar + preference

This commit is contained in:
vanous 2021-02-25 22:48:04 +01:00
parent b8f42b6bee
commit 5758c9e564
8 changed files with 99 additions and 11 deletions

View File

@ -287,6 +287,22 @@ public class ActivityListingAdapter extends AbstractActivityListingAdapter<Activ
return DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS); return DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS);
} }
@Override
protected String getSpeedLabel(ActivitySession item) {
long duration = item.getEndTime().getTime() - item.getStartTime().getTime();
double distanceMeters = item.getDistance();
double speed = distanceMeters * 1000 / duration;
String unit = "###.#km/h";
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
unit = "###.#mi/h";
speed = speed * 0.6213712;
}
DecimalFormat df = new DecimalFormat(unit);
return df.format(speed);
}
@Override @Override
protected String getSessionCountLabel(ActivitySession item) { protected String getSessionCountLabel(ActivitySession item) {
return String.valueOf(item.getSessionCount()); return String.valueOf(item.getSessionCount());

View File

@ -19,30 +19,29 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.Chart; import com.github.mikephil.charting.charts.Chart;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
@ -66,8 +65,7 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
stepsDateView = rootView.findViewById(R.id.stepsDateView); stepsDateView = rootView.findViewById(R.id.stepsDateView);
FloatingActionButton fab; FloatingActionButton fab;
fab = rootView.findViewById(R.id.fab); fab = rootView.findViewById(R.id.fab);
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.GONE);
refresh(); refresh();
return rootView; return rootView;
@ -96,6 +94,7 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
List<? extends ActivitySample> activitySamples; List<? extends ActivitySample> activitySamples;
activitySamples = getSamples(db, device); activitySamples = getSamples(db, device);
List<ActivitySession> stepSessions = null; List<ActivitySession> stepSessions = null;
ActivitySession ongoingSession = null;
StepAnalysis stepAnalysis = new StepAnalysis(); StepAnalysis stepAnalysis = new StepAnalysis();
boolean isEmptySummary = false; boolean isEmptySummary = false;
@ -105,8 +104,9 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
isEmptySummary = true; isEmptySummary = true;
} }
stepSessions = stepAnalysis.calculateSummary(stepSessions, isEmptySummary); stepSessions = stepAnalysis.calculateSummary(stepSessions, isEmptySummary);
ongoingSession = stepAnalysis.getOngoingSessions(stepSessions);
} }
return new MyChartsData(stepSessions); return new MyChartsData(stepSessions, ongoingSession);
} }
@Override @Override
@ -119,12 +119,17 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
} }
stepsDateView.setText(DateTimeUtils.formatDate(new Date(tsDateFrom * 1000L))); stepsDateView.setText(DateTimeUtils.formatDate(new Date(tsDateFrom * 1000L)));
if (GBApplication.getPrefs().getBoolean("charts_show_ongoing_activity", true)) {
if (mcd.getOngoingSession() != null) {
showOngoingActivitySnackbar(mcd.getOngoingSession());
}
}
stepListAdapter.setItems(mcd.getStepSessions(), true); stepListAdapter.setItems(mcd.getStepSessions(), true);
} }
@Override @Override
protected void renderCharts() { protected void renderCharts() {
} }
@Override @Override
protected void setupLegend(Chart chart) { protected void setupLegend(Chart chart) {
@ -143,16 +148,52 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
return getAllSamples(db, device, tsFrom, tsTo); return getAllSamples(db, device, tsFrom, tsTo);
} }
private void showOngoingActivitySnackbar(ActivitySession ongoingSession) {
String distanceLabel = stepListAdapter.getDistanceLabel(ongoingSession);
String stepLabel = stepListAdapter.getStepLabel(ongoingSession);
String durationLabel = stepListAdapter.getDurationLabel(ongoingSession);
String hrLabel = stepListAdapter.getHrLabel(ongoingSession);
String activityName = stepListAdapter.getActivityName(ongoingSession);
int icon = stepListAdapter.getIcon(ongoingSession);
String text = String.format("%s:\u00A0%s, %s:\u00A0%s, %s:\u00A0%s, %s:\u00A0%s", activityName, durationLabel, getString(R.string.heart_rate), hrLabel, getString(R.string.steps), stepLabel, getString(R.string.distance), distanceLabel);
final Snackbar snackbar = Snackbar.make(rootView, text, 1000 * 8);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(getContext().getResources().getColor(R.color.accent));
snackbar.setActionTextColor(Color.WHITE);
snackbar.setAction(getString(R.string.dialog_hide).toUpperCase(), new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
}
);
snackbar.show();
}
private static class MyChartsData extends ChartsData { private static class MyChartsData extends ChartsData {
private final List<ActivitySession> stepSessions; private final List<ActivitySession> stepSessions;
private final ActivitySession ongoingSession;
MyChartsData(List<ActivitySession> stepSessions) { MyChartsData(List<ActivitySession> stepSessions, ActivitySession ongoingSession) {
this.stepSessions = stepSessions; this.stepSessions = stepSessions;
this.ongoingSession = ongoingSession;
} }
public List<ActivitySession> getStepSessions() { public List<ActivitySession> getStepSessions() {
return stepSessions; return stepSessions;
} }
public ActivitySession getOngoingSession() {
return this.ongoingSession;
}
} }

View File

@ -124,7 +124,9 @@ public class StepAnalysis {
float distance = (float) (activeSteps * STEP_LENGTH_M); float distance = (float) (activeSteps * STEP_LENGTH_M);
sessionEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L); sessionEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L);
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity); activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
result.add(new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind)); ActivitySession activitySession = new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind);
//activitySession.setSessionType(ActivitySession.SESSION_ONGOING);
result.add(activitySession);
} }
sessionStart = null; sessionStart = null;
} }
@ -144,7 +146,9 @@ public class StepAnalysis {
float distance = (float) (activeSteps * STEP_LENGTH_M); float distance = (float) (activeSteps * STEP_LENGTH_M);
sessionEnd = getDateFromSample(previousSample); sessionEnd = getDateFromSample(previousSample);
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity); activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
result.add(new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind)); ActivitySession ongoingActivity = new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind);
ongoingActivity.setSessionType(ActivitySession.SESSION_ONGOING);
result.add(ongoingActivity);
} }
} }
return result; return result;
@ -195,6 +199,16 @@ public class StepAnalysis {
return newList; return newList;
} }
public ActivitySession getOngoingSessions(List<ActivitySession> sessions) {
for (ActivitySession session : sessions) {
if (session.getSessionType() == ActivitySession.SESSION_ONGOING) {
return session;
}
}
return null;
}
private int calculateSumOfInts(List<Integer> samples) { private int calculateSumOfInts(List<Integer> samples) {
int result = 0; int result = 0;
for (Integer sample : samples) { for (Integer sample : samples) {

View File

@ -199,6 +199,8 @@ public abstract class AbstractActivityListingAdapter<T> extends ArrayAdapter<T>
protected abstract String getDurationLabel(T item); protected abstract String getDurationLabel(T item);
protected abstract String getSpeedLabel(T item);
protected abstract String getSessionCountLabel(T item); protected abstract String getSessionCountLabel(T item);
protected abstract boolean hasHR(T item); protected abstract boolean hasHR(T item);

View File

@ -357,6 +357,11 @@ public class ActivitySummariesAdapter extends AbstractActivityListingAdapter<Bas
return DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS); return DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS);
} }
@Override
protected String getSpeedLabel(BaseActivitySummary item) {
return null;
}
@Override @Override
protected String getSessionCountLabel(BaseActivitySummary item) { protected String getSessionCountLabel(BaseActivitySummary item) {
return ""; return "";

View File

@ -8,6 +8,7 @@ public class ActivitySession {
public static int SESSION_NORMAL = 1; public static int SESSION_NORMAL = 1;
public static int SESSION_SUMMARY = 2; public static int SESSION_SUMMARY = 2;
public static int SESSION_ONGOING = 3;
private final Date startTime; private final Date startTime;
private final Date endTime; private final Date endTime;

View File

@ -464,6 +464,8 @@
<string name="activity_list_summary_active_time">Active time</string> <string name="activity_list_summary_active_time">Active time</string>
<string name="activity_list_summary_intensity">Movement\nIntensity</string> <string name="activity_list_summary_intensity">Movement\nIntensity</string>
<string name="activity_list_summary_activities">Activities</string> <string name="activity_list_summary_activities">Activities</string>
<string name="dialog_hide">Hide</string>
<string name="show_ongoing_activity">Show ongoing activity popup</string>
<string name="lack_of_sleep">Lack of sleep: %1$s</string> <string name="lack_of_sleep">Lack of sleep: %1$s</string>
<string name="overslept">Overslept: %1$s</string> <string name="overslept">Overslept: %1$s</string>
<!-- Firmware updating --> <!-- Firmware updating -->

View File

@ -53,6 +53,13 @@
android:summaryOff="@string/pref_charts_range_off" android:summaryOff="@string/pref_charts_range_off"
android:summaryOn="@string/pref_charts_range_on" android:summaryOn="@string/pref_charts_range_on"
android:title="@string/pref_title_charts_range" /> android:title="@string/pref_title_charts_range" />
<CheckBoxPreference
android:defaultValue="true"
android:key="charts_show_ongoing_activity"
android:layout="@layout/preference_checkbox"
android:title="@string/show_ongoing_activity" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory