allow only 1 updategui running

This commit is contained in:
Milos Kozak 2017-06-06 10:56:26 +02:00
parent 17f59df597
commit f93090b012

View file

@ -189,6 +189,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledUpdate = null; private static ScheduledFuture<?> scheduledUpdate = null;
private static UpdateGUIAsyncTask updateGUIAsyncTask;
public OverviewFragment() { public OverviewFragment() {
super(); super();
if (sHandlerThread == null) { if (sHandlerThread == null) {
@ -866,11 +868,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (scheduledUpdate != null) if (scheduledUpdate != null)
scheduledUpdate.cancel(false); scheduledUpdate.cancel(false);
Runnable task = new UpdateRunnable(); Runnable task = new UpdateRunnable();
final int msec = 2000; final int msec = 1000;
scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS); scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS);
} }
private class UpdateGUIAsyncTask extends AsyncTask<String, Void, String> { private class UpdateGUIAsyncTask extends AsyncTask<String, Void, String> {
private String from;
BgReading actualBG = DatabaseHelper.actualBg(); BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg(); BgReading lastBG = DatabaseHelper.lastBg();
@ -932,9 +935,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
Integer numOfHorizLines; Integer numOfHorizLines;
public UpdateGUIAsyncTask(String from) {
super();
this.from = from;
}
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
log.debug("UpdateGUIAsyncTask onPreExecute"); log.debug("UpdateGUIAsyncTask onPreExecute from: " + from);
if (getActivity() == null) if (getActivity() == null)
return; return;
@ -1230,7 +1238,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
@Override @Override
protected String doInBackground(String... params) { protected String doInBackground(String... params) {
log.debug("UpdateGUIAsyncTask doInBackground"); log.debug("UpdateGUIAsyncTask doInBackground from: " + from);
// IOB // IOB
MainApp.getConfigBuilder().updateTotalIOBTreatments(); MainApp.getConfigBuilder().updateTotalIOBTreatments();
@ -1492,7 +1500,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
log.debug("UpdateGUIAsyncTask onPostExecute"); log.debug("UpdateGUIAsyncTask onPostExecute from: " + from);
// IOB // IOB
if (getActivity() == null) if (getActivity() == null)
return; return;
@ -1591,16 +1599,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
}); });
if (updating != null) if (updating != null)
updating.setVisibility(View.GONE); updating.setVisibility(View.GONE);
// release for next run
log.debug("UpdateGUIAsyncTask finishing onPostExecute from: " + from);
updateGUIAsyncTask = null;
} }
} }
public void updateGUIAsync(String from) {
new UpdateGUIAsyncTask().execute(from);
}
@SuppressLint("SetTextI18n")
public void updateGUI(String from) { public void updateGUI(String from) {
log.debug("updateGUI entered from: " + from);
if (MainApp.getConfigBuilder().getProfile() == null) {// app not initialized yet if (MainApp.getConfigBuilder().getProfile() == null) {// app not initialized yet
pumpStatusView.setText(R.string.noprofileset); pumpStatusView.setText(R.string.noprofileset);
pumpStatusLayout.setVisibility(View.VISIBLE); pumpStatusLayout.setVisibility(View.VISIBLE);
@ -1610,7 +1615,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
pumpStatusLayout.setVisibility(View.GONE); pumpStatusLayout.setVisibility(View.GONE);
loopStatusLayout.setVisibility(View.VISIBLE); loopStatusLayout.setVisibility(View.VISIBLE);
updateGUIAsync(from); if (updateGUIAsyncTask != null) {
log.debug("Update already running. From: " + from);
return;
}
log.debug("updateGUI entered from: " + from);
updateGUIAsyncTask = new UpdateGUIAsyncTask(from);
updateGUIAsyncTask.execute(from);
} }
public double getNearestBg(long date, List<BgReading> bgReadingsArray) { public double getNearestBg(long date, List<BgReading> bgReadingsArray) {