Skip to content

Commit

Permalink
Draw battery level on the chart
Browse files Browse the repository at this point in the history
  • Loading branch information
krzys-h committed Jan 23, 2021
1 parent 55a2fd1 commit 1552603
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ private static Entity addDaFitActivitySample(Schema schema, Entity user, Entity
addHeartRateProperties(activitySample);
addBloodPressureProperies(activitySample);
addBloodOxidationProperies(activitySample);
activitySample.addIntProperty("batteryLevel").notNull();
activitySample.addIntProperty("batteryLevel").notNull().codeBeforeGetterAndSetter(OVERRIDE);
return activitySample;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
Expand Down Expand Up @@ -66,8 +67,10 @@
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.dafit.DaFitSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.UserInfo;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaFitActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
Expand Down Expand Up @@ -452,6 +455,7 @@ public DefaultChartsData<LineData> refresh(GBDevice gbDevice, List<? extends Act
List<Entry> notWornEntries = new ArrayList<>(numEntries);
boolean hr = supportsHeartrate(gbDevice);
List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
List<Entry> batteryEntries = new ArrayList<>(numEntries);
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
int lastHrSampleIndex = -1;
HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
Expand Down Expand Up @@ -540,6 +544,10 @@ public DefaultChartsData<LineData> refresh(GBDevice gbDevice, List<? extends Act
heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
lastHrSampleIndex = ts;
}
if (sample.getBatteryLevel() != ActivitySample.NOT_MEASURED)
{
batteryEntries.add(createLineEntry((float)sample.getBatteryLevel() / 100.0f, ts));
}

String xLabel = "";
if (annotate) {
Expand Down Expand Up @@ -577,12 +585,15 @@ public DefaultChartsData<LineData> refresh(GBDevice gbDevice, List<? extends Act
lineDataSets.add(lightSleepSet);
LineDataSet notWornSet = createDataSet(notWornEntries, akNotWorn.color, akNotWorn.label);
lineDataSets.add(notWornSet);

if (hr && heartrateEntries.size() > 0) {
LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");

lineDataSets.add(heartrateSet);
}
if (batteryEntries.size() > 0) {
LineDataSet batterySet = createBatterySet(batteryEntries, "Battery level");
lineDataSets.add(batterySet);
}

lineData = new LineData(lineDataSets);

// chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
Expand Down Expand Up @@ -652,6 +663,19 @@ protected LineDataSet createHeartrateSet(List<Entry> values, String label) {
return set1;
}


protected LineDataSet createBatterySet(List<Entry> values, String label) {
LineDataSet set1 = new LineDataSet(values, label);
set1.setLineWidth(2.2f);
set1.setColor(Color.argb(255, 0, 255, 0));
set1.setMode(LineDataSet.Mode.LINEAR);
set1.setDrawCircles(false);
set1.setDrawValues(true);
set1.setValueTextColor(CHART_TEXT_COLOR);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
return set1;
}

protected RefreshTask createRefreshTask(String task, Context context) {
return new RefreshTask(task, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public void setRawIntensity(int intensity) {
public void setSteps(int steps) {
}

@Override
public int getBatteryLevel() {
return NOT_MEASURED;
}

public void setBatteryLevel(int batteryLevel) {

}

/**
* Unix timestamp of the sample, i.e. the number of seconds since 1970-01-01 00:00:00 UTC.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public interface ActivitySample extends TimeStamped {
*/
int getSteps();

/**
* Returns the battery level at the time of this sample as a percentage in range 0-100
* or ActivitySample.NOT_MEASURED if no data is available
*/
int getBatteryLevel();

/**
* Returns the heart rate measured at the corresponding timestamp.
* The value is returned in heart beats per minute, in the range from
Expand Down

0 comments on commit 1552603

Please sign in to comment.