use of nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts in project Gadgetbridge by Freeyourgadget.
the class AbstractWeekChartFragment method getActivityAmountsForDay.
private ActivityAmounts getActivityAmountsForDay(DBHandler db, Calendar day, GBDevice device) {
LimitedQueue activityAmountCache = null;
ActivityAmounts amounts = null;
Activity activity = getActivity();
int key = (int) (day.getTimeInMillis() / 1000) + (mOffsetHours * 3600);
if (activity != null) {
activityAmountCache = ((ChartsActivity) activity).mActivityAmountCache;
amounts = (ActivityAmounts) (activityAmountCache.lookup(key));
}
if (amounts == null) {
ActivityAnalysis analysis = new ActivityAnalysis();
amounts = analysis.calculateActivityAmounts(getSamplesOfDay(db, day, mOffsetHours, device));
if (activityAmountCache != null) {
activityAmountCache.add(key, amounts);
}
}
return amounts;
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts in project Gadgetbridge by Freeyourgadget.
the class ActivityAnalysis method calculateActivityAmounts.
public ActivityAmounts calculateActivityAmounts(List<? extends ActivitySample> samples) {
ActivityAmount deepSleep = new ActivityAmount(ActivityKind.TYPE_DEEP_SLEEP);
ActivityAmount lightSleep = new ActivityAmount(ActivityKind.TYPE_LIGHT_SLEEP);
ActivityAmount notWorn = new ActivityAmount(ActivityKind.TYPE_NOT_WORN);
ActivityAmount activity = new ActivityAmount(ActivityKind.TYPE_ACTIVITY);
ActivityAmount previousAmount = null;
ActivitySample previousSample = null;
for (ActivitySample sample : samples) {
ActivityAmount amount;
switch(sample.getKind()) {
case ActivityKind.TYPE_DEEP_SLEEP:
amount = deepSleep;
break;
case ActivityKind.TYPE_LIGHT_SLEEP:
amount = lightSleep;
break;
case ActivityKind.TYPE_NOT_WORN:
amount = notWorn;
break;
case ActivityKind.TYPE_ACTIVITY:
default:
amount = activity;
break;
}
int steps = sample.getSteps();
if (steps > 0) {
amount.addSteps(steps);
}
if (previousSample != null) {
long timeDifference = sample.getTimestamp() - previousSample.getTimestamp();
if (previousSample.getRawKind() == sample.getRawKind()) {
amount.addSeconds(timeDifference);
} else {
long sharedTimeDifference = (long) (timeDifference / 2.0f);
previousAmount.addSeconds(sharedTimeDifference);
amount.addSeconds(sharedTimeDifference);
}
// add time
if (steps > 0 && sample.getKind() == ActivityKind.TYPE_ACTIVITY) {
if (steps > maxSpeed) {
maxSpeed = steps;
}
if (!stats.containsKey(steps)) {
// LOG.debug("Adding: " + steps);
stats.put(steps, timeDifference);
} else {
long time = stats.get(steps);
// LOG.debug("Updating: " + steps + " " + timeDifference + time);
stats.put(steps, timeDifference + time);
}
}
}
amount.setStartDate(sample.getTimestamp());
amount.setEndDate(sample.getTimestamp());
previousAmount = amount;
previousSample = sample;
}
ActivityAmounts result = new ActivityAmounts();
if (deepSleep.getTotalSeconds() > 0) {
result.addAmount(deepSleep);
}
if (lightSleep.getTotalSeconds() > 0) {
result.addAmount(lightSleep);
}
if (activity.getTotalSeconds() > 0) {
result.addAmount(activity);
}
if (notWorn.getTotalSeconds() > 0) {
result.addAmount(notWorn);
}
result.calculatePercentages();
return result;
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts in project Gadgetbridge by Freeyourgadget.
the class AbstractWeekChartFragment method refreshWeekBeforeData.
private WeekChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
// do not modify the caller's argument
day = (Calendar) day.clone();
day.add(Calendar.DATE, -TOTAL_DAYS);
List<BarEntry> entries = new ArrayList<>();
ArrayList<String> labels = new ArrayList<String>();
long balance = 0;
long daily_balance = 0;
TOTAL_DAYS_FOR_AVERAGE = 0;
for (int counter = 0; counter < TOTAL_DAYS; counter++) {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
daily_balance = calculateBalance(amounts);
if (daily_balance > 0) {
TOTAL_DAYS_FOR_AVERAGE++;
}
balance += daily_balance;
entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
labels.add(getWeeksChartsLabel(day));
day.add(Calendar.DATE, 1);
}
BarDataSet set = new BarDataSet(entries, "");
set.setColors(getColors());
set.setValueFormatter(getBarValueFormatter());
BarData barData = new BarData(set);
// prevent tearing other graph elements with the black text. Another approach would be to hide the values cmpletely with data.setDrawValues(false);
barData.setValueTextColor(Color.GRAY);
barData.setValueTextSize(10f);
LimitLine target = new LimitLine(mTargetValue);
barChart.getAxisLeft().removeAllLimitLines();
barChart.getAxisLeft().addLimitLine(target);
float average = 0;
if (TOTAL_DAYS_FOR_AVERAGE > 0) {
average = Math.abs(balance / TOTAL_DAYS_FOR_AVERAGE);
}
LimitLine average_line = new LimitLine(average);
average_line.setLabel(getString(R.string.average, getAverage(average)));
if (average > (mTargetValue)) {
average_line.setLineColor(Color.GREEN);
average_line.setTextColor(Color.GREEN);
} else {
average_line.setLineColor(Color.RED);
average_line.setTextColor(Color.RED);
}
if (average > 0) {
if (GBApplication.getPrefs().getBoolean("charts_show_average", true)) {
barChart.getAxisLeft().addLimitLine(average_line);
}
}
return new WeekChartsData(barData, new PreformattedXIndexLabelFormatter(labels), getBalanceMessage(balance, mTargetValue));
}
use of nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts in project Gadgetbridge by Freeyourgadget.
the class AbstractWeekChartFragment method refreshDayPie.
private DayData refreshDayPie(DBHandler db, Calendar day, GBDevice device) {
PieData data = new PieData();
List<PieEntry> entries = new ArrayList<>();
PieDataSet set = new PieDataSet(entries, "");
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
float[] totalValues = getTotalsForActivityAmounts(amounts);
String[] pieLabels = getPieLabels();
float totalValue = 0;
for (int i = 0; i < totalValues.length; i++) {
float value = totalValues[i];
totalValue += value;
entries.add(new PieEntry(value, pieLabels[i]));
}
set.setColors(getColors());
if (totalValues.length < 2) {
if (totalValue < mTargetValue) {
entries.add(new PieEntry((mTargetValue - totalValue)));
set.addColor(Color.GRAY);
}
}
data.setDataSet(set);
if (totalValues.length < 2) {
data.setDrawValues(false);
} else {
set.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
set.setValueTextColor(DESCRIPTION_COLOR);
set.setValueTextSize(13f);
set.setValueFormatter(getPieValueFormatter());
}
return new DayData(data, formatPieValue((long) totalValue));
}
Aggregations