DroidFish: Made it possible to load/save PGN files in any directory, using the "OI file manager".

This commit is contained in:
Peter Osterlund
2012-04-14 12:37:15 +00:00
parent cc5582bdbc
commit 14d17d6719
4 changed files with 130 additions and 48 deletions

View File

@@ -226,8 +226,10 @@ you are not actively using the program.\
<string name="internal_book">&lt;Internal Book&gt;</string> <string name="internal_book">&lt;Internal Book&gt;</string>
<string name="select_opening_book_file">Select opening book file</string> <string name="select_opening_book_file">Select opening book file</string>
<string name="select_chess_engine">Select Chess Engine</string> <string name="select_chess_engine">Select Chess Engine</string>
<string name="select_pgn_file">Select PGN file to open</string> <string name="select_pgn_file">Open PGN file</string>
<string name="select_pgn_file_save">Save to PGN file</string> <string name="select_pgn_file_save">Save to PGN file</string>
<string name="pgn_load">Load</string>
<string name="pgn_save">Save</string>
<string name="select_scid_file">Select Scid file to open</string> <string name="select_scid_file">Select Scid file to open</string>
<string name="select_pgn_game">Select game to open</string> <string name="select_pgn_game">Select game to open</string>
<string name="reading_pgn_file">Reading PGN file</string> <string name="reading_pgn_file">Reading PGN file</string>
@@ -317,6 +319,7 @@ you are not actively using the program.\
<string name="failed_to_save_game">Failed to save game</string> <string name="failed_to_save_game">Failed to save game</string>
<string name="failed_to_delete_game">Failed to delete game</string> <string name="failed_to_delete_game">Failed to delete game</string>
<string name="file_too_large">File too large</string> <string name="file_too_large">File too large</string>
<string name="not_a_pgn_file">Not a PGN file</string>
<string name="save_game_question">Save game?</string> <string name="save_game_question">Save game?</string>
<string name="before_selected">Before Selected</string> <string name="before_selected">Before Selected</string>
<string name="after_selected">After Selected</string> <string name="after_selected">After Selected</string>

View File

