use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.
the class GamesBackupSerializer method serializeAsRawXml.
private static String serializeAsRawXml(GamesBackup gamesBackup) {
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("", Tag.GamesBackup.name());
addTag(serializer, Tag.gameCount, gamesBackup.getGameCount());
addTag(serializer, Tag.version, gamesBackup.getVersion());
addTag(serializer, Tag.automatic, gamesBackup.isAutomatic());
addTag(serializer, Tag.backupFilename, gamesBackup.getFilename());
addTag(serializer, Tag.dateBackupSaved, gamesBackup.getDateSaved());
serializer.startTag("", Tag.Games.name());
for (Game game : gamesBackup.getGames()) {
serializer.startTag("", Tag.Game.name());
addTag(serializer, Tag.dateGameSaved, game.getDateSaved());
addTag(serializer, Tag.dateGameStarted, game.getDateStarted());
addTag(serializer, Tag.gameName, game.getName());
serializer.startTag("", Tag.PlayerScores.name());
for (PlayerScore playerScore : game.getPlayerScores()) {
serializer.startTag("", Tag.PlayerScore.name());
addTag(serializer, Tag.playerName, playerScore.getName());
addTag(serializer, Tag.score, playerScore.getScore());
addTag(serializer, Tag.playerNumber, playerScore.getPlayerNumber());
Pair<String, String> historyAsStrings = Delta.toJoinedStrings(playerScore.getHistory());
addTag(serializer, Tag.history, historyAsStrings.getFirst());
addTag(serializer, Tag.historyTimestamps, historyAsStrings.getSecond());
addTag(serializer, Tag.lastUpdate, Long.toString(playerScore.getLastUpdate()));
addTag(serializer, Tag.color, PlayerColor.serialize(playerScore.getPlayerColor()));
serializer.endTag("", Tag.PlayerScore.name());
}
serializer.endTag("", Tag.PlayerScores.name());
serializer.endTag("", Tag.Game.name());
}
serializer.endTag("", Tag.Games.name());
serializer.endTag("", Tag.GamesBackup.name());
serializer.endDocument();
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.
the class SerializationTest method createRandomGamesBackup.
private GamesBackup createRandomGamesBackup() {
GamesBackup gamesBackup = new GamesBackup();
gamesBackup.setGames(new ArrayList<Game>());
gamesBackup.setDateSaved(2437934297L);
gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
int numGames = random.nextInt(10) + 2;
for (int i = 0; i < numGames; i++) {
Game game = new Game();
game.setDateSaved(random.nextLong());
game.setDateStarted(random.nextLong());
game.setName(Long.toString(random.nextLong(), 16));
game.setPlayerScores(createRandomPlayerScores());
gamesBackup.getGames().add(game);
}
gamesBackup.setGameCount(gamesBackup.getGames().size());
return gamesBackup;
}
use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.
the class MainActivity method copyGame.
private void copyGame(Game game, final boolean resetScores) {
final Game newGame = game.makeCleanCopy();
if (resetScores) {
for (PlayerScore playerScore : newGame.getPlayerScores()) {
playerScore.setScore(PreferenceHelper.getIntPreference(R.string.CONSTANT_pref_initial_score, R.string.CONSTANT_pref_initial_score_default, this));
playerScore.setHistory(new ArrayList<Delta>());
}
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
dbHelper.saveGame(newGame);
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
onNewGameCreated(newGame);
ToastHelper.showShort(MainActivity.this, resetScores ? R.string.toast_rematch_created : R.string.toast_game_copied);
}
}.execute((Void) null);
}
use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.
the class MainActivity method exportToSpreadsheet.
private void exportToSpreadsheet(final List<Integer> gameIds) {
final ProgressDialog progressDialog = showProgressDialog(R.string.text_loading_generic, // plus one for saving the spreadsheet itself
gameIds.size() + 1);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
List<Game> games = new ArrayList<Game>();
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
for (Integer gameId : gameIds) {
games.add(dbHelper.findGameById(gameId));
publishProgress((Void) null);
}
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
String filename = SdcardHelper.createSpreadsheetFilename();
SdcardHelper.saveSpreadsheet(filename, games, MainActivity.this);
publishProgress((Void) null);
return filename;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progressDialog.incrementProgressBy(1);
}
@Override
protected void onPostExecute(String filename) {
super.onPostExecute(filename);
progressDialog.dismiss();
sendSpreadsheetAsAttachment(filename, gameIds.size());
}
}.execute((Void) null);
}
use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.
the class PeriodicAutomaticBackupService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
String filename = SdcardHelper.createBackupFilename(Format.GZIP);
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(this);
List<Game> games = dbHelper.findAllGames();
log.i("Beginning periodic automatic backup of %d saved KeepScore games...", games.size());
GamesBackup gamesBackup = new GamesBackup();
gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
gamesBackup.setDateSaved(System.currentTimeMillis());
gamesBackup.setGameCount(games.size());
gamesBackup.setAutomatic(true);
gamesBackup.setFilename(filename);
gamesBackup.setGames(games);
String xmlData = GamesBackupSerializer.serialize(gamesBackup);
SdcardHelper.save(filename, Format.GZIP, Location.Backups, xmlData);
log.i("KeepScore backed up %d games to \"%s\".", games.size(), filename);
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
}
Aggregations