DroidFish: Cancel old toast before showing new.

This commit is contained in:
Peter Osterlund
2018-10-14 09:05:25 +02:00
parent ac72cf115e
commit 2038855893
10 changed files with 75 additions and 58 deletions

View File

@@ -157,7 +157,7 @@ public class ChessBoardPlay extends ChessBoard {
int pieceType = (pgnOptions == null) ? PGNOptions.PT_LOCAL
: pgnOptions.view.pieceType;
msg += ": " + TextIO.pieceAndSquareToString(pieceType, p, sq);
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(msg, Toast.LENGTH_SHORT);
}
}
setSelection(anyMatch ? sq : -1);

View File

@@ -758,11 +758,9 @@ public class DroidFish extends Activity
}
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.failed_to_read_pgn_data,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.failed_to_read_pgn_data, Toast.LENGTH_SHORT);
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
}
return new Pair<String,String>(pgnOrFen,filename);
}
@@ -1666,7 +1664,7 @@ public class DroidFish extends Activity
if (ctrl.claimDrawIfPossible())
ctrl.stopPonder();
else
Toast.makeText(getApplicationContext(), R.string.offer_draw, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.offer_draw, Toast.LENGTH_SHORT);
}
break;
case ITEM_SELECT_BOOK:
@@ -1732,7 +1730,7 @@ public class DroidFish extends Activity
ctrl.setFENOrPGN(pgn);
setBoardFlip(true);
} catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
DroidFishApp.toast(getParseErrString(e), Toast.LENGTH_SHORT);
}
}
break;
@@ -2294,7 +2292,7 @@ public class DroidFish extends Activity
ctrl.setFENOrPGN(fenPgn.toString());
setBoardFlip(true);
} catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
DroidFishApp.toast(getParseErrString(e), Toast.LENGTH_SHORT);
}
}
break;
@@ -2385,7 +2383,7 @@ public class DroidFish extends Activity
ow.close();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
return;
}
String authority = "org.petero.droidfish.fileprovider";
@@ -2418,7 +2416,7 @@ public class DroidFish extends Activity
os.close();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
return;
}
@@ -3487,7 +3485,7 @@ public class DroidFish extends Activity
errMsg = R.string.engine_name_in_use;
}
if (!nameOk) {
Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_LONG).show();
DroidFishApp.toast(errMsg, Toast.LENGTH_LONG);
reShowDialog(NETWORK_ENGINE_DIALOG);
return;
}
@@ -3561,7 +3559,7 @@ public class DroidFish extends Activity
fw.close();
setEngineOptions(true);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
}
}
};
@@ -3686,7 +3684,7 @@ public class DroidFish extends Activity
try {
startActivityForResult(intent, RESULT_SELECT_SCID);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
}
}
@@ -3703,7 +3701,7 @@ public class DroidFish extends Activity
try {
startActivityForResult(i, RESULT_GET_FEN);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
}
}
@@ -3829,21 +3827,21 @@ public class DroidFish extends Activity
String msg = String.format(Locale.US, "%s %s-%s",
getString(R.string.invalid_move),
TextIO.squareToString(m.from), TextIO.squareToString(m.to));
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(msg, Toast.LENGTH_SHORT);
}
@Override
public void reportEngineName(String engine) {
String msg = String.format(Locale.US, "%s: %s",
getString(R.string.engine), engine);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(msg, Toast.LENGTH_SHORT);
}
@Override
public void reportEngineError(String errMsg) {
String msg = String.format(Locale.US, "%s: %s",
getString(R.string.engine_error), errMsg);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
DroidFishApp.toast(msg, Toast.LENGTH_LONG);
}
/** Initialize text to speech if enabled in settings. */
@@ -3855,8 +3853,7 @@ public class DroidFish extends Activity
@Override
public void movePlayed(Position pos, Move move, boolean computerMove) {
if (move == null) {
Toast.makeText(getApplicationContext(), R.string.engine_error,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.engine_error, Toast.LENGTH_SHORT);
newGameMode(GameMode.EDIT_GAME);
return;
}

View File

@@ -20,9 +20,12 @@ package org.petero.droidfish;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;
public class DroidFishApp extends Application {
private static Context appContext;
private static Toast toast;
@Override
public void onCreate() {
super.onCreate();
@@ -33,4 +36,28 @@ public class DroidFishApp extends Application {
public static Context getContext() {
return appContext;
}
/** Show a toast after canceling current toast. */
public static void toast(int resId, int duration) {
if (toast != null) {
toast.cancel();
toast = null;
}
if (appContext != null) {
toast = Toast.makeText(appContext, resId, duration);
toast.show();
}
}
/** Show a toast after canceling current toast. */
public static void toast(CharSequence text, int duration) {
if (toast != null) {
toast.cancel();
toast = null;
}
if (appContext != null) {
toast = Toast.makeText(appContext, text, duration);
toast.show();
}
}
}

View File

@@ -99,7 +99,7 @@ public class Speech {
toast = R.string.tts_failed_to_init;
}
if (toast != -1)
Toast.makeText(context, toast, Toast.LENGTH_LONG).show();
DroidFishApp.toast(toast, Toast.LENGTH_LONG);
}
});
}

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Locale;
import org.petero.droidfish.DroidFish;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.R;
import org.petero.droidfish.Util;
import org.petero.droidfish.Util.MaterialDiff;
@@ -367,7 +368,7 @@ public class EditBoard extends Activity {
try {
startActivityForResult(i, RESULT_GET_FEN);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
DroidFishApp.toast(e.getMessage(), Toast.LENGTH_LONG);
}
}
}
@@ -607,7 +608,7 @@ public class EditBoard extends Activity {
cb.pos.halfMoveClock = halfClock;
cb.pos.fullMoveCounter = fullCount;
} catch (NumberFormatException nfe) {
Toast.makeText(getApplicationContext(), R.string.invalid_number_format, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.invalid_number_format, Toast.LENGTH_SHORT);
}
}
};
@@ -645,7 +646,7 @@ public class EditBoard extends Activity {
} catch (ChessParseError e) {
if (e.pos != null)
cb.setPosition(e.pos);
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
DroidFishApp.toast(getParseErrString(e), Toast.LENGTH_SHORT);
}
setSelection(-1);
checkValidAndUpdateMaterialDiff();

