[#728] Post review / test related minor improvements:

- improving mockability when Android classes are used
- POJO Pair instead Java
- old BGWatchData cleanup fix
- Persistence getDataMap fix
- minor fixes and improvements to display formating
This commit is contained in:
dlvoy 2019-11-26 09:23:26 +01:00
parent 20eae8eb12
commit 1042e8aab8
8 changed files with 99 additions and 27 deletions

View file

@ -4,10 +4,10 @@ import android.app.PendingIntent;
import android.support.wearable.complications.ComplicationData;
import android.support.wearable.complications.ComplicationText;
import android.util.Log;
import android.util.Pair;
import info.nightscout.androidaps.data.DisplayRawData;
import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.Pair;
/*
* Created by dlvoy on 2019-11-12

View file

@ -4,10 +4,10 @@ import android.app.PendingIntent;
import android.support.wearable.complications.ComplicationData;
import android.support.wearable.complications.ComplicationText;
import android.util.Log;
import android.util.Pair;
import info.nightscout.androidaps.data.DisplayRawData;
import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.Pair;
/*
* Created by dlvoy on 2019-11-12

View file

@ -7,6 +7,7 @@ import android.os.PowerManager;
import com.google.android.gms.wearable.DataMap;
import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.interaction.utils.Constants;
import info.nightscout.androidaps.interaction.utils.Persistence;
@ -120,7 +121,7 @@ public class DisplayRawData {
public DataMap updateDataFromMessage(Intent intent, PowerManager.WakeLock wakeLock) {
Bundle bundle = intent.getBundleExtra("data");
if (bundle != null) {
DataMap dataMap = DataMap.fromBundle(bundle);
DataMap dataMap = WearUtil.bundleToDataMap(bundle);
updateData(dataMap);
return dataMap;
}
@ -141,7 +142,7 @@ public class DisplayRawData {
public DataMap updateStatusFromMessage(Intent intent, PowerManager.WakeLock wakeLock) {
Bundle bundle = intent.getBundleExtra("status");
if (bundle != null) {
DataMap dataMap = DataMap.fromBundle(bundle);
DataMap dataMap = WearUtil.bundleToDataMap(bundle);
updateStatus(dataMap);
return dataMap;
}
@ -168,7 +169,7 @@ public class DisplayRawData {
public DataMap updateBasalsFromMessage(Intent intent, PowerManager.WakeLock wakeLock) {
Bundle bundle = intent.getBundleExtra("basals");
if (bundle != null) {
DataMap dataMap = DataMap.fromBundle(bundle);
DataMap dataMap = WearUtil.bundleToDataMap(bundle);
updateBasals(dataMap);
return dataMap;
}
@ -259,10 +260,12 @@ public class DisplayRawData {
bgDataList.add(new BgWatchData(sgv, high, low, timestamp, color));
}
for (int i = 0; i < bgDataList.size(); i++) {
if (bgDataList.get(i).timestamp < (System.currentTimeMillis() - (Constants.HOUR_IN_MS * 5))) {
bgDataList.remove(i); //Get rid of anything more than 5 hours old
break;
// We use iterator instead for-loop because we iterate and remove on the go
Iterator itr = bgDataList.iterator();
while (itr.hasNext()) {
BgWatchData entry = (BgWatchData)itr.next();
if (entry.timestamp < (WearUtil.timestamp() - (Constants.HOUR_IN_MS * 5))) {
itr.remove(); //Get rid of anything more than 5 hours old
}
}
}

View file

@ -1,7 +1,5 @@
package info.nightscout.androidaps.interaction.utils;
import android.util.Pair;
import info.nightscout.androidaps.aaps;
import info.nightscout.androidaps.data.DisplayRawData;
@ -45,20 +43,30 @@ public class DisplayFormat {
return days + "d";
} else {
int weeks = days / 7;
return weeks + "d";
return weeks + "w";
}
}
}
public static String shortTrend(final DisplayRawData raw) {
String minutes = shortTimeSince(raw.datetime);
String delta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_SHORT_FIELD-1);
if (minutes.length() + delta.length() + 1 < MAX_SHORT_FIELD) {
delta = deltaSymbol() + delta;
String minutes = "--";
if (raw.datetime > 0) {
minutes = shortTimeSince(raw.datetime);
}
return minutes + " " + delta;
if (minutes.length() + raw.sDelta.length() + deltaSymbol().length() + 1 <= MAX_SHORT_FIELD) {
return minutes + " " + deltaSymbol() + raw.sDelta;
}
// that only optimizes obvious things like 0 before . or at end, + at beginning
String delta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_SHORT_FIELD-1);
if (minutes.length() + delta.length() + deltaSymbol().length() + 1 <= MAX_SHORT_FIELD) {
return minutes + " " + deltaSymbol() + delta;
}
String shortDelta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_SHORT_FIELD-(1+minutes.length()));
return minutes + " " + shortDelta;
}
public static String longGlucoseLine(final DisplayRawData raw) {
@ -105,13 +113,17 @@ public class DisplayFormat {
final String iob1 = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_SHORT_FIELD);
String iob2 = "";
if (raw.sIOB2.contains("|")) {
String[] iobs = raw.sIOB2.replace("(", "").replace(")", "").split("\\|");
if (iobs.length == 2) {
final String iobBolus = new SmallestDoubleString(iobs[0]).minimise(MIN_IOB_FIELD);
final String iobBasal = new SmallestDoubleString(iobs[1]).minimise((MAX_SHORT_FIELD-1) - Math.max(MIN_IOB_FIELD, iobBolus.length()));
iob2 = iobBolus+" "+iobBasal;
String iobBolus = new SmallestDoubleString(iobs[0]).minimise(MIN_IOB_FIELD);
if (iobBolus.trim().length() == 0) {
iobBolus = "--";
}
String iobBasal = new SmallestDoubleString(iobs[1]).minimise((MAX_SHORT_FIELD-1) - Math.max(MIN_IOB_FIELD, iobBolus.length()));
if (iobBasal.trim().length() == 0) {
iobBasal = "--";
}
iob2 = iobBolus+" "+iobBasal;
}
return Pair.create(iob1, iob2);
}

View file

@ -0,0 +1,43 @@
package info.nightscout.androidaps.interaction.utils;
import java.util.Objects;
/**
* Same as android Pair, but clean room java class - does not require Android SDK for tests
*/
public class Pair<F, S> {
public final F first;
public final S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public static <F, S> Pair<F, S> create(F f, S s) {
return new Pair<>(f, s);
}
@Override
public boolean equals(java.lang.Object o) {
if (o instanceof Pair) {
return ((Pair) o).first.equals(first) && ((Pair) o).second.equals(second);
} else {
return false;
}
}
@Override
public String toString() {
return "First: \""+first.toString()+"\" Second: \""+second.toString()+"\"";
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
}

View file

@ -23,7 +23,7 @@ public class Persistence {
}
public DataMap getDataMap(String key) {
if (preferences.contains("raw_data")) {
if (preferences.contains(key)) {
final String rawB64Data = preferences.getString(key, null);
byte[] rawData = Base64.decode(rawB64Data, Base64.DEFAULT);
try {

View file

@ -71,6 +71,8 @@ public class SmallestDoubleString {
}
public String minimise(int maxSize) {
final String originalSeparator = separator;
if (Integer.parseInt("0"+fractional) == 0) {
separator = "";
fractional = "";
@ -95,7 +97,7 @@ public class SmallestDoubleString {
return toString();
}
if ((fractional.length() > 0)&&(decimal.length() > 0)) {
if (fractional.length() > 0) {
int remainingForFraction = maxSize-currentLen()+fractional.length();
String formatCandidate = "#";
if (remainingForFraction>=1) {
@ -104,8 +106,10 @@ public class SmallestDoubleString {
DecimalFormat df = new DecimalFormat(formatCandidate);
df.setRoundingMode(RoundingMode.HALF_UP);
return sign + df.format(Double.parseDouble(decimal+"."+fractional)).replace(".", separator) +
final String decimalSup = (decimal.length() > 0) ? decimal : "0";
String result = sign + df.format(Double.parseDouble(decimalSup+"."+fractional)).replace(",", originalSeparator).replace(".", originalSeparator) +
((withUnits == Units.USE) ? units : "");
return (decimal.length() > 0) ? result : result.substring(1);
}
return toString();
}
@ -115,6 +119,7 @@ public class SmallestDoubleString {
((withUnits == Units.USE) ? units.length() : 0);
}
@Override
public String toString() {
return sign+decimal+separator+fractional +
((withUnits == Units.USE) ? units : "");

View file

@ -2,9 +2,12 @@ package info.nightscout.androidaps.interaction.utils;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import com.google.android.gms.wearable.DataMap;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
@ -12,7 +15,6 @@ import java.util.Map;
import java.util.Set;
import info.nightscout.androidaps.aaps;
import info.nightscout.androidaps.data.DisplayRawData;
/**
* Created by andy on 3/5/19.
@ -124,4 +126,11 @@ public class WearUtil {
}
return set;
}
/**
* Taken out to helper method to allow testing
*/
public static DataMap bundleToDataMap(Bundle bundle) {
return DataMap.fromBundle(bundle);
}
}