2021-01-31 11:10:03 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Dikay900, Pavel Elagin
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
2021-02-11 18:47:05 +01:00
|
|
|
import android.content.Context;
|
2021-01-31 11:10:03 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2021-02-11 18:47:05 +01:00
|
|
|
import android.widget.TextView;
|
2021-01-31 11:10:03 +01:00
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
2021-01-31 11:10:03 +01:00
|
|
|
import com.github.mikephil.charting.charts.LineChart;
|
|
|
|
import com.github.mikephil.charting.components.Legend;
|
2021-02-11 18:47:05 +01:00
|
|
|
import com.github.mikephil.charting.components.MarkerView;
|
2021-01-31 11:10:03 +01:00
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
2021-02-11 18:47:05 +01:00
|
|
|
import com.github.mikephil.charting.data.ChartData;
|
2021-01-31 11:10:03 +01:00
|
|
|
import com.github.mikephil.charting.data.Entry;
|
|
|
|
import com.github.mikephil.charting.data.LineData;
|
|
|
|
import com.github.mikephil.charting.data.LineDataSet;
|
2021-02-11 18:47:05 +01:00
|
|
|
import com.github.mikephil.charting.formatter.ValueFormatter;
|
|
|
|
import com.github.mikephil.charting.highlight.Highlight;
|
|
|
|
import com.github.mikephil.charting.utils.MPPointF;
|
2021-01-31 11:10:03 +01:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-02-11 18:47:05 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2021-01-31 11:10:03 +01:00
|
|
|
import java.util.ArrayList;
|
2021-02-11 18:47:05 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
2021-01-31 11:10:03 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment;
|
2021-03-04 17:59:51 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
|
2021-01-31 11:10:03 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BatteryLevel;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BatteryLevelDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
public class BatteryInfoChartFragment extends AbstractGBFragment {
|
2021-01-31 11:10:03 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoChartFragment.class);
|
2021-02-25 20:11:19 +01:00
|
|
|
protected int BACKGROUND_COLOR;
|
|
|
|
protected int DESCRIPTION_COLOR;
|
|
|
|
protected int CHART_TEXT_COLOR;
|
|
|
|
protected int LEGEND_TEXT_COLOR;
|
|
|
|
protected String BATTERY_LABEL;
|
2021-01-31 11:10:03 +01:00
|
|
|
|
|
|
|
private LineChart mChart;
|
|
|
|
private int startTime;
|
|
|
|
private int endTime;
|
|
|
|
private GBDevice gbDevice;
|
2021-10-16 22:40:30 +02:00
|
|
|
private int batteryIndex;
|
2021-01-31 11:10:03 +01:00
|
|
|
|
2021-10-16 22:40:30 +02:00
|
|
|
public void setDateAndGetData(GBDevice gbDevice, int batteryIndex, long startTime, long endTime) {
|
2021-01-31 11:10:03 +01:00
|
|
|
this.startTime = (int) startTime;
|
|
|
|
this.endTime = (int) endTime;
|
|
|
|
this.gbDevice = gbDevice;
|
2021-10-16 22:40:30 +02:00
|
|
|
this.batteryIndex = batteryIndex;
|
2021-01-31 11:10:03 +01:00
|
|
|
try {
|
2021-03-04 17:59:51 +01:00
|
|
|
createRefreshTask("Visualizing data", getActivity()).execute();
|
2021-01-31 11:10:03 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.debug("Unable to fill charts data right now:", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
|
2021-03-04 17:59:51 +01:00
|
|
|
protected RefreshTask createRefreshTask(String task, Context context) {
|
|
|
|
return new RefreshTask(task, context);
|
2021-02-11 18:47:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultBatteryChartsData fill_dcd(List<? extends BatteryLevel> samples) {
|
2021-02-25 20:11:19 +01:00
|
|
|
AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation();
|
2021-02-11 18:47:05 +01:00
|
|
|
List<Entry> entries = new ArrayList<Entry>();
|
|
|
|
int firstTs = 0;
|
|
|
|
|
|
|
|
for (BatteryLevel sample : samples) {
|
|
|
|
entries.add(new Entry(tsTranslation.shorten(sample.getTimestamp()), sample.getLevel()));
|
|
|
|
if (firstTs == 0) {
|
|
|
|
firstTs = sample.getTimestamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
LineDataSet dataSet = new LineDataSet(entries, BATTERY_LABEL);
|
2021-02-11 18:47:05 +01:00
|
|
|
dataSet.setLineWidth(2.2f);
|
|
|
|
dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
|
|
|
|
dataSet.setCubicIntensity(0.1f);
|
|
|
|
dataSet.setDrawCircles(false);
|
|
|
|
dataSet.setCircleRadius(2f);
|
|
|
|
dataSet.setDrawValues(true);
|
|
|
|
dataSet.setValueTextColor(CHART_TEXT_COLOR);
|
|
|
|
dataSet.setHighlightEnabled(true);
|
|
|
|
dataSet.setHighlightEnabled(true);
|
|
|
|
LineData lineData = new LineData(dataSet);
|
|
|
|
|
|
|
|
return new DefaultBatteryChartsData(lineData, new customFormatter(tsTranslation), firstTs);
|
2021-01-31 11:10:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
private void init() {
|
|
|
|
BACKGROUND_COLOR = GBApplication.getBackgroundColor(getContext());
|
|
|
|
LEGEND_TEXT_COLOR = DESCRIPTION_COLOR = GBApplication.getTextColor(getContext());
|
|
|
|
CHART_TEXT_COLOR = ContextCompat.getColor(getContext(), R.color.secondarytext);
|
|
|
|
BATTERY_LABEL = getString(R.string.battery_level);
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:10:03 +01:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
init();
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
|
|
|
|
mChart = rootView.findViewById(R.id.activitysleepchart);
|
2021-02-11 18:47:05 +01:00
|
|
|
if (this.gbDevice != null) {
|
2021-01-31 11:10:03 +01:00
|
|
|
setupChart();
|
2021-03-04 17:59:51 +01:00
|
|
|
createRefreshTask("Visualizing data", getActivity()).execute();
|
2021-01-31 11:10:03 +01:00
|
|
|
}
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupChart() {
|
2021-02-11 18:47:05 +01:00
|
|
|
LEGEND_TEXT_COLOR = GBApplication.getTextColor(getContext());
|
|
|
|
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2021-01-31 11:10:03 +01:00
|
|
|
mChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mChart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
2021-02-11 18:47:05 +01:00
|
|
|
mChart.setTouchEnabled(true);
|
2021-02-25 20:11:19 +01:00
|
|
|
mChart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
|
2021-02-27 11:05:16 +01:00
|
|
|
mChart.getDescription().setEnabled(false);
|
2021-02-11 18:47:05 +01:00
|
|
|
|
2021-01-31 11:10:03 +01:00
|
|
|
XAxis x = mChart.getXAxis();
|
|
|
|
x.setDrawLabels(true);
|
|
|
|
x.setDrawGridLines(false);
|
|
|
|
x.setEnabled(true);
|
2021-02-11 18:47:05 +01:00
|
|
|
mChart.getXAxis().setSpaceMin(0.5f);
|
|
|
|
x.setLabelCount(3);
|
2021-01-31 11:10:03 +01:00
|
|
|
x.setTextColor(CHART_TEXT_COLOR);
|
|
|
|
|
2021-02-11 18:47:05 +01:00
|
|
|
YAxis yAxisLeft = mChart.getAxisLeft();
|
2021-01-31 11:10:03 +01:00
|
|
|
yAxisLeft.setAxisMaximum(100L);
|
|
|
|
yAxisLeft.setAxisMinimum(0);
|
|
|
|
yAxisLeft.setEnabled(true);
|
2021-02-11 18:47:05 +01:00
|
|
|
yAxisLeft.setTextColor(CHART_TEXT_COLOR);
|
2021-01-31 11:10:03 +01:00
|
|
|
|
|
|
|
YAxis yAxisRight = mChart.getAxisRight();
|
2021-02-11 18:47:05 +01:00
|
|
|
yAxisRight.setAxisMaximum(100L);
|
2021-01-31 11:10:03 +01:00
|
|
|
yAxisRight.setAxisMinimum(0);
|
2021-02-11 18:47:05 +01:00
|
|
|
yAxisRight.setEnabled(true);
|
|
|
|
yAxisRight.setTextColor(CHART_TEXT_COLOR);
|
2021-01-31 11:10:03 +01:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:40:30 +02:00
|
|
|
private List<? extends BatteryLevel> getBatteryLevels(DBHandler db, GBDevice device, int batteryIndex, int tsFrom, int tsTo) {
|
2021-01-31 11:10:03 +01:00
|
|
|
BatteryLevelDao batteryLevelDao = db.getDaoSession().getBatteryLevelDao();
|
|
|
|
Device dbDevice = DBHelper.findDevice(device, db.getDaoSession());
|
|
|
|
QueryBuilder<BatteryLevel> qb = batteryLevelDao.queryBuilder();
|
|
|
|
|
|
|
|
qb.where(BatteryLevelDao.Properties.DeviceId.eq(dbDevice.getId())).orderAsc(BatteryLevelDao.Properties.Timestamp);
|
2021-10-16 22:40:30 +02:00
|
|
|
qb.where(BatteryLevelDao.Properties.BatteryIndex.eq(batteryIndex));
|
2021-01-31 11:10:03 +01:00
|
|
|
qb.where(BatteryLevelDao.Properties.Timestamp.gt(tsFrom));
|
|
|
|
qb.where(BatteryLevelDao.Properties.Timestamp.lt(tsTo));
|
|
|
|
|
2021-02-11 18:47:05 +01:00
|
|
|
List<BatteryLevel> levels = new ArrayList<>();
|
|
|
|
levels.addAll(qb.build().list());
|
|
|
|
return levels;
|
2021-01-31 11:10:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 18:47:05 +01:00
|
|
|
protected static class customFormatter extends ValueFormatter {
|
2021-02-25 20:11:19 +01:00
|
|
|
private final AbstractChartFragment.TimestampTranslation tsTranslation;
|
2021-02-11 18:47:05 +01:00
|
|
|
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("dd.MM HH:mm");
|
|
|
|
Calendar cal = GregorianCalendar.getInstance();
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
public customFormatter(AbstractChartFragment.TimestampTranslation tsTranslation) {
|
2021-02-11 18:47:05 +01:00
|
|
|
this.tsTranslation = tsTranslation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFormattedValue(float value) {
|
|
|
|
cal.clear();
|
|
|
|
int ts = (int) value;
|
|
|
|
cal.setTimeInMillis(tsTranslation.toOriginalValue(ts) * 1000L);
|
|
|
|
Date date = cal.getTime();
|
2021-02-14 16:46:57 +01:00
|
|
|
return annotationDateFormat.format(date);
|
2021-02-11 18:47:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:59:51 +01:00
|
|
|
public class RefreshTask extends DBAccess {
|
|
|
|
|
|
|
|
public RefreshTask(String task, Context context) {
|
|
|
|
super(task, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doInBackground(DBHandler handler) {
|
2021-10-16 22:40:30 +02:00
|
|
|
List<? extends BatteryLevel> samples = getBatteryLevels(handler, gbDevice, batteryIndex, startTime, endTime);
|
2021-03-04 17:59:51 +01:00
|
|
|
DefaultBatteryChartsData dcd = null;
|
|
|
|
try {
|
|
|
|
dcd = fill_dcd(samples);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.debug("Unable to get charts data right now:", e);
|
|
|
|
}
|
|
|
|
if (dcd != null && mChart != null) {
|
|
|
|
mChart.setTouchEnabled(true);
|
|
|
|
mChart.setMarker(new batteryValuesAndDateMarker(getContext(), R.layout.custom_chart_marker, dcd.firstTs));
|
|
|
|
mChart.getXAxis().setValueFormatter(dcd.getXValueFormatter());
|
|
|
|
mChart.setData((LineData) dcd.getData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
|
|
|
mChart.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:11:19 +01:00
|
|
|
private class DefaultBatteryChartsData extends AbstractChartFragment.DefaultChartsData {
|
2021-02-11 18:47:05 +01:00
|
|
|
public int firstTs;
|
|
|
|
|
|
|
|
public DefaultBatteryChartsData(ChartData data, ValueFormatter xValueFormatter, int ts) {
|
|
|
|
super(data, xValueFormatter);
|
|
|
|
firstTs = ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class batteryValuesAndDateMarker extends MarkerView {
|
|
|
|
|
|
|
|
customFormatter formatter;
|
|
|
|
private TextView top_text;
|
|
|
|
private TextView bottom_text;
|
|
|
|
private MPPointF mOffset;
|
|
|
|
private int firstTs;
|
|
|
|
|
|
|
|
public batteryValuesAndDateMarker(Context context, int layoutResource, int ts) {
|
|
|
|
super(context, layoutResource);
|
2021-02-25 20:11:19 +01:00
|
|
|
AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation();
|
2021-02-11 18:47:05 +01:00
|
|
|
formatter = new customFormatter(tsTranslation);
|
|
|
|
top_text = (TextView) findViewById(R.id.chart_marker_item_top);
|
|
|
|
bottom_text = (TextView) findViewById(R.id.chart_marker_item_bottom);
|
|
|
|
firstTs = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callbacks everytime the MarkerView is redrawn, can be used to update the
|
|
|
|
// content (user-interface)
|
|
|
|
@Override
|
|
|
|
public void refreshContent(Entry e, Highlight highlight) {
|
|
|
|
|
|
|
|
top_text.setText(String.format("%1s%%", (int) e.getY()));
|
|
|
|
bottom_text.setText(formatter.getFormattedValue(e.getX() + firstTs));
|
|
|
|
|
|
|
|
// this will perform necessary layouting
|
|
|
|
super.refreshContent(e, highlight);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MPPointF getOffset() {
|
|
|
|
|
|
|
|
if (mOffset == null) {
|
|
|
|
// center the marker horizontally and vertically
|
|
|
|
mOffset = new MPPointF(-(getWidth() / 2) + 20, -getHeight() - 10);
|
|
|
|
}
|
|
|
|
return mOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:10:03 +01:00
|
|
|
}
|