Search in sources :

Example 1 with GamesBackup

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;
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) ArrayList(java.util.ArrayList) LoadGamesBackupResult(com.nolanlawson.keepscore.data.LoadGamesBackupResult) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Example 2 with GamesBackup

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);
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) List(java.util.List) ArrayList(java.util.ArrayList) ProgressDialog(android.app.ProgressDialog) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Example 3 with GamesBackup

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);
}
Also used : GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Example 4 with 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;
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Example 5 with 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();
        }
    }
}
Also used : Game(com.nolanlawson.keepscore.db.Game) GameDBHelper(com.nolanlawson.keepscore.db.GameDBHelper) GamesBackup(com.nolanlawson.keepscore.serialization.GamesBackup)

Aggregations

GamesBackup (com.nolanlawson.keepscore.serialization.GamesBackup)9 Game (com.nolanlawson.keepscore.db.Game)4 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)3 ArrayList (java.util.ArrayList)2 ProgressDialog (android.app.ProgressDialog)1 LoadGamesBackupResult (com.nolanlawson.keepscore.data.LoadGamesBackupResult)1 CustomPlayerColor (com.nolanlawson.keepscore.helper.PlayerColor.CustomPlayerColor)1 List (java.util.List)1