1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-14 22:19:29 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoChartFragment.java

201 lines
7.2 KiB
Java
Raw Normal View History

/* 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;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
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;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsData;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
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;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public class BatteryInfoChartFragment extends AbstractChartFragment {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoChartFragment.class);
private LineChart mChart;
private int startTime;
private int endTime;
private GBDevice gbDevice;
public void setDateAndGetData(GBDevice gbDevice, long startTime, long endTime) {
this.startTime = (int) startTime;
this.endTime = (int) endTime;
this.gbDevice = gbDevice;
try {
setupLegend(mChart);
populate_charts_data();
} catch (Exception e) {
LOG.debug("Unable to fill charts data right now:", e);
}
}
private void populate_charts_data() {
try (DBHandler handler = GBApplication.acquireDB()) {
List<? extends BatteryLevel> samples = getLevels(handler, gbDevice, startTime, endTime);
List<Entry> entries = new ArrayList<Entry>();
for (BatteryLevel sample : samples) {
entries.add(new Entry(sample.getTimestamp(), sample.getLevel()));
}
LineDataSet dataSet = new LineDataSet(entries, "Battery level");
dataSet.setDrawCircles(false);
dataSet.setLineWidth(2.2f);
dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
dataSet.setCubicIntensity(0.1f);
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
LineData lineData = new LineData(dataSet);
try {
if (lineData != null) {
//mChart.getXAxis().setValueFormatter(null);
//mChart.setData(null);
mChart.setData((LineData) lineData);
mChart.invalidate();
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
}
@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);
if (this.gbDevice != null || mChart != null) {
setupChart();
populate_charts_data();
}
return rootView;
}
@Override
public String getTitle() {
return "";
}
private void setupChart() {
mChart.setBackgroundColor(BACKGROUND_COLOR);
mChart.getDescription().setTextColor(DESCRIPTION_COLOR);
configureBarLineChartDefaults(mChart);
XAxis x = mChart.getXAxis();
x.setDrawLabels(true);
x.setDrawGridLines(false);
x.setEnabled(true);
x.setTextColor(CHART_TEXT_COLOR);
x.setDrawLimitLinesBehindData(true);
YAxis yAxisLeft = mChart.getAxisRight();
yAxisLeft.setAxisMaximum(100L);
yAxisLeft.setAxisMinimum(0);
yAxisLeft.setEnabled(true);
YAxis yAxisRight = mChart.getAxisRight();
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawLabels(true);
yAxisRight.setDrawTopYLabelEntry(true);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
yAxisRight.setAxisMaximum(100);
yAxisRight.setAxisMinimum(0);
yAxisRight.setEnabled(false);
}
private List<? extends BatteryLevel> getLevels(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
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);
qb.where(BatteryLevelDao.Properties.Timestamp.gt(tsFrom));
qb.where(BatteryLevelDao.Properties.Timestamp.lt(tsTo));
List<BatteryLevel> allLevels = new ArrayList<>();
allLevels.addAll(qb.build().list());
return allLevels;
}
@Override
protected void setupLegend(Chart chart) {
chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
}
@Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
return null;
}
@Override
protected void renderCharts() {
}
@Override
protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
return null;
}
protected Entry createLineEntry(float value, int xValue) {
return new Entry(xValue, value);
}
@Override
protected void updateChartsnUIThread(ChartsData chartsData) {
}
}