1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +01:00

add time span and select date

This commit is contained in:
vanous 2021-02-11 18:47:05 +01:00
parent 99830f5a84
commit cde018399a
5 changed files with 319 additions and 60 deletions

View File

@ -1,25 +1,35 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class BatteryInfoActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoActivity.class);
GBDevice gbDevice;
private int timeFrom;
private int timeTo;
private int timeSpanDays = 14;
@Override
protected void onCreate(Bundle savedInstanceState) {
GBDevice gbDevice;
super.onCreate(savedInstanceState);
final Context appContext = this.getApplicationContext();
@ -42,20 +52,106 @@ public class BatteryInfoActivity extends AbstractGBActivity {
.replace(R.id.batteryChartFragmentHolder, batteryInfoChartFragment)
.commit();
int timeTo = (int) (System.currentTimeMillis() / 1000);
int timeFrom = timeTo - 24 * 3600 * 14; //two weeks of data for the start
timeTo = (int) (System.currentTimeMillis() / 1000);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
TextView battery_status_device_name_text = (TextView) findViewById(R.id.battery_status_device_name_text);
TextView battery_status_device_name_text = (TextView) findViewById(R.id.battery_status_device_name);
TextView battery_status_battery_voltage = (TextView) findViewById(R.id.battery_status_battery_voltage);
SeekBar battery_status_time_span_seekbar = (SeekBar) findViewById(R.id.battery_status_time_span_seekbar);
final TextView battery_status_time_span_text = (TextView) findViewById(R.id.battery_status_time_span_text);
battery_status_time_span_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String text;
switch (i) {
case 0:
text = "Day";
timeSpanDays = 1;
break;
case 1:
text = "Week";
timeSpanDays = 7;
break;
case 2:
text = "Two weeks";
timeSpanDays = 14;
break;
case 3:
text = "Month";
timeSpanDays = 30;
break;
case 4:
text = "Six months";
timeSpanDays = 182;
break;
case 5:
text = "Year";
timeSpanDays = 365;
break;
default:
text = "Two weeks";
timeSpanDays = 14;
}
battery_status_time_span_text.setText(text);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Button battery_status_calendar_button = findViewById(R.id.battery_status_calendar_button);
battery_status_calendar_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar currentDate = Calendar.getInstance();
currentDate.setTimeInMillis(timeTo * 1000L);
Context context = getApplicationContext();
if (context instanceof GBApplication) {
new DatePickerDialog(BatteryInfoActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar date = Calendar.getInstance();
date.set(year, monthOfYear, dayOfMonth);
timeTo = (int) (date.getTimeInMillis() / 1000);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
}
});
battery_status_time_span_seekbar.setProgress(2);
ImageView battery_status_device_icon = findViewById(R.id.battery_status_device_icon);
battery_status_device_icon.setImageResource(gbDevice.isInitialized() ? gbDevice.getType().getIcon() : gbDevice.getType().getDisabledIcon());
TextView battery_status_battery_level_text = (TextView) findViewById(R.id.battery_status_battery_level_text);
TextView battery_status_battery_level_text = (TextView) findViewById(R.id.battery_status_battery_level);
String level = gbDevice.getBatteryLevel() > 0 ? String.format("%1s%%", gbDevice.getBatteryLevel()) : "";
String voltage = gbDevice.getBatteryVoltage() > 0 ? String.format("%1sV", gbDevice.getBatteryVoltage()) : "";
String level = String.valueOf(gbDevice.getBatteryLevel());
String state = String.valueOf(gbDevice.getBatteryState());
battery_status_device_name_text.setText(gbDevice.getName());
battery_status_battery_level_text.setText(String.format("%s%%", level));
battery_status_battery_level_text.setText(level);
battery_status_battery_voltage.setText(voltage);
}
}

View File

@ -17,25 +17,35 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import de.greenrobot.dao.query.QueryBuilder;
@ -66,7 +76,7 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
this.endTime = (int) endTime;
this.gbDevice = gbDevice;
try {
setupLegend(mChart);
//setupLegend(mChart);
populate_charts_data();
} catch (Exception e) {
LOG.debug("Unable to fill charts data right now:", e);
@ -74,39 +84,54 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
}
private void populate_charts_data() {
int LEGEND_TEXT_COLOR = 0;
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);
List<? extends BatteryLevel> samples = getBatteryLevels(handler, gbDevice, startTime, endTime);
DefaultBatteryChartsData dcd = null;
try {
if (lineData != null) {
//mChart.getXAxis().setValueFormatter(null);
//mChart.setData(null);
mChart.setData((LineData) lineData);
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());
mChart.invalidate();
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
private DefaultBatteryChartsData fill_dcd(List<? extends BatteryLevel> samples) {
TimestampTranslation tsTranslation = new TimestampTranslation();
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();
}
}
LineDataSet dataSet = new LineDataSet(entries, "Battery level");
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);
}
@Override
@ -115,7 +140,7 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
init();
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
mChart = rootView.findViewById(R.id.activitysleepchart);
if (this.gbDevice != null || mChart != null) {
if (this.gbDevice != null) {
setupChart();
populate_charts_data();
}
@ -128,36 +153,36 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
}
private void setupChart() {
LEGEND_TEXT_COLOR = GBApplication.getTextColor(getContext());
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
mChart.setBackgroundColor(BACKGROUND_COLOR);
mChart.getDescription().setTextColor(DESCRIPTION_COLOR);
configureBarLineChartDefaults(mChart);
mChart.setTouchEnabled(true);
XAxis x = mChart.getXAxis();
x.setDrawLabels(true);
x.setDrawGridLines(false);
x.setEnabled(true);
mChart.getXAxis().setSpaceMin(0.5f);
x.setLabelCount(3);
x.setTextColor(CHART_TEXT_COLOR);
x.setDrawLimitLinesBehindData(true);
YAxis yAxisLeft = mChart.getAxisRight();
YAxis yAxisLeft = mChart.getAxisLeft();
yAxisLeft.setAxisMaximum(100L);
yAxisLeft.setAxisMinimum(0);
yAxisLeft.setEnabled(true);
yAxisLeft.setTextColor(CHART_TEXT_COLOR);
YAxis yAxisRight = mChart.getAxisRight();
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawLabels(true);
yAxisRight.setDrawTopYLabelEntry(true);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
yAxisRight.setAxisMaximum(100);
yAxisRight.setAxisMaximum(100L);
yAxisRight.setAxisMinimum(0);
yAxisRight.setEnabled(false);
yAxisRight.setEnabled(true);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
}
private List<? extends BatteryLevel> getLevels(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
private List<? extends BatteryLevel> getBatteryLevels(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();
@ -165,9 +190,9 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
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;
List<BatteryLevel> levels = new ArrayList<>();
levels.addAll(qb.build().list());
return levels;
}
@Override
@ -182,6 +207,7 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
@Override
protected void renderCharts() {
}
@Override
@ -197,4 +223,75 @@ public class BatteryInfoChartFragment extends AbstractChartFragment {
protected void updateChartsnUIThread(ChartsData chartsData) {
}
protected static class customFormatter extends ValueFormatter {
private final TimestampTranslation tsTranslation;
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("dd.MM HH:mm");
Calendar cal = GregorianCalendar.getInstance();
public customFormatter(TimestampTranslation tsTranslation) {
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();
String dateString = annotationDateFormat.format(date);
return dateString;
}
}
private class DefaultBatteryChartsData extends DefaultChartsData {
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);
TimestampTranslation tsTranslation = new TimestampTranslation();
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;
}
}
}

View File

@ -141,7 +141,7 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
//begin of action row
//battery
holder.batteryStatusBox.setVisibility(View.GONE);
holder.batteryStatusBox.setVisibility(View.VISIBLE);
short batteryLevel = device.getBatteryLevel();
float batteryVoltage = device.getBatteryVoltage();
BatteryState batteryState = device.getBatteryState();
@ -165,12 +165,10 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
{
@Override
public void onClick(View v) {
/* will be enabled soon :)
Intent startIntent;
startIntent = new Intent(context, BatteryInfoActivity.class);
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
context.startActivity(startIntent);
*/
}
}
);

View File

@ -27,8 +27,8 @@
android:id="@+id/battery_status_device_icon"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/ic_devices_other"
android:contentDescription="TODO" />
android:contentDescription="TODO"
app:srcCompat="@drawable/ic_devices_other" />
<LinearLayout
android:id="@+id/activity_battery_info_top_layout"
@ -39,7 +39,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/battery_status_device_name_text"
android:id="@+id/battery_status_device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
@ -47,7 +47,7 @@
tools:text="Device name" />
<TextView
android:id="@+id/battery_status_battery_level_text"
android:id="@+id/battery_status_battery_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
@ -55,6 +55,18 @@
android:textSize="48sp"
tools:text="90%" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/battery_status_battery_voltage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
@ -69,7 +81,35 @@
<FrameLayout
android:id="@+id/batteryChartFragmentHolder"
android:layout_width="match_parent"
android:layout_height="400dp"/>
android:layout_height="350dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/battery_status_time_span_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="20sp" />
<SeekBar
android:id="@+id/battery_status_time_span_seekbar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="5"
android:progress="1" />
<Button
android:id="@+id/battery_status_calendar_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose end date" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/chart_marker_item_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="@+id/chart_marker_item_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/chart_marker_item_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chart_marker_item_top" />
</androidx.constraintlayout.widget.ConstraintLayout>