2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Cre3per,
|
2019-11-23 21:52:46 +01:00
|
|
|
Daniele Gobbetti, Dikay900, Pavel, Pavel Elagin
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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/>. */
|
2015-09-02 08:00:26 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-10-02 23:38:07 +02:00
|
|
|
import android.graphics.Paint;
|
2015-09-02 08:00:26 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2015-09-04 21:55:29 +02:00
|
|
|
import android.view.WindowManager;
|
2018-09-13 11:51:52 +02:00
|
|
|
import android.widget.TextView;
|
2015-09-02 23:49:06 +02:00
|
|
|
import android.widget.Toast;
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
import com.github.mikephil.charting.charts.BarLineChartBase;
|
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
2015-10-21 00:36:18 +02:00
|
|
|
import com.github.mikephil.charting.components.LimitLine;
|
2015-09-02 08:00:26 +02:00
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
2015-10-01 22:36:33 +02:00
|
|
|
import com.github.mikephil.charting.data.BarData;
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet;
|
|
|
|
import com.github.mikephil.charting.data.BarEntry;
|
2016-04-12 23:12:15 +02:00
|
|
|
import com.github.mikephil.charting.data.ChartData;
|
2015-10-02 00:07:19 +02:00
|
|
|
import com.github.mikephil.charting.data.Entry;
|
|
|
|
import com.github.mikephil.charting.data.LineData;
|
|
|
|
import com.github.mikephil.charting.data.LineDataSet;
|
2015-10-02 23:38:07 +02:00
|
|
|
import com.github.mikephil.charting.utils.Utils;
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-09-02 23:49:06 +02:00
|
|
|
import java.util.ArrayList;
|
2015-09-02 08:00:26 +02:00
|
|
|
import java.util.List;
|
2015-10-21 00:36:18 +02:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-09-02 08:00:26 +02:00
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.FragmentActivity;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2015-09-02 08:00:26 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2016-04-12 23:12:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
|
2015-09-02 08:00:26 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
2018-09-13 11:51:52 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2015-09-02 08:00:26 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
2015-09-02 23:49:06 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
public class LiveActivityFragment extends AbstractChartFragment {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(LiveActivityFragment.class);
|
2015-10-21 00:36:18 +02:00
|
|
|
private static final int MAX_STEPS_PER_MINUTE = 300;
|
|
|
|
private static final int MIN_STEPS_PER_MINUTE = 60;
|
|
|
|
private static final int RESET_COUNT = 10; // reset the max steps per minute value every 10s
|
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
private BarEntry totalStepsEntry;
|
|
|
|
private BarEntry stepsPerMinuteEntry;
|
|
|
|
private BarDataSet mStepsPerMinuteData;
|
|
|
|
private BarDataSet mTotalStepsData;
|
2015-10-03 10:08:48 +02:00
|
|
|
private LineDataSet mHistorySet;
|
2015-10-21 00:36:18 +02:00
|
|
|
private BarLineChartBase mStepsPerMinuteHistoryChart;
|
|
|
|
private CustomBarChart mStepsPerMinuteCurrentChart;
|
|
|
|
private CustomBarChart mTotalStepsChart;
|
2018-09-13 11:51:52 +02:00
|
|
|
private TextView mMaxHeartRateView;
|
2015-10-21 00:36:18 +02:00
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Steps mSteps = new Steps();
|
2015-10-21 00:36:18 +02:00
|
|
|
private ScheduledExecutorService pulseScheduler;
|
|
|
|
private int maxStepsResetCounter;
|
2016-04-12 23:12:15 +02:00
|
|
|
private LineDataSet mHeartRateSet;
|
|
|
|
private int mHeartRate;
|
2018-09-13 11:51:52 +02:00
|
|
|
private int mMaxHeartRate = 0;
|
2016-10-08 11:16:40 +02:00
|
|
|
private TimestampTranslation tsTranslation;
|
2015-09-02 23:49:06 +02:00
|
|
|
|
|
|
|
private class Steps {
|
|
|
|
private int steps;
|
2016-10-08 11:16:40 +02:00
|
|
|
private int lastTimestamp;
|
2015-09-02 23:49:06 +02:00
|
|
|
private int currentStepsPerMinute;
|
2015-09-03 23:20:47 +02:00
|
|
|
private int maxStepsPerMinute;
|
2015-10-21 00:36:18 +02:00
|
|
|
private int lastStepsPerMinute;
|
2015-09-02 23:49:06 +02:00
|
|
|
|
2015-10-21 00:36:18 +02:00
|
|
|
public int getStepsPerMinute(boolean reset) {
|
|
|
|
lastStepsPerMinute = currentStepsPerMinute;
|
|
|
|
int result = currentStepsPerMinute;
|
|
|
|
if (reset) {
|
|
|
|
currentStepsPerMinute = 0;
|
|
|
|
}
|
|
|
|
return result;
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getTotalSteps() {
|
2016-12-26 11:33:01 +01:00
|
|
|
return steps;
|
2015-09-03 23:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxStepsPerMinute() {
|
|
|
|
return maxStepsPerMinute;
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 01:38:20 +01:00
|
|
|
public void updateCurrentSteps(int stepsDelta, int timestamp) {
|
2015-09-02 23:49:06 +02:00
|
|
|
try {
|
|
|
|
if (steps == 0) {
|
2016-12-26 01:38:20 +01:00
|
|
|
steps += stepsDelta;
|
2015-09-02 23:49:06 +02:00
|
|
|
lastTimestamp = timestamp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-26 01:38:20 +01:00
|
|
|
int timeDelta = timestamp - lastTimestamp;
|
|
|
|
currentStepsPerMinute = calculateStepsPerMinute(stepsDelta, timeDelta);
|
|
|
|
if (currentStepsPerMinute > maxStepsPerMinute) {
|
|
|
|
maxStepsPerMinute = currentStepsPerMinute;
|
|
|
|
maxStepsResetCounter = 0;
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
2016-12-26 01:38:20 +01:00
|
|
|
steps += stepsDelta;
|
|
|
|
lastTimestamp = timestamp;
|
2015-09-02 23:49:06 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
GB.toast(LiveActivityFragment.this.getContext(), ex.getMessage(), Toast.LENGTH_SHORT, GB.ERROR, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private int calculateStepsPerMinute(int stepsDelta, int seconds) {
|
2015-09-02 23:49:06 +02:00
|
|
|
if (stepsDelta == 0) {
|
|
|
|
return 0; // not walking or not enough data per mills?
|
|
|
|
}
|
2016-10-08 11:16:40 +02:00
|
|
|
if (seconds <= 0) {
|
|
|
|
throw new IllegalArgumentException("delta in seconds is <= 0 -- time change?");
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:23:02 +01:00
|
|
|
int oneMinute = 60;
|
2016-10-08 11:16:40 +02:00
|
|
|
float factor = oneMinute / seconds;
|
2015-10-21 00:36:18 +02:00
|
|
|
int result = (int) (stepsDelta * factor);
|
|
|
|
if (result > MAX_STEPS_PER_MINUTE) {
|
|
|
|
// ignore, return previous value instead
|
|
|
|
result = lastStepsPerMinute;
|
|
|
|
}
|
|
|
|
return result;
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
2015-09-02 08:00:26 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
switch (action) {
|
2016-12-26 00:23:02 +01:00
|
|
|
case DeviceService.ACTION_REALTIME_SAMPLES: {
|
|
|
|
ActivitySample sample = (ActivitySample) intent.getSerializableExtra(DeviceService.EXTRA_REALTIME_SAMPLE);
|
|
|
|
addSample(sample);
|
2016-04-12 23:12:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-26 00:23:02 +01:00
|
|
|
private void addSample(ActivitySample sample) {
|
|
|
|
int heartRate = sample.getHeartRate();
|
|
|
|
int timestamp = tsTranslation.shorten(sample.getTimestamp());
|
2018-08-16 13:13:12 +02:00
|
|
|
if (HeartRateUtils.getInstance().isValidHeartRateValue(heartRate)) {
|
2016-12-26 00:23:02 +01:00
|
|
|
setCurrentHeartRate(heartRate, timestamp);
|
|
|
|
}
|
|
|
|
int steps = sample.getSteps();
|
2018-09-11 23:08:29 +02:00
|
|
|
if (steps > 0) {
|
2016-12-26 11:33:01 +01:00
|
|
|
addEntries(steps, timestamp);
|
|
|
|
}
|
2016-12-26 00:23:02 +01:00
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private int translateTimestampFrom(Intent intent) {
|
|
|
|
return translateTimestamp(intent.getLongExtra(DeviceService.EXTRA_TIMESTAMP, System.currentTimeMillis()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private int translateTimestamp(long tsMillis) {
|
|
|
|
int timestamp = (int) (tsMillis / 1000); // translate to seconds
|
|
|
|
return tsTranslation.shorten(timestamp); // and shorten
|
2016-10-03 21:06:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private void setCurrentHeartRate(int heartRate, int timestamp) {
|
2016-04-12 23:12:15 +02:00
|
|
|
addHistoryDataSet(true);
|
|
|
|
mHeartRate = heartRate;
|
2018-09-13 11:51:52 +02:00
|
|
|
if (mMaxHeartRate < mHeartRate) {
|
|
|
|
mMaxHeartRate = mHeartRate;
|
|
|
|
}
|
|
|
|
mMaxHeartRateView.setText(getContext().getString(R.string.live_activity_max_heart_rate, heartRate, mMaxHeartRate));
|
2016-04-12 23:12:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getCurrentHeartRate() {
|
|
|
|
int result = mHeartRate;
|
|
|
|
mHeartRate = -1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private void addEntries(int steps, int timestamp) {
|
2015-09-02 23:49:06 +02:00
|
|
|
mSteps.updateCurrentSteps(steps, timestamp);
|
2015-10-21 00:36:18 +02:00
|
|
|
if (++maxStepsResetCounter > RESET_COUNT) {
|
|
|
|
maxStepsResetCounter = 0;
|
|
|
|
mSteps.maxStepsPerMinute = 0;
|
|
|
|
}
|
2015-09-04 21:55:29 +02:00
|
|
|
// Or: count down the steps until goal reached? And then flash GOAL REACHED -> Set stretch goal
|
2015-10-21 00:36:18 +02:00
|
|
|
LOG.info("Steps: " + steps + ", total: " + mSteps.getTotalSteps() + ", current: " + mSteps.getStepsPerMinute(false));
|
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
// addEntries();
|
2015-10-21 00:36:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private void addEntries(int timestamp) {
|
2015-10-01 22:36:33 +02:00
|
|
|
mTotalStepsChart.setSingleEntryYValue(mSteps.getTotalSteps());
|
2015-10-21 00:36:18 +02:00
|
|
|
YAxis stepsPerMinuteCurrentYAxis = mStepsPerMinuteCurrentChart.getAxisLeft();
|
|
|
|
int maxStepsPerMinute = mSteps.getMaxStepsPerMinute();
|
|
|
|
// int extraRoom = maxStepsPerMinute/5;
|
|
|
|
// buggy in MPAndroidChart? Disable.
|
|
|
|
// stepsPerMinuteCurrentYAxis.setAxisMaxValue(Math.max(MIN_STEPS_PER_MINUTE, maxStepsPerMinute + extraRoom));
|
|
|
|
LimitLine target = new LimitLine(maxStepsPerMinute);
|
|
|
|
stepsPerMinuteCurrentYAxis.removeAllLimitLines();
|
|
|
|
stepsPerMinuteCurrentYAxis.addLimitLine(target);
|
|
|
|
|
|
|
|
int stepsPerMinute = mSteps.getStepsPerMinute(true);
|
|
|
|
mStepsPerMinuteCurrentChart.setSingleEntryYValue(stepsPerMinute);
|
2015-10-02 00:07:19 +02:00
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
if (!addHistoryDataSet(false)) {
|
|
|
|
return;
|
2015-10-02 23:38:07 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
ChartData data = mStepsPerMinuteHistoryChart.getData();
|
|
|
|
if (stepsPerMinute < 0) {
|
|
|
|
stepsPerMinute = 0;
|
|
|
|
}
|
2016-10-03 21:06:18 +02:00
|
|
|
mHistorySet.addEntry(new Entry(timestamp, stepsPerMinute));
|
2016-04-12 23:12:15 +02:00
|
|
|
int hr = getCurrentHeartRate();
|
2018-09-13 11:51:52 +02:00
|
|
|
if (hr > HeartRateUtils.getInstance().getMinHeartRate()) {
|
|
|
|
mHeartRateSet.addEntry(new Entry(timestamp, hr));
|
2016-04-12 23:12:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
private boolean addHistoryDataSet(boolean force) {
|
|
|
|
if (mStepsPerMinuteHistoryChart.getData() == null) {
|
|
|
|
// ignore the first default value to keep the "no-data-description" visible
|
|
|
|
if (force || mSteps.getTotalSteps() > 0) {
|
|
|
|
LineData data = new LineData();
|
|
|
|
data.addDataSet(mHistorySet);
|
|
|
|
data.addDataSet(mHeartRateSet);
|
|
|
|
mStepsPerMinuteHistoryChart.setData(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
IntentFilter filterLocal = new IntentFilter();
|
2016-12-26 00:23:02 +01:00
|
|
|
filterLocal.addAction(DeviceService.ACTION_REALTIME_SAMPLES);
|
2016-10-08 11:16:40 +02:00
|
|
|
tsTranslation = new TimestampTranslation();
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_live_activity, container, false);
|
|
|
|
|
2018-09-17 22:11:15 +02:00
|
|
|
mStepsPerMinuteCurrentChart = rootView.findViewById(R.id.livechart_steps_per_minute_current);
|
|
|
|
mTotalStepsChart = rootView.findViewById(R.id.livechart_steps_total);
|
|
|
|
mStepsPerMinuteHistoryChart = rootView.findViewById(R.id.livechart_steps_per_minute_history);
|
2015-09-02 23:49:06 +02:00
|
|
|
|
2016-10-03 21:06:18 +02:00
|
|
|
totalStepsEntry = new BarEntry(1, 0);
|
|
|
|
stepsPerMinuteEntry = new BarEntry(1, 0);
|
2015-09-02 08:00:26 +02:00
|
|
|
|
2015-10-02 23:38:07 +02:00
|
|
|
mStepsPerMinuteData = setupCurrentChart(mStepsPerMinuteCurrentChart, stepsPerMinuteEntry, getString(R.string.live_activity_current_steps_per_minute));
|
2018-09-13 11:51:52 +02:00
|
|
|
mStepsPerMinuteData.setDrawValues(true);
|
|
|
|
mStepsPerMinuteData.setValueTextColor(DESCRIPTION_COLOR);
|
2015-10-02 23:38:07 +02:00
|
|
|
mTotalStepsData = setupTotalStepsChart(mTotalStepsChart, totalStepsEntry, getString(R.string.live_activity_total_steps));
|
2018-09-13 11:51:52 +02:00
|
|
|
mTotalStepsData.setDrawValues(true);
|
|
|
|
mTotalStepsData.setValueTextColor(DESCRIPTION_COLOR);
|
2015-10-02 00:07:19 +02:00
|
|
|
setupHistoryChart(mStepsPerMinuteHistoryChart);
|
2018-09-13 11:51:52 +02:00
|
|
|
mMaxHeartRateView = rootView.findViewById(R.id.livechart_max_heart_rate);
|
2015-10-02 00:07:19 +02:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filterLocal);
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2015-10-21 00:36:18 +02:00
|
|
|
@Override
|
|
|
|
public void onPause() {
|
2017-03-20 22:58:45 +01:00
|
|
|
enableRealtimeTracking(false);
|
2015-10-21 00:36:18 +02:00
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2017-03-20 22:58:45 +01:00
|
|
|
enableRealtimeTracking(true);
|
2015-10-21 00:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private ScheduledExecutorService startActivityPulse() {
|
|
|
|
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
service.scheduleAtFixedRate(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
FragmentActivity activity = LiveActivityFragment.this.getActivity();
|
|
|
|
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
|
|
|
activity.runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
pulse();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 0, getPulseIntervalMillis(), TimeUnit.MILLISECONDS);
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
2016-04-13 23:36:14 +02:00
|
|
|
private void stopActivityPulse() {
|
|
|
|
if (pulseScheduler != null) {
|
|
|
|
pulseScheduler.shutdownNow();
|
|
|
|
pulseScheduler = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 00:36:18 +02:00
|
|
|
/**
|
|
|
|
* Called in the UI thread.
|
|
|
|
*/
|
|
|
|
private void pulse() {
|
2016-10-08 11:16:40 +02:00
|
|
|
addEntries(translateTimestamp(System.currentTimeMillis()));
|
2016-04-12 23:12:15 +02:00
|
|
|
|
|
|
|
LineData historyData = (LineData) mStepsPerMinuteHistoryChart.getData();
|
|
|
|
if (historyData == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
historyData.notifyDataChanged();
|
|
|
|
mTotalStepsData.notifyDataSetChanged();
|
|
|
|
mStepsPerMinuteData.notifyDataSetChanged();
|
|
|
|
mStepsPerMinuteHistoryChart.notifyDataSetChanged();
|
|
|
|
|
|
|
|
renderCharts();
|
|
|
|
|
2019-10-03 11:53:54 +02:00
|
|
|
// have to enable it again and again to keep it measuring
|
2016-04-12 23:12:15 +02:00
|
|
|
GBApplication.deviceService().onEnableRealtimeHeartRateMeasurement(true);
|
2015-10-21 00:36:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 11:16:40 +02:00
|
|
|
private int getPulseIntervalMillis() {
|
2015-10-21 00:36:18 +02:00
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
|
2015-09-02 08:00:26 +02:00
|
|
|
@Override
|
2015-09-04 21:55:29 +02:00
|
|
|
protected void onMadeVisibleInActivity() {
|
|
|
|
super.onMadeVisibleInActivity();
|
2017-03-20 22:58:45 +01:00
|
|
|
enableRealtimeTracking(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableRealtimeTracking(boolean enable) {
|
|
|
|
if (enable && pulseScheduler != null) {
|
|
|
|
// already running
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GBApplication.deviceService().onEnableRealtimeSteps(enable);
|
|
|
|
GBApplication.deviceService().onEnableRealtimeHeartRateMeasurement(enable);
|
|
|
|
if (enable) {
|
|
|
|
if (getActivity() != null) {
|
|
|
|
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
}
|
|
|
|
pulseScheduler = startActivityPulse();
|
|
|
|
} else {
|
|
|
|
stopActivityPulse();
|
|
|
|
if (getActivity() != null) {
|
|
|
|
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
}
|
2015-09-04 21:55:29 +02:00
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-04 21:55:29 +02:00
|
|
|
protected void onMadeInvisibleInActivity() {
|
2017-03-20 22:58:45 +01:00
|
|
|
enableRealtimeTracking(false);
|
2015-09-04 21:55:29 +02:00
|
|
|
super.onMadeInvisibleInActivity();
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2016-05-15 22:24:37 +02:00
|
|
|
onMadeInvisibleInActivity();
|
2015-09-02 08:00:26 +02:00
|
|
|
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
private BarDataSet setupCurrentChart(CustomBarChart chart, BarEntry entry, String title) {
|
2018-09-13 11:51:52 +02:00
|
|
|
mStepsPerMinuteCurrentChart.getAxisLeft().setAxisMaximum(MAX_STEPS_PER_MINUTE);
|
2015-09-04 21:55:29 +02:00
|
|
|
return setupCommonChart(chart, entry, title);
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
private BarDataSet setupCommonChart(CustomBarChart chart, BarEntry entry, String title) {
|
|
|
|
chart.setSinglAnimationEntry(entry);
|
|
|
|
|
2015-10-02 23:38:07 +02:00
|
|
|
// chart.getXAxis().setPosition(XAxis.XAxisPosition.TOP);
|
|
|
|
chart.getXAxis().setDrawLabels(false);
|
|
|
|
chart.getXAxis().setEnabled(false);
|
2016-12-26 12:51:20 +01:00
|
|
|
chart.getXAxis().setTextColor(CHART_TEXT_COLOR);
|
|
|
|
chart.getAxisLeft().setTextColor(CHART_TEXT_COLOR);
|
|
|
|
|
2015-09-02 08:00:26 +02:00
|
|
|
chart.setBackgroundColor(BACKGROUND_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
chart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
|
|
|
chart.getDescription().setText(title);
|
|
|
|
// chart.setNoDataTextDescription("");
|
2015-09-02 08:00:26 +02:00
|
|
|
chart.setNoDataText("");
|
2015-09-04 21:55:29 +02:00
|
|
|
chart.getAxisRight().setEnabled(false);
|
2015-09-02 23:49:06 +02:00
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
List<BarEntry> entries = new ArrayList<>();
|
2015-09-02 23:49:06 +02:00
|
|
|
List<Integer> colors = new ArrayList<>();
|
|
|
|
|
|
|
|
entries.add(entry);
|
2015-10-01 22:36:33 +02:00
|
|
|
colors.add(akActivity.color);
|
|
|
|
colors.add(akActivity.color);
|
2015-09-02 23:49:06 +02:00
|
|
|
colors.add(akActivity.color);
|
2016-09-18 23:37:41 +02:00
|
|
|
// //we don't want labels
|
|
|
|
// xLabels.add("");
|
|
|
|
// xLabels.add("");
|
|
|
|
// xLabels.add("");
|
2015-09-02 23:49:06 +02:00
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
BarDataSet set = new BarDataSet(entries, "");
|
2015-10-02 23:38:07 +02:00
|
|
|
set.setDrawValues(false);
|
2015-09-02 23:49:06 +02:00
|
|
|
set.setColors(colors);
|
2016-09-18 23:37:41 +02:00
|
|
|
BarData data = new BarData(set);
|
|
|
|
// data.setGroupSpace(0);
|
2015-09-02 23:49:06 +02:00
|
|
|
chart.setData(data);
|
|
|
|
|
|
|
|
chart.getLegend().setEnabled(false);
|
|
|
|
|
2015-09-04 21:55:29 +02:00
|
|
|
return set;
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
private BarDataSet setupTotalStepsChart(CustomBarChart chart, BarEntry entry, String label) {
|
2018-09-16 20:49:00 +02:00
|
|
|
mTotalStepsChart.getAxisLeft().addLimitLine(new LimitLine(GBApplication.getPrefs().getInt(ActivityUser.PREF_USER_STEPS_GOAL, ActivityUser.defaultUserStepsGoal), "ss")); // TODO: use daily goal - already reached steps
|
2018-09-13 11:51:52 +02:00
|
|
|
mTotalStepsChart.getAxisLeft().setAxisMinimum(0);
|
|
|
|
mTotalStepsChart.setAutoScaleMinMaxEnabled(true);
|
2015-09-04 21:55:29 +02:00
|
|
|
return setupCommonChart(chart, entry, label); // at the moment, these look the same
|
2015-09-02 23:49:06 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 08:00:26 +02:00
|
|
|
private void setupHistoryChart(BarLineChartBase chart) {
|
2015-10-02 00:07:19 +02:00
|
|
|
configureBarLineChartDefaults(chart);
|
|
|
|
|
2016-04-12 23:12:15 +02:00
|
|
|
chart.setTouchEnabled(false); // no zooming or anything, because it's updated all the time
|
2015-09-02 08:00:26 +02:00
|
|
|
chart.setBackgroundColor(BACKGROUND_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
chart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
|
|
|
chart.getDescription().setText(getString(R.string.live_activity_steps_per_minute_history));
|
2015-10-02 23:38:07 +02:00
|
|
|
chart.setNoDataText(getString(R.string.live_activity_start_your_activity));
|
|
|
|
chart.getLegend().setEnabled(false);
|
|
|
|
Paint infoPaint = chart.getPaint(Chart.PAINT_INFO);
|
|
|
|
infoPaint.setTextSize(Utils.convertDpToPixel(20f));
|
|
|
|
infoPaint.setFakeBoldText(true);
|
|
|
|
chart.setPaint(infoPaint, Chart.PAINT_INFO);
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
XAxis x = chart.getXAxis();
|
|
|
|
x.setDrawLabels(true);
|
|
|
|
x.setDrawGridLines(false);
|
|
|
|
x.setEnabled(true);
|
|
|
|
x.setTextColor(CHART_TEXT_COLOR);
|
2018-09-13 11:51:52 +02:00
|
|
|
x.setValueFormatter(new SampleXLabelFormatter(tsTranslation));
|
2015-09-02 08:00:26 +02:00
|
|
|
x.setDrawLimitLinesBehindData(true);
|
|
|
|
|
|
|
|
YAxis y = chart.getAxisLeft();
|
|
|
|
y.setDrawGridLines(false);
|
|
|
|
y.setDrawTopYLabelEntry(false);
|
|
|
|
y.setTextColor(CHART_TEXT_COLOR);
|
|
|
|
y.setEnabled(true);
|
2016-10-03 21:06:18 +02:00
|
|
|
y.setAxisMinimum(0);
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
YAxis yAxisRight = chart.getAxisRight();
|
|
|
|
yAxisRight.setDrawGridLines(false);
|
2016-04-12 23:12:15 +02:00
|
|
|
yAxisRight.setEnabled(true);
|
|
|
|
yAxisRight.setDrawLabels(true);
|
2015-09-02 08:00:26 +02:00
|
|
|
yAxisRight.setDrawTopYLabelEntry(false);
|
|
|
|
yAxisRight.setTextColor(CHART_TEXT_COLOR);
|
2015-10-02 00:07:19 +02:00
|
|
|
|
2015-10-03 10:08:48 +02:00
|
|
|
mHistorySet = new LineDataSet(new ArrayList<Entry>(), getString(R.string.live_activity_steps_history));
|
2016-04-12 23:12:15 +02:00
|
|
|
mHistorySet.setAxisDependency(YAxis.AxisDependency.LEFT);
|
2015-10-02 23:38:07 +02:00
|
|
|
mHistorySet.setColor(akActivity.color);
|
|
|
|
mHistorySet.setDrawCircles(false);
|
2016-09-18 23:37:41 +02:00
|
|
|
mHistorySet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
|
2015-10-02 23:38:07 +02:00
|
|
|
mHistorySet.setDrawFilled(true);
|
|
|
|
mHistorySet.setDrawValues(false);
|
2016-04-12 23:12:15 +02:00
|
|
|
|
2016-04-13 21:21:10 +02:00
|
|
|
mHeartRateSet = createHeartrateSet(new ArrayList<Entry>(), getString(R.string.live_activity_heart_rate));
|
2016-04-12 23:12:15 +02:00
|
|
|
mHeartRateSet.setDrawValues(false);
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
|
|
|
return getContext().getString(R.string.liveactivity_live_activity);
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:55:29 +02:00
|
|
|
@Override
|
|
|
|
protected void showDateBar(boolean show) {
|
|
|
|
// never show the data bar
|
|
|
|
super.showDateBar(false);
|
|
|
|
}
|
|
|
|
|
2015-09-03 23:20:47 +02:00
|
|
|
@Override
|
|
|
|
protected void refresh() {
|
|
|
|
// do nothing, we don't have any db interaction
|
|
|
|
}
|
2015-09-04 21:55:29 +02:00
|
|
|
|
2015-09-02 08:00:26 +02:00
|
|
|
@Override
|
2016-04-11 23:18:12 +02:00
|
|
|
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void updateChartsnUIThread(ChartsData chartsData) {
|
|
|
|
throw new UnsupportedOperationException();
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void renderCharts() {
|
2015-09-04 21:55:29 +02:00
|
|
|
mStepsPerMinuteCurrentChart.animateY(150);
|
2015-10-01 22:36:33 +02:00
|
|
|
mTotalStepsChart.animateY(150);
|
2015-10-02 00:07:19 +02:00
|
|
|
mStepsPerMinuteHistoryChart.invalidate();
|
2015-09-02 08:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
|
|
|
throw new UnsupportedOperationException("no db access supported for live activity");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setupLegend(Chart chart) {
|
|
|
|
// no legend
|
|
|
|
}
|
|
|
|
}
|