use of com.nolanlawson.keepscore.serialization.GamesBackup in project KeepScore by nolanlawson.
the class MainActivity method loadBackupInBackground.
private LoadGamesBackupResult loadBackupInBackground(Uri uri, Format format, Runnable onProgress) {
// use the start date as a unique identifier; it's a
// millisecond-timestamp, so it should work
GamesBackup gamesBackup;
try {
String xmlData = SdcardHelper.open(uri, format, getContentResolver());
gamesBackup = GamesBackupSerializer.deserialize(xmlData);
} catch (Exception e) {
log.e(e, "unexpected");
return null;
}
List<Game> loadedGames = new ArrayList<Game>();
int numFound = 0, numDuplicates = 0;
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(this);
for (Game game : gamesBackup.getGames()) {
numFound++;
if (dbHelper.existsByDateStarted(game.getDateStarted())) {
numDuplicates++;
} else {
// don't update 'dateSaved'
dbHelper.saveGame(game, false);
// value - keep original
loadedGames.add(game);
}
onProgress.run();
}
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
// this is just for the summary message we show the user
LoadGamesBackupResult result = new LoadGamesBackupResult();
result.setLoadedGames(loadedGames);
result.setNumDuplicates(numDuplicates);
result.setNumFound(numFound);
// Pre-version 3, we don't have the filename in the deserialized XML
String filenameToDisplay = gamesBackup.getFilename() != null ? gamesBackup.getFilename() : uri.getLastPathSegment();
result.setFilename(filenameToDisplay);
return result;
}
use of com.nolanlawson.keepscore.serialization.GamesBackup in project KeepScore by nolanlawson.
the class MainActivity method saveBackup.
private void saveBackup(final Format format, final Location location, final List<Integer> gameIds, final Callback<String> onSuccessWithFilename) {
if (!SdcardHelper.isAvailable()) {
ToastHelper.showLong(this, R.string.toast_no_sdcard);
return;
}
final String filename = SdcardHelper.createBackupFilename(format);
final ProgressDialog progressDialog = showProgressDialog(R.string.text_saving, gameIds.size());
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
List<Game> games = new ArrayList<Game>();
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
for (Integer gameId : gameIds) {
Game game = dbHelper.findGameById(gameId);
games.add(game);
publishProgress((Void) null);
}
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
GamesBackup gamesBackup = new GamesBackup();
gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
gamesBackup.setDateSaved(System.currentTimeMillis());
gamesBackup.setAutomatic(false);
gamesBackup.setGameCount(games.size());
gamesBackup.setGames(games);
gamesBackup.setFilename(filename);
String xmlData = GamesBackupSerializer.serialize(gamesBackup);
return SdcardHelper.save(filename, format, location, xmlData);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
onSuccessWithFilename.onCallback(filename);
} else {
ToastHelper.showLong(MainActivity.this, R.string.toast_save_backup_failed);
}
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progressDialog.incrementProgressBy(1);
}
}.execute((Void) null);
}
use of com.nolanlawson.keepscore.serialization.GamesBackup in project KeepScore by nolanlawson.
the class SerializationTest method testNoGames.
public void testNoGames() {
GamesBackup gamesBackup = createRandomGamesBackup();
gamesBackup.setGames(Collections.<Game>emptyList());
testGamesBackup(gamesBackup);
}
use of com.nolanlawson.keepscore.serialization.GamesBackup 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.serialization.GamesBackup 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