View File

@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Locale;
import org.petero.droidfish.ColorTheme;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.ObjectCache;
import org.petero.droidfish.R;
import org.petero.droidfish.Util;
@@ -133,8 +134,7 @@ public class EditPGN extends ListActivity {
boolean next = action.equals("org.petero.droidfish.loadFileNextGame");
final int loadItem = defaultItem + (next ? 1 : -1);
if (loadItem < 0) {
Toast.makeText(getApplicationContext(), R.string.no_prev_game,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_prev_game, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {
@@ -145,8 +145,7 @@ public class EditPGN extends ListActivity {
runOnUiThread(new Runnable() {
public void run() {
if (loadItem >= gamesInFile.size()) {
Toast.makeText(getApplicationContext(), R.string.no_next_game,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_next_game, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {
@@ -166,7 +165,7 @@ public class EditPGN extends ListActivity {
boolean silent = i.getBooleanExtra("org.petero.droidfish.silent", false);
if (silent) { // Silently append to file
PGNFile pgnFile2 = new PGNFile(fileName);
pgnFile2.appendPGN(pgnToSave, null);
pgnFile2.appendPGN(pgnToSave);
} else {
pgnFile = new PGNFile(fileName);
showDialog(PROGRESS_DIALOG);
@@ -181,7 +180,7 @@ public class EditPGN extends ListActivity {
setResult(RESULT_CANCELED);
finish();
} else if (gamesInFile.size() == 0) {
pgnFile.appendPGN(pgnToSave, getApplicationContext());
pgnFile.appendPGN(pgnToSave);
finish();
} else {
lpgn.showList();
@@ -391,7 +390,7 @@ public class EditPGN extends ListActivity {
default:
finish(); return;
}
pgnFile.replacePGN(pgnToSave, giToReplace, getApplicationContext());
pgnFile.replacePGN(pgnToSave, giToReplace);
finish();
}
});
@@ -437,16 +436,14 @@ public class EditPGN extends ListActivity {
case OUT_OF_MEMORY:
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), R.string.file_too_large,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.file_too_large, Toast.LENGTH_SHORT);
}
});
break;
case NOT_PGN:
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), R.string.not_a_pgn_file,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.not_a_pgn_file, Toast.LENGTH_SHORT);
}
});
break;
@@ -478,7 +475,7 @@ public class EditPGN extends ListActivity {
}
private final void deleteGame(GameInfo gi) {
if (pgnFile.deleteGame(gi, getApplicationContext(), gamesInFile)) {
if (pgnFile.deleteGame(gi, gamesInFile)) {
ListView lv = getListView();
int pos = lv.pointToPosition(0,0);
aa = new ArrayAdapter<GameInfo>(this, R.layout.select_game_list_item, gamesInFile);

View File

@@ -24,6 +24,7 @@ import java.util.concurrent.CountDownLatch;
import org.petero.droidfish.ChessBoardPlay;
import org.petero.droidfish.ColorTheme;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.R;
import org.petero.droidfish.Util;
import org.petero.droidfish.activities.FENFile.FenInfo;
@@ -135,8 +136,7 @@ public class LoadFEN extends ListActivity {
boolean next = action.equals("org.petero.droidfish.loadNextFen");
final int loadItem = defaultItem + (next ? 1 : -1);
if (loadItem < 0) {
Toast.makeText(getApplicationContext(), R.string.no_prev_fen,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_prev_fen, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {
@@ -147,8 +147,7 @@ public class LoadFEN extends ListActivity {
runOnUiThread(new Runnable() {
public void run() {
if (loadItem >= fensInFile.size()) {
Toast.makeText(getApplicationContext(), R.string.no_next_fen,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_next_fen, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {
@@ -338,8 +337,7 @@ public class LoadFEN extends ListActivity {
if (p.first == FenInfoResult.OUT_OF_MEMORY) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), R.string.file_too_large,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.file_too_large, Toast.LENGTH_SHORT);
}
});
}

View File

@@ -24,6 +24,7 @@ import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import org.petero.droidfish.ColorTheme;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.ObjectCache;
import org.petero.droidfish.R;
import org.petero.droidfish.Util;
@@ -167,8 +168,7 @@ public class LoadScid extends ListActivity {
boolean next = action.equals("org.petero.droidfish.loadScidNextGame");
final int loadItem = defaultItem + (next ? 1 : -1);
if (loadItem < 0) {
Toast.makeText(getApplicationContext(), R.string.no_prev_game,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_prev_game, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {
@@ -180,8 +180,7 @@ public class LoadScid extends ListActivity {
runOnUiThread(new Runnable() {
public void run() {
if (loadItem >= gamesInFile.size()) {
Toast.makeText(getApplicationContext(), R.string.no_next_game,
Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.no_next_game, Toast.LENGTH_SHORT);
setResult(RESULT_CANCELED);
finish();
} else {

View File

@@ -26,6 +26,7 @@ import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.R;
import org.petero.droidfish.gamelogic.Pair;
@@ -398,20 +399,19 @@ public class PGNFile {
}
/** Append PGN to the end of this PGN file. */
public final void appendPGN(String pgn, Context context) {
public final void appendPGN(String pgn) {
try {
mkDirs();
FileWriter fw = new FileWriter(fileName, true);
fw.write(pgn);
fw.close();
Toast.makeText(context, R.string.game_saved, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
} catch (IOException e) {
if (context != null)
Toast.makeText(context, R.string.failed_to_save_game, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.failed_to_save_game, Toast.LENGTH_SHORT);
}
}
final boolean deleteGame(GameInfo gi, Context context, ArrayList<GameInfo> gamesInFile) {
final boolean deleteGame(GameInfo gi, ArrayList<GameInfo> gamesInFile) {
try {
File tmpFile = new File(fileName + ".tmp_delete");
RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
@@ -439,13 +439,12 @@ public class PGNFile {
}
return true;
} catch (IOException e) {
if (context != null)
Toast.makeText(context, R.string.failed_to_delete_game, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.failed_to_delete_game, Toast.LENGTH_SHORT);
}
return false;
}
final boolean replacePGN(String pgnToSave, GameInfo gi, Context context) {
final boolean replacePGN(String pgnToSave, GameInfo gi) {
try {
File tmpFile = new File(fileName + ".tmp_delete");
RandomAccessFile fileReader = new RandomAccessFile(fileName, "r");
@@ -458,11 +457,10 @@ public class PGNFile {
fileWriter.close();
if (!tmpFile.renameTo(fileName))
throw new IOException();
Toast.makeText(context, R.string.game_saved, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.game_saved, Toast.LENGTH_SHORT);
return true;
} catch (IOException e) {
if (context != null)
Toast.makeText(context, R.string.failed_to_save_game, Toast.LENGTH_SHORT).show();
DroidFishApp.toast(R.string.failed_to_save_game, Toast.LENGTH_SHORT);
}
return false;
}

View File

@@ -20,6 +20,7 @@ package org.petero.droidfish.activities;
import java.util.Locale;
import org.petero.droidfish.DroidFishApp;
import org.petero.droidfish.R;
import android.app.AlertDialog;
@@ -200,8 +201,7 @@ public class SeekBarPreference extends Preference
if ("stockfish".equals(engine)) {
showStrengthHint = false;
if (getKey().equals("strength"))
Toast.makeText(getContext(), R.string.strength_cuckoo_hint,
Toast.LENGTH_LONG).show();
DroidFishApp.toast(R.string.strength_cuckoo_hint, Toast.LENGTH_LONG);
}
}
}