2015-06-11 23:34:16 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.os.Bundle;
|
2015-07-11 00:03:41 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2015-06-11 23:34:16 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-07-11 00:03:41 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
import com.github.mikephil.charting.animation.Easing;
|
|
|
|
import com.github.mikephil.charting.charts.BarLineChartBase;
|
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
|
|
|
import com.github.mikephil.charting.data.BarData;
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet;
|
|
|
|
import com.github.mikephil.charting.data.BarEntry;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-07-11 00:03:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils;
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
public class ActivitySleepChartFragment extends AbstractChartFragment {
|
|
|
|
protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
private BarLineChartBase mChart;
|
|
|
|
|
|
|
|
private int mSmartAlarmFrom = -1;
|
|
|
|
private int mSmartAlarmTo = -1;
|
|
|
|
private int mTimestampFrom = -1;
|
|
|
|
private int mSmartAlarmGoneOff = -1;
|
|
|
|
private GBDevice mGBDevice = null;
|
|
|
|
|
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2015-07-11 00:03:41 +02:00
|
|
|
if (action.equals(ACTION_REFRESH)) {
|
2015-06-11 23:34:16 +02:00
|
|
|
// TODO: use LimitLines to visualize smart alarms?
|
|
|
|
mSmartAlarmFrom = intent.getIntExtra("smartalarm_from", -1);
|
|
|
|
mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1);
|
|
|
|
mTimestampFrom = intent.getIntExtra("recording_base_timestamp", -1);
|
|
|
|
mSmartAlarmGoneOff = intent.getIntExtra("alarm_gone_off", -1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
2015-07-11 00:03:41 +02:00
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
Bundle extras = getActivity().getIntent().getExtras();
|
2015-06-11 23:34:16 +02:00
|
|
|
if (extras != null) {
|
2015-07-11 00:03:41 +02:00
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
2015-06-11 23:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
|
|
|
filter.addAction(ACTION_REFRESH);
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
mChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
setupChart();
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
private void setupChart() {
|
|
|
|
mChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mChart.setDescriptionColor(DESCRIPTION_COLOR);
|
|
|
|
configureBarLineChartDefaults(mChart);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
XAxis x = mChart.getXAxis();
|
|
|
|
x.setDrawLabels(true);
|
|
|
|
x.setDrawGridLines(false);
|
|
|
|
x.setEnabled(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
x.setTextColor(CHART_TEXT_COLOR);
|
2015-06-18 23:41:54 +02:00
|
|
|
x.setDrawLimitLinesBehindData(true);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
YAxis y = mChart.getAxisLeft();
|
|
|
|
y.setDrawGridLines(false);
|
2015-06-16 21:04:44 +02:00
|
|
|
// y.setDrawLabels(false);
|
2015-06-17 00:05:55 +02:00
|
|
|
// TODO: make fixed max value optional
|
|
|
|
y.setAxisMaxValue(1f);
|
2015-06-11 23:34:16 +02:00
|
|
|
y.setDrawTopYLabelEntry(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
y.setTextColor(CHART_TEXT_COLOR);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
// y.setLabelCount(5);
|
|
|
|
y.setEnabled(true);
|
|
|
|
|
|
|
|
YAxis yAxisRight = mChart.getAxisRight();
|
|
|
|
yAxisRight.setDrawGridLines(false);
|
|
|
|
yAxisRight.setEnabled(false);
|
|
|
|
yAxisRight.setDrawLabels(false);
|
|
|
|
yAxisRight.setDrawTopYLabelEntry(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
yAxisRight.setTextColor(CHART_TEXT_COLOR);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2015-06-11 23:34:16 +02:00
|
|
|
// mChart.getLegend().setEnabled(false);
|
|
|
|
//
|
|
|
|
// mChart.animateXY(2000, 2000);
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
// don't forget to refresh the drawing
|
2015-06-11 23:34:16 +02:00
|
|
|
mChart.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-07-11 00:03:41 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
2015-06-11 23:34:16 +02:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refresh() {
|
|
|
|
if (mGBDevice == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayList<GBActivitySample> samples = getTestSamples(mGBDevice, -1, -1);
|
|
|
|
ArrayList<GBActivitySample> samples = getSamples(mGBDevice, -1, -1);
|
|
|
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.clear();
|
|
|
|
Date date;
|
|
|
|
String dateStringFrom = "";
|
|
|
|
String dateStringTo = "";
|
|
|
|
|
|
|
|
LOG.info("number of samples:" + samples.size());
|
|
|
|
if (samples.size() > 1) {
|
|
|
|
float movement_divisor;
|
2015-06-21 19:43:45 +02:00
|
|
|
boolean annotate = true;
|
2015-06-11 23:34:16 +02:00
|
|
|
boolean use_steps_as_movement;
|
|
|
|
switch (getProvider(mGBDevice)) {
|
|
|
|
case GBActivitySample.PROVIDER_MIBAND:
|
|
|
|
movement_divisor = 256.0f;
|
|
|
|
use_steps_as_movement = true;
|
|
|
|
break;
|
|
|
|
default: // Morpheuz
|
|
|
|
movement_divisor = 5000.0f;
|
|
|
|
use_steps_as_movement = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte last_type = GBActivitySample.TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
|
|
|
|
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm");
|
|
|
|
|
|
|
|
int numEntries = samples.size();
|
|
|
|
List<String> xLabels = new ArrayList<>(numEntries);
|
2015-06-17 00:05:55 +02:00
|
|
|
// List<BarEntry> deepSleepEntries = new ArrayList<>(numEntries / 4);
|
|
|
|
// List<BarEntry> lightSleepEntries = new ArrayList<>(numEntries / 4);
|
2015-06-11 23:34:16 +02:00
|
|
|
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
|
2015-06-17 00:05:55 +02:00
|
|
|
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < numEntries; i++) {
|
|
|
|
GBActivitySample sample = samples.get(i);
|
|
|
|
byte type = sample.getType();
|
|
|
|
|
|
|
|
// determine start and end dates
|
|
|
|
if (i == 0) {
|
|
|
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L); // make sure it's converted to long
|
|
|
|
date = cal.getTime();
|
|
|
|
dateStringFrom = dateFormat.format(date);
|
|
|
|
} else if (i == samples.size() - 1) {
|
|
|
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L); // same here
|
|
|
|
date = cal.getTime();
|
|
|
|
dateStringTo = dateFormat.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
short movement = sample.getIntensity();
|
|
|
|
|
|
|
|
float value;
|
|
|
|
if (type == GBActivitySample.TYPE_DEEP_SLEEP) {
|
2015-06-19 00:20:38 +02:00
|
|
|
// value = Y_VALUE_DEEP_SLEEP;
|
|
|
|
value = ((float) movement) / movement_divisor;
|
2015-07-11 00:03:41 +02:00
|
|
|
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
|
|
|
|
activityEntries.add(createBarEntry(value, i));
|
2015-06-17 00:05:55 +02:00
|
|
|
colors.add(akDeepSleep.color);
|
2015-06-11 23:34:16 +02:00
|
|
|
} else {
|
|
|
|
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
|
2015-06-19 00:20:38 +02:00
|
|
|
value = ((float) movement) / movement_divisor;
|
2015-07-11 00:03:41 +02:00
|
|
|
// value += SleepUtils.Y_VALUE_LIGHT_SLEEP;
|
2015-06-19 00:20:38 +02:00
|
|
|
// value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP);
|
2015-07-11 00:03:41 +02:00
|
|
|
activityEntries.add(createBarEntry(value, i));
|
2015-06-17 00:05:55 +02:00
|
|
|
colors.add(akLightSleep.color);
|
2015-06-11 23:34:16 +02:00
|
|
|
} else {
|
|
|
|
byte 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;
|
|
|
|
}
|
2015-06-19 00:20:38 +02:00
|
|
|
value = ((float) movement) / movement_divisor;
|
2015-07-11 00:03:41 +02:00
|
|
|
activityEntries.add(createBarEntry(value, i));
|
2015-06-17 00:05:55 +02:00
|
|
|
colors.add(akActivity.color);
|
2015-06-11 23:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String xLabel = "";
|
|
|
|
if (annotate) {
|
2015-06-18 23:41:54 +02:00
|
|
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L);
|
|
|
|
date = cal.getTime();
|
|
|
|
String dateString = annotationDateFormat.format(date);
|
|
|
|
xLabel = dateString;
|
2015-06-21 19:43:45 +02:00
|
|
|
// 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);
|
|
|
|
// mChart.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);
|
|
|
|
// mChart.getXAxis().addLimitLine(line);
|
|
|
|
// }
|
|
|
|
// }
|
2015-06-11 23:34:16 +02:00
|
|
|
last_type = type;
|
|
|
|
}
|
|
|
|
xLabels.add(xLabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
mChart.getXAxis().setValues(xLabels);
|
|
|
|
|
2015-06-17 00:05:55 +02:00
|
|
|
// BarDataSet deepSleepSet = createDeepSleepSet(deepSleepEntries, "Deep Sleep");
|
|
|
|
// BarDataSet lightSleepSet = createLightSleepSet(lightSleepEntries, "Light Sleep");
|
|
|
|
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
ArrayList<BarDataSet> dataSets = new ArrayList<>();
|
2015-06-17 00:05:55 +02:00
|
|
|
// dataSets.add(deepSleepSet);
|
|
|
|
// dataSets.add(lightSleepSet);
|
2015-06-11 23:34:16 +02:00
|
|
|
dataSets.add(activitySet);
|
|
|
|
|
|
|
|
// create a data object with the datasets
|
|
|
|
BarData data = new BarData(xLabels, dataSets);
|
2015-06-17 00:05:55 +02:00
|
|
|
data.setGroupSpace(0);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
mChart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
|
|
|
|
// mChart.setDescriptionPosition(?, ?);
|
|
|
|
// set data
|
2015-06-17 00:05:55 +02:00
|
|
|
|
|
|
|
setupLegend(mChart);
|
|
|
|
|
2015-06-11 23:34:16 +02:00
|
|
|
mChart.setData(data);
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
mChart.animateX(500, Easing.EasingOption.EaseInOutQuart);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
// textView.setText(dateStringFrom + " to " + dateStringTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 00:05:55 +02:00
|
|
|
private void setupLegend(BarLineChartBase chart) {
|
|
|
|
List<Integer> legendColors = new ArrayList<>(3);
|
|
|
|
List<String> legendLabels = new ArrayList<>(3);
|
|
|
|
legendColors.add(akActivity.color);
|
|
|
|
legendLabels.add(akActivity.label);
|
|
|
|
legendColors.add(akLightSleep.color);
|
|
|
|
legendLabels.add(akLightSleep.label);
|
|
|
|
legendColors.add(akDeepSleep.color);
|
|
|
|
legendLabels.add(akDeepSleep.label);
|
|
|
|
chart.getLegend().setColors(legendColors);
|
|
|
|
chart.getLegend().setLabels(legendLabels);
|
|
|
|
}
|
2015-06-11 23:34:16 +02:00
|
|
|
}
|