diff --git a/wear/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java b/wear/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java index 7d29a50447..ab106a100d 100644 --- a/wear/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java +++ b/wear/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java @@ -380,7 +380,8 @@ public static void create(EGVRecord egvRecord, long addativeOffset, Context cont bgReading.save(); bgReading.find_new_curve(); bgReading.find_new_raw_curve(); - context.startService(new Intent(context, Notifications.class)); + //context.startService(new Intent(context, Notifications.class)); + Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading BgSendQueue.handleNewBgReading(bgReading, "create", context); } } @@ -582,7 +583,8 @@ public static BgReading create(double raw_data, double filtered_data, Context co BloodTest.opportunisticCalibration(); } - context.startService(new Intent(context, Notifications.class)); + //context.startService(new Intent(context, Notifications.class)); + // allow this instead to be fired inside handleNewBgReading when noise will have been injected already } bgReading.injectNoise(true); // Add noise parameter for nightscout bgReading.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout @@ -1058,7 +1060,7 @@ public boolean isBackfilled() { public static final double SPECIAL_G5_PLACEHOLDER = -0.1597; // TODO remember to sync this with wear code base - public static synchronized BgReading bgReadingInsertFromG5(double calculated_value, long timestamp) { + public static synchronized BgReading bgReadingInsertFromG5(double calculated_value, long timestamp, String sourceInfoAppend) { final Sensor sensor = Sensor.currentSensor(); if (sensor == null) { @@ -1077,10 +1079,14 @@ public static synchronized BgReading bgReadingInsertFromG5(double calculated_val bgr.calculated_value = calculated_value; bgr.raw_data = SPECIAL_G5_PLACEHOLDER; // placeholder bgr.appendSourceInfo("G5 Native"); + if (sourceInfoAppend != null && sourceInfoAppend.length() > 0) { + bgr.appendSourceInfo(sourceInfoAppend); + } bgr.save(); if (JoH.ratelimit("sync wakelock", 15)) { final PowerManager.WakeLock linger = JoH.getWakeLock("G5 Insert", 4000); } + // TODO make Inevitable new Thread(() -> { JoH.threadSleep(3000); notifyAndSync(bgr); @@ -1094,7 +1100,7 @@ public static synchronized BgReading bgReadingInsertFromG5(double calculated_val public static void notifyAndSync(final BgReading bgr) { final boolean recent = bgr.isCurrent(); if (recent) { - xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al + Notifications.start(); // may not be needed as this is duplicated in handleNewBgReading // probably not wanted for G5 internal values? //bgr.injectNoise(true); // Add noise parameter for nightscout //bgr.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout @@ -1125,7 +1131,8 @@ public static void bgReadingInsertFromJson(String json, boolean do_notification, } bgr.save(); if (do_notification) { - xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al + Notifications.start(); // this may not be needed as it fires in handleNewBgReading + //xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), Home.get_follower()); // pebble and widget and follower } } else { @@ -1174,7 +1181,8 @@ public static void bgReadingInsertFromInt(int value, long timestamp, boolean do_ bgr.save(); bgr.find_slope(); if (do_notification) { - xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al + // xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al + Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading } BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), false, !do_notification); // pebble and widget } else { diff --git a/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/BgSendQueue.java b/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/BgSendQueue.java index c64ac09e04..edd7b17acd 100644 --- a/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/BgSendQueue.java +++ b/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/BgSendQueue.java @@ -123,6 +123,7 @@ public static void handleNewBgReading(BgReading bgReading, String operation_type wakeLock.acquire(120000); try { + Notifications.start(); CustomComplicationProviderService.refresh(); if (!is_follower) addToQueue(bgReading, operation_type); diff --git a/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java b/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java index ed54770eef..628e178eb1 100644 --- a/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java +++ b/wear/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/Notifications.java @@ -941,4 +941,13 @@ public static String unitsConvert2Disp(boolean doMgdl, double threshold) { return df.format(threshold / Constants.MMOLL_TO_MGDL); } + + // rate limited + public static void start() { + // TODO consider how inevitable task could change dynamic of this instead of rate limit + if (JoH.ratelimit("start-notifications",10)) { + JoH.startService(Notifications.class); + } + } + }