Silly me, of course fragments (and everything else) are recreated by Android all the time.

This commit is contained in:
Johannes Mockenhaupt 2017-07-17 16:15:51 +02:00
parent 29e7ea1966
commit e9fa9b1788
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 29 additions and 42 deletions

View file

@ -53,16 +53,9 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
lastCmdTime = (TextView) view.findViewById(R.id.combo_last_command_time); lastCmdTime = (TextView) view.findViewById(R.id.combo_last_command_time);
lastCmdResult = (TextView) view.findViewById(R.id.combo_last_command_result); lastCmdResult = (TextView) view.findViewById(R.id.combo_last_command_result);
status.setText("Initializing");
tbrPercentage.setText("");
tbrDurationRemaining.setText("");
tbrRate.setText("");
lastCmd.setText("");
lastCmdTime.setText("");
lastCmdResult.setText("");
refresh.setOnClickListener(this); refresh.setOnClickListener(this);
updateGUI();
return view; return view;
} }
@ -87,12 +80,10 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.combo_refresh: case R.id.combo_refresh:
status.setText("Refreshing");
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
getPlugin().refreshDataFromPump("User request"); getPlugin().refreshDataFromPump("User request");
updateGUI();
} }
}); });
thread.start(); thread.start();
@ -107,25 +98,27 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
@Override @Override
public void run() { public void run() {
status.setText(getPlugin().statusSummary); status.setText(getPlugin().statusSummary);
PumpState ps = getPlugin().pumpState; if (getPlugin().isInitialized()) {
boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100; PumpState ps = getPlugin().pumpState;
if (tbrActive) { boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100;
tbrPercentage.setText("" + ps.tbrPercent + "%"); if (tbrActive) {
tbrDurationRemaining.setText("" + ps.tbrRemainingDuration + " min"); tbrPercentage.setText("" + ps.tbrPercent + "%");
tbrRate.setText("" + ps.tbrRate + " U/h"); tbrDurationRemaining.setText("" + ps.tbrRemainingDuration + " min");
} else { tbrRate.setText("" + ps.tbrRate + " U/h");
tbrPercentage.setText("Default basal rate running"); } else {
tbrDurationRemaining.setText(""); tbrPercentage.setText("Default basal rate running");
tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h"); tbrDurationRemaining.setText("");
} tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h");
if (getPlugin().lastCmd != null) { }
lastCmd.setText("" + getPlugin().lastCmd); if (getPlugin().lastCmd != null) {
lastCmdTime.setText(ps.timestamp.toLocaleString()); lastCmd.setText("" + getPlugin().lastCmd);
lastCmdResult.setText(ps.errorMsg == null ? "Success" : ps.errorMsg); lastCmdTime.setText(ps.timestamp.toLocaleString());
} else { lastCmdResult.setText(ps.errorMsg == null ? "Success" : ps.errorMsg);
lastCmd.setText(""); } else {
lastCmdTime.setText(""); lastCmd.setText("");
lastCmdResult.setText(""); lastCmdTime.setText("");
lastCmdResult.setText("");
}
} }
} }
}); });

View file

@ -108,12 +108,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service)); ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service));
log.debug("ruffy serivce connected"); log.debug("ruffy serivce connected");
new Thread(new Runnable() {
@Override
public void run() {
log.debug("Querying pump for initial state");
}
}).start();
} }
@Override @Override
@ -136,9 +130,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
long lastAlarmTime = 0; long lastAlarmTime = 0;
while (true) { while (true) {
String errorMsg = pumpState.errorMsg; String errorMsg = pumpState.errorMsg;
long now = System.currentTimeMillis(); if (errorMsg != null) {
long fiveMinutesSinceLastAlarm = lastAlarmTime + (5 * 60 * 1000) + (15 * 1000); long now = System.currentTimeMillis();
if (errorMsg != null) long fiveMinutesSinceLastAlarm = lastAlarmTime + (5 * 60 * 1000) + (15 * 1000);
if (now > fiveMinutesSinceLastAlarm) { if (now > fiveMinutesSinceLastAlarm) {
log.warn("Pump is in error state, raising alert: " + errorMsg); log.warn("Pump is in error state, raising alert: " + errorMsg);
log.warn(" LastCmd: " + lastCmd); log.warn(" LastCmd: " + lastCmd);
@ -159,8 +153,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
lastAlarmTime = now; lastAlarmTime = now;
} else { } else {
log.warn("Pump still in error state, but alarm raised recently, so not triggering again: " + errorMsg); log.warn("Pump still in error state, but alarm raised recently, so not triggering again: " + errorMsg);
}
} else { } else {
log.debug("Pump state normal"); log.trace("Pump state normal");
} }
SystemClock.sleep(5 * 1000); SystemClock.sleep(5 * 1000);
} }
@ -257,9 +252,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
// TODO // consider initialized when the pump's state was initially fetched
// hm, lastCmdDate > 0, like the DanaR does it? return lastCmdTime.getTime() > 0;
return true; // scripter does this as needed; ruffyScripter != null;
} }
@Override @Override