mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-01-30 17:13:50 +01:00
DroidFish: Use DialogFragment instead of showDialog() in CPUWarning,
LoadFEN and LoadScid activities.
This commit is contained in:
parent
efc6402829
commit
ba39b30be7
|
@ -19,37 +19,36 @@
|
||||||
package org.petero.droidfish.activities;
|
package org.petero.droidfish.activities;
|
||||||
|
|
||||||
import org.petero.droidfish.R;
|
import org.petero.droidfish.R;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
|
import android.app.DialogFragment;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.DialogInterface.OnDismissListener;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
public class CPUWarning extends Activity {
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
showDialog(CPU_WARNING_DIALOG);
|
DialogFragment df = new Fragment();
|
||||||
}
|
df.show(getFragmentManager(), "");
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.petero.droidfish.activities;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import org.petero.droidfish.ChessBoardPlay;
|
import org.petero.droidfish.ChessBoardPlay;
|
||||||
import org.petero.droidfish.ColorTheme;
|
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.Position;
|
||||||
import org.petero.droidfish.gamelogic.TextIO;
|
import org.petero.droidfish.gamelogic.TextIO;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.Dialog;
|
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.app.ProgressDialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.DialogInterface.OnCancelListener;
|
|
||||||
import android.content.SharedPreferences.Editor;
|
import android.content.SharedPreferences.Editor;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
@ -69,6 +72,7 @@ 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 ChessBoardPlay cb;
|
private ChessBoardPlay cb;
|
||||||
private Button okButton;
|
private Button okButton;
|
||||||
|
@ -97,10 +101,18 @@ public class LoadFEN extends ListActivity {
|
||||||
String fileName = i.getStringExtra("org.petero.droidfish.pathname");
|
String fileName = i.getStringExtra("org.petero.droidfish.pathname");
|
||||||
if (action.equals("org.petero.droidfish.loadFen")) {
|
if (action.equals("org.petero.droidfish.loadFen")) {
|
||||||
fenFile = new FENFile(fileName);
|
fenFile = new FENFile(fileName);
|
||||||
showDialog(PROGRESS_DIALOG);
|
progressLatch = new CountDownLatch(1);
|
||||||
|
showProgressDialog();
|
||||||
final LoadFEN lfen = this;
|
final LoadFEN lfen = this;
|
||||||
workThread = new Thread(new Runnable() {
|
workThread = new Thread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
try {
|
||||||
|
progressLatch.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
setResult(RESULT_CANCELED);
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!readFile())
|
if (!readFile())
|
||||||
return;
|
return;
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
|
@ -182,7 +194,7 @@ public class LoadFEN extends ListActivity {
|
||||||
|
|
||||||
private final void showList() {
|
private final void showList() {
|
||||||
progress = null;
|
progress = null;
|
||||||
removeDialog(PROGRESS_DIALOG);
|
removeProgressDialog();
|
||||||
setContentView(R.layout.load_fen);
|
setContentView(R.layout.load_fen);
|
||||||
|
|
||||||
cb = (ChessBoardPlay)findViewById(R.id.loadfen_chessboard);
|
cb = (ChessBoardPlay)findViewById(R.id.loadfen_chessboard);
|
||||||
|
@ -270,27 +282,38 @@ public class LoadFEN extends ListActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final static int PROGRESS_DIALOG = 0;
|
public static class ProgressFragment extends DialogFragment {
|
||||||
|
@Override
|
||||||
@Override
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
protected Dialog onCreateDialog(int id) {
|
LoadFEN a = (LoadFEN)getActivity();
|
||||||
switch (id) {
|
ProgressDialog progress = new ProgressDialog(a);
|
||||||
case PROGRESS_DIALOG:
|
|
||||||
progress = new ProgressDialog(this);
|
|
||||||
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
||||||
progress.setTitle(R.string.reading_fen_file);
|
progress.setTitle(R.string.reading_fen_file);
|
||||||
progress.setOnCancelListener(new OnCancelListener() {
|
a.progress = progress;
|
||||||
@Override
|
a.progressLatch.countDown();
|
||||||
public void onCancel(DialogInterface dialog) {
|
|
||||||
Thread thr = workThread;
|
|
||||||
if (thr != null)
|
|
||||||
thr.interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return progress;
|
return progress;
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
super.onCancel(dialog);
|
||||||
|
Activity a = getActivity();
|
||||||
|
if (a instanceof LoadFEN) {
|
||||||
|
Thread thr = ((LoadFEN)a).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 final boolean readFile() {
|
private final boolean readFile() {
|
||||||
|
|
|
@ -21,12 +21,16 @@ package org.petero.droidfish.activities;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import org.petero.droidfish.ColorTheme;
|
import org.petero.droidfish.ColorTheme;
|
||||||
import org.petero.droidfish.R;
|
import org.petero.droidfish.R;
|
||||||
import org.petero.droidfish.Util;
|
import org.petero.droidfish.Util;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
|
import android.app.DialogFragment;
|
||||||
|
import android.app.Fragment;
|
||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
import android.app.LoaderManager;
|
import android.app.LoaderManager;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
|
@ -35,7 +39,6 @@ import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.Loader;
|
import android.content.Loader;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.DialogInterface.OnCancelListener;
|
|
||||||
import android.content.SharedPreferences.Editor;
|
import android.content.SharedPreferences.Editor;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
@ -73,7 +76,9 @@ public class LoadScid extends ListActivity {
|
||||||
private String lastFileName = "";
|
private String lastFileName = "";
|
||||||
private long lastModTime = -1;
|
private long lastModTime = -1;
|
||||||
|
|
||||||
Thread workThread = null;
|
private Thread workThread = null;
|
||||||
|
private CountDownLatch progressLatch = null;
|
||||||
|
|
||||||
private int idIdx;
|
private int idIdx;
|
||||||
private int summaryIdx;
|
private int summaryIdx;
|
||||||
private boolean resultSentBack = false;
|
private boolean resultSentBack = false;
|
||||||
|
@ -131,11 +136,19 @@ public class LoadScid extends ListActivity {
|
||||||
fileName = i.getStringExtra("org.petero.droidfish.pathname");
|
fileName = i.getStringExtra("org.petero.droidfish.pathname");
|
||||||
resultSentBack = false;
|
resultSentBack = false;
|
||||||
if (action.equals("org.petero.droidfish.loadScid")) {
|
if (action.equals("org.petero.droidfish.loadScid")) {
|
||||||
showDialog(PROGRESS_DIALOG);
|
progressLatch = new CountDownLatch(1);
|
||||||
|
showProgressDialog();
|
||||||
final LoadScid lpgn = this;
|
final LoadScid lpgn = this;
|
||||||
startReadFile(new OnCursorReady() {
|
startReadFile(new OnCursorReady() {
|
||||||
@Override
|
@Override
|
||||||
public void run(Cursor cursor) {
|
public void run(Cursor cursor) {
|
||||||
|
try {
|
||||||
|
progressLatch.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
setResult(RESULT_CANCELED);
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!readFile(cursor))
|
if (!readFile(cursor))
|
||||||
return;
|
return;
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
|
@ -215,7 +228,7 @@ public class LoadScid extends ListActivity {
|
||||||
|
|
||||||
private final void showList() {
|
private final void showList() {
|
||||||
progress = null;
|
progress = null;
|
||||||
removeDialog(PROGRESS_DIALOG);
|
removeProgressDialog();
|
||||||
final ArrayAdapter<GameInfo> aa =
|
final ArrayAdapter<GameInfo> aa =
|
||||||
new ArrayAdapter<GameInfo>(this, R.layout.select_game_list_item, gamesInFile) {
|
new ArrayAdapter<GameInfo>(this, R.layout.select_game_list_item, gamesInFile) {
|
||||||
@Override
|
@Override
|
||||||
|
@ -242,27 +255,38 @@ public class LoadScid extends ListActivity {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final static int PROGRESS_DIALOG = 0;
|
public static class ProgressFragment extends DialogFragment {
|
||||||
|
@Override
|
||||||
@Override
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
protected Dialog onCreateDialog(int id) {
|
LoadScid a = (LoadScid)getActivity();
|
||||||
switch (id) {
|
ProgressDialog progress = new ProgressDialog(a);
|
||||||
case PROGRESS_DIALOG:
|
|
||||||
progress = new ProgressDialog(this);
|
|
||||||
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
||||||
progress.setTitle(R.string.reading_scid_file);
|
progress.setTitle(R.string.reading_scid_file);
|
||||||
progress.setOnCancelListener(new OnCancelListener() {
|
a.progress = progress;
|
||||||
@Override
|
a.progressLatch.countDown();
|
||||||
public void onCancel(DialogInterface dialog) {
|
|
||||||
Thread thr = workThread;
|
|
||||||
if (thr != null)
|
|
||||||
thr.interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return progress;
|
return progress;
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
super.onCancel(dialog);
|
||||||
|
Activity a = getActivity();
|
||||||
|
if (a instanceof LoadScid) {
|
||||||
|
Thread thr = ((LoadScid)a).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 final boolean readFile(Cursor cursor) {
|
private final boolean readFile(Cursor cursor) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user