DroidFish: Fixed race condition when canceling a progress dialog.

This commit is contained in:
Peter Osterlund 2017-05-26 15:50:06 +02:00
parent 1141efca4f
commit 76d79ef7a8
3 changed files with 34 additions and 7 deletions

View File

@ -73,6 +73,7 @@ public class EditPGN extends ListActivity {
long lastModTime = -1;
Thread workThread = null;
boolean canceled = false;
boolean loadGame; // True when loading game, false when saving
String pgnToSave;
@ -101,6 +102,7 @@ public class EditPGN extends ListActivity {
Intent i = getIntent();
String action = i.getAction();
String fileName = i.getStringExtra("org.petero.droidfish.pathname");
canceled = false;
if (action.equals("org.petero.droidfish.loadFile")) {
pgnFile = new PGNFile(fileName);
loadGame = true;
@ -112,8 +114,13 @@ public class EditPGN extends ListActivity {
return;
runOnUiThread(new Runnable() {
public void run() {
if (canceled) {
setResult(RESULT_CANCELED);
finish();
} else {
lpgn.showList();
}
}
});
}
});
@ -168,7 +175,10 @@ public class EditPGN extends ListActivity {
return;
runOnUiThread(new Runnable() {
public void run() {
if (gamesInFile.size() == 0) {
if (canceled) {
setResult(RESULT_CANCELED);
finish();
} else if (gamesInFile.size() == 0) {
pgnFile.appendPGN(pgnToSave, getApplicationContext());
finish();
} else {
@ -327,6 +337,7 @@ public class EditPGN extends ListActivity {
progress.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
canceled = true;
Thread thr = workThread;
if (thr != null)
thr.interrupt();
@ -417,7 +428,6 @@ public class EditPGN extends ListActivity {
long modTime = new File(fileName).lastModified();
if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName))
return true;
pgnFile = new PGNFile(fileName);
Pair<GameInfoResult, ArrayList<GameInfo>> p = pgnFile.getGameInfo(this, progress);
if (p.first != GameInfoResult.OK) {
gamesInFile = new ArrayList<GameInfo>();

View File

@ -73,6 +73,7 @@ public class LoadFEN extends ListActivity {
private Thread workThread = null;
private CountDownLatch progressLatch = null;
private boolean canceled = false;
private ChessBoardPlay cb;
private Button okButton;
@ -117,8 +118,13 @@ public class LoadFEN extends ListActivity {
return;
runOnUiThread(new Runnable() {
public void run() {
if (canceled) {
setResult(RESULT_CANCELED);
finish();
} else {
lfen.showList();
}
}
});
}
});
@ -298,7 +304,9 @@ public class LoadFEN extends ListActivity {
super.onCancel(dialog);
Activity a = getActivity();
if (a instanceof LoadFEN) {
Thread thr = ((LoadFEN)a).workThread;
LoadFEN lf = (LoadFEN)a;
lf.canceled = true;
Thread thr = lf.workThread;
if (thr != null)
thr.interrupt();
}

View File

@ -78,6 +78,7 @@ public class LoadScid extends ListActivity {
private Thread workThread = null;
private CountDownLatch progressLatch = null;
private boolean canceled = false;
private boolean resultSentBack = false;
@ -131,6 +132,7 @@ public class LoadScid extends ListActivity {
String action = i.getAction();
fileName = i.getStringExtra("org.petero.droidfish.pathname");
resultSentBack = false;
canceled = false;
if (action.equals("org.petero.droidfish.loadScid")) {
progressLatch = new CountDownLatch(1);
showProgressDialog();
@ -149,8 +151,13 @@ public class LoadScid extends ListActivity {
return;
runOnUiThread(new Runnable() {
public void run() {
if (canceled) {
setResult(RESULT_CANCELED);
finish();
} else {
lpgn.showList();
}
}
});
}
});
@ -267,7 +274,9 @@ public class LoadScid extends ListActivity {
super.onCancel(dialog);
Activity a = getActivity();
if (a instanceof LoadScid) {
Thread thr = ((LoadScid)a).workThread;
LoadScid ls = (LoadScid)a;
ls.canceled = true;
Thread thr = ls.workThread;
if (thr != null)
thr.interrupt();
}