From 54221541556c65e2d83ec34d1304a92ffdd94068 Mon Sep 17 00:00:00 2001
From: Johannes Mockenhaupt <git@jotomo.de>
Date: Tue, 27 Jun 2017 16:11:13 +0200
Subject: [PATCH] Extract method scheduleNextPoll.

---
 .../service/MedtronicCnlIntentService.java    | 48 ++++++++++---------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java
index 778c781..c6af1ca 100644
--- a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java
+++ b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java
@@ -315,34 +315,38 @@ public class MedtronicCnlIntentService extends IntentService {
                 sendToXDrip();
                 uploadToNightscout();
 
-                // smart polling and pump-sensor poll clash detection
-                long lastActualPollTime = timePollStarted;
-                if (timeLastGoodSGV > 0) {
-                    lastActualPollTime = timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
-                }
-                long nextActualPollTime = lastActualPollTime + POLL_PERIOD_MS;
-                long nextRequestedPollTime = lastActualPollTime + pollInterval;
-                if ((nextRequestedPollTime - System.currentTimeMillis()) < 10000L) {
-                    nextRequestedPollTime = nextActualPollTime;
-                }
-                // extended unavailable SGV may be due to clash with the current polling time
-                // while we wait for a good SGV event, polling is auto adjusted by offsetting the next poll based on miss count
-                if (dataStore.getUnavailableSGVCount() > 0) {
-                    if (timeLastGoodSGV == 0) {
-                        nextRequestedPollTime += POLL_PERIOD_MS / 5L; // if there is a uploader/sensor poll clash on startup then this will push the next attempt out by 60 seconds
-                    } else if (dataStore.getUnavailableSGVCount() > 2) {
-                        sendStatus("Warning: No SGV available from pump for " + dataStore.getUnavailableSGVCount() + " attempts");
-                        nextRequestedPollTime += ((long) ((dataStore.getUnavailableSGVCount() - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10)
-                    }
-                }
-                MedtronicCnlAlarmManager.setAlarm(nextRequestedPollTime);
-                sendStatus("Next poll due at: " + df.format(nextRequestedPollTime));
+                scheduleNextPoll(timePollStarted, timeLastGoodSGV, pollInterval, df);
             }
         } finally {
             MedtronicCnlAlarmReceiver.completeWakefulIntent(intent);
         }
     }
 
+    private void scheduleNextPoll(long timePollStarted, long timeLastGoodSGV, long pollInterval, DateFormat df) {
+        // smart polling and pump-sensor poll clash detection
+        long lastActualPollTime = timePollStarted;
+        if (timeLastGoodSGV > 0) {
+            lastActualPollTime = timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
+        }
+        long nextActualPollTime = lastActualPollTime + POLL_PERIOD_MS;
+        long nextRequestedPollTime = lastActualPollTime + pollInterval;
+        if ((nextRequestedPollTime - System.currentTimeMillis()) < 10000L) {
+            nextRequestedPollTime = nextActualPollTime;
+        }
+        // extended unavailable SGV may be due to clash with the current polling time
+        // while we wait for a good SGV event, polling is auto adjusted by offsetting the next poll based on miss count
+        if (dataStore.getUnavailableSGVCount() > 0) {
+            if (timeLastGoodSGV == 0) {
+                nextRequestedPollTime += POLL_PERIOD_MS / 5L; // if there is a uploader/sensor poll clash on startup then this will push the next attempt out by 60 seconds
+            } else if (dataStore.getUnavailableSGVCount() > 2) {
+                sendStatus("Warning: No SGV available from pump for " + dataStore.getUnavailableSGVCount() + " attempts");
+                nextRequestedPollTime += ((long) ((dataStore.getUnavailableSGVCount() - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10)
+            }
+        }
+        MedtronicCnlAlarmManager.setAlarm(nextRequestedPollTime);
+        sendStatus("Next poll due at: " + df.format(nextRequestedPollTime));
+    }
+
     private boolean acquireUsbDevice() {
         if (!hasUsbHostFeature()) {
             sendStatus("It appears that this device doesn't support USB OTG.");
-- 
GitLab