1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 02:25:50 +01:00

Some more refactorings to support different charts

This commit is contained in:
cpfeiffer 2015-07-12 23:42:23 +02:00
parent f53a037d7f
commit 8b44f90bb6
4 changed files with 215 additions and 304 deletions

View File

@ -3,24 +3,36 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.Chart;
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.GregorianCalendar;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils;
public abstract class AbstractChartFragment extends Fragment {
private static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
public class AbstractChartFragment extends Fragment {
public static final String ACTION_REFRESH
= "nodomain.freeyourgadget.gadgetbride.chart.action.refresh";
protected static final class ActivityKind {
public final byte type;
public final String label;
@ -55,24 +67,34 @@ public class AbstractChartFragment extends Fragment {
return provider;
}
protected ArrayList<GBActivitySample> getSamples(GBDevice device, int tsFrom, int tsTo) {
protected ArrayList<GBActivitySample> getAllSamples(GBDevice device, int tsFrom, int tsTo) {
if (tsFrom == -1) {
long ts = System.currentTimeMillis();
tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours
tsFrom = getTSLast24Hours();
}
byte provider = getProvider(device);
return GBApplication.getActivityDatabaseHandler().getAllActivitySamples(tsFrom, tsTo, provider);
}
byte provider = getProvider(device);
return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider);
private int getTSLast24Hours() {
long now = System.currentTimeMillis();
return (int) ((now / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours
}
protected ArrayList<GBActivitySample> getActivitySamples(GBDevice device, int tsFrom, int tsTo) {
if (tsFrom == -1) {
tsFrom = getTSLast24Hours();
}
byte provider = getProvider(device);
return GBApplication.getActivityDatabaseHandler().getActivitySamples(tsFrom, tsTo, provider);
}
protected ArrayList<GBActivitySample> getSleepSamples(GBDevice device, int tsFrom, int tsTo) {
if (tsFrom == -1) {
long ts = System.currentTimeMillis();
tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours
tsFrom = getTSLast24Hours();
}
byte provider = getProvider(device);
return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider);
return GBApplication.getActivityDatabaseHandler().getSleepSamples(tsFrom, tsTo, provider);
}
protected ArrayList<GBActivitySample> getTestSamples(GBDevice device, int tsFrom, int tsTo) {
@ -84,7 +106,7 @@ public class AbstractChartFragment extends Fragment {
tsFrom = tsTo - (24 * 60 * 60);
byte provider = getProvider(device);
return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider);
return GBApplication.getActivityDatabaseHandler().getAllActivitySamples(tsFrom, tsTo, provider);
}
protected void configureChartDefaults(Chart<?> chart) {
@ -106,11 +128,155 @@ public class AbstractChartFragment extends Fragment {
chart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
// mChart.setPinchZoom(true);
// chart.setPinchZoom(true);
chart.setDrawGridBackground(false);
}
protected void refresh(GBDevice mGBDevice, BarLineChartBase chart) {
if (mGBDevice == null) {
return;
}
// ArrayList<GBActivitySample> samples = getTestSamples(mGBDevice, -1, -1);
ArrayList<GBActivitySample> samples = getSleepSamples(mGBDevice, -1, -1);
Calendar cal = GregorianCalendar.getInstance();
cal.clear();
Date date;
String dateStringFrom = "";
String dateStringTo = "";
LOG.info("number of samples:" + samples.size());
if (samples.size() > 1) {
float movement_divisor;
boolean annotate = true;
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);
// List<BarEntry> deepSleepEntries = new ArrayList<>(numEntries / 4);
// List<BarEntry> lightSleepEntries = new ArrayList<>(numEntries / 4);
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
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) {
// value = Y_VALUE_DEEP_SLEEP;
value = ((float) movement) / movement_divisor;
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
activityEntries.add(createBarEntry(value, i));
colors.add(akDeepSleep.color);
} else {
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
value = ((float) movement) / movement_divisor;
// value += SleepUtils.Y_VALUE_LIGHT_SLEEP;
// value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP);
activityEntries.add(createBarEntry(value, i));
colors.add(akLightSleep.color);
} 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;
}
value = ((float) movement) / movement_divisor;
activityEntries.add(createBarEntry(value, i));
colors.add(akActivity.color);
}
}
String xLabel = "";
if (annotate) {
cal.setTimeInMillis(sample.getTimestamp() * 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;
}
xLabels.add(xLabel);
}
chart.getXAxis().setValues(xLabels);
// BarDataSet deepSleepSet = createDeepSleepSet(deepSleepEntries, "Deep Sleep");
// BarDataSet lightSleepSet = createLightSleepSet(lightSleepEntries, "Light Sleep");
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
ArrayList<BarDataSet> dataSets = new ArrayList<>();
// dataSets.add(deepSleepSet);
// dataSets.add(lightSleepSet);
dataSets.add(activitySet);
// create a data object with the datasets
BarData data = new BarData(xLabels, dataSets);
data.setGroupSpace(0);
chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
// chart.setDescriptionPosition(?, ?);
// set data
setupLegend(chart);
chart.setData(data);
chart.animateX(500, Easing.EasingOption.EaseInOutQuart);
// textView.setText(dateStringFrom + " to " + dateStringTo);
}
}
protected abstract void setupLegend(BarLineChartBase chart);
protected BarEntry createBarEntry(float value, int index) {
return new BarEntry(value, index);
}

