2015-06-23 23:03:05 +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;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
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;
|
2015-07-12 23:42:23 +02:00
|
|
|
import java.util.GregorianCalendar;
|
2015-06-23 23:03:05 +02:00
|
|
|
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-23 23:03:05 +02:00
|
|
|
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
public class SleepChartFragment extends AbstractChartFragment {
|
|
|
|
protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
|
2015-06-23 23:03:05 +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();
|
|
|
|
if (action.equals(ACTION_REFRESH)) {
|
|
|
|
// 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);
|
2015-07-12 23:42:23 +02:00
|
|
|
refresh(mGBDevice, mChart);
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
|
|
|
|
|
|
|
|
Bundle extras = getActivity().getIntent().getExtras();
|
|
|
|
if (extras != null) {
|
2015-07-11 00:03:41 +02:00
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
|
|
|
filter.addAction(ACTION_REFRESH);
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);
|
|
|
|
|
|
|
|
mChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
|
|
|
|
|
|
|
|
setupChart();
|
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupChart() {
|
2015-07-11 00:03:41 +02:00
|
|
|
mChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mChart.setDescriptionColor(DESCRIPTION_COLOR);
|
2015-06-23 23:03:05 +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-23 23:03:05 +02:00
|
|
|
x.setDrawLimitLinesBehindData(true);
|
|
|
|
|
|
|
|
YAxis y = mChart.getAxisLeft();
|
|
|
|
y.setDrawGridLines(false);
|
|
|
|
// y.setDrawLabels(false);
|
|
|
|
// TODO: make fixed max value optional
|
|
|
|
y.setAxisMaxValue(1f);
|
|
|
|
y.setDrawTopYLabelEntry(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
y.setTextColor(CHART_TEXT_COLOR);
|
2015-06-23 23:03:05 +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-23 23:03:05 +02:00
|
|
|
|
2015-07-12 23:42:23 +02:00
|
|
|
refresh(mGBDevice, mChart);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2015-06-23 23:03:05 +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-23 23:03:05 +02:00
|
|
|
mChart.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:42:23 +02:00
|
|
|
protected void setupLegend(BarLineChartBase chart) {
|
2015-06-23 23:03:05 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|