diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index 4c44f9ac2e..9d5c19bdf3 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.data; +import android.support.v4.util.LongSparseArray; + import com.crashlytics.android.Crashlytics; import org.json.JSONArray; @@ -30,8 +32,11 @@ public class Profile { double dia = Constants.defaultDIA; TimeZone timeZone = TimeZone.getDefault(); JSONArray isf; + private LongSparseArray isf_v = null; // oldest at index 0 JSONArray ic; + private LongSparseArray ic_v = null; // oldest at index 0 JSONArray basal; + private LongSparseArray basal_v = null; // oldest at index 0 JSONArray targetLow; JSONArray targetHigh; @@ -136,6 +141,21 @@ public class Profile { return timeZone; } + private LongSparseArray convertToSparseArray(JSONArray array) { + LongSparseArray sparse = new LongSparseArray<>(); + for (Integer index = 0; index < array.length(); index++) { + try { + JSONObject o = array.getJSONObject(index); + long tas = o.getLong("timeAsSeconds"); + Double value = o.getDouble("value"); + sparse.put(tas, value); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return sparse; + } + private Double getValueToTime(JSONArray array, Integer timeAsSeconds) { Double lastValue = null; @@ -156,6 +176,21 @@ public class Profile { return lastValue; } + private Double getValueToTime(LongSparseArray array, long timeAsSeconds) { + Double lastValue = null; + + for (Integer index = 0; index < array.size(); index++) { + long tas = array.keyAt(index); + double value = array.valueAt(index); + if (lastValue == null) lastValue = value; + if (timeAsSeconds < tas) { + break; + } + lastValue = value; + } + return lastValue; + } + private String getValuesList(JSONArray array, JSONArray array2, DecimalFormat format, String units) { String retValue = ""; @@ -188,7 +223,9 @@ public class Profile { } public Double getIsf(Integer timeAsSeconds) { - return getValueToTime(isf, timeAsSeconds); + if (isf_v == null) + isf_v = convertToSparseArray(isf); + return getValueToTime(isf_v, timeAsSeconds); } public String getIsfList() { @@ -204,7 +241,9 @@ public class Profile { } public Double getIc(Integer timeAsSeconds) { - return getValueToTime(ic, timeAsSeconds); + if (ic_v == null) + ic_v = convertToSparseArray(ic); + return getValueToTime(ic_v, timeAsSeconds); } public String getIcList() { @@ -220,7 +259,9 @@ public class Profile { } public Double getBasal(Integer timeAsSeconds) { - return getValueToTime(basal, timeAsSeconds); + if (basal_v == null) + basal_v = convertToSparseArray(basal); + return getValueToTime(basal_v, timeAsSeconds); } public String getBasalList() { diff --git a/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java index d422a94211..05c5360e22 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java @@ -180,7 +180,7 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface { int realDuration = getDurationToTime(time); if (realDuration > 0) { - Double dia_ago = time - profile.getDia() * 60 * 60 * 1000; + Double dia_ago = time - dia * 60 * 60 * 1000; int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d); double spacing = realDuration / aboutFiveMinIntervals; @@ -191,11 +191,11 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface { if (calcdate > dia_ago && calcdate <= time) { double tempBolusSize = absoluteRate() * spacing / 60d; - Treatment tempBolusPart = new Treatment(insulinInterface); + Treatment tempBolusPart = new Treatment(insulinInterface, dia); tempBolusPart.insulin = tempBolusSize; tempBolusPart.date = calcdate; - Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia()); + Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia); result.iob += aIOB.iobContrib; result.activity += aIOB.activityContrib; result.extendedBolusInsulin += tempBolusPart.insulin; diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java index 4bf668411d..b1798b380b 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java @@ -179,8 +179,8 @@ public class TemporaryBasal implements Interval { if (realDuration > 0) { Double netBasalRate = 0d; - - Double dia_ago = time - profile.getDia() * 60 * 60 * 1000; + double dia = profile.getDia(); + Double dia_ago = time - dia * 60 * 60 * 1000; int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d); double tempBolusSpacing = realDuration / aboutFiveMinIntervals; @@ -202,11 +202,11 @@ public class TemporaryBasal implements Interval { double tempBolusSize = netBasalRate * tempBolusSpacing / 60d; netBasalAmount += tempBolusSize; - Treatment tempBolusPart = new Treatment(insulinInterface); + Treatment tempBolusPart = new Treatment(insulinInterface, dia); tempBolusPart.insulin = tempBolusSize; tempBolusPart.date = calcdate; - Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia()); + Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia); result.basaliob += aIOB.iobContrib; result.activity += aIOB.activityContrib; result.netbasalinsulin += tempBolusPart.insulin; diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index e386a195b8..5a73fa0d18 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -65,6 +65,11 @@ public class Treatment implements DataPointWithLabelInterface { dia = insulin.getDia(); } + public Treatment(InsulinInterface insulin, double dia) { + insulinInterfaceID = insulin.getId(); + this.dia = dia; + } + public long getMillisecondsFromStart() { return new Date().getTime() - date; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java index 2f98e45272..2d4795a3ec 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java @@ -38,7 +38,7 @@ public class ActivityGraph extends GraphView { double dia = insulin.getDia(); int hours = (int) Math.floor(dia + 1); - Treatment t = new Treatment(insulin); + Treatment t = new Treatment(insulin, dia); t.date = 0; t.insulin = 1d; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java index 4b3e059b6e..df88174b79 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java @@ -189,11 +189,11 @@ public class NSClientInternalPlugin implements PluginBase { private void updateLog() { try { - String newTextLog = ""; + StringBuilder newTextLog = new StringBuilder(); for (EventNSClientNewLog log : listLog) { - newTextLog = newTextLog + log.toPreparedHtml(); + newTextLog.append(log.toPreparedHtml()); } - textLog = Html.fromHtml(newTextLog); + textLog = Html.fromHtml(newTextLog.toString()); MainApp.bus().post(new EventNSClientUpdateGUI()); } catch (OutOfMemoryError e) { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java index 3af3333d65..274a7ad828 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java @@ -22,14 +22,15 @@ public class EventNSClientNewLog { this.logText = logText; } - public Spanned toHtml() { + public StringBuilder toPreparedHtml() { + StringBuilder stringBuilder = new StringBuilder(); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); - Spanned line = Html.fromHtml(timeFormat.format(date) + " " + action + " " + logText + "
"); - return line; - } - - public String toPreparedHtml() { - SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); - return timeFormat.format(date) + " " + action + " " + logText + "
"; + stringBuilder.append(timeFormat.format(date)); + stringBuilder.append(" "); + stringBuilder.append(action); + stringBuilder.append(" "); + stringBuilder.append(logText); + stringBuilder.append("
"); + return stringBuilder; } }