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

View File

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

View File

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