Search in sources :

Example 1 with ActivitySample

use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySample in project Gadgetbridge by Freeyourgadget.

the class AbstractChartFragment method refresh.

protected DefaultChartsData<CombinedData> refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
    //        Calendar cal = GregorianCalendar.getInstance();
    //        cal.clear();
    TimestampTranslation tsTranslation = new TimestampTranslation();
    //        Date date;
    //        String dateStringFrom = "";
    //        String dateStringTo = "";
    //        ArrayList<String> xLabels = null;
    LOG.info("" + getTitle() + ": number of samples:" + samples.size());
    CombinedData combinedData;
    if (samples.size() > 1) {
        boolean annotate = true;
        boolean use_steps_as_movement;
        int last_type = ActivityKind.TYPE_UNKNOWN;
        int numEntries = samples.size();
        List<BarEntry> activityEntries = new ArrayList<>(numEntries);
        boolean hr = supportsHeartrate(gbDevice);
        List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
        // this is kinda inefficient...
        List<Integer> colors = new ArrayList<>(numEntries);
        int lastHrSampleIndex = -1;
        for (int i = 0; i < numEntries; i++) {
            ActivitySample sample = samples.get(i);
            int type = sample.getKind();
            int ts = tsTranslation.shorten(sample.getTimestamp());
            //                System.out.println(ts);
            //                ts = i;
            // determine start and end dates
            //                if (i == 0) {
            //                    cal.setTimeInMillis(ts * 1000L); // make sure it's converted to long
            //                    date = cal.getTime();
            //                    dateStringFrom = dateFormat.format(date);
            //                } else if (i == samples.size() - 1) {
            //                    cal.setTimeInMillis(ts * 1000L); // same here
            //                    date = cal.getTime();
            //                    dateStringTo = dateFormat.format(date);
            //                }
            float movement = sample.getIntensity();
            float value = movement;
            switch(type) {
                case ActivityKind.TYPE_DEEP_SLEEP:
                    value += SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akDeepSleep.color);
                    break;
                case ActivityKind.TYPE_LIGHT_SLEEP:
                    colors.add(akLightSleep.color);
                    break;
                case ActivityKind.TYPE_NOT_WORN:
                    //a small value, just to show something on the graphs
                    value = SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akNotWorn.color);
                    break;
                default:
                    //                        short steps = sample.getSteps();
                    //                        if (use_steps_as_movement && steps != 0) {
                    //                            // I'm not sure using steps for this is actually a good idea
                    //                            movement = steps;
                    //                        }
                    //                        value = ((float) movement) / movement_divisor;
                    colors.add(akActivity.color);
            }
            activityEntries.add(createBarEntry(value, ts));
            if (hr && isValidHeartRateValue(sample.getHeartRate())) {
                if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 60 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
                    heartrateEntries.add(createLineEntry(0, lastHrSampleIndex + 1));
                    heartrateEntries.add(createLineEntry(0, ts - 1));
                }
                heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
                lastHrSampleIndex = ts;
            }
            String xLabel = "";
            if (annotate) {
            //                    cal.setTimeInMillis((ts + tsOffset) * 1000L);
            //                    date = cal.getTime();
            //                    String dateString = annotationDateFormat.format(date);
            //                    xLabel = dateString;
            //                    if (last_type != type) {
            //                        if (isSleep(last_type) && !isSleep(type)) {
            //                            // woken up
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextColor(Color.WHITE);
            //                            line.setTextSize(15);
            //                            chart.getXAxis().addLimitLine(line);
            //                        } else if (!isSleep(last_type) && isSleep(type)) {
            //                            // fallen asleep
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextSize(15);
            //                            line.setTextColor(Color.WHITE);
            //                            chart.getXAxis().addLimitLine(line);
            //                        }
            //                    }
            //                    last_type = type;
            }
        }
        BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
        // create a data object with the datasets
        //            combinedData = new CombinedData(xLabels);
        combinedData = new CombinedData();
        List<IBarDataSet> list = new ArrayList<>();
        list.add(activitySet);
        BarData barData = new BarData(list);
        barData.setBarWidth(100f);
        //            barData.setGroupSpace(0);
        combinedData.setData(barData);
        if (hr && heartrateEntries.size() > 0) {
            LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");
            LineData lineData = new LineData(heartrateSet);
            combinedData.setData(lineData);
        }
    //            chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
    //            chart.setDescriptionPosition(?, ?);
    } else {
        combinedData = new CombinedData();
    }
    IAxisValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
    return new DefaultChartsData(combinedData, xValueFormatter);
}
Also used : AbstractActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample) ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) CombinedData(com.github.mikephil.charting.data.CombinedData) BarEntry(com.github.mikephil.charting.data.BarEntry) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) LineData(com.github.mikephil.charting.data.LineData) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 2 with ActivitySample

use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySample in project Gadgetbridge by Freeyourgadget.

the class ActivityAnalysis method calculateActivityAmounts.

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(sample.getSteps());
        }
        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);
            }
        }
        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);
    }
    result.calculatePercentages();
    return result;
}
Also used : ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample) ActivityAmounts(nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts) ActivityAmount(nodomain.freeyourgadget.gadgetbridge.model.ActivityAmount)

Example 3 with ActivitySample

use of nodomain.freeyourgadget.gadgetbridge.model.ActivitySample in project Gadgetbridge by Freeyourgadget.

the class AbstractChartFragment method ensureStartAndEndSamples.

protected void ensureStartAndEndSamples(List<ActivitySample> samples, int tsStart, int tsEnd) {
    if (samples == null || samples.isEmpty()) {
        return;
    }
    ActivitySample lastSample = samples.get(samples.size() - 1);
    if (lastSample.getTimestamp() < tsEnd) {
        samples.add(createTrailingActivitySample(lastSample, tsEnd));
    }
    ActivitySample firstSample = samples.get(0);
    if (firstSample.getTimestamp() > tsStart) {
        samples.add(createTrailingActivitySample(firstSample, tsStart));
    }
}
Also used : AbstractActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample) ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample)

Aggregations

ActivitySample (nodomain.freeyourgadget.gadgetbridge.model.ActivitySample)3 AbstractActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample)2 BarData (com.github.mikephil.charting.data.BarData)1 BarDataSet (com.github.mikephil.charting.data.BarDataSet)1 BarEntry (com.github.mikephil.charting.data.BarEntry)1 CombinedData (com.github.mikephil.charting.data.CombinedData)1 Entry (com.github.mikephil.charting.data.Entry)1 LineData (com.github.mikephil.charting.data.LineData)1 LineDataSet (com.github.mikephil.charting.data.LineDataSet)1 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)1 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)1 ArrayList (java.util.ArrayList)1 ActivityAmount (nodomain.freeyourgadget.gadgetbridge.model.ActivityAmount)1 ActivityAmounts (nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts)1