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.os.Bundle;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import com.github.mikephil.charting.charts.BarLineChartBase;
|
2015-07-14 00:29:32 +02:00
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
|
|
|
import com.github.mikephil.charting.charts.PieChart;
|
2015-06-23 23:03:05 +02:00
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
2015-07-14 00:29:32 +02:00
|
|
|
import com.github.mikephil.charting.data.Entry;
|
|
|
|
import com.github.mikephil.charting.data.PieData;
|
|
|
|
import com.github.mikephil.charting.data.PieDataSet;
|
2015-07-20 23:11:16 +02:00
|
|
|
import com.github.mikephil.charting.utils.ValueFormatter;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-07-20 23:11:16 +02:00
|
|
|
import java.text.NumberFormat;
|
2015-06-23 23:03:05 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-07-20 23:11:16 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
|
2015-07-20 23:11:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GB;
|
2015-06-23 23:03:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-07-14 00:29:32 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.charts.ActivityAmount;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.charts.ActivityAmounts;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.charts.ActivityAnalysis;
|
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
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
private BarLineChartBase mActivityChart;
|
|
|
|
private PieChart mSleepAmountChart;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
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-14 00:29:32 +02:00
|
|
|
refresh();
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
private void refresh() {
|
|
|
|
List<GBActivitySample> samples = getSamples();
|
|
|
|
refresh(mGBDevice, mActivityChart, getSamples());
|
|
|
|
refreshSleepAmounts(mGBDevice, mSleepAmountChart, samples);
|
|
|
|
|
|
|
|
mActivityChart.invalidate();
|
|
|
|
mSleepAmountChart.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<GBActivitySample> getSamples() {
|
|
|
|
return getSamples(mGBDevice, -1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refreshSleepAmounts(GBDevice mGBDevice, PieChart pieChart, List<GBActivitySample> samples) {
|
|
|
|
ActivityAnalysis analysis = new ActivityAnalysis();
|
|
|
|
ActivityAmounts amounts = analysis.calculateActivityAmounts(samples);
|
2015-07-20 23:11:16 +02:00
|
|
|
String totalSleep = GB.formatDurationHoursMinutes(amounts.getTotalSeconds(), TimeUnit.SECONDS);
|
|
|
|
pieChart.setCenterText(totalSleep);
|
2015-07-14 00:29:32 +02:00
|
|
|
PieData data = new PieData();
|
|
|
|
List<Entry> entries = new ArrayList<>();
|
|
|
|
List<Integer> colors = new ArrayList<>();
|
|
|
|
int index = 0;
|
|
|
|
for (ActivityAmount amount : amounts.getAmounts()) {
|
2015-07-20 23:11:16 +02:00
|
|
|
long value = amount.getTotalSeconds();
|
2015-07-14 00:29:32 +02:00
|
|
|
entries.add(new Entry(amount.getTotalSeconds(), index++));
|
|
|
|
colors.add(getColorFor(amount.getActivityKind()));
|
|
|
|
data.addXValue(amount.getName(getActivity()));
|
|
|
|
}
|
2015-07-20 23:11:16 +02:00
|
|
|
PieDataSet set = new PieDataSet(entries, "");
|
|
|
|
set.setValueFormatter(new ValueFormatter() {
|
|
|
|
@Override
|
|
|
|
public String getFormattedValue(float value) {
|
|
|
|
return GB.formatDurationHoursMinutes((long)value, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
});
|
2015-07-14 00:29:32 +02:00
|
|
|
set.setColors(colors);
|
|
|
|
data.setDataSet(set);
|
|
|
|
pieChart.setData(data);
|
|
|
|
|
|
|
|
setupLegend(pieChart);
|
|
|
|
|
|
|
|
pieChart.invalidate();
|
|
|
|
}
|
|
|
|
|
2015-06-23 23:03:05 +02:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
2015-07-14 00:29:32 +02:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
mActivityChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
|
|
|
|
mSleepAmountChart = (PieChart) rootView.findViewById(R.id.sleepchart_pie_light_deep);
|
|
|
|
|
|
|
|
setupActivityChart();
|
|
|
|
setupSleepAmountChart();
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
refresh();
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
private void setupSleepAmountChart() {
|
|
|
|
mSleepAmountChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mSleepAmountChart.setDescriptionColor(DESCRIPTION_COLOR);
|
2015-07-20 23:11:16 +02:00
|
|
|
mSleepAmountChart.setDescription("");
|
|
|
|
mSleepAmountChart.setNoDataTextDescription("");
|
|
|
|
mSleepAmountChart.setNoDataText("");
|
2015-07-14 00:29:32 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:54:46 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
private void setupActivityChart() {
|
|
|
|
mActivityChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mActivityChart.setDescriptionColor(DESCRIPTION_COLOR);
|
|
|
|
configureBarLineChartDefaults(mActivityChart);
|
|
|
|
|
|
|
|
XAxis x = mActivityChart.getXAxis();
|
2015-06-23 23:03:05 +02:00
|
|
|
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);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
YAxis y = mActivityChart.getAxisLeft();
|
2015-06-23 23:03:05 +02:00
|
|
|
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);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
YAxis yAxisRight = mActivityChart.getAxisRight();
|
2015-06-23 23:03:05 +02:00
|
|
|
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-14 00:29:32 +02:00
|
|
|
// mActivityChart.getLegend().setEnabled(false);
|
2015-06-23 23:03:05 +02:00
|
|
|
//
|
2015-07-14 00:29:32 +02:00
|
|
|
// mActivityChart.animateXY(2000, 2000);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
// don't forget to refresh the drawing
|
2015-07-14 00:29:32 +02:00
|
|
|
// mActivityChart.invalidate();
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
protected void setupLegend(Chart chart) {
|
|
|
|
List<Integer> legendColors = new ArrayList<>(2);
|
|
|
|
List<String> legendLabels = new ArrayList<>(2);
|
2015-06-23 23:03:05 +02:00
|
|
|
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-07-14 00:29:32 +02:00
|
|
|
chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
2015-07-13 21:54:46 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<GBActivitySample> getSamples(GBDevice device, int tsFrom, int tsTo) {
|
|
|
|
return super.getSleepSamples(device, tsFrom, tsTo);
|
|
|
|
}
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|