1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-29 03:55:49 +01:00

Add intensity to Sleep charts

This commit is contained in:
vanous 2020-11-06 14:28:51 +01:00
parent 79939b7339
commit 9c90452f92
6 changed files with 127 additions and 32 deletions

View File

@ -59,6 +59,8 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
rootView = inflater.inflate(R.layout.fragment_steps_list, container, false);
ListView stepsList = rootView.findViewById(R.id.itemListView);
View headerView = inflater.inflate(R.layout.heartrate_average_widget,null, false);
stepsList.addHeaderView(headerView);
stepListAdapter = new ActivityListingAdapter(getContext());
stepsList.setAdapter(stepListAdapter);
stepsDateView = rootView.findViewById(R.id.stepsDateView);

View File

@ -76,8 +76,12 @@ public class SleepChartFragment extends AbstractChartFragment {
private TextView mSleepchartInfo;
private TextView heartRateAverageLabel;
private ImageView heartRateIcon;
private TextView intensityTotalLabel;
private ImageView intensityTotalIcon;
private int heartRateMin = 0;
private int heartRateMax = 0;
private float intensityTotal = 0;
private int mSmartAlarmFrom = -1;
@ -115,9 +119,8 @@ public class SleepChartFragment extends AbstractChartFragment {
}
DefaultChartsData chartsData = refresh(device, samples);
Triple<Float, Integer, Integer> hrData = calculateHrData(samples);
//Pair<Float, Float> intensityMinMax = calculateIntensityMinMax(samples); //so far unused
Pair<Float, Float> intensityMinMax = Pair.create(0f, 0f);
return new MyChartsData(mySleepChartsData, chartsData, hrData.getLeft(), hrData.getMiddle(), hrData.getRight(), intensityMinMax.first, intensityMinMax.second);
Triple<Float, Float, Float> intensityData = calculateIntensityData(samples);
return new MyChartsData(mySleepChartsData, chartsData, hrData.getLeft(), hrData.getMiddle(), hrData.getRight(), intensityData.getLeft(), intensityData.getMiddle(), intensityData.getRight());
}
@ -199,6 +202,7 @@ public class SleepChartFragment extends AbstractChartFragment {
mSleepchartInfo.setText(buildYouSleptText(pieData));
heartRateMin = mcd.getHeartRateAxisMin();
heartRateMax = mcd.getHeartRateAxisMax();
intensityTotal = mcd.getIntensityTotal();
if (!CHARTS_SLEEP_RANGE_24H
&& supportsHeartrate(getChartsHost().getDevice())
@ -212,8 +216,9 @@ public class SleepChartFragment extends AbstractChartFragment {
hrAverage_line.setLineWidth(0.1f);
mActivityChart.getAxisRight().removeAllLimitLines();
mActivityChart.getAxisRight().addLimitLine(hrAverage_line);
DecimalFormat df = new DecimalFormat("##.#");
DecimalFormat df = new DecimalFormat("###.#");
heartRateAverageLabel.setText(df.format(mcd.getHeartRateAverage()));
intensityTotalLabel.setText(df.format(mcd.getIntensityTotal()));
}
}
@ -239,12 +244,20 @@ public class SleepChartFragment extends AbstractChartFragment {
int min = Collections.min(heartRateValues);
int max = Collections.max(heartRateValues);
int count = heartRateValues.toArray().length;
float sum = calculateSum(heartRateValues);
float sum = calculateHRSum(heartRateValues);
float average = sum / count;
return Triple.of(average, min, max);
}
private float calculateSum(List<Integer> samples) {
private float calculateIntensitySum(List<Float> samples) {
float result = 0;
for (Float sample : samples) {
result += sample;
}
return result;
}
private float calculateHRSum(List<Integer> samples) {
float result = 0;
for (Integer sample : samples) {
result += sample;
@ -252,19 +265,28 @@ public class SleepChartFragment extends AbstractChartFragment {
return result;
}
private Pair<Float, Float> calculateIntensityMinMax(List<? extends ActivitySample> samples) {
private Triple<Float, Float, Float> calculateIntensityData(List<? extends ActivitySample> samples) {
if (samples.toArray().length < 1) {
return Triple.of(0f, 0f, 0f);
}
List<Float> allIntensities = new ArrayList<>();
for (ActivitySample s : samples) {
if (s.getKind() == ActivityKind.TYPE_LIGHT_SLEEP || s.getKind() == ActivityKind.TYPE_DEEP_SLEEP) {
float HR = s.getIntensity();
allIntensities.add(HR);
float intensity = s.getIntensity();
allIntensities.add(intensity);
}
}
if (allIntensities.toArray().length < 1) {
return Triple.of(0f, 0f, 0f);
}
Float min = Collections.min(allIntensities);
Float max = Collections.max(allIntensities);
return Pair.create(min, max);
Float sum = calculateIntensitySum(allIntensities);
return Triple.of(sum, min, max);
}
private String buildYouSleptText(MySleepChartsData pieData) {
@ -298,15 +320,26 @@ public class SleepChartFragment extends AbstractChartFragment {
mSleepchartInfo = rootView.findViewById(R.id.sleepchart_info);
heartRateIcon = rootView.findViewById(R.id.heartrate_widget_icon);
heartRateAverageLabel = rootView.findViewById(R.id.heartrate_widget_label);
intensityTotalIcon = rootView.findViewById(R.id.intensity_widget_icon);
intensityTotalLabel = rootView.findViewById(R.id.intensity_widget_label);
ConstraintLayout intensityTotalWidgetLayout = rootView.findViewById(R.id.intensity_widget_layout);
ConstraintLayout heartRateWidgetLayout = rootView.findViewById(R.id.heartrate_widget_layout);
heartRateWidgetLayout.setOnClickListener(new View.OnClickListener() {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String detailedDuration = String.format(getString(R.string.charts_min_max_heartrate_popup), heartRateMin, heartRateMax);
DecimalFormat df = new DecimalFormat("###.#");
String detailedDuration = String.format(getString(R.string.charts_min_max_heartrate_popup), heartRateMin, heartRateMax, df.format(intensityTotal));
new ChartsActivity.ShowDurationDialog(detailedDuration, getContext()).show();
}
});
};
heartRateWidgetLayout.setOnClickListener(listener);
intensityTotalWidgetLayout.setOnClickListener(listener);
intensityTotalIcon.setOnClickListener(listener);
intensityTotalLabel.setOnClickListener(listener);
setupActivityChart();
setupSleepAmountChart();
@ -452,13 +485,17 @@ public class SleepChartFragment extends AbstractChartFragment {
private int heartRateAxisMin;
private float intensityAxisMax;
private float intensityAxisMin;
private float intensityTotal;
public MyChartsData(MySleepChartsData pieData, DefaultChartsData<LineData> chartsData, float heartRateAverage, int heartRateAxisMin, int heartRateAxisMax, float intensityAxisMin, float intensityAxisMax) {
public MyChartsData(MySleepChartsData pieData, DefaultChartsData<LineData> chartsData, float heartRateAverage, int heartRateAxisMin, int heartRateAxisMax, float intensityTotal, float intensityAxisMin, float intensityAxisMax) {
this.pieData = pieData;
this.chartsData = chartsData;
this.heartRateAverage = heartRateAverage;
this.heartRateAxisMax = heartRateAxisMax;
this.heartRateAxisMin = heartRateAxisMin;
this.intensityTotal = intensityTotal;
this.intensityAxisMin = intensityAxisMin;
this.intensityAxisMax = intensityAxisMax;
}
public MySleepChartsData getPieData() {
@ -486,5 +523,8 @@ public class SleepChartFragment extends AbstractChartFragment {
public float getIntensityAxisMin() {
return intensityAxisMin;
}
public float getIntensityTotal() { return intensityTotal;}
}
}

View File

@ -5,15 +5,6 @@
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity$PlaceholderFragment">
<include
layout="@layout/heartrate_average_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_gravity="top|end"
android:layout_marginTop="-5dp"
android:layout_marginEnd="-2dp" />
<LinearLayout
android:layout_width="match_parent"
@ -53,4 +44,24 @@
<!--android:layout_weight="20" />-->
</LinearLayout>
<include
layout="@layout/heartrate_average_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_gravity="top|end"
android:layout_marginTop="25dp"
android:layout_marginStart="170dp" />
<include
layout="@layout/intensity_total_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_gravity="top|end"
android:layout_marginTop="75dp"
android:layout_marginStart="171dp" />
</RelativeLayout>

View File

@ -5,14 +5,6 @@
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity$PlaceholderFragment">
<include
layout="@layout/heartrate_average_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_gravity="top|end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -38,4 +30,21 @@
</LinearLayout>
<include
layout="@layout/heartrate_average_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_gravity="top|end" />
<include
layout="@layout/intensity_total_widget"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:layout_marginEnd="-1dp"
android:layout_gravity="top|end" />
</RelativeLayout>

View File

@ -0,0 +1,32 @@
<?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:id="@+id/intensity_widget_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/intensity_widget_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/movement_intensity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_activity_unknown" />
<TextView
android:id="@+id/intensity_widget_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="3dp"
android:paddingEnd="3dp"
android:text=""
android:textAlignment="center"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -477,6 +477,7 @@
<string name="distance">Distance</string>
<string name="clock">Clock</string>
<string name="heart_rate">Heart rate</string>
<string name="movement_intensity">Movement intensity</string>
<string name="battery">Battery</string>
<string name="no_limit">No limit</string>
<string name="seconds_5">5 seconds</string>
@ -509,7 +510,7 @@
<string name="abstract_chart_fragment_kind_not_worn">Not worn</string>
<string name="you_slept">You slept from %1$s to %2$s</string>
<string name="you_did_not_sleep">You did not sleep</string>
<string name="charts_min_max_heartrate_popup">Lowest heart rate: %1$d \nHighest heart rate: %2$d</string>
<string name="charts_min_max_heartrate_popup">Lowest heart rate: %1$d \nHighest heart rate: %2$d \nMovement intensity: %3$s</string>
<string name="device_not_connected">Not connected.</string>
<string name="user_feedback_all_alarms_disabled">All alarms disabled</string>
<string name="pref_title_keep_data_on_device">Keep activity data on device</string>