Search in sources :

Example 1 with LoadGamesBackupResult

use of com.nolanlawson.keepscore.data.LoadGamesBackupResult 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 LoadGamesBackupResult

use of com.nolanlawson.keepscore.data.LoadGamesBackupResult in project KeepScore by nolanlawson.

the class MainActivity method loadBackup.

private void loadBackup(final GamesBackupSummary summary, final Uri uri, final Format format) {
    final ProgressDialog progressDialog = showProgressDialog(R.string.text_loading, summary.getGameCount());
    new AsyncTask<Void, Void, LoadGamesBackupResult>() {

        @Override
        protected LoadGamesBackupResult doInBackground(Void... params) {
            return loadBackupInBackground(uri, format, new Runnable() {

                @Override
                public void run() {
                    publishProgress((Void) null);
                }
            });
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            progressDialog.incrementProgressBy(1);
        }

        @Override
        protected void onPostExecute(LoadGamesBackupResult result) {
            super.onPostExecute(result);
            onReceiveLoadGamesBackupResult(result);
            progressDialog.dismiss();
        }
    }.execute((Void) null);
}
Also used : ProgressDialog(android.app.ProgressDialog) LoadGamesBackupResult(com.nolanlawson.keepscore.data.LoadGamesBackupResult)

Aggregations

LoadGamesBackupResult (com.nolanlawson.keepscore.data.LoadGamesBackupResult)2 ProgressDialog (android.app.ProgressDialog)1 Game (com.nolanlawson.keepscore.db.Game)1 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)1 GamesBackup (com.nolanlawson.keepscore.serialization.GamesBackup)1 ArrayList (java.util.ArrayList)1