Remove progress dialog when parsing EPD file

Current devices are so fast that it is hard to see this dialog even if
the file contains 100000 positions.
This commit is contained in:
Peter Osterlund 2020-03-15 08:15:14 +01:00
parent bcfc4ab64b
commit ed400e39ce
2 changed files with 4 additions and 83 deletions

View File

@ -23,7 +23,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Pair; import android.util.Pair;
public class FENFile { public class FENFile {
@ -55,24 +54,18 @@ public class FENFile {
} }
} }
public static enum FenInfoResult { public enum FenInfoResult {
OK, OK,
CANCEL,
OUT_OF_MEMORY; OUT_OF_MEMORY;
} }
/** Read all FEN strings (one per line) in a file. */ /** Read all FEN strings (one per line) in a file. */
public final Pair<FenInfoResult,ArrayList<FenInfo>> getFenInfo(Activity activity, public final Pair<FenInfoResult,ArrayList<FenInfo>> getFenInfo() {
final ProgressDialog progress) {
ArrayList<FenInfo> fensInFile = new ArrayList<>(); ArrayList<FenInfo> fensInFile = new ArrayList<>();
try (BufferedRandomAccessFileReader f = try (BufferedRandomAccessFileReader f =
new BufferedRandomAccessFileReader(fileName.getAbsolutePath())) { new BufferedRandomAccessFileReader(fileName.getAbsolutePath())) {
int percent = -1;
long fileLen = f.length();
long filePos = 0;
int fenNo = 1; int fenNo = 1;
while (true) { while (true) {
filePos = f.getFilePointer();
String line = f.readLine(); String line = f.readLine();
if (line == null) if (line == null)
break; // EOF break; // EOF
@ -80,15 +73,6 @@ public class FENFile {
continue; continue;
FenInfo fi = new FenInfo(fenNo++, line.trim()); FenInfo fi = new FenInfo(fenNo++, line.trim());
fensInFile.add(fi); fensInFile.add(fi);
final int newPercent = fileLen == 0 ? 0 : (int)(filePos * 100 / fileLen);
if (newPercent > percent) {
percent = newPercent;
if (progress != null) {
activity.runOnUiThread(() -> progress.setProgress(newPercent));
}
}
if (Thread.currentThread().isInterrupted())
return new Pair<>(FenInfoResult.CANCEL, null);
} }
} catch (IOException ignore) { } catch (IOException ignore) {
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {

View File

@ -18,14 +18,8 @@
package org.petero.droidfish.activities; package org.petero.droidfish.activities;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.ListActivity; import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
@ -55,13 +49,11 @@ import org.petero.droidfish.gamelogic.TextIO;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
public class LoadFEN extends ListActivity { public class LoadFEN extends ListActivity {
private static ArrayList<FenInfo> fensInFile = new ArrayList<>(); private static ArrayList<FenInfo> fensInFile = new ArrayList<>();
private static boolean cacheValid = false; private static boolean cacheValid = false;
private FENFile fenFile; private FENFile fenFile;
private ProgressDialog progress;
private FenInfo selectedFi = null; private FenInfo selectedFi = null;
private ArrayAdapter<FenInfo> aa = null; private ArrayAdapter<FenInfo> aa = null;
@ -71,8 +63,6 @@ public class LoadFEN extends ListActivity {
private long lastModTime = -1; private long lastModTime = -1;
private Thread workThread = null; private Thread workThread = null;
private CountDownLatch progressLatch = null;
private boolean canceled = false;
LoadFenBinding binding; LoadFenBinding binding;
@ -99,26 +89,12 @@ public class LoadFEN extends ListActivity {
String fileName = i.getStringExtra("org.petero.droidfish.pathname"); String fileName = i.getStringExtra("org.petero.droidfish.pathname");
if ("org.petero.droidfish.loadFen".equals(action)) { if ("org.petero.droidfish.loadFen".equals(action)) {
fenFile = new FENFile(fileName); fenFile = new FENFile(fileName);
progressLatch = new CountDownLatch(1);
showProgressDialog();
final LoadFEN lfen = this; final LoadFEN lfen = this;
workThread = new Thread(() -> { workThread = new Thread(() -> {
try {
progressLatch.await();
} catch (InterruptedException e) {
setResult(RESULT_CANCELED);
finish();
return;
}
if (!readFile()) if (!readFile())
return; return;
runOnUiThread(() -> { runOnUiThread(() -> {
if (canceled) { lfen.showList();
setResult(RESULT_CANCELED);
finish();
} else {
lfen.showList();
}
}); });
}); });
workThread.start(); workThread.start();
@ -191,8 +167,6 @@ public class LoadFEN extends ListActivity {
} }
private void showList() { private void showList() {
progress = null;
removeProgressDialog();
setContentView(R.layout.load_fen); setContentView(R.layout.load_fen);
binding = DataBindingUtil.setContentView(this, R.layout.load_fen); binding = DataBindingUtil.setContentView(this, R.layout.load_fen);
binding.loadfenOk.setEnabled(false); binding.loadfenOk.setEnabled(false);
@ -266,43 +240,6 @@ public class LoadFEN extends ListActivity {
} }
} }
public static class ProgressFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LoadFEN a = (LoadFEN) getActivity();
ProgressDialog progress = new ProgressDialog(a);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setTitle(R.string.reading_fen_file);
a.progress = progress;
a.progressLatch.countDown();
return progress;
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Activity a = getActivity();
if (a instanceof LoadFEN) {
LoadFEN lf = (LoadFEN) a;
lf.canceled = true;
Thread thr = lf.workThread;
if (thr != null)
thr.interrupt();
}
}
}
private void showProgressDialog() {
ProgressFragment f = new ProgressFragment();
f.show(getFragmentManager(), "progress");
}
private void removeProgressDialog() {
Fragment f = getFragmentManager().findFragmentByTag("progress");
if (f instanceof DialogFragment)
((DialogFragment) f).dismiss();
}
private boolean readFile() { private boolean readFile() {
String fileName = fenFile.getName(); String fileName = fenFile.getName();
if (!fileName.equals(lastFileName)) if (!fileName.equals(lastFileName))
@ -311,7 +248,7 @@ public class LoadFEN extends ListActivity {
if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName)) if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName))
return true; return true;
fenFile = new FENFile(fileName); fenFile = new FENFile(fileName);
Pair<FenInfoResult, ArrayList<FenInfo>> p = fenFile.getFenInfo(this, progress); Pair<FenInfoResult, ArrayList<FenInfo>> p = fenFile.getFenInfo();
if (p.first != FenInfoResult.OK) { if (p.first != FenInfoResult.OK) {
fensInFile = new ArrayList<>(); fensInFile = new ArrayList<>();
if (p.first == FenInfoResult.OUT_OF_MEMORY) { if (p.first == FenInfoResult.OUT_OF_MEMORY) {