DroidFish: Use DialogFragment instead of showDialog() in CPUWarning,

LoadFEN and LoadScid activities.
This commit is contained in:
Peter Osterlund 2015-12-25 10:34:58 +01:00
parent efc6402829
commit ba39b30be7
3 changed files with 108 additions and 62 deletions

View File

@ -19,37 +19,36 @@
package org.petero.droidfish.activities;
import org.petero.droidfish.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
public class CPUWarning extends Activity {
public static class Fragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.app_name)
.setMessage(R.string.cpu_warning)
.create();
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
Activity a = getActivity();
if (a != null)
a.finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog(CPU_WARNING_DIALOG);
}
static final int CPU_WARNING_DIALOG = 1;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CPU_WARNING_DIALOG:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.app_name).setMessage(R.string.cpu_warning);
AlertDialog alert = builder.create();
alert.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
finish();
}
});
return alert;
}
return null;
DialogFragment df = new Fragment();
df.show(getFragmentManager(), "");
}
}

View File

@ -20,6 +20,7 @@ package org.petero.droidfish.activities;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import org.petero.droidfish.ChessBoardPlay;
import org.petero.droidfish.ColorTheme;
@ -32,13 +33,15 @@ import org.petero.droidfish.gamelogic.Pair;
import org.petero.droidfish.gamelogic.Position;
import org.petero.droidfish.gamelogic.TextIO;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Bundle;
@ -69,6 +72,7 @@ public class LoadFEN extends ListActivity {
private long lastModTime = -1;
private Thread workThread = null;
private CountDownLatch progressLatch = null;
private ChessBoardPlay cb;
private Button okButton;
@ -97,10 +101,18 @@ public class LoadFEN extends ListActivity {
String fileName = i.getStringExtra("org.petero.droidfish.pathname");
if (action.equals("org.petero.droidfish.loadFen")) {
fenFile = new FENFile(fileName);
showDialog(PROGRESS_DIALOG);
progressLatch = new CountDownLatch(1);
showProgressDialog();
final LoadFEN lfen = this;
workThread = new Thread(new Runnable() {
public void run() {
try {
progressLatch.await();
} catch (InterruptedException e) {
setResult(RESULT_CANCELED);
finish();
return;
}
if (!readFile())
return;
runOnUiThread(new Runnable() {
@ -182,7 +194,7 @@ public class LoadFEN extends ListActivity {
private final void showList() {
progress = null;
removeDialog(PROGRESS_DIALOG);
removeProgressDialog();
setContentView(R.layout.load_fen);
cb = (ChessBoardPlay)findViewById(R.id.loadfen_chessboard);
@ -270,29 +282,40 @@ public class LoadFEN extends ListActivity {
}
}
final static int PROGRESS_DIALOG = 0;
public static class ProgressFragment extends DialogFragment {
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG:
progress = new ProgressDialog(this);
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);
progress.setOnCancelListener(new OnCancelListener() {
a.progress = progress;
a.progressLatch.countDown();
return progress;
}
@Override
public void onCancel(DialogInterface dialog) {
Thread thr = workThread;
super.onCancel(dialog);
Activity a = getActivity();
if (a instanceof LoadFEN) {
Thread thr = ((LoadFEN)a).workThread;
if (thr != null)
thr.interrupt();
}
});
return progress;
default:
return null;
}
}
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 final boolean readFile() {
String fileName = fenFile.getName();
if (!fileName.equals(lastFileName))

View File

@ -21,12 +21,16 @@ package org.petero.droidfish.activities;
import java.io.File;
import java.util.Locale;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import org.petero.droidfish.ColorTheme;
import org.petero.droidfish.R;
import org.petero.droidfish.Util;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.ProgressDialog;
@ -35,7 +39,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
@ -73,7 +76,9 @@ public class LoadScid extends ListActivity {
private String lastFileName = "";
private long lastModTime = -1;
Thread workThread = null;
private Thread workThread = null;
private CountDownLatch progressLatch = null;
private int idIdx;
private int summaryIdx;
private boolean resultSentBack = false;
@ -131,11 +136,19 @@ public class LoadScid extends ListActivity {
fileName = i.getStringExtra("org.petero.droidfish.pathname");
resultSentBack = false;
if (action.equals("org.petero.droidfish.loadScid")) {
showDialog(PROGRESS_DIALOG);
progressLatch = new CountDownLatch(1);
showProgressDialog();
final LoadScid lpgn = this;
startReadFile(new OnCursorReady() {
@Override
public void run(Cursor cursor) {
try {
progressLatch.await();
} catch (InterruptedException e) {
setResult(RESULT_CANCELED);
finish();
return;
}
if (!readFile(cursor))
return;
runOnUiThread(new Runnable() {
@ -215,7 +228,7 @@ public class LoadScid extends ListActivity {
private final void showList() {
progress = null;
removeDialog(PROGRESS_DIALOG);
removeProgressDialog();
final ArrayAdapter<GameInfo> aa =
new ArrayAdapter<GameInfo>(this, R.layout.select_game_list_item, gamesInFile) {
@Override
@ -242,29 +255,40 @@ public class LoadScid extends ListActivity {
});
}
final static int PROGRESS_DIALOG = 0;
public static class ProgressFragment extends DialogFragment {
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG:
progress = new ProgressDialog(this);
public Dialog onCreateDialog(Bundle savedInstanceState) {
LoadScid a = (LoadScid)getActivity();
ProgressDialog progress = new ProgressDialog(a);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setTitle(R.string.reading_scid_file);
progress.setOnCancelListener(new OnCancelListener() {
a.progress = progress;
a.progressLatch.countDown();
return progress;
}
@Override
public void onCancel(DialogInterface dialog) {
Thread thr = workThread;
super.onCancel(dialog);
Activity a = getActivity();
if (a instanceof LoadScid) {
Thread thr = ((LoadScid)a).workThread;
if (thr != null)
thr.interrupt();
}
});
return progress;
default:
return null;
}
}
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 final boolean readFile(Cursor cursor) {
if (!fileName.equals(lastFileName))
defaultItem = 0;