From 138978ba524272cd7a682a04603ed024dcca5db5 Mon Sep 17 00:00:00 2001
From: Volker Richert <v.richert@addmore.de>
Date: Thu, 30 Mar 2017 12:10:34 +0200
Subject: [PATCH] Move all the public static stuff into a configuration & data
 storage

---
 .../android/medtronic/MainActivity.java       | 47 ++++++++------
 .../message/PumpStatusResponseMessage.java    |  5 +-
 .../service/MedtronicCnlAlarmManager.java     |  3 +-
 .../service/MedtronicCnlIntentService.java    | 58 ++++++++++-------
 .../NightscoutUploadIntentService.java        |  5 +-
 .../android/utils/ConfigurationStore.java     | 50 ++++++++++++++
 .../nightscout/android/utils/DataStore.java   | 65 +++++++++++++++++++
 .../XDripPlusUploadIntentService.java         |  4 +-
 8 files changed, 184 insertions(+), 53 deletions(-)
 create mode 100644 app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java
 create mode 100644 app/src/main/java/info/nightscout/android/utils/DataStore.java

diff --git a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java
index 6538ae5..92ec218 100644
--- a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java
+++ b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java
@@ -75,6 +75,8 @@ import info.nightscout.android.model.medtronicNg.PumpInfo;
 import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
 import info.nightscout.android.settings.SettingsActivity;
 import info.nightscout.android.upload.nightscout.NightscoutUploadIntentService;
+import info.nightscout.android.utils.ConfigurationStore;
+import info.nightscout.android.utils.DataStore;
 import io.realm.Realm;
 import io.realm.RealmChangeListener;
 import io.realm.RealmResults;
@@ -85,10 +87,9 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
     private static final String TAG = MainActivity.class.getSimpleName();
     public static final float MMOLXLFACTOR = 18.016f;
 
-    public static int batLevel = 0;
-    public static boolean reducePollOnPumpAway = false;
-    public static long pollInterval = MedtronicCnlIntentService.POLL_PERIOD_MS;
-    public static long lowBatteryPollInterval = MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS;
+    //public static int batLevel = 0;
+    private DataStore dataStore = DataStore.getInstance();
+    private ConfigurationStore configurationStore = ConfigurationStore.getInstance();
 
     private static long activePumpMac;
     private int chartZoom = 3;
@@ -98,10 +99,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
     private static boolean mmolxl;
     private static boolean mmolxlDecimals;
 
-    public static long timeLastGoodSGV = 0;
-    public static short pumpBattery = 0;
-    public static int countUnavailableSGV = 0;
-
     boolean mEnableCgmService = true;
     SharedPreferences prefs = null;
     private PumpInfo mActivePump;
@@ -122,22 +119,23 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
      */
     public static long getNextPoll(PumpStatusEvent pumpStatusData) {
         long nextPoll = pumpStatusData.getEventDate().getTime() + pumpStatusData.getPumpTimeOffset(),
-            now = System.currentTimeMillis();
+            now = System.currentTimeMillis(),
+            pollInterval = ConfigurationStore.getInstance().getPollInterval();
 
         // align to next poll slot
         if (nextPoll + 2 * 60 * 60 * 1000 < now) { // last event more than 2h old -> could be a calibration
             nextPoll = System.currentTimeMillis() + 1000;
         } else {
             // align to poll interval
-            nextPoll += (((now - nextPoll) / MainActivity.pollInterval)) * MainActivity.pollInterval
+            nextPoll += (((now - nextPoll) / pollInterval)) * pollInterval
                     + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS;
             if (pumpStatusData.getBatteryPercentage() > 25) {
                 // poll every 5 min
-                nextPoll += MainActivity.pollInterval;
+                nextPoll += pollInterval;
             } else {
                 // if pump battery seems to be empty reduce polling to save battery (every 15 min)
                 //TODO add message & document it
-                nextPoll += MainActivity.lowBatteryPollInterval;
+                nextPoll += ConfigurationStore.getInstance().getLowBatteryPollInterval();
             }
         }
 
@@ -164,6 +162,12 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
         super.onCreate(savedInstanceState);
 
         mRealm = Realm.getDefaultInstance();
+
+        RealmResults<PumpStatusEvent> data = mRealm.where(PumpStatusEvent.class)
+                .findAllSorted("eventDate", Sort.DESCENDING);
+        if (data.size() > 0)
+            dataStore.setLastPumpStatus(data.first());
+
         mNightscoutUploadService = new Intent(this, NightscoutUploadIntentService.class);
 
         setContentView(R.layout.activity_main);
@@ -176,9 +180,10 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
         }
 
         // setup preferences
