From 1ad6d2b228dfee395cb2e50fd2d1a1fafd909986 Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Sat, 13 Apr 2024 21:39:30 +0100 Subject: [PATCH] Added watch connection signal strength trend graph (will only work for BLE2 data source at the moment!) --- CHANGELOG.md | 1 + .../openseizuredetector/FragmentWatchSig.java | 148 ++++++++++++++++++ .../openseizuredetector/MainActivity2.java | 6 +- .../main/res/layout/fragment_watch_sig.xml | 60 +++++++ 4 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/uk/org/openseizuredetector/FragmentWatchSig.java create mode 100644 app/src/main/res/layout/fragment_watch_sig.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e8cfc4..58bff31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Improved start-up checks for permissions - Improved system re-start after changing settings (but still not perfect!) - Disabled the CNN algorithm by default as it is causing some false alarms (Issue #170) + - Added watch signal strength trend graph to main activity V4.2.5 - Set BLE device time if the characteristic is available. V4.2.4 - Added checks and a FAULT condition for Bluetooth errors in Bluetooth Data Source V4.2.3 - Uses 3d accelerometer data to calculate magnitude if vector magnitude is not sent from data source. diff --git a/app/src/main/java/uk/org/openseizuredetector/FragmentWatchSig.java b/app/src/main/java/uk/org/openseizuredetector/FragmentWatchSig.java new file mode 100644 index 0000000..dfc82ab --- /dev/null +++ b/app/src/main/java/uk/org/openseizuredetector/FragmentWatchSig.java @@ -0,0 +1,148 @@ +package uk.org.openseizuredetector; + +import android.graphics.Color; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.appcompat.widget.SwitchCompat; + +import com.github.mikephil.charting.charts.LineChart; +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 com.github.mikephil.charting.utils.ValueFormatter; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + + +public class FragmentWatchSig extends FragmentOsdBaseClass { + String TAG = "FragmentWatchSig"; + + LineChart mLineChart; + LineData lineData; + LineDataSet lineDataSet; + List sigHistory = new ArrayList<>(); + List hrHistoryStrings = new ArrayList<>(); + + private TextView tvCurrSigStren; + + public FragmentWatchSig() { + // Required empty public constructor + } + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + lineDataSet = new LineDataSet(new ArrayList(), "Watch Signal Strength history"); + //lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS); + lineDataSet.setValueTextColor(Color.BLACK); + lineDataSet.setValueTextSize(18f); + lineDataSet.setDrawValues(false); + lineDataSet.setCircleSize(0f); + lineDataSet.setLineWidth(3f); + //lineDataSetAverage = new LineDataSet(new ArrayList(),"Heart rate history" ); + //lineDataSetAverage.setColors(ColorTemplate.JOYFUL_COLORS); + //lineDataSetAverage.setValueTextColor(Color.BLACK); + //lineDataSetAverage.setValueTextSize(18f); + + } + + @Override + public void onResume() { + super.onResume(); + mLineChart = mRootView.findViewById(R.id.sigStrengthLineChart); + mLineChart.getLegend().setEnabled(false); + XAxis xAxis = mLineChart.getXAxis(); + xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); + xAxis.setTextSize(10f); + xAxis.setDrawAxisLine(true); + xAxis.setDrawLabels(true); + // Note: the default text colour is BLACK, so does not show up on black background!!! + // This took a lot of finding.... + xAxis.setTextColor(Color.WHITE); + + YAxis yAxis = mLineChart.getAxisLeft(); + //yAxis.setAxisMinValue(40f); + //yAxis.setAxisMaxValue(240f); + yAxis.setDrawGridLines(true); + yAxis.setDrawLabels(true); + yAxis.setTextColor(Color.WHITE); + // Inhibit the decimal part of the y axis labels. + yAxis.setValueFormatter(new ValueFormatter() { + @Override + public String getFormattedValue(float v) { + DecimalFormat format = new DecimalFormat("###"); + return format.format(v); + } + }); + + YAxis yAxis2 = mLineChart.getAxisRight(); + yAxis2.setDrawGridLines(false); + yAxis2.setEnabled(false); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_watch_sig, container, false); + } + + @Override + protected void updateUi() { + Log.d(TAG, "updateUi()"); + tvCurrSigStren = (TextView) mRootView.findViewById(R.id.current_sig_strength_tv); + if (mConnection.mBound) { + if (Objects.nonNull(tvCurrSigStren)) + tvCurrSigStren.setText(String.valueOf((int) mConnection.mSdServer.mSdData.watchSignalStrength)); + double histArr[] = mConnection.mSdServer.mSdData.watchSignalStrengthBuff.getVals(); + int nHist = histArr.length; + if (Objects.nonNull(histArr) && nHist > 0) { + Log.v(TAG, "nHist=" + nHist); + lineDataSet.clear(); + String xVals[] = new String[nHist]; + for (int i = 0; i < nHist; i++) { + //Log.d(TAG,"i="+i+", HR="+hrHistArr[i]); + xVals[i] = String.valueOf(i); + lineDataSet.addEntry(new Entry((float) histArr[i], i)); + } + Log.d(TAG, "xVals=" + Arrays.toString(xVals) + ", lneDataSet=" + lineDataSet.toSimpleString()); + lineDataSet.setColors(new int[]{0xffff0000}); + LineData histLineData = new LineData(xVals, lineDataSet); + + + mLineChart.setData(histLineData); + mLineChart.getData().notifyDataChanged(); + mLineChart.notifyDataSetChanged(); + mLineChart.refreshDrawableState(); + float xSpan = (nHist * 5.0f) / 60.0f; // time in minutes assuming one point every 5 seconds. + mLineChart.setDescription("Signal Strength History " + + String.format("%.1f", xSpan) + + " " + getString(R.string.minutes)); + mLineChart.setDescriptionTextSize(12f); + mLineChart.invalidate(); + //if (mConnection.mBound){ + // lineChart.postInvalidate(); + //} + } + + } else { + Log.w(TAG,"not Bound to Server"); + return; + } + + + } + +} diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java index bdc4576..8275e98 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java @@ -318,7 +318,9 @@ public class MainActivity2 extends AppCompatActivity { return new FragmentHrAlg(); case 2: return new FragmentSystem(); - //case 3: + case 3: + return new FragmentWatchSig(); + //case 4: // return new FragmentBatt(); //case 4: // return new FragmentDataSharing(); @@ -331,7 +333,7 @@ public class MainActivity2 extends AppCompatActivity { @Override public int getItemCount() { - return 3; + return 4; } } diff --git a/app/src/main/res/layout/fragment_watch_sig.xml b/app/src/main/res/layout/fragment_watch_sig.xml new file mode 100644 index 0000000..640d314 --- /dev/null +++ b/app/src/main/res/layout/fragment_watch_sig.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file