2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
2015-07-11 00:03:41 +02:00
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
2015-08-03 01:17:02 +02:00
|
|
|
import android.content.Context;
|
2015-08-17 02:22:16 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-07-11 00:03:41 +02:00
|
|
|
import android.graphics.Color;
|
2015-08-17 02:22:16 +02:00
|
|
|
import android.os.Bundle;
|
2015-08-16 23:27:18 +02:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
2015-08-17 02:22:16 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
import com.github.mikephil.charting.charts.BarLineChartBase;
|
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
2015-07-12 23:42:23 +02:00
|
|
|
import com.github.mikephil.charting.data.BarData;
|
2015-07-11 00:03:41 +02:00
|
|
|
import com.github.mikephil.charting.data.BarDataSet;
|
|
|
|
import com.github.mikephil.charting.data.BarEntry;
|
|
|
|
|
2015-07-12 23:42:23 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
2015-07-11 00:03:41 +02:00
|
|
|
import java.util.ArrayList;
|
2015-08-17 02:22:16 +02:00
|
|
|
import java.util.Arrays;
|
2015-07-11 00:03:41 +02:00
|
|
|
import java.util.Calendar;
|
2015-07-12 23:42:23 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
2015-08-17 02:22:16 +02:00
|
|
|
import java.util.HashSet;
|
2015-07-11 00:03:41 +02:00
|
|
|
import java.util.List;
|
2015-08-17 02:22:16 +02:00
|
|
|
import java.util.Set;
|
2015-07-11 00:03:41 +02:00
|
|
|
|
2015-07-12 21:48:48 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-08-25 00:33:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragment;
|
2015-08-03 01:17:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-08-25 00:33:37 +02:00
|
|
|
public abstract class AbstractChartFragment extends AbstractGBFragment {
|
2015-07-12 23:42:23 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
|
2015-07-11 00:03:41 +02:00
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
private final Set<String> mIntentFilterActions;
|
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
AbstractChartFragment.this.onReceive(context, intent);
|
|
|
|
}
|
|
|
|
};
|
2015-08-25 00:33:37 +02:00
|
|
|
private boolean mChartDirty = true;
|
|
|
|
|
|
|
|
public boolean isChartDirty() {
|
|
|
|
return mChartDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract String getTitle();
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
protected static final class ActivityConfig {
|
|
|
|
public final int type;
|
2015-07-11 00:03:41 +02:00
|
|
|
public final String label;
|
|
|
|
public final Integer color;
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
public ActivityConfig(int kind, String label, Integer color) {
|
|
|
|
this.type = kind;
|
2015-07-11 00:03:41 +02:00
|
|
|
this.label = label;
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
protected ActivityConfig akActivity = new ActivityConfig(ActivityKind.TYPE_ACTIVITY, "Activity", Color.rgb(89, 178, 44));
|
|
|
|
protected ActivityConfig akLightSleep = new ActivityConfig(ActivityKind.TYPE_LIGHT_SLEEP, "Light Sleep", Color.rgb(182, 191, 255));
|
|
|
|
protected ActivityConfig akDeepSleep = new ActivityConfig(ActivityKind.TYPE_DEEP_SLEEP, "Deep Sleep", Color.rgb(76, 90, 255));
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
protected static final int BACKGROUND_COLOR = Color.rgb(24, 22, 24);
|
|
|
|
protected static final int DESCRIPTION_COLOR = Color.WHITE;
|
|
|
|
protected static final int CHART_TEXT_COLOR = Color.WHITE;
|
|
|
|
protected static final int LEGEND_TEXT_COLOR = Color.WHITE;
|
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
protected AbstractChartFragment(String... intentFilterActions) {
|
|
|
|
mIntentFilterActions = new HashSet<>();
|
|
|
|
if (intentFilterActions != null) {
|
|
|
|
mIntentFilterActions.addAll(Arrays.asList(intentFilterActions));
|
|
|
|
mIntentFilterActions.add(ChartsHost.DATE_NEXT);
|
|
|
|
mIntentFilterActions.add(ChartsHost.DATE_PREV);
|
|
|
|
mIntentFilterActions.add(ChartsHost.REFRESH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
for (String action : mIntentFilterActions) {
|
|
|
|
filter.addAction(action);
|
|
|
|
}
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-25 00:33:37 +02:00
|
|
|
public void onHiddenChanged(boolean hidden) {
|
|
|
|
super.onHiddenChanged(hidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setUserVisibleHint(boolean isVisibleToUser) {
|
|
|
|
super.setUserVisibleHint(isVisibleToUser);
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 00:33:37 +02:00
|
|
|
// @Override
|
|
|
|
// public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
// View view = super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
// updateDateInfo(mStartDate, mEndDate);
|
|
|
|
// return view;
|
|
|
|
// }
|
|
|
|
|
2015-08-25 00:54:16 +02:00
|
|
|
private void setStartDate(Date date) {
|
|
|
|
getHost().setStartDate(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setEndDate(Date date) {
|
|
|
|
getHost().setEndDate(date);
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 00:54:16 +02:00
|
|
|
public Date getStartDate() {
|
|
|
|
return getHost().getStartDate();
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 00:54:16 +02:00
|
|
|
public Date getEndDate() {
|
|
|
|
return getHost().getEndDate();
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 00:33:37 +02:00
|
|
|
/**
|
|
|
|
* Called when this fragment has been fully scrolled into the activity.
|
|
|
|
*
|
|
|
|
* @see #isVisibleInActivity()
|
|
|
|
* @see #onMadeInvisibleInActivity()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onMadeVisibleInActivity() {
|
|
|
|
super.onMadeVisibleInActivity();
|
|
|
|
if (isChartDirty()) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (ChartsHost.REFRESH.equals(action)) {
|
|
|
|
refresh();
|
|
|
|
} else if (ChartsHost.DATE_NEXT.equals(action)) {
|
2015-08-25 00:54:16 +02:00
|
|
|
handleDateNext(getStartDate(), getEndDate());
|
2015-08-17 02:22:16 +02:00
|
|
|
} else if (ChartsHost.DATE_PREV.equals(action)) {
|
2015-08-25 00:54:16 +02:00
|
|
|
handleDatePrev(getStartDate(), getEndDate());
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void handleDatePrev(Date startDate, Date endDate) {
|
2015-08-25 00:54:16 +02:00
|
|
|
if (isVisibleInActivity()) {
|
|
|
|
shiftDates(startDate, endDate, -1);
|
|
|
|
}
|
2015-08-25 00:33:37 +02:00
|
|
|
refreshIfVisible();
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void handleDateNext(Date startDate, Date endDate) {
|
2015-08-25 00:54:16 +02:00
|
|
|
if (isVisibleInActivity()) {
|
|
|
|
shiftDates(startDate, endDate, +1);
|
|
|
|
}
|
2015-08-25 00:33:37 +02:00
|
|
|
refreshIfVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void refreshIfVisible() {
|
|
|
|
if (isVisibleInActivity()) {
|
|
|
|
refresh();
|
|
|
|
} else {
|
|
|
|
mChartDirty = true;
|
|
|
|
}
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void shiftDates(Date startDate, Date endDate, int offset) {
|
|
|
|
Date newStart = DateTimeUtils.shiftByDays(startDate, offset);
|
|
|
|
Date newEnd = DateTimeUtils.shiftByDays(endDate, offset);
|
|
|
|
|
|
|
|
setDateRange(newStart, newEnd);
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:11:16 +02:00
|
|
|
protected Integer getColorFor(int activityKind) {
|
|
|
|
switch (activityKind) {
|
2015-08-03 23:09:49 +02:00
|
|
|
case ActivityKind.TYPE_DEEP_SLEEP:
|
2015-07-20 23:11:16 +02:00
|
|
|
return akDeepSleep.color;
|
2015-08-03 23:09:49 +02:00
|
|
|
case ActivityKind.TYPE_LIGHT_SLEEP:
|
2015-07-20 23:11:16 +02:00
|
|
|
return akLightSleep.color;
|
2015-08-03 23:09:49 +02:00
|
|
|
case ActivityKind.TYPE_ACTIVITY:
|
2015-07-25 21:52:52 +02:00
|
|
|
return akActivity.color;
|
2015-07-20 23:11:16 +02:00
|
|
|
}
|
|
|
|
return akActivity.color;
|
|
|
|
}
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
protected SampleProvider getProvider(GBDevice device) {
|
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
|
|
|
|
return coordinator.getSampleProvider();
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
/**
|
|
|
|
* Returns all kinds of samples for the given device.
|
|
|
|
* To be called from a background thread.
|
|
|
|
* @param device
|
|
|
|
* @param tsFrom
|
|
|
|
* @param tsTo
|
|
|
|
*/
|
|
|
|
protected List<ActivitySample> getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
2015-07-11 00:03:41 +02:00
|
|
|
if (tsFrom == -1) {
|
2015-07-12 23:42:23 +02:00
|
|
|
tsFrom = getTSLast24Hours();
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
2015-07-27 23:49:53 +02:00
|
|
|
SampleProvider provider = getProvider(device);
|
2015-08-03 01:17:02 +02:00
|
|
|
return db.getAllActivitySamples(tsFrom, tsTo, provider);
|
2015-07-12 23:42:23 +02:00
|
|
|
}
|
2015-07-11 00:03:41 +02:00
|
|
|
|
2015-07-12 23:42:23 +02:00
|
|
|
private int getTSLast24Hours() {
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
return (int) ((now / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
protected List<ActivitySample> getActivitySamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
2015-07-12 23:42:23 +02:00
|
|
|
if (tsFrom == -1) {
|
|
|
|
tsFrom = getTSLast24Hours();
|
|
|
|
}
|
2015-07-27 23:49:53 +02:00
|
|
|
SampleProvider provider = getProvider(device);
|
2015-08-03 01:17:02 +02:00
|
|
|
return db.getActivitySamples(tsFrom, tsTo, provider);
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
protected List<ActivitySample> getSleepSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
2015-07-11 00:03:41 +02:00
|
|
|
if (tsFrom == -1) {
|
2015-07-12 23:42:23 +02:00
|
|
|
tsFrom = getTSLast24Hours();
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
2015-07-27 23:49:53 +02:00
|
|
|
SampleProvider provider = getProvider(device);
|
2015-08-03 01:17:02 +02:00
|
|
|
return db.getSleepSamples(tsFrom, tsTo, provider);
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
protected List<ActivitySample> getTestSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
2015-07-11 00:03:41 +02:00
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.clear();
|
|
|
|
cal.set(2015, Calendar.JUNE, 10, 6, 40);
|
|
|
|
// ignore provided date ranges
|
|
|
|
tsTo = (int) ((cal.getTimeInMillis() / 1000) & 0xffffffff);
|
|
|
|
tsFrom = tsTo - (24 * 60 * 60);
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
SampleProvider provider = getProvider(device);
|
2015-08-03 01:17:02 +02:00
|
|
|
return db.getAllActivitySamples(tsFrom, tsTo, provider);
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 21:48:48 +02:00
|
|
|
protected void configureChartDefaults(Chart<?> chart) {
|
2015-07-11 00:03:41 +02:00
|
|
|
// if enabled, the chart will always start at zero on the y-axis
|
2015-07-12 21:48:48 +02:00
|
|
|
chart.setNoDataText(getString(R.string.chart_no_data_synchronize));
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
// disable value highlighting
|
2015-07-12 21:48:48 +02:00
|
|
|
chart.setHighlightEnabled(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
// enable touch gestures
|
2015-07-12 21:48:48 +02:00
|
|
|
chart.setTouchEnabled(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 21:48:48 +02:00
|
|
|
protected void configureBarLineChartDefaults(BarLineChartBase<?> chart) {
|
|
|
|
configureChartDefaults(chart);
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
// enable scaling and dragging
|
2015-07-12 21:48:48 +02:00
|
|
|
chart.setDragEnabled(true);
|
|
|
|
chart.setScaleEnabled(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
|
|
|
|
// if disabled, scaling can be done on x- and y-axis separately
|
2015-07-12 23:42:23 +02:00
|
|
|
// chart.setPinchZoom(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
|
2015-07-12 21:48:48 +02:00
|
|
|
chart.setDrawGridBackground(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
/**
|
|
|
|
* This method will invoke a background task to read the data from the
|
|
|
|
* database, analyze it, prepare it for the charts and eventually call
|
|
|
|
* #renderCharts
|
|
|
|
*/
|
|
|
|
protected void refresh() {
|
2015-08-25 00:33:37 +02:00
|
|
|
if (getHost().getDevice() != null) {
|
|
|
|
mChartDirty = false;
|
2015-08-25 00:54:16 +02:00
|
|
|
updateDateInfo(getStartDate(), getEndDate());
|
2015-08-25 00:33:37 +02:00
|
|
|
createRefreshTask("Visualizing data", getActivity()).execute();
|
|
|
|
}
|
2015-08-03 01:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method reads the data from the database, analyzes and prepares it for
|
|
|
|
* the charts. This will be called from a background task, so there must not be
|
|
|
|
* any UI access. #renderCharts will be automatically called after this method.
|
|
|
|
*/
|
2015-08-25 00:33:37 +02:00
|
|
|
protected abstract void refreshInBackground(DBHandler db, GBDevice device);
|
2015-08-03 01:17:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a re-rendering of the chart.
|
|
|
|
* Always called from the UI thread.
|
|
|
|
*/
|
|
|
|
protected abstract void renderCharts();
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
protected void refresh(GBDevice gbDevice, BarLineChartBase chart, List<ActivitySample> samples) {
|
2015-07-12 23:42:23 +02:00
|
|
|
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;
|
2015-07-27 23:49:53 +02:00
|
|
|
SampleProvider provider = getProvider(gbDevice);
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
int last_type = ActivityKind.TYPE_UNKNOWN;
|
2015-07-12 23:42:23 +02:00
|
|
|
|
|
|
|
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> activityEntries = new ArrayList<>(numEntries);
|
|
|
|
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
|
|
|
|
|
|
|
|
for (int i = 0; i < numEntries; i++) {
|
2015-07-27 23:49:53 +02:00
|
|
|
ActivitySample sample = samples.get(i);
|
|
|
|
int type = sample.getKind();
|
2015-07-12 23:42:23 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
float movement = sample.getIntensity();
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-07-27 23:49:53 +02:00
|
|
|
float value = movement;
|
|
|
|
if (type == ActivityKind.TYPE_DEEP_SLEEP) {
|
2015-07-12 23:42:23 +02:00
|
|
|
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
|
|
|
|
activityEntries.add(createBarEntry(value, i));
|
|
|
|
colors.add(akDeepSleep.color);
|
|
|
|
} else {
|
2015-07-27 23:49:53 +02:00
|
|
|
if (type == ActivityKind.TYPE_LIGHT_SLEEP) {
|
2015-07-12 23:42:23 +02:00
|
|
|
activityEntries.add(createBarEntry(value, i));
|
|
|
|
colors.add(akLightSleep.color);
|
|
|
|
} else {
|
2015-07-27 23:49:53 +02:00
|
|
|
// 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;
|
2015-07-12 23:42:23 +02:00
|
|
|
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 activitySet = createActivitySet(activityEntries, colors, "Activity");
|
|
|
|
|
|
|
|
ArrayList<BarDataSet> dataSets = new ArrayList<>();
|
|
|
|
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(?, ?);
|
|
|
|
|
|
|
|
setupLegend(chart);
|
|
|
|
|
|
|
|
chart.setData(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
protected abstract List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo);
|
2015-07-13 21:54:46 +02:00
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
protected abstract void setupLegend(Chart chart);
|
2015-07-12 23:42:23 +02:00
|
|
|
|
2015-07-25 21:52:52 +02:00
|
|
|
protected BarEntry createBarEntry(float value, int index) {
|
2015-07-11 00:03:41 +02:00
|
|
|
return new BarEntry(value, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected BarDataSet createActivitySet(List<BarEntry> values, List<Integer> colors, String label) {
|
|
|
|
BarDataSet set1 = new BarDataSet(values, label);
|
|
|
|
set1.setColors(colors);
|
|
|
|
// set1.setDrawCubic(true);
|
|
|
|
// set1.setCubicIntensity(0.2f);
|
|
|
|
// //set1.setDrawFilled(true);
|
|
|
|
// set1.setDrawCircles(false);
|
|
|
|
// set1.setLineWidth(2f);
|
|
|
|
// set1.setCircleSize(5f);
|
|
|
|
// set1.setFillColor(ColorTemplate.getHoloBlue());
|
|
|
|
set1.setDrawValues(false);
|
|
|
|
// set1.setHighLightColor(Color.rgb(128, 0, 255));
|
|
|
|
// set1.setColor(Color.rgb(89, 178, 44));
|
|
|
|
set1.setValueTextColor(CHART_TEXT_COLOR);
|
|
|
|
return set1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected BarDataSet createDeepSleepSet(List<BarEntry> values, String label) {
|
|
|
|
BarDataSet set1 = new BarDataSet(values, label);
|
|
|
|
// set1.setDrawCubic(true);
|
|
|
|
// set1.setCubicIntensity(0.2f);
|
|
|
|
// //set1.setDrawFilled(true);
|
|
|
|
// set1.setDrawCircles(false);
|
|
|
|
// set1.setLineWidth(2f);
|
|
|
|
// set1.setCircleSize(5f);
|
|
|
|
// set1.setFillColor(ColorTemplate.getHoloBlue());
|
|
|
|
set1.setDrawValues(false);
|
|
|
|
// set1.setHighLightColor(Color.rgb(244, 117, 117));
|
|
|
|
// set1.setColor(Color.rgb(76, 90, 255));
|
|
|
|
set1.setValueTextColor(CHART_TEXT_COLOR);
|
|
|
|
return set1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected BarDataSet createLightSleepSet(List<BarEntry> values, String label) {
|
|
|
|
BarDataSet set1 = new BarDataSet(values, label);
|
|
|
|
|
|
|
|
// set1.setDrawCubic(true);
|
|
|
|
// set1.setCubicIntensity(0.2f);
|
|
|
|
// //set1.setDrawFilled(true);
|
|
|
|
// set1.setDrawCircles(false);
|
|
|
|
// set1.setLineWidth(2f);
|
|
|
|
// set1.setCircleSize(5f);
|
|
|
|
// set1.setFillColor(ColorTemplate.getHoloBlue());
|
|
|
|
set1.setDrawValues(false);
|
|
|
|
// set1.setHighLightColor(Color.rgb(244, 117, 117));
|
|
|
|
// set1.setColor(Color.rgb(182, 191, 255));
|
|
|
|
set1.setValueTextColor(CHART_TEXT_COLOR);
|
|
|
|
// set1.setColor(Color.CYAN);
|
|
|
|
return set1;
|
|
|
|
}
|
2015-08-03 01:17:02 +02:00
|
|
|
|
|
|
|
protected RefreshTask createRefreshTask(String task, Context context) {
|
|
|
|
return new RefreshTask(task, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class RefreshTask extends DBAccess {
|
|
|
|
public RefreshTask(String task, Context context) {
|
|
|
|
super(task, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doInBackground(DBHandler db) {
|
2015-08-25 00:33:37 +02:00
|
|
|
refreshInBackground(db, getHost().getDevice());
|
2015-08-03 01:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
|
|
|
super.onPostExecute(o);
|
2015-08-16 23:27:18 +02:00
|
|
|
FragmentActivity activity = getActivity();
|
|
|
|
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
|
|
|
renderCharts();
|
|
|
|
} else {
|
|
|
|
LOG.info("Not rendering charts because activity is not available anymore");
|
|
|
|
}
|
2015-08-03 01:17:02 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 02:22:16 +02:00
|
|
|
|
|
|
|
public void setDateRange(Date from, Date to) {
|
|
|
|
if (from.compareTo(to) > 0) {
|
|
|
|
throw new IllegalArgumentException("Bad date range: " +from + ".." + to);
|
|
|
|
}
|
2015-08-25 00:54:16 +02:00
|
|
|
setStartDate(from);
|
|
|
|
setEndDate(to);
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateDateInfo(Date from, Date to) {
|
|
|
|
if (from.equals(to)) {
|
|
|
|
getHost().setDateInfo(DateTimeUtils.formatDate(from));
|
|
|
|
} else {
|
|
|
|
getHost().setDateInfo(DateTimeUtils.formatDateRange(from, to));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device) {
|
2015-08-19 00:52:03 +02:00
|
|
|
return getSamples(db, device, getTSStart(), getTSEnd());
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getTSEnd() {
|
2015-08-25 00:54:16 +02:00
|
|
|
return toTimestamp(getEndDate());
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getTSStart() {
|
2015-08-25 00:54:16 +02:00
|
|
|
return toTimestamp(getStartDate());
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private int toTimestamp(Date date) {
|
|
|
|
return (int) ((date.getTime() / 1000) & 0xffffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ChartsHost getHost() {
|
|
|
|
return (ChartsHost) getActivity();
|
|
|
|
}
|
2015-07-11 00:03:41 +02:00
|
|
|
}
|