use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySession in project Gadgetbridge by Freeyourgadget.
the class ActivityListingChartFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_steps_list, container, false);
getChartsHost().enableSwipeRefresh(false);
ListView stepsList = rootView.findViewById(R.id.itemListView);
stepListAdapter = new ActivityListingAdapter(getContext());
stepsList.setAdapter(stepListAdapter);
stepsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ActivitySession item = stepListAdapter.getItem(i);
if (item.getSessionType() != ActivitySession.SESSION_SUMMARY) {
int tsFrom = (int) (item.getStartTime().getTime() / 1000);
int tsTo = (int) (item.getEndTime().getTime() / 1000);
showDetail(tsFrom, tsTo, item, getChartsHost().getDevice());
}
}
});
stepsDateView = rootView.findViewById(R.id.stepsDateView);
FloatingActionButton fab;
fab = rootView.findViewById(R.id.fab);
fab.setVisibility(View.VISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDashboard(tsDateTo, getChartsHost().getDevice());
}
});
refresh();
return rootView;
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySession in project Gadgetbridge by Freeyourgadget.
the class StepAnalysis method calculateSummary.
public ActivitySession calculateSummary(List<ActivitySession> sessions, boolean empty) {
Date startTime = null;
Date endTime = null;
int stepsSum = 0;
int heartRateAverage = 0;
List<Integer> heartRateSum = new ArrayList<>();
int distanceSum = 0;
float intensitySum = 0;
int sessionCount;
long durationSum = 0;
for (ActivitySession session : sessions) {
startTime = session.getStartTime();
endTime = session.getEndTime();
durationSum += endTime.getTime() - startTime.getTime();
stepsSum += session.getActiveSteps();
distanceSum += session.getDistance();
heartRateSum.add(session.getHeartRateAverage());
intensitySum += session.getIntensity();
}
sessionCount = sessions.toArray().length;
if (heartRateSum.toArray().length > 0) {
heartRateAverage = calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length;
}
startTime = new Date(0);
endTime = new Date(durationSum);
ActivitySession stepSessionSummary = new ActivitySession(startTime, endTime, stepsSum, heartRateAverage, intensitySum, distanceSum, 0);
stepSessionSummary.setSessionCount(sessionCount);
stepSessionSummary.setSessionType(ActivitySession.SESSION_SUMMARY);
stepSessionSummary.setEmptySummary(empty);
stepSessionSummary.setTotalDaySteps(totalDailySteps);
return stepSessionSummary;
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySession in project Gadgetbridge by Freeyourgadget.
the class ActivityListingChartFragment method refreshInBackground.
@Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
List<? extends ActivitySample> activitySamples;
activitySamples = getSamples(db, device);
List<ActivitySession> stepSessions = null;
ActivitySession ongoingSession = null;
StepAnalysis stepAnalysis = new StepAnalysis();
boolean isEmptySummary = false;
if (activitySamples != null) {
stepSessions = stepAnalysis.calculateStepSessions(activitySamples);
if (stepSessions.toArray().length == 0) {
isEmptySummary = true;
}
ActivitySession stepSessionsSummary = stepAnalysis.calculateSummary(stepSessions, isEmptySummary);
stepSessions.add(0, stepSessionsSummary);
ActivitySession emptySession = new ActivitySession();
emptySession.setSessionType(ActivitySession.SESSION_EMPTY);
// this is to have an empty item at the end to be able to use FAB without it blocking anything
stepSessions.add(emptySession);
ongoingSession = stepAnalysis.getOngoingSessions(stepSessions);
}
return new MyChartsData(stepSessions, ongoingSession);
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySession in project Gadgetbridge by Freeyourgadget.
the class ActivityListingDetail method onViewCreated.
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int tsFrom = getArguments().getInt("tsFrom");
int tsTo = getArguments().getInt("tsTo");
ActivitySession item = (ActivitySession) getArguments().getSerializable("item");
GBDevice gbDevice;
gbDevice = getArguments().getParcelable(GBDevice.EXTRA_DEVICE);
if (gbDevice == null) {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
final ActivitySummariesChartFragment activitySummariesChartFragment = new ActivitySummariesChartFragment();
getChildFragmentManager().beginTransaction().replace(R.id.chartsFragmentHolder, activitySummariesChartFragment).commit();
activitySummariesChartFragment.setDateAndGetData(gbDevice, tsFrom, tsTo);
ActivityListingAdapter stepListAdapter = new ActivityListingAdapter(getContext());
View activityItem = view.findViewById(R.id.activityItemHolder);
stepListAdapter.fill_item(item, 0, activityItem, null);
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySession in project Gadgetbridge by Freeyourgadget.
the class StepAnalysis method calculateStepSessions.
public List<ActivitySession> calculateStepSessions(List<? extends ActivitySample> samples) {
LOG.debug("get all samples activitysessions: " + samples.toArray().length);
List<ActivitySession> result = new ArrayList<>();
ActivityUser activityUser = new ActivityUser();
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);
int stepLengthCm = activityUser.getStepLengthCm();
final double STEP_LENGTH_M = stepLengthCm * 0.01;
final double MIN_SESSION_INTENSITY = Math.max(0, Math.min(1, MIN_STEPS_PER_MINUTE * 0.01));
totalDailySteps = 0;
ActivitySample previousSample = null;
Date sessionStart = null;
Date sessionEnd;
// steps that we count
int activeSteps = 0;
// steps during time when we maybe take a rest but then restart
int stepsBetweenActivePeriods = 0;
int durationSinceLastActiveStep = 0;
int activityKind;
List<Integer> heartRateSum = new ArrayList<>();
List<Integer> heartRateBetweenActivePeriodsSum = new ArrayList<>();
float activeIntensity = 0;
float intensityBetweenActivePeriods = 0;
HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
for (ActivitySample sample : samples) {
int steps = sample.getSteps();
if (steps > 0) {
totalDailySteps += steps;
}
if (// anything but sleep counts
sample.getKind() != ActivityKind.TYPE_SLEEP && !(sample instanceof TrailingActivitySample)) {
if (sessionStart == null) {
sessionStart = getDateFromSample(sample);
activeSteps = sample.getSteps();
activeIntensity = sample.getIntensity();
heartRateSum = new ArrayList<>();
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
heartRateSum.add(sample.getHeartRate());
}
durationSinceLastActiveStep = 0;
stepsBetweenActivePeriods = 0;
heartRateBetweenActivePeriodsSum = new ArrayList<>();
previousSample = null;
}
if (previousSample != null) {
int durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
if (// either some steps
sample.getSteps() > MIN_STEPS_PER_MINUTE || (sample.getIntensity() > MIN_SESSION_INTENSITY && sample.getSteps() > 0)) {
// or some intensity plus at least one step
activeSteps += sample.getSteps() + stepsBetweenActivePeriods;
activeIntensity += sample.getIntensity() + intensityBetweenActivePeriods;
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
heartRateSum.add(sample.getHeartRate());
}
heartRateSum.addAll(heartRateBetweenActivePeriodsSum);
heartRateBetweenActivePeriodsSum = new ArrayList<>();
stepsBetweenActivePeriods = 0;
intensityBetweenActivePeriods = 0;
durationSinceLastActiveStep = 0;
} else {
// short break data to remember, we will add it to the rest later, if break not too long
stepsBetweenActivePeriods += sample.getSteps();
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
heartRateBetweenActivePeriodsSum.add(sample.getHeartRate());
}
durationSinceLastActiveStep += durationSinceLastSample;
intensityBetweenActivePeriods += sample.getIntensity();
}
if (durationSinceLastActiveStep >= MAX_IDLE_PHASE_LENGTH) {
// break too long, we split here
int current = sample.getTimestamp();
int starting = (int) (sessionStart.getTime() / 1000);
int session_length = current - starting - durationSinceLastActiveStep;
if (session_length >= MIN_SESSION_LENGTH) {
// valid activity session
int heartRateAverage = heartRateSum.toArray().length > 0 ? calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length : 0;
float distance = (float) (activeSteps * STEP_LENGTH_M);
sessionEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L);
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
ActivitySession activitySession = new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind);
// activitySession.setSessionType(ActivitySession.SESSION_ONGOING);
result.add(activitySession);
}
sessionStart = null;
}
}
previousSample = sample;
}
}
if (sessionStart != null && previousSample != null) {
int current = previousSample.getTimestamp();
int starting = (int) (sessionStart.getTime() / 1000);
int session_length = current - starting - durationSinceLastActiveStep;
if (session_length >= MIN_SESSION_LENGTH) {
int heartRateAverage = heartRateSum.toArray().length > 0 ? calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length : 0;
float distance = (float) (activeSteps * STEP_LENGTH_M);
sessionEnd = getDateFromSample(previousSample);
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
ActivitySession ongoingActivity = new ActivitySession(sessionStart, sessionEnd, activeSteps, heartRateAverage, activeIntensity, distance, activityKind);
ongoingActivity.setSessionType(ActivitySession.SESSION_ONGOING);
result.add(ongoingActivity);
}
}
return result;
}
Aggregations