From f7b556350cf962ed3af6e16f9bb02dfeb61430ca Mon Sep 17 00:00:00 2001 From: dlvoy Date: Wed, 27 Nov 2019 20:32:42 +0100 Subject: [PATCH] [#2210][#728] Post-review refactoring (thanks @jotomo !) --- .../BaseComplicationProviderService.java | 40 +- .../complications/BrCobIobComplication.java | 14 +- .../CobDetailedComplication.java | 4 +- .../complications/CobIconComplication.java | 4 +- .../complications/CobIobComplication.java | 8 +- .../IobDetailedComplication.java | 4 +- .../complications/IobIconComplication.java | 8 +- .../complications/LongStatusComplication.java | 4 +- .../LongStatusFlippedComplication.java | 5 +- .../complications/SgvComplication.java | 4 +- .../complications/UploaderBattery.java | 4 +- .../complications/WallpaperComplication.java | 5 +- .../androidaps/data/ListenerService.java | 6 +- ...isplayRawData.java => RawDisplayData.java} | 544 +++++++++--------- .../interaction/utils/DisplayFormat.java | 56 +- .../interaction/utils/Inevitable.java | 11 +- .../interaction/utils/Persistence.java | 8 +- .../interaction/utils/WearUtil.java | 14 +- .../androidaps/watchfaces/BaseWatchFace.java | 4 +- .../androidaps/watchfaces/BgGraphBuilder.java | 7 +- .../androidaps/data/BgWatchDataTest.java | 2 +- ...st.java => RawDataSgvDisplayDataTest.java} | 24 +- ...est.java => RawDisplayDataBasalsTest.java} | 24 +- ....java => RawDisplayDataBgEntriesTest.java} | 8 +- ...est.java => RawDisplayDataStatusTest.java} | 26 +- .../interaction/utils/DisplayFormatTest.java | 6 +- .../interaction/utils/PersistenceTest.java | 8 +- .../interaction/utils/WearUtilTest.java | 12 +- .../testing/mockers/AAPSMocker.java | 31 +- .../testing/mockers/AndroidMocker.java | 30 +- .../testing/mockers/RawDataMocker.java | 22 +- .../testing/mockers/WearUtilMocker.java | 26 +- 32 files changed, 477 insertions(+), 496 deletions(-) rename wear/src/main/java/info/nightscout/androidaps/data/{DisplayRawData.java => RawDisplayData.java} (96%) rename wear/src/test/java/info/nightscout/androidaps/data/{DisplayRawDataSgvDataTest.java => RawDataSgvDisplayDataTest.java} (86%) rename wear/src/test/java/info/nightscout/androidaps/data/{DisplayRawDataBasalsTest.java => RawDisplayDataBasalsTest.java} (93%) rename wear/src/test/java/info/nightscout/androidaps/data/{DisplayRawDataBgEntriesTest.java => RawDisplayDataBgEntriesTest.java} (96%) rename wear/src/test/java/info/nightscout/androidaps/data/{DisplayRawDataStatusTest.java => RawDisplayDataStatusTest.java} (88%) diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/BaseComplicationProviderService.java b/wear/src/main/java/info/nightscout/androidaps/complications/BaseComplicationProviderService.java index c1f33e41c5..49f8f82837 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/BaseComplicationProviderService.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/BaseComplicationProviderService.java @@ -20,7 +20,7 @@ import java.util.Set; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import info.nightscout.androidaps.R; import info.nightscout.androidaps.aaps; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.ListenerService; import info.nightscout.androidaps.interaction.utils.Constants; import info.nightscout.androidaps.interaction.utils.DisplayFormat; @@ -38,7 +38,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid private static final String TAG = BaseComplicationProviderService.class.getSimpleName(); private static final String KEY_COMPLICATIONS = "complications"; - private static final String KEY_LAST_SINCE = "lastSince"; + private static final String KEY_LAST_SHOWN_SINCE_VALUE = "lastSince"; private static final String KEY_STALE_REPORTED = "staleReported"; private static final String TASK_ID_REFRESH_COMPLICATION = "refresh-complication"; @@ -56,7 +56,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid // ABSTRACT COMPLICATION INTERFACE //============================================================================================== - public abstract ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent); + public abstract ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent); public abstract String getProviderCanonicalName(); public ComplicationAction getComplicationAction() { return ComplicationAction.MENU; }; @@ -66,11 +66,11 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid //---------------------------------------------------------------------------------------------- public ComplicationData buildNoSyncComplicationData(int dataType, - DisplayRawData raw, + RawDisplayData raw, PendingIntent complicationPendingIntent, PendingIntent exceptionalPendingIntent, long since) { - ComplicationData complicationData = null; + final ComplicationData.Builder builder = new ComplicationData.Builder(dataType); if (dataType != ComplicationData.TYPE_LARGE_IMAGE) { @@ -111,16 +111,14 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid } builder.setTapAction(exceptionalPendingIntent); - complicationData = builder.build(); - return complicationData; + return builder.build(); } public ComplicationData buildOutdatedComplicationData(int dataType, - DisplayRawData raw, + RawDisplayData raw, PendingIntent complicationPendingIntent, PendingIntent exceptionalPendingIntent, long since) { - ComplicationData complicationData = null; final ComplicationData.Builder builder = new ComplicationData.Builder(dataType); if (dataType != ComplicationData.TYPE_LARGE_IMAGE) { @@ -162,8 +160,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid } builder.setTapAction(exceptionalPendingIntent); - complicationData = builder.build(); - return complicationData; + return builder.build(); } /** @@ -230,12 +227,12 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid final Persistence persistence = new Persistence(); - final DisplayRawData raw = new DisplayRawData(); - raw.partialUpdateFromPersistence(persistence); + final RawDisplayData raw = new RawDisplayData(); + raw.updateForComplicationsFromPersistence(persistence); Log.d(TAG, "Complication data: " + raw.toDebugString()); - // store what is currently rendered since field, to detect it need update - persistence.putString(KEY_LAST_SINCE, DisplayFormat.shortTimeSince(raw.datetime)); + // store what is currently rendered in 'SGV since' field, to detect if it was changed and need update + persistence.putString(KEY_LAST_SHOWN_SINCE_VALUE, DisplayFormat.shortTimeSince(raw.datetime)); // by each render we clear stale flag to ensure it is re-rendered at next refresh detection round persistence.putBoolean(KEY_STALE_REPORTED, false); @@ -259,7 +256,6 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid if (complicationData != null) { complicationManager.updateComplicationData(complicationId, complicationData); - } else { // If no data is sent, we still need to inform the ComplicationManager, so the update // job can finish and the wake lock isn't held any longer than necessary. @@ -297,7 +293,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid Log.d(TAG, "Pending check if update needed - "+p.getString(KEY_COMPLICATIONS, "")); Inevitable.task(TASK_ID_REFRESH_COMPLICATION, 15 * Constants.SECOND_IN_MS, () -> { - if (WearUtil.rateLimit("complication-checkIfUpdateNeeded", 5)) { + if (WearUtil.isBelowRateLimit("complication-checkIfUpdateNeeded", 5)) { Log.d(TAG, "Checking if update needed"); requestUpdateIfSinceChanged(); // We reschedule need for check - to make sure next check will Inevitable go in next 15s @@ -314,10 +310,10 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid private static void requestUpdateIfSinceChanged() { final Persistence persistence = new Persistence(); - final DisplayRawData raw = new DisplayRawData(); - raw.partialUpdateFromPersistence(persistence); + final RawDisplayData raw = new RawDisplayData(); + raw.updateForComplicationsFromPersistence(persistence); - final String lastSince = persistence.getString(KEY_LAST_SINCE, "-"); + final String lastSince = persistence.getString(KEY_LAST_SHOWN_SINCE_VALUE, "-"); final String calcSince = DisplayFormat.shortTimeSince(raw.datetime); final boolean isStale = (WearUtil.msSince(persistence.whenDataUpdated()) > Constants.STALE_MS) ||(WearUtil.msSince(raw.datetime) > Constants.STALE_MS); @@ -326,7 +322,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid final boolean sinceWasChanged = !lastSince.equals(calcSince); if (sinceWasChanged|| (isStale && !staleWasRefreshed)) { - persistence.putString(KEY_LAST_SINCE, calcSince); + persistence.putString(KEY_LAST_SHOWN_SINCE_VALUE, calcSince); persistence.putBoolean(KEY_STALE_REPORTED, isStale); Log.d(TAG, "Detected refresh of time needed! Reason: " @@ -351,7 +347,7 @@ public abstract class BaseComplicationProviderService extends ComplicationProvid Log.d(TAG, "Pending update of "+provider); // We wait with updating allowing all request, from various sources, to arrive Inevitable.task("update-req-"+provider, 700, () -> { - if (WearUtil.rateLimit("update-req-"+provider, 2)) { + if (WearUtil.isBelowRateLimit("update-req-"+provider, 2)) { Log.d(TAG, "Requesting update of "+provider); final ComponentName componentName = new ComponentName(aaps.getAppContext(), provider); final ProviderUpdateRequester providerUpdateRequester = new ProviderUpdateRequester(aaps.getAppContext(), componentName); diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/BrCobIobComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/BrCobIobComplication.java index e9f05c5f79..ec862f4f47 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/BrCobIobComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/BrCobIobComplication.java @@ -5,13 +5,13 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; -import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_SHORT_FIELD; -import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_COB_FIELD; -import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_IOB_FIELD; +import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT; +import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_FIELD_LEN_COB; +import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_FIELD_LEN_IOB; /* * Created by dlvoy on 2019-11-12 @@ -20,13 +20,13 @@ public class BrCobIobComplication extends BaseComplicationProviderService { private static final String TAG = BrCobIobComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; if (dataType == ComplicationData.TYPE_SHORT_TEXT) { - final String cob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(MIN_COB_FIELD); - final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_IOB_FIELD, (MAX_SHORT_FIELD-1) - cob.length())); + final String cob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(MIN_FIELD_LEN_COB); + final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_FIELD_LEN_IOB, (MAX_FIELD_LEN_SHORT -1) - cob.length())); final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) .setShortText(ComplicationText.plainText(DisplayFormat.basalRateSymbol()+raw.sBasalRate)) diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/CobDetailedComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/CobDetailedComplication.java index ea35b6f45f..350ec9b0ec 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/CobDetailedComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/CobDetailedComplication.java @@ -5,7 +5,7 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; import info.nightscout.androidaps.interaction.utils.Pair; @@ -16,7 +16,7 @@ public class CobDetailedComplication extends BaseComplicationProviderService { private static final String TAG = CobDetailedComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/CobIconComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/CobIconComplication.java index 40824fd313..e42f46e3a4 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/CobIconComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/CobIconComplication.java @@ -7,7 +7,7 @@ import android.support.wearable.complications.ComplicationText; import android.util.Log; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; /* * Created by dlvoy on 2019-11-12 @@ -16,7 +16,7 @@ public class CobIconComplication extends BaseComplicationProviderService { private static final String TAG = CobIconComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/CobIobComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/CobIobComplication.java index 1f7dff7ceb..31ea4dc5f4 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/CobIobComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/CobIobComplication.java @@ -5,10 +5,10 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; -import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_SHORT_FIELD; +import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT; /* * Created by dlvoy on 2019-11-12 @@ -17,13 +17,13 @@ public class CobIobComplication extends BaseComplicationProviderService { private static final String TAG = CobIobComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; if (dataType == ComplicationData.TYPE_SHORT_TEXT) { final String cob = raw.sCOB2; - final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_SHORT_FIELD); + final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_FIELD_LEN_SHORT); final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) .setShortText(ComplicationText.plainText(cob)) diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/IobDetailedComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/IobDetailedComplication.java index 790d63aa67..db1d67a66c 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/IobDetailedComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/IobDetailedComplication.java @@ -5,7 +5,7 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; import info.nightscout.androidaps.interaction.utils.Pair; @@ -16,7 +16,7 @@ public class IobDetailedComplication extends BaseComplicationProviderService { private static final String TAG = IobDetailedComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/IobIconComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/IobIconComplication.java index ba253a048d..1dca0f2e4d 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/IobIconComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/IobIconComplication.java @@ -7,10 +7,10 @@ import android.support.wearable.complications.ComplicationText; import android.util.Log; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; -import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_SHORT_FIELD; +import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT; /* * Created by dlvoy on 2019-11-12 @@ -19,12 +19,12 @@ public class IobIconComplication extends BaseComplicationProviderService { private static final String TAG = IobIconComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; if (dataType == ComplicationData.TYPE_SHORT_TEXT) { - final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_SHORT_FIELD); + final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_FIELD_LEN_SHORT); final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) .setShortText(ComplicationText.plainText(iob)) diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusComplication.java index 834f40b0a7..f18ad21662 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusComplication.java @@ -5,7 +5,7 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; /* @@ -15,7 +15,7 @@ public class LongStatusComplication extends BaseComplicationProviderService { private static final String TAG = LongStatusComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusFlippedComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusFlippedComplication.java index da3384d358..7c0b730c76 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusFlippedComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/LongStatusFlippedComplication.java @@ -5,9 +5,8 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; -import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; /* * Created by dlvoy on 2019-11-12 @@ -16,7 +15,7 @@ public class LongStatusFlippedComplication extends BaseComplicationProviderServi private static final String TAG = LongStatusFlippedComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/SgvComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/SgvComplication.java index b6f9e94946..0296f8bab6 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/SgvComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/SgvComplication.java @@ -5,7 +5,7 @@ import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationText; import android.util.Log; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.DisplayFormat; /* @@ -15,7 +15,7 @@ public class SgvComplication extends BaseComplicationProviderService { private static final String TAG = SgvComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBattery.java b/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBattery.java index 4b268ef9c7..a5fcedacdd 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBattery.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/UploaderBattery.java @@ -8,7 +8,7 @@ import android.util.Log; import androidx.annotation.DrawableRes; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; /* * Created by dlvoy on 2019-11-12 @@ -17,7 +17,7 @@ public class UploaderBattery extends BaseComplicationProviderService { private static final String TAG = UploaderBattery.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/WallpaperComplication.java b/wear/src/main/java/info/nightscout/androidaps/complications/WallpaperComplication.java index 2448bebee0..293b1331ca 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/WallpaperComplication.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/WallpaperComplication.java @@ -15,7 +15,7 @@ import java.io.IOException; import java.io.InputStream; import info.nightscout.androidaps.aaps; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; /* * Created by dlvoy on 2019-11-12 @@ -26,7 +26,7 @@ public abstract class WallpaperComplication extends BaseComplicationProviderServ private static final String TAG = WallpaperComplication.class.getSimpleName(); - public ComplicationData buildComplicationData(int dataType, DisplayRawData raw, PendingIntent complicationPendingIntent) { + public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) { ComplicationData complicationData = null; @@ -48,7 +48,6 @@ public abstract class WallpaperComplication extends BaseComplicationProviderServ builder.setLargeImage(Icon.createWithBitmap(scaled)); } catch (IOException e) { Log.e(TAG, "Cannot read wallpaper asset: "+e.getMessage(), e); - e.printStackTrace(); } complicationData = builder.build(); diff --git a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java index 7bd43a79e1..9e018516ba 100644 --- a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java +++ b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java @@ -513,14 +513,14 @@ public class ListenerService extends WearableListenerService implements GoogleAp Intent messageIntent = new Intent(); messageIntent.setAction(Intent.ACTION_SEND); messageIntent.putExtra("status", dataMap.toBundle()); - Persistence.storeDataMap(DisplayRawData.STATUS_PERSISTENCE_KEY, dataMap); + Persistence.storeDataMap(RawDisplayData.STATUS_PERSISTENCE_KEY, dataMap); LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); } else if (path.equals(BASAL_DATA_PATH)){ dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap(); Intent messageIntent = new Intent(); messageIntent.setAction(Intent.ACTION_SEND); messageIntent.putExtra("basals", dataMap.toBundle()); - Persistence.storeDataMap(DisplayRawData.BASALS_PERSISTENCE_KEY, dataMap); + Persistence.storeDataMap(RawDisplayData.BASALS_PERSISTENCE_KEY, dataMap); LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); } else if (path.equals(NEW_PREFERENCES_PATH)){ dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap(); @@ -544,7 +544,7 @@ public class ListenerService extends WearableListenerService implements GoogleAp Intent messageIntent = new Intent(); messageIntent.setAction(Intent.ACTION_SEND); messageIntent.putExtra("data", dataMap.toBundle()); - Persistence.storeDataMap(DisplayRawData.DATA_PERSISTENCE_KEY, dataMap); + Persistence.storeDataMap(RawDisplayData.DATA_PERSISTENCE_KEY, dataMap); LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); } } diff --git a/wear/src/main/java/info/nightscout/androidaps/data/DisplayRawData.java b/wear/src/main/java/info/nightscout/androidaps/data/RawDisplayData.java similarity index 96% rename from wear/src/main/java/info/nightscout/androidaps/data/DisplayRawData.java rename to wear/src/main/java/info/nightscout/androidaps/data/RawDisplayData.java index 97183cd689..d4e3580561 100644 --- a/wear/src/main/java/info/nightscout/androidaps/data/DisplayRawData.java +++ b/wear/src/main/java/info/nightscout/androidaps/data/RawDisplayData.java @@ -1,272 +1,272 @@ -package info.nightscout.androidaps.data; - -import android.content.Intent; -import android.os.Bundle; -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; -import info.nightscout.androidaps.interaction.utils.WearUtil; - -/** - * Holds bunch of data model variables and lists that arrive from phone app and are due to be - * displayed on watchface and complications. Keeping them together makes code cleaner and allows - * passing it to complications via persistence layer. - * - * Created by dlvoy on 2019-11-12 - */ -public class DisplayRawData { - - static final String DATA_PERSISTENCE_KEY = "raw_data"; - static final String BASALS_PERSISTENCE_KEY = "raw_basals"; - static final String STATUS_PERSISTENCE_KEY = "raw_status"; - - // data bundle - public long sgvLevel = 0; - public long datetime; - public String sSgv = "---"; - public String sDirection = "--"; - public String sDelta = "--"; - public String sAvgDelta = "--"; - public String sUnits = "-"; - - // status bundle - public String sBasalRate = "-.--U/h"; - public String sUploaderBattery = "--"; - public String sRigBattery = "--"; - public boolean detailedIOB = false; - public String sIOB1 = "IOB"; - public String sIOB2 = "-.--"; - public String sCOB1 = "Carb"; - public String sCOB2= "--g"; - public String sBgi = "--"; - public boolean showBGI = false; - public String externalStatusString = "no status"; - public int batteryLevel = 1; - public long openApsStatus = -1; - - // basals bundle - public ArrayList bgDataList = new ArrayList<>(); - public ArrayList tempWatchDataList = new ArrayList<>(); - public ArrayList basalWatchDataList = new ArrayList<>(); - public ArrayList bolusWatchDataList = new ArrayList<>(); - public ArrayList predictionList = new ArrayList<>(); - - public String toDebugString() { - return "DisplayRawData{" + - "sgvLevel=" + sgvLevel + - ", datetime=" + datetime + - ", sSgv='" + sSgv + '\'' + - ", sDirection='" + sDirection + '\'' + - ", sDelta='" + sDelta + '\'' + - ", sAvgDelta='" + sAvgDelta + '\'' + - ", sUnits='" + sUnits + '\'' + - ", sBasalRate='" + sBasalRate + '\'' + - ", sUploaderBattery='" + sUploaderBattery + '\'' + - ", sRigBattery='" + sRigBattery + '\'' + - ", detailedIOB=" + detailedIOB + - ", sIOB1='" + sIOB1 + '\'' + - ", sIOB2='" + sIOB2 + '\'' + - ", sCOB1='" + sCOB1 + '\'' + - ", sCOB2='" + sCOB2 + '\'' + - ", sBgi='" + sBgi + '\'' + - ", showBGI=" + showBGI + - ", externalStatusString='" + externalStatusString + '\'' + - ", batteryLevel=" + batteryLevel + - ", openApsStatus=" + openApsStatus + - ", bgDataList size=" + bgDataList.size() + - ", tempWatchDataList size=" + tempWatchDataList.size() + - ", basalWatchDataList size=" + basalWatchDataList.size() + - ", bolusWatchDataLis size=" + bolusWatchDataList.size() + - ", predictionList size=" + predictionList.size() + - '}'; - } - - public void updateFromPersistence(Persistence persistence) { - - DataMap dataMapData = persistence.getDataMap(DATA_PERSISTENCE_KEY); - if (dataMapData != null) { - updateData(dataMapData); - } - DataMap dataMapStatus = persistence.getDataMap(STATUS_PERSISTENCE_KEY); - if (dataMapStatus != null) { - updateStatus(dataMapStatus); - } - DataMap dataMapBasals = persistence.getDataMap(BASALS_PERSISTENCE_KEY); - if (dataMapBasals != null) { - updateBasals(dataMapBasals); - } - } - - /* - * Since complications do not need Basals, we skip them for performance - */ - public void partialUpdateFromPersistence(Persistence persistence) { - - DataMap dataMapData = persistence.getDataMap(DATA_PERSISTENCE_KEY); - if (dataMapData != null) { - updateData(dataMapData); - } - DataMap dataMapStatus = persistence.getDataMap(STATUS_PERSISTENCE_KEY); - if (dataMapStatus != null) { - updateStatus(dataMapStatus); - } - } - - public DataMap updateDataFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { - Bundle bundle = intent.getBundleExtra("data"); - if (bundle != null) { - DataMap dataMap = WearUtil.bundleToDataMap(bundle); - updateData(dataMap); - return dataMap; - } - return null; - } - - private void updateData(DataMap dataMap) { - WearUtil.getWakeLock("readingPrefs", 50); - sgvLevel = dataMap.getLong("sgvLevel"); - datetime = dataMap.getLong("timestamp"); - sSgv = dataMap.getString("sgvString"); - sDirection = dataMap.getString("slopeArrow"); - sDelta = dataMap.getString("delta"); - sAvgDelta = dataMap.getString("avgDelta"); - sUnits = dataMap.getString("glucoseUnits"); - } - - public DataMap updateStatusFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { - Bundle bundle = intent.getBundleExtra("status"); - if (bundle != null) { - DataMap dataMap = WearUtil.bundleToDataMap(bundle); - updateStatus(dataMap); - return dataMap; - } - return null; - } - - private void updateStatus(DataMap dataMap) { - WearUtil.getWakeLock("readingPrefs", 50); - sBasalRate = dataMap.getString("currentBasal"); - sUploaderBattery = dataMap.getString("battery"); - sRigBattery = dataMap.getString("rigBattery"); - detailedIOB = dataMap.getBoolean("detailedIob"); - sIOB1 = dataMap.getString("iobSum") + "U"; - sIOB2 = dataMap.getString("iobDetail"); - sCOB1 = "Carb"; - sCOB2 = dataMap.getString("cob"); - sBgi = dataMap.getString("bgi"); - showBGI = dataMap.getBoolean("showBgi"); - externalStatusString = dataMap.getString("externalStatusString"); - batteryLevel = dataMap.getInt("batteryLevel"); - openApsStatus = dataMap.getLong("openApsStatus"); - } - - public DataMap updateBasalsFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { - Bundle bundle = intent.getBundleExtra("basals"); - if (bundle != null) { - DataMap dataMap = WearUtil.bundleToDataMap(bundle); - updateBasals(dataMap); - return dataMap; - } - return null; - } - - private void updateBasals(DataMap dataMap) { - WearUtil.getWakeLock("readingPrefs", 500); - loadBasalsAndTemps(dataMap); - } - - private void loadBasalsAndTemps(DataMap dataMap) { - ArrayList 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); - } - } - ArrayList 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); - } - } - ArrayList boluses = dataMap.getDataMapArrayList("boluses"); - if (boluses != null) { - bolusWatchDataList = new ArrayList<>(); - for (DataMap bolus : boluses) { - BolusWatchData bwd = new BolusWatchData(); - bwd.date = bolus.getLong("date"); - bwd.bolus = bolus.getDouble("bolus"); - bwd.carbs = bolus.getDouble("carbs"); - bwd.isSMB = bolus.getBoolean("isSMB"); - bwd.isValid = bolus.getBoolean("isValid"); - bolusWatchDataList.add(bwd); - } - } - ArrayList predictions = dataMap.getDataMapArrayList("predictions"); - if (boluses != null) { - predictionList = new ArrayList<>(); - for (DataMap prediction : predictions) { - BgWatchData bwd = new BgWatchData(); - bwd.timestamp = prediction.getLong("timestamp"); - bwd.sgv = prediction.getDouble("sgv"); - bwd.color = prediction.getInt("color"); - predictionList.add(bwd); - } - } - } - - public void addToWatchSet(DataMap dataMap) { - ArrayList entries = dataMap.getDataMapArrayList("entries"); - if (entries != null) { - bgDataList = new ArrayList<>(); - for (DataMap entry : entries) { - double sgv = entry.getDouble("sgvDouble"); - double high = entry.getDouble("high"); - double low = entry.getDouble("low"); - long timestamp = entry.getLong("timestamp"); - int color = entry.getInt("color", 0); - bgDataList.add(new BgWatchData(sgv, high, low, timestamp, color)); - } - } else { - double sgv = dataMap.getDouble("sgvDouble"); - double high = dataMap.getDouble("high"); - double low = dataMap.getDouble("low"); - long timestamp = dataMap.getLong("timestamp"); - int color = dataMap.getInt("color", 0); - - 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, color)); - } - - // 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 - } - } - } -} +package info.nightscout.androidaps.data; + +import android.content.Intent; +import android.os.Bundle; +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; +import info.nightscout.androidaps.interaction.utils.WearUtil; + +/** + * Holds bunch of data model variables and lists that arrive from phone app and are due to be + * displayed on watchface and complications. Keeping them together makes code cleaner and allows + * passing it to complications via persistence layer. + * + * Created by dlvoy on 2019-11-12 + */ +public class RawDisplayData { + + static final String DATA_PERSISTENCE_KEY = "raw_data"; + static final String BASALS_PERSISTENCE_KEY = "raw_basals"; + static final String STATUS_PERSISTENCE_KEY = "raw_status"; + + // data bundle + public long sgvLevel = 0; + public long datetime; + public String sSgv = "---"; + public String sDirection = "--"; + public String sDelta = "--"; + public String sAvgDelta = "--"; + public String sUnits = "-"; + + // status bundle + public String sBasalRate = "-.--U/h"; + public String sUploaderBattery = "--"; + public String sRigBattery = "--"; + public boolean detailedIOB = false; + public String sIOB1 = "IOB"; + public String sIOB2 = "-.--"; + public String sCOB1 = "Carb"; + public String sCOB2= "--g"; + public String sBgi = "--"; + public boolean showBGI = false; + public String externalStatusString = "no status"; + public int batteryLevel = 1; + public long openApsStatus = -1; + + // basals bundle + public ArrayList bgDataList = new ArrayList<>(); + public ArrayList tempWatchDataList = new ArrayList<>(); + public ArrayList basalWatchDataList = new ArrayList<>(); + public ArrayList bolusWatchDataList = new ArrayList<>(); + public ArrayList predictionList = new ArrayList<>(); + + public String toDebugString() { + return "DisplayRawData{" + + "sgvLevel=" + sgvLevel + + ", datetime=" + datetime + + ", sSgv='" + sSgv + '\'' + + ", sDirection='" + sDirection + '\'' + + ", sDelta='" + sDelta + '\'' + + ", sAvgDelta='" + sAvgDelta + '\'' + + ", sUnits='" + sUnits + '\'' + + ", sBasalRate='" + sBasalRate + '\'' + + ", sUploaderBattery='" + sUploaderBattery + '\'' + + ", sRigBattery='" + sRigBattery + '\'' + + ", detailedIOB=" + detailedIOB + + ", sIOB1='" + sIOB1 + '\'' + + ", sIOB2='" + sIOB2 + '\'' + + ", sCOB1='" + sCOB1 + '\'' + + ", sCOB2='" + sCOB2 + '\'' + + ", sBgi='" + sBgi + '\'' + + ", showBGI=" + showBGI + + ", externalStatusString='" + externalStatusString + '\'' + + ", batteryLevel=" + batteryLevel + + ", openApsStatus=" + openApsStatus + + ", bgDataList size=" + bgDataList.size() + + ", tempWatchDataList size=" + tempWatchDataList.size() + + ", basalWatchDataList size=" + basalWatchDataList.size() + + ", bolusWatchDataLis size=" + bolusWatchDataList.size() + + ", predictionList size=" + predictionList.size() + + '}'; + } + + public void updateFromPersistence(Persistence persistence) { + + DataMap dataMapData = persistence.getDataMap(DATA_PERSISTENCE_KEY); + if (dataMapData != null) { + updateData(dataMapData); + } + DataMap dataMapStatus = persistence.getDataMap(STATUS_PERSISTENCE_KEY); + if (dataMapStatus != null) { + updateStatus(dataMapStatus); + } + DataMap dataMapBasals = persistence.getDataMap(BASALS_PERSISTENCE_KEY); + if (dataMapBasals != null) { + updateBasals(dataMapBasals); + } + } + + /* + * Since complications do not need Basals, we skip them for performance + */ + public void updateForComplicationsFromPersistence(Persistence persistence) { + + DataMap dataMapData = persistence.getDataMap(DATA_PERSISTENCE_KEY); + if (dataMapData != null) { + updateData(dataMapData); + } + DataMap dataMapStatus = persistence.getDataMap(STATUS_PERSISTENCE_KEY); + if (dataMapStatus != null) { + updateStatus(dataMapStatus); + } + } + + public DataMap updateDataFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { + Bundle bundle = intent.getBundleExtra("data"); + if (bundle != null) { + DataMap dataMap = WearUtil.bundleToDataMap(bundle); + updateData(dataMap); + return dataMap; + } + return null; + } + + private void updateData(DataMap dataMap) { + WearUtil.getWakeLock("readingPrefs", 50); + sgvLevel = dataMap.getLong("sgvLevel"); + datetime = dataMap.getLong("timestamp"); + sSgv = dataMap.getString("sgvString"); + sDirection = dataMap.getString("slopeArrow"); + sDelta = dataMap.getString("delta"); + sAvgDelta = dataMap.getString("avgDelta"); + sUnits = dataMap.getString("glucoseUnits"); + } + + public DataMap updateStatusFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { + Bundle bundle = intent.getBundleExtra("status"); + if (bundle != null) { + DataMap dataMap = WearUtil.bundleToDataMap(bundle); + updateStatus(dataMap); + return dataMap; + } + return null; + } + + private void updateStatus(DataMap dataMap) { + WearUtil.getWakeLock("readingPrefs", 50); + sBasalRate = dataMap.getString("currentBasal"); + sUploaderBattery = dataMap.getString("battery"); + sRigBattery = dataMap.getString("rigBattery"); + detailedIOB = dataMap.getBoolean("detailedIob"); + sIOB1 = dataMap.getString("iobSum") + "U"; + sIOB2 = dataMap.getString("iobDetail"); + sCOB1 = "Carb"; + sCOB2 = dataMap.getString("cob"); + sBgi = dataMap.getString("bgi"); + showBGI = dataMap.getBoolean("showBgi"); + externalStatusString = dataMap.getString("externalStatusString"); + batteryLevel = dataMap.getInt("batteryLevel"); + openApsStatus = dataMap.getLong("openApsStatus"); + } + + public DataMap updateBasalsFromMessage(Intent intent, PowerManager.WakeLock wakeLock) { + Bundle bundle = intent.getBundleExtra("basals"); + if (bundle != null) { + DataMap dataMap = WearUtil.bundleToDataMap(bundle); + updateBasals(dataMap); + return dataMap; + } + return null; + } + + private void updateBasals(DataMap dataMap) { + WearUtil.getWakeLock("readingPrefs", 500); + loadBasalsAndTemps(dataMap); + } + + private void loadBasalsAndTemps(DataMap dataMap) { + ArrayList 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); + } + } + ArrayList 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); + } + } + ArrayList boluses = dataMap.getDataMapArrayList("boluses"); + if (boluses != null) { + bolusWatchDataList = new ArrayList<>(); + for (DataMap bolus : boluses) { + BolusWatchData bwd = new BolusWatchData(); + bwd.date = bolus.getLong("date"); + bwd.bolus = bolus.getDouble("bolus"); + bwd.carbs = bolus.getDouble("carbs"); + bwd.isSMB = bolus.getBoolean("isSMB"); + bwd.isValid = bolus.getBoolean("isValid"); + bolusWatchDataList.add(bwd); + } + } + ArrayList predictions = dataMap.getDataMapArrayList("predictions"); + if (boluses != null) { + predictionList = new ArrayList<>(); + for (DataMap prediction : predictions) { + BgWatchData bwd = new BgWatchData(); + bwd.timestamp = prediction.getLong("timestamp"); + bwd.sgv = prediction.getDouble("sgv"); + bwd.color = prediction.getInt("color"); + predictionList.add(bwd); + } + } + } + + public void addToWatchSet(DataMap dataMap) { + ArrayList entries = dataMap.getDataMapArrayList("entries"); + if (entries != null) { + bgDataList = new ArrayList<>(); + for (DataMap entry : entries) { + double sgv = entry.getDouble("sgvDouble"); + double high = entry.getDouble("high"); + double low = entry.getDouble("low"); + long timestamp = entry.getLong("timestamp"); + int color = entry.getInt("color", 0); + bgDataList.add(new BgWatchData(sgv, high, low, timestamp, color)); + } + } else { + double sgv = dataMap.getDouble("sgvDouble"); + double high = dataMap.getDouble("high"); + double low = dataMap.getDouble("low"); + long timestamp = dataMap.getLong("timestamp"); + int color = dataMap.getInt("color", 0); + + 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, color)); + } + + // 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 + } + } + } +} diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/DisplayFormat.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/DisplayFormat.java index 4925bb1f5c..f2cf06671e 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/DisplayFormat.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/DisplayFormat.java @@ -1,17 +1,19 @@ package info.nightscout.androidaps.interaction.utils; import info.nightscout.androidaps.aaps; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; public class DisplayFormat { /** - * Maximal lengths of fields/labels shown in complications + * Maximal and minimal lengths of fields/labels shown in complications, in characters + * For MAX values - above that WearOS and watch faces may start ellipsize (...) contents + * For MIN values - this is minimal length that can hold legible data */ - public static final int MAX_LONG_FIELD = 22; // this is empirical, above that many watch faces start to ellipsize - public static final int MAX_SHORT_FIELD = 7; // according to Wear OS docs for TYPE_SHORT_TEXT - public static final int MIN_COB_FIELD = 3; // since carbs are 0..99g - public static final int MIN_IOB_FIELD = 3; // IoB can range from like .1U to 99U + public static final int MAX_FIELD_LEN_LONG = 22; // this is found out empirical, for TYPE_LONG_TEXT + public static final int MAX_FIELD_LEN_SHORT = 7; // according to Wear OS docs for TYPE_SHORT_TEXT + public static final int MIN_FIELD_LEN_COB = 3; // since carbs are usually 0..99g + public static final int MIN_FIELD_LEN_IOB = 3; // IoB can range from like .1U to 99U public static String deltaSymbol() { return aaps.areComplicationsUnicode() ? "\u0394" : ""; @@ -48,32 +50,32 @@ public class DisplayFormat { } } - public static String shortTrend(final DisplayRawData raw) { + public static String shortTrend(final RawDisplayData raw) { String minutes = "--"; if (raw.datetime > 0) { minutes = shortTimeSince(raw.datetime); } - if (minutes.length() + raw.sDelta.length() + deltaSymbol().length() + 1 <= MAX_SHORT_FIELD) { + if (minutes.length() + raw.sDelta.length() + deltaSymbol().length() + 1 <= MAX_FIELD_LEN_SHORT) { 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) { + String delta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_FIELD_LEN_SHORT -1); + if (minutes.length() + delta.length() + deltaSymbol().length() + 1 <= MAX_FIELD_LEN_SHORT) { return minutes + " " + deltaSymbol() + delta; } - String shortDelta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_SHORT_FIELD-(1+minutes.length())); + String shortDelta = (new SmallestDoubleString(raw.sDelta)).minimise(MAX_FIELD_LEN_SHORT -(1+minutes.length())); return minutes + " " + shortDelta; } - public static String longGlucoseLine(final DisplayRawData raw) { + public static String longGlucoseLine(final RawDisplayData raw) { return raw.sSgv + raw.sDirection + " " + deltaSymbol() + (new SmallestDoubleString(raw.sDelta)).minimise(8) + " (" + shortTimeSince(raw.datetime) + ")"; } - public static String longDetailsLine(final DisplayRawData raw) { + public static String longDetailsLine(final RawDisplayData raw) { final String SEP_LONG = " " + verticalSeparatorSymbol() + " "; final String SEP_SHORT = " " + verticalSeparatorSymbol() + " "; @@ -81,26 +83,26 @@ public class DisplayFormat { final String SEP_MIN = " "; String line = raw.sCOB2 + SEP_LONG + raw.sIOB1 + SEP_LONG + basalRateSymbol()+raw.sBasalRate; - if (line.length() <= MAX_LONG_FIELD) { + if (line.length() <= MAX_FIELD_LEN_LONG) { return line; } line = raw.sCOB2 + SEP_SHORT + raw.sIOB1 + SEP_SHORT + raw.sBasalRate; - if (line.length() <= MAX_LONG_FIELD) { + if (line.length() <= MAX_FIELD_LEN_LONG) { return line; } - int remainingMax = MAX_LONG_FIELD - (raw.sCOB2.length() + raw.sBasalRate.length() + SEP_SHORT_LEN*2); - final String smallestIoB = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_IOB_FIELD, remainingMax)); + int remainingMax = MAX_FIELD_LEN_LONG - (raw.sCOB2.length() + raw.sBasalRate.length() + SEP_SHORT_LEN*2); + final String smallestIoB = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_FIELD_LEN_IOB, remainingMax)); line = raw.sCOB2 + SEP_SHORT + smallestIoB + SEP_SHORT + raw.sBasalRate; - if (line.length() <= MAX_LONG_FIELD) { + if (line.length() <= MAX_FIELD_LEN_LONG) { return line; } - remainingMax = MAX_LONG_FIELD - (smallestIoB.length() + raw.sBasalRate.length() + SEP_SHORT_LEN*2); - final String simplifiedCob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_COB_FIELD, remainingMax)); + remainingMax = MAX_FIELD_LEN_LONG - (smallestIoB.length() + raw.sBasalRate.length() + SEP_SHORT_LEN*2); + final String simplifiedCob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_FIELD_LEN_COB, remainingMax)); line = simplifiedCob + SEP_SHORT + smallestIoB + SEP_SHORT + raw.sBasalRate; - if (line.length() <= MAX_LONG_FIELD) { + if (line.length() <= MAX_FIELD_LEN_LONG) { return line; } @@ -109,17 +111,17 @@ public class DisplayFormat { return line; } - public static Pair detailedIob(DisplayRawData raw) { - final String iob1 = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_SHORT_FIELD); + public static Pair detailedIob(RawDisplayData raw) { + final String iob1 = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_FIELD_LEN_SHORT); String iob2 = ""; if (raw.sIOB2.contains("|")) { String[] iobs = raw.sIOB2.replace("(", "").replace(")", "").split("\\|"); - String iobBolus = new SmallestDoubleString(iobs[0]).minimise(MIN_IOB_FIELD); + String iobBolus = new SmallestDoubleString(iobs[0]).minimise(MIN_FIELD_LEN_IOB); if (iobBolus.trim().length() == 0) { iobBolus = "--"; } - String iobBasal = new SmallestDoubleString(iobs[1]).minimise((MAX_SHORT_FIELD-1) - Math.max(MIN_IOB_FIELD, iobBolus.length())); + String iobBasal = new SmallestDoubleString(iobs[1]).minimise((MAX_FIELD_LEN_SHORT -1) - Math.max(MIN_FIELD_LEN_IOB, iobBolus.length())); if (iobBasal.trim().length() == 0) { iobBasal = "--"; } @@ -128,14 +130,14 @@ public class DisplayFormat { return Pair.create(iob1, iob2); } - public static Pair detailedCob(final DisplayRawData raw) { + public static Pair detailedCob(final RawDisplayData raw) { SmallestDoubleString cobMini = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE); String cob2 = ""; if (cobMini.getExtra().length() > 0) { cob2 = cobMini.getExtra() + cobMini.getUnits(); } - final String cob1 = cobMini.minimise(MAX_SHORT_FIELD); + final String cob1 = cobMini.minimise(MAX_FIELD_LEN_SHORT); return Pair.create(cob1, cob2); } } diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Inevitable.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Inevitable.java index 21944da9c7..54361b6678 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Inevitable.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Inevitable.java @@ -5,6 +5,8 @@ import android.util.Log; import java.util.concurrent.ConcurrentHashMap; +import info.nightscout.androidaps.BuildConfig; + /** * Created for xDrip by jamorham on 07/03/2018 * Adapted for AAPS by dlvoy on 2019-11-11 @@ -18,7 +20,7 @@ public class Inevitable { private static final String TAG = Inevitable.class.getSimpleName(); private static final int MAX_QUEUE_TIME = (int) Constants.MINUTE_IN_MS * 6; - private static final boolean d = true; + private static final boolean debug = BuildConfig.DEBUG; private static final ConcurrentHashMap tasks = new ConcurrentHashMap<>(); @@ -31,14 +33,14 @@ public class Inevitable { // if it already exists then extend the time task.extendTime(idle_for); - if (d) + if (debug) Log.d(TAG, "Extending time for: " + id + " to " + WearUtil.dateTimeText(task.when)); } else { // otherwise create new task if (runnable == null) return; // extension only if already exists tasks.put(id, new Task(id, idle_for, runnable)); - if (d) { + if (debug) { Log.d(TAG, "Creating task: " + id + " due: " + WearUtil.dateTimeText(tasks.get(id).when)); } @@ -58,7 +60,6 @@ public class Inevitable { } }); t.setPriority(Thread.MIN_PRIORITY); - //t.setDaemon(true); t.start(); } } @@ -100,7 +101,7 @@ public class Inevitable { public boolean poll() { final long till = WearUtil.msTill(when); if (till < 1) { - if (d) Log.d(TAG, "Executing task! " + this.id); + if (debug) Log.d(TAG, "Executing task! " + this.id); tasks.remove(this.id); // early remove to allow overlapping scheduling what.run(); return true; diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Persistence.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Persistence.java index cfc6c2b4b4..36c0ae76ee 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Persistence.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/Persistence.java @@ -3,6 +3,8 @@ package info.nightscout.androidaps.interaction.utils; import android.content.SharedPreferences; import android.util.Base64; +import androidx.annotation.Nullable; + import com.google.android.gms.wearable.DataMap; import java.util.Set; @@ -22,15 +24,15 @@ public class Persistence { preferences = aaps.getAppContext().getSharedPreferences(COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY, 0); } + @Nullable public DataMap getDataMap(String key) { if (preferences.contains(key)) { final String rawB64Data = preferences.getString(key, null); byte[] rawData = Base64.decode(rawB64Data, Base64.DEFAULT); try { - DataMap dataMap = DataMap.fromByteArray(rawData); - return dataMap; + return DataMap.fromByteArray(rawData); } catch (IllegalArgumentException ex) { - + // Should never happen, and if it happen - we ignore and fallback to null } } return null; diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java index 446d0f7d68..f8415bdd4a 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.PowerManager; +import android.os.SystemClock; import android.util.Log; import com.google.android.gms.wearable.DataMap; @@ -53,14 +54,14 @@ public class WearUtil { //============================================================================================== // return true if below rate limit - public static synchronized boolean rateLimit(String name, int seconds) { + public static synchronized boolean isBelowRateLimit(String named, int onceForSeconds) { // check if over limit - if ((rateLimits.containsKey(name)) && (timestamp() - rateLimits.get(name) < (seconds * 1000))) { - Log.d(TAG, name + " rate limited: " + seconds + " seconds"); + if ((rateLimits.containsKey(named)) && (timestamp() - rateLimits.get(named) < (onceForSeconds * 1000))) { + Log.d(TAG, named + " rate limited to one for " + onceForSeconds + " seconds"); return false; } // not over limit - rateLimits.put(name, timestamp()); + rateLimits.put(named, timestamp()); return true; } @@ -82,18 +83,15 @@ public class WearUtil { aaps.getAppContext().startActivity(getStartActivityIntent(c)); } - public static Intent getStartActivityIntent(Class c) { return new Intent(aaps.getAppContext(), c).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } - - public static void threadSleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { - // + // we simply ignore if sleep was interrupted } } diff --git a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BaseWatchFace.java b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BaseWatchFace.java index 222f1eda6e..1d1f5758e8 100644 --- a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BaseWatchFace.java +++ b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BaseWatchFace.java @@ -35,7 +35,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import info.nightscout.androidaps.complications.BaseComplicationProviderService; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.ListenerService; import info.nightscout.androidaps.R; import lecho.lib.hellocharts.view.LineChartView; @@ -70,7 +70,7 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferen public LineChartView chart; - public DisplayRawData rawData = new DisplayRawData(); + public RawDisplayData rawData = new RawDisplayData(); public PowerManager.WakeLock wakeLock; // related endTime manual layout diff --git a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java index b40e15cfd4..ee17d91e76 100644 --- a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java +++ b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.watchfaces; import android.content.Context; -import android.graphics.Color; import android.graphics.DashPathEffect; import android.preference.PreferenceManager; import android.text.format.DateFormat; @@ -18,7 +17,7 @@ import java.util.TimeZone; import info.nightscout.androidaps.data.BasalWatchData; import info.nightscout.androidaps.data.BgWatchData; import info.nightscout.androidaps.data.BolusWatchData; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.TempWatchData; import lecho.lib.hellocharts.model.Axis; import lecho.lib.hellocharts.model.AxisValue; @@ -116,7 +115,7 @@ public class BgGraphBuilder { this.end_time = (predictionEndTime>end_time)?predictionEndTime:end_time; } - public BgGraphBuilder(Context context, DisplayRawData raw, int aPointSize, int aHighColor, int aLowColor, int aMidColor, int gridColour, int basalBackgroundColor, int basalCenterColor, int bolusInvalidColor, int carbsColor, int timespan) { + public BgGraphBuilder(Context context, RawDisplayData raw, int aPointSize, int aHighColor, int aLowColor, int aMidColor, int gridColour, int basalBackgroundColor, int basalCenterColor, int bolusInvalidColor, int carbsColor, int timespan) { this(context, raw.bgDataList, raw.predictionList, @@ -135,7 +134,7 @@ public class BgGraphBuilder { timespan); } - public BgGraphBuilder(Context context, DisplayRawData raw, int aPointSize, int aMidColor, int gridColour, int basalBackgroundColor, int basalCenterColor, int bolusInvalidColor, int carbsColor, int timespan) { + public BgGraphBuilder(Context context, RawDisplayData raw, int aPointSize, int aMidColor, int gridColour, int basalBackgroundColor, int basalCenterColor, int bolusInvalidColor, int carbsColor, int timespan) { this(context, raw.bgDataList, raw.predictionList, diff --git a/wear/src/test/java/info/nightscout/androidaps/data/BgWatchDataTest.java b/wear/src/test/java/info/nightscout/androidaps/data/BgWatchDataTest.java index fa74d17691..e920dc3202 100644 --- a/wear/src/test/java/info/nightscout/androidaps/data/BgWatchDataTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/data/BgWatchDataTest.java @@ -27,7 +27,7 @@ import static org.junit.Assert.assertTrue; public class BgWatchDataTest { @Before - public void mock() { + public void mock() throws Exception { WearUtilMocker.prepareMockNoReal(); } diff --git a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataSgvDataTest.java b/wear/src/test/java/info/nightscout/androidaps/data/RawDataSgvDisplayDataTest.java similarity index 86% rename from wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataSgvDataTest.java rename to wear/src/test/java/info/nightscout/androidaps/data/RawDataSgvDisplayDataTest.java index 55ff9bb815..fa73a0272e 100644 --- a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataSgvDataTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/data/RawDataSgvDisplayDataTest.java @@ -29,10 +29,10 @@ import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) @PrepareForTest( { WearUtil.class, Log.class, SharedPreferences.class, Context.class, aaps.class, android.util.Base64.class, Intent.class } ) -public class DisplayRawDataSgvDataTest { +public class RawDataSgvDisplayDataTest { @Before - public void mock() { + public void mock() throws Exception { AAPSMocker.prepareMock(); AAPSMocker.resetMockedSharedPrefs(); AndroidMocker.mockBase64(); @@ -55,7 +55,7 @@ public class DisplayRawDataSgvDataTest { return dataMap; } - private void assertDataEmpty(DisplayRawData newRaw) { + private void assertDataEmpty(RawDisplayData newRaw) { assertThat(newRaw.sgvLevel, is(0L)); assertThat(newRaw.datetime, is(0L)); assertThat(newRaw.sSgv, is("---")); @@ -65,7 +65,7 @@ public class DisplayRawDataSgvDataTest { assertThat(newRaw.sUnits, is("-")); } - private void assertDataOk(DisplayRawData newRaw) { + private void assertDataOk(RawDisplayData newRaw) { assertThat(newRaw.sgvLevel, is(1L)); assertThat(newRaw.datetime, is(WearUtilMocker.REF_NOW - Constants.MINUTE_IN_MS)); assertThat(newRaw.sSgv, is("106")); @@ -79,7 +79,7 @@ public class DisplayRawDataSgvDataTest { public void updateDataFromEmptyPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateFromPersistence(persistence); @@ -92,10 +92,10 @@ public class DisplayRawDataSgvDataTest { public void updateDataFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.DATA_PERSISTENCE_KEY, dataMapForData()); + Persistence.storeDataMap(RawDisplayData.DATA_PERSISTENCE_KEY, dataMapForData()); newRaw.updateFromPersistence(persistence); // THEN @@ -106,11 +106,11 @@ public class DisplayRawDataSgvDataTest { public void partialUpdateDataFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.DATA_PERSISTENCE_KEY, dataMapForData()); - newRaw.partialUpdateFromPersistence(persistence); + Persistence.storeDataMap(RawDisplayData.DATA_PERSISTENCE_KEY, dataMapForData()); + newRaw.updateForComplicationsFromPersistence(persistence); // THEN assertDataOk(newRaw); @@ -123,7 +123,7 @@ public class DisplayRawDataSgvDataTest { Bundle bundle = BundleMock.mock(dataMapForData()); intent.putExtra("data", bundle); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateDataFromMessage(intent, null); @@ -136,7 +136,7 @@ public class DisplayRawDataSgvDataTest { public void updateDataFromEmptyMessageTest() { // GIVEN Intent intent = IntentMock.mock(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateDataFromMessage(intent, null); diff --git a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBasalsTest.java b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBasalsTest.java similarity index 93% rename from wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBasalsTest.java rename to wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBasalsTest.java index d3f9dcf48b..1245d0fbcb 100644 --- a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBasalsTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBasalsTest.java @@ -35,10 +35,10 @@ import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) @PrepareForTest( { WearUtil.class, Log.class, SharedPreferences.class, Context.class, aaps.class, android.util.Base64.class, Intent.class } ) -public class DisplayRawDataBasalsTest { +public class RawDisplayDataBasalsTest { @Before - public void mock() { + public void mock() throws Exception { AAPSMocker.prepareMock(); AAPSMocker.resetMockedSharedPrefs(); AndroidMocker.mockBase64(); @@ -119,14 +119,14 @@ public class DisplayRawDataBasalsTest { return dataMap; } - private void assertBasalsEmpty(DisplayRawData newRaw) { + private void assertBasalsEmpty(RawDisplayData newRaw) { assertThat(newRaw.tempWatchDataList.size(), is(0)); assertThat(newRaw.basalWatchDataList.size(), is(0)); assertThat(newRaw.bolusWatchDataList.size(), is(0)); assertThat(newRaw.predictionList.size(), is(0)); } - private void assertBasalsOk(DisplayRawData newRaw) { + private void assertBasalsOk(RawDisplayData newRaw) { assertThat(newRaw.tempWatchDataList.size(), is(2)); assertThat(newRaw.basalWatchDataList.size(), is(1)); assertThat(newRaw.bolusWatchDataList.size(), is(3)); @@ -196,7 +196,7 @@ public class DisplayRawDataBasalsTest { public void updateBasalsFromEmptyPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateFromPersistence(persistence); @@ -209,10 +209,10 @@ public class DisplayRawDataBasalsTest { public void updateBasalsFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.BASALS_PERSISTENCE_KEY, dataMapForBasals()); + Persistence.storeDataMap(RawDisplayData.BASALS_PERSISTENCE_KEY, dataMapForBasals()); newRaw.updateFromPersistence(persistence); // THEN @@ -223,11 +223,11 @@ public class DisplayRawDataBasalsTest { public void partialUpdateBasalsFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.BASALS_PERSISTENCE_KEY, dataMapForBasals()); - newRaw.partialUpdateFromPersistence(persistence); + Persistence.storeDataMap(RawDisplayData.BASALS_PERSISTENCE_KEY, dataMapForBasals()); + newRaw.updateForComplicationsFromPersistence(persistence); // THEN assertBasalsEmpty(newRaw); @@ -240,7 +240,7 @@ public class DisplayRawDataBasalsTest { Bundle bundle = BundleMock.mock(dataMapForBasals()); intent.putExtra("basals", bundle); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateBasalsFromMessage(intent, null); @@ -253,7 +253,7 @@ public class DisplayRawDataBasalsTest { public void updateBasalsFromEmptyMessageTest() { // GIVEN Intent intent = IntentMock.mock(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateBasalsFromMessage(intent, null); diff --git a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBgEntriesTest.java b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBgEntriesTest.java similarity index 96% rename from wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBgEntriesTest.java rename to wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBgEntriesTest.java index 1908e20dd5..c200288213 100644 --- a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataBgEntriesTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataBgEntriesTest.java @@ -20,10 +20,10 @@ import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) @PrepareForTest( { WearUtil.class } ) -public class DisplayRawDataBgEntriesTest { +public class RawDisplayDataBgEntriesTest { @Before - public void mock() { + public void mock() throws Exception { WearUtilMocker.prepareMockNoReal(); } @@ -62,7 +62,7 @@ public class DisplayRawDataBgEntriesTest { @Test public void addToWatchSetTest() { // GIVEN - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); DataMap multipleEntries = dataMapForEntries(); DataMap singleEntry1 = dataMapForEntries(WearUtilMocker.REF_NOW - Constants.MINUTE_IN_MS*4*2,92); DataMap singleEntry2 = dataMapForEntries(WearUtilMocker.REF_NOW - Constants.MINUTE_IN_MS*4*1,88); @@ -107,7 +107,7 @@ public class DisplayRawDataBgEntriesTest { @Test public void addToWatchSetCleanupOldTest() { - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); newRaw.addToWatchSet(dataMapForEntries(WearUtil.timestamp(),125)); assertThat(newRaw.bgDataList.size(), is(1)); diff --git a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataStatusTest.java b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataStatusTest.java similarity index 88% rename from wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataStatusTest.java rename to wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataStatusTest.java index 17ac8fd14b..da0daea608 100644 --- a/wear/src/test/java/info/nightscout/androidaps/data/DisplayRawDataStatusTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/data/RawDisplayDataStatusTest.java @@ -31,10 +31,10 @@ import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) @PrepareForTest( { WearUtil.class, Log.class, SharedPreferences.class, Context.class, aaps.class, android.util.Base64.class, Intent.class } ) -public class DisplayRawDataStatusTest { +public class RawDisplayDataStatusTest { @Before - public void mock() { + public void mock() throws Exception { AAPSMocker.prepareMock(); AAPSMocker.resetMockedSharedPrefs(); AndroidMocker.mockBase64(); @@ -43,7 +43,7 @@ public class DisplayRawDataStatusTest { @Test public void toDebugStringTest() { - DisplayRawData raw = RawDataMocker.rawDelta(5, "1.5"); + RawDisplayData raw = RawDataMocker.rawDelta(5, "1.5"); raw.externalStatusString = "placeholder-here"; assertThat(raw.datetime, is(WearUtilMocker.REF_NOW - Constants.MINUTE_IN_MS*5)); @@ -71,7 +71,7 @@ public class DisplayRawDataStatusTest { return dataMap; } - private void assertStatusEmpty(DisplayRawData newRaw) { + private void assertStatusEmpty(RawDisplayData newRaw) { assertThat(newRaw.sBasalRate, is("-.--U/h")); assertThat(newRaw.sUploaderBattery, is("--")); assertThat(newRaw.sRigBattery, is("--")); @@ -87,7 +87,7 @@ public class DisplayRawDataStatusTest { assertThat(newRaw.openApsStatus, is(-1L)); } - private void assertStatusOk(DisplayRawData newRaw) { + private void assertStatusOk(RawDisplayData newRaw) { assertThat(newRaw.sBasalRate, is("120%")); assertThat(newRaw.sUploaderBattery, is("76")); assertThat(newRaw.sRigBattery, is("40%")); @@ -107,7 +107,7 @@ public class DisplayRawDataStatusTest { public void updateStatusFromEmptyPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateFromPersistence(persistence); @@ -120,10 +120,10 @@ public class DisplayRawDataStatusTest { public void updateStatusFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.STATUS_PERSISTENCE_KEY, dataMapForStatus()); + Persistence.storeDataMap(RawDisplayData.STATUS_PERSISTENCE_KEY, dataMapForStatus()); newRaw.updateFromPersistence(persistence); // THEN @@ -134,11 +134,11 @@ public class DisplayRawDataStatusTest { public void partialUpdateStatusFromPersistenceTest() { // GIVEN Persistence persistence = new Persistence(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN - Persistence.storeDataMap(DisplayRawData.STATUS_PERSISTENCE_KEY, dataMapForStatus()); - newRaw.partialUpdateFromPersistence(persistence); + Persistence.storeDataMap(RawDisplayData.STATUS_PERSISTENCE_KEY, dataMapForStatus()); + newRaw.updateForComplicationsFromPersistence(persistence); // THEN assertStatusOk(newRaw); @@ -151,7 +151,7 @@ public class DisplayRawDataStatusTest { Bundle bundle = BundleMock.mock(dataMapForStatus()); intent.putExtra("status", bundle); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateStatusFromMessage(intent, null); @@ -164,7 +164,7 @@ public class DisplayRawDataStatusTest { public void updateStatusFromEmptyMessageTest() { // GIVEN Intent intent = IntentMock.mock(); - DisplayRawData newRaw = new DisplayRawData(); + RawDisplayData newRaw = new RawDisplayData(); // WHEN newRaw.updateStatusFromMessage(intent, null); diff --git a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/DisplayFormatTest.java b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/DisplayFormatTest.java index 3c838f5898..98fb4fd11b 100644 --- a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/DisplayFormatTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/DisplayFormatTest.java @@ -11,7 +11,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import info.nightscout.androidaps.aaps; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.testing.mockers.AAPSMocker; import info.nightscout.androidaps.testing.mockers.WearUtilMocker; @@ -34,7 +34,7 @@ import static org.junit.Assert.assertThat; public class DisplayFormatTest { @Before - public void mock() { + public void mock() throws Exception { WearUtilMocker.prepareMock(); AAPSMocker.prepareMock(); AAPSMocker.resetMockedSharedPrefs(); @@ -104,7 +104,7 @@ public class DisplayFormatTest { @Test public void shortTrendTest() { - DisplayRawData raw = new DisplayRawData(); + RawDisplayData raw = new RawDisplayData(); assertThat(DisplayFormat.shortTrend(raw), is("-- Δ--")); raw.datetime = backInTime(0, 0, 2, 0); diff --git a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/PersistenceTest.java b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/PersistenceTest.java index 625216aadc..1faa4ddfe7 100644 --- a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/PersistenceTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/PersistenceTest.java @@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue; public class PersistenceTest { @Before - public void mock() { + public void mock() throws Exception { WearUtilMocker.prepareMock(); LogMocker.prepareMock(); AAPSMocker.prepareMock(); @@ -88,9 +88,9 @@ public class PersistenceTest { final long whenUpdatedNext = persistence.whenDataUpdated(); // THEN - assertThat(0L, is(whenNotUpdated)); - assertThat(REF_NOW, is(whenUpdatedFirst)); - assertThat(REF_NOW + 60000, is(whenUpdatedNext)); + assertThat(whenNotUpdated, is(0L)); + assertThat(whenUpdatedFirst, is(REF_NOW)); + assertThat(whenUpdatedNext, is(REF_NOW + 60000)); } @Test diff --git a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/WearUtilTest.java b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/WearUtilTest.java index 5bb501584d..9e60423c6a 100644 --- a/wear/src/test/java/info/nightscout/androidaps/interaction/utils/WearUtilTest.java +++ b/wear/src/test/java/info/nightscout/androidaps/interaction/utils/WearUtilTest.java @@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue; public class WearUtilTest { @Before - public void mock() { + public void mock() throws Exception { WearUtilMocker.prepareMock(); LogMocker.prepareMock(); } @@ -146,12 +146,12 @@ public class WearUtilTest { @Test public void rateLimitTest() { // WHEN - final boolean firstCall = WearUtil.rateLimit("test-limit", 3); - final boolean callAfterward = WearUtil.rateLimit("test-limit", 3); + final boolean firstCall = WearUtil.isBelowRateLimit("test-limit", 3); + final boolean callAfterward = WearUtil.isBelowRateLimit("test-limit", 3); WearUtilMocker.progressClock(500L); - final boolean callTooSoon = WearUtil.rateLimit("test-limit", 3); + final boolean callTooSoon = WearUtil.isBelowRateLimit("test-limit", 3); WearUtilMocker.progressClock(3100L); - final boolean callAfterRateLimit = WearUtil.rateLimit("test-limit", 3); + final boolean callAfterRateLimit = WearUtil.isBelowRateLimit("test-limit", 3); // THEN assertTrue(firstCall); @@ -166,7 +166,7 @@ public class WearUtilTest { * uses DataMap.fromBundle which need Android SDK runtime */ @Test - public void bundleToDataMapTest() { + public void bundleToDataMapTest() throws Exception { // GIVEN DataMap refMap = new DataMap(); refMap.putString("ala", "ma kota"); diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java index 664b431b5c..7684d169f1 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AAPSMocker.java @@ -22,28 +22,23 @@ public class AAPSMocker { private static final Map mockedSharedPrefs = new HashMap<>(); private static boolean unicodeComplicationsOn = true; - public static void prepareMock() { + public static void prepareMock() throws Exception { Context mockedContext = mock(Context.class); mockStatic(aaps.class, InvocationOnMock::callRealMethod); - try { - PowerMockito.when(aaps.class, "getAppContext").thenReturn(mockedContext); - PowerMockito.when(mockedContext, "getSharedPreferences", ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()).thenAnswer(invocation -> { - final String key = invocation.getArgument(0); - if (mockedSharedPrefs.containsKey(key)) { - return mockedSharedPrefs.get(key); - } else { - SharedPreferencesMock newPrefs = new SharedPreferencesMock(); - mockedSharedPrefs.put(key, newPrefs); - return newPrefs; - } - }); - PowerMockito.when(aaps.class, "areComplicationsUnicode").thenAnswer(invocation -> unicodeComplicationsOn); + PowerMockito.when(aaps.class, "getAppContext").thenReturn(mockedContext); + PowerMockito.when(mockedContext, "getSharedPreferences", ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()).thenAnswer(invocation -> { - - } catch (Exception e) { - Assert.fail("Unable to mock objects: " + e.getMessage()); - } + final String key = invocation.getArgument(0); + if (mockedSharedPrefs.containsKey(key)) { + return mockedSharedPrefs.get(key); + } else { + SharedPreferencesMock newPrefs = new SharedPreferencesMock(); + mockedSharedPrefs.put(key, newPrefs); + return newPrefs; + } + }); + PowerMockito.when(aaps.class, "areComplicationsUnicode").thenAnswer(invocation -> unicodeComplicationsOn); setMockedUnicodeComplicationsOn(true); resetMockedSharedPrefs(); diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AndroidMocker.java b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AndroidMocker.java index 62eb5c6301..a1addfdc1d 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AndroidMocker.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/AndroidMocker.java @@ -12,29 +12,25 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic; public class AndroidMocker { - public static void mockBase64() { + public static void mockBase64() throws Exception { mockStatic(android.util.Base64.class); - try { - PowerMockito.when(android.util.Base64.class, "decode", anyString(), anyInt()).thenAnswer(invocation -> { - final String payload = invocation.getArgument(0); - try { - return Base64.getDecoder().decode(payload); - } catch (java.lang.IllegalArgumentException ex) { - return null; - } - }); + PowerMockito.when(android.util.Base64.class, "decode", anyString(), anyInt()).thenAnswer(invocation -> { - PowerMockito.when(android.util.Base64.class, "encodeToString", any(), anyInt()).thenAnswer(invocation -> { + final String payload = invocation.getArgument(0); + try { + return Base64.getDecoder().decode(payload); + } catch (java.lang.IllegalArgumentException ex) { + return null; + } + }); - final byte[] payload = invocation.getArgument(0); - return Base64.getEncoder().encodeToString(payload); + PowerMockito.when(android.util.Base64.class, "encodeToString", any(), anyInt()).thenAnswer(invocation -> { - }); + final byte[] payload = invocation.getArgument(0); + return Base64.getEncoder().encodeToString(payload); - } catch (Exception e) { - Assert.fail("Unable to mock objects: " + e.getMessage()); - } + }); } } diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/RawDataMocker.java b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/RawDataMocker.java index 832720bbff..7d06f00c51 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/RawDataMocker.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/RawDataMocker.java @@ -1,14 +1,14 @@ package info.nightscout.androidaps.testing.mockers; -import info.nightscout.androidaps.data.DisplayRawData; +import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.interaction.utils.SafeParse; import static info.nightscout.androidaps.testing.mockers.WearUtilMocker.backInTime; public class RawDataMocker { - public static DisplayRawData rawSgv(String sgv, int m, String deltaString) { - DisplayRawData raw = new DisplayRawData(); + public static RawDisplayData rawSgv(String sgv, int m, String deltaString) { + RawDisplayData raw = new RawDisplayData(); raw.datetime = backInTime(0, 0, m, 0); raw.sDelta = deltaString; raw.sSgv = sgv; @@ -34,30 +34,30 @@ public class RawDataMocker { return raw; } - public static DisplayRawData rawDelta(int m, String delta) { - DisplayRawData raw = new DisplayRawData(); + public static RawDisplayData rawDelta(int m, String delta) { + RawDisplayData raw = new RawDisplayData(); raw.datetime = backInTime(0, 0, m, 0); raw.sDelta = delta; return raw; } - public static DisplayRawData rawCobIobBr(String cob, String iob, String br) { - DisplayRawData raw = new DisplayRawData(); + public static RawDisplayData rawCobIobBr(String cob, String iob, String br) { + RawDisplayData raw = new RawDisplayData(); raw.sCOB2 = cob; raw.sIOB1 = iob; raw.sBasalRate = br; return raw; } - public static DisplayRawData rawIob(String iob, String iob2) { - DisplayRawData raw = new DisplayRawData(); + public static RawDisplayData rawIob(String iob, String iob2) { + RawDisplayData raw = new RawDisplayData(); raw.sIOB1 = iob; raw.sIOB2 = iob2; return raw; } - public static DisplayRawData rawCob(String cob) { - DisplayRawData raw = new DisplayRawData(); + public static RawDisplayData rawCob(String cob) { + RawDisplayData raw = new RawDisplayData(); raw.sCOB2 = cob; return raw; } diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/WearUtilMocker.java b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/WearUtilMocker.java index fbe13e52b6..11a89f0699 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/mockers/WearUtilMocker.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/mockers/WearUtilMocker.java @@ -25,28 +25,22 @@ public class WearUtilMocker { public static final long REF_NOW = 1572610530000L; private static long clockMsDiff = 0L; - public static void prepareMock() { + public static void prepareMock() throws Exception { resetClock(); mockStatic(WearUtil.class, InvocationOnMock::callRealMethod); - try { - // because we cleverly used timestamp() by implementation, we can mock it - // and control the time in tests - PowerMockito.when(WearUtil.class, "timestamp").then(invocation -> (REF_NOW + clockMsDiff)); - } catch (Exception e) { - Assert.fail("Unable to mock the construction of the WearUtil object: " + e.getMessage()); - } + + // because we cleverly used timestamp() by implementation, we can mock it + // and control the time in tests + PowerMockito.when(WearUtil.class, "timestamp").then(invocation -> (REF_NOW + clockMsDiff)); } - public static void prepareMockNoReal() { + public static void prepareMockNoReal() throws Exception { resetClock(); mockStatic(WearUtil.class); - try { - PowerMockito.when(WearUtil.class, "timestamp").then(invocation -> REF_NOW + clockMsDiff); - PowerMockito.when(WearUtil.class, "getWakeLock", anyString(), anyInt()).then(invocation -> null); - PowerMockito.when(WearUtil.class, "bundleToDataMap", any(Bundle.class)).then(bundleToDataMapMock); - } catch (Exception e) { - Assert.fail("Unable to mock the construction of the WearUtil object: " + e.getMessage()); - } + + PowerMockito.when(WearUtil.class, "timestamp").then(invocation -> REF_NOW + clockMsDiff); + PowerMockito.when(WearUtil.class, "getWakeLock", anyString(), anyInt()).then(invocation -> null); + PowerMockito.when(WearUtil.class, "bundleToDataMap", any(Bundle.class)).then(bundleToDataMapMock); } public static void resetClock() {