-        MainActivity.pollInterval = Long.parseLong(prefs.getString("pollInterval", Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS)));
-        MainActivity.lowBatteryPollInterval = Long.parseLong(prefs.getString("lowBatPollInterval", Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS)));
-        MainActivity.reducePollOnPumpAway = prefs.getBoolean("doublePollOnPumpAway", false);
+        configurationStore.setPollInterval(Long.parseLong(prefs.getString("pollInterval", Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS))));
+        configurationStore.setLowBatteryPollInterval(Long.parseLong(prefs.getString("lowBatPollInterval", Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS))));
+        configurationStore.setReducePollOnPumpAway(prefs.getBoolean("doublePollOnPumpAway", false));
+
         chartZoom = Integer.parseInt(prefs.getString("chartZoom", "3"));
         mmolxl = prefs.getBoolean("mmolxl", false);
         mmolxlDecimals = prefs.getBoolean("mmolDecimals", false);
@@ -546,13 +551,13 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
             }
             refreshDisplay();
         } else if (key.equals("pollInterval")) {
-            MainActivity.pollInterval = Long.parseLong(sharedPreferences.getString("pollInterval",
-                    Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS)));
+            configurationStore.setPollInterval(Long.parseLong(sharedPreferences.getString("pollInterval",
+                    Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS))));
         } else if (key.equals("lowBatPollInterval")) {
-            MainActivity.lowBatteryPollInterval = Long.parseLong(sharedPreferences.getString("lowBatPollInterval",
-                    Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS)));
+            configurationStore.setLowBatteryPollInterval(Long.parseLong(sharedPreferences.getString("lowBatPollInterval",
+                    Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS))));
         } else if (key.equals("doublePollOnPumpAway")) {
-            MainActivity.reducePollOnPumpAway = sharedPreferences.getBoolean("doublePollOnPumpAway", false);
+            configurationStore.setReducePollOnPumpAway(sharedPreferences.getBoolean("doublePollOnPumpAway", false));
         } else if (key.equals("chartZoom")) {
             chartZoom = Integer.parseInt(sharedPreferences.getString("chartZoom", "3"));
             hasZoomedChart = false;
@@ -970,7 +975,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
             if (arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)
                     || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)
                     || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {
-                batLevel = arg1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
+                dataStore.setUplooaderBatteryLevel(arg1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
             }
         }
     }
diff --git a/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java b/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java
index f97a739..1d3862b 100644
--- a/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java
+++ b/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java
@@ -14,6 +14,7 @@ import info.nightscout.android.medtronic.exception.ChecksumException;
 import info.nightscout.android.medtronic.exception.EncryptionException;
 import info.nightscout.android.medtronic.exception.UnexpectedMessageException;
 import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
