AndroidAPS/wear/src/main/java/info/nightscout/androidaps/BIGChart.java

610 lines
24 KiB
Java
Raw Normal View History

2016-11-17 15:17:02 +01:00
package info.nightscout.androidaps;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Shader;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
2016-11-17 15:17:02 +01:00
import android.support.v4.content.LocalBroadcastManager;
import android.support.wearable.view.WatchViewStub;
2016-11-25 21:29:46 +01:00
import android.support.wearable.watchface.WatchFaceStyle;
2016-11-17 15:17:02 +01:00
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.wearable.DataMap;
2016-11-27 03:48:03 +01:00
import com.ustwo.clockwise.common.util.Logr;
2016-11-25 21:29:46 +01:00
import com.ustwo.clockwise.wearable.WatchFace;
import com.ustwo.clockwise.common.WatchFaceTime;
import com.ustwo.clockwise.common.WatchMode;
import com.ustwo.clockwise.common.WatchShape;
2016-11-17 15:17:02 +01:00
import java.util.ArrayList;
import java.util.Date;
import lecho.lib.hellocharts.view.LineChartView;
/**
2016-11-18 02:31:29 +01:00
* Created by adrianLxM.
2016-11-17 15:17:02 +01:00
*/
public class BIGChart extends WatchFace implements SharedPreferences.OnSharedPreferenceChangeListener {
public final static IntentFilter INTENT_FILTER;
2016-11-22 03:07:10 +01:00
public TextView mTime, mSgv, mTimestamp, mDelta, mAvgDelta;
2016-11-17 15:17:02 +01:00
public RelativeLayout mRelativeLayout;
public long sgvLevel = 0;
public int batteryLevel = 1;
public int ageLevel = 1;
public int highColor = Color.YELLOW;
public int lowColor = Color.RED;
public int midColor = Color.WHITE;
2016-11-27 03:48:03 +01:00
public int gridColour = Color.WHITE;
2016-11-17 15:17:02 +01:00
public int pointSize = 2;
public boolean singleLine = false;
public boolean layoutSet = false;
public BgGraphBuilder bgGraphBuilder;
public LineChartView chart;
public double datetime;
public ArrayList<BgWatchData> bgDataList = new ArrayList<>();
2016-11-18 01:01:42 +01:00
public ArrayList<TempWatchData> tempWatchDataList = new ArrayList<>();
2016-11-18 02:31:29 +01:00
public ArrayList<BasalWatchData> basalWatchDataList = new ArrayList<>();
2016-11-17 15:17:02 +01:00
public PowerManager.WakeLock wakeLock;
public View layoutView;
private final Point displaySize = new Point();
private int specW, specH;
private int animationAngle = 0;
private boolean isAnimated = false;
private LocalBroadcastManager localBroadcastManager;
private MessageReceiver messageReceiver;
protected SharedPreferences sharedPrefs;
private String rawString = "000 | 000 | 000";
private String batteryString = "--";
private String sgvString = "--";
2016-11-17 21:51:29 +01:00
private String externalStatusString = "no status";
private TextView statusView;
2016-11-26 01:35:08 +01:00
private long chartTapTime = 0l;
2016-11-17 15:17:02 +01:00
@Override
public void onCreate() {
super.onCreate();
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
display.getSize(displaySize);
wakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Clock");
specW = View.MeasureSpec.makeMeasureSpec(displaySize.x,
View.MeasureSpec.EXACTLY);
specH = View.MeasureSpec.makeMeasureSpec(displaySize.y,
View.MeasureSpec.EXACTLY);
sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.activity_bigchart, null);
performViewSetup();
}
@Override
protected void onLayout(WatchShape shape, Rect screenBounds, WindowInsets screenInsets) {
super.onLayout(shape, screenBounds, screenInsets);
layoutView.onApplyWindowInsets(screenInsets);
}
public void performViewSetup() {
final WatchViewStub stub = (WatchViewStub) layoutView.findViewById(R.id.watch_view_stub);
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
messageReceiver = new MessageReceiver();
localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(messageReceiver, messageFilter);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTime = (TextView) stub.findViewById(R.id.watch_time);
mSgv = (TextView) stub.findViewById(R.id.sgv);
mTimestamp = (TextView) stub.findViewById(R.id.timestamp);
mDelta = (TextView) stub.findViewById(R.id.delta);
2016-11-22 03:07:10 +01:00
mAvgDelta = (TextView) stub.findViewById(R.id.avgdelta);
2016-11-17 15:17:02 +01:00
mRelativeLayout = (RelativeLayout) stub.findViewById(R.id.main_layout);
chart = (LineChartView) stub.findViewById(R.id.chart);
2016-11-17 21:51:29 +01:00
statusView = (TextView) stub.findViewById(R.id.aps_status);
2016-11-17 15:17:02 +01:00
layoutSet = true;
2016-11-17 21:51:29 +01:00
showAgeAndStatus();
2016-11-17 15:17:02 +01:00
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
}
});
ListenerService.requestData(this);
wakeLock.acquire(50);
}
2016-11-25 21:29:46 +01:00
@Override
protected void onTapCommand(int tapType, int x, int y, long eventTime) {
2016-11-26 01:35:08 +01:00
if (tapType == TAP_TYPE_TAP&&
x >=chart.getLeft() &&
x <= chart.getRight()&&
y >= chart.getTop() &&
y <= chart.getBottom()){
if (eventTime - chartTapTime < 800){
changeChartTimeframe();
}
chartTapTime = eventTime;
}
}
private void changeChartTimeframe() {
int timeframe = Integer.parseInt(sharedPrefs.getString("chart_timeframe", "3"));
timeframe = (timeframe%5) + 1;
sharedPrefs.edit().putString("chart_timeframe", "" + timeframe).commit();
2016-11-25 21:29:46 +01:00
}
2016-11-27 03:48:03 +01:00
protected void onWatchModeChanged(WatchMode watchMode) {
if (! sharedPrefs.getBoolean("dark", true)){
//in bright mode: different colours if active:
setColor();
} else {
//TODO: Handle low bit ambient
}
}
2016-11-25 21:29:46 +01:00
@Override
protected WatchFaceStyle getWatchFaceStyle(){
return new WatchFaceStyle.Builder(this).setAcceptsTapEvents(true).build();
}
2016-11-17 15:17:02 +01:00
public int ageLevel() {
if(timeSince() <= (1000 * 60 * 12)) {
return 1;
} else {
return 0;
}
}
public double timeSince() {
return System.currentTimeMillis() - datetime;
}
public String readingAge(boolean shortString) {
if (datetime == 0) { return shortString?"--'":"-- Minute ago"; }
int minutesAgo = (int) Math.floor(timeSince()/(1000*60));
if (minutesAgo == 1) {
return minutesAgo + (shortString?"'":" Minute ago");
}
return minutesAgo + (shortString?"'":" Minutes ago");
}
@Override
public void onDestroy() {
if(localBroadcastManager != null && messageReceiver != null){
localBroadcastManager.unregisterReceiver(messageReceiver);}
if (sharedPrefs != null){
sharedPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
super.onDestroy();
}
static {
INTENT_FILTER = new IntentFilter();
INTENT_FILTER.addAction(Intent.ACTION_TIME_TICK);
INTENT_FILTER.addAction(Intent.ACTION_TIMEZONE_CHANGED);
INTENT_FILTER.addAction(Intent.ACTION_TIME_CHANGED);
}
@Override
protected void onDraw(Canvas canvas) {
if(layoutSet) {
this.mRelativeLayout.draw(canvas);
Log.d("onDraw", "draw");
}
}
@Override
protected void onTimeChanged(WatchFaceTime oldTime, WatchFaceTime newTime) {
if (layoutSet && (newTime.hasHourChanged(oldTime) || newTime.hasMinuteChanged(oldTime))) {
wakeLock.acquire(50);
final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(BIGChart.this);
mTime.setText(timeFormat.format(System.currentTimeMillis()));
2016-11-17 21:51:29 +01:00
showAgeAndStatus();
2016-11-17 15:17:02 +01:00
if(ageLevel()<=0) {
mSgv.setPaintFlags(mSgv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
mSgv.setPaintFlags(mSgv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
missedReadingAlert();
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
}
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getBundleExtra("data");
2016-11-17 21:51:29 +01:00
if (layoutSet && bundle !=null) {
DataMap dataMap = DataMap.fromBundle(bundle);
2016-11-17 15:17:02 +01:00
wakeLock.acquire(50);
sgvLevel = dataMap.getLong("sgvLevel");
batteryLevel = dataMap.getInt("batteryLevel");
datetime = dataMap.getDouble("timestamp");
rawString = dataMap.getString("rawString");
sgvString = dataMap.getString("sgvString");
batteryString = dataMap.getString("battery");
mSgv.setText(dataMap.getString("sgvString"));
if(ageLevel()<=0) {
mSgv.setPaintFlags(mSgv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
mSgv.setPaintFlags(mSgv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(BIGChart.this);
mTime.setText(timeFormat.format(System.currentTimeMillis()));
2016-11-17 21:51:29 +01:00
showAgeAndStatus();
2016-11-17 15:17:02 +01:00
String delta = dataMap.getString("delta");
if (delta.endsWith(" mg/dl")) {
mDelta.setText(delta.substring(0, delta.length() - 6));
2016-11-17 18:21:09 +01:00
} else if (delta.endsWith(" mmol/l")||delta.endsWith(" mmol")) {
2016-11-17 15:17:02 +01:00
mDelta.setText(delta.substring(0, delta.length() - 5));
2016-11-17 18:21:09 +01:00
} else {
mDelta.setText(delta);
2016-11-17 15:17:02 +01:00
}
2016-11-22 03:07:10 +01:00
String avgDelta = dataMap.getString("avgDelta");
if (delta.endsWith(" mg/dl")) {
mAvgDelta.setText(avgDelta.substring(0, avgDelta.length() - 6));
} else if (avgDelta.endsWith(" mmol/l")||avgDelta.endsWith(" mmol")) {
mAvgDelta.setText(avgDelta.substring(0, avgDelta.length() - 5));
} else {
mAvgDelta.setText(avgDelta);
}
2016-11-17 15:17:02 +01:00
if (chart != null) {
addToWatchSet(dataMap);
setupCharts();
}
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
invalidate();
setColor();
//start animation?
// dataMap.getDataMapArrayList("entries") == null -> not on "resend data".
if (sharedPrefs.getBoolean("animation", false) && dataMap.getDataMapArrayList("entries") == null && (sgvString.equals("100") || sgvString.equals("5.5") || sgvString.equals("5,5"))) {
startAnimation();
}
2016-11-17 21:51:29 +01:00
}
//status
bundle = intent.getBundleExtra("status");
if (layoutSet && bundle != null) {
DataMap dataMap = DataMap.fromBundle(bundle);
wakeLock.acquire(50);
externalStatusString = dataMap.getString("externalStatusString");
2016-11-17 15:17:02 +01:00
2016-11-17 21:51:29 +01:00
showAgeAndStatus();
2016-11-17 15:17:02 +01:00
2016-11-17 21:51:29 +01:00
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
invalidate();
setColor();
2016-11-17 15:17:02 +01:00
}
2016-11-18 01:01:42 +01:00
//basals and temps
bundle = intent.getBundleExtra("basals");
if (layoutSet && bundle != null) {
DataMap dataMap = DataMap.fromBundle(bundle);
wakeLock.acquire(500);
loadBasalsAndTemps(dataMap);
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
invalidate();
setColor();
}
}
}
private void loadBasalsAndTemps(DataMap dataMap) {
ArrayList<DataMap> temps = dataMap.getDataMapArrayList("temps");
if (temps != null) {
tempWatchDataList = new ArrayList<>();
for (DataMap temp : temps) {
TempWatchData twd = new TempWatchData();
twd.startTime = temp.getLong("starttime");
twd.startBasal = temp.getDouble("startBasal");
twd.endTime = temp.getLong("endtime");
twd.endBasal = temp.getDouble("endbasal");
twd.amount = temp.getDouble("amount");
tempWatchDataList.add(twd);
}
2016-11-17 15:17:02 +01:00
}
2016-11-18 02:31:29 +01:00
ArrayList<DataMap> basals = dataMap.getDataMapArrayList("basals");
if (basals != null) {
basalWatchDataList = new ArrayList<>();
for (DataMap basal : basals) {
BasalWatchData bwd = new BasalWatchData();
bwd.startTime = basal.getLong("starttime");
bwd.endTime = basal.getLong("endtime");
bwd.amount = basal.getDouble("amount");
basalWatchDataList.add(bwd);
}
}
2016-11-17 15:17:02 +01:00
}
2016-11-17 21:51:29 +01:00
private void showAgeAndStatus() {
if( mTimestamp != null){
mTimestamp.setText(readingAge(true));
}
2016-11-17 15:17:02 +01:00
2016-11-20 02:31:15 +01:00
boolean showStatus = sharedPrefs.getBoolean("showExternalStatus", true);
2016-11-22 03:07:10 +01:00
boolean showAvgDelta = sharedPrefs.getBoolean("showAvgDelta", true);
2016-11-17 21:51:29 +01:00
2016-11-22 03:07:10 +01:00
if(showAvgDelta){
mAvgDelta.setVisibility(View.VISIBLE);
} else {
mAvgDelta.setVisibility(View.GONE);
}
2016-11-17 21:51:29 +01:00
if(showStatus){
statusView.setText(externalStatusString);
statusView.setVisibility(View.VISIBLE);
} else {
statusView.setVisibility(View.GONE);
2016-11-17 15:17:02 +01:00
}
}
public void setColor() {
if (sharedPrefs.getBoolean("dark", true)) {
setColorDark();
} else {
setColorBright();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
setColor();
if(layoutSet){
2016-11-17 21:51:29 +01:00
showAgeAndStatus();
2016-11-17 15:17:02 +01:00
mRelativeLayout.measure(specW, specH);
mRelativeLayout.layout(0, 0, mRelativeLayout.getMeasuredWidth(),
mRelativeLayout.getMeasuredHeight());
}
invalidate();
}
protected void updateRainbow() {
animationAngle = (animationAngle + 1) % 360;
//Animation matrix:
int[] rainbow = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE
, Color.CYAN};
Shader shader = new LinearGradient(0, 0, 0, 20, rainbow,
null, Shader.TileMode.MIRROR);
Matrix matrix = new Matrix();
matrix.setRotate(animationAngle);
shader.setLocalMatrix(matrix);
mSgv.getPaint().setShader(shader);
invalidate();
}
private synchronized boolean isAnimated() {
return isAnimated;
}
private synchronized void setIsAnimated(boolean isAnimated) {
this.isAnimated = isAnimated;
}
void startAnimation() {
Log.d("CircleWatchface", "start startAnimation");
Thread animator = new Thread() {
public void run() {
setIsAnimated(true);
for (int i = 0; i <= 8 * 1000 / 40; i++) {
updateRainbow();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mSgv.getPaint().setShader(null);
setIsAnimated(false);
invalidate();
setColor();
System.gc();
}
};
animator.start();
}
//without theme
2016-11-17 15:17:02 +01:00
protected void setColorDark() {
mTime.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_mTime));
statusView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_statusView));
2016-11-26 07:05:12 +01:00
mRelativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_background));
2016-11-17 15:17:02 +01:00
if (sgvLevel == 1) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor));
2016-11-17 15:17:02 +01:00
} else if (sgvLevel == 0) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor));
2016-11-17 15:17:02 +01:00
} else if (sgvLevel == -1) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor));
2016-11-17 15:17:02 +01:00
}
if (ageLevel == 1) {
mTimestamp.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_mTimestamp1));
2016-11-17 15:17:02 +01:00
} else {
mTimestamp.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_mTimestamp));
2016-11-17 15:17:02 +01:00
}
if (chart != null) {
highColor = ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor);
lowColor = ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor);
midColor = ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor);
2016-11-27 03:48:03 +01:00
gridColour = ContextCompat.getColor(getApplicationContext(), R.color.dark_gridColor);
2016-11-17 15:17:02 +01:00
singleLine = false;
pointSize = 2;
setupCharts();
}
}
protected void setColorBright() {
if (getCurrentWatchMode() == WatchMode.INTERACTIVE) {
2016-11-26 07:05:12 +01:00
mTime.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_bigchart_time));
statusView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_bigchart_status));
mRelativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.light_background));
2016-11-17 15:17:02 +01:00
if (sgvLevel == 1) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_highColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_highColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_highColor));
2016-11-17 15:17:02 +01:00
} else if (sgvLevel == 0) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_midColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_midColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_midColor));
2016-11-17 15:17:02 +01:00
} else if (sgvLevel == -1) {
mSgv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_lowColor));
mDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_lowColor));
mAvgDelta.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_lowColor));
2016-11-17 15:17:02 +01:00
}
if (ageLevel == 1) {
mTimestamp.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_mTimestamp1));
2016-11-17 15:17:02 +01:00
} else {
mTimestamp.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.light_mTimestamp));
2016-11-17 15:17:02 +01:00
}
if (chart != null) {
highColor = ContextCompat.getColor(getApplicationContext(), R.color.light_highColor);
lowColor = ContextCompat.getColor(getApplicationContext(), R.color.light_lowColor);
midColor = ContextCompat.getColor(getApplicationContext(), R.color.light_midColor);
2016-11-27 03:48:03 +01:00
gridColour = ContextCompat.getColor(getApplicationContext(), R.color.light_gridColor);
2016-11-17 15:17:02 +01:00
singleLine = false;
pointSize = 2;
setupCharts();
}
} else {
2016-11-27 03:48:03 +01:00
setColorDark();
2016-11-17 15:17:02 +01:00
}
}
public void missedReadingAlert() {
int minutes_since = (int) Math.floor(timeSince()/(1000*60));
if(minutes_since >= 16 && ((minutes_since - 16) % 5) == 0) {
2016-11-18 01:01:42 +01:00
ListenerService.requestData(this); // attempt endTime recover missing data
2016-11-17 15:17:02 +01:00
}
}
public void addToWatchSet(DataMap dataMap) {
ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
if (entries != null) {
for (DataMap entry : entries) {
double sgv = entry.getDouble("sgvDouble");
double high = entry.getDouble("high");
double low = entry.getDouble("low");
double timestamp = entry.getDouble("timestamp");
final int size = bgDataList.size();
if (size > 0) {
if (bgDataList.get(size - 1).timestamp == timestamp)
continue; // Ignore duplicates.
}
bgDataList.add(new BgWatchData(sgv, high, low, timestamp));
}
} else {
double sgv = dataMap.getDouble("sgvDouble");
double high = dataMap.getDouble("high");
double low = dataMap.getDouble("low");
double timestamp = dataMap.getDouble("timestamp");
final int size = bgDataList.size();
if (size > 0) {
if (bgDataList.get(size - 1).timestamp == timestamp)
return; // Ignore duplicates.
}
bgDataList.add(new BgWatchData(sgv, high, low, timestamp));
}
for (int i = 0; i < bgDataList.size(); i++) {
if (bgDataList.get(i).timestamp < (new Date().getTime() - (1000 * 60 * 60 * 5))) {
bgDataList.remove(i); //Get rid of anything more than 5 hours old
break;
}
}
}
public void setupCharts() {
if(bgDataList.size() > 0) { //Dont crash things just because we dont have values, people dont like crashy things
2016-11-20 02:31:15 +01:00
int timeframe = Integer.parseInt(sharedPrefs.getString("chart_timeframe", "3"));
2016-11-17 15:17:02 +01:00
if (singleLine) {
2016-11-27 03:48:03 +01:00
bgGraphBuilder = new BgGraphBuilder(getApplicationContext(), bgDataList, tempWatchDataList, basalWatchDataList, pointSize, midColor, gridColour, timeframe);
2016-11-17 15:17:02 +01:00
} else {
2016-11-27 03:48:03 +01:00
bgGraphBuilder = new BgGraphBuilder(getApplicationContext(), bgDataList, tempWatchDataList, basalWatchDataList, pointSize, highColor, lowColor, midColor, gridColour, timeframe);
2016-11-17 15:17:02 +01:00
}
chart.setLineChartData(bgGraphBuilder.lineData());
chart.setViewportCalculationEnabled(true);
chart.setMaximumViewport(chart.getMaximumViewport());
} else {
ListenerService.requestData(this);
}
}
}