View File

@ -58,7 +58,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1);
mTimestampFrom = intent.getIntExtra("recording_base_timestamp", -1);
mSmartAlarmGoneOff = intent.getIntExtra("alarm_gone_off", -1);
refresh();
refresh(mGBDevice, mChart);
}
}
};
@ -117,7 +117,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
yAxisRight.setDrawTopYLabelEntry(false);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
refresh();
refresh(mGBDevice, mChart);
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
// mChart.getLegend().setEnabled(false);
@ -134,149 +134,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
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;
boolean annotate = true;
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);
// List<BarEntry> deepSleepEntries = new ArrayList<>(numEntries / 4);
// List<BarEntry> lightSleepEntries = new ArrayList<>(numEntries / 4);
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
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) {
// value = Y_VALUE_DEEP_SLEEP;
value = ((float) movement) / movement_divisor;
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
activityEntries.add(createBarEntry(value, i));
colors.add(akDeepSleep.color);
} else {
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
value = ((float) movement) / movement_divisor;
// value += SleepUtils.Y_VALUE_LIGHT_SLEEP;
// value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP);
activityEntries.add(createBarEntry(value, i));
colors.add(akLightSleep.color);
} 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;
}
value = ((float) movement) / movement_divisor;
activityEntries.add(createBarEntry(value, i));
colors.add(akActivity.color);
}
}
String xLabel = "";
if (annotate) {
cal.setTimeInMillis(sample.getTimestamp() * 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);
// 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);
// }
// }
last_type = type;
}
xLabels.add(xLabel);
}
mChart.getXAxis().setValues(xLabels);
// BarDataSet deepSleepSet = createDeepSleepSet(deepSleepEntries, "Deep Sleep");
// BarDataSet lightSleepSet = createLightSleepSet(lightSleepEntries, "Light Sleep");
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
ArrayList<BarDataSet> dataSets = new ArrayList<>();
// dataSets.add(deepSleepSet);
// dataSets.add(lightSleepSet);
dataSets.add(activitySet);
// create a data object with the datasets
BarData data = new BarData(xLabels, dataSets);
data.setGroupSpace(0);
mChart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
// mChart.setDescriptionPosition(?, ?);
// set data
setupLegend(mChart);
mChart.setData(data);
mChart.animateX(500, Easing.EasingOption.EaseInOutQuart);
// textView.setText(dateStringFrom + " to " + dateStringTo);
}
}
private void setupLegend(BarLineChartBase chart) {
protected void setupLegend(BarLineChartBase chart) {
List<Integer> legendColors = new ArrayList<>(3);
List<String> legendLabels = new ArrayList<>(3);
legendColors.add(akActivity.color);

View File

@ -27,6 +27,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
@ -58,7 +59,7 @@ public class SleepChartFragment extends AbstractChartFragment {
mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1);
mTimestampFrom = intent.getIntExtra("recording_base_timestamp", -1);
mSmartAlarmGoneOff = intent.getIntExtra("alarm_gone_off", -1);
refresh();
refresh(mGBDevice, mChart);
}
}
};
@ -116,7 +117,7 @@ public class SleepChartFragment extends AbstractChartFragment {
yAxisRight.setDrawTopYLabelEntry(false);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
refresh();
refresh(mGBDevice, mChart);
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
// mChart.getLegend().setEnabled(false);
@ -133,149 +134,7 @@ public class SleepChartFragment extends AbstractChartFragment {
super.onDestroy();
}
private void refresh() {
if (mGBDevice == null) {
return;
}
// ArrayList<GBActivitySample> samples = getTestSamples(mGBDevice, -1, -1);
ArrayList<GBActivitySample> samples = getSleepSamples(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;
boolean annotate = true;
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);
// List<BarEntry> deepSleepEntries = new ArrayList<>(numEntries / 4);
// List<BarEntry> lightSleepEntries = new ArrayList<>(numEntries / 4);
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
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) {
// value = Y_VALUE_DEEP_SLEEP;
value = ((float) movement) / movement_divisor;
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
activityEntries.add(createBarEntry(value, i));
colors.add(akDeepSleep.color);
} else {
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
value = ((float) movement) / movement_divisor;
// value += SleepUtils.Y_VALUE_LIGHT_SLEEP;
// value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP);
activityEntries.add(createBarEntry(value, i));
colors.add(akLightSleep.color);
} 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;
}
value = ((float) movement) / movement_divisor;
activityEntries.add(createBarEntry(value, i));
colors.add(akActivity.color);
}
}
String xLabel = "";
if (annotate) {
cal.setTimeInMillis(sample.getTimestamp() * 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);
// 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);
// }
// }
last_type = type;
}
xLabels.add(xLabel);
}
mChart.getXAxis().setValues(xLabels);
// BarDataSet deepSleepSet = createDeepSleepSet(deepSleepEntries, "Deep Sleep");
// BarDataSet lightSleepSet = createLightSleepSet(lightSleepEntries, "Light Sleep");
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
ArrayList<BarDataSet> dataSets = new ArrayList<>();
// dataSets.add(deepSleepSet);
// dataSets.add(lightSleepSet);
dataSets.add(activitySet);
// create a data object with the datasets
BarData data = new BarData(xLabels, dataSets);
data.setGroupSpace(0);
mChart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
// mChart.setDescriptionPosition(?, ?);
// set data
setupLegend(mChart);
mChart.setData(data);
mChart.animateX(500, Easing.EasingOption.EaseInOutQuart);
// textView.setText(dateStringFrom + " to " + dateStringTo);
}
}
private void setupLegend(BarLineChartBase chart) {
protected void setupLegend(BarLineChartBase chart) {
List<Integer> legendColors = new ArrayList<>(3);
List<String> legendLabels = new ArrayList<>(3);
legendColors.add(akActivity.color);

View File

@ -21,6 +21,13 @@ import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.*;
public class ActivityDatabaseHandler extends SQLiteOpenHelper {
private static final int TYPE_ACTIVITY = 1;
private static final int TYPE_LIGHT_SLEEP = 2;
private static final int TYPE_DEEP_SLEEP = 4;
private static final int TYPE_SLEEP = TYPE_LIGHT_SLEEP | TYPE_DEEP_SLEEP;
private static final int TYPE_ALL = TYPE_ACTIVITY | TYPE_SLEEP;
private static final Logger LOG = LoggerFactory.getLogger(ActivityDatabaseHandler.class);
private static final int DATABASE_VERSION = 5;
@ -110,7 +117,28 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
}
}
public ArrayList<GBActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, byte provider) {
public ArrayList<GBActivitySample> getSleepSamples(int timestamp_from, int timestamp_to, byte provider) {
return getGBActivitySamples(timestamp_from, timestamp_to, TYPE_SLEEP, provider);
}
public ArrayList<GBActivitySample> getActivitySamples(int timestamp_from, int timestamp_to, byte provider) {
return getGBActivitySamples(timestamp_from, timestamp_to, TYPE_ACTIVITY, provider);
}
public ArrayList<GBActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to, byte provider) {
return getGBActivitySamples(timestamp_from, timestamp_to, TYPE_ALL, provider);
}
/**
* Returns all available activity samples from between the two timestamps (inclusive), of the given
* provided and type(s).
* @param timestamp_from
* @param timestamp_to
* @param activityTypes ORed combination of #TYPE_DEEP_SLEEP, #TYPE_LIGHT_SLEEP, #TYPE_ACTIVITY
* @param provider
* @return
*/
private ArrayList<GBActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityTypes, byte provider) {
if (timestamp_to == -1) {
timestamp_to = Integer.MAX_VALUE; // dont know what happens when I use more than max of a signed int
}