+import info.nightscout.android.utils.DataStore;
 import info.nightscout.android.utils.HexDump;
 
 /**
@@ -197,6 +198,8 @@ public class PumpStatusResponseMessage extends MedtronicSendMessageResponseMessa
 
         // Recent Bolus Wizard BGL
         pumpRecord.setRecentBolusWizard(recentBolusWizard);
-        pumpRecord.setBolusWizardBGL(bolusWizardBGL); // In mg/DL
+        if (recentBolusWizard && activeInsulin > DataStore.getInstance().getLastPumpStatus().getActiveInsulin()) {  // there is a BolusWizard usage & the IOB increaseed
+            pumpRecord.setBolusWizardBGL(bolusWizardBGL); // In mg/DL
+        }
     }
 }
diff --git a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java
index 0873710..b4f2e15 100644
--- a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java
+++ b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java
@@ -11,6 +11,7 @@ import android.util.Log;
 import java.util.Date;
 
 import info.nightscout.android.medtronic.MainActivity;
+import info.nightscout.android.utils.ConfigurationStore;
 
 /**
  * Created by lgoedhart on 14/07/2016.
@@ -81,7 +82,7 @@ public class MedtronicCnlAlarmManager {
     // restarting the alarm after MedtronicCnlIntentService.POLL_PERIOD_MS from now
     public static void restartAlarm() {
         //setAlarmAfterMillis(MainActivity.pollInterval + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS);
-        setAlarmAfterMillis(MainActivity.pollInterval); // grace already accounted for when using current intent time to set default restart
+        setAlarmAfterMillis(ConfigurationStore.getInstance().getPollInterval()); // grace already accounted for when using current intent time to set default restart
     }
 
     // Cancel the alarm.
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 3d9e0eb..4f87955 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
@@ -35,6 +35,8 @@ import info.nightscout.android.model.medtronicNg.ContourNextLinkInfo;
 import info.nightscout.android.model.medtronicNg.PumpInfo;
 import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
 import info.nightscout.android.upload.nightscout.NightscoutUploadReceiver;
+import info.nightscout.android.utils.ConfigurationStore;
+import info.nightscout.android.utils.DataStore;
 import info.nightscout.android.xdrip_plus.XDripPlusUploadReceiver;
 import io.realm.Realm;
 import io.realm.RealmResults;
@@ -48,10 +50,13 @@ public class MedtronicCnlIntentService extends IntentService {
     // Number of additional seconds to wait after the next expected CGM poll, so that we don't interfere with CGM radio comms.
     public final static long POLL_GRACE_PERIOD_MS = 30000L;
     private static final String TAG = MedtronicCnlIntentService.class.getSimpleName();
+
     private UsbHidDriver mHidDevice;
     private Context mContext;
     private NotificationManagerCompat nm;
     private UsbManager mUsbManager;
+    private DataStore dataStore = DataStore.getInstance();
+    private ConfigurationStore configurationStore = ConfigurationStore.getInstance();
 
     public MedtronicCnlIntentService() {
         super(MedtronicCnlIntentService.class.getName());
@@ -100,10 +105,15 @@ public class MedtronicCnlIntentService extends IntentService {
     protected void onHandleIntent(Intent intent) {
         Log.d(TAG, "onHandleIntent called");
 
-        long timePollStarted = System.currentTimeMillis();
-        long timePollExpected = timePollStarted;
-        if (MainActivity.timeLastGoodSGV != 0) {
-            timePollExpected = MainActivity.timeLastGoodSGV + POLL_PERIOD_MS + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((timePollStarted - 1000L - (MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
+        long timePollStarted = System.currentTimeMillis(),
+                timePollExpected = timePollStarted,
+                timeLastGoodSGV = dataStore.getLastPumpStatus().getEventDate().getTime();
+
+        short pumpBatteryLevel = dataStore.getLastPumpStatus().getBatteryPercentage();
+
+
+        if (timeLastGoodSGV != 0) {
+            timePollExpected = timeLastGoodSGV + POLL_PERIOD_MS + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((timePollStarted - 1000L - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
         }
 
         // avoid polling when too close to sensor-pump comms
@@ -114,9 +124,9 @@ public class MedtronicCnlIntentService extends IntentService {
             return;
         }
 
-        long pollInterval = MainActivity.pollInterval;
-        if ((MainActivity.pumpBattery > 0) && (MainActivity.pumpBattery <= 25)) {
-            pollInterval = MainActivity.lowBatteryPollInterval;
+        long pollInterval = configurationStore.getPollInterval();
+        if ((pumpBatteryLevel > 0) && (pumpBatteryLevel <= 25)) {
+            pollInterval = configurationStore.getLowBatteryPollInterval();
         }
 
         if (!hasUsbHostFeature()) {
@@ -216,7 +226,7 @@ public class MedtronicCnlIntentService extends IntentService {
                 if (radioChannel == 0) {
                     sendStatus("Could not communicate with the 640g. Are you near the pump?");
                     Log.i(TAG, "Could not communicate with the 640g. Are you near the pump?");
-                    pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L); // reduce polling interval to half until pump is available
+                    pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L); // reduce polling interval to half until pump is available
                 } else {
                     setActivePumpMac(pumpMAC);
                     activePump.setLastRadioChannel(radioChannel);
@@ -242,7 +252,6 @@ public class MedtronicCnlIntentService extends IntentService {
                     cnlReader.updatePumpStatus(pumpRecord);
 
                     if (pumpRecord.getSgv() != 0) {
-
                         String offsetSign = "";
                         if (pumpOffset > 0) {
                             offsetSign = "+";
@@ -250,14 +259,14 @@ public class MedtronicCnlIntentService extends IntentService {
                         sendStatus("SGV: " + MainActivity.strFormatSGV(pumpRecord.getSgv()) + "  At: " + df.format(pumpRecord.getEventDate().getTime()) + "  Pump: " + offsetSign + (pumpOffset / 1000L) + "sec");  //note: event time is currently stored with offset
 
                         // Check if pump sent old event when new expected and schedule a re-poll
-                        if (((pumpRecord.getEventDate().getTime() - MainActivity.timeLastGoodSGV) < 5000L) && ((timePollExpected - timePollStarted) < 5000L)) {
+                        if (((pumpRecord.getEventDate().getTime() - dataStore.getLastPumpStatus().getEventDate().getTime()) < 5000L) && ((timePollExpected - timePollStarted) < 5000L)) {
                             pollInterval = 90000L; // polling interval set to 90 seconds
                             sendStatus("Pump sent old SGV event, re-polling...");
                         }
 
-                        MainActivity.timeLastGoodSGV =  pumpRecord.getEventDate().getTime(); // track last good sgv event time
-                        MainActivity.pumpBattery =  pumpRecord.getBatteryPercentage(); // track pump battery
-                        MainActivity.countUnavailableSGV = 0; // reset unavailable sgv count
+                        //MainActivity.timeLastGoodSGV =  pumpRecord.getEventDate().getTime(); // track last good sgv event time
+                        //MainActivity.pumpBattery =  pumpRecord.getBatteryPercentage(); // track pump battery
+                        dataStore.clearUnavailableSGVCount(); // reset unavailable sgv count
 
                         // Check that the record doesn't already exist before committing
                         RealmResults<PumpStatusEvent> checkExistingRecords = activePump.getPumpHistory()
@@ -269,13 +278,12 @@ public class MedtronicCnlIntentService extends IntentService {
                         // There should be the 1 record we've already added in this transaction.
                         if (checkExistingRecords.size() == 0) {
                             activePump.getPumpHistory().add(pumpRecord);
+                            dataStore.setLastPumpStatus(pumpRecord);
                         }
 
-                        Log.d(TAG, "history reading size: " + activePump.getPumpHistory().size());
-                        Log.d(TAG, "history reading date: " + activePump.getPumpHistory().last().getEventDate());
                     } else {
                         sendStatus("SGV: unavailable from pump");
-                        MainActivity.countUnavailableSGV ++; // poll clash detection
+                        dataStore.incUnavailableSGVCount(); // poll clash detection
                     }
 
                     realm.commitTransaction();
@@ -286,11 +294,11 @@ public class MedtronicCnlIntentService extends IntentService {
             } catch (UnexpectedMessageException e) {
                 Log.e(TAG, "Unexpected Message", e);
                 sendStatus("Communication Error: " + e.getMessage());
-                pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L);
+                pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L);
             } catch (TimeoutException e) {
                 Log.e(TAG, "Timeout communicating with the Contour Next Link.", e);
                 sendStatus("Timeout communicating with the Contour Next Link.");
-                pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L);
+                pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L);
             } catch (NoSuchAlgorithmException e) {
                 Log.e(TAG, "Could not determine CNL HMAC", e);
                 sendStatus("Error connecting to Contour Next Link: Hashing error.");
@@ -331,8 +339,8 @@ public class MedtronicCnlIntentService extends IntentService {
 
             // smart polling and pump-sensor poll clash detection
             long lastActualPollTime = timePollStarted;
-            if (MainActivity.timeLastGoodSGV > 0) {
-                lastActualPollTime = MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS));
+            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;
@@ -341,13 +349,13 @@ public class MedtronicCnlIntentService extends IntentService {
             }
             // 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 (MainActivity.countUnavailableSGV > 0) {
-                if (MainActivity.timeLastGoodSGV == 0) {
+            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 (MainActivity.countUnavailableSGV > 2) {
-                    sendStatus("Warning: No SGV available from pump for " + MainActivity.countUnavailableSGV + " attempts");
-                    nextRequestedPollTime += ((long) ((MainActivity.countUnavailableSGV - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10)
+                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);
diff --git a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
index 51174e3..74afe68 100644
--- a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
+++ b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
@@ -11,8 +11,8 @@ import android.support.v4.content.LocalBroadcastManager;
 import android.util.Log;
 
 import info.nightscout.android.R;
-import info.nightscout.android.medtronic.MainActivity;
 import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
+import info.nightscout.android.utils.DataStore;
 import io.realm.Realm;
 import io.realm.RealmResults;
 
@@ -67,9 +67,8 @@ public class NightscoutUploadIntentService extends IntentService {
                     Log.i(TAG, String.format("Starting upload of %s record using a REST API", records.size()));
                     String urlSetting = prefs.getString(mContext.getString(R.string.preference_nightscout_url), "");
                     String secretSetting = prefs.getString(mContext.getString(R.string.preference_api_secret), "YOURAPISECRET");
-                    int uploaderBatteryLevel = MainActivity.batLevel;
                     Boolean uploadSuccess = mNightScoutUpload.doRESTUpload(urlSetting,
-                            secretSetting, uploaderBatteryLevel, records);
+                            secretSetting, DataStore.getInstance().getUplooaderBatteryLevel(), records);
                     if (uploadSuccess) {
                         mRealm.beginTransaction();
                         for (PumpStatusEvent updateRecord : records) {
diff --git a/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java b/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java
new file mode 100644
index 0000000..d9b492b
--- /dev/null
+++ b/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java
@@ -0,0 +1,50 @@
+package info.nightscout.android.utils;
+
+
+import info.nightscout.android.medtronic.service.MedtronicCnlIntentService;
+import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
+
+/**
+ * Created by volker on 30.03.2017.
+ */
+
+public class ConfigurationStore {
+    private static ConfigurationStore instance;
+
+    private boolean reducePollOnPumpAway = false;
+    private long pollInterval = MedtronicCnlIntentService.POLL_PERIOD_MS;
+    private long lowBatteryPollInterval = MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS;
+
+    public static ConfigurationStore getInstance() {
+        if (ConfigurationStore.instance == null) {
+            instance = new ConfigurationStore();
+        }
+
+        return instance;
+    }
+
+    public boolean isReducePollOnPumpAway() {
+        return reducePollOnPumpAway;
+    }
+
+    public void setReducePollOnPumpAway(boolean reducePollOnPumpAway) {
+        this.reducePollOnPumpAway = reducePollOnPumpAway;
+    }
+
+    public long getPollInterval() {
+        return pollInterval;
+    }
+
+    public void setPollInterval(long pollInterval) {
+        this.pollInterval = pollInterval;
+    }
+
+    public long getLowBatteryPollInterval() {
+        return lowBatteryPollInterval;
+    }
+
+    public void setLowBatteryPollInterval(long lowBatteryPollInterval) {
+        this.lowBatteryPollInterval = lowBatteryPollInterval;
+    }
+
+}
diff --git a/app/src/main/java/info/nightscout/android/utils/DataStore.java b/app/src/main/java/info/nightscout/android/utils/DataStore.java
new file mode 100644
index 0000000..c065fad
--- /dev/null
+++ b/app/src/main/java/info/nightscout/android/utils/DataStore.java
@@ -0,0 +1,65 @@
+package info.nightscout.android.utils;
+
+
+import java.util.Date;
+
+import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
+
+/**
+ * Created by volker on 30.03.2017.
+ */
+
+public class DataStore {
+    private static DataStore instance;
+
+    private PumpStatusEvent lastPumpStatus;
+    private int uplooaderBatteryLevel = 0;
+    private int unavailableSGVCount = 0;
+
+    private DataStore() {}
+
+    public static DataStore getInstance() {
+        if (DataStore.instance == null) {
+            instance = new DataStore();
+
+            // set some initial dummy values
+            PumpStatusEvent dummyStatus = new PumpStatusEvent();
+            dummyStatus.setEventDate(new Date(0));
+
+            instance.setLastPumpStatus(dummyStatus);
+        }
+
+        return instance;
+    }
+
+    public PumpStatusEvent getLastPumpStatus() {
+        return lastPumpStatus;
+    }
+
+    public void setLastPumpStatus(PumpStatusEvent lastPumpStatus) {
+        this.lastPumpStatus = lastPumpStatus;
+    }
+
+    public int getUplooaderBatteryLevel() {
+        return uplooaderBatteryLevel;
+    }
+
+    public void setUplooaderBatteryLevel(int uplooaderBatteryLevel) {
+        this.uplooaderBatteryLevel = uplooaderBatteryLevel;
+    }
+
+    public int getUnavailableSGVCount() {
+        return unavailableSGVCount;
+    }
+
+    public int incUnavailableSGVCount() {
+        return unavailableSGVCount++;
+    }
+
+    public void clearUnavailableSGVCount() {
+        this.unavailableSGVCount = 0;
+    }
+    public void setUnavailableSGVCount(int unavailableSGVCount) {
+        this.unavailableSGVCount = unavailableSGVCount;
+    }
+}
diff --git a/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java b/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java
index eef010f..d27f95e 100644
--- a/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java
+++ b/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java
@@ -16,9 +16,9 @@ import java.text.SimpleDateFormat;
 import java.util.List;
 import java.util.Locale;
 
-import info.nightscout.android.medtronic.MainActivity;
 import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
 import info.nightscout.android.upload.nightscout.serializer.EntriesSerializer;
+import info.nightscout.android.utils.DataStore;
 import io.realm.Realm;
 import io.realm.RealmResults;
 import io.realm.Sort;
@@ -116,7 +116,7 @@ public class XDripPlusUploadIntentService extends IntentService {
 
     private void addDeviceStatus(JSONArray devicestatusArray, PumpStatusEvent record) throws Exception {
         JSONObject json = new JSONObject();
-        json.put("uploaderBattery", MainActivity.batLevel);
+        json.put("uploaderBattery", DataStore.getInstance().getUplooaderBatteryLevel());
         json.put("device", record.getDeviceName());
         json.put("created_at", ISO8601_DATE_FORMAT.format(record.getPumpDate()));
 
-- 
GitLab