@@ -56,6 +56,7 @@ import android.app.Dialog;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
@@ -71,6 +72,7 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
@@ -258,7 +260,7 @@ public class DroidFish extends Activity implements GUIInterface {
new File(extDir + sep + gtbDefaultDir).mkdirs(); new File(extDir + sep + gtbDefaultDir).mkdirs();
} }
private String getPgnIntent() { private final String getPgnIntent() {
String pgn = null; String pgn = null;
try { try {
Intent intent = getIntent(); Intent intent = getIntent();
@@ -774,6 +776,8 @@ public class DroidFish extends Activity implements GUIInterface {
static private final int RESULT_SETTINGS = 1; static private final int RESULT_SETTINGS = 1;
static private final int RESULT_LOAD_PGN = 2; static private final int RESULT_LOAD_PGN = 2;
static private final int RESULT_SELECT_SCID = 3; static private final int RESULT_SELECT_SCID = 3;
static private final int RESULT_OI_PGN_SAVE = 4;
static private final int RESULT_OI_PGN_LOAD = 5;
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
@@ -882,7 +886,30 @@ public class DroidFish extends Activity implements GUIInterface {
} }
} }
break; break;
case RESULT_OI_PGN_LOAD:
if (resultCode == RESULT_OK) {
String pathName = getFilePathFromUri(data.getData());
if (pathName != null)
loadPGNFromFile(pathName);
} }
break;
case RESULT_OI_PGN_SAVE:
if (resultCode == RESULT_OK) {
String pathName = getFilePathFromUri(data.getData());
if (pathName != null) {
if ((pathName.length() > 0) && !pathName.contains("."))
pathName += ".pgn";
savePGNToFile(pathName, false);
}
}
break;
}
}
private static String getFilePathFromUri(Uri uri) {
if (uri == null)
return null;
return uri.getPath();
} }
private final String getParseErrString(ChessParseError e) { private final String getParseErrString(ChessParseError e) {
@@ -1161,7 +1188,7 @@ public class DroidFish extends Activity implements GUIInterface {
editor.commit(); editor.commit();
gameMode = new GameMode(gameModeType); gameMode = new GameMode(gameModeType);
} }
// savePGNToFile(ctrl.getPGN(), ".autosave.pgn", true); // savePGNToFile(".autosave.pgn", true);
ctrl.newGame(gameMode); ctrl.newGame(gameMode);
ctrl.startGame(); ctrl.startGame();
setBoardFlip(true); setBoardFlip(true);
@@ -1277,15 +1304,13 @@ public class DroidFish extends Activity implements GUIInterface {
break; break;
} }
case LOAD_GAME: case LOAD_GAME:
removeDialog(SELECT_PGN_FILE_DIALOG); selectPgnFile(false);
showDialog(SELECT_PGN_FILE_DIALOG);
break; break;
case LOAD_SCID_GAME: case LOAD_SCID_GAME:
selectScidFile(); selectScidFile();
break; break;
case SAVE_GAME: case SAVE_GAME:
removeDialog(SELECT_PGN_FILE_SAVE_DIALOG); selectPgnFile(true);
showDialog(SELECT_PGN_FILE_SAVE_DIALOG);
break; break;
} }
} }
@@ -1312,12 +1337,10 @@ public class DroidFish extends Activity implements GUIInterface {
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int item) {
switch (finalActions.get(item)) { switch (finalActions.get(item)) {
case LOAD_GAME: case LOAD_GAME:
removeDialog(SELECT_PGN_FILE_DIALOG); selectPgnFile(false);
showDialog(SELECT_PGN_FILE_DIALOG);
break; break;
case SAVE_GAME: case SAVE_GAME:
removeDialog(SELECT_PGN_FILE_SAVE_DIALOG); selectPgnFile(true);
showDialog(SELECT_PGN_FILE_SAVE_DIALOG);
break; break;
case LOAD_SCID_GAME: case LOAD_SCID_GAME:
selectScidFile(); selectScidFile();
@@ -1497,6 +1520,7 @@ public class DroidFish extends Activity implements GUIInterface {
} }
int defaultItem = 0; int defaultItem = 0;
String currentPGNFile = settings.getString("currentPGNFile", ""); String currentPGNFile = settings.getString("currentPGNFile", "");
currentPGNFile = new File(currentPGNFile).getName();
for (int i = 0; i < numFiles; i++) { for (int i = 0; i < numFiles; i++) {
if (currentPGNFile.equals(fileNames[i])) { if (currentPGNFile.equals(fileNames[i])) {
defaultItem = i; defaultItem = i;
@@ -1507,18 +1531,11 @@ public class DroidFish extends Activity implements GUIInterface {
builder.setTitle(R.string.select_pgn_file); builder.setTitle(R.string.select_pgn_file);
builder.setSingleChoiceItems(fileNames, defaultItem, new DialogInterface.OnClickListener() { builder.setSingleChoiceItems(fileNames, defaultItem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int item) {
Editor editor = settings.edit();
String pgnFile = fileNames[item].toString();
editor.putString("currentPGNFile", pgnFile);
editor.putInt("currFT", FT_PGN);
editor.commit();
String sep = File.separator;
String pathName = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + pgnFile;
Intent i = new Intent(DroidFish.this, EditPGNLoad.class);
i.setAction("org.petero.droidfish.loadFile");
i.putExtra("org.petero.droidfish.pathname", pathName);
startActivityForResult(i, RESULT_LOAD_PGN);
dialog.dismiss(); dialog.dismiss();
String sep = File.separator;
String pgnFile = fileNames[item].toString();
String pathName = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + pgnFile;
loadPGNFromFile(pathName);
} }
}); });
AlertDialog alert = builder.create(); AlertDialog alert = builder.create();
@@ -1529,6 +1546,7 @@ public class DroidFish extends Activity implements GUIInterface {
final int numFiles = fileNames.length; final int numFiles = fileNames.length;
int defaultItem = 0; int defaultItem = 0;
String currentPGNFile = settings.getString("currentPGNFile", ""); String currentPGNFile = settings.getString("currentPGNFile", "");
currentPGNFile = new File(currentPGNFile).getName();
for (int i = 0; i < numFiles; i++) { for (int i = 0; i < numFiles; i++) {
if (currentPGNFile.equals(fileNames[i])) { if (currentPGNFile.equals(fileNames[i])) {
defaultItem = i; defaultItem = i;
@@ -1549,12 +1567,11 @@ public class DroidFish extends Activity implements GUIInterface {
dialog.dismiss(); dialog.dismiss();
showDialog(SELECT_PGN_SAVE_NEWFILE_DIALOG); showDialog(SELECT_PGN_SAVE_NEWFILE_DIALOG);
} else { } else {
Editor editor = settings.edit();
pgnFile = fileNames[item].toString();
editor.putString("currentPGNFile", pgnFile);
editor.commit();
dialog.dismiss(); dialog.dismiss();
savePGNToFile(ctrl.getPGN(), pgnFile, false); pgnFile = fileNames[item].toString();
String sep = File.separator;
String pathName = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + pgnFile;
savePGNToFile(pathName, false);
} }
} }
}); });
@@ -1573,7 +1590,9 @@ public class DroidFish extends Activity implements GUIInterface {
String pgnFile = fileNameView.getText().toString(); String pgnFile = fileNameView.getText().toString();
if ((pgnFile.length() > 0) && !pgnFile.contains(".")) if ((pgnFile.length() > 0) && !pgnFile.contains("."))
pgnFile += ".pgn"; pgnFile += ".pgn";
savePGNToFile(ctrl.getPGN(), pgnFile, false); String sep = File.separator;
String pathName = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + pgnFile;
savePGNToFile(pathName, false);
} }
}; };
builder.setPositiveButton(R.string.ok, new Dialog.OnClickListener() { builder.setPositiveButton(R.string.ok, new Dialog.OnClickListener() {
@@ -1868,7 +1887,7 @@ public class DroidFish extends Activity implements GUIInterface {
lst.add(getString(R.string.goto_prev_variation)); actions.add(GOTO_PREV_VAR); lst.add(getString(R.string.goto_prev_variation)); actions.add(GOTO_PREV_VAR);
} }
final int currFT = currFileType(); final int currFT = currFileType();
final String currFileName = currFileName(); final String currPathName = currPathName();
if (currFT != FT_NONE) { if (currFT != FT_NONE) {
lst.add(getString(R.string.load_prev_game)); actions.add(LOAD_PREV_GAME); lst.add(getString(R.string.load_prev_game)); actions.add(LOAD_PREV_GAME);
} }
@@ -1882,17 +1901,15 @@ public class DroidFish extends Activity implements GUIInterface {
case GOTO_START_VAR: ctrl.gotoStartOfVariation(); break; case GOTO_START_VAR: ctrl.gotoStartOfVariation(); break;
case GOTO_PREV_VAR: ctrl.changeVariation(-1); break; case GOTO_PREV_VAR: ctrl.changeVariation(-1); break;
case LOAD_PREV_GAME: case LOAD_PREV_GAME:
String sep = File.separator;
String pathName = Environment.getExternalStorageDirectory() + sep;
Intent i; Intent i;
if (currFT == FT_PGN) { if (currFT == FT_PGN) {
i = new Intent(DroidFish.this, EditPGNLoad.class); i = new Intent(DroidFish.this, EditPGNLoad.class);
i.setAction("org.petero.droidfish.loadFilePrevGame"); i.setAction("org.petero.droidfish.loadFilePrevGame");
i.putExtra("org.petero.droidfish.pathname", pathName + pgnDir + sep + currFileName); i.putExtra("org.petero.droidfish.pathname", currPathName);
} else { } else {
i = new Intent(DroidFish.this, LoadScid.class); i = new Intent(DroidFish.this, LoadScid.class);
i.setAction("org.petero.droidfish.loadScidPrevGame"); i.setAction("org.petero.droidfish.loadScidPrevGame");
i.putExtra("org.petero.droidfish.pathname", currFileName); i.putExtra("org.petero.droidfish.pathname", currPathName);
} }
startActivityForResult(i, RESULT_LOAD_PGN); startActivityForResult(i, RESULT_LOAD_PGN);
break; break;
@@ -1914,7 +1931,7 @@ public class DroidFish extends Activity implements GUIInterface {
lst.add(getString(R.string.goto_next_variation)); actions.add(GOTO_NEXT_VAR); lst.add(getString(R.string.goto_next_variation)); actions.add(GOTO_NEXT_VAR);
} }
final int currFT = currFileType(); final int currFT = currFileType();
final String currFileName = currFileName(); final String currPathName = currPathName();
if (currFT != FT_NONE) { if (currFT != FT_NONE) {
lst.add(getString(R.string.load_next_game)); actions.add(LOAD_NEXT_GAME); lst.add(getString(R.string.load_next_game)); actions.add(LOAD_NEXT_GAME);
} }
@@ -1927,17 +1944,15 @@ public class DroidFish extends Activity implements GUIInterface {
case GOTO_END_VAR: ctrl.gotoMove(Integer.MAX_VALUE); break; case GOTO_END_VAR: ctrl.gotoMove(Integer.MAX_VALUE); break;
case GOTO_NEXT_VAR: ctrl.changeVariation(1); break; case GOTO_NEXT_VAR: ctrl.changeVariation(1); break;
case LOAD_NEXT_GAME: case LOAD_NEXT_GAME:
String sep = File.separator;
String pathName = Environment.getExternalStorageDirectory() + sep;
Intent i; Intent i;
if (currFT == FT_PGN) { if (currFT == FT_PGN) {
i = new Intent(DroidFish.this, EditPGNLoad.class); i = new Intent(DroidFish.this, EditPGNLoad.class);
i.setAction("org.petero.droidfish.loadFileNextGame"); i.setAction("org.petero.droidfish.loadFileNextGame");
i.putExtra("org.petero.droidfish.pathname", pathName + pgnDir + sep + currFileName); i.putExtra("org.petero.droidfish.pathname", currPathName);
} else { } else {
i = new Intent(DroidFish.this, LoadScid.class); i = new Intent(DroidFish.this, LoadScid.class);
i.setAction("org.petero.droidfish.loadScidNextGame"); i.setAction("org.petero.droidfish.loadScidNextGame");
i.putExtra("org.petero.droidfish.pathname", currFileName); i.putExtra("org.petero.droidfish.pathname", currPathName);
} }
startActivityForResult(i, RESULT_LOAD_PGN); startActivityForResult(i, RESULT_LOAD_PGN);
break; break;
@@ -1951,6 +1966,31 @@ public class DroidFish extends Activity implements GUIInterface {
return null; return null;
} }
/** Open a load/save file dialog. Uses OI file manager if available. */
private void selectPgnFile(boolean save) {
String action = "org.openintents.action.PICK_FILE";
String title = getString(save ? R.string.select_pgn_file_save : R.string.select_pgn_file);
String button = getString(save ? R.string.pgn_save : R.string.pgn_load);
int result = save ? RESULT_OI_PGN_SAVE : RESULT_OI_PGN_LOAD;
int dialog = save ? SELECT_PGN_FILE_SAVE_DIALOG : SELECT_PGN_FILE_DIALOG;
Intent i = new Intent(action);
String currentPGNFile = settings.getString("currentPGNFile", "");
String sep = File.separator;
if (!currentPGNFile.contains(sep))
currentPGNFile = Environment.getExternalStorageDirectory() +
sep + pgnDir + sep + currentPGNFile;
i.setData(Uri.fromFile(new File(currentPGNFile)));
i.putExtra("org.openintents.extra.TITLE", title);
i.putExtra("org.openintents.extra.BUTTON_TEXT", button);
try {
startActivityForResult(i, result);
} catch (ActivityNotFoundException e) {
removeDialog(dialog);
showDialog(dialog);
}
}
private final void selectScidFile() { private final void selectScidFile() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setComponent(new ComponentName("org.scid.android", intent.setComponent(new ComponentName("org.scid.android",
@@ -1970,12 +2010,21 @@ public class DroidFish extends Activity implements GUIInterface {
return ft; return ft;
} }
private final String currFileName() { /** Return path name for the last used PGN or SCID file. */
private final String currPathName() {
int ft = settings.getInt("currFT", FT_NONE); int ft = settings.getInt("currFT", FT_NONE);
switch (ft) { switch (ft) {
case FT_PGN: return settings.getString("currentPGNFile", ""); case FT_PGN: {
case FT_SCID: return settings.getString("currentScidFile", ""); String ret = settings.getString("currentPGNFile", "");
default: return ""; String sep = File.separator;
if (!ret.contains(sep))
ret = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + ret;
return ret;
}
case FT_SCID:
return settings.getString("currentScidFile", "");
default:
return "";
} }
} }
@@ -2004,9 +2053,13 @@ public class DroidFish extends Activity implements GUIInterface {
return fileNames; return fileNames;
} }
private final void savePGNToFile(String pgn, String filename, boolean silent) { /** Save current game to a PGN file. */
String sep = File.separator; private final void savePGNToFile(String pathName, boolean silent) {
String pathName = Environment.getExternalStorageDirectory() + sep + pgnDir + sep + filename; String pgn = ctrl.getPGN();
Editor editor = settings.edit();
editor.putString("currentPGNFile", pathName);
editor.putInt("currFT", FT_PGN);
editor.commit();
Intent i = new Intent(DroidFish.this, EditPGNSave.class); Intent i = new Intent(DroidFish.this, EditPGNSave.class);
i.setAction("org.petero.droidfish.saveFile"); i.setAction("org.petero.droidfish.saveFile");
i.putExtra("org.petero.droidfish.pathname", pathName); i.putExtra("org.petero.droidfish.pathname", pathName);
@@ -2015,6 +2068,18 @@ public class DroidFish extends Activity implements GUIInterface {
startActivity(i); startActivity(i);
} }
/** Load a PGN game from a file. */
private final void loadPGNFromFile(String pathName) {
Editor editor = settings.edit();
editor.putString("currentPGNFile", pathName);
editor.putInt("currFT", FT_PGN);
editor.commit();
Intent i = new Intent(DroidFish.this, EditPGNLoad.class);
i.setAction("org.petero.droidfish.loadFile");
i.putExtra("org.petero.droidfish.pathname", pathName);
startActivityForResult(i, RESULT_LOAD_PGN);
}
@Override @Override
public void requestPromotePiece() { public void requestPromotePiece() {
showDialog(PROMOTE_DIALOG); showDialog(PROMOTE_DIALOG);

View File

@@ -392,13 +392,23 @@ public class EditPGN extends ListActivity {
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>();
if (p.first == GameInfoResult.OUT_OF_MEMORY) { switch (p.first) {
case OUT_OF_MEMORY:
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
Toast.makeText(getApplicationContext(), R.string.file_too_large, Toast.makeText(getApplicationContext(), R.string.file_too_large,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
}); });
break;
case NOT_PGN:
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), R.string.not_a_pgn_file,
Toast.LENGTH_SHORT).show();
}
});
break;
} }
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();

View File

@@ -183,6 +183,7 @@ public class PGNFile {
public static enum GameInfoResult { public static enum GameInfoResult {
OK, OK,
CANCEL, CANCEL,
NOT_PGN,
OUT_OF_MEMORY; OUT_OF_MEMORY;
} }
@@ -190,11 +191,12 @@ public class PGNFile {
public final Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity, public final Pair<GameInfoResult,ArrayList<GameInfo>> getGameInfo(Activity activity,
final ProgressDialog progress) { final ProgressDialog progress) {
ArrayList<GameInfo> gamesInFile = new ArrayList<GameInfo>(); ArrayList<GameInfo> gamesInFile = new ArrayList<GameInfo>();
long fileLen = 0;
try { try {
int percent = -1; int percent = -1;
gamesInFile.clear(); gamesInFile.clear();
BufferedRandomAccessFileReader f = new BufferedRandomAccessFileReader(fileName.getAbsolutePath()); BufferedRandomAccessFileReader f = new BufferedRandomAccessFileReader(fileName.getAbsolutePath());
long fileLen = f.length(); fileLen = f.length();
GameInfo gi = null; GameInfo gi = null;
HeaderInfo hi = null; HeaderInfo hi = null;
boolean inHeader = false; boolean inHeader = false;
@@ -289,6 +291,8 @@ public class PGNFile {
gamesInFile = null; gamesInFile = null;
return new Pair<GameInfoResult,ArrayList<GameInfo>>(GameInfoResult.OUT_OF_MEMORY, null); return new Pair<GameInfoResult,ArrayList<GameInfo>>(GameInfoResult.OUT_OF_MEMORY, null);
} }
if ((gamesInFile.size() == 0) && (fileLen > 0))
return new Pair<GameInfoResult,ArrayList<GameInfo>>(GameInfoResult.NOT_PGN, null);
return new Pair<GameInfoResult,ArrayList<GameInfo>>(GameInfoResult.OK, gamesInFile); return new Pair<GameInfoResult,ArrayList<GameInfo>>(GameInfoResult.OK, gamesInFile);
} }