Skip to content
Snippets Groups Projects
Commit b0629605 authored by Volker Richert's avatar Volker Richert
Browse files

Move more static stuff into a configuration & data storage

parent 138978ba
No related branches found
No related tags found
No related merge requests found
......@@ -87,20 +87,19 @@ 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;
private DataStore dataStore = DataStore.getInstance();
private ConfigurationStore configurationStore = ConfigurationStore.getInstance();
private static long activePumpMac;
// private static long activePumpMac;
// private static boolean mmolxl;
// private static boolean mmolxlDecimals;
private int chartZoom = 3;
private boolean hasZoomedChart = false;
private NumberFormat sgvFormatter;
private static boolean mmolxl;
private static boolean mmolxlDecimals;
boolean mEnableCgmService = true;
SharedPreferences prefs = null;
private boolean mEnableCgmService = true;
private SharedPreferences prefs = null;
private PumpInfo mActivePump;
private TextView mTextViewLog; // This will eventually move to a status page.
private GraphView mChart;
......@@ -143,9 +142,10 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
}
public static String strFormatSGV(float sgvValue) {
if (mmolxl) {
ConfigurationStore configurationStore = ConfigurationStore.getInstance();
if (configurationStore.isMmolxl()) {
NumberFormat sgvFormatter;
if (mmolxlDecimals) {
if (configurationStore.isMmolxlDecimals()) {
sgvFormatter = new DecimalFormat("0.00");
} else {
sgvFormatter = new DecimalFormat("0.0");
......@@ -185,11 +185,11 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
configurationStore.setReducePollOnPumpAway(prefs.getBoolean("doublePollOnPumpAway", false));
chartZoom = Integer.parseInt(prefs.getString("chartZoom", "3"));
mmolxl = prefs.getBoolean("mmolxl", false);
mmolxlDecimals = prefs.getBoolean("mmolDecimals", false);
configurationStore.setMmolxl(prefs.getBoolean("mmolxl", false));
configurationStore.setMmolxlDecimals(prefs.getBoolean("mmolDecimals", false));
if (mmolxl) {
if (mmolxlDecimals)
if (configurationStore.isMmolxl()) {
if (configurationStore.isMmolxlDecimals())
sgvFormatter = new DecimalFormat("0.00");
else
sgvFormatter = new DecimalFormat("0.0");
......@@ -539,10 +539,10 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
startCgmService();
}
} else if (key.equals("mmolxl") || key.equals("mmolDecimals")) {
mmolxl = sharedPreferences.getBoolean("mmolxl", false);
mmolxlDecimals = sharedPreferences.getBoolean("mmolDecimals", false);
if (mmolxl) {
if (mmolxlDecimals)
configurationStore.setMmolxl(sharedPreferences.getBoolean("mmolxl", false));
configurationStore.setMmolxlDecimals(sharedPreferences.getBoolean("mmolDecimals", false));
if (configurationStore.isMmolxl()) {
if (configurationStore.isMmolxlDecimals())
sgvFormatter = new DecimalFormat("0.00");
else
sgvFormatter = new DecimalFormat("0.0");
......@@ -606,11 +606,8 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
}
}
public static void setActivePumpMac(long pumpMac) {
activePumpMac = pumpMac;
}
private PumpInfo getActivePump() {
long activePumpMac = dataStore.getActivePumpMac();
if (activePumpMac != 0L && (mActivePump == null || !mActivePump.isValid() || mActivePump.getPumpMac() != activePumpMac)) {
if (mActivePump != null) {
// remove listener on old pump
......@@ -620,7 +617,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
PumpInfo pump = mRealm
.where(PumpInfo.class)
.equalTo("pumpMac", MainActivity.activePumpMac)
.equalTo("pumpMac", activePumpMac)
.findFirst();
if (pump != null && pump.isValid()) {
......@@ -741,7 +738,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
TextView textViewBg = (TextView) findViewById(R.id.textview_bg);
TextView textViewBgTime = (TextView) findViewById(R.id.textview_bg_time);
TextView textViewUnits = (TextView) findViewById(R.id.textview_units);
if (mmolxl) {
if (configurationStore.isMmolxl()) {
textViewUnits.setText(R.string.text_unit_mmolxl);
} else {
textViewUnits.setText(R.string.text_unit_mgxdl);
......@@ -766,7 +763,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
if (pumpStatusData != null) {
String sgvString;
if (mmolxl) {
if (configurationStore.isMmolxl()) {
float fBgValue = (float) pumpStatusData.getSgv();
sgvString = sgvFormatter.format(fBgValue / MMOLXLFACTOR);
Log.d(TAG, sgvString + " mmol/L");
......@@ -829,7 +826,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
mChart.getViewport().setMinX(left);
mChart.getViewport().setYAxisBoundsManual(true);
if (mmolxl) {
if (configurationStore.isMmolxl()) {
mChart.getViewport().setMinY(80 / MMOLXLFACTOR);
mChart.getViewport().setMaxY(120 / MMOLXLFACTOR);
} else {
......@@ -848,7 +845,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
// turn your data into Entry objects
int sgv = pumpStatus.getSgv();
if (mmolxl) {
if (configurationStore.isMmolxl()) {
entries[pos++] = new DataPoint(pumpStatus.getEventDate(), (float) pumpStatus.getSgv() / MMOLXLFACTOR);
} else {
entries[pos++] = new DataPoint(pumpStatus.getEventDate(), (float) pumpStatus.getSgv());
......@@ -886,6 +883,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
@Override
public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) {
double sgv = dataPoint.getY();
boolean mmolxl = configurationStore.isMmolxl();
if (sgv < (mmolxl?4.5:80))
paint.setColor(Color.RED);
else if (sgv <= (mmolxl?10:180))
......
......@@ -228,7 +228,8 @@ public class MedtronicCnlIntentService extends IntentService {
Log.i(TAG, "Could not communicate with the 640g. Are you near the pump?");
pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L); // reduce polling interval to half until pump is available
} else {
setActivePumpMac(pumpMAC);
dataStore.setActivePumpMac(pumpMAC);
activePump.setLastRadioChannel(radioChannel);
sendStatus(String.format(Locale.getDefault(), "Connected on channel %d RSSI: %d%%", (int) radioChannel, cnlReader.getPumpSession().getRadioRSSIpercentage()));
Log.d(TAG, String.format("Connected to Contour Next Link on channel %d.", (int) radioChannel));
......@@ -365,10 +366,6 @@ public class MedtronicCnlIntentService extends IntentService {
}
}
private void setActivePumpMac(long pumpMAC) {
MainActivity.setActivePumpMac(pumpMAC);
}
// reliable wake alarm manager wake up for all android versions
public static void wakeUpIntent(Context context, long wakeTime, PendingIntent pendingIntent) {
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
......
......@@ -14,6 +14,8 @@ public class ConfigurationStore {
private boolean reducePollOnPumpAway = false;
private long pollInterval = MedtronicCnlIntentService.POLL_PERIOD_MS;
private long lowBatteryPollInterval = MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS;
private boolean mmolxl;
private boolean mmolxlDecimals;
public static ConfigurationStore getInstance() {
if (ConfigurationStore.instance == null) {
......@@ -47,4 +49,19 @@ public class ConfigurationStore {
this.lowBatteryPollInterval = lowBatteryPollInterval;
}
public boolean isMmolxl() {
return mmolxl;
}
public void setMmolxl(boolean mmolxl) {
this.mmolxl = mmolxl;
}
public boolean isMmolxlDecimals() {
return mmolxlDecimals;
}
public void setMmolxlDecimals(boolean mmolxlDecimals) {
this.mmolxlDecimals = mmolxlDecimals;
}
}
......@@ -15,6 +15,7 @@ public class DataStore {
private PumpStatusEvent lastPumpStatus;
private int uplooaderBatteryLevel = 0;
private int unavailableSGVCount = 0;
private long activePumpMac = 0;
private DataStore() {}
......@@ -62,4 +63,12 @@ public class DataStore {
public void setUnavailableSGVCount(int unavailableSGVCount) {
this.unavailableSGVCount = unavailableSGVCount;
}
public long getActivePumpMac() {
return activePumpMac;
}
public void setActivePumpMac(long activePumpMac) {
this.activePumpMac = activePumpMac;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment